1 /* $NetBSD: audio.c,v 1.23 2014/12/30 01:22:09 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.23 2014/12/30 01:22:09 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 #include <sys/uio.h> 44 45 #include <unistd.h> 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 #include "auconv.h" 54 55 /* what format am i? */ 56 57 static const struct { 58 const char *fname; 59 int fno; 60 } formats[] = { 61 { "sunau", AUDIO_FORMAT_SUN }, 62 { "au", AUDIO_FORMAT_SUN }, 63 { "sun", AUDIO_FORMAT_SUN }, 64 { "wav", AUDIO_FORMAT_WAV }, 65 { "wave", AUDIO_FORMAT_WAV }, 66 { "riff", AUDIO_FORMAT_WAV }, 67 { "no", AUDIO_FORMAT_NONE }, 68 { "none", AUDIO_FORMAT_NONE }, 69 { NULL, -1 } 70 }; 71 72 char audio_default_info[8] = { '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' }; 73 74 int 75 audio_format_from_str(char *str) 76 { 77 int i; 78 79 for (i = 0; formats[i].fname; i++) 80 if (strcasecmp(formats[i].fname, str) == 0) 81 break; 82 return (formats[i].fno); 83 } 84 85 86 87 /* back and forth between encodings */ 88 static const struct { 89 const char *ename; 90 int eno; 91 } encs[] = { 92 { AudioEmulaw, AUDIO_ENCODING_ULAW }, 93 { "ulaw", AUDIO_ENCODING_ULAW }, 94 { AudioEalaw, AUDIO_ENCODING_ALAW }, 95 { AudioEslinear, AUDIO_ENCODING_SLINEAR }, 96 { "linear", AUDIO_ENCODING_SLINEAR }, 97 { AudioEulinear, AUDIO_ENCODING_ULINEAR }, 98 { AudioEadpcm, AUDIO_ENCODING_ADPCM }, 99 { "ADPCM", AUDIO_ENCODING_ADPCM }, 100 { AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE }, 101 { "linear_le", AUDIO_ENCODING_SLINEAR_LE }, 102 { AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE }, 103 { AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE }, 104 { "linear_be", AUDIO_ENCODING_SLINEAR_BE }, 105 { AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE }, 106 { AudioEmpeg_l1_stream, AUDIO_ENCODING_MPEG_L1_STREAM }, 107 { AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS }, 108 { AudioEmpeg_l1_system, AUDIO_ENCODING_MPEG_L1_SYSTEM }, 109 { AudioEmpeg_l2_stream, AUDIO_ENCODING_MPEG_L2_STREAM }, 110 { AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS }, 111 { AudioEmpeg_l2_system, AUDIO_ENCODING_MPEG_L2_SYSTEM }, 112 { AudioEac3, AUDIO_ENCODING_AC3 }, 113 { NULL, -1 } 114 }; 115 116 117 const char * 118 audio_enc_from_val(int val) 119 { 120 int i; 121 122 for (i = 0; encs[i].ename; i++) 123 if (encs[i].eno == val) 124 break; 125 return (encs[i].ename); 126 } 127 128 int 129 audio_enc_to_val(const char *enc) 130 { 131 int i; 132 133 for (i = 0; encs[i].ename; i++) 134 if (strcmp(encs[i].ename, enc) == 0) 135 break; 136 if (encs[i].ename) 137 return (encs[i].eno); 138 else 139 return (AUDIO_ENOENT); 140 } 141 142 void 143 decode_int(const char *arg, int *intp) 144 { 145 char *ep; 146 int ret; 147 148 ret = (int)strtoul(arg, &ep, 10); 149 150 if (ep[0] == '\0') { 151 *intp = ret; 152 return; 153 } 154 errx(1, "argument `%s' not a valid integer", arg); 155 } 156 157 void 158 decode_uint(const char *arg, unsigned *intp) 159 { 160 char *ep; 161 unsigned ret; 162 163 ret = (unsigned)strtoul(arg, &ep, 10); 164 165 if (ep[0] == '\0') { 166 *intp = ret; 167 return; 168 } 169 errx(1, "argument `%s' not a valid integer", arg); 170 } 171 172 void 173 decode_time(const char *arg, struct timeval *tvp) 174 { 175 char *s, *colon, *dot; 176 char *copy = strdup(arg); 177 int first; 178 179 if (copy == NULL) 180 err(1, "could not allocate a copy of %s", arg); 181 182 tvp->tv_sec = tvp->tv_usec = 0; 183 s = copy; 184 185 /* handle [hh:]mm:ss.dd */ 186 if ((colon = strchr(s, ':')) != NULL) { 187 *colon++ = '\0'; 188 decode_int(s, &first); 189 tvp->tv_sec = first * 60; /* minutes */ 190 s = colon; 191 192 if ((colon = strchr(s, ':')) != NULL) { 193 *colon++ = '\0'; 194 decode_int(s, &first); 195 tvp->tv_sec += first; /* minutes and hours */ 196 tvp->tv_sec *= 60; 197 s = colon; 198 } 199 } 200 if ((dot = strchr(s, '.')) != NULL) { 201 int i, base = 100000; 202 203 *dot++ = '\0'; 204 205 for (i = 0; i < 6; i++, base /= 10) { 206 if (!dot[i]) 207 break; 208 if (!isdigit((unsigned char)dot[i])) 209 errx(1, "argument `%s' is not a value time specification", arg); 210 tvp->tv_usec += base * (dot[i] - '0'); 211 } 212 } 213 decode_int(s, &first); 214 tvp->tv_sec += first; 215 216 free(copy); 217 } 218 219 /* 220 * decode a string into an encoding value. 221 */ 222 void 223 decode_encoding(const char *arg, int *encp) 224 { 225 size_t len; 226 int i; 227 228 len = strlen(arg); 229 for (i = 0; encs[i].ename; i++) 230 if (strncmp(encs[i].ename, arg, len) == 0) { 231 *encp = encs[i].eno; 232 return; 233 } 234 errx(1, "unknown encoding `%s'", arg); 235 } 236 237 static const char *const audio_errlist[] = { 238 "error zero", /* nothing? */ 239 "no audio entry", /* AUDIO_ENOENT */ 240 "short header", /* AUDIO_ESHORTHDR */ 241 "unsupported WAV format", /* AUDIO_EWAVUNSUPP */ 242 "bad (unsupported) WAV PCM format", /* AUDIO_EWAVBADPCM */ 243 "no WAV audio data", /* AUDIO_EWAVNODATA */ 244 "internal error", /* AUDIO_EINTERNAL */ 245 }; 246 247 const char * 248 audio_errstring(int errval) 249 { 250 251 errval = -errval; 252 if (errval < 1 || errval > AUDIO_MAXERRNO) 253 return "Invalid error"; 254 return audio_errlist[errval]; 255 } 256 257 void 258 write_header(struct write_info *wi) 259 { 260 struct iovec iv[3]; 261 int veclen, left, tlen; 262 void *hdr; 263 size_t hdrlen; 264 265 switch (wi->format) { 266 case AUDIO_FORMAT_DEFAULT: 267 case AUDIO_FORMAT_SUN: 268 if (sun_prepare_header(wi, &hdr, &hdrlen, &left) != 0) 269 return; 270 break; 271 case AUDIO_FORMAT_WAV: 272 if (wav_prepare_header(wi, &hdr, &hdrlen, &left) != 0) 273 return; 274 break; 275 case AUDIO_FORMAT_NONE: 276 return; 277 default: 278 errx(1, "unknown audio format"); 279 } 280 281 veclen = 0; 282 tlen = 0; 283 284 if (hdrlen != 0) { 285 iv[veclen].iov_base = hdr; 286 iv[veclen].iov_len = hdrlen; 287 tlen += iv[veclen++].iov_len; 288 } 289 if (wi->header_info) { 290 iv[veclen].iov_base = wi->header_info; 291 iv[veclen].iov_len = (int)strlen(wi->header_info) + 1; 292 tlen += iv[veclen++].iov_len; 293 } 294 if (left) { 295 iv[veclen].iov_base = audio_default_info; 296 iv[veclen].iov_len = left; 297 tlen += iv[veclen++].iov_len; 298 } 299 300 if (tlen == 0) 301 return; 302 303 if (writev(wi->outfd, iv, veclen) != tlen) 304 err(1, "could not write audio header"); 305 } 306 307 write_conv_func 308 write_get_conv_func(struct write_info *wi) 309 { 310 311 switch (wi->format) { 312 case AUDIO_FORMAT_DEFAULT: 313 case AUDIO_FORMAT_SUN: 314 return sun_write_get_conv_func(wi); 315 case AUDIO_FORMAT_WAV: 316 return wav_write_get_conv_func(wi); 317 case AUDIO_FORMAT_NONE: 318 return NULL; 319 default: 320 errx(1, "unknown audio format"); 321 } 322 } 323