| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.speech.tts; |
| 17 |
|
| 18 |
import android.media.AudioTrack; |
| 19 |
import android.speech.tts.TextToSpeechService.AudioOutputParams; |
| 20 |
import android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher; |
| 21 |
import android.util.Log; |
| 22 |
|
| 23 |
import java.util.LinkedList; |
| 24 |
import java.util.concurrent.ConcurrentLinkedQueue; |
| 25 |
import java.util.concurrent.atomic.AtomicInteger; |
| 26 |
import java.util.concurrent.locks.Condition; |
| 27 |
import java.util.concurrent.locks.Lock; |
| 28 |
import java.util.concurrent.locks.ReentrantLock; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
final class SynthesisPlaybackQueueItem extends PlaybackQueueItem |
| 35 |
implements AudioTrack.OnPlaybackPositionUpdateListener { |
| 36 |
private static final String TAG = "TTS.SynthQueueItem"; |
| 37 |
private static final boolean DBG = false; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private static final long MAX_UNCONSUMED_AUDIO_MS = 500; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private final Lock mListLock = new ReentrantLock(); |
| 50 |
private final Condition mReadReady = mListLock.newCondition(); |
| 51 |
private final Condition mNotFull = mListLock.newCondition(); |
| 52 |
|
| 53 |
|
| 54 |
private final LinkedList<ListEntry> mDataBufferList = new LinkedList<ListEntry>(); |
| 55 |
|
| 56 |
private int mUnconsumedBytes; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private volatile boolean mStopped; |
| 64 |
private volatile boolean mDone; |
| 65 |
private volatile int mStatusCode; |
| 66 |
|
| 67 |
private final BlockingAudioTrack mAudioTrack; |
| 68 |
private final AbstractEventLogger mLogger; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private ConcurrentLinkedQueue<ProgressMarker> markerList = new ConcurrentLinkedQueue<>(); |
| 73 |
|
| 74 |
private static final int NOT_RUN = 0; |
| 75 |
private static final int RUN_CALLED = 1; |
| 76 |
private static final int STOP_CALLED = 2; |
| 77 |
private final AtomicInteger mRunState = new AtomicInteger(NOT_RUN); |
| 78 |
|
| 79 |
SynthesisPlaybackQueueItem(AudioOutputParams audioParams, int sampleRate, |
| 80 |
int audioFormat, int channelCount, UtteranceProgressDispatcher dispatcher, |
| 81 |
Object callerIdentity, AbstractEventLogger logger) { |
| 82 |
super(dispatcher, callerIdentity); |
| 83 |
|
| 84 |
mUnconsumedBytes = 0; |
| 85 |
|
| 86 |
mStopped = false; |
| 87 |
mDone = false; |
| 88 |
mStatusCode = TextToSpeech.SUCCESS; |
| 89 |
|
| 90 |
mAudioTrack = new BlockingAudioTrack(audioParams, sampleRate, audioFormat, channelCount); |
| 91 |
mLogger = logger; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
@Override |
| 96 |
public void run() { |
| 97 |
if (!mRunState.compareAndSet(NOT_RUN, RUN_CALLED)) { |
| 98 |
|
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
final UtteranceProgressDispatcher dispatcher = getDispatcher(); |
| 103 |
dispatcher.dispatchOnStart(); |
| 104 |
|
| 105 |
if (!mAudioTrack.init()) { |
| 106 |
dispatcher.dispatchOnError(TextToSpeech.ERROR_OUTPUT); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
mAudioTrack.setPlaybackPositionUpdateListener(this); |
| 111 |
|
| 112 |
updateMarker(); |
| 113 |
|
| 114 |
try { |
| 115 |
byte[] buffer = null; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
while ((buffer = take()) != null) { |
| 124 |
mAudioTrack.write(buffer); |
| 125 |
mLogger.onAudioDataWritten(); |
| 126 |
} |
| 127 |
|
| 128 |
} catch (InterruptedException ie) { |
| 129 |
if (DBG) Log.d(TAG, "Interrupted waiting for buffers, cleaning up."); |
| 130 |
} |
| 131 |
|
| 132 |
mAudioTrack.waitAndRelease(); |
| 133 |
|
| 134 |
dispatchEndStatus(); |
| 135 |
} |
| 136 |
|
| 137 |
private void dispatchEndStatus() { |
| 138 |
final UtteranceProgressDispatcher dispatcher = getDispatcher(); |
| 139 |
|
| 140 |
if (mStatusCode == TextToSpeech.SUCCESS) { |
| 141 |
dispatcher.dispatchOnSuccess(); |
| 142 |
} else if(mStatusCode == TextToSpeech.STOPPED) { |
| 143 |
dispatcher.dispatchOnStop(); |
| 144 |
} else { |
| 145 |
dispatcher.dispatchOnError(mStatusCode); |
| 146 |
} |
| 147 |
|
| 148 |
mLogger.onCompleted(mStatusCode); |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
void stop(int statusCode) { |
| 153 |
try { |
| 154 |
mListLock.lock(); |
| 155 |
|
| 156 |
|
| 157 |
mStopped = true; |
| 158 |
mStatusCode = statusCode; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
mNotFull.signal(); |
| 166 |
|
| 167 |
if (mRunState.getAndSet(STOP_CALLED) == NOT_RUN) { |
| 168 |
|
| 169 |
|
| 170 |
dispatchEndStatus(); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
mReadReady.signal(); |
| 178 |
} finally { |
| 179 |
mListLock.unlock(); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
mAudioTrack.stop(); |
| 186 |
} |
| 187 |
|
| 188 |
void done() { |
| 189 |
try { |
| 190 |
mListLock.lock(); |
| 191 |
|
| 192 |
|
| 193 |
mDone = true; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
mReadReady.signal(); |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
mNotFull.signal(); |
| 205 |
} finally { |
| 206 |
mListLock.unlock(); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
private class ProgressMarker { |
| 212 |
|
| 213 |
public final int frames; |
| 214 |
|
| 215 |
public final int start; |
| 216 |
|
| 217 |
public final int end; |
| 218 |
|
| 219 |
public ProgressMarker(int frames, int start, int end) { |
| 220 |
this.frames = frames; |
| 221 |
this.start = start; |
| 222 |
this.end = end; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
void updateMarker() { |
| 228 |
ProgressMarker marker = markerList.peek(); |
| 229 |
if (marker != null) { |
| 230 |
|
| 231 |
|
| 232 |
int markerInFrames = marker.frames == 0 ? 1 : marker.frames; |
| 233 |
mAudioTrack.setNotificationMarkerPosition(markerInFrames); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
void rangeStart(int markerInFrames, int start, int end) { |
| 239 |
markerList.add(new ProgressMarker(markerInFrames, start, end)); |
| 240 |
updateMarker(); |
| 241 |
} |
| 242 |
|
| 243 |
@Override |
| 244 |
public void onMarkerReached(AudioTrack track) { |
| 245 |
ProgressMarker marker = markerList.poll(); |
| 246 |
if (marker == null) { |
| 247 |
Log.e(TAG, "onMarkerReached reached called but no marker in queue"); |
| 248 |
return; |
| 249 |
} |
| 250 |
|
| 251 |
getDispatcher().dispatchOnRangeStart(marker.start, marker.end, marker.frames); |
| 252 |
|
| 253 |
|
| 254 |
updateMarker(); |
| 255 |
} |
| 256 |
|
| 257 |
@Override |
| 258 |
public void onPeriodicNotification(AudioTrack track) {} |
| 259 |
|
| 260 |
void put(byte[] buffer) throws InterruptedException { |
| 261 |
try { |
| 262 |
mListLock.lock(); |
| 263 |
long unconsumedAudioMs = 0; |
| 264 |
|
| 265 |
while ((unconsumedAudioMs = mAudioTrack.getAudioLengthMs(mUnconsumedBytes)) > |
| 266 |
MAX_UNCONSUMED_AUDIO_MS && !mStopped) { |
| 267 |
mNotFull.await(); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
if (mStopped) { |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
mDataBufferList.add(new ListEntry(buffer)); |
| 279 |
mUnconsumedBytes += buffer.length; |
| 280 |
mReadReady.signal(); |
| 281 |
} finally { |
| 282 |
mListLock.unlock(); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
private byte[] take() throws InterruptedException { |
| 287 |
try { |
| 288 |
mListLock.lock(); |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
while (mDataBufferList.size() == 0 && !mStopped && !mDone) { |
| 293 |
mReadReady.await(); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
if (mStopped) { |
| 299 |
return null; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
ListEntry entry = mDataBufferList.poll(); |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
if (entry == null) { |
| 308 |
return null; |
| 309 |
} |
| 310 |
|
| 311 |
mUnconsumedBytes -= entry.mBytes.length; |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
mNotFull.signal(); |
| 316 |
|
| 317 |
return entry.mBytes; |
| 318 |
} finally { |
| 319 |
mListLock.unlock(); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
static final class ListEntry { |
| 324 |
final byte[] mBytes; |
| 325 |
|
| 326 |
ListEntry(byte[] bytes) { |
| 327 |
mBytes = bytes; |
| 328 |
} |
| 329 |
} |
| 330 |
} |