Debian Codegraph @

android/eSpeakTests/src/com/reecedunn/espeak/test/SpeechSynthesisTest.java

raw symbols SCIP
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
17 package com.reecedunn.espeak.test;
18
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Locale;
23 import java.util.Map;
24 import java.util.Set;
25
26 import com.reecedunn.espeak.SpeechSynthesis;
27 import com.reecedunn.espeak.Voice;
28
29 import android.media.AudioFormat;
30 import android.speech.tts.TextToSpeech;
31 import android.util.Log;
32
33 import static com.reecedunn.espeak.test.TtsMatcher.isTtsLangCode;
34 import static org.hamcrest.MatcherAssert.assertThat;
35 import static org.hamcrest.Matchers.*;
36 import static org.hamcrest.core.AnyOf.anyOf;
37
38 public class SpeechSynthesisTest extends TextToSpeechTestCase
39 {
40 public static final Locale af = new Locale("af"); // Afrikaans
41 public static final Locale afr = new Locale("afr"); // Afrikaans
42
43 public static final Locale de = new Locale("de"); // German
44 public static final Locale de_DE = new Locale("de", "DE"); // German (Germany)
45 public static final Locale de_1996 = new Locale("de", "", "1996"); // German (1996 Orthography)
46 public static final Locale de_CH_1901 = new Locale("de", "CH", "1901"); // German (Traditional Orthography,Switzerland)
47
48 public static final Locale deu = new Locale("deu"); // German
49 public static final Locale deu_DEU = new Locale("deu", "DEU"); // German (Germany)
50 public static final Locale deu_1996 = new Locale("deu", "", "1996"); // German (1996 Orthography)
51 public static final Locale deu_CHE_1901 = new Locale("deu", "CHE", "1901"); // German (Traditional Orthography,Switzerland)
52
53 public static final Locale fr = new Locale("fr"); // French
54 public static final Locale fr_FR = new Locale("fr", "FR"); // French (France)
55 public static final Locale fr_BE = new Locale("fr", "BE"); // French (Belgium)
56 public static final Locale fr_1694acad = new Locale("fr", "", "1694acad"); // French (Early Modern French)
57 public static final Locale fr_FR_1694acad = new Locale("fr", "FR", "1694acad"); // French (Early Modern French,France)
58 public static final Locale fr_BE_1694acad = new Locale("fr", "BE", "1694acad"); // French (Early Modern French,Belgium)
59
60 public static final Locale fra = new Locale("fra"); // French
61 public static final Locale fra_FRA = new Locale("fra", "FRA"); // French (France)
62 public static final Locale fra_BEL = new Locale("fra", "BEL"); // French (Belgium)
63 public static final Locale fra_1694acad = new Locale("fra", "", "1694acad"); // French (Early Modern French)
64 public static final Locale fra_FRA_1694acad = new Locale("fra", "FRA", "1694acad"); // French (Early Modern French,France)
65 public static final Locale fra_BEL_1694acad = new Locale("fra", "BEL", "1694acad"); // French (Early Modern French,Belgium)
66
67 public static final Locale hy = new Locale("hy"); // Armenian
68 public static final Locale hy_AM = new Locale("hy", "AM"); // Armenian (Armenia)
69 public static final Locale hy_arevela = new Locale("hy", "", "arevela"); // Armenian (Eastern)
70 public static final Locale hy_arevmda = new Locale("hy", "", "arevmda"); // Armenian (Western)
71 public static final Locale hy_AM_arevela = new Locale("hy", "AM", "arevela"); // Armenian (Eastern,Armenia)
72 public static final Locale hy_AM_arevmda = new Locale("hy", "AM", "arevmda"); // Armenian (Western,Armenia)
73
74 public static final Locale hye = new Locale("hye"); // Armenian
75 public static final Locale hye_ARM = new Locale("hye", "ARM"); // Armenian (Armenia)
76 public static final Locale hye_arevela = new Locale("hye", "", "arevela"); // Armenian (Eastern)
77 public static final Locale hye_arevmda = new Locale("hye", "", "arevmda"); // Armenian (Western)
78 public static final Locale hye_ARM_arevela = new Locale("hye", "ARM", "arevela"); // Armenian (Eastern,Armenia)
79 public static final Locale hye_ARM_arevmda = new Locale("hye", "ARM", "arevmda"); // Armenian (Western,Armenia)
80
81 public static final Locale en = new Locale("en"); // English
82 public static final Locale en_GB = new Locale("en", "GB"); // English (Great Britain)
83 public static final Locale en_US = new Locale("en", "US"); // English (USA)
84 public static final Locale en_scotland = new Locale("en", "", "scotland"); // English (Scottish)
85 public static final Locale en_GB_scotland = new Locale("en", "GB", "scotland"); // English (Scottish,Great Britain)
86 public static final Locale en_GB_north = new Locale("en", "GB", "north"); // English (North,Great Britain)
87
88 public static final Locale eng = new Locale("en"); // English
89 public static final Locale eng_GBR = new Locale("en", "GBR"); // English (Great Britain)
90 public static final Locale eng_USA = new Locale("en", "USA"); // English (USA)
91 public static final Locale eng_scotland = new Locale("en", "", "scotland"); // English (Scottish)
92 public static final Locale eng_GBR_scotland = new Locale("en", "GBR", "scotland"); // English (Scottish,Great Britain)
93 public static final Locale eng_GBR_north = new Locale("en", "GBR", "north"); // English (North,Great Britain)
94
95 private SpeechSynthesis.SynthReadyCallback mCallback = new SpeechSynthesis.SynthReadyCallback()
96 {
97 @Override
98 public void onSynthDataReady(byte[] audioData)
99 {
100 }
101
102 @Override
103 public void onSynthDataComplete()
104 {
105 }
106 };
107
108 private Map<String, Voice> mVoices = null;
109 private Set<String> mAdded = new HashSet<String>();
110 private Set<String> mRemoved = new HashSet<String>();
111
112 public Map<String, Voice> getVoices()
113 {
114 if (mVoices == null)
115 {
116 final SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
117 mVoices = new HashMap<String, Voice>();
118 for (Voice voice : synth.getAvailableVoices()) {
119 assertThat(mVoices.get(voice.name), is(nullValue()));
120 mVoices.put(voice.name, voice);
121 }
122 assertThat(mVoices, is(notNullValue()));
123
124 Set<String> voices = new HashSet<String>();
125 for (Voice data : mVoices.values())
126 {
127 voices.add(data.name);
128 }
129
130 Set<String> expected = new HashSet<String>();
131 for (VoiceData.Voice data : VoiceData.voices)
132 {
133 expected.add(data.name);
134 }
135
136 for (String voice : voices)
137 {
138 if (!expected.contains(voice))
139 {
140 mAdded.add(voice);
141 }
142 }
143
144 for (String voice : expected)
145 {
146 if (!voices.contains(voice))
147 {
148 mRemoved.add(voice);
149 }
150 }
151 }
152 return mVoices;
153 }
154
155 public Voice getVoice(String name)
156 {
157 return getVoices().get(name);
158 }
159
160 public void testConstruction()
161 {
162 final SpeechSynthesis synth = new SpeechSynthesis(getContext(), mCallback);
163 assertThat(synth.getSampleRate(), is(22050));
164 assertThat(synth.getChannelCount(), is(1));
165 assertThat(synth.getAudioFormat(), is(AudioFormat.ENCODING_PCM_16BIT));
166 }
167
168 public void testJavaToIanaLanguageCode()
169 {
170 for (VoiceData.Voice data : VoiceData.voices)
171 {
172 assertThat(SpeechSynthesis.getIanaLanguageCode(data.javaLanguage), is(data.ianaLanguage));
173 }
174 }
175
176 public void testJavaToIanaCountryCode()
177 {
178 for (VoiceData.Voice data : VoiceData.voices)
179 {
180 assertThat(SpeechSynthesis.getIanaCountryCode(data.javaCountry), is(data.ianaCountry));
181 }
182 }
183
184 public void testAddedVoices()
185 {
186 getVoices(); // Ensure that the voice data has been populated.
187 assertThat(mAdded.toString(), is("[]"));
188 }
189
190 public void testRemovedVoices()
191 {
192 getVoices(); // Ensure that the voice data has been populated.
193 assertThat(mRemoved.toString(), is("[]"));
194 }
195
196 public void testVoiceData()
197 {
198 for (VoiceData.Voice data : VoiceData.voices)
199 {
200 if (mRemoved.contains(data.name))
201 {
202 Log.i("SpeechSynthesisTest", "Skipping the missing voice '" + data.name + "'");
203 continue;
204 }
205
206 String context = "[voice]";
207 try
208 {
209 final Voice voice = getVoice(data.name);
210 assertThat(voice, is(notNullValue()));
211
212 context = "[name]";
213 assertThat(voice.name, is(data.name));
214 context = "[identifier]";
215 assertThat(voice.identifier, is(data.identifier));
216 context = "[age]";
217 assertThat(voice.age, is(0));
218 context = "[gender]";
219 assertThat(voice.gender, is(data.gender));
220 context = "[locale:language]";
221 assertThat(voice.locale.getLanguage(), is(data.ianaLanguage));
222 context = "[locale:iso3language]";
223 assertThat(voice.locale.getISO3Language(), is(data.javaLanguage));
224 context = "[locale:country]";
225 assertThat(voice.locale.getCountry(), is(data.ianaCountry));
226 context = "[locale:iso3country]";
227 assertThat(voice.locale.getISO3Country(), is(data.javaCountry));
228 context = "[locale:variant]";
229 assertThat(voice.locale.getVariant(), is(data.variant));
230 context = "[toString]";
231 assertThat(voice.toString(), is(data.locale));
232 }
233 catch (AssertionError e)
234 {
235 throw new VoiceData.Exception(data, context, e);
236 }
237 }
238 }
239
240 public void testMatchVoiceWithLanguage()
241 {
242 final Voice voice = getVoice("de"); // language="de" country="" variant=""
243 assertThat(voice, is(notNullValue()));
244
245 assertThat(voice.match(fr), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
246 assertThat(voice.match(fr_BE), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
247 assertThat(voice.match(fr_1694acad), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
248 assertThat(voice.match(fr_FR_1694acad), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
249
250 assertThat(voice.match(de), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
251 assertThat(voice.match(de_1996), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE));
252 assertThat(voice.match(de_DE), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
253 assertThat(voice.match(de_CH_1901), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
254
255 assertThat(voice.match(deu), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
256 assertThat(voice.match(deu_1996), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE));
257 assertThat(voice.match(deu_DEU), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
258 assertThat(voice.match(deu_CHE_1901), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
259 }
260
261 public void testMatchVoiceWithLanguageAndCountry()
262 {
263 final Voice voice = getVoice("fr-be"); // language="fr" country="BE" variant=""
264 assertThat(voice, is(notNullValue()));
265
266 assertThat(voice.match(de), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
267 assertThat(voice.match(de_1996), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
268 assertThat(voice.match(de_DE), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
269 assertThat(voice.match(de_CH_1901), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
270
271 assertThat(voice.match(fr), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
272 assertThat(voice.match(fr_FR), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
273 assertThat(voice.match(fr_BE), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
274 assertThat(voice.match(fr_1694acad), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
275 assertThat(voice.match(fr_FR_1694acad), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
276 assertThat(voice.match(fr_BE_1694acad), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE));
277
278 assertThat(voice.match(fra), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
279 assertThat(voice.match(fra_FRA), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
280 assertThat(voice.match(fra_BEL), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
281 assertThat(voice.match(fra_1694acad), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
282 assertThat(voice.match(fra_FRA_1694acad), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
283 assertThat(voice.match(fra_BEL_1694acad), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE));
284 }
285
286 public void testMatchVoiceWithLanguageCountryAndVariant()
287 {
288 final Voice voice = getVoice("en-gb-scotland"); // language="en" country="GB" variant="scotland"
289 assertThat(voice, is(notNullValue()));
290
291 assertThat(voice.match(de), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
292 assertThat(voice.match(de_1996), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
293 assertThat(voice.match(de_DE), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
294 assertThat(voice.match(de_CH_1901), isTtsLangCode(TextToSpeech.LANG_NOT_SUPPORTED));
295
296 assertThat(voice.match(en), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
297 assertThat(voice.match(en_GB), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE));
298 assertThat(voice.match(en_US), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
299 assertThat(voice.match(en_scotland), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); // NOTE: Android does not support LANG_VAR_AVAILABLE.
300 assertThat(voice.match(en_GB_scotland), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
301 assertThat(voice.match(en_GB_north), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE));
302
303 assertThat(voice.match(eng), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
304 assertThat(voice.match(eng_GBR), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE));
305 assertThat(voice.match(eng_USA), isTtsLangCode(TextToSpeech.LANG_AVAILABLE));
306 assertThat(voice.match(eng_scotland), isTtsLangCode(TextToSpeech.LANG_AVAILABLE)); // NOTE: Android does not support LANG_VAR_AVAILABLE.
307 assertThat(voice.match(eng_GBR_scotland), isTtsLangCode(TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE));
308 assertThat(voice.match(eng_GBR_north), isTtsLangCode(TextToSpeech.LANG_COUNTRY_AVAILABLE));
309 }
310
311 public void testGetSampleText()
312 {
313 final String[] currentLocales = getContext().getResources().getAssets().getLocales();
314 for (VoiceData.Voice data : VoiceData.voices)
315 {
316 if (mRemoved.contains(data.name))
317 {
318 Log.i("SpeechSynthesisTest", "Skipping the missing voice '" + data.name + "'");
319 continue;
320 }
321
322 String context = null;
323 try
324 {
325 final Locale ianaLocale = new Locale(data.ianaLanguage, data.ianaCountry, data.variant);
326 context = "[iana:sample-text]";
327 assertThat(SpeechSynthesis.getSampleText(getContext(), ianaLocale), isIn(data.sampleText));
328 context = "[iana:resource-locale]";
329 assertThat(getContext().getResources().getAssets().getLocales(), is(currentLocales));
330
331 if (!data.javaLanguage.equals(""))
332 {
333 final Locale javaLocale = new Locale(data.javaLanguage, data.javaCountry, data.variant);
334 context = "[java:sample-text]";
335 assertThat(SpeechSynthesis.getSampleText(getContext(), javaLocale), isIn(data.sampleText));
336 context = "[java:resource-locale]";
337 assertThat(getContext().getResources().getAssets().getLocales(), is(currentLocales));
338 }
339 }
340 catch (AssertionError e)
341 {
342 throw new VoiceData.Exception(data, context, e);
343 }
344 }
345 }
346 }