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
|
#include "wav.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int rd_u32(FILE *f, uint32_t *v) {
unsigned char b[4];
if (fread(b, 1, 4, f) != 4)
return -1;
*v = (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24;
return 0;
}
static int rd_u16(FILE *f, uint16_t *v) {
unsigned char b[2];
if (fread(b, 1, 2, f) != 2)
return -1;
*v = (uint16_t)((uint32_t)b[0] | (uint32_t)b[1] << 8);
return 0;
}
#define FAIL(msg) \
do { \
if (err) \
*err = (msg); \
goto fail; \
} while (0)
int wav_read_mono_s16(const char *path, wav_t *out, const char **err) {
memset(out, 0, sizeof *out);
FILE *f = fopen(path, "rb");
if (!f) {
if (err)
*err = "cannot open file";
return -1;
}
char id[4];
uint32_t riff_size = 0;
if (fread(id, 1, 4, f) != 4 || memcmp(id, "RIFF", 4) != 0)
FAIL("not a RIFF file");
if (rd_u32(f, &riff_size) != 0)
FAIL("truncated header");
if (fread(id, 1, 4, f) != 4 || memcmp(id, "WAVE", 4) != 0)
FAIL("not a WAVE file");
uint16_t fmt = 0, channels = 0, bits = 0;
uint32_t rate = 0;
int have_fmt = 0;
for (;;) {
uint32_t size;
if (fread(id, 1, 4, f) != 4 || rd_u32(f, &size) != 0)
break; /* end of file, or a truncated trailing chunk */
if (memcmp(id, "fmt ", 4) == 0) {
uint16_t block_align;
uint32_t byte_rate;
if (size < 16)
FAIL("short fmt chunk");
if (rd_u16(f, &fmt) || rd_u16(f, &channels) || rd_u32(f, &rate) ||
rd_u32(f, &byte_rate) || rd_u16(f, &block_align) || rd_u16(f, &bits))
FAIL("truncated fmt chunk");
if (fseek(f, (long)size - 16, SEEK_CUR) != 0)
FAIL("bad fmt chunk size");
have_fmt = 1;
continue;
}
if (memcmp(id, "data", 4) == 0) {
if (!have_fmt)
FAIL("data chunk before fmt chunk");
/* Format 1 is PCM; 0xFFFE is extensible, whose subformat this
* reader does not inspect, so it is accepted only at 16 bits. */
if ((fmt != 1 && fmt != 0xFFFE) || bits != 16)
FAIL("only 16-bit PCM is supported");
if (channels == 0)
FAIL("zero channels");
const size_t frames = size / (2u * channels);
if (frames == 0)
FAIL("empty data chunk");
int16_t *mono = malloc(frames * sizeof(int16_t));
if (!mono)
FAIL("out of memory");
const size_t step = channels;
int16_t buf[1024];
size_t got = 0;
while (got < frames) {
const size_t want = (frames - got) < (sizeof buf / sizeof buf[0]) / step
? (frames - got) * step
: (sizeof buf / sizeof buf[0]) / step * step;
const size_t n = fread(buf, sizeof(int16_t), want, f);
if (n < step)
break;
for (size_t i = 0; i + step <= n; i += step)
mono[got++] = buf[i];
}
if (got == 0) {
free(mono);
FAIL("no samples read");
}
out->samples = mono;
out->count = got;
out->sample_rate = (int)rate;
out->channels = (int)channels;
fclose(f);
return 0;
}
/* Skip unknown chunks; RIFF chunks are word aligned. */
if (fseek(f, (long)size + (size & 1u), SEEK_CUR) != 0)
break;
}
FAIL("no data chunk");
fail:
fclose(f);
return -1;
}
void wav_free(wav_t *w) {
free(w->samples);
memset(w, 0, sizeof *w);
}
|