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