wav.c revision 1.9 1 1.9 mrg /* $NetBSD: wav.c,v 1.9 2009/06/18 02:37:27 mrg Exp $ */
2 1.3 mrg
3 1.1 mrg /*
4 1.9 mrg * Copyright (c) 2002, 2009 Matthew R. Green
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg *
16 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 mrg * SUCH DAMAGE.
27 1.1 mrg */
28 1.1 mrg
29 1.1 mrg /*
30 1.1 mrg * WAV support for the audio tools; thanks go to the sox utility for
31 1.1 mrg * clearing up issues with WAV files.
32 1.1 mrg */
33 1.6 agc #include <sys/cdefs.h>
34 1.6 agc
35 1.6 agc #ifndef lint
36 1.9 mrg __RCSID("$NetBSD: wav.c,v 1.9 2009/06/18 02:37:27 mrg Exp $");
37 1.6 agc #endif
38 1.6 agc
39 1.1 mrg
40 1.1 mrg #include <sys/types.h>
41 1.1 mrg #include <sys/audioio.h>
42 1.1 mrg #include <sys/ioctl.h>
43 1.1 mrg #include <sys/time.h>
44 1.1 mrg
45 1.1 mrg #include <ctype.h>
46 1.1 mrg #include <err.h>
47 1.1 mrg #include <stdio.h>
48 1.1 mrg #include <stdlib.h>
49 1.1 mrg #include <string.h>
50 1.9 mrg #include <stdint.h>
51 1.1 mrg
52 1.1 mrg #include "libaudio.h"
53 1.1 mrg
54 1.1 mrg struct {
55 1.1 mrg int wenc;
56 1.2 mrg const char *wname;
57 1.1 mrg } wavencs[] = {
58 1.1 mrg { WAVE_FORMAT_UNKNOWN, "Microsoft Official Unknown" },
59 1.1 mrg { WAVE_FORMAT_PCM, "Microsoft PCM" },
60 1.1 mrg { WAVE_FORMAT_ADPCM, "Microsoft ADPCM" },
61 1.1 mrg { WAVE_FORMAT_ALAW, "Microsoft A-law" },
62 1.5 wiz { WAVE_FORMAT_MULAW, "Microsoft mu-law" },
63 1.1 mrg { WAVE_FORMAT_OKI_ADPCM,"OKI ADPCM" },
64 1.1 mrg { WAVE_FORMAT_DIGISTD, "Digistd format" },
65 1.1 mrg { WAVE_FORMAT_DIGIFIX, "Digifix format" },
66 1.1 mrg { -1, "?Unknown?" },
67 1.1 mrg };
68 1.1 mrg
69 1.2 mrg const char *
70 1.2 mrg wav_enc_from_val(int encoding)
71 1.1 mrg {
72 1.1 mrg int i;
73 1.1 mrg
74 1.1 mrg for (i = 0; wavencs[i].wenc != -1; i++)
75 1.1 mrg if (wavencs[i].wenc == encoding)
76 1.1 mrg break;
77 1.1 mrg return (wavencs[i].wname);
78 1.1 mrg }
79 1.1 mrg
80 1.9 mrg extern int verbose;
81 1.9 mrg
82 1.1 mrg /*
83 1.1 mrg * sample header is:
84 1.1 mrg *
85 1.1 mrg * RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
86 1.1 mrg *
87 1.1 mrg */
88 1.1 mrg /*
89 1.1 mrg * WAV format helpers
90 1.1 mrg */
91 1.1 mrg /*
92 1.1 mrg * find a .wav header, etc. returns header length on success
93 1.1 mrg */
94 1.1 mrg ssize_t
95 1.1 mrg audio_wav_parse_hdr(hdr, sz, enc, prec, sample, channels, datasize)
96 1.1 mrg void *hdr;
97 1.1 mrg size_t sz;
98 1.7 mrg u_int *enc;
99 1.7 mrg u_int *prec;
100 1.7 mrg u_int *sample;
101 1.7 mrg u_int *channels;
102 1.1 mrg size_t *datasize;
103 1.1 mrg {
104 1.1 mrg char *where = hdr, *owhere;
105 1.1 mrg wav_audioheaderpart part;
106 1.1 mrg wav_audioheaderfmt fmt;
107 1.9 mrg wav_audiohdrextensible ext;
108 1.1 mrg char *end = (((char *)hdr) + sz);
109 1.7 mrg u_int newenc, newprec;
110 1.9 mrg u_int16_t fmttag;
111 1.2 mrg static const char
112 1.2 mrg strfmt[4] = "fmt ",
113 1.2 mrg strRIFF[4] = "RIFF",
114 1.2 mrg strWAVE[4] = "WAVE",
115 1.2 mrg strdata[4] = "data";
116 1.2 mrg
117 1.1 mrg if (sz < 32)
118 1.1 mrg return (AUDIO_ENOENT);
119 1.1 mrg
120 1.2 mrg if (strncmp(where, strRIFF, sizeof strRIFF))
121 1.1 mrg return (AUDIO_ENOENT);
122 1.1 mrg where += 8;
123 1.2 mrg if (strncmp(where, strWAVE, sizeof strWAVE))
124 1.1 mrg return (AUDIO_ENOENT);
125 1.1 mrg where += 4;
126 1.1 mrg
127 1.1 mrg do {
128 1.1 mrg memcpy(&part, where, sizeof part);
129 1.1 mrg owhere = where;
130 1.1 mrg where += getle32(part.len) + 8;
131 1.2 mrg } while (where < end && strncmp(part.name, strfmt, sizeof strfmt));
132 1.1 mrg
133 1.1 mrg /* too short ? */
134 1.1 mrg if (where + sizeof fmt > end)
135 1.1 mrg return (AUDIO_ESHORTHDR);
136 1.1 mrg
137 1.1 mrg memcpy(&fmt, (owhere + 8), sizeof fmt);
138 1.1 mrg
139 1.9 mrg fmttag = getle16(fmt.tag);
140 1.9 mrg if (verbose)
141 1.9 mrg printf("WAVE format tag: %x\n", fmttag);
142 1.9 mrg
143 1.9 mrg if (fmttag == WAVE_FORMAT_EXTENSIBLE) {
144 1.9 mrg if ((uintptr_t)(where - owhere) < sizeof(fmt) + sizeof(ext))
145 1.9 mrg return (AUDIO_ESHORTHDR);
146 1.9 mrg memcpy(&ext, owhere + sizeof fmt, sizeof ext);
147 1.9 mrg if (getle16(ext.len) < sizeof(ext) - sizeof(ext.len))
148 1.9 mrg return (AUDIO_ESHORTHDR);
149 1.9 mrg fmttag = ext.sub_tag;
150 1.9 mrg if (verbose)
151 1.9 mrg printf("WAVE extensible sub tag: %x\n", fmttag);
152 1.9 mrg }
153 1.9 mrg
154 1.9 mrg switch (fmttag) {
155 1.1 mrg case WAVE_FORMAT_UNKNOWN:
156 1.1 mrg case IBM_FORMAT_MULAW:
157 1.1 mrg case IBM_FORMAT_ALAW:
158 1.1 mrg case IBM_FORMAT_ADPCM:
159 1.1 mrg default:
160 1.1 mrg return (AUDIO_EWAVUNSUPP);
161 1.1 mrg
162 1.1 mrg case WAVE_FORMAT_PCM:
163 1.9 mrg case WAVE_FORMAT_ADPCM:
164 1.9 mrg case WAVE_FORMAT_OKI_ADPCM:
165 1.9 mrg case WAVE_FORMAT_IMA_ADPCM:
166 1.9 mrg case WAVE_FORMAT_DIGIFIX:
167 1.9 mrg case WAVE_FORMAT_DIGISTD:
168 1.1 mrg switch (getle16(fmt.bits_per_sample)) {
169 1.1 mrg case 8:
170 1.1 mrg newprec = 8;
171 1.1 mrg break;
172 1.1 mrg case 16:
173 1.1 mrg newprec = 16;
174 1.1 mrg break;
175 1.1 mrg case 24:
176 1.1 mrg newprec = 24;
177 1.1 mrg break;
178 1.1 mrg case 32:
179 1.1 mrg newprec = 32;
180 1.1 mrg break;
181 1.1 mrg default:
182 1.1 mrg return (AUDIO_EWAVBADPCM);
183 1.1 mrg }
184 1.1 mrg if (newprec == 8)
185 1.1 mrg newenc = AUDIO_ENCODING_ULINEAR_LE;
186 1.1 mrg else
187 1.1 mrg newenc = AUDIO_ENCODING_SLINEAR_LE;
188 1.1 mrg break;
189 1.1 mrg case WAVE_FORMAT_ALAW:
190 1.1 mrg newenc = AUDIO_ENCODING_ALAW;
191 1.1 mrg newprec = 8;
192 1.1 mrg break;
193 1.1 mrg case WAVE_FORMAT_MULAW:
194 1.1 mrg newenc = AUDIO_ENCODING_ULAW;
195 1.1 mrg newprec = 8;
196 1.1 mrg break;
197 1.1 mrg }
198 1.1 mrg
199 1.1 mrg do {
200 1.1 mrg memcpy(&part, where, sizeof part);
201 1.1 mrg owhere = where;
202 1.1 mrg where += (getle32(part.len) + 8);
203 1.2 mrg } while (where < end && strncmp(part.name, strdata, sizeof strdata));
204 1.1 mrg
205 1.1 mrg if ((where - getle32(part.len)) <= end) {
206 1.1 mrg if (channels)
207 1.7 mrg *channels = (u_int)getle16(fmt.channels);
208 1.1 mrg if (sample)
209 1.1 mrg *sample = getle32(fmt.sample_rate);
210 1.1 mrg if (enc)
211 1.1 mrg *enc = newenc;
212 1.1 mrg if (prec)
213 1.1 mrg *prec = newprec;
214 1.1 mrg if (datasize)
215 1.1 mrg *datasize = (size_t)getle32(part.len);
216 1.1 mrg return (owhere - (char *)hdr + 8);
217 1.1 mrg }
218 1.1 mrg return (AUDIO_EWAVNODATA);
219 1.1 mrg }
220