summaryrefslogtreecommitdiff
path: root/src/com/android/speechrecorder/MicrophoneInputStream.java
blob: ab1f47527704ca918f1b140e1836398b94a87e41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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();
    }
}