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.

SpeechSynthesisTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) 2012 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.ArrayList;
  18. import java.util.HashSet;
  19. import java.util.List;
  20. import java.util.Locale;
  21. import java.util.Set;
  22. import com.reecedunn.espeak.SpeechSynthesis;
  23. import com.reecedunn.espeak.SpeechSynthesis.Voice;
  24. import android.media.AudioFormat;
  25. import android.speech.tts.TextToSpeech;
  26. import android.test.AndroidTestCase;
  27. import android.util.Log;
  28. import static org.hamcrest.MatcherAssert.assertThat;
  29. import static org.hamcrest.Matchers.*;
  30. public class SpeechSynthesisTest extends AndroidTestCase
  31. {
  32. public static final Locale af = new Locale("af"); // Afrikaans
  33. public static final Locale afr = new Locale("afr"); // Afrikaans
  34. public static final Locale de = new Locale("de"); // German
  35. public static final Locale de_DE = new Locale("de", "DE"); // German (Germany)
  36. public static final Locale de_1996 = new Locale("de", "", "1996"); // German (1996 Orthography)
  37. public static final Locale de_CH_1901 = new Locale("de", "CH", "1901"); // German (Traditional Orthography,Switzerland)
  38. public static final Locale deu = new Locale("deu"); // German
  39. public static final Locale deu_DEU = new Locale("deu", "DEU"); // German (Germany)
  40. public static final Locale deu_1996 = new Locale("deu", "", "1996"); // German (1996 Orthography)
  41. public static final Locale deu_CHE_1901 = new Locale("deu", "CHE", "1901"); // German (Traditional Orthography,Switzerland)
  42. public static final Locale fr = new Locale("fr"); // French
  43. public static final Locale fr_FR = new Locale("fr", "FR"); // French (France)
  44. public static final Locale fr_BE = new Locale("fr", "BE"); // French (Belgium)
  45. public static final Locale fr_1694acad = new Locale("fr", "", "1694acad"); // French (Early Modern French)
  46. public static final Locale fr_FR_1694acad = new Locale("fr", "FR", "1694acad"); // French (Early Modern French,France)
  47. public static final Locale fr_BE_1694acad = new Locale("fr", "BE", "1694acad"); // French (Early Modern French,Belgium)
  48. public static final Locale fra = new Locale("fra"); // French
  49. public static final Locale fra_FRA = new Locale("fra", "FRA"); // French (France)
  50. public static final Locale fra_BEL = new Locale("fra", "BEL"); // French (Belgium)
  51. public static final Locale fra_1694acad = new Locale("fra", "", "1694acad"); // French (Early Modern French)
  52. public static final Locale fra_FRA_1694acad = new Locale("fra", "FRA", "1694acad"); // French (Early Modern French,France)
  53. public static final Locale fra_BEL_1694acad = new Locale("fra", "BEL", "1694acad"); // French (Early Modern French,Belgium)
  54. public static final Locale hy = new Locale("hy"); // Armenian
  55. public static final Locale hy_AM = new Locale("hy", "AM"); // Armenian (Armenia)
  56. public static final Locale hy_arevela = new Locale("hy", "", "arevela"); // Armenian (Eastern)
  57. public static final Locale hy_arevmda = new Locale("hy", "", "arevmda"); // Armenian (Western)
  58. public static final Locale hy_AM_arevela = new Locale("hy", "AM", "arevela"); // Armenian (Eastern,Armenia)
  59. public static final Locale hy_AM_arevmda = new Locale("hy", "AM", "arevmda"); // Armenian (Western,Armenia)
  60. public static final Locale hye = new Locale("hye"); // Armenian
  61. public static final Locale hye_ARM = new Locale("hye", "ARM"); // Armenian (Armenia)
  62. public static final Locale hye_arevela = new Locale("hye", "", "arevela"); // Armenian (Eastern)
  63. public static final Locale hye_arevmda = new Locale("hye", "", "arevmda"); // Armenian (Western)
  64. public static final Locale hye_ARM_arevela = new Locale("hye", "ARM", "arevela"); // Armenian (Eastern,Armenia)
  65. public static final Locale hye_ARM_arevmda = new Locale("hye", "ARM", "arevmda"); // Armenian (Western,Armenia)
  66. public static final Locale en = new Locale("en"); // English
  67. public static final Locale en_GB = new Locale("en", "GB"); // English (Great Britain)
  68. public static final Locale en_US = new Locale("en", "US"); // English (USA)
  69. public static final Locale en_scotland = new Locale("en", "", "scotland"); // English (Scottish)
  70. public static final Locale en_GB_scotland = new Locale("en", "GB", "scotland"); // English (Scottish,Great Britain)
  71. public static final Locale en_GB_north = new Locale("en", "GB", "north"); // English (North,Great Britain)
  72. public static final Locale eng = new Locale("en"); // English
  73. public static final Locale eng_GBR = new Locale("en", "GBR"); // English (Great Britain)
  74. public static final Locale eng_USA = new Locale("en", "USA"); // English (USA)
  75. public static final Locale eng_scotland = new Locale("en", "", "scotland"); // English (Scottish)
  76. public static final Locale eng_GBR_scotland = new Locale("en", "GBR", "scotland"); // English (Scottish,Great Britain)
  77. public static final Locale eng_GBR_north = new Locale("en", "GBR", "north"); // English (North,Great Britain)
  78. private SpeechSynthesis.SynthReadyCallback mCallback = new SpeechSynthesis.SynthReadyCallback()
  79. {
  80. @Override
  81. public void onSynthDataReady(byte[] audioData)
  82. {
  83. }
  84. @Override
  85. public void onSynthDataComplete()
  86. {
  87. }
  88. };
  89. private List<Voice> mVoices = null;
  90. private Set<String> mAdded = new HashSet<String>();
  91. private Set<String> mRemoved = new HashSet<String>();
  92. public List<Voice> getVoices()
  93. {
  94. if (mVoices == null)
  95. {
  96. final SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
  97. mVoices = synth.getAvailableVoices();
  98. assertThat(mVoices, is(notNullValue()));
  99. Set<String> voices = new HashSet<String>();
  100. for (Voice data : mVoices)
  101. {
  102. voices.add(data.name);
  103. }
  104. Set<String> expected = new HashSet<String>();
  105. for (VoiceData.Voice data : VoiceData.voices)
  106. {
  107. expected.add(data.name);
  108. }
  109. for (String voice : voices)
  110. {
  111. if (!expected.contains(voice))
  112. {
  113. mAdded.add(voice);
  114. }
  115. }
  116. for (String voice : expected)
  117. {
  118. if (!voices.contains(voice))
  119. {
  120. mRemoved.add(voice);
  121. }
  122. }
  123. }
  124. return mVoices;
  125. }
  126. public Voice getVoice(String name)
  127. {
  128. for (Voice voice : getVoices())
  129. {
  130. if (voice.name.equals(name))
  131. {
  132. return voice;
  133. }
  134. }
  135. return null;
  136. }
  137. public void testConstruction()
  138. {
  139. final SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
  140. assertThat(synth.getSampleRate(), is(22050));
  141. assertThat(synth.getChannelCount(), is(1));
  142. assertThat(synth.getAudioFormat(), is(AudioFormat.ENCODING_PCM_16BIT));
  143. assertThat(synth.getBufferSizeInBytes(), is(22050));
  144. }
  145. public void testAddedVoices()
  146. {
  147. getVoices(); // Ensure that the voice data has been populated.
  148. assertThat(mAdded.toString(), is("[]"));
  149. }
  150. public void testRemovedVoices()
  151. {
  152. getVoices(); // Ensure that the voice data has been populated.
  153. assertThat(mRemoved.toString(), is("[]"));
  154. }
  155. public void testVoiceData()
  156. {
  157. for (VoiceData.Voice data : VoiceData.voices)
  158. {
  159. if (mRemoved.contains(data.name))
  160. {
  161. Log.i("SpeechSynthesisTest", "Skipping the missing voice '" + data.name + "'");
  162. continue;
  163. }
  164. try
  165. {
  166. final Voice voice = getVoice(data.name);
  167. assertThat(voice, is(notNullValue()));
  168. assertThat(voice.name, is(data.name));
  169. assertThat(voice.identifier, is(data.identifier));
  170. assertThat(voice.age, is(0));
  171. assertThat(voice.gender, is(data.gender));
  172. assertThat(voice.locale.getLanguage(), is(data.ianaLanguage));
  173. assertThat(voice.locale.getISO3Language(), is(data.javaLanguage));
  174. assertThat(voice.locale.getCountry(), is(data.ianaCountry));
  175. assertThat(voice.locale.getISO3Country(), is(data.javaCountry));
  176. assertThat(voice.locale.getVariant(), is(data.variant));
  177. assertThat(voice.toString(), is(data.locale));
  178. }
  179. catch (AssertionError e)
  180. {
  181. throw new VoiceData.Exception(data, e);
  182. }
  183. }
  184. }
  185. public void testMatchVoiceWithLanguage()
  186. {
  187. final Voice voice = getVoice("de"); // language="de" country="" variant=""
  188. assertThat(voice, is(notNullValue()));
  189. assertThat(voice.match(fr), is(TextToSpeech.LANG_NOT_SUPPORTED));
  190. assertThat(voice.match(fr_BE), is(TextToSpeech.LANG_NOT_SUPPORTED));
  191. assertThat(voice.match(fr_1694acad), is(TextToSpeech.LANG_NOT_SUPPORTED));
  192. assertThat(voice.match(fr_FR_1694acad), is(TextToSpeech.LANG_NOT_SUPPORTED));
  193. assertThat(voice.match(de), is(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
  194. assertThat(voice.match(de_1996), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  195. assertThat(voice.match(de_DE), is(TextToSpeech.LANG_AVAILABLE));
  196. assertThat(voice.match(de_CH_1901), is(TextToSpeech.LANG_AVAILABLE));
  197. assertThat(voice.match(deu), is(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
  198. assertThat(voice.match(deu_1996), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  199. assertThat(voice.match(deu_DEU), is(TextToSpeech.LANG_AVAILABLE));
  200. assertThat(voice.match(deu_CHE_1901), is(TextToSpeech.LANG_AVAILABLE));
  201. }
  202. public void testMatchVoiceWithLanguageAndCountry()
  203. {
  204. final Voice voice = getVoice("fr-fr"); // language="fr" country="fr" variant=""
  205. assertThat(voice, is(notNullValue()));
  206. assertThat(voice.match(de), is(TextToSpeech.LANG_NOT_SUPPORTED));
  207. assertThat(voice.match(de_1996), is(TextToSpeech.LANG_NOT_SUPPORTED));
  208. assertThat(voice.match(de_DE), is(TextToSpeech.LANG_NOT_SUPPORTED));
  209. assertThat(voice.match(de_CH_1901), is(TextToSpeech.LANG_NOT_SUPPORTED));
  210. assertThat(voice.match(fr), is(TextToSpeech.LANG_AVAILABLE));
  211. assertThat(voice.match(fr_FR), is(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
  212. assertThat(voice.match(fr_BE), is(TextToSpeech.LANG_AVAILABLE));
  213. assertThat(voice.match(fr_1694acad), is(TextToSpeech.LANG_AVAILABLE));
  214. assertThat(voice.match(fr_FR_1694acad), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  215. assertThat(voice.match(fr_BE_1694acad), is(TextToSpeech.LANG_AVAILABLE));
  216. assertThat(voice.match(fra), is(TextToSpeech.LANG_AVAILABLE));
  217. assertThat(voice.match(fra_FRA), is(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
  218. assertThat(voice.match(fra_BEL), is(TextToSpeech.LANG_AVAILABLE));
  219. assertThat(voice.match(fra_1694acad), is(TextToSpeech.LANG_AVAILABLE));
  220. assertThat(voice.match(fra_FRA_1694acad), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  221. assertThat(voice.match(fra_BEL_1694acad), is(TextToSpeech.LANG_AVAILABLE));
  222. }
  223. public void testMatchVoiceWithLanguageAndVariant()
  224. {
  225. final Voice voice = getVoice("hy-west"); // language="hy" country="" variant="arevmda"
  226. assertThat(voice, is(notNullValue()));
  227. assertThat(voice.match(fr), is(TextToSpeech.LANG_NOT_SUPPORTED));
  228. assertThat(voice.match(fr_BE), is(TextToSpeech.LANG_NOT_SUPPORTED));
  229. assertThat(voice.match(fr_1694acad), is(TextToSpeech.LANG_NOT_SUPPORTED));
  230. assertThat(voice.match(fr_FR_1694acad), is(TextToSpeech.LANG_NOT_SUPPORTED));
  231. assertThat(voice.match(hy), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  232. assertThat(voice.match(hy_AM), is(TextToSpeech.LANG_AVAILABLE));
  233. assertThat(voice.match(hy_arevela), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  234. assertThat(voice.match(hy_arevmda), is(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
  235. assertThat(voice.match(hy_AM_arevela), is(TextToSpeech.LANG_AVAILABLE));
  236. assertThat(voice.match(hy_AM_arevmda), is(TextToSpeech.LANG_AVAILABLE)); // NOTE: Android does not support LANG_VAR_AVAILABLE.
  237. assertThat(voice.match(hye), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  238. assertThat(voice.match(hye_ARM), is(TextToSpeech.LANG_AVAILABLE));
  239. assertThat(voice.match(hye_arevela), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  240. assertThat(voice.match(hye_arevmda), is(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
  241. assertThat(voice.match(hye_ARM_arevela), is(TextToSpeech.LANG_AVAILABLE));
  242. assertThat(voice.match(hye_ARM_arevmda), is(TextToSpeech.LANG_AVAILABLE)); // NOTE: Android does not support LANG_VAR_AVAILABLE.
  243. }
  244. public void testMatchVoiceWithLanguageCountryAndVariant()
  245. {
  246. final Voice voice = getVoice("en-sc"); // language="en" country="GB" variant="scotland"
  247. assertThat(voice, is(notNullValue()));
  248. assertThat(voice.match(de), is(TextToSpeech.LANG_NOT_SUPPORTED));
  249. assertThat(voice.match(de_1996), is(TextToSpeech.LANG_NOT_SUPPORTED));
  250. assertThat(voice.match(de_DE), is(TextToSpeech.LANG_NOT_SUPPORTED));
  251. assertThat(voice.match(de_CH_1901), is(TextToSpeech.LANG_NOT_SUPPORTED));
  252. assertThat(voice.match(en), is(TextToSpeech.LANG_AVAILABLE));
  253. assertThat(voice.match(en_GB), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  254. assertThat(voice.match(en_US), is(TextToSpeech.LANG_AVAILABLE));
  255. assertThat(voice.match(en_scotland), is(TextToSpeech.LANG_AVAILABLE)); // NOTE: Android does not support LANG_VAR_AVAILABLE.
  256. assertThat(voice.match(en_GB_scotland), is(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
  257. assertThat(voice.match(en_GB_north), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  258. assertThat(voice.match(eng), is(TextToSpeech.LANG_AVAILABLE));
  259. assertThat(voice.match(eng_GBR), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  260. assertThat(voice.match(eng_USA), is(TextToSpeech.LANG_AVAILABLE));
  261. assertThat(voice.match(eng_scotland), is(TextToSpeech.LANG_AVAILABLE)); // NOTE: Android does not support LANG_VAR_AVAILABLE.
  262. assertThat(voice.match(eng_GBR_scotland), is(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
  263. assertThat(voice.match(eng_GBR_north), is(TextToSpeech.LANG_COUNTRY_AVAILABLE));
  264. }
  265. }