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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.Build;
  31. import android.os.Bundle;
  32. import android.preference.PreferenceManager;
  33. import android.speech.tts.SynthesisCallback;
  34. import android.speech.tts.SynthesisRequest;
  35. import android.speech.tts.TextToSpeech;
  36. import android.speech.tts.TextToSpeechService;
  37. import android.util.Log;
  38. import android.util.Pair;
  39. import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback;
  40. import java.util.ArrayList;
  41. import java.util.HashMap;
  42. import java.util.List;
  43. import java.util.Locale;
  44. import java.util.Map;
  45. /**
  46. * Implements the eSpeak engine as a {@link TextToSpeechService}.
  47. *
  48. * @author [email protected] (Reece H. Dunn)
  49. * @author [email protected] (Alan Viverette)
  50. */
  51. @SuppressLint("NewApi")
  52. public class TtsService extends TextToSpeechService {
  53. public static final String ESPEAK_INITIALIZED = "com.reecedunn.espeak.ESPEAK_INITIALIZED";
  54. private static final String TAG = TtsService.class.getSimpleName();
  55. private static final boolean DEBUG = false;
  56. private SpeechSynthesis mEngine;
  57. private SynthesisCallback mCallback;
  58. private final Map<String, Voice> mAvailableVoices = new HashMap<String, Voice>();
  59. protected Voice mMatchingVoice = null;
  60. private BroadcastReceiver mOnLanguagesDownloaded = null;
  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.clear();
  83. for (Voice voice : mEngine.getAvailableVoices()) {
  84. mAvailableVoices.put(voice.name, voice);
  85. }
  86. final Intent intent = new Intent(ESPEAK_INITIALIZED);
  87. sendBroadcast(intent);
  88. }
  89. @Override
  90. protected String[] onGetLanguage() {
  91. // This is used to specify the language requested from GetSampleText.
  92. if (mMatchingVoice == null) {
  93. return new String[] { "eng", "GBR", "" };
  94. }
  95. return new String[] {
  96. mMatchingVoice.locale.getISO3Language(),
  97. mMatchingVoice.locale.getISO3Country(),
  98. mMatchingVoice.locale.getVariant()
  99. };
  100. }
  101. private Pair<Voice, Integer> findVoice(String language, String country, String variant) {
  102. if (!CheckVoiceData.hasBaseResources(this) || CheckVoiceData.canUpgradeResources(this)) {
  103. if (mOnLanguagesDownloaded == null) {
  104. mOnLanguagesDownloaded = new BroadcastReceiver() {
  105. @Override
  106. public void onReceive(Context context, Intent intent) {
  107. initializeTtsEngine();
  108. }
  109. };
  110. final IntentFilter filter = new IntentFilter(DownloadVoiceData.BROADCAST_LANGUAGES_UPDATED);
  111. registerReceiver(mOnLanguagesDownloaded, filter);
  112. }
  113. final Intent intent = new Intent(this, DownloadVoiceData.class);
  114. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  115. startActivity(intent);
  116. return new Pair<>(null, TextToSpeech.LANG_MISSING_DATA);
  117. }
  118. final Locale query = new Locale(language, country, variant);
  119. Voice languageVoice = null;
  120. Voice countryVoice = null;
  121. synchronized (mAvailableVoices) {
  122. for (Voice voice : mAvailableVoices.values()) {
  123. switch (voice.match(query)) {
  124. case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
  125. return new Pair<>(voice, TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE);
  126. case TextToSpeech.LANG_COUNTRY_AVAILABLE:
  127. countryVoice = voice;
  128. case TextToSpeech.LANG_AVAILABLE:
  129. languageVoice = voice;
  130. break;
  131. }
  132. }
  133. }
  134. if (languageVoice == null) {
  135. return new Pair<>(null, TextToSpeech.LANG_NOT_SUPPORTED);
  136. } else if (countryVoice == null) {
  137. return new Pair<>(languageVoice, TextToSpeech.LANG_AVAILABLE);
  138. } else {
  139. return new Pair<>(countryVoice, TextToSpeech.LANG_COUNTRY_AVAILABLE);
  140. }
  141. }
  142. private Pair<Voice, Integer> getDefaultVoiceFor(String language, String country, String variant) {
  143. final Pair<Voice, Integer> match = findVoice(language, country, variant);
  144. switch (match.second) {
  145. case TextToSpeech.LANG_AVAILABLE:
  146. return new Pair<>(findVoice(language, "", "").first, match.second);
  147. case TextToSpeech.LANG_COUNTRY_AVAILABLE:
  148. return new Pair<>(findVoice(language, country, "").first, match.second);
  149. default:
  150. return match;
  151. }
  152. }
  153. @Override
  154. protected int onIsLanguageAvailable(String language, String country, String variant) {
  155. return findVoice(language, country, variant).second;
  156. }
  157. @Override
  158. protected int onLoadLanguage(String language, String country, String variant) {
  159. final Pair<Voice, Integer> match = getDefaultVoiceFor(language, country, variant);
  160. if (match.first != null) {
  161. mMatchingVoice = match.first;
  162. }
  163. return match.second;
  164. }
  165. @Override
  166. public String onGetDefaultVoiceNameFor(String language, String country, String variant) {
  167. final Voice match = getDefaultVoiceFor(language, country, variant).first;
  168. return (match == null) ? null : match.name;
  169. }
  170. @Override
  171. public List<android.speech.tts.Voice> onGetVoices() {
  172. List<android.speech.tts.Voice> voices = new ArrayList<android.speech.tts.Voice>();
  173. for (Voice voice : mAvailableVoices.values()) {
  174. int quality = android.speech.tts.Voice.QUALITY_NORMAL;
  175. int latency = android.speech.tts.Voice.LATENCY_VERY_LOW;
  176. voices.add(new android.speech.tts.Voice(voice.name, new Locale(voice.locale.getISO3Language(), voice.locale.getISO3Country(), voice.locale.getVariant()), quality, latency, false, null));
  177. }
  178. return voices;
  179. }
  180. @Override
  181. public int onIsValidVoiceName(String name) {
  182. Voice voice = mAvailableVoices.get(name);
  183. return (voice == null) ? TextToSpeech.ERROR : TextToSpeech.SUCCESS;
  184. }
  185. @Override
  186. public int onLoadVoice(String name) {
  187. Voice voice = mAvailableVoices.get(name);
  188. if (voice == null) {
  189. return TextToSpeech.ERROR;
  190. }
  191. mMatchingVoice = voice;
  192. return TextToSpeech.SUCCESS;
  193. }
  194. @Override
  195. protected void onStop() {
  196. Log.i(TAG, "Received stop request.");
  197. mEngine.stop();
  198. }
  199. @SuppressWarnings("deprecation")
  200. private String getRequestString(SynthesisRequest request) {
  201. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  202. return request.getCharSequenceText().toString();
  203. } else {
  204. return request.getText();
  205. }
  206. }
  207. private int selectVoice(SynthesisRequest request) {
  208. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  209. final String name = request.getVoiceName();
  210. if (name != null && !name.isEmpty()) {
  211. return onLoadVoice(name);
  212. }
  213. }
  214. final int result = onLoadLanguage(request.getLanguage(), request.getCountry(), request.getVariant());
  215. switch (result) {
  216. case TextToSpeech.LANG_MISSING_DATA:
  217. case TextToSpeech.LANG_NOT_SUPPORTED:
  218. return TextToSpeech.ERROR;
  219. }
  220. return TextToSpeech.SUCCESS;
  221. }
  222. @Override
  223. protected synchronized void onSynthesizeText(SynthesisRequest request, SynthesisCallback callback) {
  224. if (selectVoice(request) == TextToSpeech.ERROR)
  225. return;
  226. String text = getRequestString(request);
  227. if (text == null)
  228. return;
  229. if (DEBUG) {
  230. Log.i(TAG, "Received synthesis request: {language=\"" + mMatchingVoice.name + "\"}");
  231. final Bundle params = request.getParams();
  232. for (String key : params.keySet()) {
  233. Log.v(TAG,
  234. "Synthesis request contained param {" + key + ", " + params.get(key) + "}");
  235. }
  236. }
  237. if (text.startsWith("<?xml"))
  238. {
  239. // eSpeak does not recognise/skip "<?...?>" preprocessing tags,
  240. // so need to remove these before passing to synthesize.
  241. text = text.substring(text.indexOf("?>") + 2).trim();
  242. }
  243. mCallback = callback;
  244. mCallback.start(mEngine.getSampleRate(), mEngine.getAudioFormat(), mEngine.getChannelCount());
  245. final VoiceSettings settings = new VoiceSettings(PreferenceManager.getDefaultSharedPreferences(this), mEngine);
  246. mEngine.setVoice(mMatchingVoice, settings.getVoiceVariant());
  247. mEngine.Rate.setValue(settings.getRate(), request.getSpeechRate());
  248. mEngine.Pitch.setValue(settings.getPitch(), request.getPitch());
  249. mEngine.PitchRange.setValue(settings.getPitchRange());
  250. mEngine.Volume.setValue(settings.getVolume());
  251. mEngine.Punctuation.setValue(settings.getPunctuationLevel());
  252. mEngine.setPunctuationCharacters(settings.getPunctuationCharacters());
  253. mEngine.synthesize(text, text.startsWith("<speak"));
  254. }
  255. /**
  256. * Pipes synthesizer output from native eSpeak to an {@link AudioTrack}.
  257. */
  258. private final SpeechSynthesis.SynthReadyCallback mSynthCallback = new SynthReadyCallback() {
  259. @Override
  260. public void onSynthDataReady(byte[] audioData) {
  261. if ((audioData == null) || (audioData.length == 0)) {
  262. onSynthDataComplete();
  263. return;
  264. }
  265. final int maxBytesToCopy = mCallback.getMaxBufferSize();
  266. int offset = 0;
  267. while (offset < audioData.length) {
  268. final int bytesToWrite = Math.min(maxBytesToCopy, (audioData.length - offset));
  269. mCallback.audioAvailable(audioData, offset, bytesToWrite);
  270. offset += bytesToWrite;
  271. }
  272. }
  273. @Override
  274. public void onSynthDataComplete() {
  275. mCallback.done();
  276. }
  277. };
  278. }