java - 将字节数组转换为 double 组

标签 java arrays floating-point fft wav

我在使用 Java 处理 WAV 文件时遇到了一些问题。

WAV 格式:PCM_SIGNED 44100.0 Hz,24 位,立体声,6 字节/帧,小端。

  • 我毫无问题地将 WAV 数据提取到字节数组。
  • 我正在尝试将字节数组转换为 double 组,但一些 double 组带有 NaN 值。

代码:

ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
double[] doubles = new double[byteArray.length / 8];
for (int i = 0; i < doubles.length; i++) {
    doubles[i] = byteBuffer.getDouble(i * 8);
}

16/24/32 位、单声道/立体声的事实让我感到困惑。

我打算将 double[] 传递给 FFT 算法并获取音频频率。

最佳答案

试试这个:

public static byte[] toByteArray(double[] doubleArray){
    int times = Double.SIZE / Byte.SIZE;
    byte[] bytes = new byte[doubleArray.length * times];
    for(int i=0;i<doubleArray.length;i++){
        ByteBuffer.wrap(bytes, i*times, times).putDouble(doubleArray[i]);
    }
    return bytes;
}

public static double[] toDoubleArray(byte[] byteArray){
    int times = Double.SIZE / Byte.SIZE;
    double[] doubles = new double[byteArray.length / times];
    for(int i=0;i<doubles.length;i++){
        doubles[i] = ByteBuffer.wrap(byteArray, i*times, times).getDouble();
    }
    return doubles;
}

public static byte[] toByteArray(int[] intArray){
    int times = Integer.SIZE / Byte.SIZE;
    byte[] bytes = new byte[intArray.length * times];
    for(int i=0;i<intArray.length;i++){
        ByteBuffer.wrap(bytes, i*times, times).putInt(intArray[i]);
    }
    return bytes;
}

public static int[] toIntArray(byte[] byteArray){
    int times = Integer.SIZE / Byte.SIZE;
    int[] ints = new int[byteArray.length / times];
    for(int i=0;i<ints.length;i++){
        ints[i] = ByteBuffer.wrap(byteArray, i*times, times).getInt();
    }
    return ints;
}

关于java - 将字节数组转换为 double 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15533854/

相关文章:

java - 将数据库保存到文本文件

c++ - 如何检查字符数组中是否存在字符串值?

algorithm - 通过下溢收敛到零

c - 为什么 printf ("%d",1/0.0) 的输出是 0?

c - C语言中的大数

java - 停止鼠标移动

java - 如何使 JTabbedPane 不捕获 Ctrl+Tab 键绑定(bind)?

java - 在 GWT 中下载时使用 servlet 发送字符串

c++ - 动态数组中的数组循环终止

C数组的问题,