我正在尝试将正弦波写入立体声 32 位 wav 文件。我假设该值可以在 -2147483647 和 +2147483647 之间,这可能是错误的,因为我没有得到所需的结果。
//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,所以这让我有点困惑。除了文件的标题,我找不到任何关于实际数据的信息。那么有人可以描述一下这里发生了什么吗?
解决方案(感谢 Roman 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];