I am working on something involves reading audio data. I need to convert the audio data byte[] to double[] (and vice versa). I need the conversion to pass the signal through a low pass filter.
For converting form bytes to doubles i use the following code snippet:
// where data is the byte array.
ByteBuffer byteBuffer = ByteBuffer.wrap(data);
// make sure that the data is in Little endian order.
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
// every double will represent 2 bytes (16bit audio sample)
// so the double[] is half length of byte[]
double[] doubleData = new double[data.length / 2];
int i = 0;
while (byteBuffer.remaining() > 2) {
// read shorts (16bits) and cast them to doubles
short t = byteBuffer.getShort();
doubleData[i] = t;
doubleData[i] /= 32768.0;
i++;
}
I don know whether this is the best way or not especially as it give "Java out of heap space exception" with large of data bytes.
So to sum up:
- is there a better way to do the conversion, that doesn't consume heap space ?
- how convert back the doubles to bytes again ?
Any help appreciated
Thanks,
Samer Samy