audio.c revision 1.18 1 /* $NetBSD: audio.c,v 1.18 2004/10/30 16:57:27 dsl 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 #include <sys/cdefs.h>
35
36 #ifndef lint
37 __RCSID("$NetBSD: audio.c,v 1.18 2004/10/30 16:57:27 dsl Exp $");
38 #endif
39
40
41 #include <sys/types.h>
42 #include <sys/audioio.h>
43 #include <sys/ioctl.h>
44 #include <sys/time.h>
45
46 #include <ctype.h>
47 #include <err.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include "libaudio.h"
53
54 /* what format am i? */
55
56 struct {
57 const char *fname;
58 int fno;
59 } formats[] = {
60 { "sunau", AUDIO_FORMAT_SUN },
61 { "au", AUDIO_FORMAT_SUN },
62 { "sun", AUDIO_FORMAT_SUN },
63 { "wav", AUDIO_FORMAT_WAV },
64 { "wave", AUDIO_FORMAT_WAV },
65 { "riff", AUDIO_FORMAT_WAV },
66 { "no", AUDIO_FORMAT_NONE },
67 { "none", AUDIO_FORMAT_NONE },
68 { NULL, -1 }
69 };
70
71 int
72 audio_format_from_str(str)
73 char *str;
74 {
75 int i;
76
77 for (i = 0; formats[i].fname; i++)
78 if (strcasecmp(formats[i].fname, str) == 0)
79 break;
80 return (formats[i].fno);
81 }
82
83
84
85 /* back and forth between encodings */
86 struct {
87 const char *ename;
88 int eno;
89 } encs[] = {
90 { AudioEmulaw, AUDIO_ENCODING_ULAW },
91 { "ulaw", AUDIO_ENCODING_ULAW },
92 { AudioEalaw, AUDIO_ENCODING_ALAW },
93 { AudioEslinear, AUDIO_ENCODING_SLINEAR },
94 { "linear", AUDIO_ENCODING_SLINEAR },
95 { AudioEulinear, AUDIO_ENCODING_ULINEAR },
96 { AudioEadpcm, AUDIO_ENCODING_ADPCM },
97 { "ADPCM", AUDIO_ENCODING_ADPCM },
98 { AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE },
99 { "linear_le", AUDIO_ENCODING_SLINEAR_LE },
100 { AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE },
101 { AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE },
102 { "linear_be", AUDIO_ENCODING_SLINEAR_BE },
103 { AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE },
104 { AudioEmpeg_l1_stream, AUDIO_ENCODING_MPEG_L1_STREAM },
105 { AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
106 { AudioEmpeg_l1_system, AUDIO_ENCODING_MPEG_L1_SYSTEM },
107 { AudioEmpeg_l2_stream, AUDIO_ENCODING_MPEG_L2_STREAM },
108 { AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
109 { AudioEmpeg_l2_system, AUDIO_ENCODING_MPEG_L2_SYSTEM },
110 { NULL, -1 }
111 };
112
113
114 const char *
115 audio_enc_from_val(val)
116 int val;
117 {
118 int i;
119
120 for (i = 0; encs[i].ename; i++)
121 if (encs[i].eno == val)
122 break;
123 return (encs[i].ename);
124 }
125
126 int
127 audio_enc_to_val(enc)
128 const char *enc;
129 {
130 int i;
131
132 for (i = 0; encs[i].ename; i++)
133 if (strcmp(encs[i].ename, enc) == 0)
134 break;
135 if (encs[i].ename)
136 return (encs[i].eno);
137 else
138 return (AUDIO_ENOENT);
139 }
140
141 void
142 decode_int(arg, intp)
143 const char *arg;
144 int *intp;
145 {
146 char *ep;
147 int ret;
148
149 ret = (int)strtoul(arg, &ep, 10);
150
151 if (ep[0] == '\0') {
152 *intp = ret;
153 return;
154 }
155 errx(1, "argument `%s' not a valid integer", arg);
156 }
157
158 void
159 decode_time(arg, tvp)
160 const char *arg;
161 struct timeval *tvp;
162 {
163 char *s, *colon, *dot;
164 char *copy = strdup(arg);
165 int first;
166
167 if (copy == NULL)
168 err(1, "could not allocate a copy of %s", arg);
169
170 tvp->tv_sec = tvp->tv_usec = 0;
171 s = copy;
172
173 /* handle [hh:]mm:ss.dd */
174 if ((colon = strchr(s, ':')) != NULL) {
175 *colon++ = '\0';
176 decode_int(s, &first);
177 tvp->tv_sec = first * 60; /* minutes */
178 s = colon;
179
180 if ((colon = strchr(s, ':')) != NULL) {
181 *colon++ = '\0';
182 decode_int(s, &first);
183 tvp->tv_sec += first; /* minutes and hours */
184 tvp->tv_sec *= 60;
185 s = colon;
186 }
187 }
188 if ((dot = strchr(s, '.')) != NULL) {
189 int i, base = 100000;
190
191 *dot++ = '\0';
192
193 for (i = 0; i < 6; i++, base /= 10) {
194 if (!dot[i])
195 break;
196 if (!isdigit((unsigned char)dot[i]))
197 errx(1, "argument `%s' is not a value time specification", arg);
198 tvp->tv_usec += base * (dot[i] - '0');
199 }
200 }
201 decode_int(s, &first);
202 tvp->tv_sec += first;
203
204 free(copy);
205 }
206
207 /*
208 * decode a string into an encoding value.
209 */
210 void
211 decode_encoding(arg, encp)
212 const char *arg;
213 int *encp;
214 {
215 size_t len;
216 int i;
217
218 len = strlen(arg);
219 for (i = 0; encs[i].ename; i++)
220 if (strncmp(encs[i].ename, arg, len) == 0) {
221 *encp = encs[i].eno;
222 return;
223 }
224 errx(1, "unknown encoding `%s'", arg);
225 }
226
227 const char *const audio_errlist[] = {
228 "error zero", /* nothing? */
229 "no audio entry", /* AUDIO_ENOENT */
230 "short header", /* AUDIO_ESHORTHDR */
231 "unsupported WAV format", /* AUDIO_EWAVUNSUPP */
232 "bad (unsupported) WAV PCM format", /* AUDIO_EWAVBADPCM */
233 "no WAV audio data", /* AUDIO_EWAVNODATA */
234 "internal error", /* AUDIO_EINTERNAL */
235 };
236
237 const char *
238 audio_errstring(errval)
239 int errval;
240 {
241
242 errval = -errval;
243 if (errval < 1 || errval > AUDIO_MAXERRNO)
244 return "Invalid error";
245 return audio_errlist[errval];
246 }
247