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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. public class CheckVoiceData extends Activity {
  39. private static final String TAG = "eSpeakTTS";
  40. /** Resources required for eSpeak to run correctly. */
  41. private static final String[] BASE_RESOURCES = {
  42. "version",
  43. "intonations",
  44. "phondata",
  45. "phonindex",
  46. "phontab",
  47. "en_dict",
  48. };
  49. public static File getDataPath(Context context) {
  50. return new File(context.getDir("voices", MODE_PRIVATE), "espeak-data");
  51. }
  52. public static boolean hasBaseResources(Context context) {
  53. final File dataPath = getDataPath(context);
  54. for (String resource : BASE_RESOURCES) {
  55. final File resourceFile = new File(dataPath, resource);
  56. if (!resourceFile.exists()) {
  57. Log.e(TAG, "Missing base resource: " + resourceFile.getPath());
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. public static boolean canUpgradeResources(Context context) {
  64. try {
  65. final String version = FileUtils.read(context.getResources().openRawResource(R.raw.espeakdata_version));
  66. final String installedVersion = FileUtils.read(new File(getDataPath(context), "version"));
  67. return !version.equals(installedVersion);
  68. } catch (Exception e) {
  69. return false;
  70. }
  71. }
  72. @Override
  73. protected void onCreate(Bundle savedInstanceState) {
  74. super.onCreate(savedInstanceState);
  75. ArrayList<String> availableLanguages = new ArrayList<String>();
  76. ArrayList<String> unavailableLanguages = new ArrayList<String>();
  77. boolean haveBaseResources = hasBaseResources(this);
  78. if (!haveBaseResources || canUpgradeResources(this)) {
  79. if (!haveBaseResources) {
  80. unavailableLanguages.add(Locale.ENGLISH.toString());
  81. }
  82. returnResults(Engine.CHECK_VOICE_DATA_FAIL, availableLanguages, unavailableLanguages);
  83. return;
  84. }
  85. final SpeechSynthesis engine = new SpeechSynthesis(this, mSynthReadyCallback);
  86. final List<Voice> voices = engine.getAvailableVoices();
  87. for (Voice voice : voices) {
  88. availableLanguages.add(voice.toString());
  89. }
  90. returnResults(Engine.CHECK_VOICE_DATA_PASS, availableLanguages, unavailableLanguages);
  91. }
  92. private void returnResults(int result, ArrayList<String> availableLanguages, ArrayList<String> unavailableLanguages) {
  93. final Intent returnData = new Intent();
  94. returnData.putStringArrayListExtra(Engine.EXTRA_AVAILABLE_VOICES, availableLanguages);
  95. returnData.putStringArrayListExtra(Engine.EXTRA_UNAVAILABLE_VOICES, unavailableLanguages);
  96. setResult(result, returnData);
  97. finish();
  98. }
  99. private final SynthReadyCallback mSynthReadyCallback = new SynthReadyCallback() {
  100. @Override
  101. public void onSynthDataReady(byte[] audioData) {
  102. // Do nothing.
  103. }
  104. @Override
  105. public void onSynthDataComplete() {
  106. // Do nothing.
  107. }
  108. };
  109. }