-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWavReader.cpp
109 lines (93 loc) · 2.79 KB
/
WavReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
This file is part of the Arduino_AdvancedAnalog library.
Copyright (c) 2024 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Arduino.h"
#include "WavReader.h"
WavReader::~WavReader() {
stop();
}
int WavReader::begin(const char *path, size_t n_samples, size_t n_buffers, bool loop) {
this->loop = loop;
if ((file = fopen(path, "rb")) == nullptr) {
return 0;
}
// Read file header
fread(&header, sizeof(header), 1, file);
// Add more sanity checks if needed.
if (memcmp(header.chunk_id, "RIFF", 4) != 0 ||
memcmp(header.format, "WAVEfmt", 7) != 0 ||
((sizeof(Sample) * 8) < header.bits_per_sample) ||
(n_samples * header.num_channels) > sample_count()) {
stop();
return 0;
}
// Allocate the DMA buffer pool.
pool = new DMABufferPool<Sample>(n_samples, header.num_channels, n_buffers);
if (pool == nullptr) {
stop();
return 0;
}
return 1;
}
void WavReader::stop() {
if (file) {
fclose(file);
}
if (pool) {
delete pool;
}
pool = nullptr;
file = nullptr;
}
bool WavReader::available() {
if (file != nullptr && pool != nullptr) {
return pool->writable();
}
return false;
}
DMABuffer<Sample> &WavReader::read() {
while (!available()) {
__WFI();
}
DMABuffer<Sample> *buf = pool->allocate();
size_t offset = 0;
Sample *rawbuf = buf->data();
size_t n_samples = buf->size();
while (offset < n_samples) {
offset += fread(&rawbuf[offset], sizeof(Sample), n_samples - offset, file);
if (offset < n_samples) {
if (loop) {
rewind();
} else {
for (size_t i=offset; i<n_samples; i++) {
rawbuf[i] = 0;
}
fclose(file);
file = nullptr;
break;
}
}
}
return *buf;
}
int WavReader::rewind() {
if (file == nullptr) {
return 0;
}
if (fseek(file, sizeof(WavHeader), SEEK_SET) == -1) {
return 0;
}
return 1;
}