在Android上更改AudioRecord的比特率

时间:2014-05-19 10:59:48

标签: android wav audio-recording pcm bitrate

在以wav格式录制时,如何在Android上将每个样本的比特从AudioRecord对象中的16位更改为8位(或其他比特率)?

这不起作用(例如此处讨论:Using AudioRecord with 8-bit encoding in android):

private static int RECORDER_AUDIO_ENCODING = 
         AudioFormat.ENCODING_PCM_8BIT; -->ERROR: Invalid Audio Format.
bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,
         RECORDER_AUDIO_ENCODING);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDER_SAMPLERATE, 
         RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);

标题:

private void WriteWaveFileHeader(
        FileOutputStream out, long totalAudioLen,
        long totalDataLen, long longSampleRate, int channels,
        long byteRate) throws IOException {

        byte[] header = new byte[44];

        header[0] = 'R';  // RIFF/WAVE header
        ...header content...
        header[32] = (byte) (2 * 16 / 8);  // block align
        header[33] = 0;
        header[34] = (byte)RECORDER_BPP;  // bits per sample, set to 8
        ...header content...

        out.write(header, 0, 44);
    }

2 个答案:

答案 0 :(得分:2)

您需要使用AudioFormat.ENCODING_PCM_16BIT记录16位样本,因为8位编码通常不适用于Android设备。

然后将16位缓冲区动态转换为8位,然后再将其写入波形声音文件。但由于8位波形文件使用无符号整数(0-255),静音为128而不是0,我的建议转换校正了这一点。以下只是代码框架:

// Your source above already defined a value for bufferSize and the recorder object
sampleBufferShorts = new short[bufferSize];
sampleBufferBytes = new short[bufferSize];
int numberOfShortsRead = recorder.read(samleBufferShorts, 0, bufferSize);
if (numberOfShortsRead != ERROR_INVALID_OPERATION && numberOfShortsRead != ERROR_BAD_VALUE) {
    for (int i = 0 ; i < numberOfShortsRead ; ++i)
        sampleBufferBytes[i] = (byte)((sampleBufferShorts[i]>>8)+128);
}

请参阅https://stackoverflow.com/a/18777515/1986583了解有关为什么额外抖动会改善最终质量的有用解释。

答案 1 :(得分:1)

不可能,您必须对.wav文件进行后期处理。