Decode Wav files
Diff: WAV_Reader.cpp
- Revision:
- 2:18fdd269b401
- Parent:
- 1:c1046d91bff7
--- a/WAV_Reader.cpp Fri Feb 24 15:18:01 2017 +0000 +++ b/WAV_Reader.cpp Wed Mar 01 10:35:59 2017 +0000 @@ -65,9 +65,9 @@ return success; } -int WAV_Reader::read(char buff[], int len) { +int WAV_Reader::read(int buff[], int size, int len) { int data_left = config.data_length - _current_pos; - int to_read = len; + int to_read = len*size; if (to_read > data_left) { if (data_left == 0 && _loop) { @@ -77,15 +77,22 @@ } } - size_t ret = fread(buff, 1, to_read, *fpp); - move_read_position(ret, _current_pos); + char* bytes = new char[to_read]; + size_t read_bytes = fread(bytes, 1, to_read, *fpp); + read_bytes -= read_bytes % size; + int count = 0; + for (int i = 0; i < read_bytes; i += size){ + buff[count++] = get_num(bytes+i, size); + } + delete[] bytes; + move_read_position(read_bytes, _current_pos); - to_read = len - ret; + to_read = len*size - read_bytes; if (to_read > 0 && _loop) { - ret += read(buff + ret, to_read); + read_bytes += read(buff + read_bytes/size, size, to_read); } - return ret; + return read_bytes; } void WAV_Reader::reset() { @@ -114,6 +121,10 @@ return config.samples_per_sec; } +uint32_t WAV_Reader::bytes_per_sec() { + return config.avg_bytes_per_sec; +} + uint16_t WAV_Reader::block_align() { return config.block_align; } @@ -122,6 +133,10 @@ return config.bits_per_sample; } +uint32_t WAV_Reader::data_length() { + return config.data_length; +} + /** Private **/ int WAV_Reader::read_bytes(FILE *fp, char* out, int len_to_read, int start_off) { @@ -158,7 +173,7 @@ uint32_t output = 0; for (int i = 0; i < byte_count; i++) { - // numbers are stored least significant byte in the WAV header. So reverse and add to find the number + // numbers are stored least significant byte. So reverse and add to find the number output += input[i] << (i * 8); } @@ -168,7 +183,7 @@ int WAV_Reader::find_chunk(FILE *fp, char* check, int max_len, int start) { int result = -1; - char extracted[5]; + int pos = start; while ((pos + 8) < max_len && result == -1) {