blob: ca50a5edd21230789b0e966fecd0dacc00d1de49 [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 Trivia40a4cf2009-06-05 15:01:33 -0700136 // Retrieve the currently set language, or an empty "value" if no language
137 // has been set.
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700138 // @param[out] value pointer to the retrieved language value
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700139 // @param[inout] iosize in: stores the size available to store the language
140 // value in *value
141 // out: stores the size required to hold the language
142 // value if getLanguage() returned
143 // TTS_PROPERTY_SIZE_TOO_SMALL, unchanged otherwise.
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700144 // @return TTS_SUCCESS, or TTS_PROPERTY_SIZE_TOO_SMALL, or TTS_FAILURE
145 virtual tts_result getLanguage(char *value, size_t *iosize);
146
147 // Set a property for the the TTS engine
148 // "size" is the maximum size of "value" for properties "property"
149 // @param property pointer to the property name
150 // @param value pointer to the property value
151 // @param size maximum size required to store this type of property
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700152 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS, or TTS_FAILURE,
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700153 // or TTS_VALUE_INVALID
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700154 virtual tts_result setProperty(const char *property, const char *value,
155 const size_t size);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700156
157 // Retrieve a property from the TTS engine
158 // @param property pointer to the property name
159 // @param[out] value pointer to the retrieved language value
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700160 // @param[inout] iosize in: stores the size available to store the
161 // property value.
162 // out: stores the size required to hold the language
163 // value if getLanguage() returned
164 // TTS_PROPERTY_SIZE_TOO_SMALL, unchanged otherwise
165 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS,
166 // or TTS_PROPERTY_SIZE_TOO_SMALL
167 virtual tts_result getProperty(const char *property, char *value,
168 size_t *iosize);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700169
170 // Synthesize the text.
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700171 // As the synthesis is performed, the engine invokes the callback to notify
172 // the TTS framework that it has filled the given buffer, and indicates how
173 // many bytes it wrote. The callback is called repeatedly until the engine
174 // has generated all the audio data corresponding to the text.
175 // Note about the format of the input: the text parameter may use the
176 // following elements
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700177 // and their respective attributes as defined in the SSML 1.0 specification:
178 // * lang
179 // * say-as:
180 // o interpret-as
181 // * phoneme
182 // * voice:
183 // o gender,
184 // o age,
185 // o variant,
186 // o name
187 // * emphasis
188 // * break:
189 // o strength,
190 // o time
191 // * prosody:
192 // o pitch,
193 // o contour,
194 // o range,
195 // o rate,
196 // o duration,
197 // o volume
198 // * mark
199 // Differences between this text format and SSML are:
200 // * full SSML documents are not supported
201 // * namespaces are not supported
202 // Text is coded in UTF-8.
203 // @param text the UTF-8 text to synthesize
204 // @param userdata pointer to be returned when the call is invoked
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700205 // @param buffer the location where the synthesized data must be written
206 // @param bufferSize the number of bytes that can be written in buffer
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700207 // @return TTS_SUCCESS or TTS_FAILURE
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700208 virtual tts_result synthesizeText(const char *text, int8_t *buffer,
209 size_t bufferSize, void *userdata);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700210
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700211 // Synthesize IPA text.
212 // As the synthesis is performed, the engine invokes the callback to notify
213 // the TTS framework that it has filled the given buffer, and indicates how
214 // many bytes it wrote. The callback is called repeatedly until the engine
215 // has generated all the audio data corresponding to the IPA data.
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700216 // @param ipa the IPA data to synthesize
217 // @param userdata pointer to be returned when the call is invoked
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700218 // @param buffer the location where the synthesized data must be written
219 // @param bufferSize the number of bytes that can be written in buffer
220 // @return TTS_FEATURE_UNSUPPORTED if IPA is not supported,
221 // otherwise TTS_SUCCESS or TTS_FAILURE
222 virtual tts_result synthesizeIpa(const char *ipa, int8_t *buffer,
223 size_t bufferSize, void *userdata);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700224};
225
226} // namespace android
227