1e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath/* 2e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * Copyright (C) 2011 The Android Open Source Project 3e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 4e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * use this file except in compliance with the License. You may obtain a copy of 6e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * the License at 7e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 8e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * http://www.apache.org/licenses/LICENSE-2.0 9e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 10e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * Unless required by applicable law or agreed to in writing, software 11e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * License for the specific language governing permissions and limitations under 14e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * the License. 15e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath */ 16e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamathpackage android.speech.tts; 17e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath 18e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath/** 19e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * A callback to return speech data synthesized by a text to speech engine. 20e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 21e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * The engine can provide streaming audio by calling 22e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * {@link #start}, then {@link #audioAvailable} until all audio has been provided, then finally 23e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * {@link #done}. 24e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 25e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 26e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * {@link #error} can be called at any stage in the synthesis process to 27c3da8818f0598b3ab2cd6f4168349da6d0f72cb1Narayan Kamath * indicate that an error has occurred, but if the call is made after a call 28c3da8818f0598b3ab2cd6f4168349da6d0f72cb1Narayan Kamath * to {@link #done}, it might be discarded. 29e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath */ 30e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamathpublic interface SynthesisCallback { 31e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath /** 32e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * @return the maximum number of bytes that the TTS engine can pass in a single call of 33c3da8818f0598b3ab2cd6f4168349da6d0f72cb1Narayan Kamath * {@link #audioAvailable}. Calls to {@link #audioAvailable} with data lengths 34c3da8818f0598b3ab2cd6f4168349da6d0f72cb1Narayan Kamath * larger than this value will not succeed. 35e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath */ 36e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath public int getMaxBufferSize(); 37e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath 38e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath /** 39e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * The service should call this when it starts to synthesize audio for this 40e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * request. 41e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 42e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * This method should only be called on the synthesis thread, 43e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * while in {@link TextToSpeechService#onSynthesizeText}. 44e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 45e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * @param sampleRateInHz Sample rate in HZ of the generated audio. 46e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * @param audioFormat Audio format of the generated audio. Must be one of 47e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * the ENCODING_ constants defined in {@link android.media.AudioFormat}. 48e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * @param channelCount The number of channels. Must be {@code 1} or {@code 2}. 49e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * @return {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}. 50e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath */ 51e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath public int start(int sampleRateInHz, int audioFormat, int channelCount); 52e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath 53e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath /** 54e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * The service should call this method when synthesized audio is ready for consumption. 55e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 56e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * This method should only be called on the synthesis thread, 57e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * while in {@link TextToSpeechService#onSynthesizeText}. 58e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 59e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * @param buffer The generated audio data. This method will not hold on to {@code buffer}, 60e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * so the caller is free to modify it after this method returns. 61e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * @param offset The offset into {@code buffer} where the audio data starts. 62e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * @param length The number of bytes of audio data in {@code buffer}. This must be 63e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * less than or equal to the return value of {@link #getMaxBufferSize}. 64e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * @return {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}. 65e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath */ 66e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath public int audioAvailable(byte[] buffer, int offset, int length); 67e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath 68e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath /** 69e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * The service should call this method when all the synthesized audio for a request has 70e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * been passed to {@link #audioAvailable}. 71e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 72e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * This method should only be called on the synthesis thread, 73e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * while in {@link TextToSpeechService#onSynthesizeText}. 74e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 75e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * @return {@link TextToSpeech#SUCCESS} or {@link TextToSpeech#ERROR}. 76e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath */ 77e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath public int done(); 78e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath 79e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath /** 80e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * The service should call this method if the speech synthesis fails. 81e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * 82e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * This method should only be called on the synthesis thread, 83e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath * while in {@link TextToSpeechService#onSynthesizeText}. 84e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath */ 85e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath public void error(); 86e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath 87e22b69a7de0349b99d3107349d1d3aa72d62c841Narayan Kamath}