libsndfile source files.
[Faustine.git] / interpretor / libsndfile-1.0.25 / programs / sndfile-play.c
1 /*
2 ** Copyright (C) 1999-2011 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 #endif
43
44 #include <sndfile.h>
45
46 #include "common.h"
47
48 #if HAVE_ALSA_ASOUNDLIB_H
49 #define ALSA_PCM_NEW_HW_PARAMS_API
50 #define ALSA_PCM_NEW_SW_PARAMS_API
51 #include <alsa/asoundlib.h>
52 #include <sys/time.h>
53 #endif
54
55 #if defined (__linux__) || defined (__FreeBSD_kernel__) || defined (__FreeBSD__)
56 #include <fcntl.h>
57 #include <sys/ioctl.h>
58 #include <sys/soundcard.h>
59
60 #elif (defined (__MACH__) && defined (__APPLE__))
61 #include <Carbon.h>
62 #include <CoreAudio/AudioHardware.h>
63
64 #elif defined (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
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 < 1e-10)
123 scale = 1.0 ;
124 else
125 scale = 32700.0 / scale ;
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 *
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
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 (__linux__) || defined (__FreeBSD_kernel__) || defined (__FreeBSD__)
357
358 static int opensoundsys_open_device (int channels, int srate) ;
359
360 static int
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
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 #if (defined (__MACH__) && defined (__APPLE__)) /* MacOSX */
474
475 typedef struct
476 { AudioStreamBasicDescription format ;
477
478 UInt32 buf_size ;
479 AudioDeviceID device ;
480
481 SNDFILE *sndfile ;
482 SF_INFO sfinfo ;
483
484 int fake_stereo ;
485 int done_playing ;
486 } MacOSXAudioData ;
487
488 #include <math.h>
489
490 static OSStatus
491 macosx_audio_out_callback (AudioDeviceID device, const AudioTimeStamp* current_time,
492 const AudioBufferList* data_in, const AudioTimeStamp* time_in,
493 AudioBufferList* data_out, const AudioTimeStamp* time_out,
494 void* client_data)
495 { MacOSXAudioData *audio_data ;
496 int size, sample_count, read_count, k ;
497 float *buffer ;
498
499 /* Prevent compiler warnings. */
500 device = device ;
501 current_time = current_time ;
502 data_in = data_in ;
503 time_in = time_in ;
504 time_out = time_out ;
505
506 audio_data = (MacOSXAudioData*) client_data ;
507
508 size = data_out->mBuffers [0].mDataByteSize ;
509 sample_count = size / sizeof (float) ;
510
511 buffer = (float*) data_out->mBuffers [0].mData ;
512
513 if (audio_data->fake_stereo != 0)
514 { read_count = sf_read_float (audio_data->sndfile, buffer, sample_count / 2) ;
515
516 for (k = read_count - 1 ; k >= 0 ; k--)
517 { buffer [2 * k ] = buffer [k] ;
518 buffer [2 * k + 1] = buffer [k] ;
519 } ;
520 read_count *= 2 ;
521 }
522 else
523 read_count = sf_read_float (audio_data->sndfile, buffer, sample_count) ;
524
525 /* Fill the remainder with zeroes. */
526 if (read_count < sample_count)
527 { if (audio_data->fake_stereo == 0)
528 memset (&(buffer [read_count]), 0, (sample_count - read_count) * sizeof (float)) ;
529 /* Tell the main application to terminate. */
530 audio_data->done_playing = SF_TRUE ;
531 } ;
532
533 return noErr ;
534 } /* macosx_audio_out_callback */
535
536 static void
537 macosx_play (int argc, char *argv [])
538 { MacOSXAudioData audio_data ;
539 OSStatus err ;
540 UInt32 count, buffer_size ;
541 int k ;
542
543 audio_data.fake_stereo = 0 ;
544 audio_data.device = kAudioDeviceUnknown ;
545
546 /* get the default output device for the HAL */
547 count = sizeof (AudioDeviceID) ;
548 if ((err = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultOutputDevice,
549 &count, (void *) &(audio_data.device))) != noErr)
550 { printf ("AudioHardwareGetProperty (kAudioDevicePropertyDefaultOutputDevice) failed.\n") ;
551 return ;
552 } ;
553
554 /* get the buffersize that the default device uses for IO */
555 count = sizeof (UInt32) ;
556 if ((err = AudioDeviceGetProperty (audio_data.device, 0, false, kAudioDevicePropertyBufferSize,
557 &count, &buffer_size)) != noErr)
558 { printf ("AudioDeviceGetProperty (kAudioDevicePropertyBufferSize) failed.\n") ;
559 return ;
560 } ;
561
562 /* get a description of the data format used by the default device */
563 count = sizeof (AudioStreamBasicDescription) ;
564 if ((err = AudioDeviceGetProperty (audio_data.device, 0, false, kAudioDevicePropertyStreamFormat,
565 &count, &(audio_data.format))) != noErr)
566 { printf ("AudioDeviceGetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ;
567 return ;
568 } ;
569
570 /* Base setup completed. Now play files. */
571 for (k = 1 ; k < argc ; k++)
572 { printf ("Playing %s\n", argv [k]) ;
573 if (! (audio_data.sndfile = sf_open (argv [k], SFM_READ, &(audio_data.sfinfo))))
574 { puts (sf_strerror (NULL)) ;
575 continue ;
576 } ;
577
578 if (audio_data.sfinfo.channels < 1 || audio_data.sfinfo.channels > 2)
579 { printf ("Error : channels = %d.\n", audio_data.sfinfo.channels) ;
580 continue ;
581 } ;
582
583 audio_data.format.mSampleRate = audio_data.sfinfo.samplerate ;
584
585 if (audio_data.sfinfo.channels == 1)
586 { audio_data.format.mChannelsPerFrame = 2 ;
587 audio_data.fake_stereo = 1 ;
588 }
589 else
590 audio_data.format.mChannelsPerFrame = audio_data.sfinfo.channels ;
591
592 if ((err = AudioDeviceSetProperty (audio_data.device, NULL, 0, false, kAudioDevicePropertyStreamFormat,
593 sizeof (AudioStreamBasicDescription), &(audio_data.format))) != noErr)
594 { printf ("AudioDeviceSetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ;
595 return ;
596 } ;
597
598 /* we want linear pcm */
599 if (audio_data.format.mFormatID != kAudioFormatLinearPCM)
600 return ;
601
602 /* Fire off the device. */
603 if ((err = AudioDeviceAddIOProc (audio_data.device, macosx_audio_out_callback,
604 (void *) &audio_data)) != noErr)
605 { printf ("AudioDeviceAddIOProc failed.\n") ;
606 return ;
607 } ;
608
609 err = AudioDeviceStart (audio_data.device, macosx_audio_out_callback) ;
610 if (err != noErr)
611 return ;
612
613 audio_data.done_playing = SF_FALSE ;
614
615 while (audio_data.done_playing == SF_FALSE)
616 usleep (10 * 1000) ; /* 10 000 milliseconds. */
617
618 if ((err = AudioDeviceStop (audio_data.device, macosx_audio_out_callback)) != noErr)
619 { printf ("AudioDeviceStop failed.\n") ;
620 return ;
621 } ;
622
623 err = AudioDeviceRemoveIOProc (audio_data.device, macosx_audio_out_callback) ;
624 if (err != noErr)
625 { printf ("AudioDeviceRemoveIOProc failed.\n") ;
626 return ;
627 } ;
628
629 sf_close (audio_data.sndfile) ;
630 } ;
631
632 return ;
633 } /* macosx_play */
634
635 #endif /* MacOSX */
636
637
638 /*------------------------------------------------------------------------------
639 ** Win32 functions for playing a sound.
640 **
641 ** This API sucks. Its needlessly complicated and is *WAY* too loose with
642 ** passing pointers arounf in integers and and using char* pointers to
643 ** point to data instead of short*. It plain sucks!
644 */
645
646 #if (OS_IS_WIN32 == 1)
647
648 #define WIN32_BUFFER_LEN (1<<15)
649
650 typedef struct
651 { HWAVEOUT hwave ;
652 WAVEHDR whdr [2] ;
653
654 CRITICAL_SECTION mutex ; /* to control access to BuffersInUSe */
655 HANDLE Event ; /* signal that a buffer is free */
656
657 short buffer [WIN32_BUFFER_LEN / sizeof (short)] ;
658 int current, bufferlen ;
659 int BuffersInUse ;
660
661 SNDFILE *sndfile ;
662 SF_INFO sfinfo ;
663
664 sf_count_t remaining ;
665 } Win32_Audio_Data ;
666
667
668 static void
669 win32_play_data (Win32_Audio_Data *audio_data)
670 { int thisread, readcount ;
671
672 /* fill a buffer if there is more data and we can read it sucessfully */
673 readcount = (audio_data->remaining > audio_data->bufferlen) ? audio_data->bufferlen : (int) audio_data->remaining ;
674
675 thisread = (int) sf_read_short (audio_data->sndfile, (short *) (audio_data->whdr [audio_data->current].lpData), readcount) ;
676
677 audio_data->remaining -= thisread ;
678
679 if (thisread > 0)
680 { /* Fix buffer length if this is only a partial block. */
681 if (thisread < audio_data->bufferlen)
682 audio_data->whdr [audio_data->current].dwBufferLength = thisread * sizeof (short) ;
683
684 /* Queue the WAVEHDR */
685 waveOutWrite (audio_data->hwave, (LPWAVEHDR) &(audio_data->whdr [audio_data->current]), sizeof (WAVEHDR)) ;
686
687 /* count another buffer in use */
688 EnterCriticalSection (&audio_data->mutex) ;
689 audio_data->BuffersInUse ++ ;
690 LeaveCriticalSection (&audio_data->mutex) ;
691
692 /* use the other buffer next time */
693 audio_data->current = (audio_data->current + 1) % 2 ;
694 } ;
695
696 return ;
697 } /* win32_play_data */
698
699 static void CALLBACK
700 win32_audio_out_callback (HWAVEOUT hwave, UINT msg, DWORD_PTR data, DWORD param1, DWORD param2)
701 { Win32_Audio_Data *audio_data ;
702
703 /* Prevent compiler warnings. */
704 hwave = hwave ;
705 param1 = param2 ;
706
707 if (data == 0)
708 return ;
709
710 /*
711 ** I consider this technique of passing a pointer via an integer as
712 ** fundamentally broken but thats the way microsoft has defined the
713 ** interface.
714 */
715 audio_data = (Win32_Audio_Data*) data ;
716
717 /* let main loop know a buffer is free */
718 if (msg == MM_WOM_DONE)
719 { EnterCriticalSection (&audio_data->mutex) ;
720 audio_data->BuffersInUse -- ;
721 LeaveCriticalSection (&audio_data->mutex) ;
722 SetEvent (audio_data->Event) ;
723 } ;
724
725 return ;
726 } /* win32_audio_out_callback */
727
728 static void
729 win32_play (int argc, char *argv [])
730 { Win32_Audio_Data audio_data ;
731
732 WAVEFORMATEX wf ;
733 int k, error ;
734
735 audio_data.sndfile = NULL ;
736 audio_data.hwave = 0 ;
737
738 for (k = 1 ; k < argc ; k++)
739 { printf ("Playing %s\n", argv [k]) ;
740
741 if (! (audio_data.sndfile = sf_open (argv [k], SFM_READ, &(audio_data.sfinfo))))
742 { puts (sf_strerror (NULL)) ;
743 continue ;
744 } ;
745
746 audio_data.remaining = audio_data.sfinfo.frames * audio_data.sfinfo.channels ;
747 audio_data.current = 0 ;
748
749 InitializeCriticalSection (&audio_data.mutex) ;
750 audio_data.Event = CreateEvent (0, FALSE, FALSE, 0) ;
751
752 wf.nChannels = audio_data.sfinfo.channels ;
753 wf.wFormatTag = WAVE_FORMAT_PCM ;
754 wf.cbSize = 0 ;
755 wf.wBitsPerSample = 16 ;
756
757 wf.nSamplesPerSec = audio_data.sfinfo.samplerate ;
758
759 wf.nBlockAlign = audio_data.sfinfo.channels * sizeof (short) ;
760
761 wf.nAvgBytesPerSec = wf.nBlockAlign * wf.nSamplesPerSec ;
762
763 error = waveOutOpen (&(audio_data.hwave), WAVE_MAPPER, &wf, (DWORD_PTR) win32_audio_out_callback,
764 (DWORD_PTR) &audio_data, CALLBACK_FUNCTION) ;
765 if (error)
766 { puts ("waveOutOpen failed.") ;
767 audio_data.hwave = 0 ;
768 continue ;
769 } ;
770
771 audio_data.whdr [0].lpData = (char*) audio_data.buffer ;
772 audio_data.whdr [1].lpData = ((char*) audio_data.buffer) + sizeof (audio_data.buffer) / 2 ;
773
774 audio_data.whdr [0].dwBufferLength = sizeof (audio_data.buffer) / 2 ;
775 audio_data.whdr [1].dwBufferLength = sizeof (audio_data.buffer) / 2 ;
776
777 audio_data.whdr [0].dwFlags = 0 ;
778 audio_data.whdr [1].dwFlags = 0 ;
779
780 /* length of each audio buffer in samples */
781 audio_data.bufferlen = sizeof (audio_data.buffer) / 2 / sizeof (short) ;
782
783 /* Prepare the WAVEHDRs */
784 if ((error = waveOutPrepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR))))
785 { printf ("waveOutPrepareHeader [0] failed : %08X\n", error) ;
786 waveOutClose (audio_data.hwave) ;
787 continue ;
788 } ;
789
790 if ((error = waveOutPrepareHeader (audio_data.hwave, &(audio_data.whdr [1]), sizeof (WAVEHDR))))
791 { printf ("waveOutPrepareHeader [1] failed : %08X\n", error) ;
792 waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR)) ;
793 waveOutClose (audio_data.hwave) ;
794 continue ;
795 } ;
796
797 /* Fill up both buffers with audio data */
798 audio_data.BuffersInUse = 0 ;
799 win32_play_data (&audio_data) ;
800 win32_play_data (&audio_data) ;
801
802 /* loop until both buffers are released */
803 while (audio_data.BuffersInUse > 0)
804 {
805 /* wait for buffer to be released */
806 WaitForSingleObject (audio_data.Event, INFINITE) ;
807
808 /* refill the buffer if there is more data to play */
809 win32_play_data (&audio_data) ;
810 } ;
811
812 waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR)) ;
813 waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [1]), sizeof (WAVEHDR)) ;
814
815 waveOutClose (audio_data.hwave) ;
816 audio_data.hwave = 0 ;
817
818 DeleteCriticalSection (&audio_data.mutex) ;
819
820 sf_close (audio_data.sndfile) ;
821 } ;
822
823 } /* win32_play */
824
825 #endif /* Win32 */
826
827 /*------------------------------------------------------------------------------
828 ** OpenBDS's sndio.
829 */
830
831 #if defined (HAVE_SNDIO_H)
832
833 static void
834 sndio_play (int argc, char *argv [])
835 { struct sio_hdl *hdl ;
836 struct sio_par par ;
837 short buffer [BUFFER_LEN] ;
838 SNDFILE *sndfile ;
839 SF_INFO sfinfo ;
840 int k, readcount ;
841
842 for (k = 1 ; k < argc ; k++)
843 { printf ("Playing %s\n", argv [k]) ;
844 if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo)))
845 { puts (sf_strerror (NULL)) ;
846 continue ;
847 } ;
848
849 if (sfinfo.channels < 1 || sfinfo.channels > 2)
850 { printf ("Error : channels = %d.\n", sfinfo.channels) ;
851 continue ;
852 } ;
853
854 if ((hdl = sio_open (NULL, SIO_PLAY, 0)) == NULL)
855 { fprintf (stderr, "open sndio device failed") ;
856 return ;
857 } ;
858
859 sio_initpar (&par) ;
860 par.rate = sfinfo.samplerate ;
861 par.pchan = sfinfo.channels ;
862 par.bits = 16 ;
863 par.sig = 1 ;
864 par.le = SIO_LE_NATIVE ;
865
866 if (! sio_setpar (hdl, &par) || ! sio_getpar (hdl, &par))
867 { fprintf (stderr, "set sndio params failed") ;
868 return ;
869 } ;
870
871 if (! sio_start (hdl))
872 { fprintf (stderr, "sndio start failed") ;
873 return ;
874 } ;
875
876 while ((readcount = sf_read_short (sndfile, buffer, BUFFER_LEN)))
877 sio_write (hdl, buffer, readcount * sizeof (short)) ;
878
879 sio_close (hdl) ;
880 } ;
881
882 return ;
883 } /* sndio_play */
884
885 #endif /* sndio */
886
887 /*------------------------------------------------------------------------------
888 ** Solaris.
889 */
890
891 #if (defined (sun) && defined (unix)) /* ie Solaris */
892
893 static void
894 solaris_play (int argc, char *argv [])
895 { static short buffer [BUFFER_LEN] ;
896 audio_info_t audio_info ;
897 SNDFILE *sndfile ;
898 SF_INFO sfinfo ;
899 unsigned long delay_time ;
900 long k, start_count, output_count, write_count, read_count ;
901 int audio_fd, error, done ;
902
903 for (k = 1 ; k < argc ; k++)
904 { printf ("Playing %s\n", argv [k]) ;
905 if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo)))
906 { puts (sf_strerror (NULL)) ;
907 continue ;
908 } ;
909
910 if (sfinfo.channels < 1 || sfinfo.channels > 2)
911 { printf ("Error : channels = %d.\n", sfinfo.channels) ;
912 continue ;
913 } ;
914
915 /* open the audio device - write only, non-blocking */
916 if ((audio_fd = open ("/dev/audio", O_WRONLY | O_NONBLOCK)) < 0)
917 { perror ("open (/dev/audio) failed") ;
918 return ;
919 } ;
920
921 /* Retrive standard values. */
922 AUDIO_INITINFO (&audio_info) ;
923
924 audio_info.play.sample_rate = sfinfo.samplerate ;
925 audio_info.play.channels = sfinfo.channels ;
926 audio_info.play.precision = 16 ;
927 audio_info.play.encoding = AUDIO_ENCODING_LINEAR ;
928 audio_info.play.gain = AUDIO_MAX_GAIN ;
929 audio_info.play.balance = AUDIO_MID_BALANCE ;
930
931 if ((error = ioctl (audio_fd, AUDIO_SETINFO, &audio_info)))
932 { perror ("ioctl (AUDIO_SETINFO) failed") ;
933 return ;
934 } ;
935
936 /* Delay time equal to 1/4 of a buffer in microseconds. */
937 delay_time = (BUFFER_LEN * 1000000) / (audio_info.play.sample_rate * 4) ;
938
939 done = 0 ;
940 while (! done)
941 { read_count = sf_read_short (sndfile, buffer, BUFFER_LEN) ;
942 if (read_count < BUFFER_LEN)
943 { memset (&(buffer [read_count]), 0, (BUFFER_LEN - read_count) * sizeof (short)) ;
944 /* Tell the main application to terminate. */
945 done = SF_TRUE ;
946 } ;
947
948 start_count = 0 ;
949 output_count = BUFFER_LEN * sizeof (short) ;
950
951 while (output_count > 0)
952 { /* write as much data as possible */
953 write_count = write (audio_fd, &(buffer [start_count]), output_count) ;
954 if (write_count > 0)
955 { output_count -= write_count ;
956 start_count += write_count ;
957 }
958 else
959 { /* Give the audio output time to catch up. */
960 usleep (delay_time) ;
961 } ;
962 } ; /* while (outpur_count > 0) */
963 } ; /* while (! done) */
964
965 close (audio_fd) ;
966 } ;
967
968 return ;
969 } /* solaris_play */
970
971 #endif /* Solaris */
972
973 /*==============================================================================
974 ** Main function.
975 */
976
977 int
978 main (int argc, char *argv [])
979 {
980 if (argc < 2)
981 {
982 printf ("\nUsage : %s <input sound file>\n\n", program_name (argv [0])) ;
983 printf (" Using %s.\n\n", sf_version_string ()) ;
984 #if (OS_IS_WIN32 == 1)
985 printf ("This is a Unix style command line application which\n"
986 "should be run in a MSDOS box or Command Shell window.\n\n") ;
987 printf ("Sleeping for 5 seconds before exiting.\n\n") ;
988
989 Sleep (5 * 1000) ;
990 #endif
991 return 1 ;
992 } ;
993
994 #if defined (__linux__)
995 #if HAVE_ALSA_ASOUNDLIB_H
996 if (access ("/proc/asound/cards", R_OK) == 0)
997 alsa_play (argc, argv) ;
998 else
999 #endif
1000 opensoundsys_play (argc, argv) ;
1001 #elif defined (__FreeBSD_kernel__) || defined (__FreeBSD__)
1002 opensoundsys_play (argc, argv) ;
1003 #elif (defined (__MACH__) && defined (__APPLE__))
1004 macosx_play (argc, argv) ;
1005 #elif defined HAVE_SNDIO_H
1006 sndio_play (argc, argv) ;
1007 #elif (defined (sun) && defined (unix))
1008 solaris_play (argc, argv) ;
1009 #elif (OS_IS_WIN32 == 1)
1010 win32_play (argc, argv) ;
1011 #elif defined (__BEOS__)
1012 printf ("This program cannot be compiled on BeOS.\n") ;
1013 printf ("Instead, compile the file sfplay_beos.cpp.\n") ;
1014 return 1 ;
1015 #else
1016 puts ("*** Playing sound not yet supported on this platform.") ;
1017 puts ("*** Please feel free to submit a patch.") ;
1018 return 1 ;
1019 #endif
1020
1021 return 0 ;
1022 } /* main */
1023