blob: 28b0d2ff9a1792358386569d67cf47920ce4b5fb [file] [log] [blame]
Jean-Michel Trivi895fb8e2009-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 Trivi6270d612009-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 Trivi895fb8e2009-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 Chen83e712a2009-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 Trivi895fb8e2009-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 Chen83e712a2009-06-05 13:58:33 -070041// completed. // TODO reword
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -070042// The callback for synthesis completed takes:
Jean-Michel Trivi6270d612009-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
Eric Laurenta553c252009-07-17 12:17:14 -070046// @param [in] uint32_t - The audio format
Jean-Michel Trivi6270d612009-06-05 15:01:33 -070047// @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,
Eric Laurenta553c252009-07-17 12:17:14 -070057 uint32_t, int, int8_t *&, size_t&, tts_synth_status);
Jean-Michel Trivi895fb8e2009-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 Trivi14f861a2009-05-28 11:11:25 -070068 TTS_PROPERTY_SIZE_TOO_SMALL = -5,
69 TTS_MISSING_RESOURCES = -6
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -070070};
71
Jean-Michel Trivid6d03e02009-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 Trivi895fb8e2009-05-21 15:32:11 -070080class TtsEngine
81{
82public:
Marco Nelissenf6807d72009-07-08 16:24:38 -070083 virtual ~TtsEngine() {}
84
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -070085 // Initialize the TTS engine and returns whether initialization succeeded.
86 // @param synthDoneCBPtr synthesis callback function pointer
87 // @return TTS_SUCCESS, or TTS_FAILURE
88 virtual tts_result init(synthDoneCB_t synthDoneCBPtr);
89
90 // Shut down the TTS engine and releases all associated resources.
91 // @return TTS_SUCCESS, or TTS_FAILURE
92 virtual tts_result shutdown();
93
Jean-Michel Trivi6270d612009-06-05 15:01:33 -070094 // Interrupt synthesis and flushes any synthesized data that hasn't been
95 // output yet. This will block until callbacks underway are completed.
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -070096 // @return TTS_SUCCESS, or TTS_FAILURE
97 virtual tts_result stop();
98
Jean-Michel Trivid6d03e02009-06-25 18:37:55 -070099 // Returns the level of support for the language, country and variant.
100 // @return TTS_LANG_COUNTRY_VAR_AVAILABLE if the language, country and variant are supported,
101 // and the corresponding resources are correctly installed
102 // TTS_LANG_COUNTRY_AVAILABLE if the language and country are supported and the
103 // corresponding resources are correctly installed, but there is no match for
104 // the specified variant
105 // TTS_LANG_AVAILABLE if the language is supported and the
106 // corresponding resources are correctly installed, but there is no match for
107 // the specified country and variant
108 // TTS_LANG_MISSING_DATA if the required resources to provide any level of support
109 // for the language are not correctly installed
110 // TTS_LANG_NOT_SUPPORTED if the language is not supported by the TTS engine.
111 virtual tts_support_result isLanguageAvailable(const char *lang, const char *country,
112 const char *variant);
113
Jean-Michel Trivi6270d612009-06-05 15:01:33 -0700114 // Load the resources associated with the specified language. The loaded
115 // language will only be used once a call to setLanguage() with the same
Jean-Michel Trivid6d03e02009-06-25 18:37:55 -0700116 // language value is issued. Language and country values are coded according to the ISO three
117 // letter codes for languages and countries, as can be retrieved from a java.util.Locale
118 // instance. The variant value is encoded as the variant string retrieved from a
119 // java.util.Locale instance built with that variant data.
120 // @param lang pointer to the ISO three letter code for the language
121 // @param country pointer to the ISO three letter code for the country
122 // @param variant pointer to the variant code
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700123 // @return TTS_SUCCESS, or TTS_FAILURE
Jean-Michel Trivid6d03e02009-06-25 18:37:55 -0700124 virtual tts_result loadLanguage(const char *lang, const char *country, const char *variant);
Jean-Michel Trivi35a8e802009-06-15 15:14:49 -0700125
126 // Load the resources associated with the specified language, country and Locale variant.
127 // The loaded language will only be used once a call to setLanguageFromLocale() with the same
128 // language value is issued. Language and country values are coded according to the ISO three
129 // letter codes for languages and countries, as can be retrieved from a java.util.Locale
130 // instance. The variant value is encoded as the variant string retrieved from a
131 // java.util.Locale instance built with that variant data.
132 // @param lang pointer to the ISO three letter code for the language
133 // @param country pointer to the ISO three letter code for the country
134 // @param variant pointer to the variant code
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700135 // @return TTS_SUCCESS, or TTS_FAILURE
Jean-Michel Trivi35a8e802009-06-15 15:14:49 -0700136 virtual tts_result setLanguage(const char *lang, const char *country, const char *variant);
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700137
Jean-Michel Trivi287148b2009-06-26 17:09:21 -0700138 // Retrieve the currently set language, country and variant, or empty strings if none of
139 // parameters have been set. Language and country are represented by their 3-letter ISO code
140 // @param[out] pointer to the retrieved 3-letter code language value
141 // @param[out] pointer to the retrieved 3-letter code country value
142 // @param[out] pointer to the retrieved variant value
143 // @return TTS_SUCCESS, or TTS_FAILURE
144 virtual tts_result getLanguage(char *language, char *country, char *variant);
145
146 // Notifies the engine what audio parameters should be used for the synthesis.
147 // This is meant to be used as a hint, the engine implementation will set the output values
148 // to those of the synthesis format, based on a given hint.
149 // @param[inout] encoding in: the desired audio sample format
150 // out: the format used by the TTS engine
151 // @param[inout] rate in: the desired audio sample rate
152 // out: the sample rate used by the TTS engine
153 // @param[inout] channels in: the desired number of audio channels
154 // out: the number of channels used by the TTS engine
155 // @return TTS_SUCCESS, or TTS_FAILURE
156 virtual tts_result setAudioFormat(AudioSystem::audio_format& encoding, uint32_t& rate,
157 int& channels);
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700158
159 // Set a property for the the TTS engine
160 // "size" is the maximum size of "value" for properties "property"
161 // @param property pointer to the property name
162 // @param value pointer to the property value
163 // @param size maximum size required to store this type of property
Jean-Michel Trivi6270d612009-06-05 15:01:33 -0700164 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS, or TTS_FAILURE,
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700165 // or TTS_VALUE_INVALID
Jean-Michel Trivi6270d612009-06-05 15:01:33 -0700166 virtual tts_result setProperty(const char *property, const char *value,
167 const size_t size);
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700168
169 // Retrieve a property from the TTS engine
170 // @param property pointer to the property name
171 // @param[out] value pointer to the retrieved language value
Jean-Michel Trivi6270d612009-06-05 15:01:33 -0700172 // @param[inout] iosize in: stores the size available to store the
173 // property value.
174 // out: stores the size required to hold the language
175 // value if getLanguage() returned
176 // TTS_PROPERTY_SIZE_TOO_SMALL, unchanged otherwise
177 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS,
178 // or TTS_PROPERTY_SIZE_TOO_SMALL
179 virtual tts_result getProperty(const char *property, char *value,
180 size_t *iosize);
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700181
182 // Synthesize the text.
Jean-Michel Trivi6270d612009-06-05 15:01:33 -0700183 // As the synthesis is performed, the engine invokes the callback to notify
184 // the TTS framework that it has filled the given buffer, and indicates how
185 // many bytes it wrote. The callback is called repeatedly until the engine
186 // has generated all the audio data corresponding to the text.
187 // Note about the format of the input: the text parameter may use the
188 // following elements
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700189 // and their respective attributes as defined in the SSML 1.0 specification:
190 // * lang
191 // * say-as:
192 // o interpret-as
193 // * phoneme
194 // * voice:
195 // o gender,
196 // o age,
197 // o variant,
198 // o name
199 // * emphasis
200 // * break:
201 // o strength,
202 // o time
203 // * prosody:
204 // o pitch,
205 // o contour,
206 // o range,
207 // o rate,
208 // o duration,
209 // o volume
210 // * mark
211 // Differences between this text format and SSML are:
212 // * full SSML documents are not supported
213 // * namespaces are not supported
214 // Text is coded in UTF-8.
215 // @param text the UTF-8 text to synthesize
216 // @param userdata pointer to be returned when the call is invoked
Jean-Michel Trivi6270d612009-06-05 15:01:33 -0700217 // @param buffer the location where the synthesized data must be written
218 // @param bufferSize the number of bytes that can be written in buffer
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700219 // @return TTS_SUCCESS or TTS_FAILURE
Jean-Michel Trivi6270d612009-06-05 15:01:33 -0700220 virtual tts_result synthesizeText(const char *text, int8_t *buffer,
221 size_t bufferSize, void *userdata);
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700222
Jean-Michel Trivi6270d612009-06-05 15:01:33 -0700223 // Synthesize IPA text.
224 // As the synthesis is performed, the engine invokes the callback to notify
225 // the TTS framework that it has filled the given buffer, and indicates how
226 // many bytes it wrote. The callback is called repeatedly until the engine
227 // has generated all the audio data corresponding to the IPA data.
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700228 // @param ipa the IPA data to synthesize
229 // @param userdata pointer to be returned when the call is invoked
Jean-Michel Trivi6270d612009-06-05 15:01:33 -0700230 // @param buffer the location where the synthesized data must be written
231 // @param bufferSize the number of bytes that can be written in buffer
232 // @return TTS_FEATURE_UNSUPPORTED if IPA is not supported,
233 // otherwise TTS_SUCCESS or TTS_FAILURE
234 virtual tts_result synthesizeIpa(const char *ipa, int8_t *buffer,
235 size_t bufferSize, void *userdata);
Jean-Michel Trivi895fb8e2009-05-21 15:32:11 -0700236};
237
238} // namespace android
239