我有一個聲音文件,我正在練習相位聲碼。我已將文件的字節轉換爲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的規範來完成。 –
但是你沒有轉換字節<=>雙倍,你有效地轉換字節<=>整數並將整數轉換爲雙精度。如果使用相同的方案將雙重結果轉換回字節,您將失去一些重要意義。 –