1 /* $NetBSD: audio.c,v 1.27 2024/02/27 21:05:34 gson Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2013, 2015, 2019 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.27 2024/02/27 21:05:34 gson 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
audio_format_from_str(char * str)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 { "ieee_float32", AUDIO_ENCODING_LIBAUDIO_FLOAT32 },
114 { "ieee_float64", AUDIO_ENCODING_LIBAUDIO_FLOAT64 },
115 { NULL, -1 }
116 };
117
118
119 const char *
audio_enc_from_val(int val)120 audio_enc_from_val(int val)
121 {
122 int i;
123
124 for (i = 0; encs[i].ename; i++)
125 if (encs[i].eno == val)
126 break;
127 return (encs[i].ename);
128 }
129
130 int
audio_enc_to_val(const char * enc)131 audio_enc_to_val(const char *enc)
132 {
133 int i;
134
135 for (i = 0; encs[i].ename; i++)
136 if (strcmp(encs[i].ename, enc) == 0)
137 break;
138 if (encs[i].ename)
139 return (encs[i].eno);
140 else
141 return (AUDIO_ENOENT);
142 }
143
144 /*
145 * decode a string into an encoding value.
146 */
147 void
decode_encoding(const char * arg,int * encp)148 decode_encoding(const char *arg, int *encp)
149 {
150 size_t len;
151 int i;
152
153 len = strlen(arg);
154 for (i = 0; encs[i].ename; i++)
155 if (strncmp(encs[i].ename, arg, len) == 0) {
156 *encp = encs[i].eno;
157 return;
158 }
159 errx(1, "unknown encoding `%s'", arg);
160 }
161
162 static const char *const audio_errlist[] = {
163 "error zero", /* nothing? */
164 "no audio entry", /* AUDIO_ENOENT */
165 "short header", /* AUDIO_ESHORTHDR */
166 "unsupported WAV format", /* AUDIO_EWAVUNSUPP */
167 "bad (unsupported) WAV PCM format", /* AUDIO_EWAVBADPCM */
168 "no WAV audio data", /* AUDIO_EWAVNODATA */
169 "internal error", /* AUDIO_EINTERNAL */
170 };
171
172 const char *
audio_errstring(int errval)173 audio_errstring(int errval)
174 {
175
176 errval = -errval;
177 if (errval < 1 || errval > AUDIO_MAXERRNO)
178 return "Invalid error";
179 return audio_errlist[errval];
180 }
181
182 void
write_header(struct track_info * ti)183 write_header(struct track_info *ti)
184 {
185 struct iovec iv[3];
186 int veclen, left, tlen;
187 void *hdr;
188 size_t hdrlen;
189
190 switch (ti->format) {
191 case AUDIO_FORMAT_DEFAULT:
192 case AUDIO_FORMAT_SUN:
193 if (sun_prepare_header(ti, &hdr, &hdrlen, &left) != 0)
194 return;
195 break;
196 case AUDIO_FORMAT_WAV:
197 if (wav_prepare_header(ti, &hdr, &hdrlen, &left) != 0)
198 return;
199 break;
200 case AUDIO_FORMAT_NONE:
201 return;
202 default:
203 errx(1, "unknown audio format");
204 }
205
206 veclen = 0;
207 tlen = 0;
208
209 if (hdrlen != 0) {
210 iv[veclen].iov_base = hdr;
211 iv[veclen].iov_len = hdrlen;
212 tlen += iv[veclen++].iov_len;
213 }
214 if (ti->header_info) {
215 iv[veclen].iov_base = ti->header_info;
216 iv[veclen].iov_len = (int)strlen(ti->header_info) + 1;
217 tlen += iv[veclen++].iov_len;
218 }
219 if (left) {
220 iv[veclen].iov_base = audio_default_info;
221 iv[veclen].iov_len = left;
222 tlen += iv[veclen++].iov_len;
223 }
224
225 if (tlen == 0)
226 return;
227
228 if (writev(ti->outfd, iv, veclen) != tlen)
229 err(1, "could not write audio header");
230 }
231
232 write_conv_func
write_get_conv_func(struct track_info * ti)233 write_get_conv_func(struct track_info *ti)
234 {
235
236 switch (ti->format) {
237 case AUDIO_FORMAT_DEFAULT:
238 case AUDIO_FORMAT_SUN:
239 return sun_write_get_conv_func(ti);
240 case AUDIO_FORMAT_WAV:
241 return wav_write_get_conv_func(ti);
242 case AUDIO_FORMAT_NONE:
243 return NULL;
244 default:
245 errx(1, "unknown audio format");
246 }
247 }
248