| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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"); |
| 41 |
public static final Locale afr = new Locale("afr"); |
| 42 |
|
| 43 |
public static final Locale de = new Locale("de"); |
| 44 |
public static final Locale de_DE = new Locale("de", "DE"); |
| 45 |
public static final Locale de_1996 = new Locale("de", "", "1996"); |
| 46 |
public static final Locale de_CH_1901 = new Locale("de", "CH", "1901"); |
| 47 |
|
| 48 |
public static final Locale deu = new Locale("deu"); |
| 49 |
public static final Locale deu_DEU = new Locale("deu", "DEU"); |
| 50 |
public static final Locale deu_1996 = new Locale("deu", "", "1996"); |
| 51 |
public static final Locale deu_CHE_1901 = new Locale("deu", "CHE", "1901"); |
| 52 |
|
| 53 |
public static final Locale fr = new Locale("fr"); |
| 54 |
public static final Locale fr_FR = new Locale("fr", "FR"); |
| 55 |
public static final Locale fr_BE = new Locale("fr", "BE"); |
| 56 |
public static final Locale fr_1694acad = new Locale("fr", "", "1694acad"); |
| 57 |
public static final Locale fr_FR_1694acad = new Locale("fr", "FR", "1694acad"); |
| 58 |
public static final Locale fr_BE_1694acad = new Locale("fr", "BE", "1694acad"); |
| 59 |
|
| 60 |
public static final Locale fra = new Locale("fra"); |
| 61 |
public static final Locale fra_FRA = new Locale("fra", "FRA"); |
| 62 |
public static final Locale fra_BEL = new Locale("fra", "BEL"); |
| 63 |
public static final Locale fra_1694acad = new Locale("fra", "", "1694acad"); |
| 64 |
public static final Locale fra_FRA_1694acad = new Locale("fra", "FRA", "1694acad"); |
| 65 |
public static final Locale fra_BEL_1694acad = new Locale("fra", "BEL", "1694acad"); |
| 66 |
|
| 67 |
public static final Locale hy = new Locale("hy"); |
| 68 |
public static final Locale hy_AM = new Locale("hy", "AM"); |
| 69 |
public static final Locale hy_arevela = new Locale("hy", "", "arevela"); |
| 70 |
public static final Locale hy_arevmda = new Locale("hy", "", "arevmda"); |
| 71 |
public static final Locale hy_AM_arevela = new Locale("hy", "AM", "arevela"); |
| 72 |
public static final Locale hy_AM_arevmda = new Locale("hy", "AM", "arevmda"); |
| 73 |
|
| 74 |
public static final Locale hye = new Locale("hye"); |
| 75 |
public static final Locale hye_ARM = new Locale("hye", "ARM"); |
| 76 |
public static final Locale hye_arevela = new Locale("hye", "", "arevela"); |
| 77 |
public static final Locale hye_arevmda = new Locale("hye", "", "arevmda"); |
| 78 |
public static final Locale hye_ARM_arevela = new Locale("hye", "ARM", "arevela"); |
| 79 |
public static final Locale hye_ARM_arevmda = new Locale("hye", "ARM", "arevmda"); |
| 80 |
|
| 81 |
public static final Locale en = new Locale("en"); |
| 82 |
public static final Locale en_GB = new Locale("en", "GB"); |
| 83 |
public static final Locale en_US = new Locale("en", "US"); |
| 84 |
public static final Locale en_scotland = new Locale("en", "", "scotland"); |
| 85 |
public static final Locale en_GB_scotland = new Locale("en", "GB", "scotland"); |
| 86 |
public static final Locale en_GB_north = new Locale("en", "GB", "north"); |
| 87 |
|
| 88 |
public static final Locale eng = new Locale("en"); |
| 89 |
public static final Locale eng_GBR = new Locale("en", "GBR"); |
| 90 |
public static final Locale eng_USA = new Locale("en", "USA"); |
| 91 |
public static final Locale eng_scotland = new Locale("en", "", "scotland"); |
| 92 |
public static final Locale eng_GBR_scotland = new Locale("en", "GBR", "scotland"); |
| 93 |
public static final Locale eng_GBR_north = new Locale("en", "GBR", "north"); |
| 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(); |
| 187 |
assertThat(mAdded.toString(), is("[]")); |
| 188 |
} |
| 189 |
|
| 190 |
public void testRemovedVoices() |
| 191 |
{ |
| 192 |
getVoices(); |
| 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"); |
| 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"); |
| 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"); |
| 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)); |
| 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)); |
| 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 |
} |