2
** Copyright (C) 1999-2005 Erik de Castro Lopo <erikd@mega-nerd.com>
4
** This program is free software; you can redistribute it and/or modify
5
** it under the terms of the GNU General Public License as published by
6
** the Free Software Foundation; either version 2 of the License, or
7
** (at your option) any later version.
9
** This program is distributed in the hope that it will be useful,
10
** but WITHOUT ANY WARRANTY; without even the implied warranty of
11
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
** GNU General Public License for more details.
14
** You should have received a copy of the GNU General Public License
15
** along with this program; if not, write to the Free Software
16
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30
#if HAVE_ALSA_ASOUNDLIB_H
31
#define ALSA_PCM_NEW_HW_PARAMS_API
32
#define ALSA_PCM_NEW_SW_PARAMS_API
33
#include <alsa/asoundlib.h>
37
#if defined (__linux__)
39
#include <sys/ioctl.h>
40
#include <sys/soundcard.h>
42
#elif (defined (__MACH__) && defined (__APPLE__))
44
#include <CoreAudio/AudioHardware.h>
46
#elif (defined (sun) && defined (unix))
48
#include <sys/ioctl.h>
49
#include <sys/audioio.h>
51
#elif (OS_IS_WIN32 == 1)
59
#define SIGNED_SIZEOF(x) ((int) sizeof (x))
60
#define BUFFER_LEN (2048)
62
/*------------------------------------------------------------------------------
63
** Linux/OSS functions for playing a sound.
66
#if HAVE_ALSA_ASOUNDLIB_H
68
static snd_pcm_t * alsa_open (int channels, unsigned srate, int realtime) ;
69
static int alsa_write_float (snd_pcm_t *alsa_dev, float *data, int frames, int channels) ;
72
alsa_play (int argc, char *argv [])
73
{ static float buffer [BUFFER_LEN] ;
76
snd_pcm_t * alsa_dev ;
77
int k, readcount, subformat ;
79
for (k = 1 ; k < argc ; k++)
80
{ memset (&sfinfo, 0, sizeof (sfinfo)) ;
82
printf ("Playing %s\n", argv [k]) ;
83
if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo)))
84
{ puts (sf_strerror (NULL)) ;
88
if (sfinfo.channels < 1 || sfinfo.channels > 2)
89
{ printf ("Error : channels = %d.\n", sfinfo.channels) ;
93
if ((alsa_dev = alsa_open (sfinfo.channels, (unsigned) sfinfo.samplerate, SF_FALSE)) == NULL)
96
subformat = sfinfo.format & SF_FORMAT_SUBMASK ;
98
if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
102
sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
106
scale = 32700.0 / scale ;
108
while ((readcount = sf_read_float (sndfile, buffer, BUFFER_LEN)))
109
{ for (m = 0 ; m < readcount ; m++)
110
buffer [m] *= scale ;
111
alsa_write_float (alsa_dev, buffer, BUFFER_LEN / sfinfo.channels, sfinfo.channels) ;
115
{ while ((readcount = sf_read_float (sndfile, buffer, BUFFER_LEN)))
116
alsa_write_float (alsa_dev, buffer, BUFFER_LEN / sfinfo.channels, sfinfo.channels) ;
119
snd_pcm_drain (alsa_dev) ;
120
snd_pcm_close (alsa_dev) ;
129
alsa_open (int channels, unsigned samplerate, int realtime)
130
{ const char * device = "plughw:0" ;
131
snd_pcm_t *alsa_dev = NULL ;
132
snd_pcm_hw_params_t *hw_params ;
133
snd_pcm_uframes_t buffer_size, xfer_align, start_threshold ;
134
snd_pcm_uframes_t alsa_period_size, alsa_buffer_frames ;
135
snd_pcm_sw_params_t *sw_params ;
140
{ alsa_period_size = 256 ;
141
alsa_buffer_frames = 3 * alsa_period_size ;
144
{ alsa_period_size = 1024 ;
145
alsa_buffer_frames = 4 * alsa_period_size ;
148
if ((err = snd_pcm_open (&alsa_dev, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
149
{ fprintf (stderr, "cannot open audio device \"%s\" (%s)\n", device, snd_strerror (err)) ;
153
snd_pcm_nonblock (alsa_dev, 0) ;
155
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
156
{ fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err)) ;
160
if ((err = snd_pcm_hw_params_any (alsa_dev, hw_params)) < 0)
161
{ fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err)) ;
165
if ((err = snd_pcm_hw_params_set_access (alsa_dev, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
166
{ fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err)) ;
170
if ((err = snd_pcm_hw_params_set_format (alsa_dev, hw_params, SND_PCM_FORMAT_FLOAT)) < 0)
171
{ fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err)) ;
175
if ((err = snd_pcm_hw_params_set_rate_near (alsa_dev, hw_params, &samplerate, 0)) < 0)
176
{ fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err)) ;
180
if ((err = snd_pcm_hw_params_set_channels (alsa_dev, hw_params, channels)) < 0)
181
{ fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err)) ;
185
if ((err = snd_pcm_hw_params_set_buffer_size_near (alsa_dev, hw_params, &alsa_buffer_frames)) < 0)
186
{ fprintf (stderr, "cannot set buffer size (%s)\n", snd_strerror (err)) ;
190
if ((err = snd_pcm_hw_params_set_period_size_near (alsa_dev, hw_params, &alsa_period_size, 0)) < 0)
191
{ fprintf (stderr, "cannot set period size (%s)\n", snd_strerror (err)) ;
195
if ((err = snd_pcm_hw_params (alsa_dev, hw_params)) < 0)
196
{ fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err)) ;
200
/* extra check: if we have only one period, this code won't work */
201
snd_pcm_hw_params_get_period_size (hw_params, &alsa_period_size, 0) ;
202
snd_pcm_hw_params_get_buffer_size (hw_params, &buffer_size) ;
203
if (alsa_period_size == buffer_size)
204
{ fprintf (stderr, "Can't use period equal to buffer size (%lu == %lu)", alsa_period_size, buffer_size) ;
208
snd_pcm_hw_params_free (hw_params) ;
210
if ((err = snd_pcm_sw_params_malloc (&sw_params)) != 0)
211
{ fprintf (stderr, "%s: snd_pcm_sw_params_malloc: %s", __func__, snd_strerror (err)) ;
215
if ((err = snd_pcm_sw_params_current (alsa_dev, sw_params)) != 0)
216
{ fprintf (stderr, "%s: snd_pcm_sw_params_current: %s", __func__, snd_strerror (err)) ;
220
/* note: set start threshold to delay start until the ring buffer is full */
221
snd_pcm_sw_params_current (alsa_dev, sw_params) ;
222
if ((err = snd_pcm_sw_params_get_xfer_align (sw_params, &xfer_align)) < 0)
223
{ fprintf (stderr, "cannot get xfer align (%s)\n", snd_strerror (err)) ;
227
/* round up to closest transfer boundary */
228
start_threshold = (buffer_size / xfer_align) * xfer_align ;
229
if (start_threshold < 1)
230
start_threshold = 1 ;
231
if ((err = snd_pcm_sw_params_set_start_threshold (alsa_dev, sw_params, start_threshold)) < 0)
232
{ fprintf (stderr, "cannot set start threshold (%s)\n", snd_strerror (err)) ;
236
if ((err = snd_pcm_sw_params (alsa_dev, sw_params)) != 0)
237
{ fprintf (stderr, "%s: snd_pcm_sw_params: %s", __func__, snd_strerror (err)) ;
241
snd_pcm_sw_params_free (sw_params) ;
243
snd_pcm_reset (alsa_dev) ;
247
if (err < 0 && alsa_dev != NULL)
248
{ snd_pcm_close (alsa_dev) ;
256
alsa_write_float (snd_pcm_t *alsa_dev, float *data, int frames, int channels)
257
{ static int epipe_count = 0 ;
259
snd_pcm_status_t *status ;
266
while (total < frames)
267
{ retval = snd_pcm_writei (alsa_dev, data + total * channels, frames - total) ;
279
puts ("alsa_write_float: EAGAIN") ;
285
{ printf ("alsa_write_float: EPIPE %d\n", epipe_count) ;
286
if (epipe_count > 140)
292
{ snd_pcm_status_alloca (&status) ;
293
if ((retval = snd_pcm_status (alsa_dev, status)) < 0)
294
fprintf (stderr, "alsa_out: xrun. can't determine length\n") ;
295
else if (snd_pcm_status_get_state (status) == SND_PCM_STATE_XRUN)
296
{ struct timeval now, diff, tstamp ;
298
gettimeofday (&now, 0) ;
299
snd_pcm_status_get_trigger_tstamp (status, &tstamp) ;
300
timersub (&now, &tstamp, &diff) ;
302
fprintf (stderr, "alsa_write_float xrun: of at least %.3f msecs. resetting stream\n",
303
diff.tv_sec * 1000 + diff.tv_usec / 1000.0) ;
306
fprintf (stderr, "alsa_write_float: xrun. can't determine length\n") ;
309
snd_pcm_prepare (alsa_dev) ;
313
fprintf (stderr, "alsa_write_float: Bad PCM state.n") ;
318
fprintf (stderr, "alsa_write_float: Suspend event.n") ;
323
puts ("alsa_write_float: EIO") ;
327
fprintf (stderr, "alsa_write_float: retval = %d\n", retval) ;
334
} /* alsa_write_float */
336
#endif /* HAVE_ALSA_ASOUNDLIB_H */
338
/*------------------------------------------------------------------------------
339
** Linux/OSS functions for playing a sound.
342
#if defined (__linux__)
344
static int linux_open_dsp_device (int channels, int srate) ;
347
linux_play (int argc, char *argv [])
348
{ static short buffer [BUFFER_LEN] ;
351
int k, audio_device, readcount, subformat ;
353
for (k = 1 ; k < argc ; k++)
354
{ memset (&sfinfo, 0, sizeof (sfinfo)) ;
356
printf ("Playing %s\n", argv [k]) ;
357
if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo)))
358
{ puts (sf_strerror (NULL)) ;
362
if (sfinfo.channels < 1 || sfinfo.channels > 2)
363
{ printf ("Error : channels = %d.\n", sfinfo.channels) ;
367
audio_device = linux_open_dsp_device (sfinfo.channels, sfinfo.samplerate) ;
369
subformat = sfinfo.format & SF_FORMAT_SUBMASK ;
371
if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
372
{ static float float_buffer [BUFFER_LEN] ;
376
sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
380
scale = 32700.0 / scale ;
382
while ((readcount = sf_read_float (sndfile, float_buffer, BUFFER_LEN)))
383
{ for (m = 0 ; m < readcount ; m++)
384
buffer [m] = scale * float_buffer [m] ;
385
write (audio_device, buffer, readcount * sizeof (short)) ;
389
{ while ((readcount = sf_read_short (sndfile, buffer, BUFFER_LEN)))
390
write (audio_device, buffer, readcount * sizeof (short)) ;
393
if (ioctl (audio_device, SNDCTL_DSP_POST, 0) == -1)
394
perror ("ioctl (SNDCTL_DSP_POST) ") ;
396
if (ioctl (audio_device, SNDCTL_DSP_SYNC, 0) == -1)
397
perror ("ioctl (SNDCTL_DSP_SYNC) ") ;
399
close (audio_device) ;
408
linux_open_dsp_device (int channels, int srate)
409
{ int fd, stereo, temp, error ;
411
if ((fd = open ("/dev/dsp", O_WRONLY, 0)) == -1 &&
412
(fd = open ("/dev/sound/dsp", O_WRONLY, 0)) == -1)
413
{ perror ("linux_open_dsp_device : open ") ;
418
if (ioctl (fd, SNDCTL_DSP_STEREO, &stereo) == -1)
420
perror ("linux_open_dsp_device : stereo ") ;
424
if (ioctl (fd, SNDCTL_DSP_RESET, 0))
425
{ perror ("linux_open_dsp_device : reset ") ;
430
if ((error = ioctl (fd, SOUND_PCM_WRITE_BITS, &temp)) != 0)
431
{ perror ("linux_open_dsp_device : bitwidth ") ;
435
if ((error = ioctl (fd, SOUND_PCM_WRITE_CHANNELS, &channels)) != 0)
436
{ perror ("linux_open_dsp_device : channels ") ;
440
if ((error = ioctl (fd, SOUND_PCM_WRITE_RATE, &srate)) != 0)
441
{ perror ("linux_open_dsp_device : sample rate ") ;
445
if ((error = ioctl (fd, SNDCTL_DSP_SYNC, 0)) != 0)
446
{ perror ("linux_open_dsp_device : sync ") ;
451
} /* linux_open_dsp_device */
453
#endif /* __linux__ */
455
/*------------------------------------------------------------------------------
456
** Mac OS X functions for playing a sound.
459
#if (defined (__MACH__) && defined (__APPLE__)) /* MacOSX */
462
{ AudioStreamBasicDescription format ;
465
AudioDeviceID device ;
477
macosx_audio_out_callback (AudioDeviceID device, const AudioTimeStamp* current_time,
478
const AudioBufferList* data_in, const AudioTimeStamp* time_in,
479
AudioBufferList* data_out, const AudioTimeStamp* time_out,
481
{ MacOSXAudioData *audio_data ;
482
int size, sample_count, read_count, k ;
485
/* Prevent compiler warnings. */
487
current_time = current_time ;
490
time_out = time_out ;
492
audio_data = (MacOSXAudioData*) client_data ;
494
size = data_out->mBuffers [0].mDataByteSize ;
495
sample_count = size / sizeof (float) ;
497
buffer = (float*) data_out->mBuffers [0].mData ;
499
if (audio_data->fake_stereo != 0)
500
{ read_count = sf_read_float (audio_data->sndfile, buffer, sample_count / 2) ;
502
for (k = read_count - 1 ; k >= 0 ; k--)
503
{ buffer [2 * k ] = buffer [k] ;
504
buffer [2 * k + 1] = buffer [k] ;
509
read_count = sf_read_float (audio_data->sndfile, buffer, sample_count) ;
511
/* Fill the remainder with zeroes. */
512
if (read_count < sample_count)
513
{ if (audio_data->fake_stereo == 0)
514
memset (&(buffer [read_count]), 0, (sample_count - read_count) * sizeof (float)) ;
515
/* Tell the main application to terminate. */
516
audio_data->done_playing = SF_TRUE ;
520
} /* macosx_audio_out_callback */
523
macosx_play (int argc, char *argv [])
524
{ MacOSXAudioData audio_data ;
526
UInt32 count, buffer_size ;
529
audio_data.fake_stereo = 0 ;
530
audio_data.device = kAudioDeviceUnknown ;
532
/* get the default output device for the HAL */
533
count = sizeof (AudioDeviceID) ;
534
if ((err = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultOutputDevice,
535
&count, (void *) &(audio_data.device))) != noErr)
536
{ printf ("AudioHardwareGetProperty (kAudioDevicePropertyDefaultOutputDevice) failed.\n") ;
540
/* get the buffersize that the default device uses for IO */
541
count = sizeof (UInt32) ;
542
if ((err = AudioDeviceGetProperty (audio_data.device, 0, false, kAudioDevicePropertyBufferSize,
543
&count, &buffer_size)) != noErr)
544
{ printf ("AudioDeviceGetProperty (kAudioDevicePropertyBufferSize) failed.\n") ;
548
/* get a description of the data format used by the default device */
549
count = sizeof (AudioStreamBasicDescription) ;
550
if ((err = AudioDeviceGetProperty (audio_data.device, 0, false, kAudioDevicePropertyStreamFormat,
551
&count, &(audio_data.format))) != noErr)
552
{ printf ("AudioDeviceGetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ;
556
/* Base setup completed. Now play files. */
557
for (k = 1 ; k < argc ; k++)
558
{ printf ("Playing %s\n", argv [k]) ;
559
if (! (audio_data.sndfile = sf_open (argv [k], SFM_READ, &(audio_data.sfinfo))))
560
{ puts (sf_strerror (NULL)) ;
564
if (audio_data.sfinfo.channels < 1 || audio_data.sfinfo.channels > 2)
565
{ printf ("Error : channels = %d.\n", audio_data.sfinfo.channels) ;
569
audio_data.format.mSampleRate = audio_data.sfinfo.samplerate ;
571
if (audio_data.sfinfo.channels == 1)
572
{ audio_data.format.mChannelsPerFrame = 2 ;
573
audio_data.fake_stereo = 1 ;
576
audio_data.format.mChannelsPerFrame = audio_data.sfinfo.channels ;
578
if ((err = AudioDeviceSetProperty (audio_data.device, NULL, 0, false, kAudioDevicePropertyStreamFormat,
579
sizeof (AudioStreamBasicDescription), &(audio_data.format))) != noErr)
580
{ printf ("AudioDeviceSetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ;
584
/* we want linear pcm */
585
if (audio_data.format.mFormatID != kAudioFormatLinearPCM)
588
/* Fire off the device. */
589
if ((err = AudioDeviceAddIOProc (audio_data.device, macosx_audio_out_callback,
590
(void *) &audio_data)) != noErr)
591
{ printf ("AudioDeviceAddIOProc failed.\n") ;
595
err = AudioDeviceStart (audio_data.device, macosx_audio_out_callback) ;
599
audio_data.done_playing = SF_FALSE ;
601
while (audio_data.done_playing == SF_FALSE)
602
usleep (10 * 1000) ; /* 10 000 milliseconds. */
604
if ((err = AudioDeviceStop (audio_data.device, macosx_audio_out_callback)) != noErr)
605
{ printf ("AudioDeviceStop failed.\n") ;
609
err = AudioDeviceRemoveIOProc (audio_data.device, macosx_audio_out_callback) ;
611
{ printf ("AudioDeviceRemoveIOProc failed.\n") ;
615
sf_close (audio_data.sndfile) ;
624
/*------------------------------------------------------------------------------
625
** Win32 functions for playing a sound.
627
** This API sucks. Its needlessly complicated and is *WAY* too loose with
628
** passing pointers arounf in integers and and using char* pointers to
629
** point to data instead of short*. It plain sucks!
632
#if (OS_IS_WIN32 == 1)
634
#define WIN32_BUFFER_LEN (1<<15)
640
CRITICAL_SECTION mutex ; /* to control access to BuffersInUSe */
641
HANDLE Event ; /* signal that a buffer is free */
643
short buffer [WIN32_BUFFER_LEN / sizeof (short)] ;
644
int current, bufferlen ;
650
sf_count_t remaining ;
655
win32_play_data (Win32_Audio_Data *audio_data)
656
{ int thisread, readcount ;
658
/* fill a buffer if there is more data and we can read it sucessfully */
659
readcount = (audio_data->remaining > audio_data->bufferlen) ? audio_data->bufferlen : (int) audio_data->remaining ;
661
thisread = (int) sf_read_short (audio_data->sndfile, (short *) (audio_data->whdr [audio_data->current].lpData), readcount) ;
663
audio_data->remaining -= thisread ;
666
{ /* Fix buffer length if this is only a partial block. */
667
if (thisread < audio_data->bufferlen)
668
audio_data->whdr [audio_data->current].dwBufferLength = thisread * sizeof (short) ;
670
/* Queue the WAVEHDR */
671
waveOutWrite (audio_data->hwave, (LPWAVEHDR) &(audio_data->whdr [audio_data->current]), sizeof (WAVEHDR)) ;
673
/* count another buffer in use */
674
EnterCriticalSection (&audio_data->mutex) ;
675
audio_data->BuffersInUse ++ ;
676
LeaveCriticalSection (&audio_data->mutex) ;
678
/* use the other buffer next time */
679
audio_data->current = (audio_data->current + 1) % 2 ;
683
} /* win32_play_data */
686
win32_audio_out_callback (HWAVEOUT hwave, UINT msg, DWORD data, DWORD param1, DWORD param2)
687
{ Win32_Audio_Data *audio_data ;
689
/* Prevent compiler warnings. */
697
** I consider this technique of passing a pointer via an integer as
698
** fundamentally broken but thats the way microsoft has defined the
701
audio_data = (Win32_Audio_Data*) data ;
703
/* let main loop know a buffer is free */
704
if (msg == MM_WOM_DONE)
705
{ EnterCriticalSection (&audio_data->mutex) ;
706
audio_data->BuffersInUse -- ;
707
LeaveCriticalSection (&audio_data->mutex) ;
708
SetEvent (audio_data->Event) ;
712
} /* win32_audio_out_callback */
714
/* This is needed for earlier versions of the M$ development tools. */
716
#define DWORD_PTR DWORD
720
win32_play (int argc, char *argv [])
721
{ Win32_Audio_Data audio_data ;
726
audio_data.sndfile = NULL ;
727
audio_data.hwave = 0 ;
729
for (k = 1 ; k < argc ; k++)
730
{ printf ("Playing %s\n", argv [k]) ;
732
if (! (audio_data.sndfile = sf_open (argv [k], SFM_READ, &(audio_data.sfinfo))))
733
{ puts (sf_strerror (NULL)) ;
737
audio_data.remaining = audio_data.sfinfo.frames * audio_data.sfinfo.channels ;
738
audio_data.current = 0 ;
740
InitializeCriticalSection (&audio_data.mutex) ;
741
audio_data.Event = CreateEvent (0, FALSE, FALSE, 0) ;
743
wf.nChannels = audio_data.sfinfo.channels ;
744
wf.wFormatTag = WAVE_FORMAT_PCM ;
746
wf.wBitsPerSample = 16 ;
748
wf.nSamplesPerSec = audio_data.sfinfo.samplerate ;
750
wf.nBlockAlign = audio_data.sfinfo.channels * sizeof (short) ;
752
wf.nAvgBytesPerSec = wf.nBlockAlign * wf.nSamplesPerSec ;
754
error = waveOutOpen (&(audio_data.hwave), WAVE_MAPPER, &wf, (DWORD_PTR) win32_audio_out_callback,
755
(DWORD_PTR) &audio_data, CALLBACK_FUNCTION) ;
757
{ puts ("waveOutOpen failed.") ;
758
audio_data.hwave = 0 ;
762
audio_data.whdr [0].lpData = (char*) audio_data.buffer ;
763
audio_data.whdr [1].lpData = ((char*) audio_data.buffer) + sizeof (audio_data.buffer) / 2 ;
765
audio_data.whdr [0].dwBufferLength = sizeof (audio_data.buffer) / 2 ;
766
audio_data.whdr [1].dwBufferLength = sizeof (audio_data.buffer) / 2 ;
768
audio_data.whdr [0].dwFlags = 0 ;
769
audio_data.whdr [1].dwFlags = 0 ;
771
/* length of each audio buffer in samples */
772
audio_data.bufferlen = sizeof (audio_data.buffer) / 2 / sizeof (short) ;
774
/* Prepare the WAVEHDRs */
775
if ((error = waveOutPrepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR))))
776
{ printf ("waveOutPrepareHeader [0] failed : %08X\n", error) ;
777
waveOutClose (audio_data.hwave) ;
781
if ((error = waveOutPrepareHeader (audio_data.hwave, &(audio_data.whdr [1]), sizeof (WAVEHDR))))
782
{ printf ("waveOutPrepareHeader [1] failed : %08X\n", error) ;
783
waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR)) ;
784
waveOutClose (audio_data.hwave) ;
788
/* Fill up both buffers with audio data */
789
audio_data.BuffersInUse = 0 ;
790
win32_play_data (&audio_data) ;
791
win32_play_data (&audio_data) ;
793
/* loop until both buffers are released */
794
while (audio_data.BuffersInUse > 0)
796
/* wait for buffer to be released */
797
WaitForSingleObject (audio_data.Event, INFINITE) ;
799
/* refill the buffer if there is more data to play */
800
win32_play_data (&audio_data) ;
803
waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR)) ;
804
waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [1]), sizeof (WAVEHDR)) ;
806
waveOutClose (audio_data.hwave) ;
807
audio_data.hwave = 0 ;
809
DeleteCriticalSection (&audio_data.mutex) ;
811
sf_close (audio_data.sndfile) ;
818
/*------------------------------------------------------------------------------
822
#if (defined (sun) && defined (unix)) /* ie Solaris */
825
solaris_play (int argc, char *argv [])
826
{ static short buffer [BUFFER_LEN] ;
827
audio_info_t audio_info ;
830
unsigned long delay_time ;
831
long k, start_count, output_count, write_count, read_count ;
832
int audio_fd, error, done ;
834
for (k = 1 ; k < argc ; k++)
835
{ printf ("Playing %s\n", argv [k]) ;
836
if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo)))
837
{ puts (sf_strerror (NULL)) ;
841
if (sfinfo.channels < 1 || sfinfo.channels > 2)
842
{ printf ("Error : channels = %d.\n", sfinfo.channels) ;
846
/* open the audio device - write only, non-blocking */
847
if ((audio_fd = open ("/dev/audio", O_WRONLY | O_NONBLOCK)) < 0)
848
{ perror ("open (/dev/audio) failed") ;
852
/* Retrive standard values. */
853
AUDIO_INITINFO (&audio_info) ;
855
audio_info.play.sample_rate = sfinfo.samplerate ;
856
audio_info.play.channels = sfinfo.channels ;
857
audio_info.play.precision = 16 ;
858
audio_info.play.encoding = AUDIO_ENCODING_LINEAR ;
859
audio_info.play.gain = AUDIO_MAX_GAIN ;
860
audio_info.play.balance = AUDIO_MID_BALANCE ;
862
if ((error = ioctl (audio_fd, AUDIO_SETINFO, &audio_info)))
863
{ perror ("ioctl (AUDIO_SETINFO) failed") ;
867
/* Delay time equal to 1/4 of a buffer in microseconds. */
868
delay_time = (BUFFER_LEN * 1000000) / (audio_info.play.sample_rate * 4) ;
872
{ read_count = sf_read_short (sndfile, buffer, BUFFER_LEN) ;
873
if (read_count < BUFFER_LEN)
874
{ memset (&(buffer [read_count]), 0, (BUFFER_LEN - read_count) * sizeof (short)) ;
875
/* Tell the main application to terminate. */
880
output_count = BUFFER_LEN * sizeof (short) ;
882
while (output_count > 0)
883
{ /* write as much data as possible */
884
write_count = write (audio_fd, &(buffer [start_count]), output_count) ;
886
{ output_count -= write_count ;
887
start_count += write_count ;
890
{ /* Give the audio output time to catch up. */
891
usleep (delay_time) ;
893
} ; /* while (outpur_count > 0) */
894
} ; /* while (! done) */
904
/*==============================================================================
909
main (int argc, char *argv [])
913
printf ("\nUsage : %s <input sound file>\n\n", argv [0]) ;
914
#if (OS_IS_WIN32 == 1)
915
printf ("This is a Unix style command line application which\n"
916
"should be run in a MSDOS box or Command Shell window.\n\n") ;
917
printf ("Sleeping for 5 seconds before exiting.\n\n") ;
919
/* This is the officially blessed by microsoft way but I can't get
922
** Instead, use this:
929
#if defined (__linux__)
930
#if HAVE_ALSA_ASOUNDLIB_H
931
if (access ("/proc/asound/cards", R_OK) == 0)
932
alsa_play (argc, argv) ;
935
linux_play (argc, argv) ;
936
#elif (defined (__MACH__) && defined (__APPLE__))
937
macosx_play (argc, argv) ;
938
#elif (defined (sun) && defined (unix))
939
solaris_play (argc, argv) ;
940
#elif (OS_IS_WIN32 == 1)
941
win32_play (argc, argv) ;
942
#elif defined (__BEOS__)
943
printf ("This program cannot be compiled on BeOS.\n") ;
944
printf ("Instead, compile the file sfplay_beos.cpp.\n") ;
947
puts ("*** Playing sound not yet supported on this platform.") ;
948
puts ("*** Please feel free to submit a patch.") ;
955
** Do not edit or modify anything in this comment block.
956
** The arch-tag line is a file identity tag for the GNU Arch
957
** revision control system.
959
** arch-tag: 8fc4110d-6cec-4e03-91df-0f384cabedac