Merge change 8026 into donut
* changes: Make sure the speech synthesizer proxy is a singleton in the TTS service.
This commit is contained in:
@@ -139,16 +139,18 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
private final ReentrantLock speechQueueLock = new ReentrantLock();
|
private final ReentrantLock speechQueueLock = new ReentrantLock();
|
||||||
private final ReentrantLock synthesizerLock = new ReentrantLock();
|
private final ReentrantLock synthesizerLock = new ReentrantLock();
|
||||||
|
|
||||||
private SynthProxy nativeSynth;
|
private static SynthProxy sNativeSynth = null;
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
Log.i("TTS", "TTS starting");
|
Log.i("TtsService", "TtsService.onCreate()");
|
||||||
|
|
||||||
mResolver = getContentResolver();
|
mResolver = getContentResolver();
|
||||||
|
|
||||||
String soLibPath = "/system/lib/libttspico.so";
|
String soLibPath = "/system/lib/libttspico.so";
|
||||||
nativeSynth = new SynthProxy(soLibPath);
|
if (sNativeSynth == null) {
|
||||||
|
sNativeSynth = new SynthProxy(soLibPath);
|
||||||
|
}
|
||||||
|
|
||||||
mSelf = this;
|
mSelf = this;
|
||||||
mIsSpeaking = false;
|
mIsSpeaking = false;
|
||||||
@@ -171,7 +173,7 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
// Don't hog the media player
|
// Don't hog the media player
|
||||||
cleanUpPlayer();
|
cleanUpPlayer();
|
||||||
|
|
||||||
nativeSynth.shutdown();
|
sNativeSynth.shutdown();
|
||||||
|
|
||||||
// Unregister all callbacks.
|
// Unregister all callbacks.
|
||||||
mCallbacks.kill();
|
mCallbacks.kill();
|
||||||
@@ -239,36 +241,36 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
|
|
||||||
private int setSpeechRate(String callingApp, int rate) {
|
private int setSpeechRate(String callingApp, int rate) {
|
||||||
if (isDefaultEnforced()) {
|
if (isDefaultEnforced()) {
|
||||||
return nativeSynth.setSpeechRate(getDefaultRate());
|
return sNativeSynth.setSpeechRate(getDefaultRate());
|
||||||
} else {
|
} else {
|
||||||
return nativeSynth.setSpeechRate(rate);
|
return sNativeSynth.setSpeechRate(rate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int setPitch(String callingApp, int pitch) {
|
private int setPitch(String callingApp, int pitch) {
|
||||||
return nativeSynth.setPitch(pitch);
|
return sNativeSynth.setPitch(pitch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int isLanguageAvailable(String lang, String country, String variant) {
|
private int isLanguageAvailable(String lang, String country, String variant) {
|
||||||
//Log.v("TTS", "TtsService.isLanguageAvailable(" + lang + ", " + country + ", " +variant+")");
|
//Log.v("TtsService", "TtsService.isLanguageAvailable(" + lang + ", " + country + ", " +variant+")");
|
||||||
return nativeSynth.isLanguageAvailable(lang, country, variant);
|
return sNativeSynth.isLanguageAvailable(lang, country, variant);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String[] getLanguage() {
|
private String[] getLanguage() {
|
||||||
return nativeSynth.getLanguage();
|
return sNativeSynth.getLanguage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int setLanguage(String callingApp, String lang, String country, String variant) {
|
private int setLanguage(String callingApp, String lang, String country, String variant) {
|
||||||
//Log.v("TTS", "TtsService.setLanguage(" + lang + ", " + country + ", " + variant + ")");
|
Log.v("TtsService", "TtsService.setLanguage(" + lang + ", " + country + ", " + variant + ")");
|
||||||
if (isDefaultEnforced()) {
|
if (isDefaultEnforced()) {
|
||||||
return nativeSynth.setLanguage(getDefaultLanguage(), getDefaultCountry(),
|
return sNativeSynth.setLanguage(getDefaultLanguage(), getDefaultCountry(),
|
||||||
getDefaultLocVariant());
|
getDefaultLocVariant());
|
||||||
} else {
|
} else {
|
||||||
return nativeSynth.setLanguage(lang, country, variant);
|
return sNativeSynth.setLanguage(lang, country, variant);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,7 +342,7 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
* engines.
|
* engines.
|
||||||
*/
|
*/
|
||||||
private int speak(String callingApp, String text, int queueMode, ArrayList<String> params) {
|
private int speak(String callingApp, String text, int queueMode, ArrayList<String> params) {
|
||||||
Log.i("TTS service received", text);
|
Log.v("TtsService", "TTS service received " + text);
|
||||||
if (queueMode == TextToSpeech.TTS_QUEUE_FLUSH) {
|
if (queueMode == TextToSpeech.TTS_QUEUE_FLUSH) {
|
||||||
stop(callingApp);
|
stop(callingApp);
|
||||||
} else if (queueMode == 2) {
|
} else if (queueMode == 2) {
|
||||||
@@ -390,7 +392,7 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
// something has gone very wrong with processSpeechQueue.
|
// something has gone very wrong with processSpeechQueue.
|
||||||
speechQueueAvailable = speechQueueLock.tryLock(1000, TimeUnit.MILLISECONDS);
|
speechQueueAvailable = speechQueueLock.tryLock(1000, TimeUnit.MILLISECONDS);
|
||||||
if (speechQueueAvailable) {
|
if (speechQueueAvailable) {
|
||||||
Log.i("TTS", "Stopping");
|
Log.i("TtsService", "Stopping");
|
||||||
for (int i = mSpeechQueue.size() - 1; i > -1; i--){
|
for (int i = mSpeechQueue.size() - 1; i > -1; i--){
|
||||||
if (mSpeechQueue.get(i).mCallingApp.equals(callingApp)){
|
if (mSpeechQueue.get(i).mCallingApp.equals(callingApp)){
|
||||||
mSpeechQueue.remove(i);
|
mSpeechQueue.remove(i);
|
||||||
@@ -398,7 +400,7 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
}
|
}
|
||||||
if ((mCurrentSpeechItem != null) &&
|
if ((mCurrentSpeechItem != null) &&
|
||||||
mCurrentSpeechItem.mCallingApp.equals(callingApp)) {
|
mCurrentSpeechItem.mCallingApp.equals(callingApp)) {
|
||||||
result = nativeSynth.stop();
|
result = sNativeSynth.stop();
|
||||||
mKillList.put(mCurrentSpeechItem, true);
|
mKillList.put(mCurrentSpeechItem, true);
|
||||||
if (mPlayer != null) {
|
if (mPlayer != null) {
|
||||||
try {
|
try {
|
||||||
@@ -412,10 +414,10 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
} else {
|
} else {
|
||||||
result = TextToSpeech.TTS_SUCCESS;
|
result = TextToSpeech.TTS_SUCCESS;
|
||||||
}
|
}
|
||||||
Log.i("TTS", "Stopped");
|
Log.i("TtsService", "Stopped");
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Log.e("TTS stop", "tryLock interrupted");
|
Log.e("TtsService", "TTS stop: tryLock interrupted");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
// This check is needed because finally will always run; even if the
|
// This check is needed because finally will always run; even if the
|
||||||
@@ -448,7 +450,7 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
if ((mCurrentSpeechItem != null) &&
|
if ((mCurrentSpeechItem != null) &&
|
||||||
((mCurrentSpeechItem.mType != SpeechItem.TEXT_TO_FILE) ||
|
((mCurrentSpeechItem.mType != SpeechItem.TEXT_TO_FILE) ||
|
||||||
mCurrentSpeechItem.mCallingApp.equals(callingApp))) {
|
mCurrentSpeechItem.mCallingApp.equals(callingApp))) {
|
||||||
result = nativeSynth.stop();
|
result = sNativeSynth.stop();
|
||||||
mKillList.put(mCurrentSpeechItem, true);
|
mKillList.put(mCurrentSpeechItem, true);
|
||||||
if (mPlayer != null) {
|
if (mPlayer != null) {
|
||||||
try {
|
try {
|
||||||
@@ -462,10 +464,10 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
} else {
|
} else {
|
||||||
result = TextToSpeech.TTS_SUCCESS;
|
result = TextToSpeech.TTS_SUCCESS;
|
||||||
}
|
}
|
||||||
Log.i("TTS", "Stopped all");
|
Log.i("TtsService", "Stopped all");
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Log.e("TTS stopAll", "tryLock interrupted");
|
Log.e("TtsService", "TTS stopAll: tryLock interrupted");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
// This check is needed because finally will always run; even if the
|
// This check is needed because finally will always run; even if the
|
||||||
@@ -588,10 +590,10 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
if (speechRate.length() > 0){
|
if (speechRate.length() > 0){
|
||||||
setSpeechRate("", Integer.parseInt(speechRate));
|
setSpeechRate("", Integer.parseInt(speechRate));
|
||||||
}
|
}
|
||||||
nativeSynth.speak(speechItem.mText, streamType);
|
sNativeSynth.speak(speechItem.mText, streamType);
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Log.e("TTS speakInternalOnly", "tryLock interrupted");
|
Log.e("TtsService", "TTS speakInternalOnly(): tryLock interrupted");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
// This check is needed because finally will always run;
|
// This check is needed because finally will always run;
|
||||||
@@ -617,7 +619,7 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
public void run() {
|
public void run() {
|
||||||
boolean synthAvailable = false;
|
boolean synthAvailable = false;
|
||||||
String utteranceId = "";
|
String utteranceId = "";
|
||||||
Log.i("TTS", "Synthesizing to " + speechItem.mFilename);
|
Log.i("TtsService", "Synthesizing to " + speechItem.mFilename);
|
||||||
try {
|
try {
|
||||||
synthAvailable = synthesizerLock.tryLock();
|
synthAvailable = synthesizerLock.tryLock();
|
||||||
if (!synthAvailable) {
|
if (!synthAvailable) {
|
||||||
@@ -657,10 +659,10 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
if (speechRate.length() > 0){
|
if (speechRate.length() > 0){
|
||||||
setSpeechRate("", Integer.parseInt(speechRate));
|
setSpeechRate("", Integer.parseInt(speechRate));
|
||||||
}
|
}
|
||||||
nativeSynth.synthesizeToFile(speechItem.mText, speechItem.mFilename);
|
sNativeSynth.synthesizeToFile(speechItem.mText, speechItem.mFilename);
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Log.e("TTS synthToFileInternalOnly", "tryLock interrupted");
|
Log.e("TtsService", "TTS synthToFileInternalOnly(): tryLock interrupted");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
// This check is needed because finally will always run;
|
// This check is needed because finally will always run;
|
||||||
@@ -705,7 +707,7 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
if (cb == null){
|
if (cb == null){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Log.i("TTS callback", "dispatch started");
|
Log.i("TtsService", "TTS callback: dispatch started");
|
||||||
// Broadcast to all clients the new value.
|
// Broadcast to all clients the new value.
|
||||||
final int N = mCallbacks.beginBroadcast();
|
final int N = mCallbacks.beginBroadcast();
|
||||||
try {
|
try {
|
||||||
@@ -715,7 +717,7 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
// the dead object for us.
|
// the dead object for us.
|
||||||
}
|
}
|
||||||
mCallbacks.finishBroadcast();
|
mCallbacks.finishBroadcast();
|
||||||
Log.i("TTS callback", "dispatch completed to " + N);
|
Log.i("TtsService", "TTS callback: dispatch completed to " + N);
|
||||||
}
|
}
|
||||||
|
|
||||||
private SpeechItem splitCurrentTextIfNeeded(SpeechItem currentSpeechItem){
|
private SpeechItem splitCurrentTextIfNeeded(SpeechItem currentSpeechItem){
|
||||||
@@ -764,7 +766,7 @@ public class TtsService extends Service implements OnCompletionListener {
|
|||||||
SoundResource sr = getSoundResource(mCurrentSpeechItem);
|
SoundResource sr = getSoundResource(mCurrentSpeechItem);
|
||||||
// Synth speech as needed - synthesizer should call
|
// Synth speech as needed - synthesizer should call
|
||||||
// processSpeechQueue to continue running the queue
|
// processSpeechQueue to continue running the queue
|
||||||
Log.i("TTS processing: ", mCurrentSpeechItem.mText);
|
Log.i("TtsService", "TTS processing: " + mCurrentSpeechItem.mText);
|
||||||
if (sr == null) {
|
if (sr == null) {
|
||||||
if (mCurrentSpeechItem.mType == SpeechItem.TEXT) {
|
if (mCurrentSpeechItem.mType == SpeechItem.TEXT) {
|
||||||
mCurrentSpeechItem = splitCurrentTextIfNeeded(mCurrentSpeechItem);
|
mCurrentSpeechItem = splitCurrentTextIfNeeded(mCurrentSpeechItem);
|
||||||
|
|||||||
Reference in New Issue
Block a user