2 * wav_header - write the header of a wav file
4 * Copyright (C) 2010 Antonio Ospite <ospite@studenti.unina.it>
6 * This program is free software. It comes without any warranty, to
7 * the extent permitted by applicable law. You can redistribute it
8 * and/or modify it under the terms of the Do What The Fuck You Want
9 * To Public License, Version 2, as published by Sam Hocevar. See
10 * http://sam.zoy.org/wtfpl/COPYING for more details.
20 /* This is the logical arrangement of the struct but we are splitting it
21 * because it is easier to handle the variable length extradata that way.
23 * strongly inspired by http://www.mpg123.de/mpg123/mpg123/wav.c
35 uint32_t nSamplesPerSec;
36 uint32_t nAvgBytesPerSec;
38 uint16_t wBitsPerSample;
39 uint16_t cbSize; /* cbSize = sizeof(extradata) */
47 /* from here you insert your PCM data */
55 uint32_t nSamplesPerSec;
56 uint32_t nAvgBytesPerSec;
58 uint16_t wBitsPerSample;
61 } __attribute__((__packed__));
66 } __attribute__((__packed__));
79 uint16_t len; /* same storage size as fmt.cbSize */
84 void write_wav_header(FILE *file, struct fmt *format,
85 struct extradata *extradata, unsigned data_len)
93 { 'W','A','V','E','f','m','t',' ' },
102 format->cbSize = extradata->len;
103 w.fmtlen = sizeof(*format) + format->cbSize;
104 r.WAVElen = sizeof(w) + w.fmtlen + sizeof(d);
105 d.datalen = data_len;
108 fwrite(&r.riffheader, 1, 4, file);
109 fwrite(&htole32(r.WAVElen), 4, 1, file);
112 fwrite(&w.fmtheader, 1, 8, file);
113 fwrite(&htole32(w.fmtlen), 4, 1, file);
116 fwrite(&htole16(format->wFormatTag), 2, 1, file);
117 fwrite(&htole16(format->nChannels), 2, 1, file);
118 fwrite(&htole32(format->nSamplesPerSec), 4, 1, file);
119 fwrite(&htole32(format->nAvgBytesPerSec), 4, 1, file);
120 fwrite(&htole16(format->nBlockAlign), 2, 1, file);
121 fwrite(&htole16(format->wBitsPerSample), 2, 1, file);
122 fwrite(&htole16(format->cbSize), 2, 1, file);
125 fwrite(extradata->data, 1, extradata->len, file);
128 fwrite(&d.dataheader, 1, 4, file);
129 fwrite(&htole32(d.datalen), 4, 1, file);
134 struct fmt format = {
137 .nSamplesPerSec = 48000,
138 .nAvgBytesPerSec = 192000 / 8,
139 .wBitsPerSample = 16,
144 static struct extradata codec_private_data = {
146 .data = "\x00\x88\x00\x00\x0f\x00\x00\x00\x00\x00",
153 write_wav_header(stdout, &format, &codec_private_data, data_len);