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