-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwav.h
294 lines (260 loc) · 9.22 KB
/
wav.h
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
Copyright 2018-2022 (c) Geraint Luff / Signalsmith Audio
Released under CC0 or public-domain (where applicable)
A basic class for reading/writing WAV files, currently only supporting 16-bit PCM.
Wav wav("input.wav");
// You can check the `.result`:
if (!wav.result) {
std::cout << wav.result.reason << "\n";
return 0;
}
// Or the result object is returned from the read/write
if (!wav.write("output.wav")) return 0;
// This convenience method writes a warning to stderr on failure
if (!wav.write("output.wav").warn()) return 0);
std::cout << "duration = " << wav.length()/wav.sampleRate << " seconds\n";
// You can either access samples by channel/index:
for (int i = 0; i < wav.length(); ++i) {
for (int c = 0; c < wav.channels; ++c) {
wav[c][i] *= 0.5;
}
}
// or from the interleaved sample array, a `vector<double>`:
for (auto &s : wav.samples) {
s *= 2;
}
// You can either resize with this method, or directly with `.channels` and `.samples`
wav.resize(newChannels, newLength);
// Average all channels into one, for convenience
wav.makeMono();
*/
#ifndef RIFF_WAVE_H_
#define RIFF_WAVE_H_
#include <vector>
#include <iostream>
#include <fstream>
// TODO: something better here that doesn't assume little-endian architecture
template<bool littleEndian=true>
struct BigEndian {
static uint32_t read16(std::istream& in) {
unsigned char a[2];
in.read((char*)a, sizeof(a));
return ((uint32_t)a[0]) + ((uint32_t)a[1])*256;
}
static uint32_t read32(std::istream& in) {
unsigned char a[4];
in.read((char*)a, sizeof(a));
return ((uint32_t)a[0]&0xff) + ((uint32_t)a[1])*256 + ((uint32_t)a[2])*65536 + ((uint32_t)a[3])*256*65536;
}
static void write16(std::ostream& out, uint16_t value) {
char a[2] = {(char)(value>>0), (char)(value>>8)};
out.write(a, sizeof(a));
}
static void write32(std::ostream& out, uint32_t value) {
char a[4] = {(char)(value>>0), (char)(value>>8), (char)(value>>16), (char)(value>>24)};
out.write(a, sizeof(a));
}
};
class Wav : BigEndian<true> {
// Little-endian versions of text values
uint32_t value_RIFF = 0x46464952;
uint32_t value_WAVE = 0x45564157;
uint32_t value_fmt = 0x20746d66;
uint32_t value_data = 0x61746164;
using BigEndian<true>::read16;
using BigEndian<true>::read32;
using BigEndian<true>::write16;
using BigEndian<true>::write32;
public:
struct Result {
enum class Code {
OK = 0,
IO_ERROR,
FORMAT_ERROR,
UNSUPPORTED,
WEIRD_CONFIG
};
Code code = Code::OK;
std::string reason;
Result(Code code, std::string reason="") : code(code), reason(reason) {};
Result & operator=(const Result &other) {
if (code == Code::OK) {
code = other.code;
reason = other.reason;
}
return *this;
}
// Used to neatly test for success
explicit operator bool () const {
return code == Code::OK;
};
const Result & warn(std::ostream& output=std::cerr) const {
if (!(bool)*this) {
output << "WAV error: " << reason << std::endl;
}
return *this;
}
};
unsigned int sampleRate = 48000;
unsigned int channels = 1;
std::vector<double> samples;
int length() const {
return samples.size()/channels;
}
void resize(int numChannels, int length) {
channels = numChannels;
samples.resize(channels*length);
}
template<bool isConst>
class ChannelReader {
using CSample = typename std::conditional<isConst, const double, double>::type;
CSample *data;
int stride;
public:
ChannelReader(CSample *samples, int stride) : data(samples), stride(stride) {}
CSample & operator [](int i) {
return data[i*stride];
}
};
ChannelReader<false> operator [](int c) {
return ChannelReader<false>(samples.data() + c, channels);
}
ChannelReader<true> operator [](int c) const {
return ChannelReader<true>(samples.data() + c, channels);
}
Result result = Result(Result::Code::OK);
Wav() {}
Wav(double sampleRate, int channels) : sampleRate(sampleRate), channels(channels) {}
Wav(double sampleRate, int channels, const std::vector<double> &samples) : sampleRate(sampleRate), channels(channels), samples(samples) {}
Wav(std::string filename) {
result = read(filename).warn();
}
enum class Format {
PCM=1
};
bool formatIsValid(uint16_t format, uint16_t bits) const {
if (format == (uint16_t)Format::PCM) {
if (bits == 16) {
return true;
}
}
return false;
}
Result read(std::string filename) {
std::ifstream file;
file.open(filename, std::ios::binary);
if (!file.is_open()) return result = Result(Result::Code::IO_ERROR, "Failed to open file: " + filename);
// RIFF chunk
if (read32(file) != value_RIFF) return result = Result(Result::Code::FORMAT_ERROR, "Input is not a RIFF file");
read32(file); // File length - we don't check this
if (read32(file) != value_WAVE) return result = Result(Result::Code::FORMAT_ERROR, "Input is not a plain WAVE file");
auto blockStart = file.tellg(); // start of the blocks - we will seek back to here periodically
bool hasFormat = false, hasData = false;
Format format = Format::PCM; // Shouldn't matter, we should always read the `fmt ` chunk before `data`
while (!file.eof()) {
auto blockType = read32(file), blockLength = read32(file);
if (!hasFormat && blockType == value_fmt) {
auto formatInt = read16(file);
format = (Format)formatInt;
channels = read16(file);
if (channels < 1) return result = Result(Result::Code::FORMAT_ERROR, "Cannot have zero channels");
sampleRate = read32(file);
if (sampleRate < 1) return result = Result(Result::Code::FORMAT_ERROR, "Cannot have zero sampleRate");
unsigned int expectedBytesPerSecond = read32(file);
unsigned int bytesPerFrame = read16(file);
unsigned int bitsPerSample = read16(file);
if (!formatIsValid(formatInt, bitsPerSample)) return result = Result(Result::Code::UNSUPPORTED, "Unsupported format:bits: " + std::to_string(formatInt) + ":" + std::to_string(bitsPerSample));
// Since it's plain WAVE, we can do some extra checks for consistency
if (bitsPerSample*channels != bytesPerFrame*8) return result = Result(Result::Code::FORMAT_ERROR, "Format sizes don't add up");
if (expectedBytesPerSecond != sampleRate*bytesPerFrame) return result = Result(Result::Code::FORMAT_ERROR, "Format sizes don't add up");
hasFormat = true;
file.clear();
file.seekg(blockStart);
} else if (hasFormat && blockType == value_data) {
std::vector<double> samples(0);
switch (format) {
case Format::PCM:
samples.reserve(blockLength/2);
for (size_t i = 0; i < blockLength/2; ++i) {
uint16_t value = read16(file);
if (file.eof()) break;
if (value >= 32768) {
samples.push_back(((double)value - 65536)/32768);
} else {
samples.push_back((double)value/32768);
}
}
}
while (samples.size()%channels != 0) {
samples.push_back(0);
}
this->samples = samples;
hasData = true;
} else {
file.ignore(blockLength);
}
}
if (!hasFormat) return result = Result(Result::Code::FORMAT_ERROR, "missing `fmt ` block");
if (!hasData) return result = Result(Result::Code::FORMAT_ERROR, "missing `data` block");
return result = Result(Result::Code::OK);
}
Result write(std::string filename, Format format=Format::PCM) {
if (channels == 0 || channels > 65535) return result = Result(Result::Code::WEIRD_CONFIG, "Invalid channel count");
if (sampleRate <= 0 || sampleRate > 0xFFFFFFFFu) return result = Result(Result::Code::WEIRD_CONFIG, "Invalid sample rate");
std::ofstream file;
file.open(filename, std::ios::binary);
if (!file.is_open()) return result = Result(Result::Code::IO_ERROR, "Failed to open file: " + filename);
int bytesPerSample;
switch (format) {
case Format::PCM:
bytesPerSample = 2;
break;
}
// File size - 44 bytes is RIFF header, "fmt" block, and "data" block header
unsigned int dataLength = samples.size()*bytesPerSample;
unsigned int fileLength = 44 + dataLength;
// RIFF chunk
write32(file, value_RIFF);
write32(file, fileLength - 8); // File length, excluding the RIFF header
write32(file, value_WAVE);
// "fmt " block
write32(file, value_fmt);
write32(file, 16); // block length
write16(file, (uint16_t)format);
write16(file, channels);
write32(file, sampleRate);
unsigned int expectedBytesPerSecond = sampleRate*channels*bytesPerSample;
write32(file, expectedBytesPerSecond);
write16(file, channels*bytesPerSample); // Bytes per frame
write16(file, bytesPerSample*8); // bist per sample
// "data" block
write32(file, value_data);
write32(file, dataLength);
switch (format) {
case Format::PCM:
for (unsigned int i = 0; i < samples.size(); i++) {
double value = samples[i]*32768;
if (value > 32767) value = 32767;
if (value <= -32768) value = -32768;
if (value < 0) value += 65536;
write16(file, (uint16_t)value);
}
break;
}
return result = Result(Result::Code::OK);
}
void makeMono() {
std::vector<double> newSamples(samples.size()/channels, 0);
for (size_t channel = 0; channel < channels; ++channel) {
for (size_t i = 0; i < newSamples.size(); ++i) {
newSamples[i] += samples[i*channels + channel];
}
}
for (size_t i = 0; i < newSamples.size(); ++i) {
newSamples[i] /= channels;
}
channels = 1;
samples = newSamples;
}
};
#endif // RIFF_WAVE_H_