| Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -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-Baptiste Queru | a8675f6 | 2009-07-29 14:25:07 -0700 | [diff] [blame^] | 18 | import android.media.AudioManager; |
| 19 | import android.media.AudioSystem; |
| Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -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 | |
| 35 | // |
| 36 | // External API |
| 37 | // |
| 38 | |
| 39 | /** |
| 40 | * Constructor; pass the location of the native TTS .so to use. |
| 41 | */ |
| 42 | public SynthProxy(String nativeSoLib) { |
| 43 | Log.e("TTS is loading", nativeSoLib); |
| 44 | native_setup(new WeakReference<SynthProxy>(this), nativeSoLib); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Stops and clears the AudioTrack. |
| 49 | */ |
| 50 | public int stop() { |
| 51 | return native_stop(mJniData); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Synthesize speech and speak it directly using AudioTrack. |
| 56 | */ |
| Jean-Baptiste Queru | a8675f6 | 2009-07-29 14:25:07 -0700 | [diff] [blame^] | 57 | public int speak(String text, int streamType) { |
| 58 | if ((streamType > -1) && (streamType < AudioSystem.getNumStreamTypes())) { |
| 59 | return native_speak(mJniData, text, streamType); |
| 60 | } else { |
| 61 | Log.e("SynthProxy", "Trying to speak with invalid stream type " + streamType); |
| 62 | return native_speak(mJniData, text, AudioManager.STREAM_MUSIC); |
| 63 | } |
| Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Synthesize speech to a file. The current implementation writes a valid |
| 68 | * WAV file to the given path, assuming it is writable. Something like |
| 69 | * "/sdcard/???.wav" is recommended. |
| 70 | */ |
| 71 | public int synthesizeToFile(String text, String filename) { |
| 72 | return native_synthesizeToFile(mJniData, text, filename); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Queries for language support. |
| 77 | * Return codes are defined in android.speech.tts.TextToSpeech |
| 78 | */ |
| 79 | public int isLanguageAvailable(String language, String country, String variant) { |
| 80 | return native_isLanguageAvailable(mJniData, language, country, variant); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Sets the language. |
| 85 | */ |
| 86 | public int setLanguage(String language, String country, String variant) { |
| 87 | return native_setLanguage(mJniData, language, country, variant); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Loads the language: it's not set, but prepared for use later. |
| 92 | */ |
| 93 | public int loadLanguage(String language, String country, String variant) { |
| 94 | return native_loadLanguage(mJniData, language, country, variant); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Sets the speech rate. |
| 99 | */ |
| 100 | public final int setSpeechRate(int speechRate) { |
| 101 | return native_setSpeechRate(mJniData, speechRate); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Sets the pitch of the synthesized voice. |
| 106 | */ |
| 107 | public final int setPitch(int pitch) { |
| 108 | return native_setPitch(mJniData, pitch); |
| 109 | } |
| 110 | |
| 111 | /** |
| Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 112 | * Returns the currently set language, country and variant information. |
| 113 | */ |
| 114 | public String[] getLanguage() { |
| 115 | return native_getLanguage(mJniData); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Gets the currently set rate. |
| 120 | */ |
| 121 | public int getRate() { |
| 122 | return native_getRate(mJniData); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Shuts down the native synthesizer. |
| 127 | */ |
| 128 | public void shutdown() { |
| 129 | native_shutdown(mJniData); |
| 130 | } |
| 131 | |
| 132 | // |
| 133 | // Internal |
| 134 | // |
| 135 | |
| 136 | protected void finalize() { |
| 137 | native_finalize(mJniData); |
| 138 | mJniData = 0; |
| 139 | } |
| 140 | |
| 141 | static { |
| 142 | System.loadLibrary("ttssynthproxy"); |
| 143 | } |
| 144 | |
| 145 | private final static String TAG = "SynthProxy"; |
| 146 | |
| 147 | /** |
| 148 | * Accessed by native methods |
| 149 | */ |
| 150 | private int mJniData = 0; |
| 151 | |
| 152 | private native final void native_setup(Object weak_this, |
| 153 | String nativeSoLib); |
| 154 | |
| 155 | private native final void native_finalize(int jniData); |
| 156 | |
| 157 | private native final int native_stop(int jniData); |
| 158 | |
| Jean-Baptiste Queru | a8675f6 | 2009-07-29 14:25:07 -0700 | [diff] [blame^] | 159 | private native final int native_speak(int jniData, String text, int streamType); |
| Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 160 | |
| 161 | private native final int native_synthesizeToFile(int jniData, String text, String filename); |
| 162 | |
| 163 | private native final int native_isLanguageAvailable(int jniData, String language, |
| 164 | String country, String variant); |
| 165 | |
| 166 | private native final int native_setLanguage(int jniData, String language, String country, |
| 167 | String variant); |
| 168 | |
| 169 | private native final int native_loadLanguage(int jniData, String language, String country, |
| 170 | String variant); |
| 171 | |
| 172 | private native final int native_setSpeechRate(int jniData, int speechRate); |
| 173 | |
| 174 | private native final int native_setPitch(int jniData, int speechRate); |
| 175 | |
| Jean-Baptiste Queru | cf4550c | 2009-07-21 11:16:54 -0700 | [diff] [blame] | 176 | private native final String[] native_getLanguage(int jniData); |
| 177 | |
| 178 | private native final int native_getRate(int jniData); |
| 179 | |
| 180 | private native final void native_shutdown(int jniData); |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * Callback from the C layer |
| 185 | */ |
| 186 | @SuppressWarnings("unused") |
| 187 | private static void postNativeSpeechSynthesizedInJava(Object tts_ref, |
| 188 | int bufferPointer, int bufferSize) { |
| 189 | |
| 190 | Log.i("TTS plugin debug", "bufferPointer: " + bufferPointer |
| 191 | + " bufferSize: " + bufferSize); |
| 192 | |
| 193 | SynthProxy nativeTTS = (SynthProxy)((WeakReference)tts_ref).get(); |
| 194 | // TODO notify TTS service of synthesis/playback completion, |
| 195 | // method definition to be changed. |
| 196 | } |
| 197 | } |