/tts/src/com/google/tts/SpeechSynthesis.java
Java | 121 lines | 35 code | 24 blank | 62 comment | 0 complexity | ca85d8118a4946a5863f718fc432326b MD5 | raw file
1/* 2 * Copyright (C) 2008 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 com.google.tts; 17 18import android.util.Log; 19import java.lang.ref.WeakReference; 20import java.nio.ByteBuffer; 21 22 23/** 24 * The SpeechSynthesis class provides a high-level api to create and play 25 * synthesized speech. 26 * 27 * @author dmazzoni@gmail.com (Dominic Mazzoni) 28 */ 29@SuppressWarnings("unused") 30public class SpeechSynthesis { 31 32 // 33 // External API 34 // 35 36 /** 37 * Constructor; pass a language code such as "en" for English. 38 */ 39 public SpeechSynthesis(String nativeSoLib) { 40 native_setup(new WeakReference<SpeechSynthesis>(this), nativeSoLib); 41 } 42 43 /** 44 * Stops and clears the AudioTrack. 45 */ 46 public native final void stop(); 47 48 /** 49 * Synthesize speech and speak it directly using AudioTrack. 50 */ 51 public native final void speak(String text); 52 53 /** 54 * Synthesize speech to a file. The current implementation writes a valid WAV 55 * file to the given path, assuming it is writable. Something like 56 * "/sdcard/???.wav" is recommended. 57 */ 58 public native final void synthesizeToFile(String text, String filename); 59 60 /** 61 * Sets the language 62 */ 63 public native final void setLanguage(String language); 64 65 /** 66 * Sets the speech rate 67 */ 68 public native final void setSpeechRate(int speechRate); 69 70 71 /** 72 * Plays the given audio buffer 73 */ 74 public native final void playAudioBuffer(int bufferPointer, int bufferSize); 75 76 77 /** 78 * Gets the currently set language 79 */ 80 public native final String getLanguage(); 81 82 /** 83 * Gets the currently set rate 84 */ 85 public native final int getRate(); 86 87 /** 88 * Shutsdown the native synthesizer 89 */ 90 public native final void shutdown(); 91 92 // 93 // Internal 94 // 95 96 static { 97 System.loadLibrary("speechsynthesis"); 98 } 99 100 private final static String TAG = "SpeechSynthesis"; 101 102 private int mNativeContext; // accessed by native methods 103 104 private native final void native_setup(Object weak_this, String nativeSoLib); 105 106 private native final void native_finalize(); 107 108 protected void finalize() { 109 native_finalize(); 110 } 111 112 /** 113 * Callback from the C layer 114 */ 115 @SuppressWarnings("unused") 116 private static void postNativeSpeechSynthesizedInJava(Object tts_ref, int bufferPointer, int bufferSize){ 117 Log.i("TTS plugin debug", "bufferPointer: " + bufferPointer + " bufferSize: " + bufferSize); 118 SpeechSynthesis nativeTTS = (SpeechSynthesis)((WeakReference)tts_ref).get(); 119 nativeTTS.playAudioBuffer(bufferPointer, bufferSize); 120 } 121}