/native/frameworks/espeakengine/java/SpeechSynthesis.java

http://eyes-free.googlecode.com/ · 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. */
  16. package com.google.marvin.espeak;
  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. private SynthReadyCallback cb;
  29. public interface SynthReadyCallback{
  30. void onSynthReadyListener(short[] audioData);
  31. }
  32. //
  33. // External API
  34. //
  35. /**
  36. * Constructor; pass a language code such as "en" for English.
  37. */
  38. public SpeechSynthesis(SynthReadyCallback callback, String language,
  39. int languageVariant, int speechRate) {
  40. cb = callback;
  41. native_setup(new WeakReference<SpeechSynthesis>(this), language,
  42. languageVariant, speechRate);
  43. }
  44. /**
  45. * Synthesize speech; results will be passed back to the TTS API using a
  46. * callback.
  47. */
  48. public native final void synth(String text);
  49. /**
  50. * Stops synthesis
  51. */
  52. public native final void stop();
  53. /**
  54. * Sets the language
  55. */
  56. public native final void setLanguage(String language, int languageVariant);
  57. /**
  58. * Sets the speech rate
  59. */
  60. public native final void setSpeechRate(int speechRate);
  61. //
  62. // Internal
  63. //
  64. static {
  65. System.loadLibrary("espeakengine");
  66. }
  67. private final static String TAG = "SpeechSynthesis";
  68. private int mNativeContext; // accessed by native methods
  69. private native final void native_setup(Object weak_this, String language,
  70. int languageVariant, int speechRate);
  71. private native final void native_finalize();
  72. protected void finalize() {
  73. native_finalize();
  74. }
  75. /**
  76. * Callback from the C layer
  77. */
  78. @SuppressWarnings("unused")
  79. private static void postNativeSpeechSynthesizedInJava(Object tts_ref,
  80. short[] audioData) {
  81. SpeechSynthesis nativeTTS = (SpeechSynthesis) ((WeakReference) tts_ref)
  82. .get();
  83. nativeTTS.cb.onSynthReadyListener(audioData);
  84. }
  85. }