blob: 06f3820030555527f5b957a968efa0cdbcf2b402 [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,
56 TTS_PROPERTY_SIZE_TOO_SMALL = -5
57};
58
59class TtsEngine
60{
61public:
62 // Initialize the TTS engine and returns whether initialization succeeded.
63 // @param synthDoneCBPtr synthesis callback function pointer
64 // @return TTS_SUCCESS, or TTS_FAILURE
65 virtual tts_result init(synthDoneCB_t synthDoneCBPtr);
66
67 // Shut down the TTS engine and releases all associated resources.
68 // @return TTS_SUCCESS, or TTS_FAILURE
69 virtual tts_result shutdown();
70
71 // Interrupt synthesis and flushes any synthesized data that hasn't been output yet.
72 // This will block until callbacks underway are completed.
73 // @return TTS_SUCCESS, or TTS_FAILURE
74 virtual tts_result stop();
75
76 // Load the resources associated with the specified language. The loaded language will
77 // only be used once a call to setLanguage() with the same language value is issued.
78 // Language values are based on the Android conventions for localization as described in
79 // the Android platform documentation on internationalization. This implies that language
80 // data is specified in the format xx-rYY, where xx is a two letter ISO 639-1 language code
81 // in lowercase and rYY is a two letter ISO 3166-1-alpha-2 language code in uppercase
82 // preceded by a lowercase "r".
83 // @param value pointer to the language value
84 // @param size length of the language value
85 // @return TTS_SUCCESS, or TTS_FAILURE
86 virtual tts_result loadLanguage(const char *value, const size_t size);
87
88 // Signal the engine to use the specified language. This will force the language to be
89 // loaded if it wasn't loaded previously with loadLanguage().
90 // See loadLanguage for the specification of the language.
91 // @param value pointer to the language value
92 // @param size length of the language value
93 // @return TTS_SUCCESS, or TTS_FAILURE
94 virtual tts_result setLanguage(const char *value, const size_t size);
95
96 // Retrieve the currently set language, or an empty "value" if no language has
97 // been set.
98 // @param[out] value pointer to the retrieved language value
99 // @param[inout] iosize in: stores the size available to store the language value in *value
100 // out: stores the size required to hold the language value if
101 // getLanguage() returned TTS_PROPERTY_SIZE_TOO_SMALL,
102 // unchanged otherwise.
103 // @return TTS_SUCCESS, or TTS_PROPERTY_SIZE_TOO_SMALL, or TTS_FAILURE
104 virtual tts_result getLanguage(char *value, size_t *iosize);
105
106 // Set a property for the the TTS engine
107 // "size" is the maximum size of "value" for properties "property"
108 // @param property pointer to the property name
109 // @param value pointer to the property value
110 // @param size maximum size required to store this type of property
111 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS, or TTS_FAILURE,
112 // or TTS_VALUE_INVALID
113 virtual tts_result setProperty(const char *property, const char *value, const size_t size);
114
115 // Retrieve a property from the TTS engine
116 // @param property pointer to the property name
117 // @param[out] value pointer to the retrieved language value
118 // @param[inout] iosize in: stores the size available to store the property value
119 // out: stores the size required to hold the language value if
120 // getLanguage() returned TTS_PROPERTY_SIZE_TOO_SMALL,
121 // unchanged otherwise.
122 // @return TTS_PROPERTY_UNSUPPORTED, or TTS_SUCCESS, or TTS_PROPERTY_SIZE_TOO_SMALL
123 virtual tts_result getProperty(const char *property, char *value, size_t *iosize);
124
125 // Synthesize the text.
126 // When synthesis completes, the engine invokes the callback to notify the TTS framework.
127 // Note about the format of the input: the text parameter may use the following elements
128 // and their respective attributes as defined in the SSML 1.0 specification:
129 // * lang
130 // * say-as:
131 // o interpret-as
132 // * phoneme
133 // * voice:
134 // o gender,
135 // o age,
136 // o variant,
137 // o name
138 // * emphasis
139 // * break:
140 // o strength,
141 // o time
142 // * prosody:
143 // o pitch,
144 // o contour,
145 // o range,
146 // o rate,
147 // o duration,
148 // o volume
149 // * mark
150 // Differences between this text format and SSML are:
151 // * full SSML documents are not supported
152 // * namespaces are not supported
153 // Text is coded in UTF-8.
154 // @param text the UTF-8 text to synthesize
155 // @param userdata pointer to be returned when the call is invoked
156 // @return TTS_SUCCESS or TTS_FAILURE
157 virtual tts_result synthesizeText(const char *text, void *userdata);
158
159 // Synthesize IPA text. When synthesis completes, the engine must call the given callback to notify the TTS API.
160 // @param ipa the IPA data to synthesize
161 // @param userdata pointer to be returned when the call is invoked
162 // @return TTS_FEATURE_UNSUPPORTED if IPA is not supported, otherwise TTS_SUCCESS or TTS_FAILURE
163 virtual tts_result synthesizeIpa(const char *ipa, void *userdata);
164};
165
166} // namespace android
167