繁体   English   中英

Java如何将包含多个双精度的byte []转换为double []

[英]Java how do I convert a byte[] containing multiple doubles into a double[]

我有一个声音文件,我正在练习相位声码。 我已经将文件的字节转换为double []并通过快速傅里叶变换和快速傅立叶逆变换操纵该文件的波形。 问题是现在我需要将byte []转换回double。 以下是一些有用的代码片段:

我是如何在第一时间转换数据的:

/**
 * Converts bytes from a TargetDataLine into a double[] allowing the information to be read.
 * NOTE: One byte is lost in the conversion so don't expect the arrays to be the same length!
 * @param bufferData The buffer read in from the target Data Line
 * @return the double[] that the buffer has been converted into.
 */
   private static double[] bytesToDoubleArray(byte[] bufferData){
    final int bytesRecorded = bufferData.length;
    final int bytesPerSample = getAudioFormat().getSampleSizeInBits()/8; 
    final double amplification = 100.0; // choose a number as you like
    double[] micBufferData = new double[bytesRecorded - bytesPerSample + 1];
    for (int index = 0, floatIndex = 0; index < bytesRecorded - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {
        double sample = 0;
        for (int b = 0; b < bytesPerSample; b++) {
            int v = bufferData[index + b];
            if (b < bytesPerSample - 1 || bytesPerSample == 1) {
                v &= 0xFF;
            }
            sample += v << (b * 8);
        }
        double sample32 = amplification * (sample / 32768.0);
        micBufferData[floatIndex] = sample32;

    }
    return micBufferData;
}

以及我对数据做了什么:

public static byte[] shift(byte[] data, int factor){
    double[] audioData = bytesToDoubleArray(data);
    audioData = Arrays.copyOf(audioData, roundToPowerOf2(audioData.length));
    Complex[] transformed = FFT.fft(doubleToComplex(audioData));
    transformed = shiftArray(transformed, 3);
    Complex[] reverted = FFT.ifft(transformed);
    for(int i = 0; i<reverted.length; i++){
        audioData[i] = reverted[i].re();
    }
    return null;//How do I convert audioData[] back into a byte[]????
}

关于如何解决这个问题的任何想法? 任何解决方案将不胜感激。 此外,任何已经实现相位声码的Java库也都很棒。

大约在这里。 我确定我搞砸了。 scaleFactor可能是327.68,以扭转上面的缩放。 上面的代码似乎是大端。 是否需要fullNormalize由您决定。

public byte[] doubleArrayToByteArray(double[] input, int bytesPerSample, double scaleFactor, boolean fullNormalize, boolean bigEndian) {
    byte[] result = new byte[input.length * bytesPerSample];
    performNormalization(input, scaleFactor, fullNormalize);
    for (int i = 0; i < input.length; i++) {
        long sourceVal = (long)(input[i] * scaleFactor);
        sourceVal = sourceVal >> 8 * (8 - bytesPerSample);
        for (int j = 0; j < bytesPerSample; j++) {
            int index = i * bytesPerSample;
            if (bigEndian) {
                index += (bytesPerSample - j);
            }
            else {
                index += j;
            }
            result[index] = (byte) sourceVal;
            sourceVal = sourceVal >> 8;
        }
    }
    return result;
}

public void performNormalization(double[] input, double scaleFactor, boolean fullNormalize) {
    double maxVal = 0.0;
    for (int i = 0; i < input.length; i++) {
        double val = Math.abs(input[i]) * scaleFactor;
        if (val > maxVal) {
            maxVal = val;
        }
    }
    if (fullNormalize || maxVal > Long.MAX_VALUE) {
        double normalizeFactor = (double)(Long.MAX_VALUE) / maxVal;
        for (int i = 0; i < input.length; i++) {
            input[i] *= normalizeFactor;
        }
    }
}

更新:意识到我需要在规范化中考虑scaleFactor。 并且您通常不会同时指定不是1.0的scaleFactorfullNormalize = true

这取决于你将double []转换为byte []的含义。 回想一下,通常double为8字节长度,而byte为1字节长。

如果你可以将你的双重转换为无符号整数0-255(或签名-128-127),那么你只需要将每个double转换为byte并将其分配给一个新数组,否则你必须使用ByteBuffer:

int capacity = // calculate how much space you need for your byte array
ByteBuffer byteBuf = ByteBuffer(capacity);
for(double d : doubleArray) byteBuf.putDouble(d);
byte[] byteArray = byteBuf.array();

实际上,上述方法没有转换任何东西,只是你只是将字节流解释为字节序列而不是double的序列

就像从byte[]转换为double[]时所做的那样,现在需要转换回byte[] 只需创建一个新的byte[]数组并手动复制double[]数组中的数据:

private static byte[] doublesToByteArray(double[] a) {
    int len = a.length;
    byte[] b = new byte[len];
    for (int i = 0; i < len; i++)
        b[i] = (byte) a[i];
    return b;
}

这当然假设double[]数组中的数据可以存储在byte[]数组中而无需进一步转换/缩放。 否则,复制时需要注意这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM