[英]Create sample array from 32bit sample size WAV
我得到一個WAV(32位樣本大小,每幀8字節,44100 Hz,PCM_Float),需要創建一個WAV樣本數組。 這是我用於Wav的代碼,具有16位樣本大小,每幀4個字節,44100 Hz,PCM_Signed。
private float[] getSampleArray(byte[] eightBitByteArray) {
int newArrayLength = eightBitByteArray.length
/ (2 * calculateNumberOfChannels()) + 1;
float[] toReturn = new float[newArrayLength];
int index = 0;
for (int t = 0; t + 4 < eightBitByteArray.length; t += 2) // t+2 -> skip
//2nd channel
{
int low=((int) eightBitByteArray[t++]) & 0x00ff;
int high=((int) eightBitByteArray[t++]) << 8;
double value = Math.pow(low+high, 2);
double dB = 0;
if (value != 0) {
dB = 20.0 * Math.log10(value); // calculate decibel
}
toReturn[index] = getFloatValue(dB); //minorly important conversion
//to normalized values
index++;
}
return toReturn;
}
顯然,此代碼不能用於32位樣本大小Wav,因為我必須在第一個通道中考慮另外2個字節。
有人知道如何將其他兩個字節相加(和移位)以計算振幅嗎? 不幸的是,谷歌根本沒有幫助我:/。
提前致謝。
這樣的事情應該可以解決問題。
for (int t = 0; t + 4 < eightBitByteArray.length; t += 4) // t+4 -> skip
//2nd channel
{
float value = ByteBuffer.wrap(eightBitByteArray, t, 4).order(ByteOrder.LITTLE_ENDIAN).getFloat();
double dB = 0;
if (value != 0) {
dB = 20.0 * Math.log10(value); // calculate decibel
}
toReturn[index] = getFloatValue(dB); //minorly important conversion
//to normalized values
index++;
}
另一方面,將瞬時采樣轉換為dB是沒有意義的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.