-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibwav.c
More file actions
688 lines (581 loc) · 16.7 KB
/
libwav.c
File metadata and controls
688 lines (581 loc) · 16.7 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <inttypes.h>
#include <err.h>
#include "libwav.h"
#include "libid3.h"
/* Inline functions and macros */
static inline uint32_t
readUInt32(void *buffer)
{
uint8_t *bytes = buffer;
return bytes[0] | bytes[1]<<8 | bytes[2] << 16 | bytes[3] << 24;
}
static inline uint16_t
readUInt16(void *buffer)
{
uint8_t *bytes = buffer;
return bytes[0] | bytes[1]<<8;
}
static inline void
writeUInt32(void *buffer, uint32_t val)
{
uint8_t *bytes = buffer;
bytes[0] = val & 0xff;
bytes[1] = (val>>8) & 0xff;
bytes[2] = (val>>16) & 0xff;
bytes[3] = (val>>24) & 0xff;
}
static inline void
writeUInt16(void *buffer, uint16_t val)
{
uint8_t *bytes = buffer;
bytes[0] = val & 0xff;
bytes[1] = (val>>8) & 0xff;
}
#define NA(a) (sizeof(a)/sizeof(a[0]))
/* Internal type definitions */
struct chunk_type {
const char *tag, *description;
Chunk *(*reader)(FILE *, uint32_t, const char *tag, const struct chunk_type *, uint32_t);
void (*writer)(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
};
typedef const struct chunk_type ChunkType;
/* Forward references */
static Chunk *readChunk(FILE *, uint32_t offset);
static Chunk *readList(FILE *, uint32_t offset, const char *, ChunkType *, uint32_t);
static Chunk *readFmt(FILE *, uint32_t offset, const char *, ChunkType *, uint32_t);
static Chunk *readData(FILE *, uint32_t offset, const char *, ChunkType *, uint32_t);
#if 0
static Chunk *readCues(FILE *, uint32_t offset, const char *, ChunkType *, uint32_t);
static Chunk *readLabl(FILE *, uint32_t offset, const char *, ChunkType *, uint32_t);
static Chunk *readLtxt(FILE *, uint32_t offset, const char *, ChunkType *, uint32_t);
#endif
static Chunk *readText(FILE *, uint32_t offset, const char *, ChunkType *, uint32_t);
static Chunk *readId3(FILE *, uint32_t offset, const char *, ChunkType *, uint32_t);
//static Chunk *readInt16(FILE *, uint32_t offset, const char *, ChunkType *, uint32_t);
static Chunk *readInt32(FILE *, uint32_t offset, const char *, ChunkType *, uint32_t);
static void writeWave(WaveChunk *, FILE *src, FILE *dst, uint32_t *offset);
static void writeChunk(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
static void writeList(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
static void writeFmt(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
static void writeData(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
#if 0
static void writeCues(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
static void writeLabl(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
static void writeLtxt(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
#endif
static void writeText(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
static void writeId3(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
//static void writeInt16(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
static void writeInt32(Chunk *, FILE *src, FILE *dst, uint32_t *offset);
const char *WaveError;
/*** READ WAV FILE */
WaveChunk *
OpenWaveFile(FILE *ifile)
{
WaveChunk *rval = NULL;
char buffer[1024];
uint32_t fileLen;
uint32_t offset;
Chunk *child;
Chunk **children;
if (fread(buffer, 1, 12, ifile) != 12) {
WaveError = "Short file";
goto exit;
}
if (memcmp(buffer, "RIFF", 4) != 0) {
WaveError = "File does not seem to be a RIFF file";
goto exit;
}
fileLen = readUInt32(buffer+4);
rval = (WaveChunk *)newChunk(buffer, fileLen, 0, sizeof(*rval));
if (rval == NULL) {
WaveError = "Out of memory in OpenWaveFile";
goto exit;
}
memcpy(rval->type, buffer+8, 4);
rval->children = NULL;
children = &rval->children;
offset = 12;
while (offset < fileLen) {
child = readChunk(ifile, offset);
if (child == NULL) {
/* Return with what we have so far */
goto exit;
}
*children = child;
children = &child->next;
offset += 8 + child->length;
}
exit:
return rval;
}
/* List of chunk keys and the function to read them */
static ChunkType chunkTypes[] = {
{"LIST", "List", readList, writeList},
{"data", "Audio data", readData, writeData},
{"fmt ", "Format", readFmt, writeFmt},
{"IARL", "Archival location", readText, writeText},
{"IART", "Artist", readText, writeText},
{"ICMS", "Commissioned", readText, writeText},
{"ICMT", "Comments", readText, writeText},
{"ICOP", "Copyright", readText, writeText},
{"ICRD", "Creation date", readText, writeText},
{"ICRP", "Cropped", readText, writeText},
{"IDIM", "Dimensions", readText, writeText},
{"IDPI", "Dots Per Inch", readText, writeText},
{"IENG", "Engineer", readText, writeText},
{"IGNR", "Genre", readText, writeText},
{"IKEY", "Keywords", readText, writeText},
{"ILGT", "Lightness", readText, writeText},
{"IMED", "Medium", readText, writeText},
{"INAM", "Name", readText, writeText},
{"IPLT", "Palette Setting", readText, writeText},
{"IPRD", "Product", readText, writeText},
{"ISBJ", "Subject", readText, writeText},
{"ISFT", "Software", readText, writeText},
{"ISHP", "Sharpness", readText, writeText},
{"ISRC", "Source", readText, writeText},
{"ISRF", "Source Form", readText, writeText},
{"ITCH", "Technician", readText, writeText},
{"ITRK", "Track", readText, writeText},
{"fact", "Samples", readInt32, writeInt32},
{"slnt", "Silence", readInt32, writeInt32},
#if 0
{"cue ", "Cues", readCues, writeCues},
{"labl", "Label", readLabl, writeLabl},
{"note", "Note", readLabl, writeLabl},
{"ltxt", "?", readLtxt, writeLtxt},
#endif
{"id3 ", "ID3 data", readId3, writeId3},
};
static Chunk *
readChunk(FILE *ifile, uint32_t offset)
{
Chunk *chunk = NULL;
char buffer[8];
uint32_t chunkLen;
int i;
if (fseek(ifile, (long)offset, SEEK_SET) != 0) {
WaveError = "OpenWaveFile: fseek failed";
goto exit;
}
if (fread(buffer, 1, 8, ifile) != 8) {
WaveError = "Premature end of file";
goto exit;
}
/* The chunk type at the start of the buffer will determine the
* function to read it in.
*/
chunkLen = readUInt32(buffer+4);
for (i=0; i < NA(chunkTypes); ++i) {
if (strncasecmp(buffer, chunkTypes[i].tag, 4) == 0) {
chunk = chunkTypes[i].reader(ifile, offset, buffer, &chunkTypes[i], chunkLen);
break;
}
}
if (chunk == NULL) {
/* Unknown chunk type, return a generic chunk. We read
* the header, but leave the data in the file.
*/
chunk = readData(ifile, offset, buffer, NULL, chunkLen);
}
exit:
return chunk;
}
/**
* Read a list chunk. Caller has already read the header, so the file
* is positioned at the type field.
*/
static Chunk *
readList(FILE *ifile, uint32_t offset, const char *tag, ChunkType *chunkType, uint32_t chunkLen)
{
Chunk *chunk = NULL;
ListChunk *lc;
char buffer[8];
uint32_t off2 = 0;
Chunk *child, **children;
if (fread(buffer, 1, 4, ifile) != 4) {
WaveError = "Premature end of file reading LIST";
goto exit;
}
if ((chunk = newChunk(tag, chunkLen, offset, sizeof(*lc))) == NULL) {
goto exit;
}
lc = (ListChunk *)chunk;
lc->children = NULL;
memcpy(lc->type, buffer, 4);
children = &lc->children;
offset += 8;
off2 += 4;
while (off2 < chunkLen) {
child = readChunk(ifile, offset + off2);
if (child == NULL) {
/* Return with what we have so far */
goto exit;
}
*children = child;
children = &child->next;
off2 += 8 + child->length;
}
exit:
return chunk;
}
/**
* Read a chunk containing file format info
*/
static Chunk *
readFmt(FILE *ifile, uint32_t offset, const char *tag, ChunkType *chunkType, uint32_t chunkLen)
{
Chunk *chunk = NULL;
FmtChunk *fc;
uint8_t buffer[16];
if (fread(buffer, 1, 16, ifile) != 16) {
WaveError = "Short file";
goto exit;
}
if ((chunk = newChunk(tag, chunkLen, offset, sizeof(*fc))) == NULL) {
goto exit;
}
fc = (FmtChunk *)chunk;
fc->type = readUInt16(buffer);
fc->channels = readUInt16(buffer+2);
fc->sample_rate = readUInt32(buffer+4);
fc->bytes_sec = readUInt32(buffer+8);
fc->block_align = readUInt16(buffer+12);
fc->bits_samp = readUInt16(buffer+14);
exit:
return chunk;
}
/**
* Read an audio data chunk. This is the big one. We don't
* actually read the data, we just make a note of where it is
* in the file.
*/
static Chunk *
readData(FILE *ifile, uint32_t offset, const char *tag, ChunkType *chunkType, uint32_t chunkLen)
{
Chunk *chunk = NULL;
DataChunk *dc;
if ((chunk = newChunk(tag, chunkLen, offset, sizeof(*dc))) == NULL) {
goto exit;
}
dc = (DataChunk *)chunk;
dc->data = NULL;
exit:
return chunk;
}
#if 0
static Chunk *
readCues(FILE *ifile, uint32_t offset, const char *tag, ChunkType *chunkType, uint32_t chunkLen)
{
return NULL;
}
static Chunk *
readLabl(FILE *ifile, uint32_t offset, const char *tag, ChunkType *chunkType, uint32_t chunkLen)
{
return NULL;
}
static Chunk *
readLtxt(FILE *ifile, uint32_t offset, const char *tag, ChunkType *chunkType, uint32_t chunkLen)
{
return NULL;
}
#endif
static Chunk *
readId3(FILE *ifile, uint32_t offset, const char *tag, ChunkType *chunkType, uint32_t chunkLen)
{
Chunk *chunk = NULL;
Id3v2Chunk *ic;
uint8_t *buffer = NULL;
if ((chunk = newChunk(tag, chunkLen, offset, sizeof(*chunk))) == NULL) {
goto exit;
}
ic = (Id3v2Chunk *)chunk;
ic->id3v2 = ReadId3V2(ifile, offset+8);
exit:
free(buffer);
return chunk;
}
/**
* Read a text that contains a single string
*/
static Chunk *
readText(FILE *ifile, uint32_t offset, const char *tag, ChunkType *chunkType, uint32_t chunkLen)
{
Chunk *chunk = NULL;
TextChunk *tc;
if ((chunk = newChunk(tag, chunkLen, offset, sizeof(*chunk)+chunkLen)) == NULL) {
goto exit;
}
tc = (TextChunk *)chunk;
if (fread(tc->string, 1, chunkLen, ifile) != chunkLen) {
WaveError = "Short file";
goto exit;
}
exit:
return chunk;
}
#if 0
static Chunk *
readInt16(FILE *ifile, uint32_t offset, const char *tag, ChunkType *chunkType, uint32_t chunkLen)
{
return NULL;
}
#endif
/**
* Read a chunk that holds a single 32-bit integer value
*/
static Chunk *
readInt32(FILE *ifile, uint32_t offset, const char *tag, ChunkType *chunkType, uint32_t chunkLen)
{
Chunk *chunk = NULL;
IntChunk *ic;
char buffer[4];
if ((chunk = newChunk(tag, chunkLen, offset, sizeof(*ic))) == NULL) {
goto exit;
}
ic = (IntChunk *)chunk;
if (fread(buffer, 1, 4, ifile) != 4) {
WaveError = "Short file";
goto exit;
}
ic->n = readUInt32(buffer);
exit:
return chunk;
}
/*** WRITE WAV FILE */
static void computeSizes(Chunk *);
void
WriteWaveFile(WaveChunk *wave, FILE *src, FILE *dst)
{
uint32_t offset = 0;
/* Recurse through all of the data structures, writing
* to the output file. We need to recompute the
* offset as we go, replacing the offsets in the chunk
* headers.
*/
computeSizes((Chunk *)wave);
writeWave(wave, src, dst, &offset);
}
/**
* Compute the length value of this chunk.
*/
static void
computeSizes(Chunk *chunk)
{
Chunk *child = NULL;
uint32_t length = 0;
/* The vast majority of the time, this value is already
* in the header and doesn't need to be changed.
* The exception is wave headers and list headers,
* which need to sum up their children.
*/
if (strncasecmp(chunk->identifier, "riff", 4) == 0 ||
strncasecmp(chunk->identifier, "list", 4) == 0)
{
ListChunk *lc = (ListChunk *)chunk;
child = lc->children;
length = 4;
}
if (child != NULL)
{
for (; child != NULL; child = child->next) {
computeSizes(child);
length += child->length + 8;
}
chunk->length = length;
}
}
static void
writeWave(WaveChunk *wave, FILE *src, FILE *dst, uint32_t *offset)
{
Chunk *child;
uint8_t buffer[12];
memcpy(buffer, wave->header.identifier, 4);
writeUInt32(buffer+4, wave->header.length);
memcpy(buffer+8, wave->type, 4);
fwrite(buffer, 1, sizeof(buffer), dst);
*offset += sizeof(buffer);
for (child = wave->children; child != NULL; child = child->next)
{
writeChunk(child, src, dst, offset);
}
}
static void
writeChunk(Chunk *chunk, FILE *src, FILE *dst, uint32_t *offset)
{
int i;
for (i=0; i < NA(chunkTypes); ++i) {
if (strncasecmp(chunk->identifier, chunkTypes[i].tag, 4) == 0) {
chunkTypes[i].writer(chunk, src, dst, offset);
return;
}
}
/* Unknown chunk type, return a generic chunk. We read
* the header, but leave the data in the file.
*/
writeData(chunk, src, dst, offset);
}
static void
writeList(Chunk *chunk, FILE *src, FILE *dst, uint32_t *offset)
{
ListChunk *lc = (ListChunk *)chunk;
Chunk *child;
char buffer[12];
memcpy(buffer, chunk->identifier, 4);
writeUInt32(buffer+4, chunk->length);
memcpy(buffer+8, lc->type, 4);
fwrite(buffer, 1, sizeof(buffer), dst);
*offset += sizeof(buffer);
for (child = lc->children; child != NULL; child = child->next)
{
writeChunk(child, src, dst, offset);
}
}
static void
writeFmt(Chunk *chunk, FILE *src, FILE *dst, uint32_t *offset)
{
FmtChunk *fc = (FmtChunk *)chunk;
char buffer[24];
memcpy(buffer, chunk->identifier, 4);
writeUInt32(buffer+4, 16);
writeUInt16(buffer+8, fc->type);
writeUInt16(buffer+10, fc->channels);
writeUInt32(buffer+12, fc->sample_rate);
writeUInt32(buffer+16, fc->bytes_sec);
writeUInt16(buffer+20, fc->block_align);
writeUInt16(buffer+22, fc->bits_samp);
fwrite(buffer, 1, sizeof(buffer), dst);
*offset += sizeof(buffer);
}
/**
* Write a data chunk. This is the only write function that
* references the source file, and only if the "data" field
* is NULL.
*/
static void
writeData(Chunk *chunk, FILE *src, FILE *dst, uint32_t *offset)
{
DataChunk *dc = (DataChunk *)chunk;
char buffer[8];
memcpy(buffer, chunk->identifier, 4);
writeUInt32(buffer+4, chunk->length);
fwrite(buffer, 1, sizeof(buffer), dst);
*offset += sizeof(buffer);
if (dc->data != NULL)
{
fwrite(dc->data, 1, chunk->length, dst);
}
else
{
/* Copy from src => dst */
char buf2[1024];
size_t len = (size_t) chunk->length;
size_t l;
fseek(src, (long)(chunk->offset+8), SEEK_SET);
while (len > 0) {
l = len > sizeof(buf2) ? sizeof(buf2) : len;
l = fread(buf2, 1, l, src);
if (l == 0) {
fprintf(stderr, "Error reading data from source file, %s\n",
strerror(errno));
break;
}
fwrite(buf2, 1, l, dst);
len -= l;
}
}
*offset += chunk->length;
}
#if 0
static void
writeCues(Chunk *chunk, FILE *src, FILE *dst, uint32_t *offset)
{
}
static void
writeLabl(Chunk *chunk, FILE *src, FILE *dst, uint32_t *offset)
{
}
static void
writeLtxt(Chunk *chunk, FILE *src, FILE *dst, uint32_t *offset)
{
}
#endif
/**
* Write an "id3 " chunk to the file. Write the 8-bye header
* here, let libid3 write the rest.
*/
static void
writeId3(Chunk *chunk, FILE *src, FILE *dst, uint32_t *offset)
{
Id3v2Chunk *ic = (Id3v2Chunk *)chunk;
char buffer[8];
int l;
static uint8_t pad = 0;
memcpy(buffer, chunk->identifier, 4);
writeUInt32(buffer+4, chunk->length);
fwrite(buffer, 1, sizeof(buffer), dst);
l = WriteId3V2(src, dst, ic->id3v2);
for (; l < chunk->length; ++l) {
fwrite(&pad, 1, 1, dst);
}
}
static void
writeText(Chunk *chunk, FILE *src, FILE *dst, uint32_t *offset)
{
TextChunk *tc = (TextChunk *)chunk;
char buffer[8];
memcpy(buffer, chunk->identifier, 4);
writeUInt32(buffer+4, chunk->length);
fwrite(buffer, 1, sizeof(buffer), dst);
fwrite(tc->string, 1, chunk->length, dst);
*offset += sizeof(buffer) + chunk->length;
}
#if 0
static void
writeInt16(Chunk *chunk, FILE *src, FILE *dst, uint32_t *offset)
{
}
#endif
static void
writeInt32(Chunk *chunk, FILE *src, FILE *dst, uint32_t *offset)
{
IntChunk *ic = (IntChunk *)chunk;
char buffer[12];
memcpy(buffer, chunk->identifier, 4);
writeUInt32(buffer+4, 4);
writeUInt32(buffer+8, ic->n);
fwrite(buffer, 1, sizeof(buffer), dst);
*offset += sizeof(buffer);
}
/*** UTILITIES ***/
/**
* Allocate the space for a chunk, and initialize the header
* @param tag chunk tag, 4 characters
* @param length Number of data bytes after the header
* @param offset File offset of the data, if applicable
* @param size Amount of space to allocate for this chunk
*/
Chunk *
newChunk(const char *tag, uint32_t length, uint32_t offset, size_t size)
{
Chunk *chunk;
if ((chunk = malloc(size)) == NULL) {
WaveError = "Out of memory";
goto exit;
}
memcpy(chunk->identifier, tag, 4);
chunk->length = length;
chunk->offset = offset;
chunk->next = NULL;
exit:
return chunk;
}