Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | package android.speech.tts; |
| 17 | |
Niels Egberts | c99ba1c | 2015-11-20 11:44:25 +0000 | [diff] [blame] | 18 | import android.annotation.NonNull; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 19 | import android.app.Service; |
| 20 | import android.content.Intent; |
Przemyslaw Szczepaniak | 672695e | 2014-07-14 17:10:42 +0100 | [diff] [blame] | 21 | import android.media.AudioAttributes; |
Glenn Kasten | 33b8404 | 2016-03-08 12:02:55 -0800 | [diff] [blame] | 22 | import android.media.AudioManager; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 23 | import android.net.Uri; |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 24 | import android.os.Binder; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 25 | import android.os.Bundle; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 26 | import android.os.Handler; |
| 27 | import android.os.HandlerThread; |
| 28 | import android.os.IBinder; |
| 29 | import android.os.Looper; |
| 30 | import android.os.Message; |
| 31 | import android.os.MessageQueue; |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 32 | import android.os.ParcelFileDescriptor; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 33 | import android.os.RemoteCallbackList; |
| 34 | import android.os.RemoteException; |
| 35 | import android.provider.Settings; |
| 36 | import android.speech.tts.TextToSpeech.Engine; |
| 37 | import android.text.TextUtils; |
| 38 | import android.util.Log; |
| 39 | |
Przemyslaw Szczepaniak | 5acb33a | 2013-02-08 16:36:25 +0000 | [diff] [blame] | 40 | import java.io.FileOutputStream; |
Przemyslaw Szczepaniak | fcf671b | 2013-03-05 11:28:50 +0000 | [diff] [blame] | 41 | import java.io.IOException; |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 42 | import java.util.ArrayList; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 43 | import java.util.HashMap; |
Niels Egberts | 54545f7 | 2015-08-05 15:41:18 +0100 | [diff] [blame] | 44 | import java.util.HashSet; |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 45 | import java.util.List; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 46 | import java.util.Locale; |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 47 | import java.util.MissingResourceException; |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 48 | import java.util.Set; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | /** |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 52 | * Abstract base class for TTS engine implementations. The following methods |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 53 | * need to be implemented: |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 54 | * <ul> |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 55 | * <li>{@link #onIsLanguageAvailable}</li> |
| 56 | * <li>{@link #onLoadLanguage}</li> |
| 57 | * <li>{@link #onGetLanguage}</li> |
| 58 | * <li>{@link #onSynthesizeText}</li> |
| 59 | * <li>{@link #onStop}</li> |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 60 | * </ul> |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 61 | * The first three deal primarily with language management, and are used to |
| 62 | * query the engine for it's support for a given language and indicate to it |
| 63 | * that requests in a given language are imminent. |
| 64 | * |
| 65 | * {@link #onSynthesizeText} is central to the engine implementation. The |
| 66 | * implementation should synthesize text as per the request parameters and |
| 67 | * return synthesized data via the supplied callback. This class and its helpers |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 68 | * will then consume that data, which might mean queuing it for playback or writing |
| 69 | * it to a file or similar. All calls to this method will be on a single thread, |
| 70 | * which will be different from the main thread of the service. Synthesis must be |
| 71 | * synchronous which means the engine must NOT hold on to the callback or call any |
| 72 | * methods on it after the method returns. |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 73 | * |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 74 | * {@link #onStop} tells the engine that it should stop |
| 75 | * all ongoing synthesis, if any. Any pending data from the current synthesis |
| 76 | * will be discarded. |
Narayan Kamath | e5b8c4d | 2011-08-22 15:37:47 +0100 | [diff] [blame] | 77 | * |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 78 | * {@link #onGetLanguage} is not required as of JELLYBEAN_MR2 (API 18) and later, it is only |
| 79 | * called on earlier versions of Android. |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 80 | * |
| 81 | * API Level 20 adds support for Voice objects. Voices are an abstraction that allow the TTS |
| 82 | * service to expose multiple backends for a single locale. Each one of them can have a different |
| 83 | * features set. In order to fully take advantage of voices, an engine should implement |
| 84 | * the following methods: |
| 85 | * <ul> |
| 86 | * <li>{@link #onGetVoices()}</li> |
Przemyslaw Szczepaniak | 35c7698 | 2014-09-05 15:36:49 +0100 | [diff] [blame] | 87 | * <li>{@link #onIsValidVoiceName(String)}</li> |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 88 | * <li>{@link #onLoadVoice(String)}</li> |
| 89 | * <li>{@link #onGetDefaultVoiceNameFor(String, String, String)}</li> |
| 90 | * </ul> |
| 91 | * The first three methods are siblings of the {@link #onGetLanguage}, |
| 92 | * {@link #onIsLanguageAvailable} and {@link #onLoadLanguage} methods. The last one, |
| 93 | * {@link #onGetDefaultVoiceNameFor(String, String, String)} is a link between locale and voice |
Przemyslaw Szczepaniak | 6ddd9c2 | 2014-09-25 10:58:48 +0100 | [diff] [blame] | 94 | * based methods. Since API level 21 {@link TextToSpeech#setLanguage} is implemented by |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 95 | * calling {@link TextToSpeech#setVoice} with the voice returned by |
| 96 | * {@link #onGetDefaultVoiceNameFor(String, String, String)}. |
| 97 | * |
| 98 | * If the client uses a voice instead of a locale, {@link SynthesisRequest} will contain the |
| 99 | * requested voice name. |
| 100 | * |
| 101 | * The default implementations of Voice-related methods implement them using the |
| 102 | * pre-existing locale-based implementation. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 103 | */ |
| 104 | public abstract class TextToSpeechService extends Service { |
| 105 | |
| 106 | private static final boolean DBG = false; |
| 107 | private static final String TAG = "TextToSpeechService"; |
| 108 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 109 | private static final String SYNTH_THREAD_NAME = "SynthThread"; |
| 110 | |
| 111 | private SynthHandler mSynthHandler; |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 112 | // A thread and it's associated handler for playing back any audio |
| 113 | // associated with this TTS engine. Will handle all requests except synthesis |
| 114 | // to file requests, which occur on the synthesis thread. |
Niels Egberts | c99ba1c | 2015-11-20 11:44:25 +0000 | [diff] [blame] | 115 | @NonNull private AudioPlaybackHandler mAudioPlaybackHandler; |
Narayan Kamath | e5b8c4d | 2011-08-22 15:37:47 +0100 | [diff] [blame] | 116 | private TtsEngines mEngineHelper; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 117 | |
| 118 | private CallbackMap mCallbacks; |
Narayan Kamath | 6dabb63 | 2011-07-08 12:13:03 +0100 | [diff] [blame] | 119 | private String mPackageName; |
Narayan Kamath | 7a3af86 | 2011-06-02 17:28:57 +0100 | [diff] [blame] | 120 | |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 121 | private final Object mVoicesInfoLock = new Object(); |
| 122 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 123 | @Override |
| 124 | public void onCreate() { |
| 125 | if (DBG) Log.d(TAG, "onCreate()"); |
| 126 | super.onCreate(); |
| 127 | |
| 128 | SynthThread synthThread = new SynthThread(); |
| 129 | synthThread.start(); |
| 130 | mSynthHandler = new SynthHandler(synthThread.getLooper()); |
| 131 | |
Narayan Kamath | 4924fe3 | 2011-06-09 11:35:13 +0100 | [diff] [blame] | 132 | mAudioPlaybackHandler = new AudioPlaybackHandler(); |
| 133 | mAudioPlaybackHandler.start(); |
Narayan Kamath | c90f1c8 | 2011-05-24 11:39:43 +0100 | [diff] [blame] | 134 | |
Narayan Kamath | e5b8c4d | 2011-08-22 15:37:47 +0100 | [diff] [blame] | 135 | mEngineHelper = new TtsEngines(this); |
| 136 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 137 | mCallbacks = new CallbackMap(); |
| 138 | |
Narayan Kamath | 6dabb63 | 2011-07-08 12:13:03 +0100 | [diff] [blame] | 139 | mPackageName = getApplicationInfo().packageName; |
| 140 | |
Narayan Kamath | e5b8c4d | 2011-08-22 15:37:47 +0100 | [diff] [blame] | 141 | String[] defaultLocale = getSettingsLocale(); |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 142 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 143 | // Load default language |
Narayan Kamath | e5b8c4d | 2011-08-22 15:37:47 +0100 | [diff] [blame] | 144 | onLoadLanguage(defaultLocale[0], defaultLocale[1], defaultLocale[2]); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void onDestroy() { |
| 149 | if (DBG) Log.d(TAG, "onDestroy()"); |
| 150 | |
| 151 | // Tell the synthesizer to stop |
| 152 | mSynthHandler.quit(); |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 153 | // Tell the audio playback thread to stop. |
| 154 | mAudioPlaybackHandler.quit(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 155 | // Unregister all callbacks. |
| 156 | mCallbacks.kill(); |
| 157 | |
| 158 | super.onDestroy(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Checks whether the engine supports a given language. |
| 163 | * |
| 164 | * Can be called on multiple threads. |
| 165 | * |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 166 | * Its return values HAVE to be consistent with onLoadLanguage. |
| 167 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 168 | * @param lang ISO-3 language code. |
| 169 | * @param country ISO-3 country code. May be empty or null. |
| 170 | * @param variant Language variant. May be empty or null. |
| 171 | * @return Code indicating the support status for the locale. |
| 172 | * One of {@link TextToSpeech#LANG_AVAILABLE}, |
| 173 | * {@link TextToSpeech#LANG_COUNTRY_AVAILABLE}, |
| 174 | * {@link TextToSpeech#LANG_COUNTRY_VAR_AVAILABLE}, |
| 175 | * {@link TextToSpeech#LANG_MISSING_DATA} |
| 176 | * {@link TextToSpeech#LANG_NOT_SUPPORTED}. |
| 177 | */ |
| 178 | protected abstract int onIsLanguageAvailable(String lang, String country, String variant); |
| 179 | |
| 180 | /** |
| 181 | * Returns the language, country and variant currently being used by the TTS engine. |
| 182 | * |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 183 | * This method will be called only on Android 4.2 and before (API <= 17). In later versions |
| 184 | * this method is not called by the Android TTS framework. |
| 185 | * |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 186 | * Can be called on multiple threads. |
| 187 | * |
| 188 | * @return A 3-element array, containing language (ISO 3-letter code), |
| 189 | * country (ISO 3-letter code) and variant used by the engine. |
| 190 | * The country and variant may be {@code ""}. If country is empty, then variant must |
| 191 | * be empty too. |
| 192 | * @see Locale#getISO3Language() |
| 193 | * @see Locale#getISO3Country() |
| 194 | * @see Locale#getVariant() |
| 195 | */ |
| 196 | protected abstract String[] onGetLanguage(); |
| 197 | |
| 198 | /** |
| 199 | * Notifies the engine that it should load a speech synthesis language. There is no guarantee |
| 200 | * that this method is always called before the language is used for synthesis. It is merely |
| 201 | * a hint to the engine that it will probably get some synthesis requests for this language |
| 202 | * at some point in the future. |
| 203 | * |
| 204 | * Can be called on multiple threads. |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 205 | * In <= Android 4.2 (<= API 17) can be called on main and service binder threads. |
| 206 | * In > Android 4.2 (> API 17) can be called on main and synthesis threads. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 207 | * |
| 208 | * @param lang ISO-3 language code. |
| 209 | * @param country ISO-3 country code. May be empty or null. |
| 210 | * @param variant Language variant. May be empty or null. |
| 211 | * @return Code indicating the support status for the locale. |
| 212 | * One of {@link TextToSpeech#LANG_AVAILABLE}, |
| 213 | * {@link TextToSpeech#LANG_COUNTRY_AVAILABLE}, |
| 214 | * {@link TextToSpeech#LANG_COUNTRY_VAR_AVAILABLE}, |
| 215 | * {@link TextToSpeech#LANG_MISSING_DATA} |
| 216 | * {@link TextToSpeech#LANG_NOT_SUPPORTED}. |
| 217 | */ |
| 218 | protected abstract int onLoadLanguage(String lang, String country, String variant); |
| 219 | |
| 220 | /** |
| 221 | * Notifies the service that it should stop any in-progress speech synthesis. |
| 222 | * This method can be called even if no speech synthesis is currently in progress. |
| 223 | * |
| 224 | * Can be called on multiple threads, but not on the synthesis thread. |
| 225 | */ |
| 226 | protected abstract void onStop(); |
| 227 | |
| 228 | /** |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 229 | * Tells the service to synthesize speech from the given text. This method should block until |
| 230 | * the synthesis is finished. Called on the synthesis thread. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 231 | * |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 232 | * @param request The synthesis request. |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 233 | * @param callback The callback that the engine must use to make data available for playback or |
| 234 | * for writing to a file. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 235 | */ |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 236 | protected abstract void onSynthesizeText(SynthesisRequest request, SynthesisCallback callback); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 237 | |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 238 | /** |
| 239 | * Queries the service for a set of features supported for a given language. |
| 240 | * |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 241 | * Can be called on multiple threads. |
| 242 | * |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 243 | * @param lang ISO-3 language code. |
| 244 | * @param country ISO-3 country code. May be empty or null. |
| 245 | * @param variant Language variant. May be empty or null. |
| 246 | * @return A list of features supported for the given language. |
| 247 | */ |
| 248 | protected Set<String> onGetFeaturesForLanguage(String lang, String country, String variant) { |
Niels Egberts | 54545f7 | 2015-08-05 15:41:18 +0100 | [diff] [blame] | 249 | return new HashSet<String>(); |
Narayan Kamath | 748af66 | 2011-10-31 14:20:01 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 252 | private int getExpectedLanguageAvailableStatus(Locale locale) { |
| 253 | int expectedStatus = TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE; |
| 254 | if (locale.getVariant().isEmpty()) { |
| 255 | if (locale.getCountry().isEmpty()) { |
| 256 | expectedStatus = TextToSpeech.LANG_AVAILABLE; |
| 257 | } else { |
| 258 | expectedStatus = TextToSpeech.LANG_COUNTRY_AVAILABLE; |
| 259 | } |
| 260 | } |
| 261 | return expectedStatus; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Queries the service for a set of supported voices. |
| 266 | * |
| 267 | * Can be called on multiple threads. |
| 268 | * |
| 269 | * The default implementation tries to enumerate all available locales, pass them to |
| 270 | * {@link #onIsLanguageAvailable(String, String, String)} and create Voice instances (using |
| 271 | * the locale's BCP-47 language tag as the voice name) for the ones that are supported. |
| 272 | * Note, that this implementation is suitable only for engines that don't have multiple voices |
| 273 | * for a single locale. Also, this implementation won't work with Locales not listed in the |
| 274 | * set returned by the {@link Locale#getAvailableLocales()} method. |
| 275 | * |
| 276 | * @return A list of voices supported. |
| 277 | */ |
Przemyslaw Szczepaniak | 35c7698 | 2014-09-05 15:36:49 +0100 | [diff] [blame] | 278 | public List<Voice> onGetVoices() { |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 279 | // Enumerate all locales and check if they are available |
| 280 | ArrayList<Voice> voices = new ArrayList<Voice>(); |
| 281 | for (Locale locale : Locale.getAvailableLocales()) { |
| 282 | int expectedStatus = getExpectedLanguageAvailableStatus(locale); |
| 283 | try { |
| 284 | int localeStatus = onIsLanguageAvailable(locale.getISO3Language(), |
| 285 | locale.getISO3Country(), locale.getVariant()); |
| 286 | if (localeStatus != expectedStatus) { |
| 287 | continue; |
| 288 | } |
| 289 | } catch (MissingResourceException e) { |
| 290 | // Ignore locale without iso 3 codes |
| 291 | continue; |
| 292 | } |
| 293 | Set<String> features = onGetFeaturesForLanguage(locale.getISO3Language(), |
| 294 | locale.getISO3Country(), locale.getVariant()); |
Niels Egberts | 992ea15 | 2015-06-26 16:00:43 +0100 | [diff] [blame] | 295 | String voiceName = onGetDefaultVoiceNameFor(locale.getISO3Language(), |
| 296 | locale.getISO3Country(), locale.getVariant()); |
| 297 | voices.add(new Voice(voiceName, locale, Voice.QUALITY_NORMAL, |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 298 | Voice.LATENCY_NORMAL, false, features)); |
| 299 | } |
| 300 | return voices; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Return a name of the default voice for a given locale. |
| 305 | * |
| 306 | * This method provides a mapping between locales and available voices. This method is |
| 307 | * used in {@link TextToSpeech#setLanguage}, which calls this method and then calls |
| 308 | * {@link TextToSpeech#setVoice} with the voice returned by this method. |
| 309 | * |
| 310 | * Also, it's used by {@link TextToSpeech#getDefaultVoice()} to find a default voice for |
| 311 | * the default locale. |
| 312 | * |
| 313 | * @param lang ISO-3 language code. |
| 314 | * @param country ISO-3 country code. May be empty or null. |
| 315 | * @param variant Language variant. May be empty or null. |
| 316 | |
| 317 | * @return A name of the default voice for a given locale. |
| 318 | */ |
Przemyslaw Szczepaniak | f9ba548 | 2014-09-22 17:39:16 +0100 | [diff] [blame] | 319 | public String onGetDefaultVoiceNameFor(String lang, String country, String variant) { |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 320 | int localeStatus = onIsLanguageAvailable(lang, country, variant); |
| 321 | Locale iso3Locale = null; |
| 322 | switch (localeStatus) { |
| 323 | case TextToSpeech.LANG_AVAILABLE: |
| 324 | iso3Locale = new Locale(lang); |
| 325 | break; |
| 326 | case TextToSpeech.LANG_COUNTRY_AVAILABLE: |
| 327 | iso3Locale = new Locale(lang, country); |
| 328 | break; |
| 329 | case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE: |
| 330 | iso3Locale = new Locale(lang, country, variant); |
| 331 | break; |
| 332 | default: |
| 333 | return null; |
| 334 | } |
| 335 | Locale properLocale = TtsEngines.normalizeTTSLocale(iso3Locale); |
| 336 | String voiceName = properLocale.toLanguageTag(); |
Przemyslaw Szczepaniak | 35c7698 | 2014-09-05 15:36:49 +0100 | [diff] [blame] | 337 | if (onIsValidVoiceName(voiceName) == TextToSpeech.SUCCESS) { |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 338 | return voiceName; |
| 339 | } else { |
| 340 | return null; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Notifies the engine that it should load a speech synthesis voice. There is no guarantee |
| 346 | * that this method is always called before the voice is used for synthesis. It is merely |
| 347 | * a hint to the engine that it will probably get some synthesis requests for this voice |
| 348 | * at some point in the future. |
| 349 | * |
| 350 | * Will be called only on synthesis thread. |
| 351 | * |
| 352 | * The default implementation creates a Locale from the voice name (by interpreting the name as |
| 353 | * a BCP-47 tag for the locale), and passes it to |
| 354 | * {@link #onLoadLanguage(String, String, String)}. |
| 355 | * |
| 356 | * @param voiceName Name of the voice. |
| 357 | * @return {@link TextToSpeech#ERROR} or {@link TextToSpeech#SUCCESS}. |
| 358 | */ |
Przemyslaw Szczepaniak | 35c7698 | 2014-09-05 15:36:49 +0100 | [diff] [blame] | 359 | public int onLoadVoice(String voiceName) { |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 360 | Locale locale = Locale.forLanguageTag(voiceName); |
| 361 | if (locale == null) { |
| 362 | return TextToSpeech.ERROR; |
| 363 | } |
| 364 | int expectedStatus = getExpectedLanguageAvailableStatus(locale); |
| 365 | try { |
| 366 | int localeStatus = onIsLanguageAvailable(locale.getISO3Language(), |
| 367 | locale.getISO3Country(), locale.getVariant()); |
| 368 | if (localeStatus != expectedStatus) { |
| 369 | return TextToSpeech.ERROR; |
| 370 | } |
| 371 | onLoadLanguage(locale.getISO3Language(), |
| 372 | locale.getISO3Country(), locale.getVariant()); |
| 373 | return TextToSpeech.SUCCESS; |
| 374 | } catch (MissingResourceException e) { |
| 375 | return TextToSpeech.ERROR; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Checks whether the engine supports a voice with a given name. |
| 381 | * |
| 382 | * Can be called on multiple threads. |
| 383 | * |
| 384 | * The default implementation treats the voice name as a language tag, creating a Locale from |
| 385 | * the voice name, and passes it to {@link #onIsLanguageAvailable(String, String, String)}. |
| 386 | * |
| 387 | * @param voiceName Name of the voice. |
| 388 | * @return {@link TextToSpeech#ERROR} or {@link TextToSpeech#SUCCESS}. |
| 389 | */ |
Przemyslaw Szczepaniak | 35c7698 | 2014-09-05 15:36:49 +0100 | [diff] [blame] | 390 | public int onIsValidVoiceName(String voiceName) { |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 391 | Locale locale = Locale.forLanguageTag(voiceName); |
| 392 | if (locale == null) { |
| 393 | return TextToSpeech.ERROR; |
| 394 | } |
| 395 | int expectedStatus = getExpectedLanguageAvailableStatus(locale); |
| 396 | try { |
| 397 | int localeStatus = onIsLanguageAvailable(locale.getISO3Language(), |
| 398 | locale.getISO3Country(), locale.getVariant()); |
| 399 | if (localeStatus != expectedStatus) { |
| 400 | return TextToSpeech.ERROR; |
| 401 | } |
| 402 | return TextToSpeech.SUCCESS; |
| 403 | } catch (MissingResourceException e) { |
| 404 | return TextToSpeech.ERROR; |
| 405 | } |
| 406 | } |
| 407 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 408 | private int getDefaultSpeechRate() { |
| 409 | return getSecureSettingInt(Settings.Secure.TTS_DEFAULT_RATE, Engine.DEFAULT_RATE); |
| 410 | } |
| 411 | |
Sergio Sancho | b0cde2c | 2017-01-26 20:39:05 +0000 | [diff] [blame] | 412 | private int getDefaultPitch() { |
| 413 | return getSecureSettingInt(Settings.Secure.TTS_DEFAULT_PITCH, Engine.DEFAULT_PITCH); |
| 414 | } |
| 415 | |
Narayan Kamath | e5b8c4d | 2011-08-22 15:37:47 +0100 | [diff] [blame] | 416 | private String[] getSettingsLocale() { |
Przemyslaw Szczepaniak | 1b5637e | 2014-05-30 11:00:17 +0100 | [diff] [blame] | 417 | final Locale locale = mEngineHelper.getLocalePrefForEngine(mPackageName); |
| 418 | return TtsEngines.toOldLocaleStringFormat(locale); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | private int getSecureSettingInt(String name, int defaultValue) { |
| 422 | return Settings.Secure.getInt(getContentResolver(), name, defaultValue); |
| 423 | } |
| 424 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 425 | /** |
| 426 | * Synthesizer thread. This thread is used to run {@link SynthHandler}. |
| 427 | */ |
| 428 | private class SynthThread extends HandlerThread implements MessageQueue.IdleHandler { |
| 429 | |
| 430 | private boolean mFirstIdle = true; |
| 431 | |
| 432 | public SynthThread() { |
Narayan Kamath | 84deb60 | 2011-07-27 13:22:09 +0100 | [diff] [blame] | 433 | super(SYNTH_THREAD_NAME, android.os.Process.THREAD_PRIORITY_DEFAULT); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | @Override |
| 437 | protected void onLooperPrepared() { |
| 438 | getLooper().getQueue().addIdleHandler(this); |
| 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public boolean queueIdle() { |
| 443 | if (mFirstIdle) { |
| 444 | mFirstIdle = false; |
| 445 | } else { |
| 446 | broadcastTtsQueueProcessingCompleted(); |
| 447 | } |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | private void broadcastTtsQueueProcessingCompleted() { |
| 452 | Intent i = new Intent(TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED); |
| 453 | if (DBG) Log.d(TAG, "Broadcasting: " + i); |
| 454 | sendBroadcast(i); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | private class SynthHandler extends Handler { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 459 | private SpeechItem mCurrentSpeechItem = null; |
| 460 | |
Niels Egberts | 10db95b | 2016-04-07 12:34:55 +0100 | [diff] [blame] | 461 | // When a message with QUEUE_FLUSH arrives we add the caller identity to the List and when a |
| 462 | // message with QUEUE_DESTROY arrives we increment mFlushAll. Then a message is added to the |
| 463 | // handler queue that removes the caller identify from the list and decrements the mFlushAll |
| 464 | // counter. This is so that when a message is processed and the caller identity is in the |
| 465 | // list or mFlushAll is not zero, we know that the message should be flushed. |
| 466 | // It's important that mFlushedObjects is a List and not a Set, and that mFlushAll is an |
| 467 | // int and not a bool. This is because when multiple messages arrive with QUEUE_FLUSH or |
| 468 | // QUEUE_DESTROY, we want to keep flushing messages until we arrive at the last QUEUE_FLUSH |
| 469 | // or QUEUE_DESTROY message. |
| 470 | private List<Object> mFlushedObjects = new ArrayList<>(); |
| 471 | private int mFlushAll = 0; |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 472 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 473 | public SynthHandler(Looper looper) { |
| 474 | super(looper); |
| 475 | } |
| 476 | |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 477 | private void startFlushingSpeechItems(Object callerIdentity) { |
| 478 | synchronized (mFlushedObjects) { |
| 479 | if (callerIdentity == null) { |
Niels Egberts | 10db95b | 2016-04-07 12:34:55 +0100 | [diff] [blame] | 480 | mFlushAll += 1; |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 481 | } else { |
| 482 | mFlushedObjects.add(callerIdentity); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | private void endFlushingSpeechItems(Object callerIdentity) { |
| 487 | synchronized (mFlushedObjects) { |
| 488 | if (callerIdentity == null) { |
Niels Egberts | 10db95b | 2016-04-07 12:34:55 +0100 | [diff] [blame] | 489 | mFlushAll -= 1; |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 490 | } else { |
| 491 | mFlushedObjects.remove(callerIdentity); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | private boolean isFlushed(SpeechItem speechItem) { |
| 496 | synchronized (mFlushedObjects) { |
Niels Egberts | 10db95b | 2016-04-07 12:34:55 +0100 | [diff] [blame] | 497 | return mFlushAll > 0 || mFlushedObjects.contains(speechItem.getCallerIdentity()); |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 501 | private synchronized SpeechItem getCurrentSpeechItem() { |
| 502 | return mCurrentSpeechItem; |
| 503 | } |
| 504 | |
| 505 | private synchronized SpeechItem setCurrentSpeechItem(SpeechItem speechItem) { |
| 506 | SpeechItem old = mCurrentSpeechItem; |
| 507 | mCurrentSpeechItem = speechItem; |
| 508 | return old; |
| 509 | } |
| 510 | |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 511 | private synchronized SpeechItem maybeRemoveCurrentSpeechItem(Object callerIdentity) { |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 512 | if (mCurrentSpeechItem != null && |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 513 | (mCurrentSpeechItem.getCallerIdentity() == callerIdentity)) { |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 514 | SpeechItem current = mCurrentSpeechItem; |
| 515 | mCurrentSpeechItem = null; |
| 516 | return current; |
| 517 | } |
| 518 | |
| 519 | return null; |
| 520 | } |
| 521 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 522 | public boolean isSpeaking() { |
| 523 | return getCurrentSpeechItem() != null; |
| 524 | } |
| 525 | |
| 526 | public void quit() { |
| 527 | // Don't process any more speech items |
| 528 | getLooper().quit(); |
| 529 | // Stop the current speech item |
| 530 | SpeechItem current = setCurrentSpeechItem(null); |
| 531 | if (current != null) { |
| 532 | current.stop(); |
| 533 | } |
Narayan Kamath | be4ad4a | 2011-07-15 13:01:09 +0100 | [diff] [blame] | 534 | // The AudioPlaybackHandler will be destroyed by the caller. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Adds a speech item to the queue. |
| 539 | * |
| 540 | * Called on a service binder thread. |
| 541 | */ |
| 542 | public int enqueueSpeechItem(int queueMode, final SpeechItem speechItem) { |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 543 | UtteranceProgressDispatcher utterenceProgress = null; |
| 544 | if (speechItem instanceof UtteranceProgressDispatcher) { |
| 545 | utterenceProgress = (UtteranceProgressDispatcher) speechItem; |
| 546 | } |
| 547 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 548 | if (!speechItem.isValid()) { |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 549 | if (utterenceProgress != null) { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 550 | utterenceProgress.dispatchOnError( |
Przemyslaw Szczepaniak | fc4b289 | 2014-06-26 11:52:20 +0100 | [diff] [blame] | 551 | TextToSpeech.ERROR_INVALID_REQUEST); |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 552 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 553 | return TextToSpeech.ERROR; |
| 554 | } |
Narayan Kamath | 4924fe3 | 2011-06-09 11:35:13 +0100 | [diff] [blame] | 555 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 556 | if (queueMode == TextToSpeech.QUEUE_FLUSH) { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 557 | stopForApp(speechItem.getCallerIdentity()); |
Narayan Kamath | abc63fb | 2011-06-10 11:36:57 +0100 | [diff] [blame] | 558 | } else if (queueMode == TextToSpeech.QUEUE_DESTROY) { |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 559 | stopAll(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 560 | } |
| 561 | Runnable runnable = new Runnable() { |
| 562 | @Override |
| 563 | public void run() { |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 564 | if (isFlushed(speechItem)) { |
| 565 | speechItem.stop(); |
| 566 | } else { |
| 567 | setCurrentSpeechItem(speechItem); |
| 568 | speechItem.play(); |
| 569 | setCurrentSpeechItem(null); |
| 570 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 571 | } |
| 572 | }; |
| 573 | Message msg = Message.obtain(this, runnable); |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 574 | |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 575 | // The obj is used to remove all callbacks from the given app in |
| 576 | // stopForApp(String). |
Narayan Kamath | abc63fb | 2011-06-10 11:36:57 +0100 | [diff] [blame] | 577 | // |
| 578 | // Note that this string is interned, so the == comparison works. |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 579 | msg.obj = speechItem.getCallerIdentity(); |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 580 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 581 | if (sendMessage(msg)) { |
| 582 | return TextToSpeech.SUCCESS; |
| 583 | } else { |
| 584 | Log.w(TAG, "SynthThread has quit"); |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 585 | if (utterenceProgress != null) { |
Przemyslaw Szczepaniak | fc4b289 | 2014-06-26 11:52:20 +0100 | [diff] [blame] | 586 | utterenceProgress.dispatchOnError(TextToSpeech.ERROR_SERVICE); |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 587 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 588 | return TextToSpeech.ERROR; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Stops all speech output and removes any utterances still in the queue for |
| 594 | * the calling app. |
| 595 | * |
| 596 | * Called on a service binder thread. |
| 597 | */ |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 598 | public int stopForApp(final Object callerIdentity) { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 599 | if (callerIdentity == null) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 600 | return TextToSpeech.ERROR; |
| 601 | } |
Narayan Kamath | 4924fe3 | 2011-06-09 11:35:13 +0100 | [diff] [blame] | 602 | |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 603 | // Flush pending messages from callerIdentity |
| 604 | startFlushingSpeechItems(callerIdentity); |
| 605 | |
Narayan Kamath | be4ad4a | 2011-07-15 13:01:09 +0100 | [diff] [blame] | 606 | // This stops writing data to the file / or publishing |
| 607 | // items to the audio playback handler. |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 608 | // |
| 609 | // Note that the current speech item must be removed only if it |
| 610 | // belongs to the callingApp, else the item will be "orphaned" and |
| 611 | // not stopped correctly if a stop request comes along for the item |
| 612 | // from the app it belongs to. |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 613 | SpeechItem current = maybeRemoveCurrentSpeechItem(callerIdentity); |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 614 | if (current != null) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 615 | current.stop(); |
| 616 | } |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 617 | |
Narayan Kamath | 4924fe3 | 2011-06-09 11:35:13 +0100 | [diff] [blame] | 618 | // Remove any enqueued audio too. |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 619 | mAudioPlaybackHandler.stopForApp(callerIdentity); |
Narayan Kamath | 4924fe3 | 2011-06-09 11:35:13 +0100 | [diff] [blame] | 620 | |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 621 | // Stop flushing pending messages |
| 622 | Runnable runnable = new Runnable() { |
| 623 | @Override |
| 624 | public void run() { |
| 625 | endFlushingSpeechItems(callerIdentity); |
| 626 | } |
| 627 | }; |
| 628 | sendMessage(Message.obtain(this, runnable)); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 629 | return TextToSpeech.SUCCESS; |
| 630 | } |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 631 | |
| 632 | public int stopAll() { |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 633 | // Order to flush pending messages |
| 634 | startFlushingSpeechItems(null); |
| 635 | |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 636 | // Stop the current speech item unconditionally . |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 637 | SpeechItem current = setCurrentSpeechItem(null); |
| 638 | if (current != null) { |
| 639 | current.stop(); |
| 640 | } |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 641 | // Remove all pending playback as well. |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 642 | mAudioPlaybackHandler.stop(); |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 643 | |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 644 | // Message to stop flushing pending messages |
| 645 | Runnable runnable = new Runnable() { |
| 646 | @Override |
| 647 | public void run() { |
| 648 | endFlushingSpeechItems(null); |
| 649 | } |
| 650 | }; |
| 651 | sendMessage(Message.obtain(this, runnable)); |
| 652 | |
| 653 | |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 654 | return TextToSpeech.SUCCESS; |
| 655 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 658 | interface UtteranceProgressDispatcher { |
Niels Egberts | 2c80a038 | 2016-12-23 12:18:22 +0000 | [diff] [blame] | 659 | void dispatchOnStop(); |
| 660 | |
| 661 | void dispatchOnSuccess(); |
| 662 | |
| 663 | void dispatchOnStart(); |
| 664 | |
| 665 | void dispatchOnError(int errorCode); |
| 666 | |
| 667 | void dispatchOnBeginSynthesis(int sampleRateInHz, int audioFormat, int channelCount); |
| 668 | |
| 669 | void dispatchOnAudioAvailable(byte[] audio); |
Niels Egberts | 65c5078 | 2016-12-23 12:01:32 +0000 | [diff] [blame] | 670 | |
Niels Egberts | 5d0ea0f | 2017-03-21 15:29:24 +0000 | [diff] [blame] | 671 | public void dispatchOnRangeStart(int start, int end, int frame); |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 672 | } |
| 673 | |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 674 | /** Set of parameters affecting audio output. */ |
| 675 | static class AudioOutputParams { |
| 676 | /** |
| 677 | * Audio session identifier. May be used to associate audio playback with one of the |
| 678 | * {@link android.media.audiofx.AudioEffect} objects. If not specified by client, |
Glenn Kasten | 33b8404 | 2016-03-08 12:02:55 -0800 | [diff] [blame] | 679 | * it should be equal to {@link AudioManager#AUDIO_SESSION_ID_GENERATE}. |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 680 | */ |
| 681 | public final int mSessionId; |
| 682 | |
| 683 | /** |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 684 | * Volume, in the range [0.0f, 1.0f]. The default value is |
| 685 | * {@link TextToSpeech.Engine#DEFAULT_VOLUME} (1.0f). |
| 686 | */ |
| 687 | public final float mVolume; |
| 688 | |
| 689 | /** |
| 690 | * Left/right position of the audio, in the range [-1.0f, 1.0f]. |
| 691 | * The default value is {@link TextToSpeech.Engine#DEFAULT_PAN} (0.0f). |
| 692 | */ |
| 693 | public final float mPan; |
| 694 | |
Przemyslaw Szczepaniak | 672695e | 2014-07-14 17:10:42 +0100 | [diff] [blame] | 695 | |
| 696 | /** |
| 697 | * Audio attributes, set by {@link TextToSpeech#setAudioAttributes} |
| 698 | * or created from the value of {@link TextToSpeech.Engine#KEY_PARAM_STREAM}. |
| 699 | */ |
| 700 | public final AudioAttributes mAudioAttributes; |
| 701 | |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 702 | /** Create AudioOutputParams with default values */ |
| 703 | AudioOutputParams() { |
Glenn Kasten | 33b8404 | 2016-03-08 12:02:55 -0800 | [diff] [blame] | 704 | mSessionId = AudioManager.AUDIO_SESSION_ID_GENERATE; |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 705 | mVolume = Engine.DEFAULT_VOLUME; |
| 706 | mPan = Engine.DEFAULT_PAN; |
Przemyslaw Szczepaniak | 672695e | 2014-07-14 17:10:42 +0100 | [diff] [blame] | 707 | mAudioAttributes = null; |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 708 | } |
| 709 | |
Przemyslaw Szczepaniak | 672695e | 2014-07-14 17:10:42 +0100 | [diff] [blame] | 710 | AudioOutputParams(int sessionId, float volume, float pan, |
| 711 | AudioAttributes audioAttributes) { |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 712 | mSessionId = sessionId; |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 713 | mVolume = volume; |
| 714 | mPan = pan; |
Przemyslaw Szczepaniak | 672695e | 2014-07-14 17:10:42 +0100 | [diff] [blame] | 715 | mAudioAttributes = audioAttributes; |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /** Create AudioOutputParams from A {@link SynthesisRequest#getParams()} bundle */ |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 719 | static AudioOutputParams createFromParamsBundle(Bundle paramsBundle, boolean isSpeech) { |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 720 | if (paramsBundle == null) { |
| 721 | return new AudioOutputParams(); |
| 722 | } |
| 723 | |
Przemyslaw Szczepaniak | 672695e | 2014-07-14 17:10:42 +0100 | [diff] [blame] | 724 | AudioAttributes audioAttributes = |
| 725 | (AudioAttributes) paramsBundle.getParcelable( |
| 726 | Engine.KEY_PARAM_AUDIO_ATTRIBUTES); |
| 727 | if (audioAttributes == null) { |
| 728 | int streamType = paramsBundle.getInt( |
| 729 | Engine.KEY_PARAM_STREAM, Engine.DEFAULT_STREAM); |
| 730 | audioAttributes = (new AudioAttributes.Builder()) |
| 731 | .setLegacyStreamType(streamType) |
| 732 | .setContentType((isSpeech ? |
| 733 | AudioAttributes.CONTENT_TYPE_SPEECH : |
| 734 | AudioAttributes.CONTENT_TYPE_SONIFICATION)) |
| 735 | .build(); |
| 736 | } |
| 737 | |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 738 | return new AudioOutputParams( |
| 739 | paramsBundle.getInt( |
| 740 | Engine.KEY_PARAM_SESSION_ID, |
Glenn Kasten | 33b8404 | 2016-03-08 12:02:55 -0800 | [diff] [blame] | 741 | AudioManager.AUDIO_SESSION_ID_GENERATE), |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 742 | paramsBundle.getFloat( |
| 743 | Engine.KEY_PARAM_VOLUME, |
| 744 | Engine.DEFAULT_VOLUME), |
| 745 | paramsBundle.getFloat( |
| 746 | Engine.KEY_PARAM_PAN, |
Przemyslaw Szczepaniak | 672695e | 2014-07-14 17:10:42 +0100 | [diff] [blame] | 747 | Engine.DEFAULT_PAN), |
| 748 | audioAttributes); |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 749 | } |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 753 | /** |
| 754 | * An item in the synth thread queue. |
| 755 | */ |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 756 | private abstract class SpeechItem { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 757 | private final Object mCallerIdentity; |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 758 | private final int mCallerUid; |
| 759 | private final int mCallerPid; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 760 | private boolean mStarted = false; |
| 761 | private boolean mStopped = false; |
| 762 | |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 763 | public SpeechItem(Object caller, int callerUid, int callerPid) { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 764 | mCallerIdentity = caller; |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 765 | mCallerUid = callerUid; |
| 766 | mCallerPid = callerPid; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 769 | public Object getCallerIdentity() { |
| 770 | return mCallerIdentity; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 773 | public int getCallerUid() { |
| 774 | return mCallerUid; |
| 775 | } |
| 776 | |
| 777 | public int getCallerPid() { |
| 778 | return mCallerPid; |
| 779 | } |
| 780 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 781 | /** |
| 782 | * Checker whether the item is valid. If this method returns false, the item should not |
| 783 | * be played. |
| 784 | */ |
| 785 | public abstract boolean isValid(); |
| 786 | |
| 787 | /** |
| 788 | * Plays the speech item. Blocks until playback is finished. |
| 789 | * Must not be called more than once. |
| 790 | * |
| 791 | * Only called on the synthesis thread. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 792 | */ |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 793 | public void play() { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 794 | synchronized (this) { |
| 795 | if (mStarted) { |
| 796 | throw new IllegalStateException("play() called twice"); |
| 797 | } |
| 798 | mStarted = true; |
| 799 | } |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 800 | playImpl(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 803 | protected abstract void playImpl(); |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 804 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 805 | /** |
| 806 | * Stops the speech item. |
| 807 | * Must not be called more than once. |
| 808 | * |
| 809 | * Can be called on multiple threads, but not on the synthesis thread. |
| 810 | */ |
| 811 | public void stop() { |
| 812 | synchronized (this) { |
| 813 | if (mStopped) { |
| 814 | throw new IllegalStateException("stop() called twice"); |
| 815 | } |
| 816 | mStopped = true; |
| 817 | } |
| 818 | stopImpl(); |
| 819 | } |
| 820 | |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 821 | protected abstract void stopImpl(); |
| 822 | |
| 823 | protected synchronized boolean isStopped() { |
| 824 | return mStopped; |
| 825 | } |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 826 | |
| 827 | protected synchronized boolean isStarted() { |
| 828 | return mStarted; |
| 829 | } |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | /** |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 833 | * An item in the synth thread queue that process utterance (and call back to client about |
| 834 | * progress). |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 835 | */ |
| 836 | private abstract class UtteranceSpeechItem extends SpeechItem |
| 837 | implements UtteranceProgressDispatcher { |
| 838 | |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 839 | public UtteranceSpeechItem(Object caller, int callerUid, int callerPid) { |
| 840 | super(caller, callerUid, callerPid); |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 843 | @Override |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 844 | public void dispatchOnSuccess() { |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 845 | final String utteranceId = getUtteranceId(); |
Narayan Kamath | 68e2af5 | 2011-11-28 17:10:04 +0000 | [diff] [blame] | 846 | if (utteranceId != null) { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 847 | mCallbacks.dispatchOnSuccess(getCallerIdentity(), utteranceId); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | @Override |
| 852 | public void dispatchOnStop() { |
| 853 | final String utteranceId = getUtteranceId(); |
| 854 | if (utteranceId != null) { |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 855 | mCallbacks.dispatchOnStop(getCallerIdentity(), utteranceId, isStarted()); |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
| 859 | @Override |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 860 | public void dispatchOnStart() { |
| 861 | final String utteranceId = getUtteranceId(); |
Narayan Kamath | 68e2af5 | 2011-11-28 17:10:04 +0000 | [diff] [blame] | 862 | if (utteranceId != null) { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 863 | mCallbacks.dispatchOnStart(getCallerIdentity(), utteranceId); |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
| 867 | @Override |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 868 | public void dispatchOnError(int errorCode) { |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 869 | final String utteranceId = getUtteranceId(); |
Narayan Kamath | 68e2af5 | 2011-11-28 17:10:04 +0000 | [diff] [blame] | 870 | if (utteranceId != null) { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 871 | mCallbacks.dispatchOnError(getCallerIdentity(), utteranceId, errorCode); |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
Niels Egberts | c99ba1c | 2015-11-20 11:44:25 +0000 | [diff] [blame] | 875 | @Override |
| 876 | public void dispatchOnBeginSynthesis(int sampleRateInHz, int audioFormat, int channelCount) { |
| 877 | final String utteranceId = getUtteranceId(); |
| 878 | if (utteranceId != null) { |
| 879 | mCallbacks.dispatchOnBeginSynthesis(getCallerIdentity(), utteranceId, sampleRateInHz, audioFormat, channelCount); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | @Override |
| 884 | public void dispatchOnAudioAvailable(byte[] audio) { |
| 885 | final String utteranceId = getUtteranceId(); |
| 886 | if (utteranceId != null) { |
| 887 | mCallbacks.dispatchOnAudioAvailable(getCallerIdentity(), utteranceId, audio); |
| 888 | } |
| 889 | } |
| 890 | |
Niels Egberts | 65c5078 | 2016-12-23 12:01:32 +0000 | [diff] [blame] | 891 | @Override |
Niels Egberts | 5d0ea0f | 2017-03-21 15:29:24 +0000 | [diff] [blame] | 892 | public void dispatchOnRangeStart(int start, int end, int frame) { |
Niels Egberts | 65c5078 | 2016-12-23 12:01:32 +0000 | [diff] [blame] | 893 | final String utteranceId = getUtteranceId(); |
| 894 | if (utteranceId != null) { |
Niels Egberts | 5d0ea0f | 2017-03-21 15:29:24 +0000 | [diff] [blame] | 895 | mCallbacks.dispatchOnRangeStart( |
| 896 | getCallerIdentity(), utteranceId, start, end, frame); |
Niels Egberts | 65c5078 | 2016-12-23 12:01:32 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 900 | abstract public String getUtteranceId(); |
| 901 | |
| 902 | String getStringParam(Bundle params, String key, String defaultValue) { |
| 903 | return params == null ? defaultValue : params.getString(key, defaultValue); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 906 | int getIntParam(Bundle params, String key, int defaultValue) { |
| 907 | return params == null ? defaultValue : params.getInt(key, defaultValue); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 910 | float getFloatParam(Bundle params, String key, float defaultValue) { |
| 911 | return params == null ? defaultValue : params.getFloat(key, defaultValue); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | /** |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 916 | * Synthesis parameters are kept in a single Bundle passed as parameter. This class allow |
| 917 | * subclasses to access them conveniently. |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 918 | */ |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 919 | private abstract class UtteranceSpeechItemWithParams extends UtteranceSpeechItem { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 920 | protected final Bundle mParams; |
Niels Egberts | df7deef | 2014-06-25 15:30:44 +0100 | [diff] [blame] | 921 | protected final String mUtteranceId; |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 922 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 923 | UtteranceSpeechItemWithParams( |
| 924 | Object callerIdentity, |
| 925 | int callerUid, |
| 926 | int callerPid, |
| 927 | Bundle params, |
| 928 | String utteranceId) { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 929 | super(callerIdentity, callerUid, callerPid); |
| 930 | mParams = params; |
Niels Egberts | df7deef | 2014-06-25 15:30:44 +0100 | [diff] [blame] | 931 | mUtteranceId = utteranceId; |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | boolean hasLanguage() { |
| 935 | return !TextUtils.isEmpty(getStringParam(mParams, Engine.KEY_PARAM_LANGUAGE, null)); |
| 936 | } |
| 937 | |
| 938 | int getSpeechRate() { |
| 939 | return getIntParam(mParams, Engine.KEY_PARAM_RATE, getDefaultSpeechRate()); |
| 940 | } |
| 941 | |
| 942 | int getPitch() { |
Sergio Sancho | b0cde2c | 2017-01-26 20:39:05 +0000 | [diff] [blame] | 943 | return getIntParam(mParams, Engine.KEY_PARAM_PITCH, getDefaultPitch()); |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | @Override |
| 947 | public String getUtteranceId() { |
Niels Egberts | df7deef | 2014-06-25 15:30:44 +0100 | [diff] [blame] | 948 | return mUtteranceId; |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 949 | } |
| 950 | |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 951 | AudioOutputParams getAudioParams() { |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 952 | return AudioOutputParams.createFromParamsBundle(mParams, true); |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 956 | class SynthesisSpeechItem extends UtteranceSpeechItemWithParams { |
Narayan Kamath | 40f71f0 | 2011-11-23 16:42:53 +0000 | [diff] [blame] | 957 | // Never null. |
Niels Egberts | df7deef | 2014-06-25 15:30:44 +0100 | [diff] [blame] | 958 | private final CharSequence mText; |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 959 | private final SynthesisRequest mSynthesisRequest; |
Narayan Kamath | e5b8c4d | 2011-08-22 15:37:47 +0100 | [diff] [blame] | 960 | private final String[] mDefaultLocale; |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 961 | // Non null after synthesis has started, and all accesses |
| 962 | // guarded by 'this'. |
| 963 | private AbstractSynthesisCallback mSynthesisCallback; |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 964 | private final EventLogger mEventLogger; |
Przemyslaw Szczepaniak | 6532783 | 2013-05-31 13:12:52 +0100 | [diff] [blame] | 965 | private final int mCallerUid; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 966 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 967 | public SynthesisSpeechItem( |
| 968 | Object callerIdentity, |
| 969 | int callerUid, |
| 970 | int callerPid, |
| 971 | Bundle params, |
| 972 | String utteranceId, |
| 973 | CharSequence text) { |
Niels Egberts | df7deef | 2014-06-25 15:30:44 +0100 | [diff] [blame] | 974 | super(callerIdentity, callerUid, callerPid, params, utteranceId); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 975 | mText = text; |
Przemyslaw Szczepaniak | 6532783 | 2013-05-31 13:12:52 +0100 | [diff] [blame] | 976 | mCallerUid = callerUid; |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 977 | mSynthesisRequest = new SynthesisRequest(mText, mParams); |
Narayan Kamath | e5b8c4d | 2011-08-22 15:37:47 +0100 | [diff] [blame] | 978 | mDefaultLocale = getSettingsLocale(); |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 979 | setRequestParams(mSynthesisRequest); |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 980 | mEventLogger = new EventLogger(mSynthesisRequest, callerUid, callerPid, mPackageName); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Niels Egberts | df7deef | 2014-06-25 15:30:44 +0100 | [diff] [blame] | 983 | public CharSequence getText() { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 984 | return mText; |
| 985 | } |
| 986 | |
| 987 | @Override |
| 988 | public boolean isValid() { |
Narayan Kamath | 40f71f0 | 2011-11-23 16:42:53 +0000 | [diff] [blame] | 989 | if (mText == null) { |
Narayan Kamath | 9c3d7a8 | 2012-07-20 18:01:43 +0100 | [diff] [blame] | 990 | Log.e(TAG, "null synthesis text"); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 991 | return false; |
| 992 | } |
Przemyslaw Szczepaniak | 2d940bc | 2012-11-19 12:22:59 +0000 | [diff] [blame] | 993 | if (mText.length() >= TextToSpeech.getMaxSpeechInputLength()) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 994 | Log.w(TAG, "Text too long: " + mText.length() + " chars"); |
| 995 | return false; |
| 996 | } |
| 997 | return true; |
| 998 | } |
| 999 | |
| 1000 | @Override |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1001 | protected void playImpl() { |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 1002 | AbstractSynthesisCallback synthesisCallback; |
Narayan Kamath | 6dabb63 | 2011-07-08 12:13:03 +0100 | [diff] [blame] | 1003 | mEventLogger.onRequestProcessingStart(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1004 | synchronized (this) { |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 1005 | // stop() might have been called before we enter this |
| 1006 | // synchronized block. |
| 1007 | if (isStopped()) { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1008 | return; |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 1009 | } |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 1010 | mSynthesisCallback = createSynthesisCallback(); |
| 1011 | synthesisCallback = mSynthesisCallback; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1012 | } |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1013 | |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 1014 | TextToSpeechService.this.onSynthesizeText(mSynthesisRequest, synthesisCallback); |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1015 | |
| 1016 | // Fix for case where client called .start() & .error(), but did not called .done() |
| 1017 | if (synthesisCallback.hasStarted() && !synthesisCallback.hasFinished()) { |
| 1018 | synthesisCallback.done(); |
| 1019 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 1022 | protected AbstractSynthesisCallback createSynthesisCallback() { |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 1023 | return new PlaybackSynthesisCallback(getAudioParams(), |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1024 | mAudioPlaybackHandler, this, getCallerIdentity(), mEventLogger, false); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | private void setRequestParams(SynthesisRequest request) { |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 1028 | String voiceName = getVoiceName(); |
Narayan Kamath | c3edf2a | 2011-06-15 12:35:06 +0100 | [diff] [blame] | 1029 | request.setLanguage(getLanguage(), getCountry(), getVariant()); |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 1030 | if (!TextUtils.isEmpty(voiceName)) { |
| 1031 | request.setVoiceName(getVoiceName()); |
| 1032 | } |
Narayan Kamath | c3edf2a | 2011-06-15 12:35:06 +0100 | [diff] [blame] | 1033 | request.setSpeechRate(getSpeechRate()); |
Przemyslaw Szczepaniak | 6532783 | 2013-05-31 13:12:52 +0100 | [diff] [blame] | 1034 | request.setCallerUid(mCallerUid); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1035 | request.setPitch(getPitch()); |
| 1036 | } |
| 1037 | |
| 1038 | @Override |
| 1039 | protected void stopImpl() { |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 1040 | AbstractSynthesisCallback synthesisCallback; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1041 | synchronized (this) { |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 1042 | synthesisCallback = mSynthesisCallback; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1043 | } |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 1044 | if (synthesisCallback != null) { |
| 1045 | // If the synthesis callback is null, it implies that we haven't |
| 1046 | // entered the synchronized(this) block in playImpl which in |
| 1047 | // turn implies that synthesis would not have started. |
| 1048 | synthesisCallback.stop(); |
| 1049 | TextToSpeechService.this.onStop(); |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 1050 | } else { |
| 1051 | dispatchOnStop(); |
Narayan Kamath | a65c62a | 2011-09-06 17:04:48 +0100 | [diff] [blame] | 1052 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1055 | private String getCountry() { |
Narayan Kamath | e5b8c4d | 2011-08-22 15:37:47 +0100 | [diff] [blame] | 1056 | if (!hasLanguage()) return mDefaultLocale[1]; |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1057 | return getStringParam(mParams, Engine.KEY_PARAM_COUNTRY, ""); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | private String getVariant() { |
Narayan Kamath | e5b8c4d | 2011-08-22 15:37:47 +0100 | [diff] [blame] | 1061 | if (!hasLanguage()) return mDefaultLocale[2]; |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1062 | return getStringParam(mParams, Engine.KEY_PARAM_VARIANT, ""); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1065 | public String getLanguage() { |
| 1066 | return getStringParam(mParams, Engine.KEY_PARAM_LANGUAGE, mDefaultLocale[0]); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1067 | } |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 1068 | |
| 1069 | public String getVoiceName() { |
| 1070 | return getStringParam(mParams, Engine.KEY_PARAM_VOICE_NAME, ""); |
| 1071 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1074 | private class SynthesisToFileOutputStreamSpeechItem extends SynthesisSpeechItem { |
Przemyslaw Szczepaniak | fcf671b | 2013-03-05 11:28:50 +0000 | [diff] [blame] | 1075 | private final FileOutputStream mFileOutputStream; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1076 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1077 | public SynthesisToFileOutputStreamSpeechItem( |
| 1078 | Object callerIdentity, |
| 1079 | int callerUid, |
| 1080 | int callerPid, |
| 1081 | Bundle params, |
| 1082 | String utteranceId, |
| 1083 | CharSequence text, |
Niels Egberts | df7deef | 2014-06-25 15:30:44 +0100 | [diff] [blame] | 1084 | FileOutputStream fileOutputStream) { |
| 1085 | super(callerIdentity, callerUid, callerPid, params, utteranceId, text); |
Przemyslaw Szczepaniak | fcf671b | 2013-03-05 11:28:50 +0000 | [diff] [blame] | 1086 | mFileOutputStream = fileOutputStream; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | @Override |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 1090 | protected AbstractSynthesisCallback createSynthesisCallback() { |
Niels Egberts | 453c13f | 2015-11-20 12:05:55 +0000 | [diff] [blame] | 1091 | return new FileSynthesisCallback(mFileOutputStream.getChannel(), this, false); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 1094 | @Override |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1095 | protected void playImpl() { |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1096 | dispatchOnStart(); |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1097 | super.playImpl(); |
Przemyslaw Szczepaniak | fcf671b | 2013-03-05 11:28:50 +0000 | [diff] [blame] | 1098 | try { |
| 1099 | mFileOutputStream.close(); |
| 1100 | } catch(IOException e) { |
| 1101 | Log.w(TAG, "Failed to close output file", e); |
| 1102 | } |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 1103 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1106 | private class AudioSpeechItem extends UtteranceSpeechItemWithParams { |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 1107 | private final AudioPlaybackQueueItem mItem; |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1108 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1109 | public AudioSpeechItem( |
| 1110 | Object callerIdentity, |
| 1111 | int callerUid, |
| 1112 | int callerPid, |
| 1113 | Bundle params, |
| 1114 | String utteranceId, |
| 1115 | Uri uri) { |
Niels Egberts | df7deef | 2014-06-25 15:30:44 +0100 | [diff] [blame] | 1116 | super(callerIdentity, callerUid, callerPid, params, utteranceId); |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 1117 | mItem = new AudioPlaybackQueueItem(this, getCallerIdentity(), |
Przemyslaw Szczepaniak | 5cbf17c | 2014-06-18 11:35:52 +0100 | [diff] [blame] | 1118 | TextToSpeechService.this, uri, getAudioParams()); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | @Override |
| 1122 | public boolean isValid() { |
| 1123 | return true; |
| 1124 | } |
| 1125 | |
| 1126 | @Override |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1127 | protected void playImpl() { |
Narayan Kamath | af802c6 | 2011-12-05 11:20:07 +0000 | [diff] [blame] | 1128 | mAudioPlaybackHandler.enqueue(mItem); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | @Override |
| 1132 | protected void stopImpl() { |
Narayan Kamath | be4ad4a | 2011-07-15 13:01:09 +0100 | [diff] [blame] | 1133 | // Do nothing. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1134 | } |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1135 | |
| 1136 | @Override |
| 1137 | public String getUtteranceId() { |
| 1138 | return getStringParam(mParams, Engine.KEY_PARAM_UTTERANCE_ID, null); |
| 1139 | } |
Przemyslaw Szczepaniak | 672695e | 2014-07-14 17:10:42 +0100 | [diff] [blame] | 1140 | |
| 1141 | @Override |
| 1142 | AudioOutputParams getAudioParams() { |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1143 | return AudioOutputParams.createFromParamsBundle(mParams, false); |
Przemyslaw Szczepaniak | 672695e | 2014-07-14 17:10:42 +0100 | [diff] [blame] | 1144 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 1147 | private class SilenceSpeechItem extends UtteranceSpeechItem { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1148 | private final long mDuration; |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1149 | private final String mUtteranceId; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1150 | |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1151 | public SilenceSpeechItem(Object callerIdentity, int callerUid, int callerPid, |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1152 | String utteranceId, long duration) { |
| 1153 | super(callerIdentity, callerUid, callerPid); |
| 1154 | mUtteranceId = utteranceId; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1155 | mDuration = duration; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | @Override |
| 1159 | public boolean isValid() { |
| 1160 | return true; |
| 1161 | } |
| 1162 | |
| 1163 | @Override |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1164 | protected void playImpl() { |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 1165 | mAudioPlaybackHandler.enqueue(new SilencePlaybackQueueItem( |
| 1166 | this, getCallerIdentity(), mDuration)); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | @Override |
| 1170 | protected void stopImpl() { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1171 | |
| 1172 | } |
| 1173 | |
| 1174 | @Override |
| 1175 | public String getUtteranceId() { |
| 1176 | return mUtteranceId; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1180 | /** |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1181 | * Call {@link TextToSpeechService#onLoadLanguage} on synth thread. |
| 1182 | */ |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 1183 | private class LoadLanguageItem extends SpeechItem { |
| 1184 | private final String mLanguage; |
| 1185 | private final String mCountry; |
| 1186 | private final String mVariant; |
| 1187 | |
| 1188 | public LoadLanguageItem(Object callerIdentity, int callerUid, int callerPid, |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1189 | String language, String country, String variant) { |
| 1190 | super(callerIdentity, callerUid, callerPid); |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 1191 | mLanguage = language; |
| 1192 | mCountry = country; |
| 1193 | mVariant = variant; |
| 1194 | } |
| 1195 | |
| 1196 | @Override |
| 1197 | public boolean isValid() { |
| 1198 | return true; |
| 1199 | } |
| 1200 | |
| 1201 | @Override |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1202 | protected void playImpl() { |
| 1203 | TextToSpeechService.this.onLoadLanguage(mLanguage, mCountry, mVariant); |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | @Override |
| 1207 | protected void stopImpl() { |
| 1208 | // No-op |
| 1209 | } |
| 1210 | } |
| 1211 | |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 1212 | /** |
| 1213 | * Call {@link TextToSpeechService#onLoadLanguage} on synth thread. |
| 1214 | */ |
| 1215 | private class LoadVoiceItem extends SpeechItem { |
| 1216 | private final String mVoiceName; |
| 1217 | |
| 1218 | public LoadVoiceItem(Object callerIdentity, int callerUid, int callerPid, |
| 1219 | String voiceName) { |
| 1220 | super(callerIdentity, callerUid, callerPid); |
| 1221 | mVoiceName = voiceName; |
| 1222 | } |
| 1223 | |
| 1224 | @Override |
| 1225 | public boolean isValid() { |
| 1226 | return true; |
| 1227 | } |
| 1228 | |
| 1229 | @Override |
| 1230 | protected void playImpl() { |
| 1231 | TextToSpeechService.this.onLoadVoice(mVoiceName); |
| 1232 | } |
| 1233 | |
| 1234 | @Override |
| 1235 | protected void stopImpl() { |
| 1236 | // No-op |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1241 | @Override |
| 1242 | public IBinder onBind(Intent intent) { |
| 1243 | if (TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE.equals(intent.getAction())) { |
| 1244 | return mBinder; |
| 1245 | } |
| 1246 | return null; |
| 1247 | } |
| 1248 | |
| 1249 | /** |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1250 | * Binder returned from {@code #onBind(Intent)}. The methods in this class can be called called |
| 1251 | * from several different threads. |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1252 | */ |
Narayan Kamath | abc63fb | 2011-06-10 11:36:57 +0100 | [diff] [blame] | 1253 | // NOTE: All calls that are passed in a calling app are interned so that |
| 1254 | // they can be used as message objects (which are tested for equality using ==). |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1255 | private final ITextToSpeechService.Stub mBinder = |
| 1256 | new ITextToSpeechService.Stub() { |
| 1257 | @Override |
| 1258 | public int speak( |
| 1259 | IBinder caller, |
| 1260 | CharSequence text, |
| 1261 | int queueMode, |
| 1262 | Bundle params, |
| 1263 | String utteranceId) { |
| 1264 | if (!checkNonNull(caller, text, params)) { |
| 1265 | return TextToSpeech.ERROR; |
| 1266 | } |
Narayan Kamath | abc63fb | 2011-06-10 11:36:57 +0100 | [diff] [blame] | 1267 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1268 | SpeechItem item = |
| 1269 | new SynthesisSpeechItem( |
| 1270 | caller, |
| 1271 | Binder.getCallingUid(), |
| 1272 | Binder.getCallingPid(), |
| 1273 | params, |
| 1274 | utteranceId, |
| 1275 | text); |
| 1276 | return mSynthHandler.enqueueSpeechItem(queueMode, item); |
Przemyslaw Szczepaniak | 13896b7 | 2012-11-09 15:18:16 +0000 | [diff] [blame] | 1277 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1278 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1279 | @Override |
| 1280 | public int synthesizeToFileDescriptor( |
| 1281 | IBinder caller, |
| 1282 | CharSequence text, |
| 1283 | ParcelFileDescriptor fileDescriptor, |
| 1284 | Bundle params, |
| 1285 | String utteranceId) { |
| 1286 | if (!checkNonNull(caller, text, fileDescriptor, params)) { |
| 1287 | return TextToSpeech.ERROR; |
| 1288 | } |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 1289 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1290 | // In test env, ParcelFileDescriptor instance may be EXACTLY the same |
| 1291 | // one that is used by client. And it will be closed by a client, thus |
| 1292 | // preventing us from writing anything to it. |
| 1293 | final ParcelFileDescriptor sameFileDescriptor = |
| 1294 | ParcelFileDescriptor.adoptFd(fileDescriptor.detachFd()); |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 1295 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1296 | SpeechItem item = |
| 1297 | new SynthesisToFileOutputStreamSpeechItem( |
| 1298 | caller, |
| 1299 | Binder.getCallingUid(), |
| 1300 | Binder.getCallingPid(), |
| 1301 | params, |
| 1302 | utteranceId, |
| 1303 | text, |
| 1304 | new ParcelFileDescriptor.AutoCloseOutputStream( |
| 1305 | sameFileDescriptor)); |
| 1306 | return mSynthHandler.enqueueSpeechItem(TextToSpeech.QUEUE_ADD, item); |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 1307 | } |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 1308 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1309 | @Override |
| 1310 | public int playAudio( |
| 1311 | IBinder caller, |
| 1312 | Uri audioUri, |
| 1313 | int queueMode, |
| 1314 | Bundle params, |
| 1315 | String utteranceId) { |
| 1316 | if (!checkNonNull(caller, audioUri, params)) { |
| 1317 | return TextToSpeech.ERROR; |
| 1318 | } |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 1319 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1320 | SpeechItem item = |
| 1321 | new AudioSpeechItem( |
| 1322 | caller, |
| 1323 | Binder.getCallingUid(), |
| 1324 | Binder.getCallingPid(), |
| 1325 | params, |
| 1326 | utteranceId, |
| 1327 | audioUri); |
| 1328 | return mSynthHandler.enqueueSpeechItem(queueMode, item); |
| 1329 | } |
Przemyslaw Szczepaniak | ad6df74 | 2014-07-01 17:04:25 +0100 | [diff] [blame] | 1330 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1331 | @Override |
| 1332 | public int playSilence( |
| 1333 | IBinder caller, long duration, int queueMode, String utteranceId) { |
| 1334 | if (!checkNonNull(caller)) { |
| 1335 | return TextToSpeech.ERROR; |
| 1336 | } |
Narayan Kamath | abc63fb | 2011-06-10 11:36:57 +0100 | [diff] [blame] | 1337 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1338 | SpeechItem item = |
| 1339 | new SilenceSpeechItem( |
| 1340 | caller, |
| 1341 | Binder.getCallingUid(), |
| 1342 | Binder.getCallingPid(), |
| 1343 | utteranceId, |
| 1344 | duration); |
| 1345 | return mSynthHandler.enqueueSpeechItem(queueMode, item); |
| 1346 | } |
Narayan Kamath | 7a3af86 | 2011-06-02 17:28:57 +0100 | [diff] [blame] | 1347 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1348 | @Override |
| 1349 | public boolean isSpeaking() { |
| 1350 | return mSynthHandler.isSpeaking() || mAudioPlaybackHandler.isSpeaking(); |
| 1351 | } |
Narayan Kamath | abc63fb | 2011-06-10 11:36:57 +0100 | [diff] [blame] | 1352 | |
Niels Egberts | 01dedf5 | 2016-12-23 15:32:11 +0000 | [diff] [blame] | 1353 | @Override |
| 1354 | public int stop(IBinder caller) { |
| 1355 | if (!checkNonNull(caller)) { |
| 1356 | return TextToSpeech.ERROR; |
| 1357 | } |
| 1358 | |
| 1359 | return mSynthHandler.stopForApp(caller); |
| 1360 | } |
| 1361 | |
| 1362 | @Override |
| 1363 | public String[] getLanguage() { |
| 1364 | return onGetLanguage(); |
| 1365 | } |
| 1366 | |
| 1367 | @Override |
| 1368 | public String[] getClientDefaultLanguage() { |
| 1369 | return getSettingsLocale(); |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | * If defaults are enforced, then no language is "available" except |
| 1374 | * perhaps the default language selected by the user. |
| 1375 | */ |
| 1376 | @Override |
| 1377 | public int isLanguageAvailable(String lang, String country, String variant) { |
| 1378 | if (!checkNonNull(lang)) { |
| 1379 | return TextToSpeech.ERROR; |
| 1380 | } |
| 1381 | |
| 1382 | return onIsLanguageAvailable(lang, country, variant); |
| 1383 | } |
| 1384 | |
| 1385 | @Override |
| 1386 | public String[] getFeaturesForLanguage( |
| 1387 | String lang, String country, String variant) { |
| 1388 | Set<String> features = onGetFeaturesForLanguage(lang, country, variant); |
| 1389 | String[] featuresArray = null; |
| 1390 | if (features != null) { |
| 1391 | featuresArray = new String[features.size()]; |
| 1392 | features.toArray(featuresArray); |
| 1393 | } else { |
| 1394 | featuresArray = new String[0]; |
| 1395 | } |
| 1396 | return featuresArray; |
| 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | * There is no point loading a non default language if defaults |
| 1401 | * are enforced. |
| 1402 | */ |
| 1403 | @Override |
| 1404 | public int loadLanguage( |
| 1405 | IBinder caller, String lang, String country, String variant) { |
| 1406 | if (!checkNonNull(lang)) { |
| 1407 | return TextToSpeech.ERROR; |
| 1408 | } |
| 1409 | int retVal = onIsLanguageAvailable(lang, country, variant); |
| 1410 | |
| 1411 | if (retVal == TextToSpeech.LANG_AVAILABLE |
| 1412 | || retVal == TextToSpeech.LANG_COUNTRY_AVAILABLE |
| 1413 | || retVal == TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE) { |
| 1414 | |
| 1415 | SpeechItem item = |
| 1416 | new LoadLanguageItem( |
| 1417 | caller, |
| 1418 | Binder.getCallingUid(), |
| 1419 | Binder.getCallingPid(), |
| 1420 | lang, |
| 1421 | country, |
| 1422 | variant); |
| 1423 | |
| 1424 | if (mSynthHandler.enqueueSpeechItem(TextToSpeech.QUEUE_ADD, item) |
| 1425 | != TextToSpeech.SUCCESS) { |
| 1426 | return TextToSpeech.ERROR; |
| 1427 | } |
| 1428 | } |
| 1429 | return retVal; |
| 1430 | } |
| 1431 | |
| 1432 | @Override |
| 1433 | public List<Voice> getVoices() { |
| 1434 | return onGetVoices(); |
| 1435 | } |
| 1436 | |
| 1437 | @Override |
| 1438 | public int loadVoice(IBinder caller, String voiceName) { |
| 1439 | if (!checkNonNull(voiceName)) { |
| 1440 | return TextToSpeech.ERROR; |
| 1441 | } |
| 1442 | int retVal = onIsValidVoiceName(voiceName); |
| 1443 | |
| 1444 | if (retVal == TextToSpeech.SUCCESS) { |
| 1445 | SpeechItem item = |
| 1446 | new LoadVoiceItem( |
| 1447 | caller, |
| 1448 | Binder.getCallingUid(), |
| 1449 | Binder.getCallingPid(), |
| 1450 | voiceName); |
| 1451 | if (mSynthHandler.enqueueSpeechItem(TextToSpeech.QUEUE_ADD, item) |
| 1452 | != TextToSpeech.SUCCESS) { |
| 1453 | return TextToSpeech.ERROR; |
| 1454 | } |
| 1455 | } |
| 1456 | return retVal; |
| 1457 | } |
| 1458 | |
| 1459 | public String getDefaultVoiceNameFor(String lang, String country, String variant) { |
| 1460 | if (!checkNonNull(lang)) { |
| 1461 | return null; |
| 1462 | } |
| 1463 | int retVal = onIsLanguageAvailable(lang, country, variant); |
| 1464 | |
| 1465 | if (retVal == TextToSpeech.LANG_AVAILABLE |
| 1466 | || retVal == TextToSpeech.LANG_COUNTRY_AVAILABLE |
| 1467 | || retVal == TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE) { |
| 1468 | return onGetDefaultVoiceNameFor(lang, country, variant); |
| 1469 | } else { |
| 1470 | return null; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | @Override |
| 1475 | public void setCallback(IBinder caller, ITextToSpeechCallback cb) { |
| 1476 | // Note that passing in a null callback is a valid use case. |
| 1477 | if (!checkNonNull(caller)) { |
| 1478 | return; |
| 1479 | } |
| 1480 | |
| 1481 | mCallbacks.setCallback(caller, cb); |
| 1482 | } |
| 1483 | |
| 1484 | private String intern(String in) { |
| 1485 | // The input parameter will be non null. |
| 1486 | return in.intern(); |
| 1487 | } |
| 1488 | |
| 1489 | private boolean checkNonNull(Object... args) { |
| 1490 | for (Object o : args) { |
| 1491 | if (o == null) return false; |
| 1492 | } |
| 1493 | return true; |
| 1494 | } |
| 1495 | }; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1496 | |
| 1497 | private class CallbackMap extends RemoteCallbackList<ITextToSpeechCallback> { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1498 | private final HashMap<IBinder, ITextToSpeechCallback> mCallerToCallback |
| 1499 | = new HashMap<IBinder, ITextToSpeechCallback>(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1500 | |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1501 | public void setCallback(IBinder caller, ITextToSpeechCallback cb) { |
| 1502 | synchronized (mCallerToCallback) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1503 | ITextToSpeechCallback old; |
| 1504 | if (cb != null) { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1505 | register(cb, caller); |
| 1506 | old = mCallerToCallback.put(caller, cb); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1507 | } else { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1508 | old = mCallerToCallback.remove(caller); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1509 | } |
| 1510 | if (old != null && old != cb) { |
| 1511 | unregister(old); |
| 1512 | } |
| 1513 | } |
| 1514 | } |
| 1515 | |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 1516 | public void dispatchOnStop(Object callerIdentity, String utteranceId, boolean started) { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1517 | ITextToSpeechCallback cb = getCallbackFor(callerIdentity); |
| 1518 | if (cb == null) return; |
| 1519 | try { |
Przemyslaw Szczepaniak | 4b73867 | 2014-11-19 17:36:57 +0000 | [diff] [blame] | 1520 | cb.onStop(utteranceId, started); |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1521 | } catch (RemoteException e) { |
| 1522 | Log.e(TAG, "Callback onStop failed: " + e); |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | public void dispatchOnSuccess(Object callerIdentity, String utteranceId) { |
| 1527 | ITextToSpeechCallback cb = getCallbackFor(callerIdentity); |
| 1528 | if (cb == null) return; |
| 1529 | try { |
| 1530 | cb.onSuccess(utteranceId); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1531 | } catch (RemoteException e) { |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1532 | Log.e(TAG, "Callback onDone failed: " + e); |
| 1533 | } |
| 1534 | } |
| 1535 | |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1536 | public void dispatchOnStart(Object callerIdentity, String utteranceId) { |
| 1537 | ITextToSpeechCallback cb = getCallbackFor(callerIdentity); |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1538 | if (cb == null) return; |
| 1539 | try { |
| 1540 | cb.onStart(utteranceId); |
| 1541 | } catch (RemoteException e) { |
| 1542 | Log.e(TAG, "Callback onStart failed: " + e); |
| 1543 | } |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1546 | public void dispatchOnError(Object callerIdentity, String utteranceId, |
| 1547 | int errorCode) { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1548 | ITextToSpeechCallback cb = getCallbackFor(callerIdentity); |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1549 | if (cb == null) return; |
| 1550 | try { |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1551 | cb.onError(utteranceId, errorCode); |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1552 | } catch (RemoteException e) { |
| 1553 | Log.e(TAG, "Callback onError failed: " + e); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | |
Niels Egberts | c99ba1c | 2015-11-20 11:44:25 +0000 | [diff] [blame] | 1557 | public void dispatchOnBeginSynthesis(Object callerIdentity, String utteranceId, int sampleRateInHz, int audioFormat, int channelCount) { |
| 1558 | ITextToSpeechCallback cb = getCallbackFor(callerIdentity); |
| 1559 | if (cb == null) return; |
| 1560 | try { |
| 1561 | cb.onBeginSynthesis(utteranceId, sampleRateInHz, audioFormat, channelCount); |
| 1562 | } catch (RemoteException e) { |
| 1563 | Log.e(TAG, "Callback dispatchOnBeginSynthesis(String, int, int, int) failed: " + e); |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | public void dispatchOnAudioAvailable(Object callerIdentity, String utteranceId, byte[] buffer) { |
| 1568 | ITextToSpeechCallback cb = getCallbackFor(callerIdentity); |
| 1569 | if (cb == null) return; |
| 1570 | try { |
| 1571 | cb.onAudioAvailable(utteranceId, buffer); |
| 1572 | } catch (RemoteException e) { |
| 1573 | Log.e(TAG, "Callback dispatchOnAudioAvailable(String, byte[]) failed: " + e); |
| 1574 | } |
| 1575 | } |
| 1576 | |
Niels Egberts | 5d0ea0f | 2017-03-21 15:29:24 +0000 | [diff] [blame] | 1577 | public void dispatchOnRangeStart( |
| 1578 | Object callerIdentity, String utteranceId, int start, int end, int frame) { |
Niels Egberts | 65c5078 | 2016-12-23 12:01:32 +0000 | [diff] [blame] | 1579 | ITextToSpeechCallback cb = getCallbackFor(callerIdentity); |
| 1580 | if (cb == null) return; |
| 1581 | try { |
Niels Egberts | 5d0ea0f | 2017-03-21 15:29:24 +0000 | [diff] [blame] | 1582 | cb.onRangeStart(utteranceId, start, end, frame); |
Niels Egberts | 65c5078 | 2016-12-23 12:01:32 +0000 | [diff] [blame] | 1583 | } catch (RemoteException e) { |
Niels Egberts | 5d0ea0f | 2017-03-21 15:29:24 +0000 | [diff] [blame] | 1584 | Log.e(TAG, "Callback dispatchOnRangeStart(String, int, int, int) failed: " + e); |
Niels Egberts | 65c5078 | 2016-12-23 12:01:32 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1588 | @Override |
| 1589 | public void onCallbackDied(ITextToSpeechCallback callback, Object cookie) { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1590 | IBinder caller = (IBinder) cookie; |
| 1591 | synchronized (mCallerToCallback) { |
| 1592 | mCallerToCallback.remove(caller); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1593 | } |
Przemyslaw Szczepaniak | 90d15d2 | 2013-06-14 12:02:53 +0100 | [diff] [blame] | 1594 | //mSynthHandler.stopForApp(caller); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | @Override |
| 1598 | public void kill() { |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1599 | synchronized (mCallerToCallback) { |
| 1600 | mCallerToCallback.clear(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1601 | super.kill(); |
| 1602 | } |
| 1603 | } |
| 1604 | |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1605 | private ITextToSpeechCallback getCallbackFor(Object caller) { |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1606 | ITextToSpeechCallback cb; |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 1607 | IBinder asBinder = (IBinder) caller; |
| 1608 | synchronized (mCallerToCallback) { |
| 1609 | cb = mCallerToCallback.get(asBinder); |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | return cb; |
| 1613 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1614 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 1615 | } |