Loading core/java/android/speech/tts/FileSynthesisRequest.java +17 −0 Original line number Diff line number Diff line Loading @@ -45,6 +45,7 @@ class FileSynthesisRequest extends SynthesisRequest { private int mChannelCount; private RandomAccessFile mFile; private boolean mStopped = false; private boolean mDone = false; FileSynthesisRequest(String text, File fileName) { super(text); Loading Loading @@ -88,6 +89,11 @@ class FileSynthesisRequest extends SynthesisRequest { return MAX_AUDIO_BUFFER_SIZE; } @Override boolean isDone() { return mDone; } @Override public int start(int sampleRateInHz, int audioFormat, int channelCount) { if (DBG) { Loading Loading @@ -164,6 +170,7 @@ class FileSynthesisRequest extends SynthesisRequest { mFile.write( makeWavHeader(mSampleRateInHz, mAudioFormat, mChannelCount, dataLength)); closeFile(); mDone = true; return TextToSpeech.SUCCESS; } catch (IOException ex) { Log.e(TAG, "Failed to write to " + mFileName + ": " + ex); Loading @@ -173,6 +180,14 @@ class FileSynthesisRequest extends SynthesisRequest { } } @Override public void error() { if (DBG) Log.d(TAG, "FileSynthesisRequest.error()"); synchronized (mStateLock) { cleanUp(); } } @Override public int completeAudioAvailable(int sampleRateInHz, int audioFormat, int channelCount, byte[] buffer, int offset, int length) { Loading @@ -187,9 +202,11 @@ class FileSynthesisRequest extends SynthesisRequest { out = new FileOutputStream(mFileName); out.write(makeWavHeader(sampleRateInHz, audioFormat, channelCount, length)); out.write(buffer, offset, length); mDone = true; return TextToSpeech.SUCCESS; } catch (IOException ex) { Log.e(TAG, "Failed to write to " + mFileName + ": " + ex); mFileName.delete(); return TextToSpeech.ERROR; } finally { try { Loading core/java/android/speech/tts/PlaybackSynthesisRequest.java +16 −0 Original line number Diff line number Diff line Loading @@ -50,6 +50,7 @@ class PlaybackSynthesisRequest extends SynthesisRequest { private final Object mStateLock = new Object(); private AudioTrack mAudioTrack = null; private boolean mStopped = false; private boolean mDone = false; PlaybackSynthesisRequest(String text, int streamType, float volume, float pan) { super(text); Loading Loading @@ -85,6 +86,11 @@ class PlaybackSynthesisRequest extends SynthesisRequest { return MIN_AUDIO_BUFFER_SIZE; } @Override boolean isDone() { return mDone; } // TODO: add a thread that writes to the AudioTrack? @Override public int start(int sampleRateInHz, int audioFormat, int channelCount) { Loading Loading @@ -183,11 +189,20 @@ class PlaybackSynthesisRequest extends SynthesisRequest { Log.e(TAG, "done(): Not started"); return TextToSpeech.ERROR; } mDone = true; cleanUp(); } return TextToSpeech.SUCCESS; } @Override public void error() { if (DBG) Log.d(TAG, "error()"); synchronized (mStateLock) { cleanUp(); } } @Override public int completeAudioAvailable(int sampleRateInHz, int audioFormat, int channelCount, byte[] buffer, int offset, int length) { Loading Loading @@ -217,6 +232,7 @@ class PlaybackSynthesisRequest extends SynthesisRequest { try { mAudioTrack.write(buffer, offset, length); mAudioTrack.play(); mDone = true; } catch (IllegalStateException ex) { Log.e(TAG, "Playback error", ex); return TextToSpeech.ERROR; Loading core/java/android/speech/tts/SynthesisRequest.java +13 −0 Original line number Diff line number Diff line Loading @@ -113,6 +113,11 @@ public abstract class SynthesisRequest { */ public abstract int getMaxBufferSize(); /** * Checks whether the synthesis request completed successfully. */ abstract boolean isDone(); /** * Aborts the speech request. * Loading Loading @@ -161,6 +166,14 @@ public abstract class SynthesisRequest { */ public abstract int done(); /** * The service should call this method if the speech synthesis fails. * * This method should only be called on the synthesis thread, * while in {@link TextToSpeechService#onSynthesizeText}. */ public abstract void error(); /** * The service can call this method instead of using {@link #start}, {@link #audioAvailable} * and {@link #done} if all the audio data is available in a single buffer. Loading core/java/android/speech/tts/TextToSpeechService.java +5 −6 Original line number Diff line number Diff line Loading @@ -150,12 +150,10 @@ public abstract class TextToSpeechService extends Service { * * Called on the synthesis thread. * * @param request The synthesis request. The method should * call {@link SynthesisRequest#start}, {@link SynthesisRequest#audioAvailable}, * and {@link SynthesisRequest#done} on this request. * @return {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}. * @param request The synthesis request. The method should use the methods in the request * object to communicate the results of the synthesis. */ protected abstract int onSynthesizeText(SynthesisRequest request); protected abstract void onSynthesizeText(SynthesisRequest request); private boolean areDefaultsEnforced() { return getSecureSettingInt(Settings.Secure.TTS_USE_DEFAULTS, Loading Loading @@ -442,7 +440,8 @@ public abstract class TextToSpeechService extends Service { synthesisRequest = mSynthesisRequest; } setRequestParams(synthesisRequest); return TextToSpeechService.this.onSynthesizeText(synthesisRequest); TextToSpeechService.this.onSynthesizeText(synthesisRequest); return synthesisRequest.isDone() ? TextToSpeech.SUCCESS : TextToSpeech.ERROR; } protected SynthesisRequest createSynthesisRequest() { Loading Loading
core/java/android/speech/tts/FileSynthesisRequest.java +17 −0 Original line number Diff line number Diff line Loading @@ -45,6 +45,7 @@ class FileSynthesisRequest extends SynthesisRequest { private int mChannelCount; private RandomAccessFile mFile; private boolean mStopped = false; private boolean mDone = false; FileSynthesisRequest(String text, File fileName) { super(text); Loading Loading @@ -88,6 +89,11 @@ class FileSynthesisRequest extends SynthesisRequest { return MAX_AUDIO_BUFFER_SIZE; } @Override boolean isDone() { return mDone; } @Override public int start(int sampleRateInHz, int audioFormat, int channelCount) { if (DBG) { Loading Loading @@ -164,6 +170,7 @@ class FileSynthesisRequest extends SynthesisRequest { mFile.write( makeWavHeader(mSampleRateInHz, mAudioFormat, mChannelCount, dataLength)); closeFile(); mDone = true; return TextToSpeech.SUCCESS; } catch (IOException ex) { Log.e(TAG, "Failed to write to " + mFileName + ": " + ex); Loading @@ -173,6 +180,14 @@ class FileSynthesisRequest extends SynthesisRequest { } } @Override public void error() { if (DBG) Log.d(TAG, "FileSynthesisRequest.error()"); synchronized (mStateLock) { cleanUp(); } } @Override public int completeAudioAvailable(int sampleRateInHz, int audioFormat, int channelCount, byte[] buffer, int offset, int length) { Loading @@ -187,9 +202,11 @@ class FileSynthesisRequest extends SynthesisRequest { out = new FileOutputStream(mFileName); out.write(makeWavHeader(sampleRateInHz, audioFormat, channelCount, length)); out.write(buffer, offset, length); mDone = true; return TextToSpeech.SUCCESS; } catch (IOException ex) { Log.e(TAG, "Failed to write to " + mFileName + ": " + ex); mFileName.delete(); return TextToSpeech.ERROR; } finally { try { Loading
core/java/android/speech/tts/PlaybackSynthesisRequest.java +16 −0 Original line number Diff line number Diff line Loading @@ -50,6 +50,7 @@ class PlaybackSynthesisRequest extends SynthesisRequest { private final Object mStateLock = new Object(); private AudioTrack mAudioTrack = null; private boolean mStopped = false; private boolean mDone = false; PlaybackSynthesisRequest(String text, int streamType, float volume, float pan) { super(text); Loading Loading @@ -85,6 +86,11 @@ class PlaybackSynthesisRequest extends SynthesisRequest { return MIN_AUDIO_BUFFER_SIZE; } @Override boolean isDone() { return mDone; } // TODO: add a thread that writes to the AudioTrack? @Override public int start(int sampleRateInHz, int audioFormat, int channelCount) { Loading Loading @@ -183,11 +189,20 @@ class PlaybackSynthesisRequest extends SynthesisRequest { Log.e(TAG, "done(): Not started"); return TextToSpeech.ERROR; } mDone = true; cleanUp(); } return TextToSpeech.SUCCESS; } @Override public void error() { if (DBG) Log.d(TAG, "error()"); synchronized (mStateLock) { cleanUp(); } } @Override public int completeAudioAvailable(int sampleRateInHz, int audioFormat, int channelCount, byte[] buffer, int offset, int length) { Loading Loading @@ -217,6 +232,7 @@ class PlaybackSynthesisRequest extends SynthesisRequest { try { mAudioTrack.write(buffer, offset, length); mAudioTrack.play(); mDone = true; } catch (IllegalStateException ex) { Log.e(TAG, "Playback error", ex); return TextToSpeech.ERROR; Loading
core/java/android/speech/tts/SynthesisRequest.java +13 −0 Original line number Diff line number Diff line Loading @@ -113,6 +113,11 @@ public abstract class SynthesisRequest { */ public abstract int getMaxBufferSize(); /** * Checks whether the synthesis request completed successfully. */ abstract boolean isDone(); /** * Aborts the speech request. * Loading Loading @@ -161,6 +166,14 @@ public abstract class SynthesisRequest { */ public abstract int done(); /** * The service should call this method if the speech synthesis fails. * * This method should only be called on the synthesis thread, * while in {@link TextToSpeechService#onSynthesizeText}. */ public abstract void error(); /** * The service can call this method instead of using {@link #start}, {@link #audioAvailable} * and {@link #done} if all the audio data is available in a single buffer. Loading
core/java/android/speech/tts/TextToSpeechService.java +5 −6 Original line number Diff line number Diff line Loading @@ -150,12 +150,10 @@ public abstract class TextToSpeechService extends Service { * * Called on the synthesis thread. * * @param request The synthesis request. The method should * call {@link SynthesisRequest#start}, {@link SynthesisRequest#audioAvailable}, * and {@link SynthesisRequest#done} on this request. * @return {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}. * @param request The synthesis request. The method should use the methods in the request * object to communicate the results of the synthesis. */ protected abstract int onSynthesizeText(SynthesisRequest request); protected abstract void onSynthesizeText(SynthesisRequest request); private boolean areDefaultsEnforced() { return getSecureSettingInt(Settings.Secure.TTS_USE_DEFAULTS, Loading Loading @@ -442,7 +440,8 @@ public abstract class TextToSpeechService extends Service { synthesisRequest = mSynthesisRequest; } setRequestParams(synthesisRequest); return TextToSpeechService.this.onSynthesizeText(synthesisRequest); TextToSpeechService.this.onSynthesizeText(synthesisRequest); return synthesisRequest.isDone() ? TextToSpeech.SUCCESS : TextToSpeech.ERROR; } protected SynthesisRequest createSynthesisRequest() { Loading