-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwav.h
More file actions
61 lines (52 loc) · 1.67 KB
/
Copy pathwav.h
File metadata and controls
61 lines (52 loc) · 1.67 KB
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
/* Simple, Minimalistic, WAVE library
* ©2017 Yuichiro Nakada
*
* Latest revisions:
* 1.00 (12-03-2017) initial release
*
* Basic usage:
* WAVE_write("file.wav", totalSampleCount, data, sampleRate);
* */
void WAVE_write_little_endian(unsigned int word, int num_bytes, FILE *fp)
{
unsigned buf;
while (num_bytes>0) {
buf = word & 0xff;
fwrite(&buf, 1, 1, fp);
num_bytes--;
word >>= 8;
}
}
void WAVE_write(char *filename, unsigned long num_samples, short int *data, int sample_rate)
{
unsigned int num_channels;
unsigned int bytes_per_sample;
unsigned int byte_rate;
unsigned long i; // counter for samples
num_channels = 1; // monaural
bytes_per_sample = 2;
byte_rate = sample_rate * num_channels * bytes_per_sample;
FILE *fp = fopen(filename, "w");
if (!fp) return;
//assert(fp); // make sure it opened
// write RIFF header
fwrite("RIFF", 1, 4, fp);
WAVE_write_little_endian(36 + bytes_per_sample * num_samples * num_channels, 4, fp);
fwrite("WAVE", 1, 4, fp);
// write fmt subchunk
fwrite("fmt ", 1, 4, fp);
WAVE_write_little_endian(16, 4, fp); // SubChunk1Size is 16
WAVE_write_little_endian(1, 2, fp); // PCM is format 1
WAVE_write_little_endian(num_channels, 2, fp);
WAVE_write_little_endian(sample_rate, 4, fp);
WAVE_write_little_endian(byte_rate, 4, fp);
WAVE_write_little_endian(num_channels*bytes_per_sample, 2, fp); // block align
WAVE_write_little_endian(8*bytes_per_sample, 2, fp); // bits/sample
// write data subchunk
fwrite("data", 1, 4, fp);
WAVE_write_little_endian(bytes_per_sample * num_samples*num_channels, 4, fp);
for (i=0; i<num_samples; i++) {
WAVE_write_little_endian((unsigned int)data[i], bytes_per_sample, fp);
}
fclose(fp);
}