eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TtsService.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (C) 2012-2013 Reece H. Dunn
  3. * Copyright (C) 2011 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * This file implements the Android Text-to-Speech engine for eSpeak.
  19. *
  20. * Android Version: 4.0 (Ice Cream Sandwich)
  21. * API Version: 14
  22. */
  23. package com.reecedunn.espeak;
  24. import android.annotation.SuppressLint;
  25. import android.content.BroadcastReceiver;
  26. import android.content.Context;
  27. import android.content.Intent;
  28. import android.content.IntentFilter;
  29. import android.media.AudioTrack;
  30. import android.os.Bundle;
  31. import android.preference.PreferenceManager;
  32. import android.speech.tts.SynthesisCallback;
  33. import android.speech.tts.SynthesisRequest;
  34. import android.speech.tts.TextToSpeech;
  35. import android.speech.tts.TextToSpeechService;
  36. import android.util.Log;
  37. import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback;
  38. import java.util.List;
  39. import java.util.Locale;
  40. /**
  41. * Implements the eSpeak engine as a {@link TextToSpeechService}.
  42. *
  43. * @author [email protected] (Reece H. Dunn)
  44. * @author [email protected] (Alan Viverette)
  45. */
  46. @SuppressLint("NewApi")
  47. public class TtsService extends TextToSpeechService {
  48. private static final String TAG = TtsService.class.getSimpleName();
  49. private static final boolean DEBUG = false;
  50. private static final String DEFAULT_LANGUAGE = "en";
  51. private static final String DEFAULT_COUNTRY = "uk";
  52. private static final String DEFAULT_VARIANT = "";
  53. private SpeechSynthesis mEngine;
  54. private SynthesisCallback mCallback;
  55. private List<Voice> mAvailableVoices;
  56. private Voice mMatchingVoice = null;
  57. private BroadcastReceiver mOnLanguagesDownloaded = null;
  58. private String mLanguage = DEFAULT_LANGUAGE;
  59. private String mCountry = DEFAULT_COUNTRY;
  60. private String mVariant = DEFAULT_VARIANT;
  61. @Override
  62. public void onCreate() {
  63. initializeTtsEngine();
  64. super.onCreate();
  65. }
  66. @Override
  67. public void onDestroy() {
  68. super.onDestroy();
  69. if (mOnLanguagesDownloaded != null) {
  70. unregisterReceiver(mOnLanguagesDownloaded);
  71. }
  72. }
  73. /**
  74. * Sets up the native eSpeak engine.
  75. */
  76. private void initializeTtsEngine() {
  77. if (mEngine != null) {
  78. mEngine.stop();
  79. mEngine = null;
  80. }
  81. mEngine = new SpeechSynthesis(this, mSynthCallback);
  82. mAvailableVoices = mEngine.getAvailableVoices();
  83. }
  84. @Override
  85. protected String[] onGetLanguage() {
  86. // This is used to specify the language requested from GetSampleText.
  87. return new String[] {
  88. mLanguage, mCountry, mVariant
  89. };
  90. }
  91. @Override
  92. protected int onIsLanguageAvailable(String language, String country, String variant) {
  93. if (!CheckVoiceData.hasBaseResources(this) || CheckVoiceData.canUpgradeResources(this)) {
  94. if (mOnLanguagesDownloaded == null) {
  95. mOnLanguagesDownloaded = new BroadcastReceiver() {
  96. @Override
  97. public void onReceive(Context context, Intent intent) {
  98. initializeTtsEngine();
  99. }
  100. };
  101. final IntentFilter filter = new IntentFilter(DownloadVoiceData.BROADCAST_LANGUAGES_UPDATED);
  102. registerReceiver(mOnLanguagesDownloaded, filter);
  103. }
  104. final Intent intent = new Intent(this, DownloadVoiceData.class);
  105. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  106. startActivity(intent);
  107. return TextToSpeech.LANG_MISSING_DATA;
  108. }
  109. final Locale query = new Locale(language, country, variant);
  110. Voice languageVoice = null;
  111. Voice countryVoice = null;
  112. synchronized (mAvailableVoices) {
  113. for (Voice voice : mAvailableVoices) {
  114. switch (voice.match(query)) {
  115. case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
  116. mMatchingVoice = voice;
  117. return TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE;
  118. case TextToSpeech.LANG_COUNTRY_AVAILABLE:
  119. countryVoice = voice;
  120. case TextToSpeech.LANG_AVAILABLE:
  121. languageVoice = voice;
  122. break;
  123. }
  124. }
  125. }
  126. if (languageVoice == null) {
  127. mMatchingVoice = null;
  128. return TextToSpeech.LANG_NOT_SUPPORTED;
  129. } else if (countryVoice == null) {
  130. mMatchingVoice = languageVoice;
  131. return TextToSpeech.LANG_AVAILABLE;
  132. } else {
  133. mMatchingVoice = countryVoice;
  134. return TextToSpeech.LANG_COUNTRY_AVAILABLE;
  135. }
  136. }
  137. @Override
  138. protected int onLoadLanguage(String language, String country, String variant) {
  139. final int result = onIsLanguageAvailable(language, country, variant);
  140. switch (result) {
  141. case TextToSpeech.LANG_AVAILABLE:
  142. synchronized (this) {
  143. mLanguage = language;
  144. mCountry = "";
  145. mVariant = "";
  146. }
  147. break;
  148. case TextToSpeech.LANG_COUNTRY_AVAILABLE:
  149. synchronized (this) {
  150. mLanguage = language;
  151. mCountry = ((country == null) ? "" : country);
  152. mVariant = "";
  153. }
  154. break;
  155. case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
  156. synchronized (this) {
  157. mLanguage = language;
  158. mCountry = ((country == null) ? "" : country);
  159. mVariant = ((variant == null) ? "" : variant);
  160. }
  161. break;
  162. case TextToSpeech.LANG_NOT_SUPPORTED:
  163. Log.e(TAG, "Unsupported language {language='" + language + "', country='" + country
  164. + "', variant='" + variant + "'}");
  165. break;
  166. }
  167. return result;
  168. }
  169. @Override
  170. protected void onStop() {
  171. Log.i(TAG, "Received stop request.");
  172. mEngine.stop();
  173. }
  174. @Override
  175. protected synchronized void onSynthesizeText(
  176. SynthesisRequest request, SynthesisCallback callback) {
  177. final int result = onLoadLanguage(request.getLanguage(), request.getCountry(), request.getVariant());
  178. switch (result) {
  179. case TextToSpeech.LANG_MISSING_DATA:
  180. case TextToSpeech.LANG_NOT_SUPPORTED:
  181. return;
  182. }
  183. String text = request.getText();
  184. if (text == null)
  185. return;
  186. if (DEBUG) {
  187. Log.i(TAG, "Received synthesis request: {language=\"" + mMatchingVoice.name + "\"}");
  188. final Bundle params = request.getParams();
  189. for (String key : params.keySet()) {
  190. Log.v(TAG,
  191. "Synthesis request contained param {" + key + ", " + params.get(key) + "}");
  192. }
  193. }
  194. if (text.startsWith("<?xml"))
  195. {
  196. // eSpeak does not recognise/skip "<?...?>" preprocessing tags,
  197. // so need to remove these before passing to synthesize.
  198. text = text.substring(text.indexOf("?>") + 2).trim();
  199. }
  200. mCallback = callback;
  201. mCallback.start(mEngine.getSampleRate(), mEngine.getAudioFormat(), mEngine.getChannelCount());
  202. final VoiceSettings settings = new VoiceSettings(PreferenceManager.getDefaultSharedPreferences(this), mEngine);
  203. mEngine.setVoice(mMatchingVoice, settings.getVoiceVariant());
  204. mEngine.Rate.setValue(settings.getRate(), request.getSpeechRate());
  205. mEngine.Pitch.setValue(settings.getPitch(), request.getPitch());
  206. mEngine.PitchRange.setValue(settings.getPitchRange());
  207. mEngine.Volume.setValue(settings.getVolume());
  208. mEngine.Punctuation.setValue(settings.getPunctuationLevel());
  209. mEngine.setPunctuationCharacters(settings.getPunctuationCharacters());
  210. mEngine.synthesize(text, text.startsWith("<speak"));
  211. }
  212. /**
  213. * Pipes synthesizer output from native eSpeak to an {@link AudioTrack}.
  214. */
  215. private final SpeechSynthesis.SynthReadyCallback mSynthCallback = new SynthReadyCallback() {
  216. @Override
  217. public void onSynthDataReady(byte[] audioData) {
  218. if ((audioData == null) || (audioData.length == 0)) {
  219. onSynthDataComplete();
  220. return;
  221. }
  222. final int maxBytesToCopy = mCallback.getMaxBufferSize();
  223. int offset = 0;
  224. while (offset < audioData.length) {
  225. final int bytesToWrite = Math.min(maxBytesToCopy, (audioData.length - offset));
  226. mCallback.audioAvailable(audioData, offset, bytesToWrite);
  227. offset += bytesToWrite;
  228. }
  229. }
  230. @Override
  231. public void onSynthDataComplete() {
  232. mCallback.done();
  233. }
  234. };
  235. }