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

Commit 50e657bb authored by Bjorn Bringert's avatar Bjorn Bringert
Browse files

Add Java API for writing TTS engines

This removes the old non-public C++ API for TTS
engines and replaces it with a Java API.

The new API is still @hidden, until it has been approved.

Bug: 4148636
Change-Id: I7614ff788e11f897e87052f684f1b4938d539fb7
parent 720dd9a8
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -137,8 +137,8 @@ LOCAL_SRC_FILES += \
	core/java/android/view/IWindowSession.aidl \
	core/java/android/speech/IRecognitionListener.aidl \
	core/java/android/speech/IRecognitionService.aidl \
	core/java/android/speech/tts/ITts.aidl \
	core/java/android/speech/tts/ITtsCallback.aidl \
	core/java/android/speech/tts/ITextToSpeechCallback.aidl \
	core/java/android/speech/tts/ITextToSpeechService.aidl \
	core/java/com/android/internal/app/IBatteryStats.aidl \
	core/java/com/android/internal/app/IUsageStats.aidl \
	core/java/com/android/internal/app/IMediaContainerService.aidl \
+1 −0
Original line number Diff line number Diff line
@@ -96,6 +96,7 @@ $(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/PerfTest_interme
$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/RSTest_intermediates/)
$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/hardware/IUsbManager.java)
$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/nfc)
$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/framework_intermediates)

# ************************************************
# NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
+146 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package android.speech.tts;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.ConditionVariable;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.util.Log;

/**
 * A media player that allows blocking to wait for it to finish.
 */
class BlockingMediaPlayer {

    private static final String TAG = "BlockMediaPlayer";

    private static final String MEDIA_PLAYER_THREAD_NAME = "TTS-MediaPlayer";

    private final Context mContext;
    private final Uri mUri;
    private final int mStreamType;
    private final ConditionVariable mDone;
    // Only accessed on the Handler thread
    private MediaPlayer mPlayer;
    private volatile boolean mFinished;

    /**
     * Creates a new blocking media player.
     * Creating a blocking media player is a cheap operation.
     *
     * @param context
     * @param uri
     * @param streamType
     */
    public BlockingMediaPlayer(Context context, Uri uri, int streamType) {
        mContext = context;
        mUri = uri;
        mStreamType = streamType;
        mDone = new ConditionVariable();

    }

    /**
     * Starts playback and waits for it to finish.
     * Can be called from any thread.
     *
     * @return {@code true} if the playback finished normally, {@code false} if the playback
     *         failed or {@link #stop} was called before the playback finished.
     */
    public boolean startAndWait() {
        HandlerThread thread = new HandlerThread(MEDIA_PLAYER_THREAD_NAME);
        thread.start();
        Handler handler = new Handler(thread.getLooper());
        mFinished = false;
        handler.post(new Runnable() {
            @Override
            public void run() {
                startPlaying();
            }
        });
        mDone.block();
        handler.post(new Runnable() {
            @Override
            public void run() {
                finish();
                // No new messages should get posted to the handler thread after this
                Looper.myLooper().quit();
            }
        });
        return mFinished;
    }

    /**
     * Stops playback. Can be called multiple times.
     * Can be called from any thread.
     */
    public void stop() {
        mDone.open();
    }

    /**
     * Starts playback.
     * Called on the handler thread.
     */
    private void startPlaying() {
        mPlayer = MediaPlayer.create(mContext, mUri);
        if (mPlayer == null) {
            Log.w(TAG, "Failed to play " + mUri);
            mDone.open();
            return;
        }
        try {
            mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Log.w(TAG, "Audio playback error: " + what + ", " + extra);
                    mDone.open();
                    return true;
                }
            });
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mFinished = true;
                    mDone.open();
                }
            });
            mPlayer.setAudioStreamType(mStreamType);
            mPlayer.start();
        } catch (IllegalArgumentException ex) {
            Log.w(TAG, "MediaPlayer failed", ex);
            mDone.open();
        }
    }

    /**
     * Stops playback and release the media player.
     * Called on the handler thread.
     */
    private void finish() {
        try {
            mPlayer.stop();
        } catch (IllegalStateException ex) {
            // Do nothing, the player is already stopped
        }
        mPlayer.release();
    }

}
 No newline at end of file
+197 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package android.speech.tts;

import android.media.AudioFormat;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Speech synthesis request that writes the audio to a WAV file.
 */
class FileSynthesisRequest extends SynthesisRequest {

    private static final String TAG = "FileSynthesisRequest";
    private static final boolean DBG = false;

    private static final int WAV_HEADER_LENGTH = 44;
    private static final short WAV_FORMAT_PCM = 0x0001;

    private final Object mStateLock = new Object();
    private final File mFileName;
    private int mSampleRateInHz;
    private int mAudioFormat;
    private int mChannelCount;
    private RandomAccessFile mFile;
    private boolean mStopped = false;

    FileSynthesisRequest(String text, File fileName) {
        super(text);
        mFileName = fileName;
    }

    @Override
    void stop() {
        synchronized (mStateLock) {
            mStopped = true;
            cleanUp();
        }
    }

    /**
     * Must be called while holding the monitor on {@link #mStateLock}.
     */
    private void cleanUp() {
        closeFile();
        if (mFile != null) {
            mFileName.delete();
        }
    }

