-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathWaveReader.h
More file actions
256 lines (208 loc) · 6.75 KB
/
WaveReader.h
File metadata and controls
256 lines (208 loc) · 6.75 KB
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
/************************** BEGIN WaveReader.h **************************/
/************************************************************************
FAUST Architecture File
Copyright (C) 2020 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This Architecture section is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; If not, see <http://www.gnu.org/licenses/>.
EXCEPTION : As a special exception, you may create a larger work
that contains this FAUST architecture section and distribute
that work under terms of your choice, so long as this FAUST
architecture section is not modified.
************************************************************************/
#ifndef __WaveReader__
#define __WaveReader__
#undef min
#undef max
#include <string>
#include "Resource.h"
// WAVE file description
class WaveFile {
public:
// The canonical WAVE format starts with the RIFF header
/**
Variable: chunk_id
Contains the letters "RIFF" in ASCII form (0x52494646 big-endian form).
**/
int chunk_id;
/**
Variable: chunk_size
36 + SubChunk2Size, or more precisely: 4 + (8 + SubChunk1Size) + (8 + SubChunk2Size)
This is the size of the rest of the chunk following this number.
This is the size of the entire file in bytes minus 8 bytes for the
two fields not included in this count: ChunkID and ChunkSize.
**/
int chunk_size;
/**
Variable: format
Contains the letters "WAVE" (0x57415645 big-endian form).
**/
int format;
// The "WAVE" format consists of two subchunks: "fmt " and "data":
// The "fmt " subchunk describes the sound data's format:
/**
Variable: subchunk_1_id
Contains the letters "fmt " (0x666d7420 big-endian form).
**/
int subchunk_1_id;
/**
Variable: subchunk_1_size
16 for PCM. This is the size of the rest of the Subchunk which follows this number.
**/
int subchunk_1_size;
/**
Variable: audio_format
PCM = 1 (i.e. Linear quantization) Values other than 1 indicate some form of compression.
**/
short audio_format;
/**
Variable: num_channels
Mono = 1, Stereo = 2, etc.
**/
short num_channels;
/**
Variable: sample_rate
8000, 44100, etc.
**/
int sample_rate;
/**
Variable: byte_rate
== SampleRate * NumChannels * BitsPerSample/8
**/
int byte_rate;
/**
Variable: block_align
== NumChannels * BitsPerSample/8
The number of bytes for one sample including all channels. I wonder what happens
when this number isn't an integer?
**/
short block_align;
/**
Variable: bits_per_sample
8 bits = 8, 16 bits = 16, etc.
**/
short bits_per_sample;
/**
Here should come some extra parameters which i will avoid.
**/
// The "data" subchunk contains the size of the data and the actual sound:
/**
Variable: subchunk_2_id
Contains the letters "data" (0x64617461 big-endian form).
**/
int subchunk_2_id;
/**
Variable: subchunk_2_size
== NumSamples * NumChannels * BitsPerSample/8
This is the number of bytes in the data. You can also think of this as the size
of the read of the subchunk following this number.
**/
int subchunk_2_size;
/**
Variable: data
The actual sound data.
**/
char* data;
};
// Base reader
class WaveReader {
public:
WaveFile* fWave;
WaveReader() {
fWave = new WaveFile();
}
virtual ~WaveReader() {
if (fWave->data != NULL)
delete[] fWave->data;
delete fWave;
}
bool loadWaveHeader() {
char buffer[4];
read(buffer, 4);
if (strncmp(buffer, "RIFF", 4) != 0) {
debugMessage("Not a valid WAV file!");
return false;
}
fWave->chunk_id = toInt(buffer, 4);
read(buffer, 4);
fWave->chunk_size = toInt(buffer, 4);
read(buffer, 4);
fWave->format = toInt(buffer, 4);
read(buffer, 4);
fWave->subchunk_1_id = toInt(buffer, 4);
read(buffer, 4);
fWave->subchunk_1_size = toInt(buffer, 4);
read(buffer, 2);
fWave->audio_format = toInt(buffer, 2);
read(buffer, 2);
fWave->num_channels = toInt(buffer, 2);
read(buffer, 4);
fWave->sample_rate = toInt(buffer, 4);
read(buffer, 4);
fWave->byte_rate = toInt(buffer, 4);
read(buffer, 2);
fWave->block_align = toInt(buffer, 2);
read(buffer, 2);
fWave->bits_per_sample = toInt(buffer, 2);
read(buffer, 4);
if (strncmp(buffer, "data", 4) != 0) {
read(buffer, 4);
int _extra_size = toInt(buffer, 4);
char _extra_data[_extra_size];
read(_extra_data, _extra_size);
read(buffer, 4);
fWave->subchunk_2_id = toInt(buffer, 4);
} else {
fWave->subchunk_2_id = toInt(buffer, 4);
}
read(buffer, 4);
fWave->subchunk_2_size = toInt(buffer, 4);
return true;
}
void loadWave()
{
// Read sound data
fWave->data = new char[fWave->subchunk_2_size];
read(fWave->data, fWave->subchunk_2_size);
}
virtual void read(char* buffer, unsigned int size) = 0;
protected:
inline int toInt(char* buffer, int len)
{
int a = 0;
for(int i = 0; i < len; i++) {
((char*)&a)[i] = buffer[i];
}
return a;
}
};
/*
* WAV file reader that handles resource parser
*/
class WaveResourceReader : public WaveReader {
public:
WaveResourceReader(const char* name) :
offset(0) {
resource = Resource::open(name);
};
~WaveResourceReader() {
Resource::destroy(resource);
};
void read(char* buffer, unsigned int size) override {
size_t read_len = resource->read((void*)buffer, size, offset);
offset += read_len;
}
private:
Resource* resource;
size_t offset;
};
#endif
/************************** END WaveReader.h **************************/