更新时间:2023-11-13 11:03:58
音频文件是 16 位 PCM 签名的,采样率 = 44100,帧大小 = 2,帧长度 = 114048.
The audio file is 16 bit PCM signed, with samplerate = 44100, framesize = 2, framelength= 114048.
我从上面假设您只有一个通道(2 字节样本 * 1 通道 = 2 字节帧).
I'm assuming from the above that you have only a single channel (2 byte samples * 1 channel = 2 byte frames).
第一步是将数据作为16位整数类型的序列获取,在Java中是short
.
First step is to get the data as a sequence of a 16-bit integral type, which is short
in Java.
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;
...
byte[] audioBytes = ...
ShortBuffer sbuf =
ByteBuffer.wrap(audioBytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
short[] audioShorts = new short[sbuf.capacity()];
sbuf.get(audioShorts);
现在如何将其转换为 float
s 取决于下游函数期望音频如何表示.例如,如果他们期望浮点数 >= -1 和 <= 1,那么您可以这样做:
Now how you convert that to float
s depends on how downstream functions expect the audio to be represented. For example if they expect floating point numbers >= -1 and <= 1, then you can do this:
float[] audioFloats = new float[audioShorts.length];
for (int i = 0; i < audioShorts.length; i++) {
audioFloats[i] = ((float)audioShorts[i])/0x8000;
}
不幸的是,有很多方法可以表示音频.
Unfortunately there are a lot of ways to represent audio.