blob: 21cb73bc5527324b81df16c7ead99e8044d70d15 [file] [log] [blame]
Jean-Michel Trivic7104572009-05-21 15:32:11 -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#include <media/AudioSystem.h>
17
18// This header defines the interface used by the Android platform
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -070019// to access Text-To-Speech functionality in shared libraries that implement
20// speech synthesis and the management of resources associated with the
21// synthesis.
22// An example of the implementation of this interface can be found in
Jean-Michel Trivic7104572009-05-21 15:32:11 -070023// FIXME: add path+name to implementation of default TTS engine
24// Libraries implementing this interface are used in:
25// frameworks/base/tts/jni/android_tts_SpeechSynthesis.cpp
26
27namespace android {
28
Charles Chena17cef02009-06-05 13:58:33 -070029enum tts_synth_status {
30 TTS_SYNTH_DONE = 0,
31 TTS_SYNTH_PENDING = 1
32};
33
34enum tts_callback_status {
35 TTS_CALLBACK_HALT = 0,
36 TTS_CALLBACK_CONTINUE = 1
37};
38
Jean-Michel Trivic7104572009-05-21 15:32:11 -070039// The callback is used by the implementation of this interface to notify its
40// client, the Android TTS service, that the last requested synthesis has been
Charles Chena17cef02009-06-05 13:58:33 -070041// completed. // TODO reword
Jean-Michel Trivic7104572009-05-21 15:32:11 -070042// The callback for synthesis completed takes:
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -070043// @param [inout] void *& - The userdata pointer set in the original
44// synth call
45// @param [in] uint32_t - Track sampling rate in Hz
46// @param [in] audio_format - The AudioSystem::audio_format enum
47// @param [in] int - The number of channels
48// @param [inout] int8_t *& - A buffer of audio data only valid during the
49// execution of the callback
50// @param [inout] size_t & - The size of the buffer
51// @param [in] tts_synth_status - indicate whether the synthesis is done, or
52// if more data is to be synthesized.
53// @return TTS_CALLBACK_HALT to indicate the synthesis must stop,
54// TTS_CALLBACK_CONTINUE to indicate the synthesis must continue if
55// there is more data to produce.
56typedef tts_callback_status (synthDoneCB_t)(void *&, uint32_t,
57 AudioSystem::audio_format, int, int8_t *&, size_t&, tts_synth_status);
Jean-Michel Trivic7104572009-05-21 15:32:11 -070058
59class TtsEngine;
60extern "C" TtsEngine* getTtsEngine();
61
62enum tts_result {
63 TTS_SUCCESS = 0,
64 TTS_FAILURE = -1,
65 TTS_FEATURE_UNSUPPORTED = -2,
66 TTS_VALUE_INVALID = -3,
67 TTS_PROPERTY_UNSUPPORTED = -4,
Jean-Michel Trivi8d336f92009-05-28 11:11:25 -070068 TTS_PROPERTY_SIZE_TOO_SMALL = -5,
69 TTS_MISSING_RESOURCES = -6
Jean-Michel Trivic7104572009-05-21 15:32:11 -070070};
71
Jean-Michel Trivi0a90b692009-06-25 18:37:55 -070072enum tts_support_result {
73 TTS_LANG_COUNTRY_VAR_AVAILABLE = 2,
74 TTS_LANG_COUNTRY_AVAILABLE = 1,
75 TTS_LANG_AVAILABLE = 0,
76 TTS_LANG_MISSING_DATA = -1,
77 TTS_LANG_NOT_SUPPORTED = -2
78};
79
Jean-Michel Trivic7104572009-05-21 15:32:11 -070080class TtsEngine
81{
82public:
83 // Initialize the TTS engine and returns whether initialization succeeded.
84 // @param synthDoneCBPtr synthesis callback function pointer
85 // @return TTS_SUCCESS, or TTS_FAILURE
86 virtual tts_result init(synthDoneCB_t synthDoneCBPtr);
87
88 // Shut down the TTS engine and releases all associated resources.
89 // @return TTS_SUCCESS, or TTS_FAILURE
90 virtual tts_result shutdown();
91
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -070092 // Interrupt synthesis and flushes any synthesized data that hasn't been
93 // output yet. This will block until callbacks underway are completed.
Jean-Michel Trivic7104572009-05-21 15:32:11 -070094 // @return TTS_SUCCESS, or TTS_FAILURE
95 virtual tts_result stop();
96
Jean-Michel Trivi0a90b692009-06-25 18:37:55 -070097 // Returns the level of support for the language, country and variant.
98 // @return TTS_LANG_COUNTRY_VAR_AVAILABLE if the language, country and variant are supported,
99 // and the corresponding resources are correctly installed
100 // TTS_LANG_COUNTRY_AVAILABLE if the language and country are supported and the
101 // corresponding resources are correctly installed, but there is no match for
102 // the specified variant
103 // TTS_LANG_AVAILABLE if the language is supported and the
104 // corresponding resources are correctly installed, but there is no match for
105 // the specified country and variant
106 // TTS_LANG_MISSING_DATA if the required resources to provide any level of support
107 // for the language are not correctly installed
108 // TTS_LANG_NOT_SUPPORTED if the language is not supported by the TTS engine.
109 virtual tts_support_result isLanguageAvailable(const char *lang, const char *country,
110 const char *variant);
111
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700112 // Load the resources associated with the specified language. The loaded
113 // language will only be used once a call to setLanguage() with the same
Jean-Michel Trivi0a90b692009-06-25 18:37:55 -0700114 // language value is issued. Language and country values are coded according to the ISO three
115 // letter codes for languages and countries, as can be retrieved from a java.util.Locale
116 // instance. The variant value is encoded as the variant string retrieved from a
117 // java.util.Locale instance built with that variant data.
118 // @param lang pointer to the ISO three letter code for the language
119 // @param country pointer to the ISO three letter code for the country
120 // @param variant pointer to the variant code
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700121 // @return TTS_SUCCESS, or TTS_FAILURE
Jean-Michel Trivi0a90b692009-06-25 18:37:55 -0700122 virtual tts_result loadLanguage(const char *lang, const char *country, const char *variant);
Jean-Michel Trivibc275c82009-06-15 15:14:49 -0700123
124 // Load the resources associated with the specified language, country and Locale variant.
125 // The loaded language will only be used once a call to setLanguageFromLocale() with the same
126 // language value is issued. Language and country values are coded according to the ISO three
127 // letter codes for languages and countries, as can be retrieved from a java.util.Locale
128 // instance. The variant value is encoded as the variant string retrieved from a
129 // java.util.Locale instance built with that variant data.
130 // @param lang pointer to the ISO three letter code for the language
131 // @param country pointer to the ISO three letter code for the country
132 // @param variant pointer to the variant code
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700133 // @return TTS_SUCCESS, or TTS_FAILURE
Jean-Michel Trivibc275c82009-06-15 15:14:49 -0700134 virtual tts_result setLanguage(const char *lang, const char *country, const char *variant);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700135
Jean-Michel Trivi3f818882009-06-26 17:09:21 -0700136 // Retrieve the currently set language, country and variant, or empty strings if none of
137 // parameters have been set. Language and country are represented by their 3-letter ISO code
138 // @param[out] pointer to the retrieved 3-letter code language value
139 // @param[out] pointer to the retrieved 3-letter code country value
140 // @param[out] pointer to the retrieved variant value
141 // @return TTS_SUCCESS, or TTS_FAILURE
142 virtual tts_result getLanguage(char *language, char *country, char *variant);
143
144 // Notifies the engine what audio parameters should be used for the synthesis.
145 // This is meant to be used as a hint, the engine implementation will set the output values
146 // to those of the synthesis format, based on a given hint.
147 // @param[inout] encoding in: the desired audio sample format
148 // out: the format used by the TTS engine
149 // @param[inout] rate in: the desired audio sample rate
150 // out: the sample rate used by the TTS engine
151 // @param[inout] channels in: the desired number of audio channels
152 // out: the number of channels used by the TTS engine
153 // @return TTS_SUCCESS, or TTS_FAILURE
154 virtual tts_result setAudioFormat(AudioSystem::audio_format& encoding, uint32_t& rate,
155 int& channels);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700156
157 // Set a property for the the TTS engine
158 // "size" is the maximum size of "value" for properties "property"
159 // @param property pointer to the property name
160 // @param value pointer to the property value
161 // @param size maximum size required to store this type of property
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700162 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS, or TTS_FAILURE,
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700163 // or TTS_VALUE_INVALID
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700164 virtual tts_result setProperty(const char *property, const char *value,
165 const size_t size);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700166
167 // Retrieve a property from the TTS engine
168 // @param property pointer to the property name
169 // @param[out] value pointer to the retrieved language value
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700170 // @param[inout] iosize in: stores the size available to store the
171 // property value.
172 // out: stores the size required to hold the language
173 // value if getLanguage() returned
174 // TTS_PROPERTY_SIZE_TOO_SMALL, unchanged otherwise
175 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS,
176 // or TTS_PROPERTY_SIZE_TOO_SMALL
177 virtual tts_result getProperty(const char *property, char *value,
178 size_t *iosize);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700179
180 // Synthesize the text.
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700181 // As the synthesis is performed, the engine invokes the callback to notify
182 // the TTS framework that it has filled the given buffer, and indicates how
183 // many bytes it wrote. The callback is called repeatedly until the engine
184 // has generated all the audio data corresponding to the text.
185 // Note about the format of the input: the text parameter may use the
186 // following elements
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700187 // and their respective attributes as defined in the SSML 1.0 specification:
188 // * lang
189 // * say-as:
190 // o interpret-as
191 // * phoneme
192 // * voice:
193 // o gender,
194 // o age,
195 // o variant,
196 // o name
197 // * emphasis
198 // * break:
199 // o strength,
200 // o time
201 // * prosody:
202 // o pitch,
203 // o contour,
204 // o range,
205 // o rate,
206 // o duration,
207 // o volume
208 // * mark
209 // Differences between this text format and SSML are:
210 // * full SSML documents are not supported
211 // * namespaces are not supported
212 // Text is coded in UTF-8.
213 // @param text the UTF-8 text to synthesize
214 // @param userdata pointer to be returned when the call is invoked
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700215 // @param buffer the location where the synthesized data must be written
216 // @param bufferSize the number of bytes that can be written in buffer
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700217 // @return TTS_SUCCESS or TTS_FAILURE
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700218 virtual tts_result synthesizeText(const char *text, int8_t *buffer,
219 size_t bufferSize, void *userdata);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700220
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700221 // Synthesize IPA text.
222 // As the synthesis is performed, the engine invokes the callback to notify
223 // the TTS framework that it has filled the given buffer, and indicates how
224 // many bytes it wrote. The callback is called repeatedly until the engine
225 // has generated all the audio data corresponding to the IPA data.
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700226 // @param ipa the IPA data to synthesize
227 // @param userdata pointer to be returned when the call is invoked
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700228 // @param buffer the location where the synthesized data must be written
229 // @param bufferSize the number of bytes that can be written in buffer
230 // @return TTS_FEATURE_UNSUPPORTED if IPA is not supported,
231 // otherwise TTS_SUCCESS or TTS_FAILURE
232 virtual tts_result synthesizeIpa(const char *ipa, int8_t *buffer,
233 size_t bufferSize, void *userdata);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700234};
235
236} // namespace android
237