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