Merge "Restrict audioformat to 8bit, 16bit or float PCM with an annotation and a warning."
diff --git a/core/java/android/speech/tts/FileSynthesisCallback.java b/core/java/android/speech/tts/FileSynthesisCallback.java
index a88eead..2b882d3 100644
--- a/core/java/android/speech/tts/FileSynthesisCallback.java
+++ b/core/java/android/speech/tts/FileSynthesisCallback.java
@@ -107,6 +107,14 @@
Log.d(TAG, "FileSynthesisRequest.start(" + sampleRateInHz + "," + audioFormat
+ "," + channelCount + ")");
}
+ if (audioFormat != AudioFormat.ENCODING_PCM_8BIT ||
+ audioFormat != AudioFormat.ENCODING_PCM_16BIT ||
+ audioFormat != AudioFormat.ENCODING_PCM_FLOAT) {
+ Log.e(TAG, "Audio format encoding " + audioFormat + " not supported. Please use one " +
+ "of AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT or " +
+ "AudioFormat.ENCODING_PCM_FLOAT");
+ }
+
FileChannel fileChannel = null;
synchronized (mStateLock) {
if (mStatusCode == TextToSpeech.STOPPED) {
diff --git a/core/java/android/speech/tts/PlaybackSynthesisCallback.java b/core/java/android/speech/tts/PlaybackSynthesisCallback.java
index f850f10..a6fb543 100644
--- a/core/java/android/speech/tts/PlaybackSynthesisCallback.java
+++ b/core/java/android/speech/tts/PlaybackSynthesisCallback.java
@@ -15,6 +15,7 @@
*/
package android.speech.tts;
+import android.media.AudioFormat;
import android.speech.tts.TextToSpeechService.AudioOutputParams;
import android.speech.tts.TextToSpeechService.UtteranceProgressDispatcher;
import android.util.Log;
@@ -122,6 +123,13 @@
public int start(int sampleRateInHz, int audioFormat, int channelCount) {
if (DBG) Log.d(TAG, "start(" + sampleRateInHz + "," + audioFormat + "," + channelCount
+ ")");
+ if (audioFormat != AudioFormat.ENCODING_PCM_8BIT ||
+ audioFormat != AudioFormat.ENCODING_PCM_16BIT ||
+ audioFormat != AudioFormat.ENCODING_PCM_FLOAT) {
+ Log.w(TAG, "Audio format encoding " + audioFormat + " not supported. Please use one " +
+ "of AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT or " +
+ "AudioFormat.ENCODING_PCM_FLOAT");
+ }
int channelConfig = BlockingAudioTrack.getChannelConfig(channelCount);
diff --git a/core/java/android/speech/tts/SynthesisCallback.java b/core/java/android/speech/tts/SynthesisCallback.java
index e32438b..aad0d87 100644
--- a/core/java/android/speech/tts/SynthesisCallback.java
+++ b/core/java/android/speech/tts/SynthesisCallback.java
@@ -15,6 +15,14 @@
*/
package android.speech.tts;
+import android.annotation.IntDef;
+import android.annotation.IntRange;
+import android.media.AudioFormat;
+import android.speech.tts.TextToSpeech;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
/**
* A callback to return speech data synthesized by a text to speech engine.
*
@@ -31,6 +39,13 @@
* All methods can be only called on the synthesis thread.
*/
public interface SynthesisCallback {
+
+ /** @hide */
+ @Retention(RetentionPolicy.SOURCE)
+ @IntDef({AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT,
+ AudioFormat.ENCODING_PCM_FLOAT})
+ public @interface SupportedAudioFormat {};
+
/**
* @return the maximum number of bytes that the TTS engine can pass in a single call of
* {@link #audioAvailable}. Calls to {@link #audioAvailable} with data lengths
@@ -38,6 +53,7 @@
*/
public int getMaxBufferSize();
+ // TODO: Replace reference to Android N to an API level when the API level for N is decided.
/**
* The service should call this when it starts to synthesize audio for this
* request.
@@ -47,12 +63,16 @@
*
* @param sampleRateInHz Sample rate in HZ of the generated audio.
* @param audioFormat Audio format of the generated audio. Must be one of
- * the ENCODING_ constants defined in {@link android.media.AudioFormat}.
+ * {@link android.media.AudioFormat.ENCODING_PCM_8BIT} or
+ * {@link android.media.AudioFormat.ENCODING_PCM_16BIT}. Or
+ * {@link android.media.AudioFormat.ENCODING_PCM_FLOAT} when targetting Android N and
+ * above.
* @param channelCount The number of channels. Must be {@code 1} or {@code 2}.
* @return {@link TextToSpeech#SUCCESS}, {@link TextToSpeech#ERROR} or
* {@link TextToSpeech#STOPPED}.
*/
- public int start(int sampleRateInHz, int audioFormat, int channelCount);
+ public int start(int sampleRateInHz, @SupportedAudioFormat int audioFormat,
+ @IntRange(from=1,to=2) int channelCount);
/**
* The service should call this method when synthesized audio is ready for consumption.
@@ -102,7 +122,7 @@
* @param errorCode Error code to pass to the client. One of the ERROR_ values from
* {@link TextToSpeech}
*/
- public void error(int errorCode);
+ public void error(@TextToSpeech.Error int errorCode);
/**
* Check if {@link #start} was called or not.
diff --git a/core/java/android/speech/tts/TextToSpeech.java b/core/java/android/speech/tts/TextToSpeech.java
index eae4329..61c33ff 100644
--- a/core/java/android/speech/tts/TextToSpeech.java
+++ b/core/java/android/speech/tts/TextToSpeech.java
@@ -15,6 +15,7 @@
*/
package android.speech.tts;
+import android.annotation.IntDef;
import android.annotation.RawRes;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
@@ -38,6 +39,8 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -75,6 +78,12 @@
*/
public static final int STOPPED = -2;
+ /** @hide */
+ @IntDef({ERROR_SYNTHESIS, ERROR_SERVICE, ERROR_OUTPUT, ERROR_NETWORK, ERROR_NETWORK_TIMEOUT,
+ ERROR_INVALID_REQUEST, ERROR_NOT_INSTALLED_YET})
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface Error {}
+
/**
* Denotes a failure of a TTS engine to synthesize the given input.
*/