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