    /**
     * Must be called while holding the monitor on {@link #mStateLock}.
     */
    private void closeFile() {
        try {
            if (mFile != null) {
                mFile.close();
                mFile = null;
            }
        } catch (IOException ex) {
            Log.e(TAG, "Failed to close " + mFileName + ": " + ex);
        }
    }

    @Override
    public int start(int sampleRateInHz, int audioFormat, int channelCount) {
        if (DBG) {
            Log.d(TAG, "FileSynthesisRequest.start(" + sampleRateInHz + "," + audioFormat
                    + "," + channelCount + ")");
        }
        synchronized (mStateLock) {
            if (mStopped) {
                if (DBG) Log.d(TAG, "Request has been aborted.");
                return TextToSpeech.ERROR;
            }
            if (mFile != null) {
                cleanUp();
                throw new IllegalArgumentException("FileSynthesisRequest.start() called twice");
            }
            mSampleRateInHz = sampleRateInHz;
            mAudioFormat = audioFormat;
            mChannelCount = channelCount;
            try {
                mFile = new RandomAccessFile(mFileName, "rw");
                // Reserve space for WAV header
                mFile.write(new byte[WAV_HEADER_LENGTH]);
                return TextToSpeech.SUCCESS;
            } catch (IOException ex) {
                Log.e(TAG, "Failed to open " + mFileName + ": " + ex);
                cleanUp();
                return TextToSpeech.ERROR;
            }
        }
    }

    @Override
    public int audioAvailable(byte[] buffer, int offset, int length) {
        if (DBG) {
            Log.d(TAG, "FileSynthesisRequest.audioAvailable(" + buffer + "," + offset
                    + "," + length + ")");
        }
        synchronized (mStateLock) {
            if (mStopped) {
                if (DBG) Log.d(TAG, "Request has been aborted.");
                return TextToSpeech.ERROR;
            }
            if (mFile == null) {
                Log.e(TAG, "File not open");
                return TextToSpeech.ERROR;
            }
            try {
                mFile.write(buffer, offset, length);
                return TextToSpeech.SUCCESS;
            } catch (IOException ex) {
                Log.e(TAG, "Failed to write to " + mFileName + ": " + ex);
                cleanUp();
                return TextToSpeech.ERROR;
            }
        }
    }

    @Override
    public int done() {
        if (DBG) Log.d(TAG, "FileSynthesisRequest.done()");
        synchronized (mStateLock) {
            if (mStopped) {
                if (DBG) Log.d(TAG, "Request has been aborted.");
                return TextToSpeech.ERROR;
            }
            if (mFile == null) {
                Log.e(TAG, "File not open");
                return TextToSpeech.ERROR;
            }
            try {
                // Write WAV header at start of file
                mFile.seek(0);
                int fileLen = (int) mFile.length();
                mFile.write(makeWavHeader(mSampleRateInHz, mAudioFormat, mChannelCount, fileLen));
                closeFile();
                return TextToSpeech.SUCCESS;
            } catch (IOException ex) {
                Log.e(TAG, "Failed to write to " + mFileName + ": " + ex);
                cleanUp();
                return TextToSpeech.ERROR;
            }
        }
    }

    private byte[] makeWavHeader(int sampleRateInHz, int audioFormat, int channelCount,
            int fileLength) {
        // TODO: is AudioFormat.ENCODING_DEFAULT always the same as ENCODING_PCM_16BIT?
        int sampleSizeInBytes = (audioFormat == AudioFormat.ENCODING_PCM_8BIT ? 1 : 2);
        int byteRate = sampleRateInHz * sampleSizeInBytes * channelCount;
        short blockAlign = (short) (sampleSizeInBytes * channelCount);
        short bitsPerSample = (short) (sampleSizeInBytes * 8);

        byte[] headerBuf = new byte[WAV_HEADER_LENGTH];
        ByteBuffer header = ByteBuffer.wrap(headerBuf);
        header.order(ByteOrder.LITTLE_ENDIAN);

        header.put(new byte[]{ 'R', 'I', 'F', 'F' });
        header.putInt(fileLength - 8);  // RIFF chunk size
        header.put(new byte[]{ 'W', 'A', 'V', 'E' });
        header.put(new byte[]{ 'f', 'm', 't', ' ' });
        header.putInt(16);  // size of fmt chunk
        header.putShort(WAV_FORMAT_PCM);
        header.putShort((short) channelCount);
        header.putInt(sampleRateInHz);
        header.putInt(byteRate);
        header.putShort(blockAlign);
        header.putShort(bitsPerSample);
        header.put(new byte[]{ 'd', 'a', 't', 'a' });
        int dataLength = fileLength - WAV_HEADER_LENGTH;
        header.putInt(dataLength);

        return headerBuf;
    }

}
+3 −5
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 The Android Open Source Project
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -13,15 +13,13 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.speech.tts;

/**
 * AIDL for the callback from the TTS Service
 * ITtsCallback.java is autogenerated from this.
 * Interface for callbacks from TextToSpeechService
 *
 * {@hide}
 */
oneway interface ITtsCallback {
oneway interface ITextToSpeechCallback {
    void utteranceCompleted(String utteranceId);
}
Loading