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