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 | |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 18 | import android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 19 | import android.util.Log; |
| 20 | |
| 21 | /** |
| 22 | * Speech synthesis request that plays the audio as it is received. |
| 23 | */ |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 24 | class PlaybackSynthesisCallback extends AbstractSynthesisCallback { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 25 | |
| 26 | private static final String TAG = "PlaybackSynthesisRequest"; |
| 27 | private static final boolean DBG = false; |
| 28 | |
| 29 | private static final int MIN_AUDIO_BUFFER_SIZE = 8192; |
| 30 | |
| 31 | /** |
| 32 | * Audio stream type. Must be one of the STREAM_ contants defined in |
| 33 | * {@link android.media.AudioManager}. |
| 34 | */ |
| 35 | private final int mStreamType; |
| 36 | |
| 37 | /** |
| 38 | * Volume, in the range [0.0f, 1.0f]. The default value is |
| 39 | * {@link TextToSpeech.Engine#DEFAULT_VOLUME} (1.0f). |
| 40 | */ |
| 41 | private final float mVolume; |
| 42 | |
| 43 | /** |
| 44 | * Left/right position of the audio, in the range [-1.0f, 1.0f]. |
| 45 | * The default value is {@link TextToSpeech.Engine#DEFAULT_PAN} (0.0f). |
| 46 | */ |
| 47 | private final float mPan; |
| 48 | |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 49 | /** |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 50 | * Guards {@link #mAudioTrackHandler}, {@link #mItem} and {@link #mStopped}. |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 51 | */ |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 52 | private final Object mStateLock = new Object(); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 53 | |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 54 | // Handler associated with a thread that plays back audio requests. |
| 55 | private final AudioPlaybackHandler mAudioTrackHandler; |
Narayan Kamath | c3da881 | 2011-07-01 10:13:54 +0100 | [diff] [blame] | 56 | // A request "token", which will be non null after start() has been called. |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 57 | private SynthesisPlaybackQueueItem mItem = null; |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 58 | // Whether this request has been stopped. This is useful for keeping |
| 59 | // track whether stop() has been called before start(). In all other cases, |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 60 | // a non-null value of mItem will provide the same information. |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 61 | private boolean mStopped = false; |
| 62 | |
| 63 | private volatile boolean mDone = false; |
| 64 | |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 65 | private final UtteranceProgressDispatcher mDispatcher; |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 66 | private final Object mCallerIdentity; |
Narayan Kamath | 6dabb63 | 2011-07-08 12:13:03 +0100 | [diff] [blame] | 67 | private final EventLogger mLogger; |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 68 | |
Narayan Kamath | e22b69a | 2011-06-08 11:41:47 +0100 | [diff] [blame] | 69 | PlaybackSynthesisCallback(int streamType, float volume, float pan, |
Narayan Kamath | 754c72e | 2011-11-09 14:22:32 +0000 | [diff] [blame] | 70 | AudioPlaybackHandler audioTrackHandler, UtteranceProgressDispatcher dispatcher, |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 71 | Object callerIdentity, EventLogger logger) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 72 | mStreamType = streamType; |
| 73 | mVolume = volume; |
| 74 | mPan = pan; |
Narayan Kamath | c90f1c8 | 2011-05-24 11:39:43 +0100 | [diff] [blame] | 75 | mAudioTrackHandler = audioTrackHandler; |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 76 | mDispatcher = dispatcher; |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 77 | mCallerIdentity = callerIdentity; |
Narayan Kamath | 6dabb63 | 2011-07-08 12:13:03 +0100 | [diff] [blame] | 78 | mLogger = logger; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | void stop() { |
Narayan Kamath | 40f71f0 | 2011-11-23 16:42:53 +0000 | [diff] [blame] | 83 | stopImpl(false); |
| 84 | } |
| 85 | |
| 86 | void stopImpl(boolean wasError) { |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 87 | if (DBG) Log.d(TAG, "stop()"); |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 88 | |
Narayan Kamath | 6dabb63 | 2011-07-08 12:13:03 +0100 | [diff] [blame] | 89 | // Note that mLogger.mError might be true too at this point. |
| 90 | mLogger.onStopped(); |
| 91 | |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 92 | SynthesisPlaybackQueueItem item; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 93 | synchronized (mStateLock) { |
Narayan Kamath | 6dabb63 | 2011-07-08 12:13:03 +0100 | [diff] [blame] | 94 | if (mStopped) { |
| 95 | Log.w(TAG, "stop() called twice"); |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 96 | return; |
Narayan Kamath | c90f1c8 | 2011-05-24 11:39:43 +0100 | [diff] [blame] | 97 | } |
Narayan Kamath | be4ad4a | 2011-07-15 13:01:09 +0100 | [diff] [blame] | 98 | |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 99 | item = mItem; |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 100 | mStopped = true; |
| 101 | } |
Narayan Kamath | 2a0518c | 2011-08-26 11:43:03 +0100 | [diff] [blame] | 102 | |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 103 | if (item != null) { |
Narayan Kamath | 2a0518c | 2011-08-26 11:43:03 +0100 | [diff] [blame] | 104 | // This might result in the synthesis thread being woken up, at which |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 105 | // point it will write an additional buffer to the item - but we |
Narayan Kamath | 2a0518c | 2011-08-26 11:43:03 +0100 | [diff] [blame] | 106 | // won't worry about that because the audio playback queue will be cleared |
| 107 | // soon after (see SynthHandler#stop(String). |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 108 | item.stop(wasError); |
Narayan Kamath | 40f71f0 | 2011-11-23 16:42:53 +0000 | [diff] [blame] | 109 | } else { |
| 110 | // This happens when stop() or error() were called before start() was. |
| 111 | |
| 112 | // In all other cases, mAudioTrackHandler.stop() will |
| 113 | // result in onSynthesisDone being called, and we will |
| 114 | // write data there. |
| 115 | mLogger.onWriteData(); |
| 116 | |
| 117 | if (wasError) { |
| 118 | // We have to dispatch the error ourselves. |
| 119 | mDispatcher.dispatchOnError(); |
| 120 | } |
Narayan Kamath | 2a0518c | 2011-08-26 11:43:03 +0100 | [diff] [blame] | 121 | } |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Bjorn Bringert | 71e0b48 | 2011-04-15 14:37:05 +0100 | [diff] [blame] | 124 | @Override |
| 125 | public int getMaxBufferSize() { |
| 126 | // The AudioTrack buffer will be at least MIN_AUDIO_BUFFER_SIZE, so that should always be |
| 127 | // a safe buffer size to pass in. |
| 128 | return MIN_AUDIO_BUFFER_SIZE; |
| 129 | } |
| 130 | |
Bjorn Bringert | 360eb16 | 2011-04-19 09:20:35 +0100 | [diff] [blame] | 131 | @Override |
| 132 | boolean isDone() { |
| 133 | return mDone; |
| 134 | } |
| 135 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 136 | @Override |
| 137 | public int start(int sampleRateInHz, int audioFormat, int channelCount) { |
| 138 | if (DBG) { |
| 139 | Log.d(TAG, "start(" + sampleRateInHz + "," + audioFormat |
| 140 | + "," + channelCount + ")"); |
| 141 | } |
| 142 | |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 143 | int channelConfig = BlockingAudioTrack.getChannelConfig(channelCount); |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 144 | if (channelConfig == 0) { |
| 145 | Log.e(TAG, "Unsupported number of channels :" + channelCount); |
| 146 | return TextToSpeech.ERROR; |
| 147 | } |
| 148 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 149 | synchronized (mStateLock) { |
| 150 | if (mStopped) { |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 151 | if (DBG) Log.d(TAG, "stop() called before start(), returning."); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 152 | return TextToSpeech.ERROR; |
| 153 | } |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 154 | SynthesisPlaybackQueueItem item = new SynthesisPlaybackQueueItem( |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 155 | mStreamType, sampleRateInHz, audioFormat, channelCount, mVolume, mPan, |
Narayan Kamath | 492b7f0 | 2011-11-29 17:02:06 +0000 | [diff] [blame] | 156 | mDispatcher, mCallerIdentity, mLogger); |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 157 | mAudioTrackHandler.enqueue(item); |
| 158 | mItem = item; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | return TextToSpeech.SUCCESS; |
| 162 | } |
| 163 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 164 | |
| 165 | @Override |
| 166 | public int audioAvailable(byte[] buffer, int offset, int length) { |
| 167 | if (DBG) { |
| 168 | Log.d(TAG, "audioAvailable(byte[" + buffer.length + "]," |
Bjorn Bringert | 71e0b48 | 2011-04-15 14:37:05 +0100 | [diff] [blame] | 169 | + offset + "," + length + ")"); |
| 170 | } |
Narayan Kamath | c90f1c8 | 2011-05-24 11:39:43 +0100 | [diff] [blame] | 171 | if (length > getMaxBufferSize() || length <= 0) { |
| 172 | throw new IllegalArgumentException("buffer is too large or of zero length (" + |
| 173 | + length + " bytes)"); |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 174 | } |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 175 | |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 176 | SynthesisPlaybackQueueItem item = null; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 177 | synchronized (mStateLock) { |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 178 | if (mItem == null || mStopped) { |
Narayan Kamath | c90f1c8 | 2011-05-24 11:39:43 +0100 | [diff] [blame] | 179 | return TextToSpeech.ERROR; |
| 180 | } |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 181 | item = mItem; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 182 | } |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 183 | |
Narayan Kamath | 2a0518c | 2011-08-26 11:43:03 +0100 | [diff] [blame] | 184 | // Sigh, another copy. |
| 185 | final byte[] bufferCopy = new byte[length]; |
| 186 | System.arraycopy(buffer, offset, bufferCopy, 0, length); |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 187 | |
| 188 | // Might block on mItem.this, if there are too many buffers waiting to |
Narayan Kamath | 2a0518c | 2011-08-26 11:43:03 +0100 | [diff] [blame] | 189 | // be consumed. |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 190 | try { |
| 191 | item.put(bufferCopy); |
| 192 | } catch (InterruptedException ie) { |
| 193 | return TextToSpeech.ERROR; |
| 194 | } |
Narayan Kamath | 2a0518c | 2011-08-26 11:43:03 +0100 | [diff] [blame] | 195 | |
Narayan Kamath | 6dabb63 | 2011-07-08 12:13:03 +0100 | [diff] [blame] | 196 | mLogger.onEngineDataReceived(); |
| 197 | |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 198 | return TextToSpeech.SUCCESS; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public int done() { |
| 203 | if (DBG) Log.d(TAG, "done()"); |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 204 | |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 205 | SynthesisPlaybackQueueItem item = null; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 206 | synchronized (mStateLock) { |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 207 | if (mDone) { |
| 208 | Log.w(TAG, "Duplicate call to done()"); |
Narayan Kamath | c90f1c8 | 2011-05-24 11:39:43 +0100 | [diff] [blame] | 209 | return TextToSpeech.ERROR; |
| 210 | } |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 211 | |
Bjorn Bringert | 360eb16 | 2011-04-19 09:20:35 +0100 | [diff] [blame] | 212 | mDone = true; |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 213 | |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 214 | if (mItem == null) { |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 215 | return TextToSpeech.ERROR; |
| 216 | } |
| 217 | |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 218 | item = mItem; |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 219 | } |
Narayan Kamath | 2a0518c | 2011-08-26 11:43:03 +0100 | [diff] [blame] | 220 | |
Narayan Kamath | 67ae6bc | 2011-11-30 14:51:00 +0000 | [diff] [blame] | 221 | item.done(); |
Narayan Kamath | 2a0518c | 2011-08-26 11:43:03 +0100 | [diff] [blame] | 222 | mLogger.onEngineComplete(); |
| 223 | |
Bjorn Bringert | 50e657b | 2011-03-08 16:00:40 +0000 | [diff] [blame] | 224 | return TextToSpeech.SUCCESS; |
| 225 | } |
Bjorn Bringert | 71e0b48 | 2011-04-15 14:37:05 +0100 | [diff] [blame] | 226 | |
| 227 | @Override |
Bjorn Bringert | 360eb16 | 2011-04-19 09:20:35 +0100 | [diff] [blame] | 228 | public void error() { |
Narayan Kamath | 8d1fc24 | 2011-06-03 18:11:54 +0100 | [diff] [blame] | 229 | if (DBG) Log.d(TAG, "error() [will call stop]"); |
Narayan Kamath | 6dabb63 | 2011-07-08 12:13:03 +0100 | [diff] [blame] | 230 | // Currently, this call will not be logged if error( ) is called |
| 231 | // before start. |
| 232 | mLogger.onError(); |
Narayan Kamath | 40f71f0 | 2011-11-23 16:42:53 +0000 | [diff] [blame] | 233 | stopImpl(true); |
Bjorn Bringert | 360eb16 | 2011-04-19 09:20:35 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Narayan Kamath | 53f6f95 | 2011-04-19 16:39:20 +0100 | [diff] [blame] | 236 | } |