blob: cece94e97b7160a22117f653a4d73c9660d03989 [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 */
51 public SynthProxy(String nativeSoLib) {
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 /**
108 * Sets the language.
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700109 */
Charles Chena3f89292009-07-06 14:12:36 -0700110 public int setLanguage(String language, String country, String variant) {
111 return native_setLanguage(mJniData, language, country, variant);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700112 }
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700113
Jean-Michel Trivid6d03e02009-06-25 18:37:55 -0700114 /**
115 * Loads the language: it's not set, but prepared for use later.
116 */
Charles Chena3f89292009-07-06 14:12:36 -0700117 public int loadLanguage(String language, String country, String variant) {
118 return native_loadLanguage(mJniData, language, country, variant);
Jean-Michel Trivid6d03e02009-06-25 18:37:55 -0700119 }
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700120
121 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700122 * Sets the speech rate.
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700123 */
Charles Chena3f89292009-07-06 14:12:36 -0700124 public final int setSpeechRate(int speechRate) {
125 return native_setSpeechRate(mJniData, speechRate);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700126 }
127
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -0700128 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700129 * Sets the pitch of the synthesized voice.
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -0700130 */
Charles Chena3f89292009-07-06 14:12:36 -0700131 public final int setPitch(int pitch) {
132 return native_setPitch(mJniData, pitch);
Jean-Michel Trivi2ea53492009-06-23 13:44:40 -0700133 }
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700134
135 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700136 * Returns the currently set language, country and variant information.
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700137 */
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700138 public String[] getLanguage() {
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700139 return native_getLanguage(mJniData);
140 }
141
142 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700143 * Gets the currently set rate.
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700144 */
145 public int getRate() {
146 return native_getRate(mJniData);
147 }
148
149 /**
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700150 * Shuts down the native synthesizer.
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700151 */
152 public void shutdown() {
153 native_shutdown(mJniData);
154 }
155
156 //
157 // Internal
158 //
159
160 protected void finalize() {
161 native_finalize(mJniData);
162 mJniData = 0;
163 }
164
165 static {
Jean-Michel Trivi1f4b92a2009-06-02 16:02:31 -0700166 System.loadLibrary("ttssynthproxy");
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700167 }
168
169 private final static String TAG = "SynthProxy";
170
171 /**
172 * Accessed by native methods
173 */
174 private int mJniData = 0;
175
Jean-Michel Trivi0320f8b2010-01-11 14:04:50 -0800176 private native final int native_setup(Object weak_this, String nativeSoLib);
177
178 private native final int native_setLowShelf(boolean applyFilter, float filterGain,
179 float attenuationInDb, float freqInHz, float slope);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700180
181 private native final void native_finalize(int jniData);
182
Charles Chen35b86c22009-07-06 10:51:48 -0700183 private native final int native_stop(int jniData);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700184
Jean-Michel Trivi09f8db72009-08-31 10:41:55 -0700185 private native final int native_stopSync(int jniData);
186
Jean-Michel Trivi9440bce2009-07-13 10:12:37 -0700187 private native final int native_speak(int jniData, String text, int streamType);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700188
Charles Chen35b86c22009-07-06 10:51:48 -0700189 private native final int native_synthesizeToFile(int jniData, String text, String filename);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700190
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700191 private native final int native_isLanguageAvailable(int jniData, String language,
192 String country, String variant);
193
Charles Chen35b86c22009-07-06 10:51:48 -0700194 private native final int native_setLanguage(int jniData, String language, String country,
Jean-Michel Trivi679d7282009-06-16 15:36:28 -0700195 String variant);
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700196
Charles Chen35b86c22009-07-06 10:51:48 -0700197 private native final int native_loadLanguage(int jniData, String language, String country,
Jean-Michel Trivid6d03e02009-06-25 18:37:55 -0700198 String variant);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700199
Charles Chen35b86c22009-07-06 10:51:48 -0700200 private native final int native_setSpeechRate(int jniData, int speechRate);
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700201
Charles Chen35b86c22009-07-06 10:51:48 -0700202 private native final int native_setPitch(int jniData, int speechRate);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700203
Jean-Michel Trivibee1c7e2009-06-29 15:55:05 -0700204 private native final String[] native_getLanguage(int jniData);
Jean-Michel Trivi700ec652009-05-27 15:01:59 -0700205
206 private native final int native_getRate(int jniData);
207
208 private native final void native_shutdown(int jniData);
209
210
211 /**
212 * Callback from the C layer
213 */
214 @SuppressWarnings("unused")
215 private static void postNativeSpeechSynthesizedInJava(Object tts_ref,
216 int bufferPointer, int bufferSize) {
217
218 Log.i("TTS plugin debug", "bufferPointer: " + bufferPointer
219 + " bufferSize: " + bufferSize);
220
221 SynthProxy nativeTTS = (SynthProxy)((WeakReference)tts_ref).get();
222 // TODO notify TTS service of synthesis/playback completion,
223 // method definition to be changed.
224 }
225}