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

Commit 2450173b authored by Android (Google) Code Review's avatar Android (Google) Code Review
Browse files

Merge change 5414 into donut

* changes:
  Add caching of the speech rate and language in android.speech.tts.TextToSpeech so the speech params can be passed along in calls for text synthesis.
parents c4e0ff75 87c9684f
Loading
Loading
Loading
Loading
+41 −52
Original line number Original line Diff line number Diff line
@@ -88,8 +88,6 @@ public class TextToSpeech {
    public static final int TTS_LANG_NOT_SUPPORTED = -2;
    public static final int TTS_LANG_NOT_SUPPORTED = -2;






    /**
    /**
     * Called when the TTS has initialized.
     * Called when the TTS has initialized.
     *
     *
@@ -100,15 +98,6 @@ public class TextToSpeech {
        public void onInit(int status);
        public void onInit(int status);
    }
    }


    /**
     * Called when the TTS has finished speaking by itself (speaking
     * finished without being canceled).
     *
     */
    public interface OnSpeechCompletedListener {
        public void onSpeechCompleted();
    }

    /**
    /**
     * Internal constants for the TTS functionality
     * Internal constants for the TTS functionality
     *
     *
@@ -129,6 +118,16 @@ public class TextToSpeech {
        public static final int CHECK_VOICE_DATA_BAD_DATA = -1;
        public static final int CHECK_VOICE_DATA_BAD_DATA = -1;
        public static final int CHECK_VOICE_DATA_MISSING_DATA = -2;
        public static final int CHECK_VOICE_DATA_MISSING_DATA = -2;
        public static final int CHECK_VOICE_DATA_MISSING_DATA_NO_SDCARD = -3;
        public static final int CHECK_VOICE_DATA_MISSING_DATA_NO_SDCARD = -3;
        
        // keys for the parameters passed with speak commands
        public static final String TTS_KEY_PARAM_RATE = "rate";
        public static final String TTS_KEY_PARAM_LANGUAGE = "language";
        public static final String TTS_KEY_PARAM_COUNTRY = "country";
        public static final String TTS_KEY_PARAM_VARIANT = "variant";
        public static final int TTS_PARAM_POSITION_RATE = 0;
        public static final int TTS_PARAM_POSITION_LANGUAGE = 2;
        public static final int TTS_PARAM_POSITION_COUNTRY = 4;
        public static final int TTS_PARAM_POSITION_VARIANT = 6;
    }
    }


    /**
    /**
@@ -141,11 +140,11 @@ public class TextToSpeech {
    private OnInitListener mInitListener = null;
    private OnInitListener mInitListener = null;
    private boolean mStarted = false;
    private boolean mStarted = false;
    private final Object mStartLock = new Object();
    private final Object mStartLock = new Object();
    private ITtsCallback mITtsCallback;
    private int mCachedRate = Engine.FALLBACK_TTS_DEFAULT_RATE;
    private OnSpeechCompletedListener mSpeechCompListener = null;
    private String mCachedLang = Engine.FALLBACK_TTS_DEFAULT_LANG;
    private final Object mSpeechCompListenerLock = new Object();
    private String mCachedCountry = Engine.FALLBACK_TTS_DEFAULT_COUNTRY;

    private String mCachedVariant = Engine.FALLBACK_TTS_DEFAULT_VARIANT;

    private String[] mCachedParams;


    /**
    /**
     * The constructor for the TTS.
     * The constructor for the TTS.
@@ -159,14 +158,23 @@ public class TextToSpeech {
    public TextToSpeech(Context context, OnInitListener listener) {
    public TextToSpeech(Context context, OnInitListener listener) {
        mContext = context;
        mContext = context;
        mInitListener = listener;
        mInitListener = listener;

        mCachedParams = new String[2*4]; //4 parameters, store key and value
        mCachedParams[Engine.TTS_PARAM_POSITION_RATE] = Engine.TTS_KEY_PARAM_RATE;
        mCachedParams[Engine.TTS_PARAM_POSITION_LANGUAGE] = Engine.TTS_KEY_PARAM_LANGUAGE;
        mCachedParams[Engine.TTS_PARAM_POSITION_COUNTRY] = Engine.TTS_KEY_PARAM_COUNTRY;
        mCachedParams[Engine.TTS_PARAM_POSITION_VARIANT] = Engine.TTS_KEY_PARAM_VARIANT;
        updateCachedParamArray();

        initTts();
        initTts();
    }
    }




    public void setOnSpeechCompletedListener(final OnSpeechCompletedListener listener) {
    private void updateCachedParamArray() {
        synchronized(mSpeechCompListenerLock) {
        mCachedParams[Engine.TTS_PARAM_POSITION_RATE+1] = String.valueOf(mCachedRate);
            mSpeechCompListener = listener;
        mCachedParams[Engine.TTS_PARAM_POSITION_LANGUAGE+1] = mCachedLang;
        }
        mCachedParams[Engine.TTS_PARAM_POSITION_COUNTRY+1] = mCachedCountry;
        mCachedParams[Engine.TTS_PARAM_POSITION_VARIANT+1] = mCachedVariant;
    }
    }




@@ -178,34 +186,7 @@ public class TextToSpeech {
            public void onServiceConnected(ComponentName name, IBinder service) {
            public void onServiceConnected(ComponentName name, IBinder service) {
                synchronized(mStartLock) {
                synchronized(mStartLock) {
                    mITts = ITts.Stub.asInterface(service);
                    mITts = ITts.Stub.asInterface(service);
                    try {
                        mITtsCallback = new ITtsCallback.Stub() {
                            public void markReached(String mark)
                            throws RemoteException {
                                // call the listener of that event, but not
                                // while locked.
                                OnSpeechCompletedListener listener = null;
                                synchronized(mSpeechCompListenerLock) {
                                    listener = mSpeechCompListener;
                                }
                                if (listener != null) {
                                    listener.onSpeechCompleted();
                                }
                            }
                        };
                        mITts.registerCallback(mITtsCallback);

                    } catch (RemoteException e) {
                        initTts();
                        return;
                    }

                    mStarted = true;
                    mStarted = true;
                    // The callback can become null if the Android OS decides to
                    // restart the TTS process as well as whatever is using it.
                    // In such cases, do nothing - the error handling from the
                    // speaking calls will kick in and force a proper restart of
                    // the TTS.
                    if (mInitListener != null) {
                    if (mInitListener != null) {
                        // TODO manage failures and missing resources
                        // TODO manage failures and missing resources
                        mInitListener.onInit(TTS_SUCCESS);
                        mInitListener.onInit(TTS_SUCCESS);
@@ -352,8 +333,8 @@ public class TextToSpeech {
                return;
                return;
            }
            }
            try {
            try {
                // TODO support extra parameters, passing null for the moment
                // TODO support extra parameters, passing cache of current parameters for the moment
                mITts.speak(text, queueMode, null);
                mITts.speak(text, queueMode, mCachedParams);
            } catch (RemoteException e) {
            } catch (RemoteException e) {
                // TTS died; restart it.
                // TTS died; restart it.
                mStarted = false;
                mStarted = false;
@@ -510,7 +491,9 @@ public class TextToSpeech {
            }
            }
            try {
            try {
                if (speechRate > 0) {
                if (speechRate > 0) {
                    mITts.setSpeechRate((int)(speechRate*100));
                    mCachedRate = (int)(speechRate*100);
                    updateCachedParamArray();
                    mITts.setSpeechRate(mCachedRate);
                }
                }
            } catch (RemoteException e) {
            } catch (RemoteException e) {
                // TTS died; restart it.
                // TTS died; restart it.
@@ -568,7 +551,11 @@ public class TextToSpeech {
                return;
                return;
            }
            }
            try {
            try {
                mITts.setLanguage(loc.getISO3Language(), loc.getISO3Country(), loc.getVariant());
                mCachedLang = loc.getISO3Language();
                mCachedCountry = loc.getISO3Country();
                mCachedVariant = loc.getVariant();
                updateCachedParamArray();
                mITts.setLanguage(mCachedLang, mCachedCountry, mCachedVariant);
            } catch (RemoteException e) {
            } catch (RemoteException e) {
                // TTS died; restart it.
                // TTS died; restart it.
                mStarted = false;
                mStarted = false;
@@ -582,10 +569,12 @@ public class TextToSpeech {
     *
     *
     * @param loc
     * @param loc
     *            The locale describing the language to be used.
     *            The locale describing the language to be used.
     * @return one of TTS_LANG_NOT_SUPPORTED, TTS_LANG_MISSING_DATA, TTS_LANG_AVAILABLE,
               TTS_LANG_COUNTRY_AVAILABLE, TTS_LANG_COUNTRY_VAR_AVAILABLE.
     */
     */
    public int isLanguageAvailable(Locale loc) {
    public int isLanguageAvailable(Locale loc) {
        //TODO: Implement isLanguageAvailable
        //TODO: Implement isLanguageAvailable
        return 0;
        return TTS_LANG_NOT_SUPPORTED;
    }
    }