Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 60dd3606 authored by Charles Chen's avatar Charles Chen
Browse files

Enabling multiple apps to use different speech synthesis engines and not

interfere with one another.
parent 6414970a
Loading
Loading
Loading
Loading
+71 −49
Original line number Diff line number Diff line
@@ -285,6 +285,10 @@ public class TextToSpeech {
         * {@hide}
         */
        public static final String KEY_PARAM_VARIANT = "variant";
        /**
         * {@hide}
         */
        public static final String KEY_PARAM_ENGINE = "engine";
        /**
         * Parameter key to specify the audio stream type to be used when speaking text
         * or playing back a file.
@@ -327,10 +331,16 @@ public class TextToSpeech {
         * {@hide}
         */
        protected static final int PARAM_POSITION_UTTERANCE_ID = 10;

        /**
         * {@hide}
         */
        protected static final int PARAM_POSITION_ENGINE = 12;

        /**
         * {@hide}
         */
        protected static final int NB_CACHED_PARAMS = 6;
        protected static final int NB_CACHED_PARAMS = 7;
    }

    /**
@@ -373,6 +383,7 @@ public class TextToSpeech {
        mCachedParams[Engine.PARAM_POSITION_VARIANT] = Engine.KEY_PARAM_VARIANT;
        mCachedParams[Engine.PARAM_POSITION_STREAM] = Engine.KEY_PARAM_STREAM;
        mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID] = Engine.KEY_PARAM_UTTERANCE_ID;
        mCachedParams[Engine.PARAM_POSITION_ENGINE] = Engine.KEY_PARAM_ENGINE;

        mCachedParams[Engine.PARAM_POSITION_RATE + 1] =
                String.valueOf(Engine.DEFAULT_RATE);
@@ -381,10 +392,10 @@ public class TextToSpeech {
        mCachedParams[Engine.PARAM_POSITION_LANGUAGE + 1] = defaultLoc.getISO3Language();
        mCachedParams[Engine.PARAM_POSITION_COUNTRY + 1] = defaultLoc.getISO3Country();
        mCachedParams[Engine.PARAM_POSITION_VARIANT + 1] = defaultLoc.getVariant();

        mCachedParams[Engine.PARAM_POSITION_STREAM + 1] =
                String.valueOf(Engine.DEFAULT_STREAM);
        mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID + 1] = "";
        mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = Engine.DEFAULT_SYNTH;

        initTts();
    }
@@ -684,6 +695,10 @@ public class TextToSpeech {
                    if (extra != null) {
                        mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID + 1] = extra;
                    }
                    extra = params.get(Engine.KEY_PARAM_ENGINE);
                    if (extra != null) {
                        mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = extra;
                    }
                }
                result = mITts.speak(mPackageName, text, queueMode, mCachedParams);
            } catch (RemoteException e) {
@@ -1166,6 +1181,10 @@ public class TextToSpeech {
                    if (extra != null) {
                        mCachedParams[Engine.PARAM_POSITION_UTTERANCE_ID + 1] = extra;
                    }
                    extra = params.get(Engine.KEY_PARAM_ENGINE);
                    if (extra != null) {
                        mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = extra;
                    }
                }
                if (mITts.synthesizeToFile(mPackageName, text, mCachedParams, filename)){
                    result = SUCCESS;
@@ -1270,6 +1289,9 @@ public class TextToSpeech {
            }
            try {
                result = mITts.setEngineByPackageName(enginePackageName);
                if (result == TextToSpeech.SUCCESS){
                    mCachedParams[Engine.PARAM_POSITION_ENGINE + 1] = enginePackageName;
                }
            } catch (RemoteException e) {
                // TTS died; restart it.
                Log.e("TextToSpeech.java - setEngineByPackageName", "RemoteException");
+12 −0
Original line number Diff line number Diff line
@@ -744,6 +744,7 @@ public class TtsService extends Service implements OnCompletionListener {
                    String country = "";
                    String variant = "";
                    String speechRate = "";
                    String engine = "";
                    if (speechItem.mParams != null){
                        for (int i = 0; i < speechItem.mParams.size() - 1; i = i + 2){
                            String param = speechItem.mParams.get(i);
@@ -765,12 +766,17 @@ public class TtsService extends Service implements OnCompletionListener {
                                    } catch (NumberFormatException e) {
                                        streamType = DEFAULT_STREAM_TYPE;
                                    }
                                } else if (param.equals(TextToSpeech.Engine.KEY_PARAM_ENGINE)) {
                                    engine = speechItem.mParams.get(i + 1);
                                }
                            }
                        }
                    }
                    // Only do the synthesis if it has not been killed by a subsequent utterance.
                    if (mKillList.get(speechItem) == null) {
                        if (engine.length() > 0) {
                            setEngine(engine);
                        }
                        if (language.length() > 0){
                            setLanguage("", language, country, variant);
                        }
@@ -825,6 +831,7 @@ public class TtsService extends Service implements OnCompletionListener {
                    String country = "";
                    String variant = "";
                    String speechRate = "";
                    String engine = "";
                    if (speechItem.mParams != null){
                        for (int i = 0; i < speechItem.mParams.size() - 1; i = i + 2){
                            String param = speechItem.mParams.get(i);
@@ -839,12 +846,17 @@ public class TtsService extends Service implements OnCompletionListener {
                                    variant = speechItem.mParams.get(i+1);
                                } else if (param.equals(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID)){
                                    utteranceId = speechItem.mParams.get(i+1);
                                } else if (param.equals(TextToSpeech.Engine.KEY_PARAM_ENGINE)) {
                                    engine = speechItem.mParams.get(i + 1);
                                }
                            }
                        }
                    }
                    // Only do the synthesis if it has not been killed by a subsequent utterance.
                    if (mKillList.get(speechItem) == null){
                        if (engine.length() > 0) {
                            setEngine(engine);
                        }
                        if (language.length() > 0){
                            setLanguage("", language, country, variant);
                        }