diff options
author | Narayan Kamath <narayan@google.com> | 2015-06-16 20:26:07 +0100 |
---|---|---|
committer | Narayan Kamath <narayan@google.com> | 2015-06-17 12:37:28 +0100 |
commit | 44568997f88595aa59dac61e61b1e8f9b326d7de (patch) | |
tree | 1f30c4998666be79704a691131a963ac82f1fdca /src/com/android/speechrecorder/MicrophoneInputStream.java | |
parent | 536aa74ff3a77186bef29dc9333a34688fa59d13 (diff) | |
download | SpeechRecorder-44568997f88595aa59dac61e61b1e8f9b326d7de.tar.gz |
Reimplement MicrophoneInputStream in terms of AudioRecord.
Lets us delete the defunct MicrophoneInputStream class from
the frameworks.
bug: 21855957
Change-Id: I46cd0c822f486eecb2223fefd6a0919cdddf0eb3
Diffstat (limited to 'src/com/android/speechrecorder/MicrophoneInputStream.java')
-rw-r--r-- | src/com/android/speechrecorder/MicrophoneInputStream.java | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/com/android/speechrecorder/MicrophoneInputStream.java b/src/com/android/speechrecorder/MicrophoneInputStream.java new file mode 100644 index 0000000..ab1f475 --- /dev/null +++ b/src/com/android/speechrecorder/MicrophoneInputStream.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2006 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 com.android.speechrecorder; + +import java.io.IOException; +import java.io.InputStream; + +import android.media.AudioRecord; + +import static android.media.AudioFormat.CHANNEL_IN_MONO; +import static android.media.AudioFormat.ENCODING_PCM_16BIT; +import static android.media.MediaRecorder.AudioSource.VOICE_RECOGNITION; + +/** + * Provides an InputStream like API over {@code android.media.AudioRecord}. + * + * <b>Not thread safe.</b> + */ +public final class MicrophoneInputStream extends InputStream { + + private final int mSampleRate; + private final int mBufferSize; + private final AudioRecord mAudioRecord; + + private boolean mRecording; + + public MicrophoneInputStream(int sampleRate) throws IOException { + mSampleRate = sampleRate; + mBufferSize = AudioRecord.getMinBufferSize(sampleRate, + CHANNEL_IN_MONO, ENCODING_PCM_16BIT); + mAudioRecord = createAudioRecord(); + mRecording = false; + } + + private AudioRecord createAudioRecord() throws IOException { + AudioRecord ar = new AudioRecord(VOICE_RECOGNITION, + mSampleRate, CHANNEL_IN_MONO, ENCODING_PCM_16BIT, mBufferSize); + + if (ar.getState() != AudioRecord.STATE_INITIALIZED) { + ar.release(); + throw new IOException("Unable to create AudioRecord"); + } + + return ar; + } + + private void maybeStartRecording() throws IOException { + if (mRecording) { + return; + } + + mAudioRecord.startRecording(); + + int recordingState = mAudioRecord.getRecordingState(); + if (recordingState != AudioRecord.RECORDSTATE_RECORDING) { + throw new IOException("Unexpected recordingState: " + recordingState); + } + mRecording = true; + } + + @Override + public int read() throws IOException { + throw new UnsupportedOperationException("Single byte read."); + } + + @Override + public int read(byte[] b, int offset, int length) throws IOException { + maybeStartRecording(); + + final int ret = mAudioRecord.read(b, offset, length); + if (ret == AudioRecord.ERROR_INVALID_OPERATION || + ret == AudioRecord.ERROR_BAD_VALUE) { + throw new IOException("AudioRecord.read returned: " + ret); + } + + return ret; + } + + @Override + public void close() throws IOException { + mAudioRecord.stop(); + mAudioRecord.release(); + } +} |