gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:29:36 25 4
gpt4 key购买 nike

我有一个正在练习相位声码的声音文件。我已将文件的字节转换为 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

关于Java如何将包含多个 double 的byte []转换为double [],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21583170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com