Annotation of src/usr.bin/aucat/headers.c, Revision 1.4
1.4 ! ratchov 1: /* $OpenBSD: headers.c,v 1.3 2008/11/16 17:08:32 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>
22: #include <stdlib.h>
23: #include <string.h>
24: #include <unistd.h>
25:
26: #include "conf.h"
27: #include "aparams.h"
28:
29: struct wavriff {
30: char magic[4];
31: uint32_t size;
32: char type[4];
33: } __packed;
34:
35: struct wavchunk {
36: char id[4];
37: uint32_t size;
38: } __packed;
39:
40: struct wavfmt {
41: uint16_t fmt;
42: uint16_t nch;
43: uint32_t rate;
44: uint32_t byterate;
45: uint16_t blkalign;
1.4 ! ratchov 46: uint16_t bits;
1.1 ratchov 47: } __packed;
48:
49: char wav_id_riff[4] = { 'R', 'I', 'F', 'F' };
50: char wav_id_wave[4] = { 'W', 'A', 'V', 'E' };
51: char wav_id_data[4] = { 'd', 'a', 't', 'a' };
52: char wav_id_fmt[4] = { 'f', 'm', 't', ' ' };
53:
54: int
55: wav_readfmt(int fd, unsigned csize, struct aparams *par)
56: {
57: struct wavfmt fmt;
58: unsigned nch, cmax, rate, bits, enc;
59:
60: if (csize < sizeof(fmt)) {
61: warnx("bogus format chunk");
62: return 0;
63: }
64: if (read(fd, &fmt, sizeof(fmt)) != sizeof(fmt)) {
65: warn("riff_read: chunk");
66: return 0;
67: }
68: enc = letoh16(fmt.fmt);
69: if (enc != 1) {
70: warnx("%u: only \"pcm\" encoding supported", enc);
71: return 0;
72: }
73: nch = letoh16(fmt.nch);
74: if (nch == 0) {
75: warnx("zero number of channels");
76: return 0;
77: }
78: cmax = par->cmin + nch - 1;
1.2 ratchov 79: if (cmax >= NCHAN_MAX) {
1.1 ratchov 80: warnx("%u:%u: bad range", par->cmin, cmax);
81: return 0;
82: }
83: rate = letoh32(fmt.rate);
84: if (rate < RATE_MIN || rate >= RATE_MAX) {
85: warnx("%u: bad sample rate", rate);
86: return 0;
87: }
88: bits = letoh16(fmt.bits);
89: if (bits == 0 || bits >= 32) {
90: warnx("%u: bad number of bits", bits);
91: return 0;
92: }
93: par->bps = (bits + 7) / 8;
94: par->bits = bits;
95: par->le = 1;
96: par->sig = (bits <= 8) ? 0 : 1; /* ask microsoft why... */
97: par->msb = 1;
98: par->cmax = cmax;
99: par->rate = rate;
1.3 ratchov 100: #ifdef DEBUG
1.1 ratchov 101: if (debug_level > 0) {
102: fprintf(stderr, "wav_readfmt: using ");
103: aparams_print(par);
104: fprintf(stderr, "\n");
105: }
1.3 ratchov 106: #endif
1.1 ratchov 107: return 1;
108: }
109:
110: int
111: wav_readhdr(int fd, struct aparams *par, off_t *datasz)
112: {
113: struct wavriff riff;
114: struct wavchunk chunk;
115: unsigned csize, rsize, pos = 0;
116: int fmt_done = 0;
117:
118: if (lseek(fd, 0, SEEK_SET) < 0) {
119: warn("lseek: 0");
120: return 0;
121: }
122: if (read(fd, &riff, sizeof(riff)) != sizeof(riff)) {
123: warn("riff_read: header");
124: return 0;
125: }
126: if (memcmp(&riff.magic, &wav_id_riff, 4) != 0 ||
127: memcmp(&riff.type, &wav_id_wave, 4)) {
128: warnx("not a wave file");
129: return 0;
130: }
131: rsize = letoh32(riff.size);
132: while (pos + sizeof(struct wavchunk) <= rsize) {
133: if (read(fd, &chunk, sizeof(chunk)) != sizeof(chunk)) {
134: warn("riff_read: chunk");
135: return 0;
136: }
137: csize = letoh32(chunk.size);
138: if (memcmp(chunk.id, wav_id_fmt, 4) == 0) {
139: if (!wav_readfmt(fd, csize, par))
140: return 0;
141: fmt_done = 1;
142: } else if (memcmp(chunk.id, wav_id_data, 4) == 0) {
143: *datasz = csize;
144: break;
145: } else {
146: DPRINTF("unknown chunk: <%.4s>\n", chunk.id);
147: }
148:
149: /*
150: * next chunk
151: */
152: pos += sizeof(struct wavchunk) + csize;
153: if (lseek(fd, sizeof(riff) + pos, SEEK_SET) < 0) {
154: warn("lseek");
155: return 0;
156: }
157: }
158: if (!fmt_done) {
159: warnx("missing format chunk");
160: return 0;
161: }
162: return 1;
163: }
164:
165: int
166: wav_writehdr(int fd, struct aparams *par)
167: {
168: off_t datasz;
169: unsigned nch = par->cmax - par->cmin + 1;
170: struct {
171: struct wavriff riff;
172: struct wavchunk fmt_hdr;
173: struct wavfmt fmt;
174: struct wavchunk data_hdr;
175: } hdr;
176:
177: datasz = lseek(fd, 0, SEEK_CUR);
178: if (datasz < 0) {
179: warn("wav_writehdr: lseek(end)");
180: return 0;
181: }
182: if (datasz >= sizeof(hdr))
183: datasz -= sizeof(hdr);
184: else
185: datasz = 0;
186:
187: /*
188: * check that encoding is supported by .wav file format
189: */
190: if (par->bits > 8 && !par->le) {
191: warnx("samples must be little endian");
192: return 0;
193: }
194: if (8 * par->bps - par->bits >= 8) {
195: warnx("padding must be less than 8 bits");
196: return 0;
197: }
198: if ((par->bits <= 8 && par->sig) || (par->bits > 8 && !par->sig)) {
199: warnx("samples with more (less) than 8 bits must be signed (unsigned)");
200: return 0;
201: }
202: if (8 * par->bps != par->bits && !par->msb) {
203: warnx("samples must be MSB justified");
204: return 0;
205: }
206:
207: memcpy(hdr.riff.magic, wav_id_riff, 4);
208: memcpy(hdr.riff.type, wav_id_wave, 4);
209: hdr.riff.size = htole32(datasz + sizeof(hdr) - sizeof(hdr.riff));
210:
211: memcpy(hdr.fmt_hdr.id, wav_id_fmt, 4);
212: hdr.fmt_hdr.size = htole32(sizeof(hdr.fmt));
213: hdr.fmt.fmt = htole16(1);
214: hdr.fmt.nch = htole16(nch);
215: hdr.fmt.rate = htole32(par->rate);
216: hdr.fmt.byterate = htole32(par->rate * par->bps * nch);
217: hdr.fmt.bits = htole16(par->bits);
218: hdr.fmt.blkalign = 0;
219:
220: memcpy(hdr.data_hdr.id, wav_id_data, 4);
221: hdr.data_hdr.size = htole32(datasz);
1.4 ! ratchov 222:
1.1 ratchov 223: if (lseek(fd, 0, SEEK_SET) < 0) {
224: warn("wav_writehdr: lseek");
225: return 0;
226: }
227: if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
228: warn("wav_writehdr: write");
229: return 0;
230: }
231: return 1;
232: }