Loading packages/TtsService/src/android/tts/TtsService.java +81 −27 Original line number Diff line number Diff line Loading @@ -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<String> mParams = null; public int mType = TEXT; public long mDuration = 0; public String mFilename = null; public SpeechItem(String text, ArrayList<String> params, int itemType) { mText = text; Loading @@ -65,6 +68,14 @@ public class TtsService extends Service implements OnCompletionListener { public SpeechItem(long silenceTime) { mDuration = silenceTime; } public SpeechItem(String text, ArrayList<String> params, int itemType, String filename) { mText = text; mParams = params; mType = itemType; mFilename = filename; } } /** Loading Loading @@ -464,6 +475,60 @@ public class TtsService extends Service implements OnCompletionListener { synth.start(); } private void synthToFileInternalOnly(final String text, final ArrayList<String> 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; Loading Loading @@ -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 { Loading Loading @@ -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<String> 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(); } Log.i("TTS", "Synthesizing to " + filename); boolean synthAvailable = false; try { synthAvailable = synthesizerLock.tryLock(); if (!synthAvailable) { return false; } String filename) { // 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; } Loading Loading @@ -957,7 +1011,7 @@ public class TtsService extends Service implements OnCompletionListener { if (params != null) { speakingParams = new ArrayList<String>(Arrays.asList(params)); } return mSelf.synthesizeToFile(text, speakingParams, filename, true); return mSelf.synthesizeToFile(text, speakingParams, filename); } /** Loading Loading
packages/TtsService/src/android/tts/TtsService.java +81 −27 Original line number Diff line number Diff line Loading @@ -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<String> mParams = null; public int mType = TEXT; public long mDuration = 0; public String mFilename = null; public SpeechItem(String text, ArrayList<String> params, int itemType) { mText = text; Loading @@ -65,6 +68,14 @@ public class TtsService extends Service implements OnCompletionListener { public SpeechItem(long silenceTime) { mDuration = silenceTime; } public SpeechItem(String text, ArrayList<String> params, int itemType, String filename) { mText = text; mParams = params; mType = itemType; mFilename = filename; } } /** Loading Loading @@ -464,6 +475,60 @@ public class TtsService extends Service implements OnCompletionListener { synth.start(); } private void synthToFileInternalOnly(final String text, final ArrayList<String> 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; Loading Loading @@ -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 { Loading Loading @@ -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<String> 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(); } Log.i("TTS", "Synthesizing to " + filename); boolean synthAvailable = false; try { synthAvailable = synthesizerLock.tryLock(); if (!synthAvailable) { return false; } String filename) { // 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; } Loading Loading @@ -957,7 +1011,7 @@ public class TtsService extends Service implements OnCompletionListener { if (params != null) { speakingParams = new ArrayList<String>(Arrays.asList(params)); } return mSelf.synthesizeToFile(text, speakingParams, filename, true); return mSelf.synthesizeToFile(text, speakingParams, filename); } /** Loading