-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
WavFileHeader.cs
282 lines (259 loc) · 8.63 KB
/
WavFileHeader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Text;
namespace MicrophoneIn
{
/// <summary>
/// This class can be used to parse / create a WAV file header.
/// </summary>
/// <remarks>
/// For WAV header specification see <see href="https://docs.fileformat.com/audio/wav/" />.
/// </remarks>
public class WavFileHeader
{
private readonly byte[] _header;
/// <summary>
/// Creates a new instance of <see cref="WavFileHeader" /> based on the provided header bytes.
/// </summary>
/// <param name="header">The header bytes to use as backing for all supported header properties.</param>
/// <exception cref="ArgumentException">Throws if the header array does not contain exactly 44 bytes.</exception>
public WavFileHeader(byte[] header)
{
if (header == null || header.Length != 44)
{
throw new ArgumentException("Only WAV file headers with 44 bytes are supported.");
}
_header = header;
}
public WavFileHeader()
{
_header = new byte[44];
RiffChunkId = "RIFF";
WaveFormat = "WAVE";
FormatChunkId = "fmt ";
DataChunkId = "data";
}
/// <summary>
/// Marks the file as a riff file.
/// Characters are each 1 byte long.
/// </summary>
public string RiffChunkId
{
get => Encoding.UTF8.GetString(_header, 0, 4);
set
{
var bytes = Encoding.UTF8.GetBytes(value);
if (bytes.Length != 4)
{
throw new ArgumentException("RiffChunkId must be 4 bytes long");
}
Array.Copy(bytes, 0, _header, 0, 4);
}
}
/// <summary>
/// Size of the overall file - 8 bytes, in bytes (32-bit integer).
/// Typically, you’d fill this in after creation.
/// </summary>
public int FileSize
{
get => BitConverter.ToInt32(_header, 4);
set
{
var bytes = BitConverter.GetBytes(value);
Array.Copy(bytes, 0, _header, 4, 4);
}
}
/// <summary>
/// File Type Header.
/// For our purposes, it must always equal to “WAVE”.
/// </summary>
public string WaveFormat
{
get => Encoding.UTF8.GetString(_header, 8, 4);
set
{
var bytes = Encoding.UTF8.GetBytes(value);
if (bytes.Length != 4)
{
throw new ArgumentException("WaveFormat must be 4 bytes long");
}
Array.Copy(bytes, 0, _header, 8, 4);
}
}
/// <summary>
/// Format chunk marker (4 characters).
/// Includes trailing <see cref="string.Empty" />.
/// </summary>
public string FormatChunkId
{
get => Encoding.UTF8.GetString(_header, 12, 4);
set
{
var bytes = Encoding.UTF8.GetBytes(value);
if (bytes.Length != 4)
{
throw new ArgumentException("FormatChunkId must be 4 bytes long");
}
Array.Copy(bytes, 0, _header, 12, 4);
}
}
/// <summary>
/// Length of format data.
/// </summary>
public int FormatChunkSize
{
get => BitConverter.ToInt32(_header, 16);
set
{
var bytes = BitConverter.GetBytes(value);
Array.Copy(bytes, 0, _header, 16, 4);
}
}
/// <summary>
/// AudioFormat: Indicates how the sample data for the wave file is stored.
/// The most common format is integer PCM, which has a code of 1.
/// <para>
/// Other formats include:
/// <list type="bullet">
/// <item><term>2</term><description>ADPCM</description></item>
/// <item><term>3</term><description>floating point PCM</description></item>
/// <item><term>6</term><description>A-law</description></item>
/// <item><term>7</term><description>μ-law</description></item>
/// <item><term>65534</term><description>WaveFormatExtensible</description></item>
/// </list>
/// </para>
/// </summary>
public short AudioFormat
{
get => BitConverter.ToInt16(_header, 20);
set
{
var bytes = BitConverter.GetBytes(value);
Array.Copy(bytes, 0, _header, 20, 2);
}
}
/// <summary>
/// Number of channels: Typically a file will have 1 channel (mono) or 2 channels (stereo).
/// A 5.1 surround sound file will have 6 channels.
/// </summary>
public short NumberOfChannels
{
get => BitConverter.ToInt16(_header, 22);
set
{
var bytes = BitConverter.GetBytes(value);
Array.Copy(bytes, 0, _header, 22, 2);
}
}
/// <summary>
/// Sample Rate: The number of sample frames that occur each second.
/// A typical value would be 44100, which is the same as an audio CD.
/// </summary>
public int SampleRate
{
get => BitConverter.ToInt32(_header, 24);
set
{
var bytes = BitConverter.GetBytes(value);
Array.Copy(bytes, 0, _header, 24, 4);
}
}
/// <summary>
/// Bytes per second
/// <para>
/// The spec calls this byte rate, which means the number of bytes required for one second of audio data. This is
/// equal to the bytes per sample frame times the sample rate.
/// </para>
/// <para>
/// So with a bytes per sample frame of 4, and a sample
/// rate of 44100, this should equal 176400.
/// </para>
/// </summary>
public int BytesPerSecond
{
get => BitConverter.ToInt32(_header, 28);
set
{
var bytes = BitConverter.GetBytes(value);
Array.Copy(bytes, 0, _header, 28, 4);
}
}
/// <summary>
/// Bytes per sample frame
/// <para>
/// Called block align by the spec, this is the number of
/// bytes required to store a single sample frame,
/// i.e. a single sample for each channel.
/// </para>
/// </summary>
public short BytesPerSampleFrame
{
get => BitConverter.ToInt16(_header, 32);
set
{
var bytes = BitConverter.GetBytes(value);
Array.Copy(bytes, 0, _header, 32, 2);
}
}
/// <summary>
/// Bits per sample
/// <para>
/// For integer PCM data, typical values will be 8, 16, or 32.
/// </para>
/// </summary>
public short BitsPerSample
{
get => BitConverter.ToInt16(_header, 34);
set
{
var bytes = BitConverter.GetBytes(value);
Array.Copy(bytes, 0, _header, 32, 2);
}
}
/// <summary>
/// Marks the start of the data chunk.
/// <para>
/// Should always be "data" for PCM WAV files.
/// </para>
/// </summary>
public string DataChunkId
{
get => Encoding.UTF8.GetString(_header, 36, 4);
set
{
var bytes = Encoding.UTF8.GetBytes(value);
if (bytes.Length != 4)
{
throw new ArgumentException("DataChunkId must be 4 bytes long");
}
Array.Copy(bytes, 0, _header, 36, 4);
}
}
/// <summary>
/// Data chunk size
/// <para>
/// The size of the chunk data.
/// </para>
/// </summary>
public int DataChunkSize
{
get => BitConverter.ToInt32(_header, 40);
set
{
var bytes = BitConverter.GetBytes(value);
Array.Copy(bytes, 0, _header, 40, 4);
}
}
/// <summary>
/// Gets the WAV file header as byte[].
/// </summary>
/// <returns>The header byte[].</returns>
public byte[] GetHeaderData()
{
return _header;
}
}
}