用Java读取24位mono-PCM
我正在努力理解24位mono PCM音频格式数据,并用Java读取这些数据 我理解并能读取RIFF头,但不知道如何读取24位PCM样本。我需要一个接一个地读取PCM样本。假设,这应该可以让您开始:用Java读取24位mono-PCM,java,wav,javasound,pcm,Java,Wav,Javasound,Pcm,我正在努力理解24位mono PCM音频格式数据,并用Java读取这些数据 我理解并能读取RIFF头,但不知道如何读取24位PCM样本。我需要一个接一个地读取PCM样本。假设,这应该可以让您开始: // constant holding the minimum value of a signed 24bit sample: -2^22. private static final int MIN_VALUE_24BIT = -2 << 22; // constant holding
// 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
//保存有符号24位样本的最小值的常数:-2^22。
private static final int MIN_VALUE_24BIT=-2 Java声音API应该能够阅读这篇文章,它有很多例子的链接。这很好。重要的一点是注释:“//打开您的AudioInputStream…”当设置为正确的格式时,不需要解码或解析RIFF头。您将处理的唯一数据是PCM。