Skip to content

Commit ceaefeb

Browse files
committed
Add basic WAV file reader for use in examples.
Signed-off-by: iabdalkader <[email protected]>
1 parent f7d081c commit ceaefeb

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

src/Arduino_AdvancedAnalog.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626

2727
#include "AdvancedADC.h"
2828
#include "AdvancedDAC.h"
29+
#include "WavReader.h"
2930

3031
#endif /* ADVANCEDANALOGREDUX_ARDUINO_ADVANCEDANALOG_H */

src/WavReader.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 "Arduino.h"
21+
#include "WavReader.h"
22+
23+
WavReader::~WavReader() {
24+
if (file) {
25+
fclose(file);
26+
}
27+
}
28+
29+
int WavReader::open(const char *path, void *buf, size_t size) {
30+
if ((file = fopen(path, "rb")) == nullptr) {
31+
return 0;
32+
}
33+
34+
if (buf && size) {
35+
setbuffer(file, (char *) buf, size);
36+
}
37+
38+
// Read file header
39+
fread(&header, sizeof(header), 1, file);
40+
41+
// Check format.
42+
if (memcmp(header.chunk_id, "RIFF", 4) != 0) {
43+
return 0;
44+
}
45+
if (memcmp(header.format, "WAVEfmt", 7) != 0) {
46+
return 0;
47+
}
48+
// Add more sanity checks if needed.
49+
return 1;
50+
}
51+
52+
int WavReader::read(void *samples, size_t n_samples) {
53+
if (fread(samples, header.bits_per_sample / 8, n_samples, file) != n_samples) {
54+
return 0;
55+
}
56+
return 1;
57+
}
58+
59+
int WavReader::rewind() {
60+
if (fseek(file, sizeof(WavHeader), SEEK_SET) == -1) {
61+
return 0;
62+
}
63+
return 1;
64+
}

src/WavReader.h

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)