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