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.getMinBufferSize(rate, CHANNEL, AudioFormat.ENCODING_PCM_16BIT);

if (minSize > 128) {
AudioRecord rec = new AudioRecord(SOURCE, rate, CHANNEL,
AudioFormat.ENCODING_PCM_16BIT, minSize);
rec.startRecording();
while (isRec) {
rec.read(data, 0, block);
// balabala
}
rec.stop();
rec.release();
}

转换调用:

   
  for (int i = 0; i < out.length; i++) {
  
short origin = (short) (data[i * 2 + 1] << 8 | data[i * 2]);
out[i] = (byte) (origin >> 8 | 0x80);
}

播放:

   
  AudioTrack track = new AudioTrack(STREAM_TYPE, SAMPLERATE, CHANNEL,
  
((16 == bit) ? AudioFormat.ENCODING_PCM_16BIT
: AudioFormat.ENCODING_PCM_8BIT), minSize, TRACK_MODE);
track.play();
while(...) {
track.write(data, offset, length);
}
track.stop();
track.release();

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



相关链接

Android 音视频开发

星野ましろ 9 years, 11 months ago

Your Answer