Annotation of src/usr.bin/aucat/headers.c, Revision 1.16
1.16 ! ratchov 1: /* $OpenBSD: headers.c,v 1.15 2010/05/25 06:27:41 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... */
151: } else {
152: if (bits != 8) {
153: warnx("%u: mulaw/alaw encoding not 8-bit", bits);
154: return 0;
155: }
156: par->bits = 8 * sizeof(short);
157: par->bps = sizeof(short);
158: par->le = NATIVE_LE;
159: par->sig = 1;
160: }
1.1 ratchov 161: par->msb = 1;
162: par->cmax = cmax;
163: par->rate = rate;
164: return 1;
165: }
166:
167: int
1.13 ratchov 168: wav_readhdr(int fd, struct aparams *par, off_t *startpos, off_t *datasz, short **map)
1.1 ratchov 169: {
170: struct wavriff riff;
171: struct wavchunk chunk;
172: unsigned csize, rsize, pos = 0;
173: int fmt_done = 0;
174:
175: if (lseek(fd, 0, SEEK_SET) < 0) {
176: warn("lseek: 0");
177: return 0;
178: }
179: if (read(fd, &riff, sizeof(riff)) != sizeof(riff)) {
1.8 ratchov 180: warn("wav_readhdr: header");
1.1 ratchov 181: return 0;
182: }
183: if (memcmp(&riff.magic, &wav_id_riff, 4) != 0 ||
184: memcmp(&riff.type, &wav_id_wave, 4)) {
185: warnx("not a wave file");
186: return 0;
187: }
188: rsize = letoh32(riff.size);
189: while (pos + sizeof(struct wavchunk) <= rsize) {
190: if (read(fd, &chunk, sizeof(chunk)) != sizeof(chunk)) {
1.8 ratchov 191: warn("wav_readhdr: chunk");
1.1 ratchov 192: return 0;
193: }
194: csize = letoh32(chunk.size);
195: if (memcmp(chunk.id, wav_id_fmt, 4) == 0) {
1.6 ratchov 196: if (!wav_readfmt(fd, csize, par, map))
1.1 ratchov 197: return 0;
198: fmt_done = 1;
199: } else if (memcmp(chunk.id, wav_id_data, 4) == 0) {
1.13 ratchov 200: *startpos = pos;
1.1 ratchov 201: *datasz = csize;
202: break;
203: } else {
1.10 ratchov 204: #ifdef DEBUG
1.14 ratchov 205: if (debug_level >= 2)
1.10 ratchov 206: warnx("ignoring chuck <%.4s>\n", chunk.id);
207: #endif
1.1 ratchov 208: }
209:
210: /*
211: * next chunk
212: */
213: pos += sizeof(struct wavchunk) + csize;
214: if (lseek(fd, sizeof(riff) + pos, SEEK_SET) < 0) {
215: warn("lseek");
216: return 0;
217: }
218: }
219: if (!fmt_done) {
220: warnx("missing format chunk");
221: return 0;
222: }
223: return 1;
224: }
225:
1.13 ratchov 226: /*
227: * Write header and seek to start position
228: */
1.1 ratchov 229: int
1.13 ratchov 230: wav_writehdr(int fd, struct aparams *par, off_t *startpos, off_t datasz)
1.1 ratchov 231: {
232: unsigned nch = par->cmax - par->cmin + 1;
233: struct {
234: struct wavriff riff;
235: struct wavchunk fmt_hdr;
236: struct wavfmt fmt;
237: struct wavchunk data_hdr;
238: } hdr;
239:
240: /*
1.7 ratchov 241: * Check that encoding is supported by .wav file format.
1.1 ratchov 242: */
243: if (par->bits > 8 && !par->le) {
244: warnx("samples must be little endian");
245: return 0;
246: }
247: if (8 * par->bps - par->bits >= 8) {
248: warnx("padding must be less than 8 bits");
249: return 0;
250: }
251: if ((par->bits <= 8 && par->sig) || (par->bits > 8 && !par->sig)) {
1.7 ratchov 252: warnx("samples with more (less) than 8 bits must be signed "
253: "(unsigned)");
1.1 ratchov 254: return 0;
255: }
256: if (8 * par->bps != par->bits && !par->msb) {
257: warnx("samples must be MSB justified");
258: return 0;
259: }
260:
261: memcpy(hdr.riff.magic, wav_id_riff, 4);
262: memcpy(hdr.riff.type, wav_id_wave, 4);
263: hdr.riff.size = htole32(datasz + sizeof(hdr) - sizeof(hdr.riff));
264:
265: memcpy(hdr.fmt_hdr.id, wav_id_fmt, 4);
266: hdr.fmt_hdr.size = htole32(sizeof(hdr.fmt));
267: hdr.fmt.fmt = htole16(1);
268: hdr.fmt.nch = htole16(nch);
269: hdr.fmt.rate = htole32(par->rate);
270: hdr.fmt.byterate = htole32(par->rate * par->bps * nch);
271: hdr.fmt.bits = htole16(par->bits);
1.15 ratchov 272: hdr.fmt.blkalign = par->bps * nch;
1.1 ratchov 273:
274: memcpy(hdr.data_hdr.id, wav_id_data, 4);
275: hdr.data_hdr.size = htole32(datasz);
1.4 ratchov 276:
1.1 ratchov 277: if (lseek(fd, 0, SEEK_SET) < 0) {
278: warn("wav_writehdr: lseek");
279: return 0;
280: }
281: if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
282: warn("wav_writehdr: write");
283: return 0;
284: }
1.13 ratchov 285: *startpos = sizeof(hdr);
1.1 ratchov 286: return 1;
287: }