TTS Sample Overview

Mobile native

The TTS sample application demonstrates how you can use the TTS (Text-To-Speech) feature in your application or service.

The following figure illustrates the main view of the TTS sample application.

Figure: Application view

TTS sample application view

In the application, set the language and voice type of text, and add the text which you want to be read aloud. Because the application stores the added text information, you can view the number of added texts and a text list. To request play, pause or stop, use the applicable b/sites/default/files/dev_guide/org.tizen.guidesuttons.

Implementation

To implement the TTS application:

  1. Create the layout for the application.

    This sample application uses native UI components (including a genlist and entry), and the appdata_s structure and some global values (including the GList *g_tts_text_list) to manage the UI component implementation. However, this topic concentrates on how to use the TTS API and not the UI components. For more information on UI component usage, see the UI Components sample application.

  2. Initialize TTS.

    At the application startup, the init_tts() function creates the TTS handle and sets the callback functions for the state change and utterance completed events (for additional callbacks, see the TTS API). Then the application obtains the default TTS voice (language and voice type) and connects to the TTS service.

    The __tts_utterance_completed_cb() function is called when the reading for each added text is finished. At that point, the application removes the text from the added text list.

    static void 
    __tts_state_changed_cb(tts_h tts, tts_state_e previous, tts_state_e current, void* user_data) {}
    
    static void 
    __tts_utterance_completed_cb(tts_h tts, int utt_id, void* user_data) {}
    
    static int 
    init_tts(appdata_s *ad)
    {
       // Create TTS handle
       if (0 != tts_create(&g_tts))
       {
          return -1;
       }
       // Set state changed callback
       if (0 != tts_set_state_changed_cb(g_tts, __tts_state_changed_cb, ad))
       {
          tts_destroy(g_tts);
    
          return -1;
       }
       // Set utterance completed callback
       if (0 != tts_set_utterance_completed_cb(g_tts, __tts_utterance_completed_cb, ad))
       {
          tts_destroy(g_tts);
    
          return -1;
       }
       // Get default voice
       char *language = NULL;
       int voice_type;
       if (0 != tts_get_default_voice(g_tts, &language, &voice_type))
       {
          tts_destroy(g_tts);
    
          return -1;
       }
       if (NULL != language)
       {
          free(language);
       }
       // Connect to TTS service
       if (0 != tts_prepare(g_tts))
       {
          tts_destroy(g_tts);
    
          return -1;
       }
    
       return 0;
    }
    

    When the TTS feature is no longer needed, call the deinit_tts() function to disconnect the service and destroy the handle.

    If you want to only disconnect the service but not destroy the handle, use the tts_unprepare() function.

    static voice 
    deinit_tts(appdata_s *ad)
    {
       // Destroy TTS handle (service disconnect is included)
       if (0 != tts_destroy(g_tts))
       {
          // Do something
       }
    }
    
  3. Get the supported voice list.

    The application shows the supported TTS voice list when the Select Voice item is clicked. The __voice_item_selected_cb() function is the callback for the click event, and the __show_voice_list() function calls the tts_foreach_supported_voices() function for get the supported voice list.

    static bool 
    __tts_supported_voice_cb(const char* engine_id, const char* language, int voice_type, void* user_data)
    {
       // If you want to continue to get next supported voice
       return true;
       // If you want to stop
       return false;
    }
    
    static void 
    __show_voice_list(appdata_s *ad)
    {
       if (0 != tts_foreach_supported_voices(g_tts, __tts_supported_voice_cb, ad))
       {
          // Do something
       }
    }
    
    static void 
    __voice_item_selected_cb(void *data, Evas_Object *obj, void *event_info)
    {
       appdata_s *ad = (appdata_s *)data;
       __show_voice_list(ad);
    }
    
  4. Add the text with the current voice.

    When the user clicks Add text, the __add_text_item_selected_cb() function is called. In this function, the tts_add_text() function adds the text from the input entry with the current selected voice. In this application, the added text is stored with the GList to show the list.

    __add_text_item_selected_cb(void *data, Evas_Object *obj, void *event_info)
    {
       appdata_s *ad = (appdata_s *)data;
       char *text = elm_entry_entry_get(ad->entry);
       if (NULL != text)
       {
          int utt_id;
          if (0 != tts_add_text(g_tts, text, g_current_voice->language, g_current_voice->voice_type, TTS_SPEED_AUTO, &utt_id))
          {
             // Do something
          }
          else
          {
             // Do something
          }
       }
    }
    
  5. Play or pause the reading.

    The Play button text changes according to the current TTS state (when the state is TTS_STATE_PLAYING, the text changes to Play). The __play_pause_item_selected_cb() callback is called when the button is clicked. Within the callback, the application calls the TTS API based on the state.

    static void 
    __play_pause_item_selected_cb(void *data, Evas_Object *obj, void *event_info)
    {
       if (TTS_STATE_READY == g_current_state || TTS_STATE_PAUSED == g_current_state)
       {
          if (0 != tts_play(g_tts))
          {
             // Do something
          }
       }
       else if (TTS_STATE_PLAYING == g_current_state)
       {
          if (0 != tts_pause(g_tts))
          {
             // Do something
          }
       }
    }
    
  6. Stop the reading.

    The Stop button invokes the __stop_item_selected_cb() callback and the tts_stop() function is called inside the callback. The tts_stop() function removes all the added text, so the application also removes the stored text list.

    static void 
    __stop_item_selected_cb(void *data, Evas_Object *obj, void *event_info)
    {
       if (0 != tts_stop(g_tts))
       {
          // Do something
       }
    }