Merge change 5807 into donut

* changes:
  Fixing synth to file to use the speech queue.
This commit is contained in:
Android (Google) Code Review
2009-06-30 14:08:30 -07:00

View File

@@ -51,10 +51,13 @@ public class TtsService extends Service implements OnCompletionListener {
public static final int IPA = 1; public static final int IPA = 1;
public static final int EARCON = 2; public static final int EARCON = 2;
public static final int SILENCE = 3; 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 String mText = null;
public ArrayList<String> mParams = null; public ArrayList<String> mParams = null;
public int mType = TEXT; public int mType = TEXT;
public long mDuration = 0; public long mDuration = 0;
public String mFilename = null;
public SpeechItem(String text, ArrayList<String> params, int itemType) { public SpeechItem(String text, ArrayList<String> params, int itemType) {
mText = text; mText = text;
@@ -65,6 +68,14 @@ public class TtsService extends Service implements OnCompletionListener {
public SpeechItem(long silenceTime) { public SpeechItem(long silenceTime) {
mDuration = silenceTime; mDuration = silenceTime;
} }
public SpeechItem(String text, ArrayList<String> 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(); 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) { private SoundResource getSoundResource(SpeechItem speechItem) {
SoundResource sr = null; SoundResource sr = null;
String text = speechItem.mText; String text = speechItem.mText;
@@ -549,6 +614,9 @@ public class TtsService extends Service implements OnCompletionListener {
currentSpeechItem = splitCurrentTextIfNeeded(currentSpeechItem); currentSpeechItem = splitCurrentTextIfNeeded(currentSpeechItem);
speakInternalOnly(currentSpeechItem.mText, speakInternalOnly(currentSpeechItem.mText,
currentSpeechItem.mParams); currentSpeechItem.mParams);
} else if (currentSpeechItem.mType == SpeechItem.TEXT_TO_FILE) {
synthToFileInternalOnly(currentSpeechItem.mText,
currentSpeechItem.mParams, currentSpeechItem.mFilename);
} else if (currentSpeechItem.mType == SpeechItem.IPA) { } else if (currentSpeechItem.mType == SpeechItem.IPA) {
// TODO Implement IPA support // TODO Implement IPA support
} else { } else {
@@ -629,34 +697,20 @@ public class TtsService extends Service implements OnCompletionListener {
* @return A boolean that indicates if the synthesis succeeded * @return A boolean that indicates if the synthesis succeeded
*/ */
private boolean synthesizeToFile(String text, ArrayList<String> params, private boolean synthesizeToFile(String text, ArrayList<String> params,
String filename, boolean calledFromApi) { String filename) {
// Only stop everything if this is a call made by an outside app trying // Don't allow a filename that is too long
// to if (filename.length() > MAX_FILENAME_LENGTH) {
// use the API. Do NOT stop if this is a call from within the service as return false;
// clearing the speech queue here would be a mistake.
if (calledFromApi) {
stop();
} }
Log.i("TTS", "Synthesizing to " + filename); // Don't allow anything longer than the max text length; since this
boolean synthAvailable = false; // is synthing to a file, don't even bother splitting it.
try { if (text.length() >= MAX_SPEECH_ITEM_CHAR_LENGTH){
synthAvailable = synthesizerLock.tryLock(); return false;
if (!synthAvailable) { }
return false; mSpeechQueue.add(new SpeechItem(text, params, SpeechItem.TEXT_TO_FILE, filename));
} if (!mIsSpeaking) {
// Don't allow a filename that is too long processSpeechQueue();
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();
}
} }
Log.i("TTS", "Completed synthesis for " + filename);
return true; return true;
} }
@@ -957,7 +1011,7 @@ public class TtsService extends Service implements OnCompletionListener {
if (params != null) { if (params != null) {
speakingParams = new ArrayList<String>(Arrays.asList(params)); speakingParams = new ArrayList<String>(Arrays.asList(params));
} }
return mSelf.synthesizeToFile(text, speakingParams, filename, true); return mSelf.synthesizeToFile(text, speakingParams, filename);
} }
/** /**