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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2022 Beka Gozalishvili
  3. * Copyright (C) 2012-2013 Reece H. Dunn
  4. * Copyright (C) 2009 The Android Open Source Project
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /*
  19. * This Activity is used by Android to get the list of languages to display
  20. * to the user when selecting the text-to-speech language. This is by locale,
  21. * not voice name.
  22. */
  23. package com.reecedunn.espeak;
  24. import android.app.Activity;
  25. import android.content.Context;
  26. import android.content.Intent;
  27. import android.os.Bundle;
  28. import android.speech.tts.TextToSpeech.Engine;
  29. import android.util.Log;
  30. import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback;
  31. import java.io.File;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import java.util.Locale;
  35. public class CheckVoiceData extends Activity {
  36. private static final String TAG = "eSpeakTTS";
  37. /** Resources required for eSpeak to run correctly. */
  38. private static final String[] BASE_RESOURCES = {
  39. "version",
  40. "intonations",
  41. "phondata",
  42. "phonindex",
  43. "phontab",
  44. "en_dict",
  45. };
  46. public static File getDataPath(Context context) {
  47. return new File(context.getDir("voices", MODE_PRIVATE), "espeak-ng-data");
  48. }
  49. public static boolean hasBaseResources(Context context) {
  50. final File dataPath = getDataPath(context);
  51. for (String resource : BASE_RESOURCES) {
  52. final File resourceFile = new File(dataPath, resource);
  53. if (!resourceFile.exists()) {
  54. Log.e(TAG, "Missing base resource: " + resourceFile.getPath());
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. public static boolean canUpgradeResources(Context context) {
  61. try {
  62. final String version = FileUtils.read(context.getResources().openRawResource(R.raw.espeakdata_version));
  63. final String installedVersion = FileUtils.read(new File(getDataPath(context), "version"));
  64. return !version.equals(installedVersion);
  65. } catch (Exception e) {
  66. return false;
  67. }
  68. }
  69. @Override
  70. protected void onCreate(Bundle savedInstanceState) {
  71. super.onCreate(savedInstanceState);
  72. Context storageContext = EspeakApp.getStorageContext();
  73. ArrayList<String> availableLanguages = new ArrayList<String>();
  74. ArrayList<String> unavailableLanguages = new ArrayList<String>();
  75. boolean haveBaseResources = hasBaseResources(storageContext);
  76. if (!haveBaseResources || canUpgradeResources(storageContext)) {
  77. if (!haveBaseResources) {
  78. unavailableLanguages.add(Locale.ENGLISH.toString());
  79. }
  80. returnResults(Engine.CHECK_VOICE_DATA_FAIL, availableLanguages, unavailableLanguages);
  81. return;
  82. }
  83. final SpeechSynthesis engine = new SpeechSynthesis(storageContext, mSynthReadyCallback);
  84. final List<Voice> voices = engine.getAvailableVoices();
  85. for (Voice voice : voices) {
  86. availableLanguages.add(voice.toString());
  87. }
  88. returnResults(Engine.CHECK_VOICE_DATA_PASS, availableLanguages, unavailableLanguages);
  89. }
  90. private void returnResults(int result, ArrayList<String> availableLanguages, ArrayList<String> unavailableLanguages) {
  91. final Intent returnData = new Intent();
  92. returnData.putStringArrayListExtra(Engine.EXTRA_AVAILABLE_VOICES, availableLanguages);
  93. returnData.putStringArrayListExtra(Engine.EXTRA_UNAVAILABLE_VOICES, unavailableLanguages);
  94. setResult(result, returnData);
  95. finish();
  96. }
  97. private final SynthReadyCallback mSynthReadyCallback = new SynthReadyCallback() {
  98. @Override
  99. public void onSynthDataReady(byte[] audioData) {
  100. // Do nothing.
  101. }
  102. @Override
  103. public void onSynthDataComplete() {
  104. // Do nothing.
  105. }
  106. };
  107. }