FileDocCategorySizeDatePackage
MicrophoneInputStream.javaAPI DocAndroid 1.5 API4550Wed May 06 22:41:56 BST 2009android.speech.srec

MicrophoneInputStream

public final class MicrophoneInputStream extends InputStream
PCM input stream from the microphone, 16 bits per sample.

Fields Summary
private static final String
TAG
private int
mAudioRecord
private byte[]
mOneByte
Constructors Summary
public MicrophoneInputStream(int sampleRate, int fifoDepth)
MicrophoneInputStream constructor.

param
sampleRate sample rate of the microphone, typically 11025 or 8000.
param
fifoDepth depth of the real time fifo, measured in sampleRate clock ticks. This determines how long an application may delay before losing data.

    
                                              
           
        mAudioRecord = AudioRecordNew(sampleRate, fifoDepth);
        if (mAudioRecord == 0) throw new IOException("AudioRecord constructor failed - busy?");
        int status = AudioRecordStart(mAudioRecord);
        if (status != 0) {
            close();
            throw new IOException("AudioRecord start failed: " + status);
        }
    
Methods Summary
private static native voidAudioRecordDelete(int audioRecord)

private static native intAudioRecordNew(int sampleRate, int fifoDepth)

private static native intAudioRecordRead(int audioRecord, byte[] b, int offset, int length)

private static native intAudioRecordStart(int audioRecord)

private static native voidAudioRecordStop(int audioRecord)

public voidclose()
Closes this stream.

        if (mAudioRecord != 0) {
            try {
                AudioRecordStop(mAudioRecord);
            } finally {
                try {
                    AudioRecordDelete(mAudioRecord);
                } finally {
                    mAudioRecord = 0;
                }
            }
        }
    
protected voidfinalize()

        if (mAudioRecord != 0) {
            close();
            throw new IOException("someone forgot to close MicrophoneInputStream");
        }
    
public intread()

        if (mAudioRecord == 0) throw new IllegalStateException("not open");
        int rtn = AudioRecordRead(mAudioRecord, mOneByte, 0, 1);
        return rtn == 1 ? ((int)mOneByte[0] & 0xff) : -1;
    
public intread(byte[] b)

        if (mAudioRecord == 0) throw new IllegalStateException("not open");
        return AudioRecordRead(mAudioRecord, b, 0, b.length);
    
public intread(byte[] b, int offset, int length)

        if (mAudioRecord == 0) throw new IllegalStateException("not open");
        // TODO: should we force all reads to be a multiple of the sample size?
        return AudioRecordRead(mAudioRecord, b, offset, length);