-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwavstreamer.cpp
349 lines (306 loc) · 9.61 KB
/
wavstreamer.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/********************************************************************
Wav streamer rev3.
Code by N.A. Moseley
Copyright 2006-2007
Note: only stereo files using PCM or IEEE floats are supported!
supports 16,24,32 bit PCM and 32 bit float.
stereo only!!!
License: GPLv2
rev3: first working version
rev4: converted to unicode filenames & wxwidgets FileStream
********************************************************************/
#include <QFile>
#include "wavstreamer.h"
#define TEMPBUFFERSIZE 65536
WavStreamer::WavStreamer() :
m_waveStream(NULL),
sampleIndex(0),
tempBuffer(NULL),
m_isOK(false)
{
}
WavStreamer::~WavStreamer()
{
if (m_waveStream != NULL)
delete m_waveStream; // destructor automatically closes the file.
if (tempBuffer != NULL)
delete[] static_cast<char*>(tempBuffer);
}
int32_t WavStreamer::openFile(const QString &filename)
{
if (m_waveStream != 0)
{
delete m_waveStream;
m_waveStream = 0;
}
if (m_file.isOpen())
{
m_file.close();
}
m_isOK = false;
// try to open the file
m_file.setFileName(filename);
if (m_file.open(QIODevice::ReadOnly) == false)
{
return -2; // error opening file
}
m_waveStream = new QDataStream(&m_file);
// check for RIFF file & size
size_t bytes = m_waveStream->readRawData(m_chunkType, 4);
if ((bytes != 4) || (strncmp(m_chunkType, "RIFF", 4)!=0))
{
delete m_waveStream;
m_waveStream = NULL;
m_file.close();
return -1;
}
bytes = m_waveStream->readRawData((char*)&m_chunkSize, 4);
if (bytes != 4)
{
delete m_waveStream;
m_waveStream = NULL;
m_file.close();
return -1;
}
bytes = m_waveStream->readRawData((char*)&m_chunkType, 4);
if ((bytes != 4) && (strncmp(m_chunkType, "WAVE", 4)!=0))
{
delete m_waveStream;
m_waveStream = NULL;
m_file.close();
return -1;
}
m_riffSize = m_chunkSize + 8; // size of RIFF chunk + 4-byte ID + 4-byte chunksize.
if (!findChunk("fmt "))
{
// error, format chunk not found!
delete m_waveStream;
m_waveStream = NULL;
m_file.close();
return -1;
}
// now, read the format chunk
bytes = m_waveStream->readRawData((char*)&m_waveFormat, sizeof(WavFormatChunk));
if (m_waveFormat.wFormatTag == 65534) // WAVE_FORMAT_EXTENSIBLE case..
{
m_waveFormat.wFormatTag = 1; // treat as PCM data, which should be the same when we have only 2 channels.
}
if (((m_waveFormat.wFormatTag != 3) && (m_waveFormat.wFormatTag != 1)) || (m_waveFormat.wChannels != 2))
{
delete m_waveStream;
m_waveStream = NULL;
m_file.close();
return -1; // wrong format!
}
// formats smaller than 16 bits are not supported!
if (m_waveFormat.wBitsPerSample < 16)
{
delete m_waveStream;
m_waveStream = NULL;
m_file.close();
return -1;
}
// if there are additional bytes in the format chunk, skip them.
// important in the WAVE_FORMAT_EXTENSIBLE case!
if (sizeof(WavFormatChunk) != m_chunkSize)
{
m_waveStream->skipRawData(m_chunkSize - sizeof(WavFormatChunk));
}
// now, search for the audio data and set the file offset pointers
if (!findChunk("data"))
{
delete m_waveStream;
m_waveStream = NULL;
m_file.close();
return -1;
}
m_playStart = m_waveStream->device()->pos();
m_playOffset = m_playStart;
m_playEnd = m_playStart + m_chunkSize;
// allocate the correct temporary buffer.
if (tempBuffer!=NULL) delete[] static_cast<char*>(tempBuffer);
tempBuffer = static_cast<void*>(new char[m_waveFormat.wBitsPerSample*TEMPBUFFERSIZE*2/8]);
// don't forget to actually read the data
readRawData(TEMPBUFFERSIZE);
sampleIndex = 0;
m_filename = filename;
m_isOK = true;
return 0;
}
bool WavStreamer::findChunk(const char ID[4])
{
if (m_waveStream == 0)
{
return false;
}
size_t bytesRead = m_waveStream->device()->pos();
m_chunkSize = 0;
// iterate until we get a format chunk..
while ((m_riffSize > bytesRead) && (strncmp(m_chunkType, ID, 4)!=0))
{
// skip the size of the chunk (except for the first RIFF chunk!)
m_waveStream->skipRawData(m_chunkSize);
bytesRead += m_chunkSize;
// read type of next chunk
size_t bytes = m_waveStream->readRawData(m_chunkType, 4);
bytesRead += bytes;
bytes = m_waveStream->readRawData((char*)&m_chunkSize, 4);
bytesRead += bytes;
}
if (m_riffSize < bytesRead)
{
return false;
}
return true;
}
uint32_t WavStreamer::readRawData(uint32_t requestedSamples)
{
if (m_waveStream == 0)
{
return 0;
}
uint32_t bytes_per_sample;
switch(m_waveFormat.wBitsPerSample)
{
case 16:
bytes_per_sample = 2;
break;
case 24:
bytes_per_sample = 3;
break;
case 32:
bytes_per_sample = 4;
break;
default:
return -1; // error, wrong format!
}
int bytes_to_read = requestedSamples * 2 * bytes_per_sample; // RequestedSamples is in 'stereo', so 2 real samples.
int offset = 0;
while(bytes_to_read>0)
{
if (bytes_to_read > (m_playEnd - m_playOffset)) // check for wrap-around
{
int readAmount = (m_playEnd - m_playOffset);
uint32_t bytesRead = m_waveStream->readRawData(static_cast<char*>(tempBuffer) + offset, readAmount);
if (bytesRead != readAmount)
{
// FIXME: file read error
m_isOK = false;
return 0;
}
m_waveStream->device()->seek(m_playStart);
offset += readAmount;
m_playOffset = m_playStart;
bytes_to_read -= readAmount;
}
else
{
uint32_t bytesRead = m_waveStream->readRawData(static_cast<char*>(tempBuffer) + offset, bytes_to_read);
if (bytesRead != bytes_to_read)
{
// FIXME: file read error
m_isOK = false;
return 0;
}
m_playOffset += bytes_to_read;
bytes_to_read = 0;
}
}
sampleIndex = 0;
return requestedSamples;
}
void WavStreamer::fillBuffer(float *stereoBuffer, uint32_t stereoSamples)
{
if (m_waveStream==NULL)
{
// clear the buffer if we have no file to read
// Note: this is compatible with the IEEE 756 floating-point format!
memset(stereoBuffer, 0, sizeof(float)*2*stereoSamples);
return;
}
else
{
int monoSamples = stereoSamples * 2;
if (m_waveFormat.wFormatTag == 1) // PCM
{
switch(m_waveFormat.wBitsPerSample)
{
case 16:
{
int16_t *ptr = static_cast<short*>(tempBuffer);
for(int i=0; i<monoSamples; i++)
{
if (sampleIndex > (TEMPBUFFERSIZE*2-1)) readRawData(TEMPBUFFERSIZE);
stereoBuffer[i] = static_cast<float>(ptr[sampleIndex]) / 32768.0f;
sampleIndex++;
}
}
break;
case 24:
{
uint8_t *ptr = static_cast<uint8_t*>(tempBuffer);
long v = 0;
int index = sampleIndex * 3;
for(int i=0; i<monoSamples; i++)
{
if (sampleIndex > (TEMPBUFFERSIZE*2-1))
{
readRawData(TEMPBUFFERSIZE);
index = 0;
}
v = static_cast<uint32_t>(ptr[index]) << 8;
v |= static_cast<uint32_t>(ptr[index+1]) << 16;
uint8_t top = static_cast<uint32_t>(ptr[index+2]);
v |= (top << 24);
if (top >= 0x80)
{
// signed!
v |= 0xFF000000;
}
index += 3;
stereoBuffer[i] = static_cast<float>(v) / 2147483648.0f;
sampleIndex++;
}
}
break;
case 32:
{
int32_t *ptr = static_cast<int*>(tempBuffer);
for(int i=0; i<monoSamples; i++)
{
if (sampleIndex > (TEMPBUFFERSIZE*2-1))
{
readRawData(TEMPBUFFERSIZE);
}
stereoBuffer[i] = static_cast<float>(ptr[sampleIndex]) / 2147483648.0f;
sampleIndex++;
}
}
break;
}
}
else if (m_waveFormat.wFormatTag == 3)
{
// uncompressed 32-bit float data
float *ptr = static_cast<float*>(tempBuffer);
for(int i=0; i<monoSamples; i++)
{
if (sampleIndex > (TEMPBUFFERSIZE*2-1))
{
readRawData(TEMPBUFFERSIZE);
}
stereoBuffer[i] = ptr[sampleIndex]; // FIXME: scaling??
sampleIndex++;
}
}
}
}
bool WavStreamer::getFormat(WavFormatChunk &output) const
{
if (m_waveStream == NULL)
{
return false;
}
output = m_waveFormat;
return true;
}