Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.tts; |
| 17 | |
Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 18 | import android.media.AudioManager; |
| 19 | import android.media.AudioSystem; |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 20 | import android.util.Log; |
| 21 | import 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") |
| 33 | public class SynthProxy { |
| 34 | |
Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 35 | // 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 Trivi | 1105f0f | 2010-03-15 15:37:51 -0700 | [diff] [blame] | 39 | 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 Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 42 | private final static float PICO_FILTER_SHELF_SLOPE = 1.0f; // Q |
| 43 | |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 44 | // |
| 45 | // External API |
| 46 | // |
| 47 | |
| 48 | /** |
| 49 | * Constructor; pass the location of the native TTS .so to use. |
| 50 | */ |
Jean-Michel Trivi | 76dd788 | 2010-03-17 22:07:13 -0700 | [diff] [blame] | 51 | public SynthProxy(String nativeSoLib, String engineConfig) { |
Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 52 | boolean applyFilter = nativeSoLib.toLowerCase().contains("pico"); |
Jean-Michel Trivi | c612019 | 2010-03-07 14:29:58 -0800 | [diff] [blame] | 53 | Log.v(TtsService.SERVICE_TAG, "About to load "+ nativeSoLib + ", applyFilter="+applyFilter); |
Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 54 | native_setup(new WeakReference<SynthProxy>(this), nativeSoLib, engineConfig); |
Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 55 | native_setLowShelf(applyFilter, PICO_FILTER_GAIN, PICO_FILTER_LOWSHELF_ATTENUATION, |
| 56 | PICO_FILTER_TRANSITION_FREQ, PICO_FILTER_SHELF_SLOPE); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Stops and clears the AudioTrack. |
| 61 | */ |
Charles Chen | a3f8929 | 2009-07-06 14:12:36 -0700 | [diff] [blame] | 62 | public int stop() { |
| 63 | return native_stop(mJniData); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
Jean-Michel Trivi | 09f8db7 | 2009-08-31 10:41:55 -0700 | [diff] [blame] | 67 | * 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 Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 79 | * Synthesize speech and speak it directly using AudioTrack. |
| 80 | */ |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 81 | public int speak(String text, int streamType, float volume, float pan) { |
| 82 | Log.i(TAG, "speak() on stream "+ streamType); |
Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 83 | if ((streamType > -1) && (streamType < AudioSystem.getNumStreamTypes())) { |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 84 | return native_speak(mJniData, text, streamType, volume, pan); |
Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 85 | } else { |
| 86 | Log.e("SynthProxy", "Trying to speak with invalid stream type " + streamType); |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 87 | return native_speak(mJniData, text, AudioManager.STREAM_MUSIC, volume, pan); |
Jean-Michel Trivi | 9440bce | 2009-07-13 10:12:37 -0700 | [diff] [blame] | 88 | } |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Synthesize speech to a file. The current implementation writes a valid |
| 93 | * WAV file to the given path, assuming it is writable. Something like |
| 94 | * "/sdcard/???.wav" is recommended. |
| 95 | */ |
Charles Chen | a3f8929 | 2009-07-06 14:12:36 -0700 | [diff] [blame] | 96 | public int synthesizeToFile(String text, String filename) { |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 97 | Log.i(TAG, "synthesizeToFile() to file "+ filename); |
Charles Chen | a3f8929 | 2009-07-06 14:12:36 -0700 | [diff] [blame] | 98 | return native_synthesizeToFile(mJniData, text, filename); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 101 | /** |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 102 | * Queries for language support. |
| 103 | * Return codes are defined in android.speech.tts.TextToSpeech |
| 104 | */ |
| 105 | public int isLanguageAvailable(String language, String country, String variant) { |
| 106 | return native_isLanguageAvailable(mJniData, language, country, variant); |
| 107 | } |
| 108 | |
| 109 | /** |
Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 110 | * Updates the engine configuration. |
Jean-Michel Trivi | 76dd788 | 2010-03-17 22:07:13 -0700 | [diff] [blame] | 111 | */ |
| 112 | public int setConfig(String engineConfig) { |
Jean-Michel Trivi | 54a3dcd | 2010-03-18 14:50:06 -0700 | [diff] [blame] | 113 | return native_setConfig(mJniData, engineConfig); |
Jean-Michel Trivi | 76dd788 | 2010-03-17 22:07:13 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 117 | * Sets the language. |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 118 | */ |
Charles Chen | a3f8929 | 2009-07-06 14:12:36 -0700 | [diff] [blame] | 119 | public int setLanguage(String language, String country, String variant) { |
| 120 | return native_setLanguage(mJniData, language, country, variant); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 121 | } |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 122 | |
Jean-Michel Trivi | d6d03e0 | 2009-06-25 18:37:55 -0700 | [diff] [blame] | 123 | /** |
| 124 | * Loads the language: it's not set, but prepared for use later. |
| 125 | */ |
Charles Chen | a3f8929 | 2009-07-06 14:12:36 -0700 | [diff] [blame] | 126 | public int loadLanguage(String language, String country, String variant) { |
| 127 | return native_loadLanguage(mJniData, language, country, variant); |
Jean-Michel Trivi | d6d03e0 | 2009-06-25 18:37:55 -0700 | [diff] [blame] | 128 | } |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 131 | * Sets the speech rate. |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 132 | */ |
Charles Chen | a3f8929 | 2009-07-06 14:12:36 -0700 | [diff] [blame] | 133 | public final int setSpeechRate(int speechRate) { |
| 134 | return native_setSpeechRate(mJniData, speechRate); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 137 | /** |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 138 | * Sets the pitch of the synthesized voice. |
Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 139 | */ |
Charles Chen | a3f8929 | 2009-07-06 14:12:36 -0700 | [diff] [blame] | 140 | public final int setPitch(int pitch) { |
| 141 | return native_setPitch(mJniData, pitch); |
Jean-Michel Trivi | 2ea5349 | 2009-06-23 13:44:40 -0700 | [diff] [blame] | 142 | } |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 145 | * Returns the currently set language, country and variant information. |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 146 | */ |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 147 | public String[] getLanguage() { |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 148 | return native_getLanguage(mJniData); |
| 149 | } |
| 150 | |
| 151 | /** |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 152 | * Gets the currently set rate. |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 153 | */ |
| 154 | public int getRate() { |
| 155 | return native_getRate(mJniData); |
| 156 | } |
| 157 | |
| 158 | /** |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 159 | * Shuts down the native synthesizer. |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 160 | */ |
| 161 | public void shutdown() { |
| 162 | native_shutdown(mJniData); |
| 163 | } |
| 164 | |
| 165 | // |
| 166 | // Internal |
| 167 | // |
| 168 | |
| 169 | protected void finalize() { |
| 170 | native_finalize(mJniData); |
| 171 | mJniData = 0; |
| 172 | } |
| 173 | |
| 174 | static { |
Jean-Michel Trivi | 1f4b92a | 2009-06-02 16:02:31 -0700 | [diff] [blame] | 175 | System.loadLibrary("ttssynthproxy"); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | private final static String TAG = "SynthProxy"; |
| 179 | |
| 180 | /** |
| 181 | * Accessed by native methods |
| 182 | */ |
| 183 | private int mJniData = 0; |
| 184 | |
Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 185 | private native final int native_setup(Object weak_this, String nativeSoLib, |
| 186 | String engineConfig); |
Jean-Michel Trivi | 0320f8b | 2010-01-11 14:04:50 -0800 | [diff] [blame] | 187 | |
| 188 | private native final int native_setLowShelf(boolean applyFilter, float filterGain, |
| 189 | float attenuationInDb, float freqInHz, float slope); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 190 | |
| 191 | private native final void native_finalize(int jniData); |
| 192 | |
Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 193 | private native final int native_stop(int jniData); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 194 | |
Jean-Michel Trivi | 09f8db7 | 2009-08-31 10:41:55 -0700 | [diff] [blame] | 195 | private native final int native_stopSync(int jniData); |
| 196 | |
Jean-Michel Trivi | 9d2d26a | 2011-01-05 16:08:21 -0800 | [diff] [blame] | 197 | private native final int native_speak(int jniData, String text, int streamType, float volume, |
| 198 | float pan); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 199 | |
Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 200 | private native final int native_synthesizeToFile(int jniData, String text, String filename); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 201 | |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 202 | private native final int native_isLanguageAvailable(int jniData, String language, |
| 203 | String country, String variant); |
| 204 | |
Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 205 | private native final int native_setLanguage(int jniData, String language, String country, |
Jean-Michel Trivi | 679d728 | 2009-06-16 15:36:28 -0700 | [diff] [blame] | 206 | String variant); |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 207 | |
Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 208 | private native final int native_loadLanguage(int jniData, String language, String country, |
Jean-Michel Trivi | d6d03e0 | 2009-06-25 18:37:55 -0700 | [diff] [blame] | 209 | String variant); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 210 | |
Jean-Michel Trivi | 54a3dcd | 2010-03-18 14:50:06 -0700 | [diff] [blame] | 211 | private native final int native_setConfig(int jniData, String engineConfig); |
Jean-Michel Trivi | 900e0d0 | 2010-03-18 11:07:45 -0700 | [diff] [blame] | 212 | |
Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 213 | private native final int native_setSpeechRate(int jniData, int speechRate); |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 214 | |
Charles Chen | 35b86c2 | 2009-07-06 10:51:48 -0700 | [diff] [blame] | 215 | private native final int native_setPitch(int jniData, int speechRate); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 216 | |
Jean-Michel Trivi | bee1c7e | 2009-06-29 15:55:05 -0700 | [diff] [blame] | 217 | private native final String[] native_getLanguage(int jniData); |
Jean-Michel Trivi | 700ec65 | 2009-05-27 15:01:59 -0700 | [diff] [blame] | 218 | |
| 219 | private native final int native_getRate(int jniData); |
| 220 | |
| 221 | private native final void native_shutdown(int jniData); |
| 222 | |
| 223 | |
| 224 | /** |
| 225 | * Callback from the C layer |
| 226 | */ |
| 227 | @SuppressWarnings("unused") |
| 228 | private static void postNativeSpeechSynthesizedInJava(Object tts_ref, |
| 229 | int bufferPointer, int bufferSize) { |
| 230 | |
| 231 | Log.i("TTS plugin debug", "bufferPointer: " + bufferPointer |
| 232 | + " bufferSize: " + bufferSize); |
| 233 | |
| 234 | SynthProxy nativeTTS = (SynthProxy)((WeakReference)tts_ref).get(); |
| 235 | // TODO notify TTS service of synthesis/playback completion, |
| 236 | // method definition to be changed. |
| 237 | } |
| 238 | } |