blob: d2aa30ef4b1a4f579c9c9cd6bb944bb566039639 [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);
102
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700103 // Signal the engine to use the specified language. This will force the
104 // language to be loaded if it wasn't loaded previously with loadLanguage().
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700105 // See loadLanguage for the specification of the language.
106 // @param value pointer to the language value
107 // @param size length of the language value
108 // @return TTS_SUCCESS, or TTS_FAILURE
109 virtual tts_result setLanguage(const char *value, const size_t size);
110
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700111 // Retrieve the currently set language, or an empty "value" if no language
112 // has been set.
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700113 // @param[out] value pointer to the retrieved language value
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700114 // @param[inout] iosize in: stores the size available to store the language
115 // value in *value
116 // out: stores the size required to hold the language
117 // value if getLanguage() returned
118 // TTS_PROPERTY_SIZE_TOO_SMALL, unchanged otherwise.
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700119 // @return TTS_SUCCESS, or TTS_PROPERTY_SIZE_TOO_SMALL, or TTS_FAILURE
120 virtual tts_result getLanguage(char *value, size_t *iosize);
121
122 // Set a property for the the TTS engine
123 // "size" is the maximum size of "value" for properties "property"
124 // @param property pointer to the property name
125 // @param value pointer to the property value
126 // @param size maximum size required to store this type of property
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700127 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS, or TTS_FAILURE,
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700128 // or TTS_VALUE_INVALID
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700129 virtual tts_result setProperty(const char *property, const char *value,
130 const size_t size);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700131
132 // Retrieve a property from the TTS engine
133 // @param property pointer to the property name
134 // @param[out] value pointer to the retrieved language value
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700135 // @param[inout] iosize in: stores the size available to store the
136 // property value.
137 // out: stores the size required to hold the language
138 // value if getLanguage() returned
139 // TTS_PROPERTY_SIZE_TOO_SMALL, unchanged otherwise
140 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS,
141 // or TTS_PROPERTY_SIZE_TOO_SMALL
142 virtual tts_result getProperty(const char *property, char *value,
143 size_t *iosize);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700144
145 // Synthesize the text.
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700146 // As the synthesis is performed, the engine invokes the callback to notify
147 // the TTS framework that it has filled the given buffer, and indicates how
148 // many bytes it wrote. The callback is called repeatedly until the engine
149 // has generated all the audio data corresponding to the text.
150 // Note about the format of the input: the text parameter may use the
151 // following elements
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700152 // and their respective attributes as defined in the SSML 1.0 specification:
153 // * lang
154 // * say-as:
155 // o interpret-as
156 // * phoneme
157 // * voice:
158 // o gender,
159 // o age,
160 // o variant,
161 // o name
162 // * emphasis
163 // * break:
164 // o strength,
165 // o time
166 // * prosody:
167 // o pitch,
168 // o contour,
169 // o range,
170 // o rate,
171 // o duration,
172 // o volume
173 // * mark
174 // Differences between this text format and SSML are:
175 // * full SSML documents are not supported
176 // * namespaces are not supported
177 // Text is coded in UTF-8.
178 // @param text the UTF-8 text to synthesize
179 // @param userdata pointer to be returned when the call is invoked
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700180 // @param buffer the location where the synthesized data must be written
181 // @param bufferSize the number of bytes that can be written in buffer
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700182 // @return TTS_SUCCESS or TTS_FAILURE
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700183 virtual tts_result synthesizeText(const char *text, int8_t *buffer,
184 size_t bufferSize, void *userdata);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700185
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700186 // Synthesize IPA text.
187 // As the synthesis is performed, the engine invokes the callback to notify
188 // the TTS framework that it has filled the given buffer, and indicates how
189 // many bytes it wrote. The callback is called repeatedly until the engine
190 // has generated all the audio data corresponding to the IPA data.
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700191 // @param ipa the IPA data to synthesize
192 // @param userdata pointer to be returned when the call is invoked
Jean-Michel Trivia40a4cf2009-06-05 15:01:33 -0700193 // @param buffer the location where the synthesized data must be written
194 // @param bufferSize the number of bytes that can be written in buffer
195 // @return TTS_FEATURE_UNSUPPORTED if IPA is not supported,
196 // otherwise TTS_SUCCESS or TTS_FAILURE
197 virtual tts_result synthesizeIpa(const char *ipa, int8_t *buffer,
198 size_t bufferSize, void *userdata);
Jean-Michel Trivic7104572009-05-21 15:32:11 -0700199};
200
201} // namespace android
202