我正在研究某些涉及读取音频数据的内容。我需要将音频数据字节[]转换为double [](反之亦然)。 我需要通过低通滤波器转换信号。Java - 将byte []转换为double [],反之亦然
用于转换形式字节双打我用下面的代码片段:
// 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++;
}
我不知道这是否是最好的方式还是不特别,因为它给的“Java出堆空间异常”大的数据字节。
所以总结起来:
- 是有更好的方法来进行转换,不消耗堆空间?
- 如何将双打再次转换为字节?
任何帮助表示赞赏
感谢,
萨默尔萨米
似乎是http://stackoverflow.com/questions/2905556/how-can-i-convert-a-byte-array-into的副本 - 双后面 –
是的,我遇到了这个问题,但它没有提到任何有关堆问题。 –