我正在尝试将一个sin波写入立体声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];
}
我用程序创建了一个sin wave,似乎最大值是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];
答案 0 :(得分:1)
32位PCM格式也可以使用(可能更频繁)单精度浮点值,范围为-1.0..1.0
。
标题必须将其指定为WAVE_FORMAT_IEEE_FLOAT
,有关详细信息,请参阅Extensible Wave-Format Descriptors。