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.

CheckVoiceData.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.googlecode.eyesfree.espeak;
  17. import android.app.Activity;
  18. import android.content.Context;
  19. import android.content.Intent;
  20. import android.os.Bundle;
  21. import android.speech.tts.TextToSpeech;
  22. import android.speech.tts.TextToSpeech.Engine;
  23. import android.util.Log;
  24. import com.googlecode.eyesfree.espeak.SpeechSynthesis.SynthReadyCallback;
  25. import com.googlecode.eyesfree.espeak.SpeechSynthesis.Voice;
  26. import java.io.File;
  27. import java.util.ArrayList;
  28. import java.util.HashSet;
  29. import java.util.List;
  30. import java.util.Locale;
  31. import java.util.Set;
  32. public class CheckVoiceData extends Activity {
  33. private static final String TAG = "eSpeakTTS";
  34. private static final int REQUEST_DOWNLOAD = 1;
  35. /** Resources required for eSpeak to run correctly. */
  36. private static final String[] BASE_RESOURCES = {
  37. "intonations", "phondata", "phonindex", "phontab", "en_dict", "voices/en/en-us"
  38. };
  39. public static File getDataPath(Context context) {
  40. return new File(context.getDir("voices", MODE_WORLD_READABLE), "espeak-data");
  41. }
  42. public static boolean hasBaseResources(Context context) {
  43. final File dataPath = getDataPath(context);
  44. for (String resource : BASE_RESOURCES) {
  45. final File resourceFile = new File(dataPath, resource);
  46. if (!resourceFile.exists()) {
  47. Log.e(TAG, "Missing base resource: " + resourceFile.getPath());
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. @Override
  54. protected void onCreate(Bundle savedInstanceState) {
  55. super.onCreate(savedInstanceState);
  56. checkForVoices(false);
  57. }
  58. @Override
  59. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  60. switch (requestCode) {
  61. case REQUEST_DOWNLOAD:
  62. checkForVoices(true);
  63. break;
  64. }
  65. }
  66. private void checkForVoices(boolean attemptedInstall) {
  67. final File dataPath = getDataPath(this);
  68. ArrayList<String> availableLanguages = new ArrayList<String>();
  69. ArrayList<String> unavailableLanguages = new ArrayList<String>();
  70. if (!hasBaseResources(this)) {
  71. if (!attemptedInstall) {
  72. downloadVoiceData();
  73. return;
  74. }
  75. // No base resource, can't load available voices.
  76. unavailableLanguages.add(Locale.ENGLISH.toString());
  77. returnResults(Engine.CHECK_VOICE_DATA_MISSING_DATA, dataPath, availableLanguages,
  78. unavailableLanguages);
  79. return;
  80. }
  81. final SpeechSynthesis engine = new SpeechSynthesis(this, mSynthReadyCallback);
  82. final List<Voice> voices = engine.getAvailableVoices();
  83. for (Voice voice : voices) {
  84. availableLanguages.add(voice.toString());
  85. }
  86. final ArrayList<String> checkFor = getIntent().getStringArrayListExtra(
  87. TextToSpeech.Engine.EXTRA_CHECK_VOICE_DATA_FOR);
  88. if (checkFor != null && !checkFor.isEmpty()) {
  89. final Set<String> checkForSet = new HashSet<String>(checkFor);
  90. availableLanguages = filter(availableLanguages, checkForSet);
  91. unavailableLanguages = filter(unavailableLanguages, checkForSet);
  92. }
  93. returnResults(Engine.CHECK_VOICE_DATA_PASS, dataPath, availableLanguages,
  94. unavailableLanguages);
  95. }
  96. /**
  97. * Launches the voice data installer.
  98. */
  99. private void downloadVoiceData() {
  100. final Intent checkIntent = new Intent(this, DownloadVoiceData.class);
  101. startActivityForResult(checkIntent, REQUEST_DOWNLOAD);
  102. }
  103. private void returnResults(int result, File dataPath, ArrayList<String> availableLanguages,
  104. ArrayList<String> unavailableLanguages) {
  105. final Intent returnData = new Intent();
  106. returnData.putStringArrayListExtra(Engine.EXTRA_AVAILABLE_VOICES, availableLanguages);
  107. returnData.putStringArrayListExtra(Engine.EXTRA_UNAVAILABLE_VOICES, unavailableLanguages);
  108. // Don't bother returning Engine.EXTRA_VOICE_DATA_FILES,
  109. // Engine.EXTRA_VOICE_DATA_FILES_INFO, or
  110. // Engine.EXTRA_VOICE_DATA_ROOT_DIRECTORY
  111. // because they're don't seem necessary.
  112. setResult(result, returnData);
  113. finish();
  114. }
  115. /**
  116. * Filters a given array list, maintaining only elements that are in the
  117. * constraint. Returns a new list containing only the filtered elements.
  118. */
  119. private ArrayList<String> filter(ArrayList<String> in, Set<String> constraint) {
  120. final ArrayList<String> out = new ArrayList<String>(constraint.size());
  121. for (String s : in) {
  122. if (constraint.contains(s)) {
  123. out.add(s);
  124. }
  125. }
  126. return out;
  127. }
  128. private final SynthReadyCallback mSynthReadyCallback = new SynthReadyCallback() {
  129. @Override
  130. public void onSynthDataReady(byte[] audioData) {
  131. // Do nothing.
  132. }
  133. @Override
  134. public void onSynthDataComplete() {
  135. // Do nothing.
  136. }
  137. };
  138. }