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