blob: 848653232dd615e8c9664b33424a3d04d24c042d [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
72class TtsEngine
73{
74public:
75 // Initialize the TTS engine and returns whether initialization succeeded.
76 // @param synthDoneCBPtr synthesis callback function pointer
77 // @return TTS_SUCCESS, or TTS_FAILURE
78 virtual tts_result init(synthDoneCB_t synthDoneCBPtr);
79
80 // Shut down the TTS engine and releases all associated resources.
81 // @return TTS_SUCCESS, or TTS_FAILURE
82 virtual tts_result shutdown();
83
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -070084 // Interrupt synthesis and flushes any synthesized data that hasn't been
85 // output yet. This will block until callbacks underway are completed.
Jean-Michel Trivic7104572009-05-21 15:32:11 -070086 // @return TTS_SUCCESS, or TTS_FAILURE
87 virtual tts_result stop();
88
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -070089 // Load the resources associated with the specified language. The loaded
90 // language will only be used once a call to setLanguage() with the same
91 // language value is issued. Language values are based on the Android
92 // conventions for localization as described in the Android platform
93 // documentation on internationalization. This implies that language
94 // data is specified in the format xx-rYY, where xx is a two letter
95 // ISO 639-1 language code in lowercase and rYY is a two letter
96 // ISO 3166-1-alpha-2 language code in uppercase preceded by a
97 // lowercase "r".
Jean-Michel Trivic7104572009-05-21 15:32:11 -070098 // @param value pointer to the language value
99 // @param size length of the language value
100 // @return TTS_SUCCESS, or TTS_FAILURE
101 virtual tts_result loadLanguage(const char *value, const size_t size);
Jean-Michel Trivibc275c82009-06-15 15:14:49 -0700102
103 // Load the resources associated with the specified language, country and Locale variant.
104 // The loaded language will only be used once a call to setLanguageFromLocale() with the same
105 // language value is issued. Language and country values are coded according to the ISO three
106 // letter codes for languages and countries, as can be retrieved from a java.util.Locale
107 // instance. The variant value is encoded as the variant string retrieved from a
108 // java.util.Locale instance built with that variant data.
109 // @param lang pointer to the ISO three letter code for the language
110 // @param country pointer to the ISO three letter code for the country
111 // @param variant pointer to the variant code
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700112 // @return TTS_SUCCESS, or TTS_FAILURE
Jean-Michel Trivibc275c82009-06-15 15:14:49 -0700113 virtual tts_result setLanguage(const char *lang, const char *country, const char *variant);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700114
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700115 // Retrieve the currently set language, or an empty "value" if no language
116 // has been set.
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700117 // @param[out] value pointer to the retrieved language value
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700118 // @param[inout] iosize in: stores the size available to store the language
119 // value in *value
120 // out: stores the size required to hold the language
121 // value if getLanguage() returned
122 // TTS_PROPERTY_SIZE_TOO_SMALL, unchanged otherwise.
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700123 // @return TTS_SUCCESS, or TTS_PROPERTY_SIZE_TOO_SMALL, or TTS_FAILURE
124 virtual tts_result getLanguage(char *value, size_t *iosize);
125
126 // Set a property for the the TTS engine
127 // "size" is the maximum size of "value" for properties "property"
128 // @param property pointer to the property name
129 // @param value pointer to the property value
130 // @param size maximum size required to store this type of property
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700131 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS, or TTS_FAILURE,
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700132 // or TTS_VALUE_INVALID
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700133 virtual tts_result setProperty(const char *property, const char *value,
134 const size_t size);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700135
136 // Retrieve a property from the TTS engine
137 // @param property pointer to the property name
138 // @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
140 // property value.
141 // out: stores the size required to hold the language
142 // value if getLanguage() returned
143 // TTS_PROPERTY_SIZE_TOO_SMALL, unchanged otherwise
144 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS,
145 // or TTS_PROPERTY_SIZE_TOO_SMALL
146 virtual tts_result getProperty(const char *property, char *value,
147 size_t *iosize);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700148
149 // Synthesize the text.
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700150 // As the synthesis is performed, the engine invokes the callback to notify
151 // the TTS framework that it has filled the given buffer, and indicates how
152 // many bytes it wrote. The callback is called repeatedly until the engine
153 // has generated all the audio data corresponding to the text.
154 // Note about the format of the input: the text parameter may use the
155 // following elements
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700156 // and their respective attributes as defined in the SSML 1.0 specification:
157 // * lang
158 // * say-as:
159 // o interpret-as
160 // * phoneme
161 // * voice:
162 // o gender,
163 // o age,
164 // o variant,
165 // o name
166 // * emphasis
167 // * break:
168 // o strength,
169 // o time
170 // * prosody:
171 // o pitch,
172 // o contour,
173 // o range,
174 // o rate,
175 // o duration,
176 // o volume
177 // * mark
178 // Differences between this text format and SSML are:
179 // * full SSML documents are not supported
180 // * namespaces are not supported
181 // Text is coded in UTF-8.
182 // @param text the UTF-8 text to synthesize
183 // @param userdata pointer to be returned when the call is invoked
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700184 // @param buffer the location where the synthesized data must be written
185 // @param bufferSize the number of bytes that can be written in buffer
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700186 // @return TTS_SUCCESS or TTS_FAILURE
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700187 virtual tts_result synthesizeText(const char *text, int8_t *buffer,
188 size_t bufferSize, void *userdata);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700189
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700190 // Synthesize IPA text.
191 // As the synthesis is performed, the engine invokes the callback to notify
192 // the TTS framework that it has filled the given buffer, and indicates how
193 // many bytes it wrote. The callback is called repeatedly until the engine
194 // has generated all the audio data corresponding to the IPA data.
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700195 // @param ipa the IPA data to synthesize
196 // @param userdata pointer to be returned when the call is invoked
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700197 // @param buffer the location where the synthesized data must be written
198 // @param bufferSize the number of bytes that can be written in buffer
199 // @return TTS_FEATURE_UNSUPPORTED if IPA is not supported,
200 // otherwise TTS_SUCCESS or TTS_FAILURE
201 virtual tts_result synthesizeIpa(const char *ipa, int8_t *buffer,
202 size_t bufferSize, void *userdata);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700203};
204
205} // namespace android
206