Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright (C) 2009 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 #ifndef ANDROID_TTS_H
     17 #define ANDROID_TTS_H
     18 
     19 // This header defines the interface used by the Android platform
     20 // to access Text-To-Speech functionality in shared libraries that implement
     21 // speech synthesis and the management of resources associated with the
     22 // synthesis.
     23 
     24 // The shared library must contain a function named "android_getTtsEngine"
     25 // that returns an 'android_tts_engine_t' instance.
     26 
     27 #ifdef __cplusplus
     28 extern "C" {
     29 #endif
     30 
     31 #include <stdint.h>
     32 
     33 #define ANDROID_TTS_ENGINE_PROPERTY_CONFIG "engineConfig"
     34 #define ANDROID_TTS_ENGINE_PROPERTY_PITCH  "pitch"
     35 #define ANDROID_TTS_ENGINE_PROPERTY_RATE   "rate"
     36 #define ANDROID_TTS_ENGINE_PROPERTY_VOLUME "volume"
     37 
     38 typedef enum {
     39     ANDROID_TTS_SUCCESS                 = 0,
     40     ANDROID_TTS_FAILURE                 = -1,
     41     ANDROID_TTS_FEATURE_UNSUPPORTED     = -2,
     42     ANDROID_TTS_VALUE_INVALID           = -3,
     43     ANDROID_TTS_PROPERTY_UNSUPPORTED    = -4,
     44     ANDROID_TTS_PROPERTY_SIZE_TOO_SMALL = -5,
     45     ANDROID_TTS_MISSING_RESOURCES       = -6
     46 } android_tts_result_t;
     47 
     48 typedef enum {
     49     ANDROID_TTS_LANG_COUNTRY_VAR_AVAILABLE = 2,
     50     ANDROID_TTS_LANG_COUNTRY_AVAILABLE    = 1,
     51     ANDROID_TTS_LANG_AVAILABLE            = 0,
     52     ANDROID_TTS_LANG_MISSING_DATA         = -1,
     53     ANDROID_TTS_LANG_NOT_SUPPORTED        = -2
     54 } android_tts_support_result_t;
     55 
     56 typedef enum {
     57     ANDROID_TTS_SYNTH_DONE              = 0,
     58     ANDROID_TTS_SYNTH_PENDING           = 1
     59 } android_tts_synth_status_t;
     60 
     61 typedef enum {
     62     ANDROID_TTS_CALLBACK_HALT           = 0,
     63     ANDROID_TTS_CALLBACK_CONTINUE       = 1
     64 } android_tts_callback_status_t;
     65 
     66 // Supported audio formats
     67 typedef enum {
     68     ANDROID_TTS_AUDIO_FORMAT_INVALID    = -1,
     69     ANDROID_TTS_AUDIO_FORMAT_DEFAULT    = 0,
     70     ANDROID_TTS_AUDIO_FORMAT_PCM_16_BIT = 1,
     71     ANDROID_TTS_AUDIO_FORMAT_PCM_8_BIT  = 2,
     72 } android_tts_audio_format_t;
     73 
     74 
     75 /* An android_tts_engine_t object can be anything, but must have,
     76  * as its first field, a pointer to a table of functions.
     77  *
     78  * See the full definition of struct android_tts_engine_t_funcs_t
     79  * below for details.
     80  */
     81 typedef struct android_tts_engine_funcs_t  android_tts_engine_funcs_t;
     82 
     83 typedef struct {
     84     android_tts_engine_funcs_t *funcs;
     85 } android_tts_engine_t;
     86 
     87 /* This function must be located in the TTS Engine shared library
     88  * and must return the address of an android_tts_engine_t library.
     89  */
     90 extern android_tts_engine_t *android_getTtsEngine();
     91 
     92 /* Including the old version for legacy support (Froyo compatibility).
     93  * This should return the same thing as android_getTtsEngine.
     94  */
     95 android_tts_engine_t *getTtsEngine();
     96 
     97 // A callback type used to notify the framework of new synthetized
     98 // audio samples, status will be SYNTH_DONE for the last sample of
     99 // the last request, of SYNTH_PENDING otherwise.
    100 //
    101 // This is passed by the framework to the engine through the
    102 // 'engine_init' function (see below).
    103 //
    104 // The callback for synthesis completed takes:
    105 // @param [inout] void *&       - The userdata pointer set in the original
    106 //                                 synth call
    107 // @param [in]    uint32_t      - Track sampling rate in Hz
    108 // @param [in]    uint32_t      - The audio format
    109 // @param [in]    int           - The number of channels
    110 // @param [inout] int8_t *&     - A buffer of audio data only valid during the
    111 //                                execution of the callback
    112 // @param [inout] size_t  &     - The size of the buffer
    113 // @param [in] tts_synth_status - indicate whether the synthesis is done, or
    114 //                                 if more data is to be synthesized.
    115 // @return TTS_CALLBACK_HALT to indicate the synthesis must stop,
    116 //         TTS_CALLBACK_CONTINUE to indicate the synthesis must continue if
    117 //            there is more data to produce.
    118 typedef android_tts_callback_status_t (*android_tts_synth_cb_t)
    119             (void **pUserData,
    120              uint32_t trackSamplingHz,
    121              android_tts_audio_format_t audioFormat,
    122              int channelCount,
    123              int8_t **pAudioBuffer,
    124              size_t *pBufferSize,
    125              android_tts_synth_status_t status);
    126 
    127 
    128 // The table of function pointers that the android_tts_engine_t must point to.
    129 // Note that each of these functions will take a handle to the engine itself
    130 // as their first parameter.
    131 //
    132 
    133 struct android_tts_engine_funcs_t {
    134     // reserved fields, ignored by the framework
    135     // they must be placed here to ensure binary compatibility
    136     // of legacy binary plugins.
    137     void *reserved[2];
    138 
    139     // Initialize the TTS engine and returns whether initialization succeeded.
    140     // @param synthDoneCBPtr synthesis callback function pointer
    141     // @return TTS_SUCCESS, or TTS_FAILURE
    142     android_tts_result_t (*init)
    143             (void *engine,
    144              android_tts_synth_cb_t synthDonePtr,
    145              const char *engineConfig);
    146 
    147     // Shut down the TTS engine and releases all associated resources.
    148     // @return TTS_SUCCESS, or TTS_FAILURE
    149     android_tts_result_t (*shutdown)
    150             (void *engine);
    151 
    152     // Interrupt synthesis and flushes any synthesized data that hasn't been
    153     // output yet. This will block until callbacks underway are completed.
    154     // @return TTS_SUCCESS, or TTS_FAILURE
    155     android_tts_result_t (*stop)
    156             (void *engine);
    157 
    158     // Returns the level of support for the language, country and variant.
    159     // @return TTS_LANG_COUNTRY_VAR_AVAILABLE if the language, country and variant are supported,
    160     //            and the corresponding resources are correctly installed
    161     //         TTS_LANG_COUNTRY_AVAILABLE if the language and country are supported and the
    162     //             corresponding resources are correctly installed, but there is no match for
    163     //             the specified variant
    164     //         TTS_LANG_AVAILABLE if the language is supported and the
    165     //             corresponding resources are correctly installed, but there is no match for
    166     //             the specified country and variant
    167     //         TTS_LANG_MISSING_DATA if the required resources to provide any level of support
    168     //             for the language are not correctly installed
    169     //         TTS_LANG_NOT_SUPPORTED if the language is not supported by the TTS engine.
    170     android_tts_support_result_t (*isLanguageAvailable)
    171             (void *engine,
    172              const char *lang,
    173              const char *country,
    174              const char *variant);
    175 
    176     // Load the resources associated with the specified language. The loaded
    177     // language will only be used once a call to setLanguage() with the same
    178     // language value is issued. Language and country values are coded according to the ISO three
    179     // letter codes for languages and countries, as can be retrieved from a java.util.Locale
    180     // instance. The variant value is encoded as the variant string retrieved from a
    181     // java.util.Locale instance built with that variant data.
    182     // @param lang pointer to the ISO three letter code for the language
    183     // @param country pointer to the ISO three letter code for the country
    184     // @param variant pointer to the variant code
    185     // @return TTS_SUCCESS, or TTS_FAILURE
    186     android_tts_result_t (*loadLanguage)
    187             (void *engine,
    188              const char *lang,
    189              const char *country,
    190              const char *variant);
    191 
    192     // Load the resources associated with the specified language, country and Locale variant.
    193     // The loaded language will only be used once a call to setLanguageFromLocale() with the same
    194     // language value is issued. Language and country values are coded according to the ISO three
    195     // letter codes for languages and countries, as can be retrieved from a java.util.Locale
    196     // instance. The variant value is encoded as the variant string retrieved from a
    197     // java.util.Locale instance built with that variant data.
    198     // @param lang pointer to the ISO three letter code for the language
    199     // @param country pointer to the ISO three letter code for the country
    200     // @param variant pointer to the variant code
    201     // @return TTS_SUCCESS, or TTS_FAILURE
    202     android_tts_result_t (*setLanguage)
    203             (void *engine,
    204              const char *lang,
    205              const char *country,
    206              const char *variant);
    207 
    208     // Retrieve the currently set language, country and variant, or empty strings if none of
    209     // parameters have been set. Language and country are represented by their 3-letter ISO code
    210     // @param[out]   pointer to the retrieved 3-letter code language value
    211     // @param[out]   pointer to the retrieved 3-letter code country value
    212     // @param[out]   pointer to the retrieved variant value
    213     // @return TTS_SUCCESS, or TTS_FAILURE
    214     android_tts_result_t (*getLanguage)
    215             (void *engine,
    216              char *language,
    217              char *country,
    218              char *variant);
    219 
    220     // Notifies the engine what audio parameters should be used for the synthesis.
    221     // This is meant to be used as a hint, the engine implementation will set the output values
    222     // to those of the synthesis format, based on a given hint.
    223     // @param[inout] encoding in: the desired audio sample format
    224     //                         out: the format used by the TTS engine
    225     // @param[inout] rate in: the desired audio sample rate
    226     //                         out: the sample rate used by the TTS engine
    227     // @param[inout] channels in: the desired number of audio channels
    228     //                         out: the number of channels used by the TTS engine
    229     // @return TTS_SUCCESS, or TTS_FAILURE
    230     android_tts_result_t (*setAudioFormat)
    231             (void *engine,
    232              android_tts_audio_format_t* pEncoding,
    233              uint32_t* pRate,
    234              int* pChannels);
    235 
    236     // Set a property for the the TTS engine
    237     // "size" is the maximum size of "value" for properties "property"
    238     // @param property pointer to the property name
    239     // @param value    pointer to the property value
    240     // @param size     maximum size required to store this type of property
    241     // @return         TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS, or TTS_FAILURE,
    242     //                  or TTS_VALUE_INVALID
    243     android_tts_result_t (*setProperty)
    244             (void *engine,
    245              const char *property,
    246              const char *value,
    247              const size_t size);
    248 
    249     // Retrieve a property from the TTS engine
    250     // @param        property pointer to the property name
    251     // @param[out]   value    pointer to the retrieved language value
    252     // @param[inout] iosize   in: stores the size available to store the
    253     //                          property value.
    254     //                        out: stores the size required to hold the language
    255     //                          value if getLanguage() returned
    256     //                          TTS_PROPERTY_SIZE_TOO_SMALL, unchanged otherwise
    257     // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS,
    258     //         or TTS_PROPERTY_SIZE_TOO_SMALL
    259     android_tts_result_t (*getProperty)
    260             (void *engine,
    261              const char *property,
    262              char *value,
    263              size_t *iosize);
    264 
    265     // Synthesize the text.
    266     // As the synthesis is performed, the engine invokes the callback to notify
    267     // the TTS framework that it has filled the given buffer, and indicates how
    268     // many bytes it wrote. The callback is called repeatedly until the engine
    269     // has generated all the audio data corresponding to the text.
    270     // Note about the format of the input: the text parameter may use the
    271     // following elements
    272     // and their respective attributes as defined in the SSML 1.0 specification:
    273     //    * lang
    274     //    * say-as:
    275     //          o interpret-as
    276     //    * phoneme
    277     //    * voice:
    278     //          o gender,
    279     //          o age,
    280     //          o variant,
    281     //          o name
    282     //    * emphasis
    283     //    * break:
    284     //          o strength,
    285     //          o time
    286     //    * prosody:
    287     //          o pitch,
    288     //          o contour,
    289     //          o range,
    290     //          o rate,
    291     //          o duration,
    292     //          o volume
    293     //    * mark
    294     // Differences between this text format and SSML are:
    295     //    * full SSML documents are not supported
    296     //    * namespaces are not supported
    297     // Text is coded in UTF-8.
    298     // @param text      the UTF-8 text to synthesize
    299     // @param userdata  pointer to be returned when the call is invoked
    300     // @param buffer    the location where the synthesized data must be written
    301     // @param bufferSize the number of bytes that can be written in buffer
    302     // @return          TTS_SUCCESS or TTS_FAILURE
    303     android_tts_result_t (*synthesizeText)
    304             (void *engine,
    305              const char *text,
    306              int8_t *buffer,
    307              size_t bufferSize,
    308              void *userdata);
    309 };
    310 
    311 #ifdef __cplusplus
    312 }
    313 #endif
    314 
    315 #endif /* ANDROID_TTS_H */
    316