audio.c revision 1.9.6.1 1 /* $NetBSD: audio.c,v 1.9.6.1 2001/03/11 21:29:46 he Exp $ */
2
3 /*
4 * Copyright (c) 1999 Matthew R. Green
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * XXX this is slightly icky in places...
33 */
34
35 #include <sys/types.h>
36 #include <sys/audioio.h>
37 #include <sys/ioctl.h>
38 #include <sys/time.h>
39
40 #include <ctype.h>
41 #include <err.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "libaudio.h"
47
48 /* back and forth between encodings */
49 struct {
50 char *ename;
51 int eno;
52 } encs[] = {
53 { AudioEmulaw, AUDIO_ENCODING_ULAW },
54 { "ulaw", AUDIO_ENCODING_ULAW },
55 { AudioEalaw, AUDIO_ENCODING_ALAW },
56 { AudioEslinear, AUDIO_ENCODING_SLINEAR },
57 { "linear", AUDIO_ENCODING_SLINEAR },
58 { AudioEulinear, AUDIO_ENCODING_ULINEAR },
59 { AudioEadpcm, AUDIO_ENCODING_ADPCM },
60 { "ADPCM", AUDIO_ENCODING_ADPCM },
61 { AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE },
62 { "linear_le", AUDIO_ENCODING_SLINEAR_LE },
63 { AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE },
64 { AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE },
65 { "linear_be", AUDIO_ENCODING_SLINEAR_BE },
66 { AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE },
67 { AudioEmpeg_l1_stream, AUDIO_ENCODING_MPEG_L1_STREAM },
68 { AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
69 { AudioEmpeg_l1_system, AUDIO_ENCODING_MPEG_L1_SYSTEM },
70 { AudioEmpeg_l2_stream, AUDIO_ENCODING_MPEG_L2_STREAM },
71 { AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
72 { AudioEmpeg_l2_system, AUDIO_ENCODING_MPEG_L2_SYSTEM },
73 { NULL, -1 }
74 };
75
76
77 char *
78 audio_enc_from_val(val)
79 int val;
80 {
81 int i;
82
83 for (i = 0; encs[i].ename; i++)
84 if (encs[i].eno == val)
85 break;
86 return (encs[i].ename);
87 }
88
89 int
90 audio_enc_to_val(enc)
91 const char *enc;
92 {
93 int i;
94
95 for (i = 0; encs[i].ename; i++)
96 if (strcmp(encs[i].ename, enc) == 0)
97 break;
98 if (encs[i].ename)
99 return (encs[i].eno);
100 else
101 return (-1);
102 }
103
104 /*
105 * SunOS/NeXT .au format helpers
106 */
107 struct {
108 int file_encoding;
109 int encoding;
110 int precision;
111 } file2sw_encodings[] = {
112 { AUDIO_FILE_ENCODING_MULAW_8, AUDIO_ENCODING_ULAW, 8 },
113 { AUDIO_FILE_ENCODING_LINEAR_8, AUDIO_ENCODING_ULINEAR_BE, 8 },
114 { AUDIO_FILE_ENCODING_LINEAR_16, AUDIO_ENCODING_ULINEAR_BE, 16 },
115 { AUDIO_FILE_ENCODING_LINEAR_24, AUDIO_ENCODING_ULINEAR_BE, 24 },
116 { AUDIO_FILE_ENCODING_LINEAR_32, AUDIO_ENCODING_ULINEAR_BE, 32 },
117 #if 0
118 /*
119 * we should make some of these available. the, eg ultrasparc, port
120 * can use the VIS instructions (if available) do do some of these
121 * mpeg ones.
122 */
123 { AUDIO_FILE_ENCODING_FLOAT, AUDIO_ENCODING_ULAW, 32 },
124 { AUDIO_FILE_ENCODING_DOUBLE, AUDIO_ENCODING_ULAW, 64 },
125 { AUDIO_FILE_ENCODING_ADPCM_G721, AUDIO_ENCODING_ULAW, 4 },
126 { AUDIO_FILE_ENCODING_ADPCM_G722, AUDIO_ENCODING_ULAW, 0 },
127 { AUDIO_FILE_ENCODING_ADPCM_G723_3, AUDIO_ENCODING_ULAW, 3 },
128 { AUDIO_FILE_ENCODING_ADPCM_G723_5, AUDIO_ENCODING_ULAW, 5 },
129 #endif
130 { AUDIO_FILE_ENCODING_ALAW_8, AUDIO_ENCODING_ALAW, 8 },
131 { -1, -1 }
132 };
133
134 int
135 audio_get_sun_encoding(sun_encoding, encp, precp)
136 int sun_encoding;
137 int *encp;
138 int *precp;
139 {
140 int i;
141
142 for (i = 0; file2sw_encodings[i].file_encoding != -1; i++)
143 if (file2sw_encodings[i].file_encoding == sun_encoding) {
144 *precp = file2sw_encodings[i].precision;
145 *encp = file2sw_encodings[i].encoding;
146 return (0);
147 }
148 return (1);
149 }
150
151 /*
152 * sample header is:
153 *
154 * RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
155 *
156 */
157 /*
158 * WAV format helpers
159 */
160 /*
161 * find a .wav header, etc. returns header length on success
162 */
163 size_t
164 audio_parse_wav_hdr(hdr, sz, enc, prec, sample, channels, datasize)
165 void *hdr;
166 size_t sz;
167 int *enc;
168 int *prec;
169 int *sample;
170 int *channels;
171 size_t *datasize;
172 {
173 char *where = hdr;
174 wav_audioheaderpart *part;
175 wav_audioheaderfmt *fmt;
176 char *end = (((char *)hdr) + sz);
177 int newenc, newprec;
178
179 if (sz < 32)
180 return (AUDIO_ENOENT);
181
182 if (strncmp(where, "RIFF", 4))
183 return (AUDIO_ENOENT);
184 where += 8;
185 if (strncmp(where, "WAVE", 4))
186 return (AUDIO_ENOENT);
187 where += 4;
188
189 do {
190 part = (wav_audioheaderpart *)where;
191 where += getle32(part->len) + 8;
192 } while (where < end && strncmp(part->name, "fmt ", 4));
193
194 /* too short ? */
195 if (where + 16 > end)
196 return (AUDIO_ESHORTHDR);
197
198 fmt = (wav_audioheaderfmt *)(part + 1);
199
200 #if 0
201 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));
202 #endif
203
204 switch (getle16(fmt->tag)) {
205 case WAVE_FORMAT_UNKNOWN:
206 case WAVE_FORMAT_ADPCM:
207 case WAVE_FORMAT_OKI_ADPCM:
208 case WAVE_FORMAT_DIGISTD:
209 case WAVE_FORMAT_DIGIFIX:
210 case IBM_FORMAT_MULAW:
211 case IBM_FORMAT_ALAW:
212 case IBM_FORMAT_ADPCM:
213 default:
214 return (AUDIO_EWAVUNSUPP);
215
216 case WAVE_FORMAT_PCM:
217 switch (getle16(fmt->bits_per_sample)) {
218 case 8:
219 newprec = 8;
220 break;
221 case 16:
222 newprec = 16;
223 break;
224 case 24:
225 newprec = 24;
226 break;
227 case 32:
228 newprec = 32;
229 break;
230 default:
231 return (AUDIO_EWAVBADPCM);
232 }
233 if (newprec == 8)
234 newenc = AUDIO_ENCODING_ULINEAR_LE;
235 else
236 newenc = AUDIO_ENCODING_SLINEAR_LE;
237 break;
238 case WAVE_FORMAT_ALAW:
239 newenc = AUDIO_ENCODING_ALAW;
240 newprec = 8;
241 break;
242 case WAVE_FORMAT_MULAW:
243 newenc = AUDIO_ENCODING_ULAW;
244 newprec = 8;
245 break;
246 }
247
248 do {
249 part = (wav_audioheaderpart *)where;
250 #if 0
251 printf("part `%c%c%c%c' len = %d\n", part->name[0], part->name[1], part->name[2], part->name[3], getle32(part->len));
252 #endif
253 where += (getle32(part->len) + 8);
254 } while ((char *)where < end && strncmp(part->name, "data", 4));
255
256 if ((where - getle32(part->len)) <= end) {
257 *channels = getle16(fmt->channels);
258 *sample = getle32(fmt->sample_rate);
259 *enc = newenc;
260 *prec = newprec;
261 if (datasize)
262 *datasize = (size_t)getle32(part->len);
263 part++;
264 return ((char *)part - (char *)hdr);
265 }
266 return (AUDIO_EWAVNODATA);
267 }
268
269 /*
270 * these belong elsewhere??
271 */
272 void
273 decode_int(arg, intp)
274 const char *arg;
275 int *intp;
276 {
277 char *ep;
278 int ret;
279
280 ret = strtoul(arg, &ep, 0);
281
282 if (ep[0] == '\0') {
283 *intp = ret;
284 return;
285 }
286 errx(1, "argument `%s' not a valid integer", arg);
287 }
288
289 void
290 decode_time(arg, tvp)
291 const char *arg;
292 struct timeval *tvp;
293 {
294 char *s, *colon, *dot;
295 char *copy = strdup(arg);
296 int first;
297
298 if (copy == NULL)
299 err(1, "could not allocate a copy of %s", arg);
300
301 tvp->tv_sec = tvp->tv_usec = 0;
302 s = copy;
303
304 /* handle [hh:]mm:ss.dd */
305 if ((colon = strchr(s, ':'))) {
306 *colon++ = '\0';
307 decode_int(s, &first);
308 tvp->tv_sec = first * 60; /* minutes */
309 s = colon;
310
311 if ((colon = strchr(s, ':'))) {
312 *colon++ = '\0';
313 decode_int(s, &first);
314 tvp->tv_sec += first;
315 tvp->tv_sec *= 60; /* minutes and hours */
316 s = colon;
317 }
318 }
319 if ((dot = strchr(s, '.'))) {
320 int i, base = 100000;
321
322 *dot++ = '\0';
323
324 for (i = 0; i < 6; i++, base /= 10) {
325 if (!dot[i])
326 break;
327 if (!isdigit(dot[i]))
328 errx(1, "argument `%s' is not a value time specification", arg);
329 tvp->tv_usec += base * (dot[i] - '0');
330 }
331 }
332 decode_int(s, &first);
333 tvp->tv_sec += first;
334 #if 0
335 printf("tvp->tv_sec = %ld, tvp->tv_usec = %ld\n", tvp->tv_sec, tvp->tv_usec);
336 #endif
337
338 free(copy);
339 }
340
341 /*
342 * decode a string into an encoding value.
343 */
344 void
345 decode_encoding(arg, encp)
346 const char *arg;
347 int *encp;
348 {
349 size_t len;
350 int i;
351
352 len = strlen(arg);
353 for (i = 0; encs[i].ename; i++)
354 if (strncmp(encs[i].ename, arg, len) == 0) {
355 *encp = encs[i].eno;
356 return;
357 }
358 errx(1, "unknown encoding `%s'", arg);
359 }
360
361 const char *const audio_errlist[] = {
362 "error zero", /* nothing? */
363 "no audio entry", /* AUDIO_ENOENT */
364 "short header", /* AUDIO_ESHORTHDR */
365 "unsupported WAV format", /* AUDIO_EWAVUNSUPP */
366 "bad (unsupported) WAV PCM format", /* AUDIO_EWAVBADPCM */
367 "no WAV audio data", /* AUDIO_EWAVNODATA */
368 };
369
370 const char *
371 audio_errstring(errval)
372 int errval;
373 {
374
375 errval = -errval;
376 if (errval < 1 || errval > AUDIO_MAXERRNO)
377 return "Invalid error";
378 return audio_errlist[errval];
379 }
380