我正在努力理解24位单声道PCM音频格式数据并以Java读取此数据。
我理解并且可以读取RIFF标头,但无法弄清楚如何读取24位PCM样本。我需要一一阅读PCM样本。
答案 0 :(得分:1)
假设little-endian encoding,这应该使您入门:
// constant holding the minimum value of a signed 24bit sample: -2^22.
private static final int MIN_VALUE_24BIT = -2 << 22;
// constant holding the maximum value a signed 24bit sample can have, 2^(22-1).
private static final int MAX_VALUE_24BIT = -MIN_VALUE_24BIT-1;
[...]
// open your AudioInputStream using AudioSystem and read values into a buffer buf
[...]
final byte[] buf = ... ; // your audio byte buffer
final int bytesPerSample = 3; // because 24 / 8 = 3
// read one sample:
int sample = 0;
for (int byteIndex = 0; byteIndex < bytesPerSample; byteIndex++) {
final int aByte = buf[byteIndex] & 0xff;
sample += aByte << 8 * (byteIndex);
}
// now handle the sign / valid range
final int threeByteSample = sample > MAX_VALUE_24BIT
? sample + MIN_VALUE_24BIT + MIN_VALUE_24BIT
: sample;
// do something with your threeByteSample / read the next sample
有关PCM解码的更多常规处理,请参见jipes AudioSignalSource。