如何将16位PCM音频字节数组转换为双精度或浮点型数组?
我正在尝试对.3gpp音频文件执行快速傅立叶变换.该文件包含一个5秒钟的小录音,该录音来自手机麦克风,频率为44100kHz.
I'm trying to perform Fast Fourier Transform on a .3gpp audio file. The file contains a small 5 second recording in 44100kHz from the phones microphone.
出于明显的原因,我可以找到的每个Java FFT算法都只采用double [],float []或Complex []输入,但是我以字节数组的形式读取音频文件,所以我有点关于我从这里去哪里感到困惑.我唯一能找到的是上一个问题的答案:
Every Java FFT algorithm I can find only takes double[], float[] or Complex[] inputs, for obvious reasons, but I'm reading in the audio file in a byte-array, so I'm kind of confused as to where I go from here. The only thing I could find is the answer to a previous question:
但是我不确定是否正确.任何有见识的人吗?
But I'm unsure as to wether or not this is the correct procedure. Anyone with any insight?
别无选择.您必须运行循环并分别转换数组的每个元素.
There is no alternative. You have to run a loop and cast each element of the array separately.
我为漂浮的短裤做同样的事情:
I do the same thing for shorts that I fft as floats:
public static float[] floatMe(short[] pcms) {
float[] floaters = new float[pcms.length];
for (int i = 0; i < pcms.length; i++) {
floaters[i] = pcms[i];
}
return floaters;
}
根据评论编辑4/26/2012
如果您确实有16位PCM但将其作为字节[],则可以执行以下操作:
EDIT 4/26/2012 based on comments
If you really do have 16 bit PCM but have it as a byte[], then you can do this:
public static short[] shortMe(byte[] bytes) {
short[] out = new short[bytes.length / 2]; // will drop last byte if odd number
ByteBuffer bb = ByteBuffer.wrap(bytes);
for (int i = 0; i < out.length; i++) {
out[i] = bb.getShort();
}
return out;
}
然后
float[] pcmAsFloats = floatMe(shortMe(bytes));
除非您使用的是一个怪异且设计不良的类,该类首先为您提供了字节数组,否则该类的设计人员应打包字节,以与Java转换字节的方式保持一致(一次转换2个)短裤.
Unless you are working with a weird and badly designed class that gave you the byte array in the first place, the designers of that class should have packed the bytes to be consistent with the way Java converts bytes (2 at a time) to shorts.