|
| 1 | +/* |
| 2 | + This file is part of the Arduino_AdvancedAnalog library. |
| 3 | + Copyright (c) 2024 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +#include "AdvancedAnalog.h" |
| 21 | + |
| 22 | +#ifndef ARDUINO_WAV_READER_H_ |
| 23 | +#define ARDUINO_WAV_READER_H_ |
| 24 | + |
| 25 | +class WavReader { |
| 26 | + typedef struct { |
| 27 | + char chunk_id[4]; |
| 28 | + unsigned int chunk_size; |
| 29 | + char format[4]; |
| 30 | + char subchunk1_id[4]; |
| 31 | + unsigned int subchunk1_size; |
| 32 | + unsigned short audio_format; |
| 33 | + unsigned short num_channels; |
| 34 | + unsigned int sample_rate; |
| 35 | + unsigned int byte_rate; |
| 36 | + unsigned short block_align; |
| 37 | + unsigned short bits_per_sample; |
| 38 | + char subchunk2_id[4]; |
| 39 | + unsigned int subchunk2_size; |
| 40 | + } WavHeader; |
| 41 | + |
| 42 | + private: |
| 43 | + FILE *file; |
| 44 | + WavHeader header; |
| 45 | + |
| 46 | + public: |
| 47 | + WavReader(): file(nullptr) { |
| 48 | + } |
| 49 | + ~WavReader(); |
| 50 | + size_t channels() { |
| 51 | + return header.num_channels; |
| 52 | + } |
| 53 | + |
| 54 | + size_t sample_rate() { |
| 55 | + return header.sample_rate; |
| 56 | + } |
| 57 | + |
| 58 | + size_t sample_size() { |
| 59 | + return header.bits_per_sample; |
| 60 | + } |
| 61 | + |
| 62 | + size_t sample_count() { |
| 63 | + return (header.subchunk2_size * 8) / header.bits_per_sample; |
| 64 | + } |
| 65 | + int open(const char *path, void *buf=NULL, size_t size=0); |
| 66 | + int read(void *samples, size_t n_samples); |
| 67 | + int rewind(); |
| 68 | +}; |
| 69 | +#endif /* ARDUINO_WAV_READER_H_ */ |
0 commit comments