blob: 5f283e11e247025453121c31843fd32632dced70 [file] [log] [blame]
Jean-Michel Trivi700ec652009-05-27 15:01:59 -07001/*
2 * Copyright (C) 2009 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package android.tts;
17
Jean-Michel Trivi9440bce2009-07-13 10:12:37 -070018import android.media.AudioManager;
19import android.media.AudioSystem;
Jean-Michel Trivi700ec652009-05-27 15:01:59 -070020import android.util.Log;
21import java.lang.ref.WeakReference;
22
23/**
24 * @hide
25 *
26 * The SpeechSynthesis class provides a high-level api to create and play
27 * synthesized speech. This class is used internally to talk to a native
28 * TTS library that implements the interface defined in
29 * frameworks/base/include/tts/TtsEngine.h
30 *
31 */
32@SuppressWarnings("unused")
33public class SynthProxy {
34
Jean-Michel Trivi0320f8b2010-01-11 14:04:50 -080035 // Default parameters of a filter to be applied when using the Pico engine.
36 // Such a huge filter gain is justified by how much energy in the low frequencies is "wasted" at
37 // the output of the synthesis. The low shelving filter removes it, leaving room for
38 // amplification.
Jean-Michel Trivi1105f0f2010-03-15 15:37:51 -070039 private final static float PICO_FILTER_GAIN = 5.0f; // linear gain
40 private final static float PICO_FILTER_LOWSHELF_ATTENUATION = -18.0f; // in dB
41 private final static float PICO_FILTER_TRANSITION_FREQ = 1100.0f; // in Hz
Jean-Michel Trivi0320f8b2010-01-11 14:04:50 -080042 private final static float PICO_FILTER_SHELF_SLOPE = 1.0f; // Q
43
Jean-Michel Trivi700ec652009-05-27 15:01:59 -070044 //
45 // External API
46 //
47
48 /**
49 * Constructor; pass the location of the native TTS .so to use.
50 */
Jean-Michel Trivi76dd7882010-03-17 22:07:13 -070051 public SynthProxy(String nativeSoLib, String engineConfig) {
Jean-Michel Trivi0320f8b2010-01-11 14:04:50 -080052 boolean applyFilter = nativeSoLib.toLowerCase().contains("pico");
Jean-Michel Trivic6120192010-03-07 14:29:58 -080053 Log.v(TtsService.SERVICE_TAG, "About to load "+ nativeSoLib + ", applyFilter="+applyFilter);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -070054 native_setup(new WeakReference<SynthProxy>(this), nativeSoLib);
Jean-Michel Trivi0320f8b2010-01-11 14:04:50 -080055 native_setLowShelf(applyFilter, PICO_FILTER_GAIN, PICO_FILTER_LOWSHELF_ATTENUATION,
56 PICO_FILTER_TRANSITION_FREQ, PICO_FILTER_SHELF_SLOPE);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -070057 }
58
59 /**
60 * Stops and clears the AudioTrack.
61 */
Charles Chena3f89292009-07-06 14:12:36 -070062 public int stop() {
63 return native_stop(mJniData);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -070064 }
65
66 /**
Jean-Michel Trivi09f8db72009-08-31 10:41:55 -070067 * Synchronous stop of the synthesizer. This method returns when the synth
68 * has completed the stop procedure and doesn't use any of the resources it
69 * was using while synthesizing.
70 *
71 * @return {@link android.speech.tts.TextToSpeech.SUCCESS} or
72 * {@link android.speech.tts.TextToSpeech.ERROR}
73 */
74 public int stopSync() {
75 return native_stopSync(mJniData);
76 }
77
78 /**
Jean-Michel Trivi700ec652009-05-27 15:01:59 -070079 * Synthesize speech and speak it directly using AudioTrack.
80 */
Jean-Michel Trivi9440bce2009-07-13 10:12:37 -070081 public int speak(String text, int streamType) {
82 if ((streamType > -1) && (streamType < AudioSystem.getNumStreamTypes())) {
83 return native_speak(mJniData, text, streamType);
84 } else {
85 Log.e("SynthProxy", "Trying to speak with invalid stream type " + streamType);
86 return native_speak(mJniData, text, AudioManager.STREAM_MUSIC);
87 }
Jean-Michel Trivi700ec652009-05-27 15:01:59 -070088 }
89
90 /**
91 * Synthesize speech to a file. The current implementation writes a valid
92 * WAV file to the given path, assuming it is writable. Something like
93 * "/sdcard/???.wav" is recommended.
94 */
Charles Chena3f89292009-07-06 14:12:36 -070095 public int synthesizeToFile(String text, String filename) {
96 return native_synthesizeToFile(mJniData, text, filename);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -070097 }
98
Jean-Michel Trivi700ec652009-05-27 15:01:59 -070099 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700100 * Queries for language support.
101 * Return codes are defined in android.speech.tts.TextToSpeech
102 */
103 public int isLanguageAvailable(String language, String country, String variant) {
104 return native_isLanguageAvailable(mJniData, language, country, variant);
105 }
106
107 /**
Jean-Michel Trivi76dd7882010-03-17 22:07:13 -0700108 * Sets the engine configuration.
109 */
110 public int setConfig(String engineConfig) {
111 return android.speech.tts.TextToSpeech.SUCCESS;
112 }
113
114 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700115 * Sets the language.
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700116 */
Charles Chena3f89292009-07-06 14:12:36 -0700117 public int setLanguage(String language, String country, String variant) {
118 return native_setLanguage(mJniData, language, country, variant);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700119 }
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700120
Jean-Michel Trivid6d03e02009-06-25 18:37:55 -0700121 /**
122 * Loads the language: it's not set, but prepared for use later.
123 */
Charles Chena3f89292009-07-06 14:12:36 -0700124 public int loadLanguage(String language, String country, String variant) {
125 return native_loadLanguage(mJniData, language, country, variant);
Jean-Michel Trivid6d03e02009-06-25 18:37:55 -0700126 }
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700127
128 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700129 * Sets the speech rate.
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700130 */
Charles Chena3f89292009-07-06 14:12:36 -0700131 public final int setSpeechRate(int speechRate) {
132 return native_setSpeechRate(mJniData, speechRate);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700133 }
134
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -0700135 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700136 * Sets the pitch of the synthesized voice.
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -0700137 */
Charles Chena3f89292009-07-06 14:12:36 -0700138 public final int setPitch(int pitch) {
139 return native_setPitch(mJniData, pitch);
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -0700140 }
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700141
142 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700143 * Returns the currently set language, country and variant information.
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700144 */
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700145 public String[] getLanguage() {
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700146 return native_getLanguage(mJniData);
147 }
148
149 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700150 * Gets the currently set rate.
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700151 */
152 public int getRate() {
153 return native_getRate(mJniData);
154 }
155
156 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700157 * Shuts down the native synthesizer.
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700158 */
159 public void shutdown() {
160 native_shutdown(mJniData);
161 }
162
163 //
164 // Internal
165 //
166
167 protected void finalize() {
168 native_finalize(mJniData);
169 mJniData = 0;
170 }
171
172 static {
Jean-Michel Trivi1f4b92a2009-06-02 16:02:31 -0700173 System.loadLibrary("ttssynthproxy");
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700174 }
175
176 private final static String TAG = "SynthProxy";
177
178 /**
179 * Accessed by native methods
180 */
181 private int mJniData = 0;
182
Jean-Michel Trivi0320f8b2010-01-11 14:04:50 -0800183 private native final int native_setup(Object weak_this, String nativeSoLib);
184
185 private native final int native_setLowShelf(boolean applyFilter, float filterGain,
186 float attenuationInDb, float freqInHz, float slope);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700187
188 private native final void native_finalize(int jniData);
189
Charles Chen35b86c22009-07-06 10:51:48 -0700190 private native final int native_stop(int jniData);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700191
Jean-Michel Trivi09f8db72009-08-31 10:41:55 -0700192 private native final int native_stopSync(int jniData);
193
Jean-Michel Trivi9440bce2009-07-13 10:12:37 -0700194 private native final int native_speak(int jniData, String text, int streamType);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700195
Charles Chen35b86c22009-07-06 10:51:48 -0700196 private native final int native_synthesizeToFile(int jniData, String text, String filename);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700197
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700198 private native final int native_isLanguageAvailable(int jniData, String language,
199 String country, String variant);
200
Charles Chen35b86c22009-07-06 10:51:48 -0700201 private native final int native_setLanguage(int jniData, String language, String country,
Jean-Michel Trivi679d7282009-06-16 15:36:28 -0700202 String variant);
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700203
Charles Chen35b86c22009-07-06 10:51:48 -0700204 private native final int native_loadLanguage(int jniData, String language, String country,
Jean-Michel Trivid6d03e02009-06-25 18:37:55 -0700205 String variant);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700206
Charles Chen35b86c22009-07-06 10:51:48 -0700207 private native final int native_setSpeechRate(int jniData, int speechRate);
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700208
Charles Chen35b86c22009-07-06 10:51:48 -0700209 private native final int native_setPitch(int jniData, int speechRate);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700210
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700211 private native final String[] native_getLanguage(int jniData);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700212
213 private native final int native_getRate(int jniData);
214
215 private native final void native_shutdown(int jniData);
216
217
218 /**
219 * Callback from the C layer
220 */
221 @SuppressWarnings("unused")
222 private static void postNativeSpeechSynthesizedInJava(Object tts_ref,
223 int bufferPointer, int bufferSize) {
224
225 Log.i("TTS plugin debug", "bufferPointer: " + bufferPointer
226 + " bufferSize: " + bufferSize);
227
228 SynthProxy nativeTTS = (SynthProxy)((WeakReference)tts_ref).get();
229 // TODO notify TTS service of synthesis/playback completion,
230 // method definition to be changed.
231 }
232}