1 ///
2 /// 写wav文件头信息
3 ///
4 ///
5 ///
6 public void WriteWavHeader(Stream stream,
int sampleRate)
7 {
8 const int bitsPerSample =
16;
9 const int bytesPerSample = bitsPerSample /
8;
10 var encoding = System.Text.Encoding.UTF8;
11 12 // ChunkID Contains the letters "RIFF" in ASCII form (0x52494646 big-endian form).
13 stream.Write(encoding.GetBytes(
"RIFF"),
0,
4);
14 15 // NOTE this will be filled in later
16 stream.Write(BitConverter.GetBytes(
0),
0,
4);
17 18 // Format Contains the letters "WAVE"(0x57415645 big-endian form).
19 stream.Write(encoding.GetBytes(
"WAVE"),
0,
4);
20 21 // Subchunk1ID Contains the letters "fmt " (0x666d7420 big-endian form).
22 stream.Write(encoding.GetBytes(
"fmt "),
0,
4);
23 24 // Subchunk1Size 16 for PCM. This is the size of therest of the Subchunk which follows this number.
25 stream.Write(BitConverter.GetBytes(
16),
0,
4);
26 27 // AudioFormat PCM = 1 (i.e. Linear quantization) Values other than 1 indicate some form of compression.
28 stream.Write(BitConverter.GetBytes((
short)
1),
0,
2);
29 30 // NumChannels Mono = 1, Stereo = 2, etc.
31 stream.Write(BitConverter.GetBytes((
short)
1),
0,
2);
32 33 // SampleRate 8000, 44100, etc.
34 stream.Write(BitConverter.GetBytes(sampleRate),
0,
4);
35 36 // ByteRate = SampleRate * NumChannels * BitsPerSample/8
37 stream.Write(BitConverter.GetBytes(sampleRate * bytesPerSample),
0,
4);
38 39 // BlockAlign NumChannels * BitsPerSample/8 The number of bytes for one sample including all channels.
40 stream.Write(BitConverter.GetBytes((
short)(bytesPerSample)),
0,
2);
41 42 // BitsPerSample 8 bits = 8, 16 bits = 16, etc.
43 stream.Write(BitConverter.GetBytes((
short)(bitsPerSample)),
0,
2);
44 45 // Subchunk2ID Contains the letters "data" (0x64617461 big-endian form).
46 stream.Write(encoding.GetBytes(
"data"),
0,
4);
47 48 // NOTE to be filled in later
49 stream.Write(BitConverter.GetBytes(
0),
0,
4);
50 }