-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwavplay.c
492 lines (451 loc) · 10.9 KB
/
wavplay.c
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*-
* wavplay - a C library to play WAV sound via OSS/ALSA
*
* Copyright (c) 2011, 2012
* Zhihao Yuan. All rights reserved.
*
* This file is distributed under the 2-clause BSD License.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <unistd.h>
#include "wavplay.h"
#if !defined(USE_ALSA)
#include <fcntl.h>
#include <sys/ioctl.h>
#if defined(__NetBSD__) || defined(__OpenBSD__)
#include <soundcard.h>
#else
#include <sys/soundcard.h>
#endif
#else
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>
#endif
#define BUF_SIZE 4096
#define eputs(s) (fprintf(stderr, "%s: " s "\n", __func__))
#ifndef USE_ALSA
#define WAV_FMT_8 AFMT_U8
#define AIF_FMT_8 AFMT_S8
#define WAV_FMT_16 AFMT_S16_LE
#define AIF_FMT_16 AFMT_S16_BE
#ifdef AFMT_S24_PACKED
#define WAV_FMT_24 AFMT_S24_PACKED
#elif __FreeBSD__
#define WAV_FMT_24 AFMT_S24_LE
#define SUN_FMT_24 AFMT_S24_BE
#endif
#ifdef AFMT_S32_LE
#define WAV_FMT_32 AFMT_S32_LE
#define AIF_FMT_32 AFMT_S32_BE
#endif
#define WAV_FMT_A_LAW AFMT_A_LAW
#define WAV_FMT_MU_LAW AFMT_MU_LAW
#define WAV_FMT_IMA_ADPCM AFMT_IMA_ADPCM
static int devfd = -1;
int snd_init(void) {
if (devfd > -1) snd_end();
devfd = open(DEV_NAME, O_WRONLY);
return devfd;
}
int snd_set(int format, int nchannels, int framerate) {
if (devfd > -1) {
fcntl(devfd, F_GETFD);
if (errno == EBADF) {
devfd = -1;
snd_init();
}
}
int st = 0;
st |= ioctl(devfd, SNDCTL_DSP_RESET, NULL);
st |= ioctl(devfd, SNDCTL_DSP_SETFMT, &format);
st |= ioctl(devfd, SNDCTL_DSP_CHANNELS, &nchannels);
st |= ioctl(devfd, SNDCTL_DSP_SPEED, &framerate);
return st;
}
int snd_end(void) {
int st = close(devfd);
devfd = -1;
return st;
}
int snd_send(FILE *fp, size_t n) {
unsigned char buf[BUF_SIZE];
size_t l;
while (n > sizeof(buf)) {
if ((l = fread(buf, 1, sizeof(buf), fp))) {
if (write(devfd, buf, l) < 0)
return -1;
}
else goto EOS;
n -= l;
}
if ((l = fread(buf, 1, n, fp)))
write(devfd, buf, l);
if (l < n)
EOS: if (ftell(fp) > 0) eputs("Unexpected end of stream");
return ioctl(devfd, SNDCTL_DSP_SYNC, NULL);
}
int snd_drop(void) {
return close(devfd);
}
#else
#define WAV_FMT_8 SND_PCM_FORMAT_U8
#define AIF_FMT_8 SND_PCM_FORMAT_S8
#define WAV_FMT_16 SND_PCM_FORMAT_S16_LE
#define AIF_FMT_16 SND_PCM_FORMAT_S16_BE
#define WAV_FMT_24 SND_PCM_FORMAT_S24_3LE
#define SUN_FMT_24 SND_PCM_FORMAT_S24_3BE
#define WAV_FMT_32 SND_PCM_FORMAT_S32_LE
#define AIF_FMT_32 SND_PCM_FORMAT_S32_BE
#define WAV_FMT_A_LAW SND_PCM_FORMAT_A_LAW
#define WAV_FMT_MU_LAW SND_PCM_FORMAT_MU_LAW
#define WAV_FMT_IMA_ADPCM SND_PCM_FORMAT_IMA_ADPCM
static snd_pcm_t *pcm = NULL;
int snd_init(void) {
if (pcm) snd_end();
return snd_pcm_open(&pcm, DEV_NAME, SND_PCM_STREAM_PLAYBACK, 0);
}
int snd_set(int format, int nchannels, int framerate) {
int st = 0;
unsigned int val = framerate;
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcm, params);
st |= snd_pcm_hw_params_set_access(pcm, params, SND_PCM_ACCESS_RW_INTERLEAVED);
st |= snd_pcm_hw_params_set_format(pcm, params, format);
st |= snd_pcm_hw_params_set_channels(pcm, params, nchannels);
st |= snd_pcm_hw_params_set_rate_near(pcm, params, &val, 0);
st |= snd_pcm_hw_params(pcm, params);
return st;
}
int snd_end(void) {
int st = snd_pcm_close(pcm);
pcm = NULL;
return st;
}
int snd_send(FILE *fp, size_t n) {
snd_pcm_format_t format;
unsigned int nchannels;
snd_pcm_uframes_t period;
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_current(pcm, params);
snd_pcm_hw_params_get_format(params, &format);
snd_pcm_hw_params_get_channels(params, &nchannels);
snd_pcm_hw_params_get_period_size(params, &period, 0);
int framesize = snd_pcm_format_width(format) / 8 * nchannels;
unsigned char buf[period * framesize * 128];
size_t l;
while (n > sizeof(buf)) {
if ((l = fread(buf, 1, sizeof(buf), fp))) {
switch (snd_pcm_writei(pcm, buf, l / framesize)) {
case -EBADF:
return -1;
case -EPIPE:
#ifndef NDEBUG
snd_pcm_recover(pcm, -EPIPE, 0);
#else
snd_pcm_prepare(pcm);
#endif
}
}
else goto EOS;
n -= l;
}
if ((l = fread(buf, 1, n, fp)))
snd_pcm_writei(pcm, buf, l / framesize);
if (l < n)
EOS: if (ftell(fp) > 0) eputs("Unexpected end of stream");
return snd_pcm_drain(pcm);
}
int snd_drop(void) {
return snd_pcm_drop(pcm);
}
#endif
static int wav2format(const wavheader_t *wav) {
switch (wav->format) {
case 1:
case -2:
switch ((wav->bitdepth + 7) / 8) {
case 1: return WAV_FMT_8;
case 2: return WAV_FMT_16;
#ifdef WAV_FMT_24
case 3: return WAV_FMT_24;
#endif
#ifdef WAV_FMT_32
case 4: return WAV_FMT_32;
#endif
default: return -1;
}
case 258:
case 6: return WAV_FMT_A_LAW;
case 257:
case 7: return WAV_FMT_MU_LAW;
case 17: return wav->bitdepth == 4 ? WAV_FMT_IMA_ADPCM : -1;
default: return -1;
}
}
#define chktype(s) (!strncmp(aif->comptype, s, 4))
static int aif2format(aifheader_t *aif) {
int sampwidth = (aif->bitdepth + 7) / 8;
if (chktype("") || chktype("NONE"))
switch (sampwidth) {
case 1: return AIF_FMT_8;
case 2: return AIF_FMT_16;
#ifdef WAV_FMT_24
case 3: return WAV_FMT_24;
#endif
#ifdef AIF_FMT_32
case 4: return AIF_FMT_32;
#endif
default: return -1;
}
else if (chktype("sowt")) {
if (sampwidth == 1)
return AIF_FMT_8;
return wav2format(&(wavheader_t)
{ .format = 1, .bitdepth = aif->bitdepth });
}
else if (chktype("alaw") || chktype("ALAW"))
return WAV_FMT_A_LAW;
else if (chktype("ulaw") || chktype("ULAW"))
return WAV_FMT_MU_LAW;
else if (chktype("ima4"))
return WAV_FMT_IMA_ADPCM;
else
return -1;
}
static int sun2format(sunheader_t *sun) {
switch (sun->encoding) {
case 1: return WAV_FMT_MU_LAW;
case 2: return AIF_FMT_8;
case 3: return AIF_FMT_16;
#ifdef SUN_FMT_24
case 4: return SUN_FMT_24;
#endif
#ifdef AIF_FMT_32
case 5: return AIF_FMT_32;
#endif
case 27: return WAV_FMT_A_LAW;
default: return -1;
}
}
#undef bswap16
#undef bswap32
#undef bswap64
static __inline uint16_t bswap16(uint16_t x)
{
return (x << 8 | x >> 8);
}
/* $NetBSD: bswap32.c,v 1.1 1997/10/09 15:42:33 bouyer Exp $ */
static __inline uint32_t bswap32(uint32_t x)
{
return ((x << 24) & 0xff000000 ) |
((x << 8) & 0x00ff0000 ) |
((x >> 8) & 0x0000ff00 ) |
((x >> 24) & 0x000000ff );
}
static __inline uint64_t bswap64(uint64_t x)
{
uint32_t *p = (void*)&x;
uint32_t t;
t = bswap32(p[0]);
p[0] = bswap32(p[1]);
p[1] = t;
return x;
}
/*
* converts any endianess to host
* returns -1 when format is wrong,
* 0 when no convertion is needed,
* positive value == len(fmt).
*/
static int endian2h(const char fmt[], void *p) {
#if BYTE_ORDER == BIG_ENDIAN
if (fmt[0] == '>') return 0;
else if (fmt[0] != '<') return -1;
#else
if (fmt[0] == '<') return 0;
else if (fmt[0] != '>') return -1;
#endif
const char *i = fmt;
while (*(++i) != '\0') {
#define fmtInt(x, T) \
case x: { \
uint##T##_t *n = p; \
*n = bswap##T(*n); \
p += sizeof(uint##T##_t); \
break; \
}
#define fmtSkip(x, n) \
case x: { \
p += n; \
break; \
}
switch (*i) {
fmtInt('h', 16);
fmtInt('l', 32);
fmtInt('q', 64);
fmtSkip('.', 1);
default : return -1;
}
#undef fmtInt
#undef fmtSkip
}
return i - fmt;
}
typedef union {
uint32_t com[4];
long double value;
} ldouble_t;
static __inline long double ext2l(extdouble_t x) {
return (ldouble_t)
#if BYTE_ORDER == BIG_ENDIAN
{{ 0, x.expon, x.himant, x.lomant }}
#else
{{ x.lomant, x.himant, x.expon, 0 }}
#endif
.value;
}
#define skip(n) do { \
if (! fseek(fp, 0, SEEK_CUR)) { \
fseek(fp, (n), SEEK_CUR); \
break; \
} \
char c[BUF_SIZE]; \
size_t i = n; \
ssize_t l; \
while (i > 0) { \
l = fread(c, 1, i > sizeof(c) ? sizeof(c) : i, fp); \
if (l > 0) i -= l; \
else break; \
} \
} while (0)
#define read2(t) (fread(&t, sizeof(t), 1, fp))
#define chkid(s) (!strncmp(ck.id, s, 4))
static size_t wavparse(wavheader_t *wav, FILE *fp) {
riffchunk_t ck;
if (!read2(ck.id) || !chkid("WAVE"))
eputs("Not a WAVE file");
else {
while (read2(ck)) {
endian2h("<l", &ck.size);
if (chkid("data"))
return ck.size;
ck.size = (ck.size + 1) / 2 * 2;
if (chkid("fmt ")) {
fread(wav, sizeof(wavheader_t), 1, fp);
if (ck.size < sizeof(wavheader_t))
eputs("Bad format chunk");
else
skip(ck.size - sizeof(wavheader_t));
}
else skip(ck.size);
}
eputs("Malformed WAVE file");
}
return 0;
}
static size_t aifparse(aifheader_t *aif, FILE *fp) {
riffchunk_t ck;
if (!read2(ck.id) || !(chkid("AIFF") || chkid("AIFC")))
eputs("Not an AIFF/AIFC file");
else {
while (read2(ck)) {
endian2h(">l", &ck.size);
if (chkid("SSND"))
return ck.size;
ck.size = (ck.size + 1) / 2 * 2;
if (chkid("COMM")) {
size_t ckl = MIN(ck.size, sizeof(aifheader_t));
fread(aif, ckl, 1, fp);
if (ckl < (sizeof(aifheader_t) - sizeof(aif->comptype)))
eputs("Bad common chunk");
else
skip(ck.size - ckl);
}
else skip(ck.size);
}
eputs("Malformed AIFF/AIFC file");
}
return 0;
}
static size_t wav_readinfo(wav_info_t *info, FILE *fp) {
riffchunk_t ck;
if (read2(ck)) {
if (chkid("RIFF")) {
wavheader_t wav[1];
size_t sz = wavparse(wav, fp);
endian2h("<hhllhh", wav);
info->nchannels = wav->nchannels;
info->framerate = wav->framerate;
info->sampwidth = (wav->bitdepth + 7) / 8;
info->nframes = sz / (info->nchannels * info->sampwidth);
info->devformat = wav2format(wav);
return sz;
}
else if (chkid("FORM")) {
aifheader_t aif[1] = {{ .comptype = "" }};
size_t sz = aifparse(aif, fp);
endian2h(">hlhhll", aif);
info->nchannels = aif->nchannels;
info->framerate = (int) ext2l(aif->framerate);
info->sampwidth = (aif->bitdepth + 7) / 8;
info->nframes = aif->nframes;
info->devformat = aif2format(aif);
return sz;
}
else if (chkid(".snd")) {
sunheader_t sun;
endian2h(">l", &ck.size);
read2(sun);
endian2h(">llll", &sun);
if (ck.size < sizeof(ck) + sizeof(sun))
eputs("Bad Au header");
else
skip(ck.size - sizeof(ck) - sizeof(sun));
info->nchannels = sun.nchannels;
info->framerate = sun.framerate;
if (1 < sun.encoding && sun.encoding < 6)
info->nframes = sun.size /
(info->sampwidth = sun.encoding - 1);
else {
info->sampwidth = 2;
info->nframes = sun.size;
}
info->devformat = sun2format(&sun);
return sun.size;
}
}
eputs("Unknown file format");
return 0;
}
#undef skip
#undef read2
#undef chkid
static int wav_setdev(const wav_info_t *info) {
if (info->devformat < 0)
eputs("Unsupported PCM format");
else if (snd_set(info->devformat, info->nchannels, info->framerate))
eputs("Failed to setup the sound device");
else return 0;
return -1;
}
int wav_send(FILE *fp) {
wav_info_t info[1];
size_t size = wav_readinfo(info, fp);
if (size && !wav_setdev(info))
return snd_send(fp, size);
return -1;
}
int wav_play(const char *fn) {
int st = -1;
FILE *fp = fopen(fn, "rb");
if (fp) {
st = wav_send(fp);
fclose(fp);
} else perror(__func__);
return st;
}