【发布时间】:2011-08-31 16:31:50
【问题描述】:
我正在做一些涉及读取音频数据的事情。我需要将音频数据 byte[] 转换为 double[](反之亦然)。 我需要转换以使信号通过低通滤波器。
为了将表单字节转换为双精度,我使用以下代码 sn-p:
// 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 堆空间不足异常”以及大量数据字节。
总结一下:
- 有没有更好的方法来进行转换,不消耗堆空间?
- 如何再次将双精度转换回字节?
任何帮助表示赞赏
谢谢,
萨默·萨米
【问题讨论】:
-
是的,我遇到了这个问题,但它没有提到堆问题。