am 9ea4668c: am ddc2eb8e: Merge "Fix for bug 2564771 - pitch setting between apps can interfere with one another." into froyo

Merge commit '9ea4668c3fabba6d9b8e610030d2ba370b782e86' into kraken

* commit '9ea4668c3fabba6d9b8e610030d2ba370b782e86':
  Fix for bug 2564771 - pitch setting between apps can
This commit is contained in:
Charles Chen
2010-04-01 18:05:21 -07:00
committed by Android Git Automerger
3 changed files with 87 additions and 27 deletions

View File

@@ -43,7 +43,7 @@ interface ITts {
String[] getLanguage(); String[] getLanguage();
int isLanguageAvailable(in String language, in String country, in String variant); int isLanguageAvailable(in String language, in String country, in String variant, in String[] params);
int setLanguage(in String callingApp, in String language, in String country, in String variant); int setLanguage(in String callingApp, in String language, in String country, in String variant);

View File

@@ -314,6 +314,10 @@ public class TextToSpeech {
* {@hide} * {@hide}
*/ */
public static final String KEY_PARAM_ENGINE = "engine"; public static final String KEY_PARAM_ENGINE = "engine";
/**
* {@hide}
*/
public static final String KEY_PARAM_PITCH = "pitch";
/** /**
* Parameter key to specify the audio stream type to be used when speaking text * Parameter key to specify the audio stream type to be used when speaking text
* or playing back a file. * or playing back a file.
@@ -365,7 +369,12 @@ public class TextToSpeech {
/** /**
* {@hide} * {@hide}
*/ */
protected static final int NB_CACHED_PARAMS = 7; protected static final int PARAM_POSITION_PITCH = 14;
/**
* {@hide}
*/
protected static final int NB_CACHED_PARAMS = 8;
} }
/** /**
@@ -409,9 +418,10 @@ public class TextToSpeech {
mCachedParams[Engine.PARAM_POSITION_STREAM] = Engine.KEY_PARAM_STREAM; mCachedParams[Engine.PARAM_POSITION_STREAM] = Engine.KEY_PARAM_STREAM;
mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID] = Engine.KEY_PARAM_UTTERANCE_ID; mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID] = Engine.KEY_PARAM_UTTERANCE_ID;
mCachedParams[Engine.PARAM_POSITION_ENGINE] = Engine.KEY_PARAM_ENGINE; mCachedParams[Engine.PARAM_POSITION_ENGINE] = Engine.KEY_PARAM_ENGINE;
mCachedParams[Engine.PARAM_POSITION_PITCH] = Engine.KEY_PARAM_PITCH;
// Leave all defaults that are shown in Settings uninitialized so that // Leave all defaults that are shown in Settings uninitialized/at the default
// the values set in Settings will take effect if the application does // so that the values set in Settings will take effect if the application does
// not try to change these settings itself. // not try to change these settings itself.
mCachedParams[Engine.PARAM_POSITION_RATE + 1] = ""; mCachedParams[Engine.PARAM_POSITION_RATE + 1] = "";
mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1] = ""; mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1] = "";
@@ -421,6 +431,7 @@ public class TextToSpeech {
String.valueOf(Engine.DEFAULT_STREAM); String.valueOf(Engine.DEFAULT_STREAM);
mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID + 1] = ""; mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID + 1] = "";
mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = ""; mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = "";
mCachedParams[Engine.PARAM_POSITION_PITCH + 1] = "100";
initTts(); initTts();
} }
@@ -435,6 +446,9 @@ public class TextToSpeech {
synchronized(mStartLock) { synchronized(mStartLock) {
mITts = ITts.Stub.asInterface(service); mITts = ITts.Stub.asInterface(service);
mStarted = true; mStarted = true;
// Cache the default engine and current language
setEngineByPackageName(getDefaultEngine());
setLanguage(getLanguage());
if (mInitListener != null) { if (mInitListener != null) {
// TODO manage failures and missing resources // TODO manage failures and missing resources
mInitListener.onInit(SUCCESS); mInitListener.onInit(SUCCESS);
@@ -1008,15 +1022,13 @@ public class TextToSpeech {
return result; return result;
} }
try { try {
// the pitch is not set here, instead it is cached so it will be associated
// with all upcoming utterances.
if (pitch > 0) { if (pitch > 0) {
result = mITts.setPitch(mPackageName, (int)(pitch*100)); int p = (int)(pitch*100);
mCachedParams[Engine.PARAM_POSITION_PITCH + 1] = String.valueOf(p);
result = SUCCESS;
} }
} catch (RemoteException e) {
// TTS died; restart it.
Log.e("TextToSpeech.java - setPitch", "RemoteException");
e.printStackTrace();
mStarted = false;
initTts();
} catch (NullPointerException e) { } catch (NullPointerException e) {
// TTS died; restart it. // TTS died; restart it.
Log.e("TextToSpeech.java - setPitch", "NullPointerException"); Log.e("TextToSpeech.java - setPitch", "NullPointerException");
@@ -1057,16 +1069,27 @@ public class TextToSpeech {
return result; return result;
} }
try { try {
mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1] = loc.getISO3Language(); String language = loc.getISO3Language();
mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1] = loc.getISO3Country(); String country = loc.getISO3Country();
mCachedParams[Engine.PARAM_POSITION_VARIANT + 1] = loc.getVariant(); String variant = loc.getVariant();
// the language is not set here, instead it is cached so it will be associated // Check if the language, country, variant are available, and cache
// with all upcoming utterances. But we still need to report the language support, // the available parts.
// which is achieved by calling isLanguageAvailable() // Note that the language is not actually set here, instead it is cached so it
result = mITts.isLanguageAvailable( // will be associated with all upcoming utterances.
mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1], result = mITts.isLanguageAvailable(language, country, variant, mCachedParams);
mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1], if (result >= LANG_AVAILABLE){
mCachedParams[Engine.PARAM_POSITION_VARIANT + 1] ); mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1] = language;
if (result >= LANG_COUNTRY_AVAILABLE){
mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1] = country;
} else {
mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1] = "";
}
if (result >= LANG_COUNTRY_VAR_AVAILABLE){
mCachedParams[Engine.PARAM_POSITION_VARIANT + 1] = variant;
} else {
mCachedParams[Engine.PARAM_POSITION_VARIANT + 1] = "";
}
}
} catch (RemoteException e) { } catch (RemoteException e) {
// TTS died; restart it. // TTS died; restart it.
Log.e("TextToSpeech.java - setLanguage", "RemoteException"); Log.e("TextToSpeech.java - setLanguage", "RemoteException");
@@ -1104,11 +1127,18 @@ public class TextToSpeech {
return null; return null;
} }
try { try {
String[] locStrings = mITts.getLanguage(); // Only do a call to the native synth if there is nothing in the cached params
if ((locStrings != null) && (locStrings.length == 3)) { if (mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1].length() < 1){
return new Locale(locStrings[0], locStrings[1], locStrings[2]); String[] locStrings = mITts.getLanguage();
if ((locStrings != null) && (locStrings.length == 3)) {
return new Locale(locStrings[0], locStrings[1], locStrings[2]);
} else {
return null;
}
} else { } else {
return null; return new Locale(mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1],
mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1],
mCachedParams[Engine.PARAM_POSITION_VARIANT + 1]);
} }
} catch (RemoteException e) { } catch (RemoteException e) {
// TTS died; restart it. // TTS died; restart it.
@@ -1151,7 +1181,7 @@ public class TextToSpeech {
} }
try { try {
result = mITts.isLanguageAvailable(loc.getISO3Language(), result = mITts.isLanguageAvailable(loc.getISO3Language(),
loc.getISO3Country(), loc.getVariant()); loc.getISO3Country(), loc.getVariant(), mCachedParams);
} catch (RemoteException e) { } catch (RemoteException e) {
// TTS died; restart it. // TTS died; restart it.
Log.e("TextToSpeech.java - isLanguageAvailable", "RemoteException"); Log.e("TextToSpeech.java - isLanguageAvailable", "RemoteException");

View File

@@ -321,6 +321,10 @@ public class TtsService extends Service implements OnCompletionListener {
TextToSpeech.Engine.DEFAULT_RATE); TextToSpeech.Engine.DEFAULT_RATE);
} }
private int getDefaultPitch() {
// Pitch is not user settable; the default pitch is always 100.
return 100;
}
private String getDefaultLanguage() { private String getDefaultLanguage() {
String defaultLang = android.provider.Settings.Secure.getString(mResolver, String defaultLang = android.provider.Settings.Secure.getString(mResolver,
@@ -786,6 +790,7 @@ public class TtsService extends Service implements OnCompletionListener {
String variant = ""; String variant = "";
String speechRate = ""; String speechRate = "";
String engine = ""; String engine = "";
String pitch = "";
if (speechItem.mParams != null){ if (speechItem.mParams != null){
for (int i = 0; i < speechItem.mParams.size() - 1; i = i + 2){ for (int i = 0; i < speechItem.mParams.size() - 1; i = i + 2){
String param = speechItem.mParams.get(i); String param = speechItem.mParams.get(i);
@@ -809,6 +814,8 @@ public class TtsService extends Service implements OnCompletionListener {
} }
} else if (param.equals(TextToSpeech.Engine.KEY_PARAM_ENGINE)) { } else if (param.equals(TextToSpeech.Engine.KEY_PARAM_ENGINE)) {
engine = speechItem.mParams.get(i + 1); engine = speechItem.mParams.get(i + 1);
} else if (param.equals(TextToSpeech.Engine.KEY_PARAM_PITCH)) {
pitch = speechItem.mParams.get(i + 1);
} }
} }
} }
@@ -831,6 +838,11 @@ public class TtsService extends Service implements OnCompletionListener {
} else { } else {
setSpeechRate("", getDefaultRate()); setSpeechRate("", getDefaultRate());
} }
if (pitch.length() > 0){
setPitch("", Integer.parseInt(pitch));
} else {
setPitch("", getDefaultPitch());
}
try { try {
sNativeSynth.speak(speechItem.mText, streamType); sNativeSynth.speak(speechItem.mText, streamType);
} catch (NullPointerException e) { } catch (NullPointerException e) {
@@ -885,6 +897,7 @@ public class TtsService extends Service implements OnCompletionListener {
String variant = ""; String variant = "";
String speechRate = ""; String speechRate = "";
String engine = ""; String engine = "";
String pitch = "";
if (speechItem.mParams != null){ if (speechItem.mParams != null){
for (int i = 0; i < speechItem.mParams.size() - 1; i = i + 2){ for (int i = 0; i < speechItem.mParams.size() - 1; i = i + 2){
String param = speechItem.mParams.get(i); String param = speechItem.mParams.get(i);
@@ -901,6 +914,8 @@ public class TtsService extends Service implements OnCompletionListener {
utteranceId = speechItem.mParams.get(i+1); utteranceId = speechItem.mParams.get(i+1);
} else if (param.equals(TextToSpeech.Engine.KEY_PARAM_ENGINE)) { } else if (param.equals(TextToSpeech.Engine.KEY_PARAM_ENGINE)) {
engine = speechItem.mParams.get(i + 1); engine = speechItem.mParams.get(i + 1);
} else if (param.equals(TextToSpeech.Engine.KEY_PARAM_PITCH)) {
pitch = speechItem.mParams.get(i + 1);
} }
} }
} }
@@ -923,6 +938,11 @@ public class TtsService extends Service implements OnCompletionListener {
} else { } else {
setSpeechRate("", getDefaultRate()); setSpeechRate("", getDefaultRate());
} }
if (pitch.length() > 0){
setPitch("", Integer.parseInt(pitch));
} else {
setPitch("", getDefaultPitch());
}
try { try {
sNativeSynth.synthesizeToFile(speechItem.mText, speechItem.mFilename); sNativeSynth.synthesizeToFile(speechItem.mText, speechItem.mFilename);
} catch (NullPointerException e) { } catch (NullPointerException e) {
@@ -1377,7 +1397,17 @@ public class TtsService extends Service implements OnCompletionListener {
* TTS_LANG_COUNTRY_AVAILABLE, TTS_LANG_COUNTRY_VAR_AVAILABLE as defined in * TTS_LANG_COUNTRY_AVAILABLE, TTS_LANG_COUNTRY_VAR_AVAILABLE as defined in
* android.speech.tts.TextToSpeech. * android.speech.tts.TextToSpeech.
*/ */
public int isLanguageAvailable(String lang, String country, String variant) { public int isLanguageAvailable(String lang, String country, String variant,
String[] params) {
for (int i = 0; i < params.length - 1; i = i + 2){
String param = params[i];
if (param != null) {
if (param.equals(TextToSpeech.Engine.KEY_PARAM_ENGINE)) {
mSelf.setEngine(params[i + 1]);
break;
}
}
}
return mSelf.isLanguageAvailable(lang, country, variant); return mSelf.isLanguageAvailable(lang, country, variant);
} }