-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathwave.c
More file actions
356 lines (311 loc) · 9.13 KB
/
wave.c
File metadata and controls
356 lines (311 loc) · 9.13 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//SPDX-License-Identifier: (LGPL-2.1-only OR BSD-3-Clause)
//
// WAVE helper functions
//
// Copyright 2021 NXP
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sound/asound.h>
#include "tinycompress/tinywave.h"
static const struct wave_header blank_wave_header = {
.riff = {
.chunk = {
.desc = "RIFF",
},
.format = "WAVE",
},
.fmt = {
.chunk = {
.desc = "fmt ", /* Note the space is important here */
.size = sizeof(blank_wave_header.fmt) -
sizeof(blank_wave_header.fmt.chunk),
},
.type = 0x01, /* PCM */
},
.data = {
.chunk = {
.desc = "data",
},
},
};
void init_wave_header(struct wave_header *header, uint16_t channels,
uint32_t rate, uint16_t samplebits)
{
memcpy(header, &blank_wave_header, sizeof(blank_wave_header));
header->fmt.channels = channels;
header->fmt.rate = rate;
header->fmt.byterate = channels * rate * (samplebits / 8);
header->fmt.blockalign = channels * (samplebits / 8);
header->fmt.samplebits = samplebits;
}
void size_wave_header(struct wave_header *header, uint32_t size)
{
header->riff.chunk.size = sizeof(*header) -
sizeof(header->riff.chunk) + size;
header->data.chunk.size = size;
}
int parse_wave_header(struct wave_header *header, unsigned int *channels,
unsigned int *rate, unsigned int *format)
{
if (strncmp(header->riff.chunk.desc, "RIFF", 4) != 0) {
fprintf(stderr, "RIFF magic not found\n");
return -1;
}
if (strncmp(header->riff.format, "WAVE", 4) != 0) {
fprintf(stderr, "WAVE magic not found\n");
return -1;
}
if (strncmp(header->fmt.chunk.desc, "fmt", 3) != 0) {
fprintf(stderr, "FMT section not found");
return -1;
}
*channels = header->fmt.channels;
*rate = header->fmt.rate;
switch(header->fmt.samplebits) {
case 8:
*format = SNDRV_PCM_FORMAT_U8;
break;
case 16:
*format = SNDRV_PCM_FORMAT_S16_LE;
break;
case 32:
*format = SNDRV_PCM_FORMAT_S32_LE;
break;
default:
fprintf(stderr, "Unsupported sample bits %d\n",
header->fmt.samplebits);
return -1;
}
return 0;
}
/* WAVE_FORMAT_EXTENSIBLE sub-format GUIDs (first 2 bytes are the format tag) */
static const uint8_t pcm_subformat_guid[16] = {
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71
};
/**
* get_default_channel_mask() - Get default WAV channel mask for a given
* channel count (per Microsoft WAV specification)
*/
static unsigned int get_default_channel_mask(unsigned int channels)
{
switch (channels) {
case 1:
return WAV_CHANNEL_MASK_MONO;
case 2:
return WAV_CHANNEL_MASK_STEREO;
case 3:
return WAV_SPEAKER_FRONT_LEFT | WAV_SPEAKER_FRONT_RIGHT |
WAV_SPEAKER_FRONT_CENTER;
case 4:
return WAV_SPEAKER_FRONT_LEFT | WAV_SPEAKER_FRONT_RIGHT |
WAV_SPEAKER_BACK_LEFT | WAV_SPEAKER_BACK_RIGHT;
case 5:
return WAV_SPEAKER_FRONT_LEFT | WAV_SPEAKER_FRONT_RIGHT |
WAV_SPEAKER_FRONT_CENTER |
WAV_SPEAKER_BACK_LEFT | WAV_SPEAKER_BACK_RIGHT;
case 6:
return WAV_CHANNEL_MASK_5_1;
case 7:
return WAV_SPEAKER_FRONT_LEFT | WAV_SPEAKER_FRONT_RIGHT |
WAV_SPEAKER_FRONT_CENTER | WAV_SPEAKER_LOW_FREQUENCY |
WAV_SPEAKER_BACK_LEFT | WAV_SPEAKER_BACK_RIGHT |
WAV_SPEAKER_BACK_CENTER;
case 8:
return WAV_CHANNEL_MASK_7_1;
default:
return 0;
}
}
static const char *speaker_name(unsigned int mask)
{
switch (mask) {
case WAV_SPEAKER_FRONT_LEFT: return "FL";
case WAV_SPEAKER_FRONT_RIGHT: return "FR";
case WAV_SPEAKER_FRONT_CENTER: return "FC";
case WAV_SPEAKER_LOW_FREQUENCY: return "LFE";
case WAV_SPEAKER_BACK_LEFT: return "BL";
case WAV_SPEAKER_BACK_RIGHT: return "BR";
case WAV_SPEAKER_FRONT_LEFT_OF_CENTER: return "FLC";
case WAV_SPEAKER_FRONT_RIGHT_OF_CENTER: return "FRC";
case WAV_SPEAKER_BACK_CENTER: return "BC";
case WAV_SPEAKER_SIDE_LEFT: return "SL";
case WAV_SPEAKER_SIDE_RIGHT: return "SR";
default: return "?";
}
}
static void print_channel_map(unsigned int channels, unsigned int channel_mask)
{
unsigned int i, ch = 0;
fprintf(stderr, "Channel map (%u ch, mask 0x%04x): ", channels,
channel_mask);
for (i = 0; i < 18 && ch < channels; i++) {
unsigned int bit = 1u << i;
if (channel_mask & bit) {
if (ch > 0)
fprintf(stderr, ", ");
fprintf(stderr, "ch%u=%s", ch, speaker_name(bit));
ch++;
}
}
fprintf(stderr, "\n");
}
int parse_wave_file(FILE *file, unsigned int *channels, unsigned int *rate,
unsigned int *format, unsigned int *channel_mask)
{
struct riff_chunk chunk;
char riff_format[4];
uint16_t fmt_type, fmt_channels, fmt_blockalign, fmt_samplebits;
uint32_t fmt_rate, fmt_byterate;
uint16_t fmt_cb_size;
uint16_t fmt_valid_bits;
uint32_t fmt_channel_mask;
uint8_t fmt_subformat[16];
uint32_t fmt_chunk_size;
long fmt_end;
int found_fmt = 0, found_data = 0;
size_t nread;
/* Seek to beginning */
if (fseek(file, 0, SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek to start of file\n");
return -1;
}
/* Read RIFF header */
nread = fread(&chunk, 1, sizeof(chunk), file);
if (nread != sizeof(chunk)) {
fprintf(stderr, "Failed to read RIFF header\n");
return -1;
}
if (strncmp(chunk.desc, "RIFF", 4) != 0) {
fprintf(stderr, "RIFF magic not found\n");
return -1;
}
nread = fread(riff_format, 1, sizeof(riff_format), file);
if (nread != sizeof(riff_format)) {
fprintf(stderr, "Failed to read RIFF format\n");
return -1;
}
if (strncmp(riff_format, "WAVE", 4) != 0) {
fprintf(stderr, "WAVE magic not found\n");
return -1;
}
/* Scan chunks until we find both "fmt " and "data" */
while (!found_data) {
nread = fread(&chunk, 1, sizeof(chunk), file);
if (nread != sizeof(chunk)) {
fprintf(stderr, "Unexpected end of file while scanning chunks\n");
return -1;
}
if (strncmp(chunk.desc, "fmt ", 4) == 0) {
fmt_chunk_size = chunk.size;
fmt_end = ftell(file) + fmt_chunk_size;
/* Read basic fmt fields (16 bytes) */
if (fmt_chunk_size < 16) {
fprintf(stderr, "fmt chunk too small (%u)\n",
fmt_chunk_size);
return -1;
}
nread = fread(&fmt_type, 1, sizeof(fmt_type), file);
nread += fread(&fmt_channels, 1, sizeof(fmt_channels), file);
nread += fread(&fmt_rate, 1, sizeof(fmt_rate), file);
nread += fread(&fmt_byterate, 1, sizeof(fmt_byterate), file);
nread += fread(&fmt_blockalign, 1, sizeof(fmt_blockalign), file);
nread += fread(&fmt_samplebits, 1, sizeof(fmt_samplebits), file);
if (nread != 16) {
fprintf(stderr, "Failed to read fmt fields\n");
return -1;
}
fmt_channel_mask = 0;
if (fmt_type == WAVE_FORMAT_EXTENSIBLE) {
/* Need at least 2 (cbSize) + 2 (validBits) +
* 4 (channelMask) + 16 (subformat) = 24 extra bytes
*/
if (fmt_chunk_size < 40) {
fprintf(stderr,
"WAVE_FORMAT_EXTENSIBLE fmt chunk too small (%u, need 40)\n",
fmt_chunk_size);
return -1;
}
nread = fread(&fmt_cb_size, 1, sizeof(fmt_cb_size), file);
nread += fread(&fmt_valid_bits, 1, sizeof(fmt_valid_bits), file);
nread += fread(&fmt_channel_mask, 1, sizeof(fmt_channel_mask), file);
nread += fread(fmt_subformat, 1, sizeof(fmt_subformat), file);
if (nread != 24) {
fprintf(stderr,
"Failed to read extensible fmt fields\n");
return -1;
}
/* Verify subformat GUID is PCM */
if (memcmp(fmt_subformat, pcm_subformat_guid,
sizeof(pcm_subformat_guid)) != 0) {
fprintf(stderr,
"Unsupported subformat (not PCM)\n");
return -1;
}
/* Use valid bits for format selection */
fmt_samplebits = fmt_valid_bits;
fprintf(stderr,
"WAVE_FORMAT_EXTENSIBLE: %u ch, %u Hz, %u bits, channel mask 0x%04x\n",
fmt_channels, fmt_rate, fmt_samplebits,
fmt_channel_mask);
} else if (fmt_type == WAVE_FORMAT_PCM) {
/* Basic PCM - use default channel mask */
fmt_channel_mask = get_default_channel_mask(fmt_channels);
if (fmt_channels > 2)
fprintf(stderr,
"Basic PCM with %u channels, using default channel mask 0x%04x\n",
fmt_channels, fmt_channel_mask);
} else {
fprintf(stderr,
"Unsupported WAVE format type: 0x%04x\n",
fmt_type);
return -1;
}
/* Seek to end of fmt chunk (skip any remaining bytes) */
if (fseek(file, fmt_end, SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek past fmt chunk\n");
return -1;
}
found_fmt = 1;
} else if (strncmp(chunk.desc, "data", 4) == 0) {
if (!found_fmt) {
fprintf(stderr,
"data chunk found before fmt chunk\n");
return -1;
}
/* File pointer is now at start of audio data */
found_data = 1;
} else {
/* Unknown chunk - skip it */
if (fseek(file, chunk.size, SEEK_CUR) < 0) {
fprintf(stderr,
"Failed to skip chunk '%.4s' (%u bytes)\n",
chunk.desc, chunk.size);
return -1;
}
}
}
*channels = fmt_channels;
*rate = fmt_rate;
*channel_mask = fmt_channel_mask;
switch (fmt_samplebits) {
case 8:
*format = SNDRV_PCM_FORMAT_U8;
break;
case 16:
*format = SNDRV_PCM_FORMAT_S16_LE;
break;
case 24:
*format = SNDRV_PCM_FORMAT_S24_LE;
break;
case 32:
*format = SNDRV_PCM_FORMAT_S32_LE;
break;
default:
fprintf(stderr, "Unsupported sample bits %d\n", fmt_samplebits);
return -1;
}
print_channel_map(fmt_channels, fmt_channel_mask);
return 0;
}