-
Notifications
You must be signed in to change notification settings - Fork 12
/
wave_writer.c
316 lines (251 loc) · 6.13 KB
/
wave_writer.c
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Base documents:
* https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
* http://en.wikipedia.org/wiki/WAV
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "wave_writer.h"
#define FOUR_CC(a,b,c,d) (((a)<<24) | ((b)<<16) | ((c)<<8) | ((d)<<0))
#define FORMAT 1
#define FMT_LEN 16
#define DEFAULT_RIFF_LEN (4 + 8 + FMT_LEN + 8)
struct wave_writer {
int format;
int num_channels;
int sample_rate;
int sample_bits;
int num_samples;
FILE *fp;
};
static int
get_data_len(const wave_writer *ww) {
int l = ww->num_samples * ww->num_channels * ww->sample_bits / 8;
assert(0 <= l);
if (l % 2 != 0) {
l++;
}
return l;
}
static int
write_byte(FILE *fp, unsigned char in)
{
if (1 > fwrite(&in, 1, 1, fp)) {
return 0;
}
return 1;
}
static int
write_int32_b(FILE *fp, int in)
{
unsigned char a, b, c, d;
a = (in>>24) & 0xFF;
b = (in>>16) & 0xFF;
c = (in>> 8) & 0xFF;
d = (in>> 0) & 0xFF;
if (!write_byte(fp, a)) return 0;
if (!write_byte(fp, b)) return 0;
if (!write_byte(fp, c)) return 0;
if (!write_byte(fp, d)) return 0;
return 1;
}
static int
write_int32_l(FILE *fp, int in)
{
unsigned char a, b, c, d;
a = (in>> 0) & 0xFF;
b = (in>> 8) & 0xFF;
c = (in>>16) & 0xFF;
d = (in>>24) & 0xFF;
if (!write_byte(fp, a)) return 0;
if (!write_byte(fp, b)) return 0;
if (!write_byte(fp, c)) return 0;
if (!write_byte(fp, d)) return 0;
return 1;
}
static int
write_int16_l(FILE *fp, int in)
{
unsigned char a, b;
a = (in>> 0) & 0xFF;
b = (in>> 8) & 0xFF;
if (!write_byte(fp, a)) return 0;
if (!write_byte(fp, b)) return 0;
return 1;
}
static int
write_riff_chunk(struct wave_writer *ww)
{
if (!write_int32_b(ww->fp, FOUR_CC('R','I','F','F'))) {
return 0;
}
if (!write_int32_l(ww->fp, DEFAULT_RIFF_LEN + get_data_len(ww))) {
return 0;
}
if (!write_int32_b(ww->fp, FOUR_CC('W','A','V','E'))) {
return 0;
}
if (!write_int32_b(ww->fp, FOUR_CC('f','m','t',' '))) {
return 0;
}
if (!write_int32_l(ww->fp, FMT_LEN)) {
return 0;
}
if (!write_int16_l(ww->fp, ww->format)) {
return 0;
}
if (!write_int16_l(ww->fp, ww->num_channels)) {
return 0;
}
if (!write_int32_l(ww->fp, ww->sample_rate)) {
return 0;
}
if (!write_int32_l(ww->fp, ww->sample_rate * ww->num_channels * ww->sample_bits / 8)) {
return 0;
}
if (!write_int16_l(ww->fp, ww->num_channels * ww->sample_bits / 8)) {
return 0;
}
if (!write_int16_l(ww->fp, ww->sample_bits)) {
return 0;
}
if (!write_int32_b(ww->fp, FOUR_CC('d','a','t','a'))) {
return 0;
}
if (!write_int32_l(ww->fp, get_data_len(ww))) {
return 0;
}
return 1;
}
static int
is_acceptable_num_channels(const wave_writer_format *format) {
return 1 <= format->num_channels
&& format->num_channels <= 8;
}
static int
is_acceptable_sample_bits(const wave_writer_format *format) {
return format->sample_bits == 8
|| format->sample_bits == 16
|| format->sample_bits == 24;
}
static int
is_acceptable_sample_rate(const wave_writer_format *format) {
return format->sample_rate == 8000
|| format->sample_rate == 11025
|| format->sample_rate == 16000
|| format->sample_rate == 22050
|| format->sample_rate == 24000
|| format->sample_rate == 32000
|| format->sample_rate == 44100
|| format->sample_rate == 48000;
}
struct wave_writer *
wave_writer_open(const char *filename, const wave_writer_format *format, wave_writer_error *error)
{
struct wave_writer *ww = NULL;
assert(filename != NULL);
assert(format != NULL);
assert(error != NULL);
if (!is_acceptable_num_channels(format) || !is_acceptable_sample_bits(format) || !is_acceptable_sample_rate(format)) {
*error = WW_BAD_FORMAT;
goto format_error;
}
ww = (struct wave_writer *)calloc(1, sizeof(struct wave_writer));
if (!ww) {
*error = WW_ALLOC_ERROR;
goto alloc_error;
}
ww->fp = fopen(filename, "wb");
if (!ww->fp) {
*error = WW_OPEN_ERROR;
goto open_error;
}
ww->format = FORMAT;
ww->num_channels = format->num_channels;
ww->sample_rate = format->sample_rate;
ww->sample_bits = format->sample_bits;
ww->num_samples = 0;
if (!write_riff_chunk(ww)) {
*error = WW_IO_ERROR;
goto writing_error;
}
return ww;
writing_error:
fclose(ww->fp);
open_error:
free(ww);
alloc_error:
format_error:
return NULL;
}
int
wave_writer_close(struct wave_writer *ww, wave_writer_error *error)
{
int result = 1;
if (ww) {
int l = ww->num_samples * ww->num_channels * ww->sample_bits / 8;
assert(0 <= l);
if (l % 2 != 0) {
write_byte(ww->fp, 0);
}
if (fseek(ww->fp, 0, SEEK_SET) != 0) {
*error = WW_IO_ERROR;
result = 0;
goto release;
}
if (!write_riff_chunk(ww)) {
*error = WW_IO_ERROR;
result = 0;
goto release;
}
release:
fclose(ww->fp);
free(ww);
}
return result;
}
int
wave_writer_get_format(struct wave_writer *ww)
{
assert(ww != NULL);
return ww->format;
}
int
wave_writer_get_num_channels(struct wave_writer *ww)
{
assert(ww != NULL);
return ww->num_channels;
}
int
wave_writer_get_sample_rate(struct wave_writer *ww)
{
assert(ww != NULL);
return ww->sample_rate;
}
int
wave_writer_get_sample_bits(struct wave_writer *ww)
{
assert(ww != NULL);
return ww->sample_bits;
}
int
wave_writer_get_num_samples(struct wave_writer *ww)
{
assert(ww != NULL);
return ww->num_samples;
}
int
wave_writer_put_samples(struct wave_writer *ww, int n, void *buf)
{
int ret;
assert(ww != NULL);
assert(buf != NULL);
ret = fwrite(buf, ww->num_channels * ww->sample_bits / 8, n, ww->fp);
if (ret < n) {
return -1;
}
ww->num_samples += ret;
return ret;
}