1 /* $NetBSD: audio.c,v 1.25 2015/08/05 06:54:39 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.25 2015/08/05 06:54:39 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 /* 143 * decode a string into an encoding value. 144 */ 145 void 146 decode_encoding(const char *arg, int *encp) 147 { 148 size_t len; 149 int i; 150 151 len = strlen(arg); 152 for (i = 0; encs[i].ename; i++) 153 if (strncmp(encs[i].ename, arg, len) == 0) { 154 *encp = encs[i].eno; 155 return; 156 } 157 errx(1, "unknown encoding `%s'", arg); 158 } 159 160 static const char *const audio_errlist[] = { 161 "error zero", /* nothing? */ 162 "no audio entry", /* AUDIO_ENOENT */ 163 "short header", /* AUDIO_ESHORTHDR */ 164 "unsupported WAV format", /* AUDIO_EWAVUNSUPP */ 165 "bad (unsupported) WAV PCM format", /* AUDIO_EWAVBADPCM */ 166 "no WAV audio data", /* AUDIO_EWAVNODATA */ 167 "internal error", /* AUDIO_EINTERNAL */ 168 }; 169 170 const char * 171 audio_errstring(int errval) 172 { 173 174 errval = -errval; 175 if (errval < 1 || errval > AUDIO_MAXERRNO) 176 return "Invalid error"; 177 return audio_errlist[errval]; 178 } 179 180 void 181 write_header(struct track_info *ti) 182 { 183 struct iovec iv[3]; 184 int veclen, left, tlen; 185 void *hdr; 186 size_t hdrlen; 187 188 switch (ti->format) { 189 case AUDIO_FORMAT_DEFAULT: 190 case AUDIO_FORMAT_SUN: 191 if (sun_prepare_header(ti, &hdr, &hdrlen, &left) != 0) 192 return; 193 break; 194 case AUDIO_FORMAT_WAV: 195 if (wav_prepare_header(ti, &hdr, &hdrlen, &left) != 0) 196 return; 197 break; 198 case AUDIO_FORMAT_NONE: 199 return; 200 default: 201 errx(1, "unknown audio format"); 202 } 203 204 veclen = 0; 205 tlen = 0; 206 207 if (hdrlen != 0) { 208 iv[veclen].iov_base = hdr; 209 iv[veclen].iov_len = hdrlen; 210 tlen += iv[veclen++].iov_len; 211 } 212 if (ti->header_info) { 213 iv[veclen].iov_base = ti->header_info; 214 iv[veclen].iov_len = (int)strlen(ti->header_info) + 1; 215 tlen += iv[veclen++].iov_len; 216 } 217 if (left) { 218 iv[veclen].iov_base = audio_default_info; 219 iv[veclen].iov_len = left; 220 tlen += iv[veclen++].iov_len; 221 } 222 223 if (tlen == 0) 224 return; 225 226 if (writev(ti->outfd, iv, veclen) != tlen) 227 err(1, "could not write audio header"); 228 } 229 230 write_conv_func 231 write_get_conv_func(struct track_info *ti) 232 { 233 234 switch (ti->format) { 235 case AUDIO_FORMAT_DEFAULT: 236 case AUDIO_FORMAT_SUN: 237 return sun_write_get_conv_func(ti); 238 case AUDIO_FORMAT_WAV: 239 return wav_write_get_conv_func(ti); 240 case AUDIO_FORMAT_NONE: 241 return NULL; 242 default: 243 errx(1, "unknown audio format"); 244 } 245 } 246