|
| 1 | +/************************** BEGIN WaveReader.h **************************/ |
| 2 | +/************************************************************************ |
| 3 | + FAUST Architecture File |
| 4 | + Copyright (C) 2020 GRAME, Centre National de Creation Musicale |
| 5 | + --------------------------------------------------------------------- |
| 6 | + This Architecture section is free software; you can redistribute it |
| 7 | + and/or modify it under the terms of the GNU General Public License |
| 8 | + as published by the Free Software Foundation; either version 3 of |
| 9 | + the License, or (at your option) any later version. |
| 10 | + |
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | + |
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program; If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | + EXCEPTION : As a special exception, you may create a larger work |
| 20 | + that contains this FAUST architecture section and distribute |
| 21 | + that work under terms of your choice, so long as this FAUST |
| 22 | + architecture section is not modified. |
| 23 | + ************************************************************************/ |
| 24 | + |
| 25 | +#ifndef __WaveReader__ |
| 26 | +#define __WaveReader__ |
| 27 | + |
| 28 | +#undef min |
| 29 | +#undef max |
| 30 | + |
| 31 | +#include <string> |
| 32 | +#include "Resource.h" |
| 33 | + |
| 34 | +// WAVE file description |
| 35 | +class WaveFile { |
| 36 | +public: |
| 37 | + // The canonical WAVE format starts with the RIFF header |
| 38 | + |
| 39 | + /** |
| 40 | + Variable: chunk_id |
| 41 | + Contains the letters "RIFF" in ASCII form (0x52494646 big-endian form). |
| 42 | + **/ |
| 43 | + int chunk_id; |
| 44 | + |
| 45 | + /** |
| 46 | + Variable: chunk_size |
| 47 | + 36 + SubChunk2Size, or more precisely: 4 + (8 + SubChunk1Size) + (8 + SubChunk2Size) |
| 48 | + This is the size of the rest of the chunk following this number. |
| 49 | + This is the size of the entire file in bytes minus 8 bytes for the |
| 50 | + two fields not included in this count: ChunkID and ChunkSize. |
| 51 | + **/ |
| 52 | + int chunk_size; |
| 53 | + |
| 54 | + /** |
| 55 | + Variable: format |
| 56 | + Contains the letters "WAVE" (0x57415645 big-endian form). |
| 57 | + **/ |
| 58 | + int format; |
| 59 | + |
| 60 | + // The "WAVE" format consists of two subchunks: "fmt " and "data": |
| 61 | + // The "fmt " subchunk describes the sound data's format: |
| 62 | + |
| 63 | + /** |
| 64 | + Variable: subchunk_1_id |
| 65 | + Contains the letters "fmt " (0x666d7420 big-endian form). |
| 66 | + **/ |
| 67 | + int subchunk_1_id; |
| 68 | + |
| 69 | + /** |
| 70 | + Variable: subchunk_1_size |
| 71 | + 16 for PCM. This is the size of the rest of the Subchunk which follows this number. |
| 72 | + **/ |
| 73 | + int subchunk_1_size; |
| 74 | + |
| 75 | + /** |
| 76 | + Variable: audio_format |
| 77 | + PCM = 1 (i.e. Linear quantization) Values other than 1 indicate some form of compression. |
| 78 | + **/ |
| 79 | + short audio_format; |
| 80 | + |
| 81 | + /** |
| 82 | + Variable: num_channels |
| 83 | + Mono = 1, Stereo = 2, etc. |
| 84 | + **/ |
| 85 | + short num_channels; |
| 86 | + |
| 87 | + /** |
| 88 | + Variable: sample_rate |
| 89 | + 8000, 44100, etc. |
| 90 | + **/ |
| 91 | + int sample_rate; |
| 92 | + |
| 93 | + /** |
| 94 | + Variable: byte_rate |
| 95 | + == SampleRate * NumChannels * BitsPerSample/8 |
| 96 | + **/ |
| 97 | + int byte_rate; |
| 98 | + |
| 99 | + /** |
| 100 | + Variable: block_align |
| 101 | + == NumChannels * BitsPerSample/8 |
| 102 | + The number of bytes for one sample including all channels. I wonder what happens |
| 103 | + when this number isn't an integer? |
| 104 | + **/ |
| 105 | + short block_align; |
| 106 | + |
| 107 | + /** |
| 108 | + Variable: bits_per_sample |
| 109 | + 8 bits = 8, 16 bits = 16, etc. |
| 110 | + **/ |
| 111 | + short bits_per_sample; |
| 112 | + |
| 113 | + /** |
| 114 | + Here should come some extra parameters which i will avoid. |
| 115 | + **/ |
| 116 | + |
| 117 | + // The "data" subchunk contains the size of the data and the actual sound: |
| 118 | + |
| 119 | + /** |
| 120 | + Variable: subchunk_2_id |
| 121 | + Contains the letters "data" (0x64617461 big-endian form). |
| 122 | + **/ |
| 123 | + int subchunk_2_id; |
| 124 | + |
| 125 | + /** |
| 126 | + Variable: subchunk_2_size |
| 127 | + == NumSamples * NumChannels * BitsPerSample/8 |
| 128 | + This is the number of bytes in the data. You can also think of this as the size |
| 129 | + of the read of the subchunk following this number. |
| 130 | + **/ |
| 131 | + int subchunk_2_size; |
| 132 | + |
| 133 | + /** |
| 134 | + Variable: data |
| 135 | + The actual sound data. |
| 136 | + **/ |
| 137 | + char* data; |
| 138 | + |
| 139 | +}; |
| 140 | + |
| 141 | +// Base reader |
| 142 | +class WaveReader { |
| 143 | +public: |
| 144 | + WaveFile* fWave; |
| 145 | + |
| 146 | + WaveReader() { |
| 147 | + fWave = new WaveFile(); |
| 148 | + } |
| 149 | + |
| 150 | + virtual ~WaveReader() { |
| 151 | + if (fWave->data != NULL) |
| 152 | + delete[] fWave->data; |
| 153 | + delete fWave; |
| 154 | + } |
| 155 | + |
| 156 | + bool loadWaveHeader() { |
| 157 | + char buffer[4]; |
| 158 | + |
| 159 | + read(buffer, 4); |
| 160 | + if (strncmp(buffer, "RIFF", 4) != 0) { |
| 161 | + debugMessage("Not a valid WAV file!"); |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + fWave->chunk_id = toInt(buffer, 4); |
| 166 | + |
| 167 | + read(buffer, 4); |
| 168 | + fWave->chunk_size = toInt(buffer, 4); |
| 169 | + |
| 170 | + read(buffer, 4); |
| 171 | + fWave->format = toInt(buffer, 4); |
| 172 | + |
| 173 | + read(buffer, 4); |
| 174 | + fWave->subchunk_1_id = toInt(buffer, 4); |
| 175 | + |
| 176 | + read(buffer, 4); |
| 177 | + fWave->subchunk_1_size = toInt(buffer, 4); |
| 178 | + |
| 179 | + read(buffer, 2); |
| 180 | + fWave->audio_format = toInt(buffer, 2); |
| 181 | + read(buffer, 2); |
| 182 | + fWave->num_channels = toInt(buffer, 2); |
| 183 | + |
| 184 | + read(buffer, 4); |
| 185 | + fWave->sample_rate = toInt(buffer, 4); |
| 186 | + |
| 187 | + read(buffer, 4); |
| 188 | + fWave->byte_rate = toInt(buffer, 4); |
| 189 | + |
| 190 | + read(buffer, 2); |
| 191 | + fWave->block_align = toInt(buffer, 2); |
| 192 | + read(buffer, 2); |
| 193 | + fWave->bits_per_sample = toInt(buffer, 2); |
| 194 | + |
| 195 | + read(buffer, 4); |
| 196 | + if (strncmp(buffer, "data", 4) != 0) { |
| 197 | + read(buffer, 4); |
| 198 | + int _extra_size = toInt(buffer, 4); |
| 199 | + char _extra_data[_extra_size]; |
| 200 | + read(_extra_data, _extra_size); |
| 201 | + read(buffer, 4); |
| 202 | + fWave->subchunk_2_id = toInt(buffer, 4); |
| 203 | + } else { |
| 204 | + fWave->subchunk_2_id = toInt(buffer, 4); |
| 205 | + } |
| 206 | + |
| 207 | + read(buffer, 4); |
| 208 | + fWave->subchunk_2_size = toInt(buffer, 4); |
| 209 | + return true; |
| 210 | + } |
| 211 | + |
| 212 | + void loadWave() |
| 213 | + { |
| 214 | + // Read sound data |
| 215 | + fWave->data = new char[fWave->subchunk_2_size]; |
| 216 | + read(fWave->data, fWave->subchunk_2_size); |
| 217 | + } |
| 218 | + |
| 219 | + virtual void read(char* buffer, unsigned int size) = 0; |
| 220 | + |
| 221 | +protected: |
| 222 | + inline int toInt(char* buffer, int len) |
| 223 | + { |
| 224 | + int a = 0; |
| 225 | + for(int i = 0; i < len; i++) { |
| 226 | + ((char*)&a)[i] = buffer[i]; |
| 227 | + } |
| 228 | + return a; |
| 229 | + } |
| 230 | +}; |
| 231 | + |
| 232 | +/* |
| 233 | + * WAV file reader that handles resource parser |
| 234 | + */ |
| 235 | +class WaveResourceReader : public WaveReader { |
| 236 | +public: |
| 237 | + WaveResourceReader(const char* name) : |
| 238 | + offset(0) { |
| 239 | + resource = Resource::open(name); |
| 240 | + }; |
| 241 | + |
| 242 | + ~WaveResourceReader() { |
| 243 | + Resource::destroy(resource); |
| 244 | + }; |
| 245 | + |
| 246 | + void read(char* buffer, unsigned int size) override { |
| 247 | + size_t read_len = resource->read((void*)buffer, size, offset); |
| 248 | + offset += read_len; |
| 249 | + } |
| 250 | +private: |
| 251 | + Resource* resource; |
| 252 | + size_t offset; |
| 253 | +}; |
| 254 | + |
| 255 | +#endif |
| 256 | +/************************** END WaveReader.h **************************/ |
0 commit comments