-
Notifications
You must be signed in to change notification settings - Fork 1
/
wavfilereader.cpp
267 lines (218 loc) · 5.38 KB
/
wavfilereader.cpp
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
#include <fstream>
#include <iostream>
#include "wavfilereader.h"
#include "audioformats.h"
#define WAV_FORMAT_CODE_IEEE_FLOAT 0x0003
bool WavFileReader::ReadFormat(std::istream& inputStream, AudioInfo& outAudioInfo)
{
if (!CheckFirstFormatBlock(inputStream))
{
return false;
}
if (!CheckAudioFormatBlock(inputStream, outAudioInfo))
{
return false;
}
return true;
}
bool WavFileReader::ReadSamples(std::istream& inputStream, const AudioInfo& audioInfo, unsigned int nbSamplesToRead, float* outSamples, unsigned int& outNbSamplesRead)
{
if (!AudioInfo::CheckAudioInfo(audioInfo))
{
return false;
}
inputStream.read(reinterpret_cast<char*>(outSamples), nbSamplesToRead * (audioInfo.m_BitsPerSample / 8));
if (!inputStream && (inputStream.gcount() / (audioInfo.m_BitsPerSample / 8)) != audioInfo.m_NbSamples)
{
return false;
}
outNbSamplesRead = nbSamplesToRead;
return true;
}
bool WavFileReader::CheckFirstFormatBlock(std::istream& inputStream)
{
if (!inputStream)
{
return false;
}
// First, check for first 4 bytes "RIFF" (0x52, 0x49, 0x46, 0x46) in the input file
char riffBuff[5];
inputStream.read(riffBuff, 4);
riffBuff[4] = '\0';
if (strcmp(riffBuff, "RIFF"))
{
return false;
}
if (!inputStream)
{
return false;
}
// Then get the file size
unsigned int fileSize = 0;
inputStream.read(reinterpret_cast<char*>(&fileSize), 4);
if (!inputStream)
{
return false;
}
// Check the "WAVE" format
char fromatIDBuff[5];
inputStream.read(fromatIDBuff, 4);
fromatIDBuff[4] = '\0';
if (strcmp(fromatIDBuff, "WAVE"))
{
return false;
}
return true;
}
bool WavFileReader::CheckAudioFormatBlock(std::istream& inputStream, AudioInfo& outAudioInfo)
{
if (!inputStream)
{
return false;
}
char fromatBlockIDBuff[5];
inputStream.read(fromatBlockIDBuff, 4);
fromatBlockIDBuff[4] = '\0';
if (strcmp(fromatBlockIDBuff, "fmt "))
{
return false;
}
if (!inputStream)
{
return false;
}
unsigned int blockSize = 0;
inputStream.read(reinterpret_cast<char*>(&blockSize), 4);
if (blockSize != 0x10)
{
return false;
}
if (!inputStream)
{
return false;
}
unsigned short audioFormat = 0;
inputStream.read(reinterpret_cast<char*>(&audioFormat), 2);
if (audioFormat != WAV_FORMAT_CODE_IEEE_FLOAT)
{
// we currently support only floating point samples in our
// beat detection code
return false;
}
if (!inputStream)
{
return false;
}
inputStream.read(reinterpret_cast<char*>(&outAudioInfo.m_NumChannels), 2);
if (outAudioInfo.m_NumChannels == 0)
{
return false;
}
if (!inputStream)
{
return false;
}
inputStream.read(reinterpret_cast<char*>(&outAudioInfo.m_SampleRate), 4);
if (outAudioInfo.m_SampleRate == 0)
{
return false;
}
if (!inputStream)
{
return false;
}
unsigned int bytesPerSec = 0;
inputStream.read(reinterpret_cast<char*>(&bytesPerSec), 4);
if (bytesPerSec == 0)
{
return false;
}
if (!inputStream)
{
return false;
}
unsigned short bytesPerBlock = 0;
inputStream.read(reinterpret_cast<char*>(&bytesPerBlock), 2);
if (bytesPerBlock == 0)
{
return false;
}
if (!inputStream)
{
return false;
}
inputStream.read(reinterpret_cast<char*>(&outAudioInfo.m_BitsPerSample), 2);
if (outAudioInfo.m_BitsPerSample != 8 &&
outAudioInfo.m_BitsPerSample != 16 &&
outAudioInfo.m_BitsPerSample != 24 &&
outAudioInfo.m_BitsPerSample != 32)
{
return false;
}
if (!inputStream)
{
return false;
}
char factIDBuff[5];
inputStream.read(factIDBuff, 4);
factIDBuff[4] = '\0';
if (strcmp(factIDBuff, "fact"))
{
return false;
}
if (!inputStream)
{
return false;
}
unsigned int chunkSize = 0;
inputStream.read(reinterpret_cast<char*>(&chunkSize), 4);
if (chunkSize < 4)
{
return false;
}
if (!inputStream)
{
return false;
}
unsigned int nbSamplesPerChannel = 0;
inputStream.read(reinterpret_cast<char*>(&nbSamplesPerChannel), 4);
if (nbSamplesPerChannel == 0)
{
return false;
}
if (!inputStream)
{
return false;
}
// Skipping bytes until we get to the start of the "data" ID,
// where data starts. We don't care about the rest of the metadata for now
char c = inputStream.peek();
while (inputStream && c != 'd')
{
inputStream.ignore();
c = inputStream.peek();
}
if (!inputStream)
{
return false;
}
char dataIDBuff[5];
inputStream.read(dataIDBuff, 4);
dataIDBuff[4] = '\0';
if (strcmp(dataIDBuff, "data"))
{
return false;
}
if (!inputStream)
{
return false;
}
unsigned int dataSize = 0;
inputStream.read(reinterpret_cast<char*>(&dataSize), 4);
if (dataSize == 0)
{
return false;
}
outAudioInfo.m_NbSamples = dataSize / (outAudioInfo.m_NumChannels * (outAudioInfo.m_BitsPerSample / 8));
return true;
}