/native/frameworks/espeakengine/java/SpeechSynthesis.java
Java | 103 lines | 39 code | 18 blank | 46 comment | 0 complexity | a726d3b518afb547948fb0315be8f107 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.marvin.espeak; 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 private SynthReadyCallback cb; 32 33 public interface SynthReadyCallback{ 34 void onSynthReadyListener(short[] audioData); 35 } 36 37 // 38 // External API 39 // 40 41 /** 42 * Constructor; pass a language code such as "en" for English. 43 */ 44 public SpeechSynthesis(SynthReadyCallback callback, String language, 45 int languageVariant, int speechRate) { 46 cb = callback; 47 native_setup(new WeakReference<SpeechSynthesis>(this), language, 48 languageVariant, speechRate); 49 } 50 51 /** 52 * Synthesize speech; results will be passed back to the TTS API using a 53 * callback. 54 */ 55 public native final void synth(String text); 56 57 /** 58 * Stops synthesis 59 */ 60 public native final void stop(); 61 62 /** 63 * Sets the language 64 */ 65 public native final void setLanguage(String language, int languageVariant); 66 67 /** 68 * Sets the speech rate 69 */ 70 public native final void setSpeechRate(int speechRate); 71 72 // 73 // Internal 74 // 75 76 static { 77 System.loadLibrary("espeakengine"); 78 } 79 80 private final static String TAG = "SpeechSynthesis"; 81 82 private int mNativeContext; // accessed by native methods 83 84 private native final void native_setup(Object weak_this, String language, 85 int languageVariant, int speechRate); 86 87 private native final void native_finalize(); 88 89 protected void finalize() { 90 native_finalize(); 91 } 92 93 /** 94 * Callback from the C layer 95 */ 96 @SuppressWarnings("unused") 97 private static void postNativeSpeechSynthesizedInJava(Object tts_ref, 98 short[] audioData) { 99 SpeechSynthesis nativeTTS = (SpeechSynthesis) ((WeakReference) tts_ref) 100 .get(); 101 nativeTTS.cb.onSynthReadyListener(audioData); 102 } 103}