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 static inline void fwrite_le16(uint16_t x, FILE *file)
84 uint16_t tmp = htole16(x);
85 fwrite(&tmp, sizeof(tmp), 1, file);
88 static inline void fwrite_le32(uint32_t x, FILE *file)
90 uint32_t tmp = htole32(x);
91 fwrite(&tmp, sizeof(tmp), 1, file);
94 void write_wav_header(FILE *file, struct fmt *format,
95 struct extradata *extradata, unsigned data_len)
103 { 'W','A','V','E','f','m','t',' ' },
112 format->cbSize = extradata->len;
113 w.fmtlen = sizeof(*format) + format->cbSize;
114 r.WAVElen = sizeof(w) + w.fmtlen + sizeof(d);
115 d.datalen = data_len;
118 fwrite(&r.riffheader, 1, 4, file);
119 fwrite_le32(r.WAVElen, file);
122 fwrite(&w.fmtheader, 1, 8, file);
123 fwrite_le32(w.fmtlen, file);
126 fwrite_le16(format->wFormatTag, file);
127 fwrite_le16(format->nChannels, file);
128 fwrite_le32(format->nSamplesPerSec, file);
129 fwrite_le32(format->nAvgBytesPerSec, file);
130 fwrite_le16(format->nBlockAlign, file);
131 fwrite_le16(format->wBitsPerSample, file);
132 fwrite_le16(format->cbSize, file);
135 fwrite(extradata->data, 1, extradata->len, file);
138 fwrite(&d.dataheader, 1, 4, file);
139 fwrite_le32(d.datalen, file);
144 struct fmt format = {
147 .nSamplesPerSec = 48000,
148 .nAvgBytesPerSec = 192000 / 8,
149 .wBitsPerSample = 16,
154 static struct extradata codec_private_data = {
156 .data = "\x00\x88\x00\x00\x0f\x00\x00\x00\x00\x00",
163 write_wav_header(stdout, &format, &codec_private_data, data_len);