audio.c revision 1.10 1 /* $NetBSD: audio.c,v 1.10 2000/12/13 08:19:54 mrg 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_sun_to_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 int
152 audio_encoding_to_sun(encoding, precision, sunep)
153 int encoding;
154 int precision;
155 int *sunep;
156 {
157 int i;
158
159 for (i = 0; file2sw_encodings[i].file_encoding != -1; i++)
160 if (file2sw_encodings[i].encoding == encoding &&
161 file2sw_encodings[i].precision == precision) {
162 *sunep = file2sw_encodings[i].file_encoding;
163 return (0);
164 }
165 return (1);
166 }
167
168 /*
169 * sample header is:
170 *
171 * RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
172 *
173 */
174 /*
175 * WAV format helpers
176 */
177 /*
178 * find a .wav header, etc. returns header length on success
179 */
180 size_t
181 audio_parse_wav_hdr(hdr, sz, enc, prec, sample, channels)
182 void *hdr;
183 size_t sz;
184 int *enc;
185 int *prec;
186 int *sample;
187 int *channels;
188 {
189 char *where = hdr;
190 wav_audioheaderpart *part;
191 wav_audioheaderfmt *fmt;
192 char *end = (((char *)hdr) + sz);
193 int newenc, newprec;
194
195 if (sz < 32)
196 return (AUDIO_ENOENT);
197
198 if (strncmp(where, "RIFF", 4))
199 return (AUDIO_ENOENT);
200 where += 8;
201 if (strncmp(where, "WAVE", 4))
202 return (AUDIO_ENOENT);
203 where += 4;
204
205 do {
206 part = (wav_audioheaderpart *)where;
207 where += getle32(part->len) + 8;
208 } while (where < end && strncmp(part->name, "fmt ", 4));
209
210 /* too short ? */
211 if (where + 16 > end)
212 return (AUDIO_ESHORTHDR);
213
214 fmt = (wav_audioheaderfmt *)(part + 1);
215
216 #if 0
217 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));
218 #endif
219
220 switch (getle16(fmt->tag)) {
221 case WAVE_FORMAT_UNKNOWN:
222 case WAVE_FORMAT_ADPCM:
223 case WAVE_FORMAT_OKI_ADPCM:
224 case WAVE_FORMAT_DIGISTD:
225 case WAVE_FORMAT_DIGIFIX:
226 case IBM_FORMAT_MULAW:
227 case IBM_FORMAT_ALAW:
228 case IBM_FORMAT_ADPCM:
229 default:
230 return (AUDIO_EWAVUNSUPP);
231
232 case WAVE_FORMAT_PCM:
233 switch (getle16(fmt->bits_per_sample)) {
234 case 8:
235 newprec = 8;
236 break;
237 case 16:
238 newprec = 16;
239 break;
240 case 24:
241 newprec = 24;
242 break;
243 case 32:
244 newprec = 32;
245 break;
246 default:
247 return (AUDIO_EWAVBADPCM);
248 }
249 if (newprec == 8)
250 newenc = AUDIO_ENCODING_ULINEAR_LE;
251 else
252 newenc = AUDIO_ENCODING_SLINEAR_LE;
253 break;
254 case WAVE_FORMAT_ALAW:
255 newenc = AUDIO_ENCODING_ALAW;
256 newprec = 8;
257 break;
258 case WAVE_FORMAT_MULAW:
259 newenc = AUDIO_ENCODING_ULAW;
260 newprec = 8;
261 break;
262 }
263
264 do {
265 part = (wav_audioheaderpart *)where;
266 #if 0
267 printf("part `%c%c%c%c' len = %d\n", part->name[0], part->name[1], part->name[2], part->name[3], getle32(part->len));
268 #endif
269 where += (getle32(part->len) + 8);
270 } while ((char *)where < end && strncmp(part->name, "data", 4));
271
272 if ((where - getle32(part->len)) <= end) {
273 *channels = getle16(fmt->channels);
274 *sample = getle32(fmt->sample_rate);
275 *enc = newenc;
276 *prec = newprec;
277 part++;
278 return ((char *)part - (char *)hdr);
279 }
280 return (AUDIO_EWAVNODATA);
281 }
282
283 /*
284 * these belong elsewhere??
285 */
286 void
287 decode_int(arg, intp)
288 const char *arg;
289 int *intp;
290 {
291 char *ep;
292 int ret;
293
294 ret = strtoul(arg, &ep, 0);
295
296 if (ep[0] == '\0') {
297 *intp = ret;
298 return;
299 }
300 errx(1, "argument `%s' not a valid integer", arg);
301 }
302
303 void
304 decode_time(arg, tvp)
305 const char *arg;
306 struct timeval *tvp;
307 {
308 char *s, *colon, *dot;
309 char *copy = strdup(arg);
310 int first;
311
312 if (copy == NULL)
313 err(1, "could not allocate a copy of %s", arg);
314
315 tvp->tv_sec = tvp->tv_usec = 0;
316 s = copy;
317
318 /* handle [hh:]mm:ss.dd */
319 if ((colon = strchr(s, ':'))) {
320 *colon++ = '\0';
321 decode_int(s, &first);
322 tvp->tv_sec = first * 60; /* minutes */
323 s = colon;
324
325 if ((colon = strchr(s, ':'))) {
326 *colon++ = '\0';
327 decode_int(s, &first);
328 tvp->tv_sec += first;
329 tvp->tv_sec *= 60; /* minutes and hours */
330 s = colon;
331 }
332 }
333 if ((dot = strchr(s, '.'))) {
334 int i, base = 100000;
335
336 *dot++ = '\0';
337
338 for (i = 0; i < 6; i++, base /= 10) {
339 if (!dot[i])
340 break;
341 if (!isdigit(dot[i]))
342 errx(1, "argument `%s' is not a value time specification", arg);
343 tvp->tv_usec += base * (dot[i] - '0');
344 }
345 }
346 decode_int(s, &first);
347 tvp->tv_sec += first;
348 #if 0
349 printf("tvp->tv_sec = %ld, tvp->tv_usec = %ld\n", tvp->tv_sec, tvp->tv_usec);
350 #endif
351
352 free(copy);
353 }
354
355 /*
356 * decode a string into an encoding value.
357 */
358 void
359 decode_encoding(arg, encp)
360 const char *arg;
361 int *encp;
362 {
363 size_t len;
364 int i;
365
366 len = strlen(arg);
367 for (i = 0; encs[i].ename; i++)
368 if (strncmp(encs[i].ename, arg, len) == 0) {
369 *encp = encs[i].eno;
370 return;
371 }
372 errx(1, "unknown encoding `%s'", arg);
373 }
374
375 const char *const audio_errlist[] = {
376 "error zero", /* nothing? */
377 "no audio entry", /* AUDIO_ENOENT */
378 "short header", /* AUDIO_ESHORTHDR */
379 "unsupported WAV format", /* AUDIO_EWAVUNSUPP */
380 "bad (unsupported) WAV PCM format", /* AUDIO_EWAVBADPCM */
381 "no WAV audio data", /* AUDIO_EWAVNODATA */
382 };
383
384 const char *
385 audio_errstring(errval)
386 int errval;
387 {
388
389 errval = -errval;
390 if (errval < 1 || errval > AUDIO_MAXERRNO)
391 return "Invalid error";
392 return audio_errlist[errval];
393 }
394