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