diff options
author | Przemyslaw Szczepaniak <pszczepaniak@google.com> | 2014-07-14 17:10:42 +0100 |
---|---|---|
committer | Przemyslaw Szczepaniak <pszczepaniak@google.com> | 2014-07-17 13:19:39 +0100 |
commit | 672695e42387a024083d198f3d665f354ef1d27c (patch) | |
tree | d9a0c560cefac46c7127f564e02743a01f8edf0b /core/java/android/speech | |
parent | faa0192826063d40eef2cd997b1649c78cb639b0 (diff) | |
download | frameworks_base-672695e42387a024083d198f3d665f354ef1d27c.zip frameworks_base-672695e42387a024083d198f3d665f354ef1d27c.tar.gz frameworks_base-672695e42387a024083d198f3d665f354ef1d27c.tar.bz2 |
Add AudioAttributes support in the TTS.
Bug: 16259299
Change-Id: I53f4f7a2b9a3b9a422affaf6abe4aca258e67d7f
Diffstat (limited to 'core/java/android/speech')
4 files changed, 81 insertions, 24 deletions
diff --git a/core/java/android/speech/tts/AudioPlaybackQueueItem.java b/core/java/android/speech/tts/AudioPlaybackQueueItem.java index c13b47f..b4ac429 100644 --- a/core/java/android/speech/tts/AudioPlaybackQueueItem.java +++ b/core/java/android/speech/tts/AudioPlaybackQueueItem.java @@ -54,7 +54,11 @@ class AudioPlaybackQueueItem extends PlaybackQueueItem { final UtteranceProgressDispatcher dispatcher = getDispatcher(); dispatcher.dispatchOnStart(); - mPlayer = MediaPlayer.create(mContext, mUri); + + int sessionId = mAudioParams.mSessionId; + mPlayer = MediaPlayer.create( + mContext, mUri, null, mAudioParams.mAudioAttributes, + sessionId > 0 ? sessionId : AudioSystem.AUDIO_SESSION_ALLOCATE); if (mPlayer == null) { dispatcher.dispatchOnError(TextToSpeech.ERROR_OUTPUT); return; @@ -76,11 +80,8 @@ class AudioPlaybackQueueItem extends PlaybackQueueItem { mDone.open(); } }); - mPlayer.setAudioStreamType(mAudioParams.mStreamType); + setupVolume(mPlayer, mAudioParams.mVolume, mAudioParams.mPan); - if (mAudioParams.mSessionId != AudioSystem.AUDIO_SESSION_ALLOCATE) { - mPlayer.setAudioSessionId(mAudioParams.mSessionId); - } mPlayer.start(); mDone.block(); finish(); diff --git a/core/java/android/speech/tts/BlockingAudioTrack.java b/core/java/android/speech/tts/BlockingAudioTrack.java index b405de0..dc4e9d3 100644 --- a/core/java/android/speech/tts/BlockingAudioTrack.java +++ b/core/java/android/speech/tts/BlockingAudioTrack.java @@ -2,6 +2,7 @@ package android.speech.tts; +import android.media.AudioAttributes; import android.media.AudioFormat; import android.media.AudioTrack; import android.speech.tts.TextToSpeechService.AudioOutputParams; @@ -214,9 +215,14 @@ class BlockingAudioTrack { = AudioTrack.getMinBufferSize(mSampleRateInHz, channelConfig, mAudioFormat); int bufferSizeInBytes = Math.max(MIN_AUDIO_BUFFER_SIZE, minBufferSizeInBytes); - AudioTrack audioTrack = new AudioTrack(mAudioParams.mStreamType, mSampleRateInHz, - channelConfig, mAudioFormat, bufferSizeInBytes, AudioTrack.MODE_STREAM, + AudioFormat audioFormat = (new AudioFormat.Builder()) + .setChannelMask(channelConfig) + .setEncoding(mAudioFormat) + .setSampleRate(mSampleRateInHz).build(); + AudioTrack audioTrack = new AudioTrack(mAudioParams.mAudioAttributes, + audioFormat, bufferSizeInBytes, AudioTrack.MODE_STREAM, mAudioParams.mSessionId); + if (audioTrack.getState() != AudioTrack.STATE_INITIALIZED) { Log.w(TAG, "Unable to create audio track."); audioTrack.release(); diff --git a/core/java/android/speech/tts/TextToSpeech.java b/core/java/android/speech/tts/TextToSpeech.java index 46077ed..d8b9b5f 100644 --- a/core/java/android/speech/tts/TextToSpeech.java +++ b/core/java/android/speech/tts/TextToSpeech.java @@ -22,6 +22,7 @@ import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; +import android.media.AudioAttributes; import android.media.AudioManager; import android.net.Uri; import android.os.AsyncTask; @@ -524,6 +525,17 @@ public class TextToSpeech { public static final String KEY_PARAM_STREAM = "streamType"; /** + * Parameter key to specify the audio attributes to be used when + * speaking text or playing back a file. The value should be set + * using {@link TextToSpeech#setAudioAttributes(AudioAttributes)}. + * + * @see TextToSpeech#speak(String, int, HashMap) + * @see TextToSpeech#playEarcon(String, int, HashMap) + * @hide + */ + public static final String KEY_PARAM_AUDIO_ATTRIBUTES = "audioAttributes"; + + /** * Parameter key to identify an utterance in the * {@link TextToSpeech.OnUtteranceCompletedListener} after text has been * spoken, a file has been played back or a silence duration has elapsed. @@ -1357,6 +1369,25 @@ public class TextToSpeech { } /** + * Sets the audio attributes to be used when speaking text or playing + * back a file. + * + * @param audioAttributes Valid AudioAttributes instance. + * + * @return {@link #ERROR} or {@link #SUCCESS}. + */ + public int setAudioAttributes(AudioAttributes audioAttributes) { + if (audioAttributes != null) { + synchronized (mStartLock) { + mParams.putParcelable(Engine.KEY_PARAM_AUDIO_ATTRIBUTES, + audioAttributes); + } + return SUCCESS; + } + return ERROR; + } + + /** * @return the engine currently in use by this TextToSpeech instance. * @hide */ diff --git a/core/java/android/speech/tts/TextToSpeechService.java b/core/java/android/speech/tts/TextToSpeechService.java index a0743f7..4fea109 100644 --- a/core/java/android/speech/tts/TextToSpeechService.java +++ b/core/java/android/speech/tts/TextToSpeechService.java @@ -17,7 +17,7 @@ package android.speech.tts; import android.app.Service; import android.content.Intent; -import android.media.AudioManager; +import android.media.AudioAttributes; import android.media.AudioSystem; import android.net.Uri; import android.os.Binder; @@ -608,12 +608,6 @@ public abstract class TextToSpeechService extends Service { public final int mSessionId; /** - * Audio stream type. Must be one of the STREAM_ contants defined in - * {@link android.media.AudioManager}. - */ - public final int mStreamType; - - /** * Volume, in the range [0.0f, 1.0f]. The default value is * {@link TextToSpeech.Engine#DEFAULT_VOLUME} (1.0f). */ @@ -625,42 +619,62 @@ public abstract class TextToSpeechService extends Service { */ public final float mPan; + + /** + * Audio attributes, set by {@link TextToSpeech#setAudioAttributes} + * or created from the value of {@link TextToSpeech.Engine#KEY_PARAM_STREAM}. + */ + public final AudioAttributes mAudioAttributes; + /** Create AudioOutputParams with default values */ AudioOutputParams() { mSessionId = AudioSystem.AUDIO_SESSION_ALLOCATE; - mStreamType = Engine.DEFAULT_STREAM; mVolume = Engine.DEFAULT_VOLUME; mPan = Engine.DEFAULT_PAN; + mAudioAttributes = null; } - AudioOutputParams(int sessionId, int streamType, float volume, float pan) { + AudioOutputParams(int sessionId, float volume, float pan, + AudioAttributes audioAttributes) { mSessionId = sessionId; - mStreamType = streamType; mVolume = volume; mPan = pan; + mAudioAttributes = audioAttributes; } /** Create AudioOutputParams from A {@link SynthesisRequest#getParams()} bundle */ - static AudioOutputParams createFromV1ParamsBundle(Bundle paramsBundle) { + static AudioOutputParams createFromV1ParamsBundle(Bundle paramsBundle, + boolean isSpeech) { if (paramsBundle == null) { return new AudioOutputParams(); } + AudioAttributes audioAttributes = + (AudioAttributes) paramsBundle.getParcelable( + Engine.KEY_PARAM_AUDIO_ATTRIBUTES); + if (audioAttributes == null) { + int streamType = paramsBundle.getInt( + Engine.KEY_PARAM_STREAM, Engine.DEFAULT_STREAM); + audioAttributes = (new AudioAttributes.Builder()) + .setLegacyStreamType(streamType) + .setContentType((isSpeech ? + AudioAttributes.CONTENT_TYPE_SPEECH : + AudioAttributes.CONTENT_TYPE_SONIFICATION)) + .build(); + } + return new AudioOutputParams( paramsBundle.getInt( Engine.KEY_PARAM_SESSION_ID, AudioSystem.AUDIO_SESSION_ALLOCATE), - paramsBundle.getInt( - Engine.KEY_PARAM_STREAM, - Engine.DEFAULT_STREAM), paramsBundle.getFloat( Engine.KEY_PARAM_VOLUME, Engine.DEFAULT_VOLUME), paramsBundle.getFloat( Engine.KEY_PARAM_PAN, - Engine.DEFAULT_PAN)); + Engine.DEFAULT_PAN), + audioAttributes); } - } @@ -832,7 +846,7 @@ public abstract class TextToSpeechService extends Service { } AudioOutputParams getAudioParams() { - return AudioOutputParams.createFromV1ParamsBundle(mParams); + return AudioOutputParams.createFromV1ParamsBundle(mParams, true); } } @@ -1005,6 +1019,11 @@ public abstract class TextToSpeechService extends Service { public String getUtteranceId() { return getStringParam(mParams, Engine.KEY_PARAM_UTTERANCE_ID, null); } + + @Override + AudioOutputParams getAudioParams() { + return AudioOutputParams.createFromV1ParamsBundle(mParams, false); + } } private class SilenceSpeechItem extends UtteranceSpeechItem { |