99#include < fstream>
1010#include < iostream>
1111#include < string>
12- #include < vector>
1312#include < unordered_set>
13+ #include < vector>
1414
1515#include " wav.h"
1616
1717bool idIsJunk (char *id) {
18- return strncmp (id, " junk" , 4 ) == 0 || strncmp (id, " JUNK" , 4 ) == 0 || strncmp (id, " smpl" , 4 ) == 0 || strncmp (id, " LIST" , 4 ) == 0 ;
18+ return strncmp (id, " junk" , 4 ) == 0 || strncmp (id, " JUNK" , 4 ) == 0 ||
19+ strncmp (id, " smpl" , 4 ) == 0 || strncmp (id, " LIST" , 4 ) == 0 ||
20+ strncmp (id, " bext" , 4 ) == 0 ;
1921}
2022
2123bool ReadChunkAndSkipJunk (std::ifstream &file, char *chunkID) {
@@ -50,11 +52,10 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName,
5052 // Read the WAV file header
5153 char chunkId[4 ];
5254 if (!ReadChunkAndSkipJunk (wavFile, chunkId)) {
53- std::cerr << " Error while reading for next chunk." << std::endl;
54- return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE;
55+ std::cerr << " Error while reading for next chunk." << std::endl;
56+ return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE;
5557 }
56-
57-
58+
5859 if (strncmp (chunkId, " RIFF" , 4 ) != 0 ) {
5960 std::cerr << " Error: File does not start with expected RIFF chunk. Got"
6061 << chunkId << " instead." << std::endl;
@@ -75,8 +76,8 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName,
7576 // Read the format chunk
7677 char subchunk1Id[4 ];
7778 if (!ReadChunkAndSkipJunk (wavFile, subchunk1Id)) {
78- std::cerr << " Error while reading for next chunk." << std::endl;
79- return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE;
79+ std::cerr << " Error while reading for next chunk." << std::endl;
80+ return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE;
8081 }
8182 if (strncmp (subchunk1Id, " fmt " , 4 ) != 0 ) {
8283 std::cerr << " Error: Invalid WAV file missing expected fmt section; got "
@@ -87,15 +88,19 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName,
8788 int subchunk1Size;
8889 wavFile.read (reinterpret_cast <char *>(&subchunk1Size), 4 );
8990 if (subchunk1Size < 16 ) {
90- std::cerr << " WAV chunk 1 size is " << subchunk1Size << " , which is smaller than the requried 16 to fit the expected information." << std::endl;
91- return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE;
91+ std::cerr << " WAV chunk 1 size is " << subchunk1Size
92+ << " , which is smaller than the requried 16 to fit the expected "
93+ " information."
94+ << std::endl;
95+ return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE;
9296 }
9397
9498 unsigned short audioFormat;
9599 wavFile.read (reinterpret_cast <char *>(&audioFormat), 2 );
96100 const short AUDIO_FORMAT_PCM = 1 ;
97101 const short AUDIO_FORMAT_IEEE = 3 ;
98- std::unordered_set<short > supportedFormats{ AUDIO_FORMAT_PCM, AUDIO_FORMAT_IEEE };
102+ std::unordered_set<short > supportedFormats{AUDIO_FORMAT_PCM,
103+ AUDIO_FORMAT_IEEE};
99104 if (supportedFormats.find (audioFormat) == supportedFormats.end ()) {
100105 std::cerr << " Error: Unsupported WAV format detected. " ;
101106 switch (audioFormat) {
@@ -136,22 +141,21 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName,
136141 short bitsPerSample;
137142 wavFile.read (reinterpret_cast <char *>(&bitsPerSample), 2 );
138143
139- // The default is for there to be 16 bytes in the fmt chunk, but sometimes it's different.
144+ // The default is for there to be 16 bytes in the fmt chunk, but sometimes
145+ // it's different.
140146 if (subchunk1Size > 16 ) {
141- const int extraBytes = subchunk1Size - 16 ;
142- const int skipChars = extraBytes / 4 ;
143- wavFile.ignore (skipChars);
144- const int remainder = extraBytes % 4 ;
145- int junk;
146- wavFile.read (reinterpret_cast <char *>(&byteRate), remainder);
147+ const int extraBytes = subchunk1Size - 16 ;
148+ const int skipChars = extraBytes / 4 ;
149+ wavFile.ignore (skipChars);
150+ const int remainder = extraBytes % 4 ;
151+ wavFile.read (reinterpret_cast <char *>(&byteRate), remainder);
147152 }
148153
149-
150154 // Read the data chunk
151155 char subchunk2Id[4 ];
152156 if (!ReadChunkAndSkipJunk (wavFile, subchunk2Id)) {
153- std::cerr << " Error while reading for next chunk." << std::endl;
154- return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE;
157+ std::cerr << " Error while reading for next chunk." << std::endl;
158+ return dsp::wav::LoadReturnCode::ERROR_INVALID_FILE;
155159 }
156160 if (strncmp (subchunk2Id, " data" , 4 ) != 0 ) {
157161 std::cerr << " Error: Invalid WAV file" << std::endl;
@@ -163,26 +167,25 @@ dsp::wav::LoadReturnCode dsp::wav::Load(const WDL_String &fileName,
163167 wavFile.read (reinterpret_cast <char *>(&subchunk2Size), 4 );
164168
165169 if (audioFormat == AUDIO_FORMAT_IEEE) {
166- if (bitsPerSample == 32 )
167- dsp::wav::_LoadSamples32 (wavFile, subchunk2Size, audio);
168- else {
169- std::cerr << " Error: Unsupported bits per sample for IEEE files: " << bitsPerSample
170- << std::endl;
171- return dsp::wav::LoadReturnCode::ERROR_UNSUPPORTED_BITS_PER_SAMPLE;
172- }
173- }
174- else if (audioFormat == AUDIO_FORMAT_PCM) {
175- if (bitsPerSample == 16 )
176- dsp::wav::_LoadSamples16 (wavFile, subchunk2Size, audio);
177- else if (bitsPerSample == 24 )
178- dsp::wav::_LoadSamples24 (wavFile, subchunk2Size, audio);
179- else if (bitsPerSample == 32 )
180- dsp::wav::_LoadSamples32 (wavFile, subchunk2Size, audio);
181- else {
182- std::cerr << " Error: Unsupported bits per sample for PCM files: " << bitsPerSample
183- << std::endl;
184- return dsp::wav::LoadReturnCode::ERROR_UNSUPPORTED_BITS_PER_SAMPLE;
185- }
170+ if (bitsPerSample == 32 )
171+ dsp::wav::_LoadSamples32 (wavFile, subchunk2Size, audio);
172+ else {
173+ std::cerr << " Error: Unsupported bits per sample for IEEE files: "
174+ << bitsPerSample << std::endl;
175+ return dsp::wav::LoadReturnCode::ERROR_UNSUPPORTED_BITS_PER_SAMPLE;
176+ }
177+ } else if (audioFormat == AUDIO_FORMAT_PCM) {
178+ if (bitsPerSample == 16 )
179+ dsp::wav::_LoadSamples16 (wavFile, subchunk2Size, audio);
180+ else if (bitsPerSample == 24 )
181+ dsp::wav::_LoadSamples24 (wavFile, subchunk2Size, audio);
182+ else if (bitsPerSample == 32 )
183+ dsp::wav::_LoadSamples32 (wavFile, subchunk2Size, audio);
184+ else {
185+ std::cerr << " Error: Unsupported bits per sample for PCM files: "
186+ << bitsPerSample << std::endl;
187+ return dsp::wav::LoadReturnCode::ERROR_UNSUPPORTED_BITS_PER_SAMPLE;
188+ }
186189 }
187190
188191 // Close the WAV file
0 commit comments