我有一个声音文件,我正在练习相位声码。我已将文件的字节转换为double [],并通过快速傅里叶变换和快速傅立叶逆变换来处理该文件的波形。现在的问题是我需要将byte []转换回double。下面是一些有用的代码片段:Java如何将包含多个双精度的byte []转换为双精度[]
我如何在第一时间转换的数据:
/**
* 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库都会非常棒。
查找到'ByteBuffer'的序列,而不是序列。 –
您可以通过阅读Double的规范来完成。 –
但是你没有转换字节<=>双倍,你有效地转换字节<=>整数并将整数转换为双精度。如果使用相同的方案将双重结果转换回字节,您将失去一些重要意义。 –