How to convert 16-bit PCM audio byte-array to double or float array?

≯℡__Kan透↙ 提交于 2019-12-18 15:53:42

问题


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.

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:

Android audio FFT to retrieve specific frequency magnitude using audiorecord

But I'm unsure as to wether or not this is the correct procedure. Anyone with any insight?


回答1:


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;
}

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;
}

then

float[] pcmAsFloats = floatMe(shortMe(bytes));

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.




回答2:


byte[] yourInitialData;
double[] yourOutputData = ByteBuffer.wrap(bytes).getDouble()


来源:https://stackoverflow.com/questions/10324355/how-to-convert-16-bit-pcm-audio-byte-array-to-double-or-float-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!