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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2012-2013 Reece H. Dunn
  3. * Copyright (C) 2009 The Android Open Source Project
  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 Activity is used by Android to get the list of languages to display
  19. * to the user when selecting the text-to-speech language. This is by locale,
  20. * not voice name.
  21. */
  22. package com.reecedunn.espeak;
  23. import android.app.Activity;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.os.Bundle;
  27. import android.speech.tts.TextToSpeech.Engine;
  28. import android.util.Log;
  29. import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback;
  30. import java.io.ByteArrayOutputStream;
  31. import java.io.File;
  32. import java.io.FileInputStream;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. import java.util.Locale;
  38. import java.util.Set;
  39. public class CheckVoiceData extends Activity {
  40. private static final String TAG = "eSpeakTTS";
  41. /** Resources required for eSpeak to run correctly. */
  42. private static final String[] BASE_RESOURCES = {
  43. "version",
  44. "intonations",
  45. "phondata",
  46. "phonindex",
  47. "phontab",
  48. "en_dict",
  49. };
  50. public static File getDataPath(Context context) {
  51. return new File(context.getDir("voices", MODE_PRIVATE), "espeak-data");
  52. }
  53. public static boolean hasBaseResources(Context context) {
  54. final File dataPath = getDataPath(context);
  55. for (String resource : BASE_RESOURCES) {
  56. final File resourceFile = new File(dataPath, resource);
  57. if (!resourceFile.exists()) {
  58. Log.e(TAG, "Missing base resource: " + resourceFile.getPath());
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. public static String readContent(InputStream stream) throws IOException {
  65. ByteArrayOutputStream content = new ByteArrayOutputStream();
  66. int c = stream.read();
  67. while (c != -1)
  68. {
  69. content.write((byte)c);
  70. c = stream.read();
  71. }
  72. return content.toString();
  73. }
  74. public static boolean canUpgradeResources(Context context) {
  75. try {
  76. final String version = readContent(context.getResources().openRawResource(R.raw.espeakdata_version));
  77. final String installedVersion = readContent(new FileInputStream(new File(getDataPath(context), "version")));
  78. return !version.equals(installedVersion);
  79. } catch (Exception e) {
  80. return false;
  81. }
  82. }
  83. @Override
  84. protected void onCreate(Bundle savedInstanceState) {
  85. super.onCreate(savedInstanceState);
  86. final File dataPath = getDataPath(this);
  87. ArrayList<String> availableLanguages = new ArrayList<String>();
  88. ArrayList<String> unavailableLanguages = new ArrayList<String>();
  89. boolean haveBaseResources = hasBaseResources(this);
  90. if (!haveBaseResources || canUpgradeResources(this)) {
  91. if (!haveBaseResources) {
  92. unavailableLanguages.add(Locale.ENGLISH.toString());
  93. }
  94. returnResults(Engine.CHECK_VOICE_DATA_FAIL, dataPath, availableLanguages,
  95. unavailableLanguages);
  96. return;
  97. }
  98. final SpeechSynthesis engine = new SpeechSynthesis(this, mSynthReadyCallback);
  99. final List<Voice> voices = engine.getAvailableVoices();
  100. for (Voice voice : voices) {
  101. availableLanguages.add(voice.toString());
  102. }
  103. returnResults(Engine.CHECK_VOICE_DATA_PASS, dataPath, availableLanguages,
  104. unavailableLanguages);
  105. }
  106. private void returnResults(int result, File dataPath, ArrayList<String> availableLanguages,
  107. ArrayList<String> unavailableLanguages) {
  108. final Intent returnData = new Intent();
  109. returnData.putStringArrayListExtra(Engine.EXTRA_AVAILABLE_VOICES, availableLanguages);
  110. returnData.putStringArrayListExtra(Engine.EXTRA_UNAVAILABLE_VOICES, unavailableLanguages);
  111. setResult(result, returnData);
  112. finish();
  113. }
  114. /**
  115. * Filters a given array list, maintaining only elements that are in the
  116. * constraint. Returns a new list containing only the filtered elements.
  117. */
  118. private ArrayList<String> filter(ArrayList<String> in, Set<String> constraint) {
  119. final ArrayList<String> out = new ArrayList<String>(constraint.size());
  120. for (String s : in) {
  121. if (constraint.contains(s)) {
  122. out.add(s);
  123. }
  124. }
  125. return out;
  126. }
  127. private final SynthReadyCallback mSynthReadyCallback = new SynthReadyCallback() {
  128. @Override
  129. public void onSynthDataReady(byte[] audioData) {
  130. // Do nothing.
  131. }
  132. @Override
  133. public void onSynthDataComplete() {
  134. // Do nothing.
  135. }
  136. };
  137. }