There is no alternative. You must start the loop and discard each element of the array separately.
I do the same for shorts that 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 have 16-bit PCM, but it's like a byte [], 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));
If you are working with a strange and poorly designed class that first gave you an array of bytes, the developers of this class should have packed the bytes to match how Java converts bytes (2 at a time) to shorts.
mwengler
source share