PageRenderTime 24ms CodeModel.GetById 8ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tts/src/com/google/tts/SynthProxyBeta.java

http://eyes-free.googlecode.com/
Java | 212 lines | 92 code | 37 blank | 83 comment | 4 complexity | 20d875d1e8caa6c271f1d2c644052840 MD5 | raw file
  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 */
 16package com.google.tts;
 17
 18import android.media.AudioManager; // import android.media.AudioSystem;
 19import android.util.Log;
 20import java.lang.ref.WeakReference;
 21
 22/**
 23 * @hide
 24 * 
 25 *       The SpeechSynthesis class provides a high-level api to create and play
 26 *       synthesized speech. This class is used internally to talk to a native
 27 *       TTS library that implements the interface defined in
 28 *       frameworks/base/include/tts/TtsEngine.h
 29 * 
 30 */
 31@SuppressWarnings("unused")
 32public class SynthProxyBeta {
 33
 34  //
 35  // External API
 36  //
 37
 38  /**
 39   * Constructor; pass the location of the native TTS .so to use.
 40   */
 41  public SynthProxyBeta(String nativeSoLib) {
 42    Log.e("TTS is loading", nativeSoLib);
 43    native_setup(new WeakReference<SynthProxyBeta>(this), nativeSoLib);
 44  }
 45
 46  /**
 47   * Stops and clears the AudioTrack.
 48   */
 49  public int stop() {
 50    return native_stop(mJniData);
 51  }
 52
 53  /**
 54   * Synthesize speech and speak it directly using AudioTrack.
 55   */
 56  public int speak(String text, int streamType) {
 57    // The following line should really be:
 58    // if ((streamType > -1) && (streamType < AudioSystem.getNumStreamTypes()))
 59    // {
 60    // but AudioSystem is not exposed publically.
 61    // Hardcoding it to "5" as that is what that call currently returns.
 62    if ((streamType > -1) && (streamType < 5)) {
 63      return native_speak(mJniData, text, streamType);
 64    } else {
 65      Log.e("SynthProxy", "Trying to speak with invalid stream type " + streamType);
 66      return native_speak(mJniData, text, AudioManager.STREAM_MUSIC);
 67    }
 68  }
 69
 70  /**
 71   * Synthesize speech to a file. The current implementation writes a valid WAV
 72   * file to the given path, assuming it is writable. Something like
 73   * "/sdcard/???.wav" is recommended.
 74   */
 75  public int synthesizeToFile(String text, String filename) {
 76    return native_synthesizeToFile(mJniData, text, filename);
 77  }
 78
 79  /**
 80   * Queries for language support. Return codes are defined in
 81   * android.speech.tts.TextToSpeech
 82   */
 83  public int isLanguageAvailable(String language, String country, String variant) {
 84    return native_isLanguageAvailable(mJniData, language, country, variant);
 85  }
 86
 87  /**
 88   * Sets the language.
 89   */
 90  public int setLanguage(String language, String country, String variant) {
 91    return native_setLanguage(mJniData, language, country, variant);
 92  }
 93
 94  /**
 95   * Loads the language: it's not set, but prepared for use later.
 96   */
 97  public int loadLanguage(String language, String country, String variant) {
 98    return native_loadLanguage(mJniData, language, country, variant);
 99  }
100
101  /**
102   * Sets the speech rate.
103   */
104  public final int setSpeechRate(int speechRate) {
105    return native_setSpeechRate(mJniData, speechRate);
106  }
107
108  /**
109   * Sets the pitch of the synthesized voice.
110   */
111  public final int setPitch(int pitch) {
112    return native_setPitch(mJniData, pitch);
113  }
114
115  /**
116   * Returns the currently set language, country and variant information.
117   */
118  public String[] getLanguage() {
119    return native_getLanguage(mJniData);
120  }
121
122  /**
123   * Gets the currently set rate.
124   */
125  public int getRate() {
126    return native_getRate(mJniData);
127  }
128
129  /**
130   * Shuts down the native synthesizer.
131   */
132  public void shutdown() {
133    native_shutdown(mJniData);
134  }
135
136  //
137  // Internal
138  //
139
140  @Override
141  protected void finalize() {
142    native_finalize(mJniData);
143    mJniData = 0;
144  }
145
146  static {
147    // System.loadLibrary("ttssynthproxy");
148    String proxyBinaryFilename = "ttssynthproxybeta";
149    int sdkInt = 4;
150    try {
151      sdkInt = Integer.parseInt(android.os.Build.VERSION.SDK);
152    } catch (NumberFormatException e) {
153      Log.e("SynthProxyBeta", "Unable to parse SDK version: " + android.os.Build.VERSION.SDK);
154    }
155    if (sdkInt > 4) {
156      proxyBinaryFilename = "ttssynthproxybeta_eclair";
157    }
158    System.loadLibrary(proxyBinaryFilename);
159  }
160
161  private final static String TAG = "SynthProxy";
162
163  /**
164   * Accessed by native methods
165   */
166  private int mJniData = 0;
167
168  private native final void native_setup(Object weak_this, String nativeSoLib);
169
170  private native final void native_finalize(int jniData);
171
172  private native final int native_stop(int jniData);
173
174  private native final int native_speak(int jniData, String text, int streamType);
175
176  private native final int native_synthesizeToFile(int jniData, String text, String filename);
177
178  private native final int native_isLanguageAvailable(int jniData, String language, String country,
179      String variant);
180
181  private native final int native_setLanguage(int jniData, String language, String country,
182      String variant);
183
184  private native final int native_loadLanguage(int jniData, String language, String country,
185      String variant);
186
187  private native final int native_setSpeechRate(int jniData, int speechRate);
188
189  private native final int native_setPitch(int jniData, int speechRate);
190
191  private native final String[] native_getLanguage(int jniData);
192
193  private native final int native_getRate(int jniData);
194
195  private native final void native_shutdown(int jniData);
196
197  private native final int native_stopSync(int jniData);
198
199  /**
200   * Callback from the C layer
201   */
202  @SuppressWarnings("unused")
203  private static void postNativeSpeechSynthesizedInJava(Object tts_ref, int bufferPointer,
204      int bufferSize) {
205
206    Log.i("TTS plugin debug", "bufferPointer: " + bufferPointer + " bufferSize: " + bufferSize);
207
208    SynthProxyBeta nativeTTS = (SynthProxyBeta) ((WeakReference) tts_ref).get();
209    // TODO notify TTS service of synthesis/playback completion,
210    // method definition to be changed.
211  }
212}