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.

TextToSpeechTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2012-2015 Reece H. Dunn
  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.reecedunn.espeak.test;
  17. import java.util.HashSet;
  18. import java.util.Locale;
  19. import java.util.Set;
  20. import android.annotation.SuppressLint;
  21. import android.os.Build;
  22. import android.speech.tts.TextToSpeech;
  23. import static com.reecedunn.espeak.test.TtsMatcher.isTtsLangCode;
  24. import static org.hamcrest.MatcherAssert.assertThat;
  25. import static org.hamcrest.Matchers.*;
  26. public class TextToSpeechTest extends TextToSpeechTestCase
  27. {
  28. private Set<Object> mVoices = null;
  29. private Set<String> mAdded = new HashSet<String>();
  30. private Set<String> mRemoved = new HashSet<String>();
  31. @SuppressLint("NewApi")
  32. public Set<Object> getVoices()
  33. {
  34. if (mVoices == null)
  35. {
  36. Set<android.speech.tts.Voice> voiceData = getEngine().getVoices();
  37. assertThat(voiceData, is(notNullValue()));
  38. mVoices = new HashSet<Object>();
  39. for (android.speech.tts.Voice voice : voiceData)
  40. {
  41. mVoices.add(voice);
  42. }
  43. Set<String> voices = new HashSet<String>();
  44. for (Object data : mVoices)
  45. {
  46. voices.add(((android.speech.tts.Voice)data).getName());
  47. }
  48. Set<String> expected = new HashSet<String>();
  49. for (VoiceData.Voice data : VoiceData.voices)
  50. {
  51. expected.add(data.name);
  52. }
  53. for (String voice : voices)
  54. {
  55. if (!expected.contains(voice))
  56. {
  57. mAdded.add(voice);
  58. }
  59. }
  60. for (String voice : expected)
  61. {
  62. if (!voices.contains(voice))
  63. {
  64. mRemoved.add(voice);
  65. }
  66. }
  67. }
  68. return mVoices;
  69. }
  70. public VoiceData.Voice getVoiceData(String name)
  71. {
  72. for (VoiceData.Voice voice : VoiceData.voices)
  73. {
  74. if (voice.name.equals(name))
  75. {
  76. return voice;
  77. }
  78. }
  79. return null;
  80. }
  81. public void testAddedVoices()
  82. {
  83. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
  84. return;
  85. getVoices(); // Ensure that the voice data has been populated.
  86. assertThat(mAdded.toString(), is("[]"));
  87. }
  88. public void testRemovedVoices()
  89. {
  90. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
  91. return;
  92. getVoices(); // Ensure that the voice data has been populated.
  93. assertThat(mRemoved.toString(), is("[]"));
  94. }
  95. @SuppressLint("NewApi")
  96. public void testVoices()
  97. {
  98. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
  99. return;
  100. getVoices(); // Ensure that the voice data has been populated.
  101. for (Object item : mVoices)
  102. {
  103. android.speech.tts.Voice voice = (android.speech.tts.Voice)item;
  104. VoiceData.Voice data = getVoiceData(voice.getName());
  105. assertThat(data, is(notNullValue()));
  106. assertThat(voice.getName(), is(data.name));
  107. assertThat(voice.getLocale().getLanguage(), is(data.ianaLanguage));
  108. assertThat(voice.getLocale().getCountry(), is(data.ianaCountry));
  109. assertThat(voice.getLocale().getVariant(), is(data.variant));
  110. assertThat(voice.getFeatures(), is(notNullValue()));
  111. assertThat(voice.getFeatures().size(), is(0));
  112. assertThat(voice.getLatency(), is(android.speech.tts.Voice.LATENCY_VERY_LOW));
  113. assertThat(voice.getQuality(), is(android.speech.tts.Voice.QUALITY_NORMAL));
  114. getEngine().setVoice(voice);
  115. assertThat(getLanguage(getEngine()).getLanguage(), is(data.javaLanguage));
  116. assertThat(getLanguage(getEngine()).getCountry(), is(data.javaCountry));
  117. assertThat(getLanguage(getEngine()).getVariant(), is(data.variant));
  118. android.speech.tts.Voice voice2 = getEngine().getVoice();
  119. assertThat(voice2, is(notNullValue()));
  120. assertThat(voice2.getName(), is(data.name));
  121. assertThat(voice2.getLocale().getLanguage(), is(data.ianaLanguage));
  122. assertThat(voice2.getLocale().getCountry(), is(data.ianaCountry));
  123. assertThat(voice2.getLocale().getVariant(), is(data.variant));
  124. assertThat(voice2.getFeatures(), is(notNullValue()));
  125. assertThat(voice2.getFeatures().size(), is(0));
  126. assertThat(voice2.getLatency(), is(android.speech.tts.Voice.LATENCY_VERY_LOW));
  127. assertThat(voice2.getQuality(), is(android.speech.tts.Voice.QUALITY_NORMAL));
  128. }
  129. }
  130. public void testUnsupportedLanguage()
  131. {
  132. assertThat(getEngine(), is(notNullValue()));
  133. Locale initialLocale = getLanguage(getEngine());
  134. assertThat(getEngine().isLanguageAvailable(new Locale("cel")), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
  135. Locale language = getLanguage(getEngine());
  136. assertThat(getLanguage(getEngine()).getLanguage(), is(initialLocale.getLanguage()));
  137. assertThat(getLanguage(getEngine()).getCountry(), is(initialLocale.getCountry()));
  138. assertThat(getLanguage(getEngine()).getVariant(), is(initialLocale.getVariant()));
  139. }
  140. public void checkLanguage(VoiceData.Voice data, Locale locale, int status, String language, String country, String variant)
  141. {
  142. String langTag = locale.toString().replace('_', '-');
  143. String context = "";
  144. try
  145. {
  146. context = "isLanguageAvailable";
  147. assertThat(getEngine().isLanguageAvailable(locale), isTtsLangCode(status));
  148. context = "setLanguage";
  149. assertThat(getEngine().setLanguage(locale), isTtsLangCode(status));
  150. context = "getLanguage";
  151. assertThat(getLanguage(getEngine()).getLanguage(), is(language));
  152. assertThat(getLanguage(getEngine()).getCountry(), is(country));
  153. assertThat(getLanguage(getEngine()).getVariant(), is(variant));
  154. }
  155. catch (AssertionError e)
  156. {
  157. throw new VoiceData.Exception(data, context + "|" + langTag, e);
  158. }
  159. }
  160. public void testLanguages()
  161. {
  162. assertThat(getEngine(), is(notNullValue()));
  163. for (VoiceData.Voice data : VoiceData.voices)
  164. {
  165. final Locale iana1 = new Locale(data.ianaLanguage, data.ianaCountry, data.variant);
  166. final Locale iana2 = new Locale(data.ianaLanguage, data.ianaCountry, "test");
  167. final Locale iana3 = new Locale(data.ianaLanguage, "VU", data.variant);
  168. final Locale java1 = new Locale(data.javaLanguage, data.javaCountry, data.variant);
  169. final Locale java2 = new Locale(data.javaLanguage, data.javaCountry, "test");
  170. final Locale java3 = new Locale(data.javaLanguage, "VUT", data.variant);
  171. checkLanguage(data, iana1, TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE, data.javaLanguage, data.javaCountry, data.variant);
  172. checkLanguage(data, java1, TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE, data.javaLanguage, data.javaCountry, data.variant);
  173. if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
  174. // Android Lollipop sets variant to "" when TextToSpeech.LANG_COUNTRY_AVAILABLE is returned.
  175. checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
  176. checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
  177. } else {
  178. if (data.ianaLanguage.equals("vi") && data.ianaCountry.equals("VN")) {
  179. checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "hue");
  180. checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "hue");
  181. } else if (data.ianaLanguage.equals("hy") && data.ianaCountry.equals("AM")) {
  182. checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "arevmda");
  183. checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "arevmda");
  184. } else {
  185. checkLanguage(data, iana2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
  186. checkLanguage(data, java2, TextToSpeech.LANG_COUNTRY_AVAILABLE, data.javaLanguage, data.javaCountry, "");
  187. }
  188. }
  189. if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
  190. // Android Lollipop sets country and variant to "" when TextToSpeech.LANG_AVAILABLE is returned.
  191. checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
  192. checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
  193. } else {
  194. switch (data.ianaLanguage) {
  195. case "fr":
  196. checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "FRA", "");
  197. checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "FRA", "");
  198. break;
  199. case "pt":
  200. checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "PRT", "");
  201. checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "PRT", "");
  202. break;
  203. default:
  204. checkLanguage(data, iana3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
  205. checkLanguage(data, java3, TextToSpeech.LANG_AVAILABLE, data.javaLanguage, "", "");
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. }