1 /*
2 ** Copyright (C) 1999-2018 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are
8 ** met:
9 **
10 ** * Redistributions of source code must retain the above copyright
11 ** notice, this list of conditions and the following disclaimer.
12 ** * Redistributions in binary form must reproduce the above copyright
13 ** notice, this list of conditions and the following disclaimer in
14 ** the documentation and/or other materials provided with the
15 ** distribution.
16 ** * Neither the author nor the names of any contributors may be used
17 ** to endorse or promote products derived from this software without
18 ** specific prior written permission.
19 **
20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "sfconfig.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #if HAVE_UNISTD_H
41 #include <unistd.h>
42 #else
43 #include "sf_unistd.h"
44 #endif
45
46 #include <sndfile.h>
47
48 #include "common.h"
49
50 #if HAVE_ALSA_ASOUNDLIB_H
51 #define ALSA_PCM_NEW_HW_PARAMS_API
52 #define ALSA_PCM_NEW_SW_PARAMS_API
53 #include <alsa/asoundlib.h>
54 #include <sys/time.h>
55 #endif
56
57 #if defined (__ANDROID__)
58
59 #elif defined (__linux__) || defined (__FreeBSD_kernel__) || defined (__FreeBSD__)
60 #include <fcntl.h>
61 #include <sys/ioctl.h>
62 #include <sys/soundcard.h>
63
64 #elif HAVE_SNDIO_H
65 #include <sndio.h>
66
67 #elif (defined (sun) && defined (unix))
68 #include <fcntl.h>
69 #include <sys/ioctl.h>
70 #include <sys/audioio.h>
71
72 #elif (OS_IS_WIN32 == 1)
73 #include <windows.h>
74 #include <mmsystem.h>
75
76 #endif
77
78 #define SIGNED_SIZEOF(x) ((int) sizeof (x))
79 #define BUFFER_LEN (2048)
80
81 /*------------------------------------------------------------------------------
82 ** Linux/OSS functions for playing a sound.
83 */
84
85 #if HAVE_ALSA_ASOUNDLIB_H
86
87 static snd_pcm_t * alsa_open (int channels, unsigned srate, int realtime) ;
88 static int alsa_write_float (snd_pcm_t *alsa_dev, float *data, int frames, int channels) ;
89
90 static void
alsa_play(int argc,char * argv[])91 alsa_play (int argc, char *argv [])
92 { static float buffer [BUFFER_LEN] ;
93 SNDFILE *sndfile ;
94 SF_INFO sfinfo ;
95 snd_pcm_t * alsa_dev ;
96 int k, readcount, subformat ;
97
98 for (k = 1 ; k < argc ; k++)
99 { memset (&sfinfo, 0, sizeof (sfinfo)) ;
100
101 printf ("Playing %s\n", argv [k]) ;
102 if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo)))
103 { puts (sf_strerror (NULL)) ;
104 continue ;
105 } ;
106
107 if (sfinfo.channels < 1 || sfinfo.channels > 2)
108 { printf ("Error : channels = %d.\n", sfinfo.channels) ;
109 continue ;
110 } ;
111
112 if ((alsa_dev = alsa_open (sfinfo.channels, (unsigned) sfinfo.samplerate, SF_FALSE)) == NULL)
113 continue ;
114
115 subformat = sfinfo.format & SF_FORMAT_SUBMASK ;
116
117 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
118 { double scale ;
119 int m ;
120
121 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
122 if (scale > 1.0)
123 scale = 1.0 / scale ;
124 else
125 scale = 1.0 ;
126
127 while ((readcount = sf_read_float (sndfile, buffer, BUFFER_LEN)))
128 { for (m = 0 ; m < readcount ; m++)
129 buffer [m] *= scale ;
130 alsa_write_float (alsa_dev, buffer, BUFFER_LEN / sfinfo.channels, sfinfo.channels) ;
131 } ;
132 }
133 else
134 { while ((readcount = sf_read_float (sndfile, buffer, BUFFER_LEN)))
135 alsa_write_float (alsa_dev, buffer, BUFFER_LEN / sfinfo.channels, sfinfo.channels) ;
136 } ;
137
138 snd_pcm_drain (alsa_dev) ;
139 snd_pcm_close (alsa_dev) ;
140
141 sf_close (sndfile) ;
142 } ;
143
144 return ;
145 } /* alsa_play */
146
147 static snd_pcm_t *
alsa_open(int channels,unsigned samplerate,int realtime)148 alsa_open (int channels, unsigned samplerate, int realtime)
149 { const char * device = "default" ;
150 snd_pcm_t *alsa_dev = NULL ;
151 snd_pcm_hw_params_t *hw_params ;
152 snd_pcm_uframes_t buffer_size ;
153 snd_pcm_uframes_t alsa_period_size, alsa_buffer_frames ;
154 snd_pcm_sw_params_t *sw_params ;
155
156 int err ;
157
158 if (realtime)
159 { alsa_period_size = 256 ;
160 alsa_buffer_frames = 3 * alsa_period_size ;
161 }
162 else
163 { alsa_period_size = 1024 ;
164 alsa_buffer_frames = 4 * alsa_period_size ;
165 } ;
166
167 if ((err = snd_pcm_open (&alsa_dev, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
168 { fprintf (stderr, "cannot open audio device \"%s\" (%s)\n", device, snd_strerror (err)) ;
169 goto catch_error ;
170 } ;
171
172 snd_pcm_nonblock (alsa_dev, 0) ;
173
174 if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
175 { fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err)) ;
176 goto catch_error ;
177 } ;
178
179 if ((err = snd_pcm_hw_params_any (alsa_dev, hw_params)) < 0)
180 { fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err)) ;
181 goto catch_error ;
182 } ;
183
184 if ((err = snd_pcm_hw_params_set_access (alsa_dev, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
185 { fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err)) ;
186 goto catch_error ;
187 } ;
188
189 if ((err = snd_pcm_hw_params_set_format (alsa_dev, hw_params, SND_PCM_FORMAT_FLOAT)) < 0)
190 { fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err)) ;
191 goto catch_error ;
192 } ;
193
194 if ((err = snd_pcm_hw_params_set_rate_near (alsa_dev, hw_params, &samplerate, 0)) < 0)
195 { fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err)) ;
196 goto catch_error ;
197 } ;
198
199 if ((err = snd_pcm_hw_params_set_channels (alsa_dev, hw_params, channels)) < 0)
200 { fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err)) ;
201 goto catch_error ;
202 } ;
203
204 if ((err = snd_pcm_hw_params_set_buffer_size_near (alsa_dev, hw_params, &alsa_buffer_frames)) < 0)
205 { fprintf (stderr, "cannot set buffer size (%s)\n", snd_strerror (err)) ;
206 goto catch_error ;
207 } ;
208
209 if ((err = snd_pcm_hw_params_set_period_size_near (alsa_dev, hw_params, &alsa_period_size, 0)) < 0)
210 { fprintf (stderr, "cannot set period size (%s)\n", snd_strerror (err)) ;
211 goto catch_error ;
212 } ;
213
214 if ((err = snd_pcm_hw_params (alsa_dev, hw_params)) < 0)
215 { fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err)) ;
216 goto catch_error ;
217 } ;
218
219 /* extra check: if we have only one period, this code won't work */
220 snd_pcm_hw_params_get_period_size (hw_params, &alsa_period_size, 0) ;
221 snd_pcm_hw_params_get_buffer_size (hw_params, &buffer_size) ;
222 if (alsa_period_size == buffer_size)
223 { fprintf (stderr, "Can't use period equal to buffer size (%lu == %lu)", alsa_period_size, buffer_size) ;
224 goto catch_error ;
225 } ;
226
227 snd_pcm_hw_params_free (hw_params) ;
228
229 if ((err = snd_pcm_sw_params_malloc (&sw_params)) != 0)
230 { fprintf (stderr, "%s: snd_pcm_sw_params_malloc: %s", __func__, snd_strerror (err)) ;
231 goto catch_error ;
232 } ;
233
234 if ((err = snd_pcm_sw_params_current (alsa_dev, sw_params)) != 0)
235 { fprintf (stderr, "%s: snd_pcm_sw_params_current: %s", __func__, snd_strerror (err)) ;
236 goto catch_error ;
237 } ;
238
239 /* note: set start threshold to delay start until the ring buffer is full */
240 snd_pcm_sw_params_current (alsa_dev, sw_params) ;
241
242 if ((err = snd_pcm_sw_params_set_start_threshold (alsa_dev, sw_params, buffer_size)) < 0)
243 { fprintf (stderr, "cannot set start threshold (%s)\n", snd_strerror (err)) ;
244 goto catch_error ;
245 } ;
246
247 if ((err = snd_pcm_sw_params (alsa_dev, sw_params)) != 0)
248 { fprintf (stderr, "%s: snd_pcm_sw_params: %s", __func__, snd_strerror (err)) ;
249 goto catch_error ;
250 } ;
251
252 snd_pcm_sw_params_free (sw_params) ;
253
254 snd_pcm_reset (alsa_dev) ;
255
256 catch_error :
257
258 if (err < 0 && alsa_dev != NULL)
259 { snd_pcm_close (alsa_dev) ;
260 return NULL ;
261 } ;
262
263 return alsa_dev ;
264 } /* alsa_open */
265
266 static int
alsa_write_float(snd_pcm_t * alsa_dev,float * data,int frames,int channels)267 alsa_write_float (snd_pcm_t *alsa_dev, float *data, int frames, int channels)
268 { static int epipe_count = 0 ;
269
270 int total = 0 ;
271 int retval ;
272
273 if (epipe_count > 0)
274 epipe_count -- ;
275
276 while (total < frames)
277 { retval = snd_pcm_writei (alsa_dev, data + total * channels, frames - total) ;
278
279 if (retval >= 0)
280 { total += retval ;
281 if (total == frames)
282 return total ;
283
284 continue ;
285 } ;
286
287 switch (retval)
288 { case -EAGAIN :
289 puts ("alsa_write_float: EAGAIN") ;
290 continue ;
291 break ;
292
293 case -EPIPE :
294 if (epipe_count > 0)
295 { printf ("alsa_write_float: EPIPE %d\n", epipe_count) ;
296 if (epipe_count > 140)
297 return retval ;
298 } ;
299 epipe_count += 100 ;
300
301 #if 0
302 if (0)
303 { snd_pcm_status_t *status ;
304
305 snd_pcm_status_alloca (&status) ;
306 if ((retval = snd_pcm_status (alsa_dev, status)) < 0)
307 fprintf (stderr, "alsa_out: xrun. can't determine length\n") ;
308 else if (snd_pcm_status_get_state (status) == SND_PCM_STATE_XRUN)
309 { struct timeval now, diff, tstamp ;
310
311 gettimeofday (&now, 0) ;
312 snd_pcm_status_get_trigger_tstamp (status, &tstamp) ;
313 timersub (&now, &tstamp, &diff) ;
314
315 fprintf (stderr, "alsa_write_float xrun: of at least %.3f msecs. resetting stream\n",
316 diff.tv_sec * 1000 + diff.tv_usec / 1000.0) ;
317 }
318 else
319 fprintf (stderr, "alsa_write_float: xrun. can't determine length\n") ;
320 } ;
321 #endif
322
323 snd_pcm_prepare (alsa_dev) ;
324 break ;
325
326 case -EBADFD :
327 fprintf (stderr, "alsa_write_float: Bad PCM state.n") ;
328 return 0 ;
329 break ;
330
331 case -ESTRPIPE :
332 fprintf (stderr, "alsa_write_float: Suspend event.n") ;
333 return 0 ;
334 break ;
335
336 case -EIO :
337 puts ("alsa_write_float: EIO") ;
338 return 0 ;
339
340 default :
341 fprintf (stderr, "alsa_write_float: retval = %d\n", retval) ;
342 return 0 ;
343 break ;
344 } ; /* switch */
345 } ; /* while */
346
347 return total ;
348 } /* alsa_write_float */
349
350 #endif /* HAVE_ALSA_ASOUNDLIB_H */
351
352 /*------------------------------------------------------------------------------
353 ** Linux/OSS functions for playing a sound.
354 */
355
356 #if !defined (__ANDROID__) && (defined (__linux__) || defined (__FreeBSD_kernel__) || defined (__FreeBSD__))
357
358 static int opensoundsys_open_device (int channels, int srate) ;
359
360 static int
opensoundsys_play(int argc,char * argv[])361 opensoundsys_play (int argc, char *argv [])
362 { static short buffer [BUFFER_LEN] ;
363 SNDFILE *sndfile ;
364 SF_INFO sfinfo ;
365 int k, audio_device, readcount, writecount, subformat ;
366
367 for (k = 1 ; k < argc ; k++)
368 { memset (&sfinfo, 0, sizeof (sfinfo)) ;
369
370 printf ("Playing %s\n", argv [k]) ;
371 if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo)))
372 { puts (sf_strerror (NULL)) ;
373 continue ;
374 } ;
375
376 if (sfinfo.channels < 1 || sfinfo.channels > 2)
377 { printf ("Error : channels = %d.\n", sfinfo.channels) ;
378 continue ;
379 } ;
380
381 audio_device = opensoundsys_open_device (sfinfo.channels, sfinfo.samplerate) ;
382
383 subformat = sfinfo.format & SF_FORMAT_SUBMASK ;
384
385 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
386 { static float float_buffer [BUFFER_LEN] ;
387 double scale ;
388 int m ;
389
390 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
391 if (scale < 1e-10)
392 scale = 1.0 ;
393 else
394 scale = 32700.0 / scale ;
395
396 while ((readcount = sf_read_float (sndfile, float_buffer, BUFFER_LEN)))
397 { for (m = 0 ; m < readcount ; m++)
398 buffer [m] = scale * float_buffer [m] ;
399 writecount = write (audio_device, buffer, readcount * sizeof (short)) ;
400 } ;
401 }
402 else
403 { while ((readcount = sf_read_short (sndfile, buffer, BUFFER_LEN)))
404 writecount = write (audio_device, buffer, readcount * sizeof (short)) ;
405 } ;
406
407 if (ioctl (audio_device, SNDCTL_DSP_POST, 0) == -1)
408 perror ("ioctl (SNDCTL_DSP_POST) ") ;
409
410 if (ioctl (audio_device, SNDCTL_DSP_SYNC, 0) == -1)
411 perror ("ioctl (SNDCTL_DSP_SYNC) ") ;
412
413 close (audio_device) ;
414
415 sf_close (sndfile) ;
416 } ;
417
418 return writecount ;
419 } /* opensoundsys_play */
420
421 static int
opensoundsys_open_device(int channels,int srate)422 opensoundsys_open_device (int channels, int srate)
423 { int fd, stereo, fmt ;
424
425 if ((fd = open ("/dev/dsp", O_WRONLY, 0)) == -1 &&
426 (fd = open ("/dev/sound/dsp", O_WRONLY, 0)) == -1)
427 { perror ("opensoundsys_open_device : open ") ;
428 exit (1) ;
429 } ;
430
431 stereo = 0 ;
432 if (ioctl (fd, SNDCTL_DSP_STEREO, &stereo) == -1)
433 { /* Fatal error */
434 perror ("opensoundsys_open_device : stereo ") ;
435 exit (1) ;
436 } ;
437
438 if (ioctl (fd, SNDCTL_DSP_RESET, 0))
439 { perror ("opensoundsys_open_device : reset ") ;
440 exit (1) ;
441 } ;
442
443 fmt = CPU_IS_BIG_ENDIAN ? AFMT_S16_BE : AFMT_S16_LE ;
444 if (ioctl (fd, SNDCTL_DSP_SETFMT, &fmt) != 0)
445 { perror ("opensoundsys_open_device : set format ") ;
446 exit (1) ;
447 } ;
448
449 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &channels) != 0)
450 { perror ("opensoundsys_open_device : channels ") ;
451 exit (1) ;
452 } ;
453
454 if (ioctl (fd, SNDCTL_DSP_SPEED, &srate) != 0)
455 { perror ("opensoundsys_open_device : sample rate ") ;
456 exit (1) ;
457 } ;
458
459 if (ioctl (fd, SNDCTL_DSP_SYNC, 0) != 0)
460 { perror ("opensoundsys_open_device : sync ") ;
461 exit (1) ;
462 } ;
463
464 return fd ;
465 } /* opensoundsys_open_device */
466
467 #endif /* __linux__ */
468
469 /*------------------------------------------------------------------------------
470 ** Mac OS X functions for playing a sound.
471 */
472
473 /* MacOSX 10.8 use a new Audio API. Someone needs to write some code for it. */
474
475 /*------------------------------------------------------------------------------
476 ** Win32 functions for playing a sound.
477 **
478 ** This API sucks. Its needlessly complicated and is *WAY* too loose with
479 ** passing pointers around in integers and using char* pointers to
480 ** point to data instead of short*. It plain sucks!
481 */
482
483 #if (OS_IS_WIN32 == 1)
484
485 #define WIN32_BUFFER_LEN (1 << 15)
486
487 typedef struct
488 { HWAVEOUT hwave ;
489 WAVEHDR whdr [2] ;
490
491 CRITICAL_SECTION mutex ; /* to control access to BuffersInUSe */
492 HANDLE Event ; /* signal that a buffer is free */
493
494 short buffer [WIN32_BUFFER_LEN / sizeof (short)] ;
495 int current, bufferlen ;
496 int BuffersInUse ;
497
498 SNDFILE *sndfile ;
499 SF_INFO sfinfo ;
500
501 sf_count_t remaining ;
502 } Win32_Audio_Data ;
503
504
505 static void
win32_play_data(Win32_Audio_Data * audio_data)506 win32_play_data (Win32_Audio_Data *audio_data)
507 { int thisread, readcount ;
508
509 /* fill a buffer if there is more data and we can read it sucessfully */
510 readcount = (audio_data->remaining > audio_data->bufferlen) ? audio_data->bufferlen : (int) audio_data->remaining ;
511
512 short *lpData = (short *) (void *) audio_data->whdr [audio_data->current].lpData ;
513 thisread = (int) sf_read_short (audio_data->sndfile, lpData, readcount) ;
514
515 audio_data->remaining -= thisread ;
516
517 if (thisread > 0)
518 { /* Fix buffer length if this is only a partial block. */
519 if (thisread < audio_data->bufferlen)
520 audio_data->whdr [audio_data->current].dwBufferLength = thisread * sizeof (short) ;
521
522 /* Queue the WAVEHDR */
523 waveOutWrite (audio_data->hwave, (LPWAVEHDR) &(audio_data->whdr [audio_data->current]), sizeof (WAVEHDR)) ;
524
525 /* count another buffer in use */
526 EnterCriticalSection (&audio_data->mutex) ;
527 audio_data->BuffersInUse ++ ;
528 LeaveCriticalSection (&audio_data->mutex) ;
529
530 /* use the other buffer next time */
531 audio_data->current = (audio_data->current + 1) % 2 ;
532 } ;
533
534 return ;
535 } /* win32_play_data */
536
537 static void CALLBACK
win32_audio_out_callback(HWAVEOUT hwave,UINT msg,DWORD_PTR data,DWORD param1,DWORD param2)538 win32_audio_out_callback (HWAVEOUT hwave, UINT msg, DWORD_PTR data, DWORD param1, DWORD param2)
539 { Win32_Audio_Data *audio_data ;
540
541 /* Prevent compiler warnings. */
542 (void) hwave ;
543 (void) param1 ;
544 (void) param2 ;
545
546 if (data == 0)
547 return ;
548
549 /*
550 ** I consider this technique of passing a pointer via an integer as
551 ** fundamentally broken but thats the way microsoft has defined the
552 ** interface.
553 */
554 audio_data = (Win32_Audio_Data*) data ;
555
556 /* let main loop know a buffer is free */
557 if (msg == MM_WOM_DONE)
558 { EnterCriticalSection (&audio_data->mutex) ;
559 audio_data->BuffersInUse -- ;
560 LeaveCriticalSection (&audio_data->mutex) ;
561 SetEvent (audio_data->Event) ;
562 } ;
563
564 return ;
565 } /* win32_audio_out_callback */
566
567 static void
win32_play(int argc,char * argv[])568 win32_play (int argc, char *argv [])
569 { Win32_Audio_Data audio_data ;
570
571 WAVEFORMATEX wf ;
572 int k, error ;
573
574 audio_data.sndfile = NULL ;
575 audio_data.hwave = 0 ;
576
577 for (k = 1 ; k < argc ; k++)
578 { printf ("Playing %s\n", argv [k]) ;
579
580 if (! (audio_data.sndfile = sf_open (argv [k], SFM_READ, &(audio_data.sfinfo))))
581 { puts (sf_strerror (NULL)) ;
582 continue ;
583 } ;
584
585 audio_data.remaining = audio_data.sfinfo.frames * audio_data.sfinfo.channels ;
586 audio_data.current = 0 ;
587
588 InitializeCriticalSection (&audio_data.mutex) ;
589 audio_data.Event = CreateEvent (0, FALSE, FALSE, 0) ;
590
591 wf.nChannels = audio_data.sfinfo.channels ;
592 wf.wFormatTag = WAVE_FORMAT_PCM ;
593 wf.cbSize = 0 ;
594 wf.wBitsPerSample = 16 ;
595
596 wf.nSamplesPerSec = audio_data.sfinfo.samplerate ;
597
598 wf.nBlockAlign = audio_data.sfinfo.channels * sizeof (short) ;
599
600 wf.nAvgBytesPerSec = wf.nBlockAlign * wf.nSamplesPerSec ;
601
602 error = waveOutOpen (&(audio_data.hwave), WAVE_MAPPER, &wf, (DWORD_PTR) win32_audio_out_callback,
603 (DWORD_PTR) &audio_data, CALLBACK_FUNCTION) ;
604 if (error)
605 { puts ("waveOutOpen failed.") ;
606 audio_data.hwave = 0 ;
607 continue ;
608 } ;
609
610 audio_data.whdr [0].lpData = (char*) audio_data.buffer ;
611 audio_data.whdr [1].lpData = ((char*) audio_data.buffer) + sizeof (audio_data.buffer) / 2 ;
612
613 audio_data.whdr [0].dwBufferLength = sizeof (audio_data.buffer) / 2 ;
614 audio_data.whdr [1].dwBufferLength = sizeof (audio_data.buffer) / 2 ;
615
616 audio_data.whdr [0].dwFlags = 0 ;
617 audio_data.whdr [1].dwFlags = 0 ;
618
619 /* length of each audio buffer in samples */
620 audio_data.bufferlen = sizeof (audio_data.buffer) / 2 / sizeof (short) ;
621
622 /* Prepare the WAVEHDRs */
623 if ((error = waveOutPrepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR))))
624 { printf ("waveOutPrepareHeader [0] failed : %08X\n", error) ;
625 waveOutClose (audio_data.hwave) ;
626 continue ;
627 } ;
628
629 if ((error = waveOutPrepareHeader (audio_data.hwave, &(audio_data.whdr [1]), sizeof (WAVEHDR))))
630 { printf ("waveOutPrepareHeader [1] failed : %08X\n", error) ;
631 waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR)) ;
632 waveOutClose (audio_data.hwave) ;
633 continue ;
634 } ;
635
636 /* Fill up both buffers with audio data */
637 audio_data.BuffersInUse = 0 ;
638 win32_play_data (&audio_data) ;
639 win32_play_data (&audio_data) ;
640
641 /* loop until both buffers are released */
642 while (audio_data.BuffersInUse > 0)
643 {
644 /* wait for buffer to be released */
645 WaitForSingleObject (audio_data.Event, INFINITE) ;
646
647 /* refill the buffer if there is more data to play */
648 win32_play_data (&audio_data) ;
649 } ;
650
651 waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR)) ;
652 waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [1]), sizeof (WAVEHDR)) ;
653
654 waveOutClose (audio_data.hwave) ;
655 audio_data.hwave = 0 ;
656
657 DeleteCriticalSection (&audio_data.mutex) ;
658
659 sf_close (audio_data.sndfile) ;
660 } ;
661
662 } /* win32_play */
663
664 #endif /* Win32 */
665
666 /*------------------------------------------------------------------------------
667 ** OpenBSD's sndio.
668 */
669
670 #if HAVE_SNDIO_H
671
672 static void
sndio_play(int argc,char * argv[])673 sndio_play (int argc, char *argv [])
674 { struct sio_hdl *hdl ;
675 struct sio_par par ;
676 short buffer [BUFFER_LEN] ;
677 SNDFILE *sndfile ;
678 SF_INFO sfinfo ;
679 int k, readcount ;
680
681 for (k = 1 ; k < argc ; k++)
682 { printf ("Playing %s\n", argv [k]) ;
683 if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo)))
684 { puts (sf_strerror (NULL)) ;
685 continue ;
686 } ;
687
688 if (sfinfo.channels < 1 || sfinfo.channels > 2)
689 { printf ("Error : channels = %d.\n", sfinfo.channels) ;
690 continue ;
691 } ;
692
693 if ((hdl = sio_open (NULL, SIO_PLAY, 0)) == NULL)
694 { fprintf (stderr, "open sndio device failed") ;
695 return ;
696 } ;
697
698 sio_initpar (&par) ;
699 par.rate = sfinfo.samplerate ;
700 par.pchan = sfinfo.channels ;
701 par.bits = 16 ;
702 par.sig = 1 ;
703 par.le = SIO_LE_NATIVE ;
704
705 if (! sio_setpar (hdl, &par) || ! sio_getpar (hdl, &par))
706 { fprintf (stderr, "set sndio params failed") ;
707 return ;
708 } ;
709
710 if (! sio_start (hdl))
711 { fprintf (stderr, "sndio start failed") ;
712 return ;
713 } ;
714
715 while ((readcount = sf_read_short (sndfile, buffer, BUFFER_LEN)))
716 sio_write (hdl, buffer, readcount * sizeof (short)) ;
717
718 sio_close (hdl) ;
719 } ;
720
721 return ;
722 } /* sndio_play */
723
724 #endif /* sndio */
725
726 /*------------------------------------------------------------------------------
727 ** Solaris.
728 */
729
730 #if (defined (sun) && defined (unix)) /* ie Solaris */
731
732 static void
solaris_play(int argc,char * argv[])733 solaris_play (int argc, char *argv [])
734 { static short buffer [BUFFER_LEN] ;
735 audio_info_t audio_info ;
736 SNDFILE *sndfile ;
737 SF_INFO sfinfo ;
738 unsigned long delay_time ;
739 long k, start_count, output_count, write_count, read_count ;
740 int audio_fd, error, done ;
741
742 for (k = 1 ; k < argc ; k++)
743 { printf ("Playing %s\n", argv [k]) ;
744 if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo)))
745 { puts (sf_strerror (NULL)) ;
746 continue ;
747 } ;
748
749 if (sfinfo.channels < 1 || sfinfo.channels > 2)
750 { printf ("Error : channels = %d.\n", sfinfo.channels) ;
751 continue ;
752 } ;
753
754 /* open the audio device - write only, non-blocking */
755 if ((audio_fd = open ("/dev/audio", O_WRONLY | O_NONBLOCK)) < 0)
756 { perror ("open (/dev/audio) failed") ;
757 return ;
758 } ;
759
760 /* Retrive standard values. */
761 AUDIO_INITINFO (&audio_info) ;
762
763 audio_info.play.sample_rate = sfinfo.samplerate ;
764 audio_info.play.channels = sfinfo.channels ;
765 audio_info.play.precision = 16 ;
766 audio_info.play.encoding = AUDIO_ENCODING_LINEAR ;
767 audio_info.play.gain = AUDIO_MAX_GAIN ;
768 audio_info.play.balance = AUDIO_MID_BALANCE ;
769
770 if ((error = ioctl (audio_fd, AUDIO_SETINFO, &audio_info)))
771 { perror ("ioctl (AUDIO_SETINFO) failed") ;
772 return ;
773 } ;
774
775 /* Delay time equal to 1/4 of a buffer in microseconds. */
776 delay_time = (BUFFER_LEN * 1000000) / (audio_info.play.sample_rate * 4) ;
777
778 done = 0 ;
779 while (! done)
780 { read_count = sf_read_short (sndfile, buffer, BUFFER_LEN) ;
781 if (read_count < BUFFER_LEN)
782 { memset (&(buffer [read_count]), 0, (BUFFER_LEN - read_count) * sizeof (short)) ;
783 /* Tell the main application to terminate. */
784 done = SF_TRUE ;
785 } ;
786
787 start_count = 0 ;
788 output_count = BUFFER_LEN * sizeof (short) ;
789
790 while (output_count > 0)
791 { /* write as much data as possible */
792 write_count = write (audio_fd, &(buffer [start_count]), output_count) ;
793 if (write_count > 0)
794 { output_count -= write_count ;
795 start_count += write_count ;
796 }
797 else
798 { /* Give the audio output time to catch up. */
799 usleep (delay_time) ;
800 } ;
801 } ; /* while (outpur_count > 0) */
802 } ; /* while (! done) */
803
804 close (audio_fd) ;
805 } ;
806
807 return ;
808 } /* solaris_play */
809
810 #endif /* Solaris */
811
812 /*==============================================================================
813 ** Main function.
814 */
815
816 int
main(int argc,char * argv[])817 main (int argc, char *argv [])
818 {
819 if (argc < 2)
820 {
821 printf ("\nUsage : %s <input sound file>\n\n", program_name (argv [0])) ;
822 printf ("Using %s.\n\n", sf_version_string ()) ;
823 #if (OS_IS_WIN32 == 1)
824 printf ("This is a Unix style command line application which\n"
825 "should be run in a MSDOS box or Command Shell window.\n\n") ;
826 printf ("Sleeping for 5 seconds before exiting.\n\n") ;
827
828 Sleep (5 * 1000) ;
829 #endif
830 return 1 ;
831 } ;
832
833 #if defined (__ANDROID__)
834 puts ("*** Playing sound not yet supported on Android.") ;
835 puts ("*** Please feel free to submit a patch.") ;
836 return 1 ;
837 #elif defined (__linux__)
838 #if HAVE_ALSA_ASOUNDLIB_H
839 if (access ("/proc/asound/cards", R_OK) == 0)
840 alsa_play (argc, argv) ;
841 else
842 #endif
843 opensoundsys_play (argc, argv) ;
844 #elif defined (__FreeBSD_kernel__) || defined (__FreeBSD__)
845 opensoundsys_play (argc, argv) ;
846 #elif HAVE_SNDIO_H
847 sndio_play (argc, argv) ;
848 #elif (defined (sun) && defined (unix))
849 solaris_play (argc, argv) ;
850 #elif (OS_IS_WIN32 == 1)
851 win32_play (argc, argv) ;
852 #else
853 puts ("*** Playing sound not supported on this platform.") ;
854 puts ("*** Please feel free to submit a patch.") ;
855 return 1 ;
856 #endif
857
858 return 0 ;
859 } /* main */
860
861