Annotation of src/usr.bin/aucat/headers.c, Revision 1.2
1.2 ! ratchov 1: /* $OpenBSD: headers.c,v 1.1 2008/05/23 07:15:46 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;
46: uint16_t bits;
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;
100: if (debug_level > 0) {
101: fprintf(stderr, "wav_readfmt: using ");
102: aparams_print(par);
103: fprintf(stderr, "\n");
104: }
105: return 1;
106: }
107:
108: int
109: wav_readhdr(int fd, struct aparams *par, off_t *datasz)
110: {
111: struct wavriff riff;
112: struct wavchunk chunk;
113: unsigned csize, rsize, pos = 0;
114: int fmt_done = 0;
115:
116: if (lseek(fd, 0, SEEK_SET) < 0) {
117: warn("lseek: 0");
118: return 0;
119: }
120: if (read(fd, &riff, sizeof(riff)) != sizeof(riff)) {
121: warn("riff_read: header");
122: return 0;
123: }
124: if (memcmp(&riff.magic, &wav_id_riff, 4) != 0 ||
125: memcmp(&riff.type, &wav_id_wave, 4)) {
126: warnx("not a wave file");
127: return 0;
128: }
129: rsize = letoh32(riff.size);
130: while (pos + sizeof(struct wavchunk) <= rsize) {
131: if (read(fd, &chunk, sizeof(chunk)) != sizeof(chunk)) {
132: warn("riff_read: chunk");
133: return 0;
134: }
135: csize = letoh32(chunk.size);
136: if (memcmp(chunk.id, wav_id_fmt, 4) == 0) {
137: if (!wav_readfmt(fd, csize, par))
138: return 0;
139: fmt_done = 1;
140: } else if (memcmp(chunk.id, wav_id_data, 4) == 0) {
141: *datasz = csize;
142: break;
143: } else {
144: DPRINTF("unknown chunk: <%.4s>\n", chunk.id);
145: }
146:
147: /*
148: * next chunk
149: */
150: pos += sizeof(struct wavchunk) + csize;
151: if (lseek(fd, sizeof(riff) + pos, SEEK_SET) < 0) {
152: warn("lseek");
153: return 0;
154: }
155: }
156: if (!fmt_done) {
157: warnx("missing format chunk");
158: return 0;
159: }
160: return 1;
161: }
162:
163: int
164: wav_writehdr(int fd, struct aparams *par)
165: {
166: off_t datasz;
167: unsigned nch = par->cmax - par->cmin + 1;
168: struct {
169: struct wavriff riff;
170: struct wavchunk fmt_hdr;
171: struct wavfmt fmt;
172: struct wavchunk data_hdr;
173: } hdr;
174:
175: datasz = lseek(fd, 0, SEEK_CUR);
176: if (datasz < 0) {
177: warn("wav_writehdr: lseek(end)");
178: return 0;
179: }
180: if (datasz >= sizeof(hdr))
181: datasz -= sizeof(hdr);
182: else
183: datasz = 0;
184:
185: /*
186: * check that encoding is supported by .wav file format
187: */
188: if (par->bits > 8 && !par->le) {
189: warnx("samples must be little endian");
190: return 0;
191: }
192: if (8 * par->bps - par->bits >= 8) {
193: warnx("padding must be less than 8 bits");
194: return 0;
195: }
196: if ((par->bits <= 8 && par->sig) || (par->bits > 8 && !par->sig)) {
197: warnx("samples with more (less) than 8 bits must be signed (unsigned)");
198: return 0;
199: }
200: if (8 * par->bps != par->bits && !par->msb) {
201: warnx("samples must be MSB justified");
202: return 0;
203: }
204:
205: memcpy(hdr.riff.magic, wav_id_riff, 4);
206: memcpy(hdr.riff.type, wav_id_wave, 4);
207: hdr.riff.size = htole32(datasz + sizeof(hdr) - sizeof(hdr.riff));
208:
209: memcpy(hdr.fmt_hdr.id, wav_id_fmt, 4);
210: hdr.fmt_hdr.size = htole32(sizeof(hdr.fmt));
211: hdr.fmt.fmt = htole16(1);
212: hdr.fmt.nch = htole16(nch);
213: hdr.fmt.rate = htole32(par->rate);
214: hdr.fmt.byterate = htole32(par->rate * par->bps * nch);
215: hdr.fmt.bits = htole16(par->bits);
216: hdr.fmt.blkalign = 0;
217:
218: memcpy(hdr.data_hdr.id, wav_id_data, 4);
219: hdr.data_hdr.size = htole32(datasz);
220:
221: if (lseek(fd, 0, SEEK_SET) < 0) {
222: warn("wav_writehdr: lseek");
223: return 0;
224: }
225: if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
226: warn("wav_writehdr: write");
227: return 0;
228: }
229: return 1;
230: }