2
0

sndfile-play.c 34 KB

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