1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/* $NetBSD: audio.c,v 1.13 2002/01/01 08:07:28 mrg Exp $ */
/*
* Copyright (c) 1999 Matthew R. Green
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* XXX this is slightly icky in places...
*/
#include <sys/types.h>
#include <sys/audioio.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libaudio.h"
/* back and forth between encodings */
struct {
char *ename;
int eno;
} encs[] = {
{ AudioEmulaw, AUDIO_ENCODING_ULAW },
{ "ulaw", AUDIO_ENCODING_ULAW },
{ AudioEalaw, AUDIO_ENCODING_ALAW },
{ AudioEslinear, AUDIO_ENCODING_SLINEAR },
{ "linear", AUDIO_ENCODING_SLINEAR },
{ AudioEulinear, AUDIO_ENCODING_ULINEAR },
{ AudioEadpcm, AUDIO_ENCODING_ADPCM },
{ "ADPCM", AUDIO_ENCODING_ADPCM },
{ AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE },
{ "linear_le", AUDIO_ENCODING_SLINEAR_LE },
{ AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE },
{ AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE },
{ "linear_be", AUDIO_ENCODING_SLINEAR_BE },
{ AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE },
{ AudioEmpeg_l1_stream, AUDIO_ENCODING_MPEG_L1_STREAM },
{ AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
{ AudioEmpeg_l1_system, AUDIO_ENCODING_MPEG_L1_SYSTEM },
{ AudioEmpeg_l2_stream, AUDIO_ENCODING_MPEG_L2_STREAM },
{ AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
{ AudioEmpeg_l2_system, AUDIO_ENCODING_MPEG_L2_SYSTEM },
{ NULL, -1 }
};
char *
audio_enc_from_val(val)
int val;
{
int i;
for (i = 0; encs[i].ename; i++)
if (encs[i].eno == val)
break;
return (encs[i].ename);
}
int
audio_enc_to_val(enc)
const char *enc;
{
int i;
for (i = 0; encs[i].ename; i++)
if (strcmp(encs[i].ename, enc) == 0)
break;
if (encs[i].ename)
return (encs[i].eno);
else
return (-1);
}
/*
* SunOS/NeXT .au format helpers
*/
struct {
int file_encoding;
int encoding;
int precision;
} file2sw_encodings[] = {
{ AUDIO_FILE_ENCODING_MULAW_8, AUDIO_ENCODING_ULAW, 8 },
{ AUDIO_FILE_ENCODING_LINEAR_8, AUDIO_ENCODING_ULINEAR_BE, 8 },
{ AUDIO_FILE_ENCODING_LINEAR_16, AUDIO_ENCODING_ULINEAR_BE, 16 },
{ AUDIO_FILE_ENCODING_LINEAR_24, AUDIO_ENCODING_ULINEAR_BE, 24 },
{ AUDIO_FILE_ENCODING_LINEAR_32, AUDIO_ENCODING_ULINEAR_BE, 32 },
#if 0
/*
* we should make some of these available. the, eg ultrasparc, port
* can use the VIS instructions (if available) do do some of these
* mpeg ones.
*/
{ AUDIO_FILE_ENCODING_FLOAT, AUDIO_ENCODING_ULAW, 32 },
{ AUDIO_FILE_ENCODING_DOUBLE, AUDIO_ENCODING_ULAW, 64 },
{ AUDIO_FILE_ENCODING_ADPCM_G721, AUDIO_ENCODING_ULAW, 4 },
{ AUDIO_FILE_ENCODING_ADPCM_G722, AUDIO_ENCODING_ULAW, 0 },
{ AUDIO_FILE_ENCODING_ADPCM_G723_3, AUDIO_ENCODING_ULAW, 3 },
{ AUDIO_FILE_ENCODING_ADPCM_G723_5, AUDIO_ENCODING_ULAW, 5 },
#endif
{ AUDIO_FILE_ENCODING_ALAW_8, AUDIO_ENCODING_ALAW, 8 },
{ -1, -1 }
};
int
audio_sun_to_encoding(sun_encoding, encp, precp)
int sun_encoding;
int *encp;
int *precp;
{
int i;
for (i = 0; file2sw_encodings[i].file_encoding != -1; i++)
if (file2sw_encodings[i].file_encoding == sun_encoding) {
*precp = file2sw_encodings[i].precision;
*encp = file2sw_encodings[i].encoding;
return (0);
}
return (1);
}
int
audio_encoding_to_sun(encoding, precision, sunep)
int encoding;
int precision;
int *sunep;
{
int i;
for (i = 0; file2sw_encodings[i].file_encoding != -1; i++)
if (file2sw_encodings[i].encoding == encoding &&
file2sw_encodings[i].precision == precision) {
*sunep = file2sw_encodings[i].file_encoding;
return (0);
}
return (1);
}
/*
* sample header is:
*
* RIFF\^@^C^@WAVEfmt ^P^@^@^@^A^@^B^@D<AC>^@^@^P<B1>^B^@^D^@^P^@data^@^@^C^@^@^@^@^@^@^@^@^@^@
*
*/
/*
* WAV format helpers
*/
/*
* find a .wav header, etc. returns header length on success
*/
ssize_t
audio_parse_wav_hdr(hdr, sz, enc, prec, sample, channels, datasize)
void *hdr;
size_t sz;
int *enc;
int *prec;
int *sample;
int *channels;
size_t *datasize;
{
char *where = hdr;
wav_audioheaderpart *part;
wav_audioheaderfmt *fmt;
char *end = (((char *)hdr) + sz);
int newenc, newprec;
if (sz < 32)
return (AUDIO_ENOENT);
if (strncmp(where, "RIFF", 4))
return (AUDIO_ENOENT);
where += 8;
if (strncmp(where, "WAVE", 4))
return (AUDIO_ENOENT);
where += 4;
do {
part = (wav_audioheaderpart *)where;
where += getle32(part->len) + 8;
} while (where < end && strncmp(part->name, "fmt ", 4));
/* too short ? */
if (where + 16 > end)
return (AUDIO_ESHORTHDR);
fmt = (wav_audioheaderfmt *)(part + 1);
#if 0
printf("fmt header is:\n\t%d\ttag\n\t%d\tchannels\n\t%d\tsample rate\n\t%d\tavg_bps\n\t%d\talignment\n\t%d\tbits per sample\n", getle16(fmt->tag), getle16(fmt->channels), getle32(fmt->sample_rate), getle32(fmt->avg_bps), getle16(fmt->alignment), getle16(fmt->bits_per_sample));
#endif
switch (getle16(fmt->tag)) {
case WAVE_FORMAT_UNKNOWN:
case WAVE_FORMAT_ADPCM:
case WAVE_FORMAT_OKI_ADPCM:
case WAVE_FORMAT_DIGISTD:
case WAVE_FORMAT_DIGIFIX:
case IBM_FORMAT_MULAW:
case IBM_FORMAT_ALAW:
case IBM_FORMAT_ADPCM:
default:
return (AUDIO_EWAVUNSUPP);
case WAVE_FORMAT_PCM:
switch (getle16(fmt->bits_per_sample)) {
case 8:
newprec = 8;
break;
case 16:
newprec = 16;
break;
case 24:
newprec = 24;
break;
case 32:
newprec = 32;
break;
default:
return (AUDIO_EWAVBADPCM);
}
if (newprec == 8)
newenc = AUDIO_ENCODING_ULINEAR_LE;
else
newenc = AUDIO_ENCODING_SLINEAR_LE;
break;
case WAVE_FORMAT_ALAW:
newenc = AUDIO_ENCODING_ALAW;
newprec = 8;
break;
case WAVE_FORMAT_MULAW:
newenc = AUDIO_ENCODING_ULAW;
newprec = 8;
break;
}
do {
part = (wav_audioheaderpart *)where;
#if 0
printf("part `%c%c%c%c' len = %d\n", part->name[0], part->name[1], part->name[2], part->name[3], getle32(part->len));
#endif
where += (getle32(part->len) + 8);
} while ((char *)where < end && strncmp(part->name, "data", 4));
if ((where - getle32(part->len)) <= end) {
*channels = getle16(fmt->channels);
*sample = getle32(fmt->sample_rate);
*enc = newenc;
*prec = newprec;
if (datasize)
*datasize = (size_t)getle32(part->len);
part++;
return ((char *)part - (char *)hdr);
}
return (AUDIO_EWAVNODATA);
}
/*
* these belong elsewhere??
*/
void
decode_int(arg, intp)
const char *arg;
int *intp;
{
char *ep;
int ret;
ret = strtoul(arg, &ep, 0);
if (ep[0] == '\0') {
*intp = ret;
return;
}
errx(1, "argument `%s' not a valid integer", arg);
}
void
decode_time(arg, tvp)
const char *arg;
struct timeval *tvp;
{
char *s, *colon, *dot;
char *copy = strdup(arg);
int first;
if (copy == NULL)
err(1, "could not allocate a copy of %s", arg);
tvp->tv_sec = tvp->tv_usec = 0;
s = copy;
/* handle [hh:]mm:ss.dd */
if ((colon = strchr(s, ':'))) {
*colon++ = '\0';
decode_int(s, &first);
tvp->tv_sec = first * 60; /* minutes */
s = colon;
if ((colon = strchr(s, ':'))) {
*colon++ = '\0';
decode_int(s, &first);
tvp->tv_sec *= 60;
tvp->tv_sec += first; /* minutes and hours */
s = colon;
}
}
if ((dot = strchr(s, '.'))) {
int i, base = 100000;
*dot++ = '\0';
for (i = 0; i < 6; i++, base /= 10) {
if (!dot[i])
break;
if (!isdigit(dot[i]))
errx(1, "argument `%s' is not a value time specification", arg);
tvp->tv_usec += base * (dot[i] - '0');
}
}
decode_int(s, &first);
tvp->tv_sec += first;
#if 0
printf("tvp->tv_sec = %ld, tvp->tv_usec = %ld\n", tvp->tv_sec, tvp->tv_usec);
#endif
free(copy);
}
/*
* decode a string into an encoding value.
*/
void
decode_encoding(arg, encp)
const char *arg;
int *encp;
{
size_t len;
int i;
len = strlen(arg);
for (i = 0; encs[i].ename; i++)
if (strncmp(encs[i].ename, arg, len) == 0) {
*encp = encs[i].eno;
return;
}
errx(1, "unknown encoding `%s'", arg);
}
const char *const audio_errlist[] = {
"error zero", /* nothing? */
"no audio entry", /* AUDIO_ENOENT */
"short header", /* AUDIO_ESHORTHDR */
"unsupported WAV format", /* AUDIO_EWAVUNSUPP */
"bad (unsupported) WAV PCM format", /* AUDIO_EWAVBADPCM */
"no WAV audio data", /* AUDIO_EWAVNODATA */
};
const char *
audio_errstring(errval)
int errval;
{
errval = -errval;
if (errval < 1 || errval > AUDIO_MAXERRNO)
return "Invalid error";
return audio_errlist[errval];
}
|