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

Commit a210e15c authored by Bjorn Bringert's avatar Bjorn Bringert Committed by Android (Google) Code Review
Browse files

Merge "TTS: SynthesisRequest.error() instead of return value"

parents 578492e9 360eb168
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -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);
@@ -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) {
@@ -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);
@@ -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) {
@@ -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 {
+16 −0
Original line number Diff line number Diff line
@@ -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);
@@ -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) {
@@ -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) {
@@ -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;
+13 −0
Original line number Diff line number Diff line
@@ -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.
     *
@@ -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.
+5 −6
Original line number Diff line number Diff line
@@ -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,
@@ -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() {