Browse Source

TextToSpeechService: use a HashMap for the voices.

This improves the performance of looking up a voice by name.
master
Reece H. Dunn 10 years ago
parent
commit
1c0e4cfaa9
1 changed files with 11 additions and 10 deletions
  1. 11
    10
      android/src/com/reecedunn/espeak/TtsService.java

+ 11
- 10
android/src/com/reecedunn/espeak/TtsService.java View File

import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback; import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;


/** /**
* Implements the eSpeak engine as a {@link TextToSpeechService}. * Implements the eSpeak engine as a {@link TextToSpeechService}.
private SpeechSynthesis mEngine; private SpeechSynthesis mEngine;
private SynthesisCallback mCallback; private SynthesisCallback mCallback;


private List<Voice> mAvailableVoices;
private Map<String, Voice> mAvailableVoices;
private Voice mMatchingVoice = null; private Voice mMatchingVoice = null;


private BroadcastReceiver mOnLanguagesDownloaded = null; private BroadcastReceiver mOnLanguagesDownloaded = null;
} }


mEngine = new SpeechSynthesis(this, mSynthCallback); mEngine = new SpeechSynthesis(this, mSynthCallback);
mAvailableVoices = mEngine.getAvailableVoices();
mAvailableVoices = new HashMap<String, Voice>();
for (Voice voice : mEngine.getAvailableVoices()) {
mAvailableVoices.put(voice.name, voice);
}


final Intent intent = new Intent(ESPEAK_INITIALIZED); final Intent intent = new Intent(ESPEAK_INITIALIZED);
sendBroadcast(intent); sendBroadcast(intent);
Voice countryVoice = null; Voice countryVoice = null;


synchronized (mAvailableVoices) { synchronized (mAvailableVoices) {
for (Voice voice : mAvailableVoices) {
for (Voice voice : mAvailableVoices.values()) {
switch (voice.match(query)) { switch (voice.match(query)) {
case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE: case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
mMatchingVoice = voice; mMatchingVoice = voice;
@Override @Override
public List<android.speech.tts.Voice> onGetVoices() { public List<android.speech.tts.Voice> onGetVoices() {
List<android.speech.tts.Voice> voices = new ArrayList<android.speech.tts.Voice>(); List<android.speech.tts.Voice> voices = new ArrayList<android.speech.tts.Voice>();
for (Voice voice : mAvailableVoices) {
for (Voice voice : mAvailableVoices.values()) {
int quality = android.speech.tts.Voice.QUALITY_NORMAL; int quality = android.speech.tts.Voice.QUALITY_NORMAL;
int latency = android.speech.tts.Voice.LATENCY_VERY_LOW; int latency = android.speech.tts.Voice.LATENCY_VERY_LOW;
voices.add(new android.speech.tts.Voice(voice.name, voice.locale, quality, latency, false, null)); voices.add(new android.speech.tts.Voice(voice.name, voice.locale, quality, latency, false, null));


@Override @Override
public int onIsValidVoiceName(String name) { 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 @Override

Loading…
Cancel
Save