-
Notifications
You must be signed in to change notification settings - Fork 0
/
wav_direct_write.h
49 lines (40 loc) · 1.51 KB
/
wav_direct_write.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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Class for writing to WAVE file as soon as a sample is received. Completes *
* proper write on destruction. (File can be in a bad state if not destroyed *
* properly. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <cassert>
#include "util.h"
template <typename sample_t>
class wav_writer_t {
wav_header_t const header;
std::ofstream outp;
uint32_t count = 0;
public:
wav_writer_t(char const *filename, wav_header_t const &hdr) : outp(filename), header(hdr) {
assert(sizeof sample_t() == header.block_align);
assert(header.block_align >= header.bits_per_sample / 8 * header.num_ch);
outp << "RIFF";
binary_write<uint32_t>(outp, 0);
outp << "WAVE";
outp << "fmt ";
binary_write<uint32_t>(outp, sizeof header);
binary_write(outp, header);
outp << "data";
binary_write<uint32_t>(outp, 0);
}
void add_sample(sample_t const &sample) {
binary_write(outp, sample);
count++;
}
~wav_writer_t() {
uint32_t data_size = count * sizeof sample_t();
outp.seekp(4, std::ios::beg);
binary_write<uint32_t>(outp, 4 /*remaining data in this chunk*/
+ (8 + sizeof header) /*fmt chunk*/
+ (8 + data_size) /*data chunk*/);
outp.seekp(12 + 8 + sizeof header + 4, std::ios::beg);
binary_write(outp, data_size);
outp.close();
}
};