/tts/src/com/google/tts/SpeechSynthesis.java

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