From 1c0e4cfaa9e3cacdfef3176bff0b499e7f850e24 Mon Sep 17 00:00:00 2001 From: "Reece H. Dunn" Date: Thu, 30 Oct 2014 12:56:06 +0000 Subject: [PATCH] TextToSpeechService: use a HashMap for the voices. This improves the performance of looking up a voice by name. --- .../src/com/reecedunn/espeak/TtsService.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/android/src/com/reecedunn/espeak/TtsService.java b/android/src/com/reecedunn/espeak/TtsService.java index 403bc38f..0618019b 100644 --- a/android/src/com/reecedunn/espeak/TtsService.java +++ b/android/src/com/reecedunn/espeak/TtsService.java @@ -42,8 +42,10 @@ import android.util.Log; import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; /** * Implements the eSpeak engine as a {@link TextToSpeechService}. @@ -65,7 +67,7 @@ public class TtsService extends TextToSpeechService { private SpeechSynthesis mEngine; private SynthesisCallback mCallback; - private List mAvailableVoices; + private Map mAvailableVoices; private Voice mMatchingVoice = null; private BroadcastReceiver mOnLanguagesDownloaded = null; @@ -98,7 +100,10 @@ public class TtsService extends TextToSpeechService { } mEngine = new SpeechSynthesis(this, mSynthCallback); - mAvailableVoices = mEngine.getAvailableVoices(); + mAvailableVoices = new HashMap(); + for (Voice voice : mEngine.getAvailableVoices()) { + mAvailableVoices.put(voice.name, voice); + } final Intent intent = new Intent(ESPEAK_INITIALIZED); sendBroadcast(intent); @@ -140,7 +145,7 @@ public class TtsService extends TextToSpeechService { Voice countryVoice = null; synchronized (mAvailableVoices) { - for (Voice voice : mAvailableVoices) { + for (Voice voice : mAvailableVoices.values()) { switch (voice.match(query)) { case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE: mMatchingVoice = voice; @@ -213,7 +218,7 @@ public class TtsService extends TextToSpeechService { @Override public List onGetVoices() { List voices = new ArrayList(); - for (Voice voice : mAvailableVoices) { + for (Voice voice : mAvailableVoices.values()) { int quality = android.speech.tts.Voice.QUALITY_NORMAL; int latency = android.speech.tts.Voice.LATENCY_VERY_LOW; voices.add(new android.speech.tts.Voice(voice.name, voice.locale, quality, latency, false, null)); @@ -223,12 +228,8 @@ public class TtsService extends TextToSpeechService { @Override public int onIsValidVoiceName(String name) { - for (Voice voice : mAvailableVoices) { - if (voice.name.equals(name)) { - return TextToSpeech.SUCCESS; - } - } - return TextToSpeech.ERROR; + Voice voice = mAvailableVoices.get(name); + return (voice == null) ? TextToSpeech.ERROR : TextToSpeech.SUCCESS; } @Override