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

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