From ebb814bbaeacbdc04faa5683f25b12d40609548c Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Tue, 30 Jun 2009 12:01:12 -0700 Subject: Fixing synth to file to use the speech queue. --- .../TtsService/src/android/tts/TtsService.java | 108 +++++++++++++++------ 1 file changed, 81 insertions(+), 27 deletions(-) diff --git a/packages/TtsService/src/android/tts/TtsService.java b/packages/TtsService/src/android/tts/TtsService.java index b1e6425..b5f5b37 100755 --- a/packages/TtsService/src/android/tts/TtsService.java +++ b/packages/TtsService/src/android/tts/TtsService.java @@ -51,10 +51,13 @@ public class TtsService extends Service implements OnCompletionListener { public static final int IPA = 1; public static final int EARCON = 2; public static final int SILENCE = 3; + public static final int TEXT_TO_FILE = 5; + public static final int IPA_TO_FILE = 6; public String mText = null; public ArrayList mParams = null; public int mType = TEXT; public long mDuration = 0; + public String mFilename = null; public SpeechItem(String text, ArrayList params, int itemType) { mText = text; @@ -65,6 +68,14 @@ public class TtsService extends Service implements OnCompletionListener { public SpeechItem(long silenceTime) { mDuration = silenceTime; } + + public SpeechItem(String text, ArrayList params, int itemType, String filename) { + mText = text; + mParams = params; + mType = itemType; + mFilename = filename; + } + } /** @@ -464,6 +475,60 @@ public class TtsService extends Service implements OnCompletionListener { synth.start(); } + private void synthToFileInternalOnly(final String text, + final ArrayList params, final String filename) { + class SynthThread implements Runnable { + public void run() { + Log.i("TTS", "Synthesizing to " + filename); + boolean synthAvailable = false; + try { + synthAvailable = synthesizerLock.tryLock(); + if (!synthAvailable) { + Thread.sleep(100); + Thread synth = (new Thread(new SynthThread())); + synth.setPriority(Thread.MIN_PRIORITY); + synth.start(); + return; + } + if (params != null){ + String language = ""; + String country = ""; + String variant = ""; + for (int i = 0; i < params.size() - 1; i = i + 2){ + String param = params.get(i); + if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_RATE)){ + setSpeechRate(Integer.parseInt(params.get(i+1))); + } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_LANGUAGE)){ + language = params.get(i+1); + } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_COUNTRY)){ + country = params.get(i+1); + } else if (param.equals(TextToSpeech.Engine.TTS_KEY_PARAM_VARIANT)){ + variant = params.get(i+1); + } + } + if (language.length() > 0){ + setLanguage(language, country, variant); + } + } + nativeSynth.synthesizeToFile(text, filename); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + // This check is needed because finally will always run; + // even if the + // method returns somewhere in the try block. + if (synthAvailable) { + synthesizerLock.unlock(); + } + processSpeechQueue(); + } + } + } + Thread synth = (new Thread(new SynthThread())); + synth.setPriority(Thread.MIN_PRIORITY); + synth.start(); + } + private SoundResource getSoundResource(SpeechItem speechItem) { SoundResource sr = null; String text = speechItem.mText; @@ -549,6 +614,9 @@ public class TtsService extends Service implements OnCompletionListener { currentSpeechItem = splitCurrentTextIfNeeded(currentSpeechItem); speakInternalOnly(currentSpeechItem.mText, currentSpeechItem.mParams); + } else if (currentSpeechItem.mType == SpeechItem.TEXT_TO_FILE) { + synthToFileInternalOnly(currentSpeechItem.mText, + currentSpeechItem.mParams, currentSpeechItem.mFilename); } else if (currentSpeechItem.mType == SpeechItem.IPA) { // TODO Implement IPA support } else { @@ -629,34 +697,20 @@ public class TtsService extends Service implements OnCompletionListener { * @return A boolean that indicates if the synthesis succeeded */ private boolean synthesizeToFile(String text, ArrayList params, - String filename, boolean calledFromApi) { - // Only stop everything if this is a call made by an outside app trying - // to - // use the API. Do NOT stop if this is a call from within the service as - // clearing the speech queue here would be a mistake. - if (calledFromApi) { - stop(); + String filename) { + // Don't allow a filename that is too long + if (filename.length() > MAX_FILENAME_LENGTH) { + return false; } - Log.i("TTS", "Synthesizing to " + filename); - boolean synthAvailable = false; - try { - synthAvailable = synthesizerLock.tryLock(); - if (!synthAvailable) { - return false; - } - // Don't allow a filename that is too long - if (filename.length() > MAX_FILENAME_LENGTH) { - return false; - } - nativeSynth.synthesizeToFile(text, filename); - } finally { - // This check is needed because finally will always run; even if the - // method returns somewhere in the try block. - if (synthAvailable) { - synthesizerLock.unlock(); - } + // Don't allow anything longer than the max text length; since this + // is synthing to a file, don't even bother splitting it. + if (text.length() >= MAX_SPEECH_ITEM_CHAR_LENGTH){ + return false; + } + mSpeechQueue.add(new SpeechItem(text, params, SpeechItem.TEXT_TO_FILE, filename)); + if (!mIsSpeaking) { + processSpeechQueue(); } - Log.i("TTS", "Completed synthesis for " + filename); return true; } @@ -957,7 +1011,7 @@ public class TtsService extends Service implements OnCompletionListener { if (params != null) { speakingParams = new ArrayList(Arrays.asList(params)); } - return mSelf.synthesizeToFile(text, speakingParams, filename, true); + return mSelf.synthesizeToFile(text, speakingParams, filename); } /** -- cgit v1.1