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
| public unsafe struct WaveHeader
{
private fixed byte _chunkId[4];
private uint _chunkSize;
private fixed byte _format[4];
private fixed byte _subChunk1Id[4];
private uint _subChunk1Size;
private WaveAudioFormat _audioFormat;
private ushort _numChannels;
private uint _sampleRate;
private uint _byteRate;
private ushort _blockAlign;
private ushort _bitsPerSample;
private fixed byte _subChunk2Id[4];
private uint _subChunk2Size;
public unsafe string ChunkId
{
get
{
fixed (byte* ptr = _chunkId)
{
return CreateString(ptr, 4);
}
}
set
{
fixed (byte* ptr = _chunkId)
{
FillString(ptr, value, 4);
}
}
}
public uint ChunkSize
{
get => _chunkSize;
set => _chunkSize = value;
}
public string Format
{
get
{
fixed (byte* ptr = _format)
{
return CreateString(ptr, 4);
}
}
set
{
fixed (byte* ptr = _format)
{
FillString(ptr, value, 4);
}
}
}
public string SubChunk1Id
{
get
{
fixed (byte* ptr = _subChunk1Id)
{
return CreateString(ptr, 4);
}
}
set
{
fixed (byte* ptr = _subChunk1Id)
{
FillString(ptr, value, 4);
}
}
}
public uint SubChunk1Size
{
get => _subChunk1Size;
set => _subChunk1Size = value;
}
public WaveAudioFormat AudioFormat
{
get => _audioFormat;
set => _audioFormat = value;
}
public ushort ChannelCount
{
get => _numChannels;
set => _numChannels = value;
}
public uint SampleRate
{
get => _sampleRate;
set => _sampleRate = value;
}
public uint ByteRate
{
get => _byteRate;
set => _byteRate = value;
}
public ushort BlockAlign
{
get => _blockAlign;
set => _blockAlign = value;
}
public ushort BitsPerSample
{
get => _bitsPerSample;
set => _bitsPerSample = value;
}
public string SubChunk2Id
{
get
{
fixed (byte* ptr = _subChunk2Id)
{
return CreateString(ptr, 4);
}
}
set
{
fixed (byte* ptr = _subChunk2Id)
{
FillString(ptr, value, 4);
}
}
}
public uint SubChunk2Size
{
get => _subChunk2Size;
set => _subChunk2Size = value;
}
public unsafe static WaveHeader Create(WaveAudioFormat audioFormat, ushort channelCount, uint sampleRate, ushort bitsPerSample, uint pcmDataSize)
{
return new WaveHeader()
{
ChunkId = "RIFF",
ChunkSize = (uint)(pcmDataSize + (sizeof(WaveHeader) - 8)),
Format = "WAVE",
SubChunk1Id = "fmt ",
SubChunk1Size = 16,
AudioFormat = audioFormat,
ChannelCount = channelCount,
SampleRate = sampleRate,
ByteRate = sampleRate * channelCount * bitsPerSample / 8,
BlockAlign = (ushort)(channelCount * bitsPerSample / 8),
BitsPerSample = bitsPerSample,
SubChunk2Id = "data",
SubChunk2Size = pcmDataSize
};
}
private static void FillString(byte* ptr, string value, int maxLength)
{
if (value.Length > maxLength)
{
throw new ArgumentException(nameof(value));
}
fixed (char* textPtr = value)
{
for (int i = 0; i < value.Length && i < maxLength; i++)
{
ptr[i] = (byte)textPtr[i];
}
}
}
private static string CreateString(byte* ptr, int maxLength)
{
StringBuilder sb = new StringBuilder(maxLength);
for (int i = 0; i < maxLength; i++)
{
if (ptr[i] == 0)
{
break;
}
sb.Append((char)ptr[i]);
}
return sb.ToString();
}
}
public enum WaveAudioFormat : ushort
{
None = 0,
PCM = 1,
}
|