blob: bf629958ba78c550b62a017144c297126046cb72 [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
19// to access Text-To-Speech functionality in shared libraries that implement speech
20// synthesis and the management of resources associated with the synthesis.
21// An example of the implementation of this interface can be found in
22// FIXME: add path+name to implementation of default TTS engine
23// Libraries implementing this interface are used in:
24// frameworks/base/tts/jni/android_tts_SpeechSynthesis.cpp
25
26namespace android {
27
28// The callback is used by the implementation of this interface to notify its
29// client, the Android TTS service, that the last requested synthesis has been
30// completed.
31// The callback for synthesis completed takes:
32// void * - The userdata pointer set in the original synth call
33// uint32_t - Track sampling rate in Hz
34// audio_format - The AudioSystem::audio_format enum
35// int - The number of channels
36// int8_t * - A buffer of audio data only valid during the execution of the callback
37// size_t - The size of the buffer
38// Note about memory management:
39// The implementation of TtsEngine is responsible for the management of the memory
40// it allocates to store the synthesized speech. After the execution of the callback
41// to hand the synthesized data to the client of TtsEngine, the TTS engine is
42// free to reuse or free the previously allocated memory.
43// This implies that the implementation of the "synthDoneCB" callback cannot use
44// the pointer to the buffer of audio samples outside of the callback itself.
45typedef void (synthDoneCB_t)(void *, uint32_t, AudioSystem::audio_format, int, int8_t *, size_t);
46
47class TtsEngine;
48extern "C" TtsEngine* getTtsEngine();
49
50enum tts_result {
51 TTS_SUCCESS = 0,
52 TTS_FAILURE = -1,
53 TTS_FEATURE_UNSUPPORTED = -2,
54 TTS_VALUE_INVALID = -3,
55 TTS_PROPERTY_UNSUPPORTED = -4,
Jean-Michel Trivi8d336f92009-05-28 11:11:25 -070056 TTS_PROPERTY_SIZE_TOO_SMALL = -5,
57 TTS_MISSING_RESOURCES = -6
Jean-Michel Trivic7104572009-05-21 15:32:11 -070058};
59
60class TtsEngine
61{
62public:
63 // Initialize the TTS engine and returns whether initialization succeeded.
64 // @param synthDoneCBPtr synthesis callback function pointer
65 // @return TTS_SUCCESS, or TTS_FAILURE
66 virtual tts_result init(synthDoneCB_t synthDoneCBPtr);
67
68 // Shut down the TTS engine and releases all associated resources.
69 // @return TTS_SUCCESS, or TTS_FAILURE
70 virtual tts_result shutdown();
71
72 // Interrupt synthesis and flushes any synthesized data that hasn't been output yet.
73 // This will block until callbacks underway are completed.
74 // @return TTS_SUCCESS, or TTS_FAILURE
75 virtual tts_result stop();
76
77 // Load the resources associated with the specified language. The loaded language will
78 // only be used once a call to setLanguage() with the same language value is issued.
79 // Language values are based on the Android conventions for localization as described in
80 // the Android platform documentation on internationalization. This implies that language
81 // data is specified in the format xx-rYY, where xx is a two letter ISO 639-1 language code
82 // in lowercase and rYY is a two letter ISO 3166-1-alpha-2 language code in uppercase
83 // preceded by a lowercase "r".
84 // @param value pointer to the language value
85 // @param size length of the language value
86 // @return TTS_SUCCESS, or TTS_FAILURE
87 virtual tts_result loadLanguage(const char *value, const size_t size);
88
89 // Signal the engine to use the specified language. This will force the language to be
90 // loaded if it wasn't loaded previously with loadLanguage().
91 // See loadLanguage for the specification of the language.
92 // @param value pointer to the language value
93 // @param size length of the language value
94 // @return TTS_SUCCESS, or TTS_FAILURE
95 virtual tts_result setLanguage(const char *value, const size_t size);
96
97 // Retrieve the currently set language, or an empty "value" if no language has
98 // been set.
99 // @param[out] value pointer to the retrieved language value
100 // @param[inout] iosize in: stores the size available to store the language value in *value
101 // out: stores the size required to hold the language value if
102 // getLanguage() returned TTS_PROPERTY_SIZE_TOO_SMALL,
103 // unchanged otherwise.
104 // @return TTS_SUCCESS, or TTS_PROPERTY_SIZE_TOO_SMALL, or TTS_FAILURE
105 virtual tts_result getLanguage(char *value, size_t *iosize);
106
107 // Set a property for the the TTS engine
108 // "size" is the maximum size of "value" for properties "property"
109 // @param property pointer to the property name
110 // @param value pointer to the property value
111 // @param size maximum size required to store this type of property
112 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS, or TTS_FAILURE,
113 // or TTS_VALUE_INVALID
114 virtual tts_result setProperty(const char *property, const char *value, const size_t size);
115
116 // Retrieve a property from the TTS engine
117 // @param property pointer to the property name
118 // @param[out] value pointer to the retrieved language value
119 // @param[inout] iosize in: stores the size available to store the property value
120 // out: stores the size required to hold the language value if
121 // getLanguage() returned TTS_PROPERTY_SIZE_TOO_SMALL,
122 // unchanged otherwise.
123 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS, or TTS_PROPERTY_SIZE_TOO_SMALL
124 virtual tts_result getProperty(const char *property, char *value, size_t *iosize);
125
126 // Synthesize the text.
127 // When synthesis completes, the engine invokes the callback to notify the TTS framework.
128 // Note about the format of the input: the text parameter may use the following elements
129 // and their respective attributes as defined in the SSML 1.0 specification:
130 // * lang
131 // * say-as:
132 // o interpret-as
133 // * phoneme
134 // * voice:
135 // o gender,
136 // o age,
137 // o variant,
138 // o name
139 // * emphasis
140 // * break:
141 // o strength,
142 // o time
143 // * prosody:
144 // o pitch,
145 // o contour,
146 // o range,
147 // o rate,
148 // o duration,
149 // o volume
150 // * mark
151 // Differences between this text format and SSML are:
152 // * full SSML documents are not supported
153 // * namespaces are not supported
154 // Text is coded in UTF-8.
155 // @param text the UTF-8 text to synthesize
156 // @param userdata pointer to be returned when the call is invoked
157 // @return TTS_SUCCESS or TTS_FAILURE
158 virtual tts_result synthesizeText(const char *text, void *userdata);
159
160 // Synthesize IPA text. When synthesis completes, the engine must call the given callback to notify the TTS API.
161 // @param ipa the IPA data to synthesize
162 // @param userdata pointer to be returned when the call is invoked
163 // @return TTS_FEATURE_UNSUPPORTED if IPA is not supported, otherwise TTS_SUCCESS or TTS_FAILURE
164 virtual tts_result synthesizeIpa(const char *ipa, void *userdata);
165};
166
167} // namespace android
168