正弦波をステレオ 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];