Annotation of src/usr.bin/aucat/headers.c, Revision 1.21
1.21 ! ratchov 1: /* $OpenBSD: headers.c,v 1.20 2011/10/12 07:20:04 ratchov Exp $ */
1.1 ratchov 2: /*
3: * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17:
18: #include <sys/param.h>
19:
20: #include <err.h>
21: #include <stdio.h>
1.16 ratchov 22: #include <stdint.h>
1.1 ratchov 23: #include <stdlib.h>
24: #include <string.h>
25: #include <unistd.h>
26:
1.7 ratchov 27: #include "aparams.h"
1.1 ratchov 28: #include "conf.h"
1.6 ratchov 29: #include "wav.h"
30:
31: /*
1.7 ratchov 32: * Encoding IDs used in .wav headers.
1.6 ratchov 33: */
34: #define WAV_ENC_PCM 1
35: #define WAV_ENC_ALAW 6
36: #define WAV_ENC_ULAW 7
1.15 ratchov 37: #define WAV_ENC_EXT 0xfffe
1.1 ratchov 38:
39: struct wavriff {
40: char magic[4];
41: uint32_t size;
42: char type[4];
43: } __packed;
44:
45: struct wavchunk {
46: char id[4];
47: uint32_t size;
48: } __packed;
49:
50: struct wavfmt {
51: uint16_t fmt;
52: uint16_t nch;
53: uint32_t rate;
54: uint32_t byterate;
55: uint16_t blkalign;
1.4 ratchov 56: uint16_t bits;
1.15 ratchov 57: #define WAV_FMT_SIZE 16
58: #define WAV_FMT_SIZE2 (16 + 2)
59: #define WAV_FMT_EXT_SIZE (16 + 24)
60: uint16_t extsize;
61: uint16_t valbits;
62: uint32_t chanmask;
63: uint16_t extfmt;
64: char guid[14];
1.1 ratchov 65: } __packed;
66:
67: char wav_id_riff[4] = { 'R', 'I', 'F', 'F' };
68: char wav_id_wave[4] = { 'W', 'A', 'V', 'E' };
69: char wav_id_data[4] = { 'd', 'a', 't', 'a' };
70: char wav_id_fmt[4] = { 'f', 'm', 't', ' ' };
1.15 ratchov 71: char wav_guid[14] = {
72: 0x00, 0x00, 0x00, 0x00,
73: 0x10, 0x00, 0x80, 0x00,
74: 0x00, 0xAA, 0x00, 0x38,
75: 0x9B, 0x71
76: };
1.1 ratchov 77:
78: int
1.6 ratchov 79: wav_readfmt(int fd, unsigned csize, struct aparams *par, short **map)
1.1 ratchov 80: {
81: struct wavfmt fmt;
1.15 ratchov 82: unsigned nch, cmax, rate, bits, bps, enc;
1.1 ratchov 83:
1.15 ratchov 84: if (csize < WAV_FMT_SIZE) {
85: warnx("%u: bugus format chunk size", csize);
1.1 ratchov 86: return 0;
87: }
1.15 ratchov 88: if (csize > WAV_FMT_EXT_SIZE)
89: csize = WAV_FMT_EXT_SIZE;
90: if (read(fd, &fmt, csize) != csize) {
1.1 ratchov 91: warn("riff_read: chunk");
92: return 0;
93: }
94: enc = letoh16(fmt.fmt);
1.15 ratchov 95: bits = letoh16(fmt.bits);
96: if (enc == WAV_ENC_EXT) {
97: if (csize != WAV_FMT_EXT_SIZE) {
98: warnx("missing extended format chunk in .wav file");
99: return 0;
100: }
101: if (memcmp(fmt.guid, wav_guid, sizeof(wav_guid)) != 0) {
102: warnx("unknown format (GUID) in .wav file");
103: return 0;
104: }
105: bps = (bits + 7) / 8;
106: bits = letoh16(fmt.valbits);
107: enc = letoh16(fmt.extfmt);
108: } else
109: bps = (bits + 7) / 8;
1.6 ratchov 110: switch (enc) {
111: case WAV_ENC_PCM:
112: *map = NULL;
113: break;
114: case WAV_ENC_ALAW:
115: *map = wav_alawmap;
116: break;
117: case WAV_ENC_ULAW:
118: *map = wav_ulawmap;
119: break;
120: default:
121: errx(1, "%u: unsupported encoding in .wav file", enc);
1.1 ratchov 122: }
123: nch = letoh16(fmt.nch);
124: if (nch == 0) {
125: warnx("zero number of channels");
126: return 0;
127: }
128: cmax = par->cmin + nch - 1;
1.2 ratchov 129: if (cmax >= NCHAN_MAX) {
1.1 ratchov 130: warnx("%u:%u: bad range", par->cmin, cmax);
131: return 0;
132: }
133: rate = letoh32(fmt.rate);
1.9 ratchov 134: if (rate < RATE_MIN || rate > RATE_MAX) {
1.1 ratchov 135: warnx("%u: bad sample rate", rate);
136: return 0;
137: }
1.9 ratchov 138: if (bits == 0 || bits > 32) {
1.1 ratchov 139: warnx("%u: bad number of bits", bits);
140: return 0;
141: }
1.15 ratchov 142: if (bits > bps * 8) {
143: warnx("%u: bits larger than bytes-per-sample", bps);
144: return 0;
145: }
1.6 ratchov 146: if (enc == WAV_ENC_PCM) {
1.15 ratchov 147: par->bps = bps;
1.6 ratchov 148: par->bits = bits;
149: par->le = 1;
150: par->sig = (bits <= 8) ? 0 : 1; /* ask microsoft why... */
1.19 ratchov 151: par->msb = 1;
1.6 ratchov 152: } else {
153: if (bits != 8) {
154: warnx("%u: mulaw/alaw encoding not 8-bit", bits);
155: return 0;
156: }
1.19 ratchov 157: par->bits = ADATA_BITS;
158: par->bps = sizeof(adata_t);
159: par->le = ADATA_LE;
1.6 ratchov 160: par->sig = 1;
1.19 ratchov 161: par->msb = 0;
1.6 ratchov 162: }
1.1 ratchov 163: par->cmax = cmax;
164: par->rate = rate;
165: return 1;
166: }
167:
168: int
1.13 ratchov 169: wav_readhdr(int fd, struct aparams *par, off_t *startpos, off_t *datasz, short **map)
1.1 ratchov 170: {
171: struct wavriff riff;
172: struct wavchunk chunk;
173: unsigned csize, rsize, pos = 0;
174: int fmt_done = 0;
175:
176: if (lseek(fd, 0, SEEK_SET) < 0) {
177: warn("lseek: 0");
178: return 0;
179: }
180: if (read(fd, &riff, sizeof(riff)) != sizeof(riff)) {
1.8 ratchov 181: warn("wav_readhdr: header");
1.1 ratchov 182: return 0;
183: }
184: if (memcmp(&riff.magic, &wav_id_riff, 4) != 0 ||
185: memcmp(&riff.type, &wav_id_wave, 4)) {
186: warnx("not a wave file");
187: return 0;
188: }
189: rsize = letoh32(riff.size);
1.17 ratchov 190: for (;;) {
191: if (pos + sizeof(struct wavchunk) > rsize) {
192: warnx("missing data chunk");
193: return 0;
194: }
1.1 ratchov 195: if (read(fd, &chunk, sizeof(chunk)) != sizeof(chunk)) {
1.8 ratchov 196: warn("wav_readhdr: chunk");
1.1 ratchov 197: return 0;
198: }
199: csize = letoh32(chunk.size);
200: if (memcmp(chunk.id, wav_id_fmt, 4) == 0) {
1.6 ratchov 201: if (!wav_readfmt(fd, csize, par, map))
1.1 ratchov 202: return 0;
203: fmt_done = 1;
204: } else if (memcmp(chunk.id, wav_id_data, 4) == 0) {
1.18 ratchov 205: *startpos = pos + sizeof(riff) + sizeof(chunk);
1.1 ratchov 206: *datasz = csize;
207: break;
208: } else {
1.10 ratchov 209: #ifdef DEBUG
1.14 ratchov 210: if (debug_level >= 2)
1.21 ! ratchov 211: warnx("ignoring chunk <%.4s>\n", chunk.id);
1.10 ratchov 212: #endif
1.1 ratchov 213: }
214:
215: /*
216: * next chunk
217: */
218: pos += sizeof(struct wavchunk) + csize;
219: if (lseek(fd, sizeof(riff) + pos, SEEK_SET) < 0) {
220: warn("lseek");
221: return 0;
222: }
223: }
224: if (!fmt_done) {
225: warnx("missing format chunk");
226: return 0;
227: }
228: return 1;
229: }
230:
1.13 ratchov 231: /*
232: * Write header and seek to start position
233: */
1.1 ratchov 234: int
1.13 ratchov 235: wav_writehdr(int fd, struct aparams *par, off_t *startpos, off_t datasz)
1.1 ratchov 236: {
237: unsigned nch = par->cmax - par->cmin + 1;
238: struct {
239: struct wavriff riff;
240: struct wavchunk fmt_hdr;
241: struct wavfmt fmt;
242: struct wavchunk data_hdr;
1.20 ratchov 243: } __packed hdr;
1.1 ratchov 244:
245: /*
1.7 ratchov 246: * Check that encoding is supported by .wav file format.
1.1 ratchov 247: */
248: if (par->bits > 8 && !par->le) {
249: warnx("samples must be little endian");
250: return 0;
251: }
252: if (8 * par->bps - par->bits >= 8) {
253: warnx("padding must be less than 8 bits");
254: return 0;
255: }
256: if ((par->bits <= 8 && par->sig) || (par->bits > 8 && !par->sig)) {
1.7 ratchov 257: warnx("samples with more (less) than 8 bits must be signed "
258: "(unsigned)");
1.1 ratchov 259: return 0;
260: }
261: if (8 * par->bps != par->bits && !par->msb) {
262: warnx("samples must be MSB justified");
263: return 0;
264: }
265:
266: memcpy(hdr.riff.magic, wav_id_riff, 4);
267: memcpy(hdr.riff.type, wav_id_wave, 4);
268: hdr.riff.size = htole32(datasz + sizeof(hdr) - sizeof(hdr.riff));
269:
270: memcpy(hdr.fmt_hdr.id, wav_id_fmt, 4);
271: hdr.fmt_hdr.size = htole32(sizeof(hdr.fmt));
272: hdr.fmt.fmt = htole16(1);
273: hdr.fmt.nch = htole16(nch);
274: hdr.fmt.rate = htole32(par->rate);
275: hdr.fmt.byterate = htole32(par->rate * par->bps * nch);
1.20 ratchov 276: hdr.fmt.blkalign = par->bps * nch;
1.1 ratchov 277: hdr.fmt.bits = htole16(par->bits);
278:
279: memcpy(hdr.data_hdr.id, wav_id_data, 4);
280: hdr.data_hdr.size = htole32(datasz);
1.4 ratchov 281:
1.1 ratchov 282: if (lseek(fd, 0, SEEK_SET) < 0) {
283: warn("wav_writehdr: lseek");
284: return 0;
285: }
286: if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
287: warn("wav_writehdr: write");
288: return 0;
289: }
1.13 ratchov 290: *startpos = sizeof(hdr);
1.1 ratchov 291: return 1;
292: }