1
我想寫一個正弦波的32位WAV文件。我認爲這個值可能在-2147483647和+2147483647之間,這可能是錯誤的,因爲我沒有得到所需的結果。32位的WAV文件元數據
//I add 8 to i because it is a stereo wav file and this way I'am able to modify only the left channel (or right)
for (int i = 0; i < capturedAudioBuffer.BytesRecorded; i=i+8)
{
//sin function gives a result between -1 and 1 therefore I convert it into the required range.
int sinval = (int)(2147483646 * ((System.Math.Sin(((i/8)/180.0f) * (double)System.Math.PI))));
byte[] b1 = new byte[4];
b1 = convertToByte(sinval);
capturedAudioBuffer.Buffer[i + 3] = b1[0];
capturedAudioBuffer.Buffer[i + 2] = b1[1];
capturedAudioBuffer.Buffer[i + 1] = b1[2];
capturedAudioBuffer.Buffer[i] = b1[3];
}
我創建了一個程序的正弦波,似乎最大值爲BF 80 00 00和最小值爲3F 80 00 00所以弄得我一點點。我找不到有關實際數據的信息,但是找不到文件頭。那麼有人能描述我這裏發生了什麼嗎?
溶液(感謝羅馬R.):
float sinval = (float)(((System.Math.Sin(((i/8)/180.0f) * (double)System.Math.PI))));
b1 = System.BitConverter.GetBytes(sinval);
capturedAudioBuffer.Buffer[i + 3] = b1[3];
capturedAudioBuffer.Buffer[i + 2] = b1[2];
capturedAudioBuffer.Buffer[i + 1] = b1[1];
capturedAudioBuffer.Buffer[i] = b1[0];
你怎麼寫wav頭? – JeffRSon
32位PCM格式也可以使用單精度浮點值「-1.0..1.0」。標題必須指定它是'WAVE_FORMAT_IEEE_FLOAT'。 –
它比你工作。 – DalekSupreme