(android) Android 8k 8bit mono PCM

有个场景需要用 Android 手机采样 8k 8bit 单声道原始 PCM 流,但是 Android API 不支持 8bit,只能通过算法由 16bit 手动转换。找了一些转换方法,但都不灵光,大伙帮忙想点办法吧~

采样大致的样子:

private static final int SAMPLERATE = 8000;
private static final int DSAMPLERATE = 16000;
private static final int SOURCE = MediaRecorder.AudioSource.MIC;
private static final int CHANNEL = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private static final int STREAM_TYPE = AudioManager.STREAM_MUSIC;
private static final int TRACK_MODE = AudioTrack.MODE_STREAM; // 16bit 采样转换成 8bit,比特率会减半,故在采样时用两倍的采样率
int rate = 8 == bit ? DSAMPLERATE : SAMPLERATE;
minSize = AudioRecord.getMinBufferSizerate, CHANNEL, AudioFormat.ENCODING_PCM_16BIT; if minSize > 128 { AudioRecord rec = new AudioRecordSOURCE, rate, CHANNEL, AudioFormat.ENCODING_PCM_16BIT, minSize; rec.startRecording; while isRec { rec.readdata, 0, block; // balabala } rec.stop; rec.release;
}

转换调用:

for int i = 0; i < out.length; i++ { out[i] = byte data[i * 2 + 1] - 0x80;
}

播放:

AudioTrack track = new AudioTrackSTREAM_TYPE, SAMPLERATE, CHANNEL, 16 == bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT, minSize, TRACK_MODE;
track.play;
while... { track.writedata, offset, length;
}
track.stop;
track.release;

如果 bit 是 16,采样和播放都是没有问题的,换成 8 播放会是一片机械杂音,不知道肿么办鸟。可以确认的是,Android 采样的数据是小端的字节,所以让后面的字节在高位。

已解决,详见上面的代码吧~

发表评论

电子邮件地址不会被公开。 必填项已用*标注