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.
18 /* This is the logical arrangement of the struct but we are splitting it
19 * because it is easier to handle the variable length extradata that way.
21 * strongly inspired by http://www.mpg123.de/mpg123/mpg123/wav.c
33 uint32_t nSamplesPerSec;
34 uint32_t nAvgBytesPerSec;
36 uint16_t wBitsPerSample;
37 uint16_t cbSize; /* cbSize = sizeof(extradata) */
45 /* from here you insert your PCM data */
53 uint32_t nSamplesPerSec;
54 uint32_t nAvgBytesPerSec;
56 uint16_t wBitsPerSample;
59 } __attribute__((__packed__));
64 } __attribute__((__packed__));
77 uint16_t len; /* same storage size as fmt.cbSize */
82 void write_wav_header(FILE *file, struct fmt *format,
83 struct extradata *extradata, unsigned data_len)
91 { 'W','A','V','E','f','m','t',' ' },
100 format->cbSize = extradata->len;
101 w.fmtlen = sizeof(*format) + format->cbSize;
102 r.WAVElen = sizeof(w) + w.fmtlen + sizeof(d);
103 d.datalen = data_len;
106 fwrite(&r.riffheader, 1, 4, file);
107 fwrite(&htole32(r.WAVElen), 4, 1, file);
110 fwrite(&w.fmtheader, 1, 8, file);
111 fwrite(&htole32(w.fmtlen), 4, 1, file);
114 fwrite(&htole16(format->wFormatTag), 2, 1, file);
115 fwrite(&htole16(format->nChannels), 2, 1, file);
116 fwrite(&htole32(format->nSamplesPerSec), 4, 1, file);
117 fwrite(&htole32(format->nAvgBytesPerSec), 4, 1, file);
118 fwrite(&htole16(format->nBlockAlign), 2, 1, file);
119 fwrite(&htole16(format->wBitsPerSample), 2, 1, file);
120 fwrite(&htole16(format->cbSize), 2, 1, file);
123 fwrite(extradata->data, 1, extradata->len, file);
126 fwrite(&d.dataheader, 1, 4, file);
127 fwrite(&htole32(d.datalen), 4, 1, file);
132 struct fmt format = {
135 .nSamplesPerSec = 48000,
136 .nAvgBytesPerSec = 192000 / 8,
137 .wBitsPerSample = 16,
142 static struct extradata codec_private_data = {
144 .data = "\x00\x88\x00\x00\x0f\x00\x00\x00\x00\x00",
151 write_wav_header(stdout, &format, &codec_private_data, data_len);