Mercurial > hg > sv-dependency-builds
comparison src/libsndfile-1.0.27/programs/sndfile-play.c @ 125:cd6cdf86811e
Current libsndfile source
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Tue, 18 Oct 2016 13:22:47 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
124:e3d5853d5918 | 125:cd6cdf86811e |
---|---|
1 /* | |
2 ** Copyright (C) 1999-2015 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 (__ANDROID__) | |
56 | |
57 #elif defined (__linux__) || defined (__FreeBSD_kernel__) || defined (__FreeBSD__) | |
58 #include <fcntl.h> | |
59 #include <sys/ioctl.h> | |
60 #include <sys/soundcard.h> | |
61 | |
62 #elif (defined (__MACH__) && defined (__APPLE__)) | |
63 #include <AvailabilityMacros.h> | |
64 #include <Availability.h> | |
65 | |
66 #if (OSX_DARWIN_VERSION > 11) | |
67 /* Includes go here. */ | |
68 #elif (OSX_DARWIN_VERSION == 11) | |
69 #include <AudioToolbox/AudioToolbox.h> | |
70 #elif (OSX_DARWIN_VERSION > 0 && OSX_DARWIN_VERSION <= 10) | |
71 #include <CoreAudio/AudioHardware.h> | |
72 #endif | |
73 | |
74 #elif HAVE_SNDIO_H | |
75 #include <sndio.h> | |
76 | |
77 #elif (defined (sun) && defined (unix)) | |
78 #include <fcntl.h> | |
79 #include <sys/ioctl.h> | |
80 #include <sys/audioio.h> | |
81 | |
82 #elif (OS_IS_WIN32 == 1) | |
83 #include <windows.h> | |
84 #include <mmsystem.h> | |
85 | |
86 #endif | |
87 | |
88 #define SIGNED_SIZEOF(x) ((int) sizeof (x)) | |
89 #define BUFFER_LEN (2048) | |
90 | |
91 /*------------------------------------------------------------------------------ | |
92 ** Linux/OSS functions for playing a sound. | |
93 */ | |
94 | |
95 #if HAVE_ALSA_ASOUNDLIB_H | |
96 | |
97 static snd_pcm_t * alsa_open (int channels, unsigned srate, int realtime) ; | |
98 static int alsa_write_float (snd_pcm_t *alsa_dev, float *data, int frames, int channels) ; | |
99 | |
100 static void | |
101 alsa_play (int argc, char *argv []) | |
102 { static float buffer [BUFFER_LEN] ; | |
103 SNDFILE *sndfile ; | |
104 SF_INFO sfinfo ; | |
105 snd_pcm_t * alsa_dev ; | |
106 int k, readcount, subformat ; | |
107 | |
108 for (k = 1 ; k < argc ; k++) | |
109 { memset (&sfinfo, 0, sizeof (sfinfo)) ; | |
110 | |
111 printf ("Playing %s\n", argv [k]) ; | |
112 if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo))) | |
113 { puts (sf_strerror (NULL)) ; | |
114 continue ; | |
115 } ; | |
116 | |
117 if (sfinfo.channels < 1 || sfinfo.channels > 2) | |
118 { printf ("Error : channels = %d.\n", sfinfo.channels) ; | |
119 continue ; | |
120 } ; | |
121 | |
122 if ((alsa_dev = alsa_open (sfinfo.channels, (unsigned) sfinfo.samplerate, SF_FALSE)) == NULL) | |
123 continue ; | |
124 | |
125 subformat = sfinfo.format & SF_FORMAT_SUBMASK ; | |
126 | |
127 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) | |
128 { double scale ; | |
129 int m ; | |
130 | |
131 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; | |
132 if (scale < 1e-10) | |
133 scale = 1.0 ; | |
134 else | |
135 scale = 32700.0 / scale ; | |
136 | |
137 while ((readcount = sf_read_float (sndfile, buffer, BUFFER_LEN))) | |
138 { for (m = 0 ; m < readcount ; m++) | |
139 buffer [m] *= scale ; | |
140 alsa_write_float (alsa_dev, buffer, BUFFER_LEN / sfinfo.channels, sfinfo.channels) ; | |
141 } ; | |
142 } | |
143 else | |
144 { while ((readcount = sf_read_float (sndfile, buffer, BUFFER_LEN))) | |
145 alsa_write_float (alsa_dev, buffer, BUFFER_LEN / sfinfo.channels, sfinfo.channels) ; | |
146 } ; | |
147 | |
148 snd_pcm_drain (alsa_dev) ; | |
149 snd_pcm_close (alsa_dev) ; | |
150 | |
151 sf_close (sndfile) ; | |
152 } ; | |
153 | |
154 return ; | |
155 } /* alsa_play */ | |
156 | |
157 static snd_pcm_t * | |
158 alsa_open (int channels, unsigned samplerate, int realtime) | |
159 { const char * device = "default" ; | |
160 snd_pcm_t *alsa_dev = NULL ; | |
161 snd_pcm_hw_params_t *hw_params ; | |
162 snd_pcm_uframes_t buffer_size ; | |
163 snd_pcm_uframes_t alsa_period_size, alsa_buffer_frames ; | |
164 snd_pcm_sw_params_t *sw_params ; | |
165 | |
166 int err ; | |
167 | |
168 if (realtime) | |
169 { alsa_period_size = 256 ; | |
170 alsa_buffer_frames = 3 * alsa_period_size ; | |
171 } | |
172 else | |
173 { alsa_period_size = 1024 ; | |
174 alsa_buffer_frames = 4 * alsa_period_size ; | |
175 } ; | |
176 | |
177 if ((err = snd_pcm_open (&alsa_dev, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) | |
178 { fprintf (stderr, "cannot open audio device \"%s\" (%s)\n", device, snd_strerror (err)) ; | |
179 goto catch_error ; | |
180 } ; | |
181 | |
182 snd_pcm_nonblock (alsa_dev, 0) ; | |
183 | |
184 if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) | |
185 { fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err)) ; | |
186 goto catch_error ; | |
187 } ; | |
188 | |
189 if ((err = snd_pcm_hw_params_any (alsa_dev, hw_params)) < 0) | |
190 { fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err)) ; | |
191 goto catch_error ; | |
192 } ; | |
193 | |
194 if ((err = snd_pcm_hw_params_set_access (alsa_dev, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) | |
195 { fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err)) ; | |
196 goto catch_error ; | |
197 } ; | |
198 | |
199 if ((err = snd_pcm_hw_params_set_format (alsa_dev, hw_params, SND_PCM_FORMAT_FLOAT)) < 0) | |
200 { fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err)) ; | |
201 goto catch_error ; | |
202 } ; | |
203 | |
204 if ((err = snd_pcm_hw_params_set_rate_near (alsa_dev, hw_params, &samplerate, 0)) < 0) | |
205 { fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err)) ; | |
206 goto catch_error ; | |
207 } ; | |
208 | |
209 if ((err = snd_pcm_hw_params_set_channels (alsa_dev, hw_params, channels)) < 0) | |
210 { fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err)) ; | |
211 goto catch_error ; | |
212 } ; | |
213 | |
214 if ((err = snd_pcm_hw_params_set_buffer_size_near (alsa_dev, hw_params, &alsa_buffer_frames)) < 0) | |
215 { fprintf (stderr, "cannot set buffer size (%s)\n", snd_strerror (err)) ; | |
216 goto catch_error ; | |
217 } ; | |
218 | |
219 if ((err = snd_pcm_hw_params_set_period_size_near (alsa_dev, hw_params, &alsa_period_size, 0)) < 0) | |
220 { fprintf (stderr, "cannot set period size (%s)\n", snd_strerror (err)) ; | |
221 goto catch_error ; | |
222 } ; | |
223 | |
224 if ((err = snd_pcm_hw_params (alsa_dev, hw_params)) < 0) | |
225 { fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err)) ; | |
226 goto catch_error ; | |
227 } ; | |
228 | |
229 /* extra check: if we have only one period, this code won't work */ | |
230 snd_pcm_hw_params_get_period_size (hw_params, &alsa_period_size, 0) ; | |
231 snd_pcm_hw_params_get_buffer_size (hw_params, &buffer_size) ; | |
232 if (alsa_period_size == buffer_size) | |
233 { fprintf (stderr, "Can't use period equal to buffer size (%lu == %lu)", alsa_period_size, buffer_size) ; | |
234 goto catch_error ; | |
235 } ; | |
236 | |
237 snd_pcm_hw_params_free (hw_params) ; | |
238 | |
239 if ((err = snd_pcm_sw_params_malloc (&sw_params)) != 0) | |
240 { fprintf (stderr, "%s: snd_pcm_sw_params_malloc: %s", __func__, snd_strerror (err)) ; | |
241 goto catch_error ; | |
242 } ; | |
243 | |
244 if ((err = snd_pcm_sw_params_current (alsa_dev, sw_params)) != 0) | |
245 { fprintf (stderr, "%s: snd_pcm_sw_params_current: %s", __func__, snd_strerror (err)) ; | |
246 goto catch_error ; | |
247 } ; | |
248 | |
249 /* note: set start threshold to delay start until the ring buffer is full */ | |
250 snd_pcm_sw_params_current (alsa_dev, sw_params) ; | |
251 | |
252 if ((err = snd_pcm_sw_params_set_start_threshold (alsa_dev, sw_params, buffer_size)) < 0) | |
253 { fprintf (stderr, "cannot set start threshold (%s)\n", snd_strerror (err)) ; | |
254 goto catch_error ; | |
255 } ; | |
256 | |
257 if ((err = snd_pcm_sw_params (alsa_dev, sw_params)) != 0) | |
258 { fprintf (stderr, "%s: snd_pcm_sw_params: %s", __func__, snd_strerror (err)) ; | |
259 goto catch_error ; | |
260 } ; | |
261 | |
262 snd_pcm_sw_params_free (sw_params) ; | |
263 | |
264 snd_pcm_reset (alsa_dev) ; | |
265 | |
266 catch_error : | |
267 | |
268 if (err < 0 && alsa_dev != NULL) | |
269 { snd_pcm_close (alsa_dev) ; | |
270 return NULL ; | |
271 } ; | |
272 | |
273 return alsa_dev ; | |
274 } /* alsa_open */ | |
275 | |
276 static int | |
277 alsa_write_float (snd_pcm_t *alsa_dev, float *data, int frames, int channels) | |
278 { static int epipe_count = 0 ; | |
279 | |
280 int total = 0 ; | |
281 int retval ; | |
282 | |
283 if (epipe_count > 0) | |
284 epipe_count -- ; | |
285 | |
286 while (total < frames) | |
287 { retval = snd_pcm_writei (alsa_dev, data + total * channels, frames - total) ; | |
288 | |
289 if (retval >= 0) | |
290 { total += retval ; | |
291 if (total == frames) | |
292 return total ; | |
293 | |
294 continue ; | |
295 } ; | |
296 | |
297 switch (retval) | |
298 { case -EAGAIN : | |
299 puts ("alsa_write_float: EAGAIN") ; | |
300 continue ; | |
301 break ; | |
302 | |
303 case -EPIPE : | |
304 if (epipe_count > 0) | |
305 { printf ("alsa_write_float: EPIPE %d\n", epipe_count) ; | |
306 if (epipe_count > 140) | |
307 return retval ; | |
308 } ; | |
309 epipe_count += 100 ; | |
310 | |
311 #if 0 | |
312 if (0) | |
313 { snd_pcm_status_t *status ; | |
314 | |
315 snd_pcm_status_alloca (&status) ; | |
316 if ((retval = snd_pcm_status (alsa_dev, status)) < 0) | |
317 fprintf (stderr, "alsa_out: xrun. can't determine length\n") ; | |
318 else if (snd_pcm_status_get_state (status) == SND_PCM_STATE_XRUN) | |
319 { struct timeval now, diff, tstamp ; | |
320 | |
321 gettimeofday (&now, 0) ; | |
322 snd_pcm_status_get_trigger_tstamp (status, &tstamp) ; | |
323 timersub (&now, &tstamp, &diff) ; | |
324 | |
325 fprintf (stderr, "alsa_write_float xrun: of at least %.3f msecs. resetting stream\n", | |
326 diff.tv_sec * 1000 + diff.tv_usec / 1000.0) ; | |
327 } | |
328 else | |
329 fprintf (stderr, "alsa_write_float: xrun. can't determine length\n") ; | |
330 } ; | |
331 #endif | |
332 | |
333 snd_pcm_prepare (alsa_dev) ; | |
334 break ; | |
335 | |
336 case -EBADFD : | |
337 fprintf (stderr, "alsa_write_float: Bad PCM state.n") ; | |
338 return 0 ; | |
339 break ; | |
340 | |
341 case -ESTRPIPE : | |
342 fprintf (stderr, "alsa_write_float: Suspend event.n") ; | |
343 return 0 ; | |
344 break ; | |
345 | |
346 case -EIO : | |
347 puts ("alsa_write_float: EIO") ; | |
348 return 0 ; | |
349 | |
350 default : | |
351 fprintf (stderr, "alsa_write_float: retval = %d\n", retval) ; | |
352 return 0 ; | |
353 break ; | |
354 } ; /* switch */ | |
355 } ; /* while */ | |
356 | |
357 return total ; | |
358 } /* alsa_write_float */ | |
359 | |
360 #endif /* HAVE_ALSA_ASOUNDLIB_H */ | |
361 | |
362 /*------------------------------------------------------------------------------ | |
363 ** Linux/OSS functions for playing a sound. | |
364 */ | |
365 | |
366 #if !defined (__ANDROID__) && (defined (__linux__) || defined (__FreeBSD_kernel__) || defined (__FreeBSD__)) | |
367 | |
368 static int opensoundsys_open_device (int channels, int srate) ; | |
369 | |
370 static int | |
371 opensoundsys_play (int argc, char *argv []) | |
372 { static short buffer [BUFFER_LEN] ; | |
373 SNDFILE *sndfile ; | |
374 SF_INFO sfinfo ; | |
375 int k, audio_device, readcount, writecount, subformat ; | |
376 | |
377 for (k = 1 ; k < argc ; k++) | |
378 { memset (&sfinfo, 0, sizeof (sfinfo)) ; | |
379 | |
380 printf ("Playing %s\n", argv [k]) ; | |
381 if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo))) | |
382 { puts (sf_strerror (NULL)) ; | |
383 continue ; | |
384 } ; | |
385 | |
386 if (sfinfo.channels < 1 || sfinfo.channels > 2) | |
387 { printf ("Error : channels = %d.\n", sfinfo.channels) ; | |
388 continue ; | |
389 } ; | |
390 | |
391 audio_device = opensoundsys_open_device (sfinfo.channels, sfinfo.samplerate) ; | |
392 | |
393 subformat = sfinfo.format & SF_FORMAT_SUBMASK ; | |
394 | |
395 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) | |
396 { static float float_buffer [BUFFER_LEN] ; | |
397 double scale ; | |
398 int m ; | |
399 | |
400 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; | |
401 if (scale < 1e-10) | |
402 scale = 1.0 ; | |
403 else | |
404 scale = 32700.0 / scale ; | |
405 | |
406 while ((readcount = sf_read_float (sndfile, float_buffer, BUFFER_LEN))) | |
407 { for (m = 0 ; m < readcount ; m++) | |
408 buffer [m] = scale * float_buffer [m] ; | |
409 writecount = write (audio_device, buffer, readcount * sizeof (short)) ; | |
410 } ; | |
411 } | |
412 else | |
413 { while ((readcount = sf_read_short (sndfile, buffer, BUFFER_LEN))) | |
414 writecount = write (audio_device, buffer, readcount * sizeof (short)) ; | |
415 } ; | |
416 | |
417 if (ioctl (audio_device, SNDCTL_DSP_POST, 0) == -1) | |
418 perror ("ioctl (SNDCTL_DSP_POST) ") ; | |
419 | |
420 if (ioctl (audio_device, SNDCTL_DSP_SYNC, 0) == -1) | |
421 perror ("ioctl (SNDCTL_DSP_SYNC) ") ; | |
422 | |
423 close (audio_device) ; | |
424 | |
425 sf_close (sndfile) ; | |
426 } ; | |
427 | |
428 return writecount ; | |
429 } /* opensoundsys_play */ | |
430 | |
431 static int | |
432 opensoundsys_open_device (int channels, int srate) | |
433 { int fd, stereo, fmt ; | |
434 | |
435 if ((fd = open ("/dev/dsp", O_WRONLY, 0)) == -1 && | |
436 (fd = open ("/dev/sound/dsp", O_WRONLY, 0)) == -1) | |
437 { perror ("opensoundsys_open_device : open ") ; | |
438 exit (1) ; | |
439 } ; | |
440 | |
441 stereo = 0 ; | |
442 if (ioctl (fd, SNDCTL_DSP_STEREO, &stereo) == -1) | |
443 { /* Fatal error */ | |
444 perror ("opensoundsys_open_device : stereo ") ; | |
445 exit (1) ; | |
446 } ; | |
447 | |
448 if (ioctl (fd, SNDCTL_DSP_RESET, 0)) | |
449 { perror ("opensoundsys_open_device : reset ") ; | |
450 exit (1) ; | |
451 } ; | |
452 | |
453 fmt = CPU_IS_BIG_ENDIAN ? AFMT_S16_BE : AFMT_S16_LE ; | |
454 if (ioctl (fd, SNDCTL_DSP_SETFMT, &fmt) != 0) | |
455 { perror ("opensoundsys_open_device : set format ") ; | |
456 exit (1) ; | |
457 } ; | |
458 | |
459 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &channels) != 0) | |
460 { perror ("opensoundsys_open_device : channels ") ; | |
461 exit (1) ; | |
462 } ; | |
463 | |
464 if (ioctl (fd, SNDCTL_DSP_SPEED, &srate) != 0) | |
465 { perror ("opensoundsys_open_device : sample rate ") ; | |
466 exit (1) ; | |
467 } ; | |
468 | |
469 if (ioctl (fd, SNDCTL_DSP_SYNC, 0) != 0) | |
470 { perror ("opensoundsys_open_device : sync ") ; | |
471 exit (1) ; | |
472 } ; | |
473 | |
474 return fd ; | |
475 } /* opensoundsys_open_device */ | |
476 | |
477 #endif /* __linux__ */ | |
478 | |
479 /*------------------------------------------------------------------------------ | |
480 ** Mac OS X functions for playing a sound. | |
481 */ | |
482 | |
483 #if (OSX_DARWIN_VERSION > 11) | |
484 /* MacOSX 10.8 use a new Audio API. Someone needs to write some code for it. */ | |
485 #endif /* OSX_DARWIN_VERSION > 11 */ | |
486 | |
487 #if (OSX_DARWIN_VERSION == 11) | |
488 /* MacOSX 10.7 use AudioQueue API */ | |
489 | |
490 #define kBytesPerAudioBuffer (1024 * 8) | |
491 #define kNumberOfAudioBuffers 4 | |
492 | |
493 typedef struct | |
494 { AudioStreamBasicDescription format ; | |
495 | |
496 AudioQueueRef queue ; | |
497 AudioQueueBufferRef queueBuffer [kNumberOfAudioBuffers] ; | |
498 | |
499 UInt32 buf_size ; | |
500 | |
501 SNDFILE *sndfile ; | |
502 SF_INFO sfinfo ; | |
503 | |
504 int done_playing ; | |
505 } MacOSXAudioData ; | |
506 | |
507 | |
508 static void | |
509 macosx_fill_buffer (MacOSXAudioData *audio_data, AudioQueueBufferRef audio_buffer) | |
510 { int size, sample_count, read_count ; | |
511 short *buffer ; | |
512 | |
513 size = audio_buffer->mAudioDataBytesCapacity ; | |
514 sample_count = size / sizeof (short) ; | |
515 | |
516 buffer = (short*) audio_buffer->mAudioData ; | |
517 | |
518 read_count = sf_read_short (audio_data->sndfile, buffer, sample_count) ; | |
519 | |
520 if (read_count > 0) | |
521 { audio_buffer->mAudioDataByteSize = read_count * sizeof (short) ; | |
522 AudioQueueEnqueueBuffer (audio_data->queue, audio_buffer, 0, NULL) ; | |
523 } | |
524 else | |
525 AudioQueueStop (audio_data->queue, false) ; | |
526 | |
527 } /* macosx_fill_buffer */ | |
528 | |
529 | |
530 static void | |
531 macosx_audio_out_callback (void *user_data, AudioQueueRef audio_queue, AudioQueueBufferRef audio_buffer) | |
532 { MacOSXAudioData *audio_data = (MacOSXAudioData *) user_data ; | |
533 | |
534 if (audio_data->queue == audio_queue) | |
535 macosx_fill_buffer (audio_data, audio_buffer) ; | |
536 | |
537 } /* macosx_audio_out_callback */ | |
538 | |
539 | |
540 static void | |
541 macosx_audio_out_property_callback (void *user_data, AudioQueueRef audio_queue, AudioQueuePropertyID prop) | |
542 { MacOSXAudioData *audio_data = (MacOSXAudioData *) user_data ; | |
543 | |
544 if (prop == kAudioQueueProperty_IsRunning) | |
545 { UInt32 is_running = 0 ; | |
546 UInt32 is_running_size = sizeof (is_running) ; | |
547 | |
548 AudioQueueGetProperty (audio_queue, kAudioQueueProperty_IsRunning, &is_running, &is_running_size) ; | |
549 | |
550 if (!is_running) | |
551 { audio_data->done_playing = SF_TRUE ; | |
552 CFRunLoopStop (CFRunLoopGetCurrent ()) ; | |
553 } ; | |
554 } ; | |
555 } /* macosx_audio_out_property_callback */ | |
556 | |
557 | |
558 | |
559 static void | |
560 macosx_play (int argc, char *argv []) | |
561 { MacOSXAudioData audio_data ; | |
562 OSStatus err ; | |
563 int i ; | |
564 int k ; | |
565 | |
566 memset (&audio_data, 0x55, sizeof (audio_data)) ; | |
567 | |
568 for (k = 1 ; k < argc ; k++) | |
569 { memset (&(audio_data.sfinfo), 0, sizeof (audio_data.sfinfo)) ; | |
570 | |
571 printf ("Playing %s\n", argv [k]) ; | |
572 if (! (audio_data.sndfile = sf_open (argv [k], SFM_READ, &(audio_data.sfinfo)))) | |
573 { puts (sf_strerror (NULL)) ; | |
574 continue ; | |
575 } ; | |
576 | |
577 if (audio_data.sfinfo.channels < 1 || audio_data.sfinfo.channels > 2) | |
578 { printf ("Error : channels = %d.\n", audio_data.sfinfo.channels) ; | |
579 continue ; | |
580 } ; | |
581 | |
582 /* fill ASBD */ | |
583 audio_data.format.mSampleRate = audio_data.sfinfo.samplerate ; | |
584 audio_data.format.mChannelsPerFrame = audio_data.sfinfo.channels ; | |
585 audio_data.format.mFormatID = kAudioFormatLinearPCM ; | |
586 audio_data.format.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked ; | |
587 audio_data.format.mBytesPerPacket = audio_data.format.mChannelsPerFrame * 2 ; | |
588 audio_data.format.mFramesPerPacket = 1 ; | |
589 audio_data.format.mBytesPerFrame = audio_data.format.mBytesPerPacket ; | |
590 audio_data.format.mBitsPerChannel = 16 ; | |
591 audio_data.format.mReserved = 0 ; | |
592 | |
593 /* create the queue */ | |
594 if ((err = AudioQueueNewOutput (&(audio_data.format), macosx_audio_out_callback, &audio_data, | |
595 CFRunLoopGetCurrent (), kCFRunLoopCommonModes, 0, &(audio_data.queue))) != noErr) | |
596 { printf ("AudioQueueNewOutput failed\n") ; | |
597 return ; | |
598 } ; | |
599 | |
600 /* add property listener */ | |
601 if ((err = AudioQueueAddPropertyListener (audio_data.queue, kAudioQueueProperty_IsRunning, macosx_audio_out_property_callback, &audio_data)) != noErr) | |
602 { printf ("AudioQueueAddPropertyListener failed\n") ; | |
603 return ; | |
604 } ; | |
605 | |
606 /* create the buffers */ | |
607 for (i = 0 ; i < kNumberOfAudioBuffers ; i++) | |
608 { if ((err = AudioQueueAllocateBuffer (audio_data.queue, kBytesPerAudioBuffer, &audio_data.queueBuffer [i])) != noErr) | |
609 { printf ("AudioQueueAllocateBuffer failed\n") ; | |
610 return ; | |
611 } ; | |
612 | |
613 macosx_fill_buffer (&audio_data, audio_data.queueBuffer [i]) ; | |
614 } ; | |
615 | |
616 audio_data.done_playing = SF_FALSE ; | |
617 | |
618 /* start queue */ | |
619 if ((err = AudioQueueStart (audio_data.queue, NULL)) != noErr) | |
620 { printf ("AudioQueueStart failed\n") ; | |
621 return ; | |
622 } ; | |
623 | |
624 while (audio_data.done_playing == SF_FALSE) | |
625 CFRunLoopRun () ; | |
626 | |
627 /* free the buffers */ | |
628 for (i = 0 ; i < kNumberOfAudioBuffers ; i++) | |
629 { if ((err = AudioQueueFreeBuffer (audio_data.queue, audio_data.queueBuffer [i])) != noErr) | |
630 { printf ("AudioQueueFreeBuffer failed\n") ; | |
631 return ; | |
632 } ; | |
633 } ; | |
634 | |
635 /* free the queue */ | |
636 if ((err = AudioQueueDispose (audio_data.queue, true)) != noErr) | |
637 { printf ("AudioQueueDispose failed\n") ; | |
638 return ; | |
639 } ; | |
640 | |
641 sf_close (audio_data.sndfile) ; | |
642 } ; | |
643 | |
644 return ; | |
645 } /* macosx_play, AudioQueue implementation */ | |
646 | |
647 #endif /* OSX_DARWIN_VERSION == 11 */ | |
648 | |
649 #if (OSX_DARWIN_VERSION > 0 && OSX_DARWIN_VERSION <= 10) | |
650 /* MacOSX 10.6 or earlier, use AudioHardware API */ | |
651 | |
652 typedef struct | |
653 { AudioStreamBasicDescription format ; | |
654 | |
655 UInt32 buf_size ; | |
656 AudioDeviceID device ; | |
657 | |
658 SNDFILE *sndfile ; | |
659 SF_INFO sfinfo ; | |
660 | |
661 int fake_stereo ; | |
662 int done_playing ; | |
663 } MacOSXAudioData ; | |
664 | |
665 #include <math.h> | |
666 | |
667 static OSStatus | |
668 macosx_audio_out_callback (AudioDeviceID device, const AudioTimeStamp* current_time, | |
669 const AudioBufferList* data_in, const AudioTimeStamp* time_in, | |
670 AudioBufferList* data_out, const AudioTimeStamp* time_out, | |
671 void* client_data) | |
672 { MacOSXAudioData *audio_data ; | |
673 int size, sample_count, read_count, k ; | |
674 float *buffer ; | |
675 | |
676 /* Prevent compiler warnings. */ | |
677 device = device ; | |
678 current_time = current_time ; | |
679 data_in = data_in ; | |
680 time_in = time_in ; | |
681 time_out = time_out ; | |
682 | |
683 audio_data = (MacOSXAudioData*) client_data ; | |
684 | |
685 size = data_out->mBuffers [0].mDataByteSize ; | |
686 sample_count = size / sizeof (float) ; | |
687 | |
688 buffer = (float*) data_out->mBuffers [0].mData ; | |
689 | |
690 if (audio_data->fake_stereo != 0) | |
691 { read_count = sf_read_float (audio_data->sndfile, buffer, sample_count / 2) ; | |
692 | |
693 for (k = read_count - 1 ; k >= 0 ; k--) | |
694 { buffer [2 * k ] = buffer [k] ; | |
695 buffer [2 * k + 1] = buffer [k] ; | |
696 } ; | |
697 read_count *= 2 ; | |
698 } | |
699 else | |
700 read_count = sf_read_float (audio_data->sndfile, buffer, sample_count) ; | |
701 | |
702 /* Fill the remainder with zeroes. */ | |
703 if (read_count < sample_count) | |
704 { if (audio_data->fake_stereo == 0) | |
705 memset (&(buffer [read_count]), 0, (sample_count - read_count) * sizeof (float)) ; | |
706 /* Tell the main application to terminate. */ | |
707 audio_data->done_playing = SF_TRUE ; | |
708 } ; | |
709 | |
710 return noErr ; | |
711 } /* macosx_audio_out_callback */ | |
712 | |
713 static void | |
714 macosx_play (int argc, char *argv []) | |
715 { MacOSXAudioData audio_data ; | |
716 OSStatus err ; | |
717 UInt32 count, buffer_size ; | |
718 int k ; | |
719 | |
720 audio_data.fake_stereo = 0 ; | |
721 audio_data.device = kAudioDeviceUnknown ; | |
722 | |
723 /* get the default output device for the HAL */ | |
724 count = sizeof (AudioDeviceID) ; | |
725 if ((err = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultOutputDevice, | |
726 &count, (void *) &(audio_data.device))) != noErr) | |
727 { printf ("AudioHardwareGetProperty (kAudioDevicePropertyDefaultOutputDevice) failed.\n") ; | |
728 return ; | |
729 } ; | |
730 | |
731 /* get the buffersize that the default device uses for IO */ | |
732 count = sizeof (UInt32) ; | |
733 if ((err = AudioDeviceGetProperty (audio_data.device, 0, false, kAudioDevicePropertyBufferSize, | |
734 &count, &buffer_size)) != noErr) | |
735 { printf ("AudioDeviceGetProperty (kAudioDevicePropertyBufferSize) failed.\n") ; | |
736 return ; | |
737 } ; | |
738 | |
739 /* get a description of the data format used by the default device */ | |
740 count = sizeof (AudioStreamBasicDescription) ; | |
741 if ((err = AudioDeviceGetProperty (audio_data.device, 0, false, kAudioDevicePropertyStreamFormat, | |
742 &count, &(audio_data.format))) != noErr) | |
743 { printf ("AudioDeviceGetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ; | |
744 return ; | |
745 } ; | |
746 | |
747 /* Base setup completed. Now play files. */ | |
748 for (k = 1 ; k < argc ; k++) | |
749 { memset (&(audio_data.sfinfo), 0, sizeof (audio_data.sfinfo)) ; | |
750 | |
751 printf ("Playing %s\n", argv [k]) ; | |
752 if (! (audio_data.sndfile = sf_open (argv [k], SFM_READ, &(audio_data.sfinfo)))) | |
753 { puts (sf_strerror (NULL)) ; | |
754 continue ; | |
755 } ; | |
756 | |
757 if (audio_data.sfinfo.channels < 1 || audio_data.sfinfo.channels > 2) | |
758 { printf ("Error : channels = %d.\n", audio_data.sfinfo.channels) ; | |
759 continue ; | |
760 } ; | |
761 | |
762 audio_data.format.mSampleRate = audio_data.sfinfo.samplerate ; | |
763 | |
764 if (audio_data.sfinfo.channels == 1) | |
765 { audio_data.format.mChannelsPerFrame = 2 ; | |
766 audio_data.fake_stereo = 1 ; | |
767 } | |
768 else | |
769 audio_data.format.mChannelsPerFrame = audio_data.sfinfo.channels ; | |
770 | |
771 if ((err = AudioDeviceSetProperty (audio_data.device, NULL, 0, false, kAudioDevicePropertyStreamFormat, | |
772 sizeof (AudioStreamBasicDescription), &(audio_data.format))) != noErr) | |
773 { printf ("AudioDeviceSetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ; | |
774 return ; | |
775 } ; | |
776 | |
777 /* we want linear pcm */ | |
778 if (audio_data.format.mFormatID != kAudioFormatLinearPCM) | |
779 return ; | |
780 | |
781 /* Fire off the device. */ | |
782 if ((err = AudioDeviceAddIOProc (audio_data.device, macosx_audio_out_callback, | |
783 (void *) &audio_data)) != noErr) | |
784 { printf ("AudioDeviceAddIOProc failed.\n") ; | |
785 return ; | |
786 } ; | |
787 | |
788 err = AudioDeviceStart (audio_data.device, macosx_audio_out_callback) ; | |
789 if (err != noErr) | |
790 return ; | |
791 | |
792 audio_data.done_playing = SF_FALSE ; | |
793 | |
794 while (audio_data.done_playing == SF_FALSE) | |
795 usleep (10 * 1000) ; /* 10 000 milliseconds. */ | |
796 | |
797 if ((err = AudioDeviceStop (audio_data.device, macosx_audio_out_callback)) != noErr) | |
798 { printf ("AudioDeviceStop failed.\n") ; | |
799 return ; | |
800 } ; | |
801 | |
802 err = AudioDeviceRemoveIOProc (audio_data.device, macosx_audio_out_callback) ; | |
803 if (err != noErr) | |
804 { printf ("AudioDeviceRemoveIOProc failed.\n") ; | |
805 return ; | |
806 } ; | |
807 | |
808 sf_close (audio_data.sndfile) ; | |
809 } ; | |
810 | |
811 return ; | |
812 } /* macosx_play, AudioHardware implementation */ | |
813 | |
814 #endif /* OSX_DARWIN_VERSION > 0 && OSX_DARWIN_VERSION <= 10 */ | |
815 | |
816 /*------------------------------------------------------------------------------ | |
817 ** Win32 functions for playing a sound. | |
818 ** | |
819 ** This API sucks. Its needlessly complicated and is *WAY* too loose with | |
820 ** passing pointers around in integers and using char* pointers to | |
821 ** point to data instead of short*. It plain sucks! | |
822 */ | |
823 | |
824 #if (OS_IS_WIN32 == 1) | |
825 | |
826 #define WIN32_BUFFER_LEN (1 << 15) | |
827 | |
828 typedef struct | |
829 { HWAVEOUT hwave ; | |
830 WAVEHDR whdr [2] ; | |
831 | |
832 CRITICAL_SECTION mutex ; /* to control access to BuffersInUSe */ | |
833 HANDLE Event ; /* signal that a buffer is free */ | |
834 | |
835 short buffer [WIN32_BUFFER_LEN / sizeof (short)] ; | |
836 int current, bufferlen ; | |
837 int BuffersInUse ; | |
838 | |
839 SNDFILE *sndfile ; | |
840 SF_INFO sfinfo ; | |
841 | |
842 sf_count_t remaining ; | |
843 } Win32_Audio_Data ; | |
844 | |
845 | |
846 static void | |
847 win32_play_data (Win32_Audio_Data *audio_data) | |
848 { int thisread, readcount ; | |
849 | |
850 /* fill a buffer if there is more data and we can read it sucessfully */ | |
851 readcount = (audio_data->remaining > audio_data->bufferlen) ? audio_data->bufferlen : (int) audio_data->remaining ; | |
852 | |
853 thisread = (int) sf_read_short (audio_data->sndfile, (short *) (audio_data->whdr [audio_data->current].lpData), readcount) ; | |
854 | |
855 audio_data->remaining -= thisread ; | |
856 | |
857 if (thisread > 0) | |
858 { /* Fix buffer length if this is only a partial block. */ | |
859 if (thisread < audio_data->bufferlen) | |
860 audio_data->whdr [audio_data->current].dwBufferLength = thisread * sizeof (short) ; | |
861 | |
862 /* Queue the WAVEHDR */ | |
863 waveOutWrite (audio_data->hwave, (LPWAVEHDR) &(audio_data->whdr [audio_data->current]), sizeof (WAVEHDR)) ; | |
864 | |
865 /* count another buffer in use */ | |
866 EnterCriticalSection (&audio_data->mutex) ; | |
867 audio_data->BuffersInUse ++ ; | |
868 LeaveCriticalSection (&audio_data->mutex) ; | |
869 | |
870 /* use the other buffer next time */ | |
871 audio_data->current = (audio_data->current + 1) % 2 ; | |
872 } ; | |
873 | |
874 return ; | |
875 } /* win32_play_data */ | |
876 | |
877 static void CALLBACK | |
878 win32_audio_out_callback (HWAVEOUT hwave, UINT msg, DWORD_PTR data, DWORD param1, DWORD param2) | |
879 { Win32_Audio_Data *audio_data ; | |
880 | |
881 /* Prevent compiler warnings. */ | |
882 (void) hwave ; | |
883 (void) param1 ; | |
884 (void) param2 ; | |
885 | |
886 if (data == 0) | |
887 return ; | |
888 | |
889 /* | |
890 ** I consider this technique of passing a pointer via an integer as | |
891 ** fundamentally broken but thats the way microsoft has defined the | |
892 ** interface. | |
893 */ | |
894 audio_data = (Win32_Audio_Data*) data ; | |
895 | |
896 /* let main loop know a buffer is free */ | |
897 if (msg == MM_WOM_DONE) | |
898 { EnterCriticalSection (&audio_data->mutex) ; | |
899 audio_data->BuffersInUse -- ; | |
900 LeaveCriticalSection (&audio_data->mutex) ; | |
901 SetEvent (audio_data->Event) ; | |
902 } ; | |
903 | |
904 return ; | |
905 } /* win32_audio_out_callback */ | |
906 | |
907 static void | |
908 win32_play (int argc, char *argv []) | |
909 { Win32_Audio_Data audio_data ; | |
910 | |
911 WAVEFORMATEX wf ; | |
912 int k, error ; | |
913 | |
914 audio_data.sndfile = NULL ; | |
915 audio_data.hwave = 0 ; | |
916 | |
917 for (k = 1 ; k < argc ; k++) | |
918 { printf ("Playing %s\n", argv [k]) ; | |
919 | |
920 if (! (audio_data.sndfile = sf_open (argv [k], SFM_READ, &(audio_data.sfinfo)))) | |
921 { puts (sf_strerror (NULL)) ; | |
922 continue ; | |
923 } ; | |
924 | |
925 audio_data.remaining = audio_data.sfinfo.frames * audio_data.sfinfo.channels ; | |
926 audio_data.current = 0 ; | |
927 | |
928 InitializeCriticalSection (&audio_data.mutex) ; | |
929 audio_data.Event = CreateEvent (0, FALSE, FALSE, 0) ; | |
930 | |
931 wf.nChannels = audio_data.sfinfo.channels ; | |
932 wf.wFormatTag = WAVE_FORMAT_PCM ; | |
933 wf.cbSize = 0 ; | |
934 wf.wBitsPerSample = 16 ; | |
935 | |
936 wf.nSamplesPerSec = audio_data.sfinfo.samplerate ; | |
937 | |
938 wf.nBlockAlign = audio_data.sfinfo.channels * sizeof (short) ; | |
939 | |
940 wf.nAvgBytesPerSec = wf.nBlockAlign * wf.nSamplesPerSec ; | |
941 | |
942 error = waveOutOpen (&(audio_data.hwave), WAVE_MAPPER, &wf, (DWORD_PTR) win32_audio_out_callback, | |
943 (DWORD_PTR) &audio_data, CALLBACK_FUNCTION) ; | |
944 if (error) | |
945 { puts ("waveOutOpen failed.") ; | |
946 audio_data.hwave = 0 ; | |
947 continue ; | |
948 } ; | |
949 | |
950 audio_data.whdr [0].lpData = (char*) audio_data.buffer ; | |
951 audio_data.whdr [1].lpData = ((char*) audio_data.buffer) + sizeof (audio_data.buffer) / 2 ; | |
952 | |
953 audio_data.whdr [0].dwBufferLength = sizeof (audio_data.buffer) / 2 ; | |
954 audio_data.whdr [1].dwBufferLength = sizeof (audio_data.buffer) / 2 ; | |
955 | |
956 audio_data.whdr [0].dwFlags = 0 ; | |
957 audio_data.whdr [1].dwFlags = 0 ; | |
958 | |
959 /* length of each audio buffer in samples */ | |
960 audio_data.bufferlen = sizeof (audio_data.buffer) / 2 / sizeof (short) ; | |
961 | |
962 /* Prepare the WAVEHDRs */ | |
963 if ((error = waveOutPrepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR)))) | |
964 { printf ("waveOutPrepareHeader [0] failed : %08X\n", error) ; | |
965 waveOutClose (audio_data.hwave) ; | |
966 continue ; | |
967 } ; | |
968 | |
969 if ((error = waveOutPrepareHeader (audio_data.hwave, &(audio_data.whdr [1]), sizeof (WAVEHDR)))) | |
970 { printf ("waveOutPrepareHeader [1] failed : %08X\n", error) ; | |
971 waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR)) ; | |
972 waveOutClose (audio_data.hwave) ; | |
973 continue ; | |
974 } ; | |
975 | |
976 /* Fill up both buffers with audio data */ | |
977 audio_data.BuffersInUse = 0 ; | |
978 win32_play_data (&audio_data) ; | |
979 win32_play_data (&audio_data) ; | |
980 | |
981 /* loop until both buffers are released */ | |
982 while (audio_data.BuffersInUse > 0) | |
983 { | |
984 /* wait for buffer to be released */ | |
985 WaitForSingleObject (audio_data.Event, INFINITE) ; | |
986 | |
987 /* refill the buffer if there is more data to play */ | |
988 win32_play_data (&audio_data) ; | |
989 } ; | |
990 | |
991 waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [0]), sizeof (WAVEHDR)) ; | |
992 waveOutUnprepareHeader (audio_data.hwave, &(audio_data.whdr [1]), sizeof (WAVEHDR)) ; | |
993 | |
994 waveOutClose (audio_data.hwave) ; | |
995 audio_data.hwave = 0 ; | |
996 | |
997 DeleteCriticalSection (&audio_data.mutex) ; | |
998 | |
999 sf_close (audio_data.sndfile) ; | |
1000 } ; | |
1001 | |
1002 } /* win32_play */ | |
1003 | |
1004 #endif /* Win32 */ | |
1005 | |
1006 /*------------------------------------------------------------------------------ | |
1007 ** OpenBSD's sndio. | |
1008 */ | |
1009 | |
1010 #if HAVE_SNDIO_H | |
1011 | |
1012 static void | |
1013 sndio_play (int argc, char *argv []) | |
1014 { struct sio_hdl *hdl ; | |
1015 struct sio_par par ; | |
1016 short buffer [BUFFER_LEN] ; | |
1017 SNDFILE *sndfile ; | |
1018 SF_INFO sfinfo ; | |
1019 int k, readcount ; | |
1020 | |
1021 for (k = 1 ; k < argc ; k++) | |
1022 { printf ("Playing %s\n", argv [k]) ; | |
1023 if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo))) | |
1024 { puts (sf_strerror (NULL)) ; | |
1025 continue ; | |
1026 } ; | |
1027 | |
1028 if (sfinfo.channels < 1 || sfinfo.channels > 2) | |
1029 { printf ("Error : channels = %d.\n", sfinfo.channels) ; | |
1030 continue ; | |
1031 } ; | |
1032 | |
1033 if ((hdl = sio_open (NULL, SIO_PLAY, 0)) == NULL) | |
1034 { fprintf (stderr, "open sndio device failed") ; | |
1035 return ; | |
1036 } ; | |
1037 | |
1038 sio_initpar (&par) ; | |
1039 par.rate = sfinfo.samplerate ; | |
1040 par.pchan = sfinfo.channels ; | |
1041 par.bits = 16 ; | |
1042 par.sig = 1 ; | |
1043 par.le = SIO_LE_NATIVE ; | |
1044 | |
1045 if (! sio_setpar (hdl, &par) || ! sio_getpar (hdl, &par)) | |
1046 { fprintf (stderr, "set sndio params failed") ; | |
1047 return ; | |
1048 } ; | |
1049 | |
1050 if (! sio_start (hdl)) | |
1051 { fprintf (stderr, "sndio start failed") ; | |
1052 return ; | |
1053 } ; | |
1054 | |
1055 while ((readcount = sf_read_short (sndfile, buffer, BUFFER_LEN))) | |
1056 sio_write (hdl, buffer, readcount * sizeof (short)) ; | |
1057 | |
1058 sio_close (hdl) ; | |
1059 } ; | |
1060 | |
1061 return ; | |
1062 } /* sndio_play */ | |
1063 | |
1064 #endif /* sndio */ | |
1065 | |
1066 /*------------------------------------------------------------------------------ | |
1067 ** Solaris. | |
1068 */ | |
1069 | |
1070 #if (defined (sun) && defined (unix)) /* ie Solaris */ | |
1071 | |
1072 static void | |
1073 solaris_play (int argc, char *argv []) | |
1074 { static short buffer [BUFFER_LEN] ; | |
1075 audio_info_t audio_info ; | |
1076 SNDFILE *sndfile ; | |
1077 SF_INFO sfinfo ; | |
1078 unsigned long delay_time ; | |
1079 long k, start_count, output_count, write_count, read_count ; | |
1080 int audio_fd, error, done ; | |
1081 | |
1082 for (k = 1 ; k < argc ; k++) | |
1083 { printf ("Playing %s\n", argv [k]) ; | |
1084 if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo))) | |
1085 { puts (sf_strerror (NULL)) ; | |
1086 continue ; | |
1087 } ; | |
1088 | |
1089 if (sfinfo.channels < 1 || sfinfo.channels > 2) | |
1090 { printf ("Error : channels = %d.\n", sfinfo.channels) ; | |
1091 continue ; | |
1092 } ; | |
1093 | |
1094 /* open the audio device - write only, non-blocking */ | |
1095 if ((audio_fd = open ("/dev/audio", O_WRONLY | O_NONBLOCK)) < 0) | |
1096 { perror ("open (/dev/audio) failed") ; | |
1097 return ; | |
1098 } ; | |
1099 | |
1100 /* Retrive standard values. */ | |
1101 AUDIO_INITINFO (&audio_info) ; | |
1102 | |
1103 audio_info.play.sample_rate = sfinfo.samplerate ; | |
1104 audio_info.play.channels = sfinfo.channels ; | |
1105 audio_info.play.precision = 16 ; | |
1106 audio_info.play.encoding = AUDIO_ENCODING_LINEAR ; | |
1107 audio_info.play.gain = AUDIO_MAX_GAIN ; | |
1108 audio_info.play.balance = AUDIO_MID_BALANCE ; | |
1109 | |
1110 if ((error = ioctl (audio_fd, AUDIO_SETINFO, &audio_info))) | |
1111 { perror ("ioctl (AUDIO_SETINFO) failed") ; | |
1112 return ; | |
1113 } ; | |
1114 | |
1115 /* Delay time equal to 1/4 of a buffer in microseconds. */ | |
1116 delay_time = (BUFFER_LEN * 1000000) / (audio_info.play.sample_rate * 4) ; | |
1117 | |
1118 done = 0 ; | |
1119 while (! done) | |
1120 { read_count = sf_read_short (sndfile, buffer, BUFFER_LEN) ; | |
1121 if (read_count < BUFFER_LEN) | |
1122 { memset (&(buffer [read_count]), 0, (BUFFER_LEN - read_count) * sizeof (short)) ; | |
1123 /* Tell the main application to terminate. */ | |
1124 done = SF_TRUE ; | |
1125 } ; | |
1126 | |
1127 start_count = 0 ; | |
1128 output_count = BUFFER_LEN * sizeof (short) ; | |
1129 | |
1130 while (output_count > 0) | |
1131 { /* write as much data as possible */ | |
1132 write_count = write (audio_fd, &(buffer [start_count]), output_count) ; | |
1133 if (write_count > 0) | |
1134 { output_count -= write_count ; | |
1135 start_count += write_count ; | |
1136 } | |
1137 else | |
1138 { /* Give the audio output time to catch up. */ | |
1139 usleep (delay_time) ; | |
1140 } ; | |
1141 } ; /* while (outpur_count > 0) */ | |
1142 } ; /* while (! done) */ | |
1143 | |
1144 close (audio_fd) ; | |
1145 } ; | |
1146 | |
1147 return ; | |
1148 } /* solaris_play */ | |
1149 | |
1150 #endif /* Solaris */ | |
1151 | |
1152 /*============================================================================== | |
1153 ** Main function. | |
1154 */ | |
1155 | |
1156 int | |
1157 main (int argc, char *argv []) | |
1158 { | |
1159 if (argc < 2) | |
1160 { | |
1161 printf ("\nUsage : %s <input sound file>\n\n", program_name (argv [0])) ; | |
1162 printf ("Using %s.\n\n", sf_version_string ()) ; | |
1163 #if (OS_IS_WIN32 == 1) | |
1164 printf ("This is a Unix style command line application which\n" | |
1165 "should be run in a MSDOS box or Command Shell window.\n\n") ; | |
1166 printf ("Sleeping for 5 seconds before exiting.\n\n") ; | |
1167 | |
1168 Sleep (5 * 1000) ; | |
1169 #endif | |
1170 return 1 ; | |
1171 } ; | |
1172 | |
1173 #if defined (__ANDROID__) | |
1174 puts ("*** Playing sound not yet supported on Android.") ; | |
1175 puts ("*** Please feel free to submit a patch.") ; | |
1176 return 1 ; | |
1177 #elif defined (__linux__) | |
1178 #if HAVE_ALSA_ASOUNDLIB_H | |
1179 if (access ("/proc/asound/cards", R_OK) == 0) | |
1180 alsa_play (argc, argv) ; | |
1181 else | |
1182 #endif | |
1183 opensoundsys_play (argc, argv) ; | |
1184 #elif defined (__FreeBSD_kernel__) || defined (__FreeBSD__) | |
1185 opensoundsys_play (argc, argv) ; | |
1186 #elif (defined (__MACH__) && defined (__APPLE__) && OSX_DARWIN_VERSION <= 11) | |
1187 macosx_play (argc, argv) ; | |
1188 #elif HAVE_SNDIO_H | |
1189 sndio_play (argc, argv) ; | |
1190 #elif (defined (sun) && defined (unix)) | |
1191 solaris_play (argc, argv) ; | |
1192 #elif (OS_IS_WIN32 == 1) | |
1193 win32_play (argc, argv) ; | |
1194 #elif (defined (__MACH__) && defined (__APPLE__) && OSX_DARWIN_VERSION > 11) | |
1195 printf ("OS X 10.8 and later have a new Audio API.\n") ; | |
1196 printf ("Someone needs to write code to use that API.\n") ; | |
1197 return 1 ; | |
1198 #elif defined (__BEOS__) | |
1199 printf ("This program cannot be compiled on BeOS.\n") ; | |
1200 printf ("Instead, compile the file sfplay_beos.cpp.\n") ; | |
1201 return 1 ; | |
1202 #else | |
1203 puts ("*** Playing sound not yet supported on this platform.") ; | |
1204 puts ("*** Please feel free to submit a patch.") ; | |
1205 return 1 ; | |
1206 #endif | |
1207 | |
1208 return 0 ; | |
1209 } /* main */ | |
1210 |