annotate src/libsamplerate-0.1.8/examples/audio_out.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 545efbb81310
children
rev   line source
cannam@85 1 /*
cannam@85 2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@85 3 **
cannam@85 4 ** This program is free software; you can redistribute it and/or modify
cannam@85 5 ** it under the terms of the GNU General Public License as published by
cannam@85 6 ** the Free Software Foundation; either version 2 of the License, or
cannam@85 7 ** (at your option) any later version.
cannam@85 8 **
cannam@85 9 ** This program is distributed in the hope that it will be useful,
cannam@85 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@85 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@85 12 ** GNU General Public License for more details.
cannam@85 13 **
cannam@85 14 ** You should have received a copy of the GNU General Public License
cannam@85 15 ** along with this program; if not, write to the Free Software
cannam@85 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cannam@85 17 */
cannam@85 18
cannam@85 19 #include <stdio.h>
cannam@85 20 #include <stdlib.h>
cannam@85 21 #include <string.h>
cannam@85 22 #include <unistd.h>
cannam@85 23
cannam@85 24 #include <config.h>
cannam@85 25
cannam@85 26 #include "audio_out.h"
cannam@85 27
cannam@85 28 #if (HAVE_SNDFILE)
cannam@85 29
cannam@85 30 #include <float_cast.h>
cannam@85 31
cannam@85 32 #include <sndfile.h>
cannam@85 33
cannam@85 34 #define BUFFER_LEN (2048)
cannam@85 35
cannam@85 36 #define MAKE_MAGIC(a,b,c,d,e,f,g,h) \
cannam@85 37 ((a) + ((b) << 1) + ((c) << 2) + ((d) << 3) + ((e) << 4) + ((f) << 5) + ((g) << 6) + ((h) << 7))
cannam@85 38
cannam@85 39 /*------------------------------------------------------------------------------
cannam@85 40 ** Linux/OSS functions for playing a sound.
cannam@85 41 */
cannam@85 42
cannam@85 43 #if defined (__linux__)
cannam@85 44
cannam@85 45 #include <fcntl.h>
cannam@85 46 #include <sys/ioctl.h>
cannam@85 47 #include <sys/soundcard.h>
cannam@85 48
cannam@85 49 #define LINUX_MAGIC MAKE_MAGIC ('L', 'i', 'n', 'u', 'x', 'O', 'S', 'S')
cannam@85 50
cannam@85 51 typedef struct
cannam@85 52 { int magic ;
cannam@85 53 int fd ;
cannam@85 54 int channels ;
cannam@85 55 } LINUX_AUDIO_OUT ;
cannam@85 56
cannam@85 57 static AUDIO_OUT *linux_open (int channels, int samplerate) ;
cannam@85 58 static void linux_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) ;
cannam@85 59 static void linux_close (AUDIO_OUT *audio_out) ;
cannam@85 60
cannam@85 61
cannam@85 62 static AUDIO_OUT *
cannam@85 63 linux_open (int channels, int samplerate)
cannam@85 64 { LINUX_AUDIO_OUT *linux_out ;
cannam@85 65 int stereo, fmt, error ;
cannam@85 66
cannam@85 67 if ((linux_out = malloc (sizeof (LINUX_AUDIO_OUT))) == NULL)
cannam@85 68 { perror ("linux_open : malloc ") ;
cannam@85 69 exit (1) ;
cannam@85 70 } ;
cannam@85 71
cannam@85 72 linux_out->magic = LINUX_MAGIC ;
cannam@85 73 linux_out->channels = channels ;
cannam@85 74
cannam@85 75 if ((linux_out->fd = open ("/dev/dsp", O_WRONLY, 0)) == -1)
cannam@85 76 { perror ("linux_open : open ") ;
cannam@85 77 exit (1) ;
cannam@85 78 } ;
cannam@85 79
cannam@85 80 stereo = 0 ;
cannam@85 81 if (ioctl (linux_out->fd, SNDCTL_DSP_STEREO, &stereo) == -1)
cannam@85 82 { /* Fatal error */
cannam@85 83 perror ("linux_open : stereo ") ;
cannam@85 84 exit (1) ;
cannam@85 85 } ;
cannam@85 86
cannam@85 87 if (ioctl (linux_out->fd, SNDCTL_DSP_RESET, 0))
cannam@85 88 { perror ("linux_open : reset ") ;
cannam@85 89 exit (1) ;
cannam@85 90 } ;
cannam@85 91
cannam@85 92 fmt = CPU_IS_BIG_ENDIAN ? AFMT_S16_BE : AFMT_S16_LE ;
cannam@85 93 if (ioctl (linux_out->fd, SNDCTL_DSP_SETFMT, &fmt) != 0)
cannam@85 94 { perror ("linux_open_dsp_device : set format ") ;
cannam@85 95 exit (1) ;
cannam@85 96 } ;
cannam@85 97
cannam@85 98 if ((error = ioctl (linux_out->fd, SNDCTL_DSP_CHANNELS, &channels)) != 0)
cannam@85 99 { perror ("linux_open : channels ") ;
cannam@85 100 exit (1) ;
cannam@85 101 } ;
cannam@85 102
cannam@85 103 if ((error = ioctl (linux_out->fd, SNDCTL_DSP_SPEED, &samplerate)) != 0)
cannam@85 104 { perror ("linux_open : sample rate ") ;
cannam@85 105 exit (1) ;
cannam@85 106 } ;
cannam@85 107
cannam@85 108 if ((error = ioctl (linux_out->fd, SNDCTL_DSP_SYNC, 0)) != 0)
cannam@85 109 { perror ("linux_open : sync ") ;
cannam@85 110 exit (1) ;
cannam@85 111 } ;
cannam@85 112
cannam@85 113 return (AUDIO_OUT*) linux_out ;
cannam@85 114 } /* linux_open */
cannam@85 115
cannam@85 116 static void
cannam@85 117 linux_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data)
cannam@85 118 { LINUX_AUDIO_OUT *linux_out ;
cannam@85 119 static float float_buffer [BUFFER_LEN] ;
cannam@85 120 static short buffer [BUFFER_LEN] ;
cannam@85 121 int k, readcount ;
cannam@85 122
cannam@85 123 if ((linux_out = (LINUX_AUDIO_OUT*) audio_out) == NULL)
cannam@85 124 { printf ("linux_play : AUDIO_OUT is NULL.\n") ;
cannam@85 125 return ;
cannam@85 126 } ;
cannam@85 127
cannam@85 128 if (linux_out->magic != LINUX_MAGIC)
cannam@85 129 { printf ("linux_play : Bad magic number.\n") ;
cannam@85 130 return ;
cannam@85 131 } ;
cannam@85 132
cannam@85 133 while ((readcount = callback (callback_data, float_buffer, BUFFER_LEN / linux_out->channels)))
cannam@85 134 { for (k = 0 ; k < readcount * linux_out->channels ; k++)
cannam@85 135 buffer [k] = lrint (32767.0 * float_buffer [k]) ;
cannam@85 136 (void) write (linux_out->fd, buffer, readcount * linux_out->channels * sizeof (short)) ;
cannam@85 137 } ;
cannam@85 138
cannam@85 139 return ;
cannam@85 140 } /* linux_play */
cannam@85 141
cannam@85 142 static void
cannam@85 143 linux_close (AUDIO_OUT *audio_out)
cannam@85 144 { LINUX_AUDIO_OUT *linux_out ;
cannam@85 145
cannam@85 146 if ((linux_out = (LINUX_AUDIO_OUT*) audio_out) == NULL)
cannam@85 147 { printf ("linux_close : AUDIO_OUT is NULL.\n") ;
cannam@85 148 return ;
cannam@85 149 } ;
cannam@85 150
cannam@85 151 if (linux_out->magic != LINUX_MAGIC)
cannam@85 152 { printf ("linux_close : Bad magic number.\n") ;
cannam@85 153 return ;
cannam@85 154 } ;
cannam@85 155
cannam@85 156 memset (linux_out, 0, sizeof (LINUX_AUDIO_OUT)) ;
cannam@85 157
cannam@85 158 free (linux_out) ;
cannam@85 159
cannam@85 160 return ;
cannam@85 161 } /* linux_close */
cannam@85 162
cannam@85 163 #endif /* __linux__ */
cannam@85 164
cannam@85 165 /*------------------------------------------------------------------------------
cannam@85 166 ** Mac OS X functions for playing a sound.
cannam@85 167 */
cannam@85 168
cannam@85 169 #if (defined (__MACH__) && defined (__APPLE__)) /* MacOSX */
cannam@85 170
cannam@85 171 #include <Carbon.h>
cannam@85 172 #include <CoreAudio/AudioHardware.h>
cannam@85 173
cannam@85 174 #define MACOSX_MAGIC MAKE_MAGIC ('M', 'a', 'c', ' ', 'O', 'S', ' ', 'X')
cannam@85 175
cannam@85 176 typedef struct
cannam@85 177 { int magic ;
cannam@85 178 AudioStreamBasicDescription format ;
cannam@85 179
cannam@85 180 UInt32 buf_size ;
cannam@85 181 AudioDeviceID device ;
cannam@85 182
cannam@85 183 int channels ;
cannam@85 184 int samplerate ;
cannam@85 185 int buffer_size ;
cannam@85 186 int done_playing ;
cannam@85 187
cannam@85 188 get_audio_callback_t callback ;
cannam@85 189
cannam@85 190 void *callback_data ;
cannam@85 191 } MACOSX_AUDIO_OUT ;
cannam@85 192
cannam@85 193 static AUDIO_OUT *macosx_open (int channels, int samplerate) ;
cannam@85 194 static void macosx_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) ;
cannam@85 195 static void macosx_close (AUDIO_OUT *audio_out) ;
cannam@85 196
cannam@85 197 static OSStatus
cannam@85 198 macosx_audio_out_callback (AudioDeviceID device, const AudioTimeStamp* current_time,
cannam@85 199 const AudioBufferList* data_in, const AudioTimeStamp* time_in,
cannam@85 200 AudioBufferList* data_out, const AudioTimeStamp* time_out, void* client_data) ;
cannam@85 201
cannam@85 202
cannam@85 203 static AUDIO_OUT *
cannam@85 204 macosx_open (int channels, int samplerate)
cannam@85 205 { MACOSX_AUDIO_OUT *macosx_out ;
cannam@85 206 OSStatus err ;
cannam@85 207 size_t count ;
cannam@85 208
cannam@85 209 if ((macosx_out = malloc (sizeof (MACOSX_AUDIO_OUT))) == NULL)
cannam@85 210 { perror ("macosx_open : malloc ") ;
cannam@85 211 exit (1) ;
cannam@85 212 } ;
cannam@85 213
cannam@85 214 macosx_out->magic = MACOSX_MAGIC ;
cannam@85 215 macosx_out->channels = channels ;
cannam@85 216 macosx_out->samplerate = samplerate ;
cannam@85 217
cannam@85 218 macosx_out->device = kAudioDeviceUnknown ;
cannam@85 219
cannam@85 220 /* get the default output device for the HAL */
cannam@85 221 count = sizeof (AudioDeviceID) ;
cannam@85 222 if ((err = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultOutputDevice,
cannam@85 223 &count, (void *) &(macosx_out->device))) != noErr)
cannam@85 224 { printf ("AudioHardwareGetProperty failed.\n") ;
cannam@85 225 free (macosx_out) ;
cannam@85 226 return NULL ;
cannam@85 227 } ;
cannam@85 228
cannam@85 229 /* get the buffersize that the default device uses for IO */
cannam@85 230 count = sizeof (UInt32) ;
cannam@85 231 if ((err = AudioDeviceGetProperty (macosx_out->device, 0, false, kAudioDevicePropertyBufferSize,
cannam@85 232 &count, &(macosx_out->buffer_size))) != noErr)
cannam@85 233 { printf ("AudioDeviceGetProperty (AudioDeviceGetProperty) failed.\n") ;
cannam@85 234 free (macosx_out) ;
cannam@85 235 return NULL ;
cannam@85 236 } ;
cannam@85 237
cannam@85 238 /* get a description of the data format used by the default device */
cannam@85 239 count = sizeof (AudioStreamBasicDescription) ;
cannam@85 240 if ((err = AudioDeviceGetProperty (macosx_out->device, 0, false, kAudioDevicePropertyStreamFormat,
cannam@85 241 &count, &(macosx_out->format))) != noErr)
cannam@85 242 { printf ("AudioDeviceGetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ;
cannam@85 243 free (macosx_out) ;
cannam@85 244 return NULL ;
cannam@85 245 } ;
cannam@85 246
cannam@85 247 macosx_out->format.mSampleRate = samplerate ;
cannam@85 248 macosx_out->format.mChannelsPerFrame = channels ;
cannam@85 249
cannam@85 250 if ((err = AudioDeviceSetProperty (macosx_out->device, NULL, 0, false, kAudioDevicePropertyStreamFormat,
cannam@85 251 sizeof (AudioStreamBasicDescription), &(macosx_out->format))) != noErr)
cannam@85 252 { printf ("AudioDeviceSetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ;
cannam@85 253 free (macosx_out) ;
cannam@85 254 return NULL ;
cannam@85 255 } ;
cannam@85 256
cannam@85 257 /* we want linear pcm */
cannam@85 258 if (macosx_out->format.mFormatID != kAudioFormatLinearPCM)
cannam@85 259 { free (macosx_out) ;
cannam@85 260 return NULL ;
cannam@85 261 } ;
cannam@85 262
cannam@85 263 macosx_out->done_playing = 0 ;
cannam@85 264
cannam@85 265 /* Fire off the device. */
cannam@85 266 if ((err = AudioDeviceAddIOProc (macosx_out->device, macosx_audio_out_callback,
cannam@85 267 (void *) macosx_out)) != noErr)
cannam@85 268 { printf ("AudioDeviceAddIOProc failed.\n") ;
cannam@85 269 free (macosx_out) ;
cannam@85 270 return NULL ;
cannam@85 271 } ;
cannam@85 272
cannam@85 273 return (MACOSX_AUDIO_OUT *) macosx_out ;
cannam@85 274 } /* macosx_open */
cannam@85 275
cannam@85 276 static void
cannam@85 277 macosx_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data)
cannam@85 278 { MACOSX_AUDIO_OUT *macosx_out ;
cannam@85 279 OSStatus err ;
cannam@85 280
cannam@85 281 if ((macosx_out = (MACOSX_AUDIO_OUT*) audio_out) == NULL)
cannam@85 282 { printf ("macosx_play : AUDIO_OUT is NULL.\n") ;
cannam@85 283 return ;
cannam@85 284 } ;
cannam@85 285
cannam@85 286 if (macosx_out->magic != MACOSX_MAGIC)
cannam@85 287 { printf ("macosx_play : Bad magic number.\n") ;
cannam@85 288 return ;
cannam@85 289 } ;
cannam@85 290
cannam@85 291 /* Set the callback function and callback data. */
cannam@85 292 macosx_out->callback = callback ;
cannam@85 293 macosx_out->callback_data = callback_data ;
cannam@85 294
cannam@85 295 err = AudioDeviceStart (macosx_out->device, macosx_audio_out_callback) ;
cannam@85 296 if (err != noErr)
cannam@85 297 printf ("AudioDeviceStart failed.\n") ;
cannam@85 298
cannam@85 299 while (macosx_out->done_playing == SF_FALSE)
cannam@85 300 usleep (10 * 1000) ; /* 10 000 milliseconds. */
cannam@85 301
cannam@85 302 return ;
cannam@85 303 } /* macosx_play */
cannam@85 304
cannam@85 305 static void
cannam@85 306 macosx_close (AUDIO_OUT *audio_out)
cannam@85 307 { MACOSX_AUDIO_OUT *macosx_out ;
cannam@85 308 OSStatus err ;
cannam@85 309
cannam@85 310 if ((macosx_out = (MACOSX_AUDIO_OUT*) audio_out) == NULL)
cannam@85 311 { printf ("macosx_close : AUDIO_OUT is NULL.\n") ;
cannam@85 312 return ;
cannam@85 313 } ;
cannam@85 314
cannam@85 315 if (macosx_out->magic != MACOSX_MAGIC)
cannam@85 316 { printf ("macosx_close : Bad magic number.\n") ;
cannam@85 317 return ;
cannam@85 318 } ;
cannam@85 319
cannam@85 320
cannam@85 321 if ((err = AudioDeviceStop (macosx_out->device, macosx_audio_out_callback)) != noErr)
cannam@85 322 { printf ("AudioDeviceStop failed.\n") ;
cannam@85 323 return ;
cannam@85 324 } ;
cannam@85 325
cannam@85 326 err = AudioDeviceRemoveIOProc (macosx_out->device, macosx_audio_out_callback) ;
cannam@85 327 if (err != noErr)
cannam@85 328 { printf ("AudioDeviceRemoveIOProc failed.\n") ;
cannam@85 329 return ;
cannam@85 330 } ;
cannam@85 331
cannam@85 332 } /* macosx_close */
cannam@85 333
cannam@85 334 static OSStatus
cannam@85 335 macosx_audio_out_callback (AudioDeviceID device, const AudioTimeStamp* current_time,
cannam@85 336 const AudioBufferList* data_in, const AudioTimeStamp* time_in,
cannam@85 337 AudioBufferList* data_out, const AudioTimeStamp* time_out, void* client_data)
cannam@85 338 { MACOSX_AUDIO_OUT *macosx_out ;
cannam@85 339 int k, size, sample_count, read_count ;
cannam@85 340 float *buffer ;
cannam@85 341
cannam@85 342 if ((macosx_out = (MACOSX_AUDIO_OUT*) client_data) == NULL)
cannam@85 343 { printf ("macosx_play : AUDIO_OUT is NULL.\n") ;
cannam@85 344 return 42 ;
cannam@85 345 } ;
cannam@85 346
cannam@85 347 if (macosx_out->magic != MACOSX_MAGIC)
cannam@85 348 { printf ("macosx_play : Bad magic number.\n") ;
cannam@85 349 return 42 ;
cannam@85 350 } ;
cannam@85 351
cannam@85 352 size = data_out->mBuffers [0].mDataByteSize ;
cannam@85 353 sample_count = size / sizeof (float) / macosx_out->channels ;
cannam@85 354
cannam@85 355 buffer = (float*) data_out->mBuffers [0].mData ;
cannam@85 356
cannam@85 357 read_count = macosx_out->callback (macosx_out->callback_data, buffer, sample_count) ;
cannam@85 358
cannam@85 359 if (read_count < sample_count)
cannam@85 360 { memset (&(buffer [read_count]), 0, (sample_count - read_count) * sizeof (float)) ;
cannam@85 361 macosx_out->done_playing = 1 ;
cannam@85 362 } ;
cannam@85 363
cannam@85 364 return noErr ;
cannam@85 365 } /* macosx_audio_out_callback */
cannam@85 366
cannam@85 367 #endif /* MacOSX */
cannam@85 368
cannam@85 369
cannam@85 370 /*------------------------------------------------------------------------------
cannam@85 371 ** Win32 functions for playing a sound.
cannam@85 372 **
cannam@85 373 ** This API sucks. Its needlessly complicated and is *WAY* too loose with
cannam@85 374 ** passing pointers arounf in integers and and using char* pointers to
cannam@85 375 ** point to data instead of short*. It plain sucks!
cannam@85 376 */
cannam@85 377
cannam@85 378 #if (defined (_WIN32) || defined (WIN32))
cannam@85 379
cannam@85 380 #include <windows.h>
cannam@85 381 #include <mmsystem.h>
cannam@85 382
cannam@85 383 #define WIN32_BUFFER_LEN (1<<15)
cannam@85 384 #define WIN32_MAGIC MAKE_MAGIC ('W', 'i', 'n', '3', '2', 's', 'u', 'x')
cannam@85 385
cannam@85 386 typedef struct
cannam@85 387 { int magic ;
cannam@85 388
cannam@85 389 HWAVEOUT hwave ;
cannam@85 390 WAVEHDR whdr [2] ;
cannam@85 391
cannam@85 392 HANDLE Event ;
cannam@85 393
cannam@85 394 short short_buffer [WIN32_BUFFER_LEN / sizeof (short)] ;
cannam@85 395 float float_buffer [WIN32_BUFFER_LEN / sizeof (short) / 2] ;
cannam@85 396
cannam@85 397 int bufferlen, current ;
cannam@85 398
cannam@85 399 int channels ;
cannam@85 400
cannam@85 401 get_audio_callback_t callback ;
cannam@85 402
cannam@85 403 void *callback_data ;
cannam@85 404 } WIN32_AUDIO_OUT ;
cannam@85 405
cannam@85 406 static AUDIO_OUT *win32_open (int channels, int samplerate) ;
cannam@85 407 static void win32_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) ;
cannam@85 408 static void win32_close (AUDIO_OUT *audio_out) ;
cannam@85 409
cannam@85 410 static DWORD CALLBACK
cannam@85 411 win32_audio_out_callback (HWAVEOUT hwave, UINT msg, DWORD data, DWORD param1, DWORD param2) ;
cannam@85 412
cannam@85 413 static AUDIO_OUT*
cannam@85 414 win32_open (int channels, int samplerate)
cannam@85 415 { WIN32_AUDIO_OUT *win32_out ;
cannam@85 416
cannam@85 417 WAVEFORMATEX wf ;
cannam@85 418 int error ;
cannam@85 419
cannam@85 420 if ((win32_out = malloc (sizeof (WIN32_AUDIO_OUT))) == NULL)
cannam@85 421 { perror ("win32_open : malloc ") ;
cannam@85 422 exit (1) ;
cannam@85 423 } ;
cannam@85 424
cannam@85 425 win32_out->magic = WIN32_MAGIC ;
cannam@85 426 win32_out->channels = channels ;
cannam@85 427
cannam@85 428 win32_out->current = 0 ;
cannam@85 429
cannam@85 430 win32_out->Event = CreateEvent (0, FALSE, FALSE, 0) ;
cannam@85 431
cannam@85 432 wf.nChannels = channels ;
cannam@85 433 wf.nSamplesPerSec = samplerate ;
cannam@85 434 wf.nBlockAlign = channels * sizeof (short) ;
cannam@85 435
cannam@85 436 wf.wFormatTag = WAVE_FORMAT_PCM ;
cannam@85 437 wf.cbSize = 0 ;
cannam@85 438 wf.wBitsPerSample = 16 ;
cannam@85 439 wf.nAvgBytesPerSec = wf.nBlockAlign * wf.nSamplesPerSec ;
cannam@85 440
cannam@85 441 error = waveOutOpen (&(win32_out->hwave), WAVE_MAPPER, &wf, (DWORD) win32_audio_out_callback,
cannam@85 442 (DWORD) win32_out, CALLBACK_FUNCTION) ;
cannam@85 443 if (error)
cannam@85 444 { puts ("waveOutOpen failed.") ;
cannam@85 445 free (win32_out) ;
cannam@85 446 return NULL ;
cannam@85 447 } ;
cannam@85 448
cannam@85 449 waveOutPause (win32_out->hwave) ;
cannam@85 450
cannam@85 451 return (WIN32_AUDIO_OUT *) win32_out ;
cannam@85 452 } /* win32_open */
cannam@85 453
cannam@85 454 static void
cannam@85 455 win32_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data)
cannam@85 456 { WIN32_AUDIO_OUT *win32_out ;
cannam@85 457 int error ;
cannam@85 458
cannam@85 459 if ((win32_out = (WIN32_AUDIO_OUT*) audio_out) == NULL)
cannam@85 460 { printf ("win32_play : AUDIO_OUT is NULL.\n") ;
cannam@85 461 return ;
cannam@85 462 } ;
cannam@85 463
cannam@85 464 if (win32_out->magic != WIN32_MAGIC)
cannam@85 465 { printf ("win32_play : Bad magic number (%d %d).\n", win32_out->magic, WIN32_MAGIC) ;
cannam@85 466 return ;
cannam@85 467 } ;
cannam@85 468
cannam@85 469 /* Set the callback function and callback data. */
cannam@85 470 win32_out->callback = callback ;
cannam@85 471 win32_out->callback_data = callback_data ;
cannam@85 472
cannam@85 473 win32_out->whdr [0].lpData = (char*) win32_out->short_buffer ;
cannam@85 474 win32_out->whdr [1].lpData = ((char*) win32_out->short_buffer) + sizeof (win32_out->short_buffer) / 2 ;
cannam@85 475
cannam@85 476 win32_out->whdr [0].dwBufferLength = sizeof (win32_out->short_buffer) / 2 ;
cannam@85 477 win32_out->whdr [1].dwBufferLength = sizeof (win32_out->short_buffer) / 2 ;
cannam@85 478
cannam@85 479 win32_out->bufferlen = sizeof (win32_out->short_buffer) / 2 / sizeof (short) ;
cannam@85 480
cannam@85 481 /* Prepare the WAVEHDRs */
cannam@85 482 if ((error = waveOutPrepareHeader (win32_out->hwave, &(win32_out->whdr [0]), sizeof (WAVEHDR))))
cannam@85 483 { printf ("waveOutPrepareHeader [0] failed : %08X\n", error) ;
cannam@85 484 waveOutClose (win32_out->hwave) ;
cannam@85 485 return ;
cannam@85 486 } ;
cannam@85 487
cannam@85 488 if ((error = waveOutPrepareHeader (win32_out->hwave, &(win32_out->whdr [1]), sizeof (WAVEHDR))))
cannam@85 489 { printf ("waveOutPrepareHeader [1] failed : %08X\n", error) ;
cannam@85 490 waveOutUnprepareHeader (win32_out->hwave, &(win32_out->whdr [0]), sizeof (WAVEHDR)) ;
cannam@85 491 waveOutClose (win32_out->hwave) ;
cannam@85 492 return ;
cannam@85 493 } ;
cannam@85 494
cannam@85 495 waveOutRestart (win32_out->hwave) ;
cannam@85 496
cannam@85 497 /* Fake 2 calls to the callback function to queue up enough audio. */
cannam@85 498 win32_audio_out_callback (0, MM_WOM_DONE, (DWORD) win32_out, 0, 0) ;
cannam@85 499 win32_audio_out_callback (0, MM_WOM_DONE, (DWORD) win32_out, 0, 0) ;
cannam@85 500
cannam@85 501 /* Wait for playback to finish. The callback notifies us when all
cannam@85 502 ** wave data has been played.
cannam@85 503 */
cannam@85 504 WaitForSingleObject (win32_out->Event, INFINITE) ;
cannam@85 505
cannam@85 506 waveOutPause (win32_out->hwave) ;
cannam@85 507 waveOutReset (win32_out->hwave) ;
cannam@85 508
cannam@85 509 waveOutUnprepareHeader (win32_out->hwave, &(win32_out->whdr [0]), sizeof (WAVEHDR)) ;
cannam@85 510 waveOutUnprepareHeader (win32_out->hwave, &(win32_out->whdr [1]), sizeof (WAVEHDR)) ;
cannam@85 511
cannam@85 512 waveOutClose (win32_out->hwave) ;
cannam@85 513 win32_out->hwave = 0 ;
cannam@85 514
cannam@85 515 return ;
cannam@85 516 } /* win32_play */
cannam@85 517
cannam@85 518 static void
cannam@85 519 win32_close (AUDIO_OUT *audio_out)
cannam@85 520 { WIN32_AUDIO_OUT *win32_out ;
cannam@85 521
cannam@85 522 if ((win32_out = (WIN32_AUDIO_OUT*) audio_out) == NULL)
cannam@85 523 { printf ("win32_close : AUDIO_OUT is NULL.\n") ;
cannam@85 524 return ;
cannam@85 525 } ;
cannam@85 526
cannam@85 527 if (win32_out->magic != WIN32_MAGIC)
cannam@85 528 { printf ("win32_close : Bad magic number.\n") ;
cannam@85 529 return ;
cannam@85 530 } ;
cannam@85 531
cannam@85 532 memset (win32_out, 0, sizeof (WIN32_AUDIO_OUT)) ;
cannam@85 533
cannam@85 534 free (win32_out) ;
cannam@85 535 } /* win32_close */
cannam@85 536
cannam@85 537 static DWORD CALLBACK
cannam@85 538 win32_audio_out_callback (HWAVEOUT hwave, UINT msg, DWORD data, DWORD param1, DWORD param2)
cannam@85 539 { WIN32_AUDIO_OUT *win32_out ;
cannam@85 540 int read_count, sample_count, k ;
cannam@85 541 short *sptr ;
cannam@85 542
cannam@85 543 /*
cannam@85 544 ** I consider this technique of passing a pointer via an integer as
cannam@85 545 ** fundamentally broken but thats the way microsoft has defined the
cannam@85 546 ** interface.
cannam@85 547 */
cannam@85 548 if ((win32_out = (WIN32_AUDIO_OUT*) data) == NULL)
cannam@85 549 { printf ("win32_audio_out_callback : AUDIO_OUT is NULL.\n") ;
cannam@85 550 return 1 ;
cannam@85 551 } ;
cannam@85 552
cannam@85 553 if (win32_out->magic != WIN32_MAGIC)
cannam@85 554 { printf ("win32_audio_out_callback : Bad magic number (%d %d).\n", win32_out->magic, WIN32_MAGIC) ;
cannam@85 555 return 1 ;
cannam@85 556 } ;
cannam@85 557
cannam@85 558 if (msg != MM_WOM_DONE)
cannam@85 559 return 0 ;
cannam@85 560
cannam@85 561 /* Do the actual audio. */
cannam@85 562 sample_count = win32_out->bufferlen ;
cannam@85 563
cannam@85 564 read_count = win32_out->callback (win32_out->callback_data, win32_out->float_buffer, sample_count) ;
cannam@85 565
cannam@85 566 sptr = (short*) win32_out->whdr [win32_out->current].lpData ;
cannam@85 567
cannam@85 568 for (k = 0 ; k < read_count ; k++)
cannam@85 569 sptr [k] = lrint (32767.0 * win32_out->float_buffer [k]) ;
cannam@85 570
cannam@85 571 if (read_count > 0)
cannam@85 572 { /* Fix buffer length is only a partial block. */
cannam@85 573 if (read_count * sizeof (short) < win32_out->bufferlen)
cannam@85 574 win32_out->whdr [win32_out->current].dwBufferLength = read_count * sizeof (short) ;
cannam@85 575
cannam@85 576 /* Queue the WAVEHDR */
cannam@85 577 waveOutWrite (win32_out->hwave, (LPWAVEHDR) &(win32_out->whdr [win32_out->current]), sizeof (WAVEHDR)) ;
cannam@85 578 }
cannam@85 579 else
cannam@85 580 { /* Stop playback */
cannam@85 581 waveOutPause (win32_out->hwave) ;
cannam@85 582
cannam@85 583 SetEvent (win32_out->Event) ;
cannam@85 584 } ;
cannam@85 585
cannam@85 586 win32_out->current = (win32_out->current + 1) % 2 ;
cannam@85 587
cannam@85 588 return 0 ;
cannam@85 589 } /* win32_audio_out_callback */
cannam@85 590
cannam@85 591 #endif /* Win32 */
cannam@85 592
cannam@85 593 /*------------------------------------------------------------------------------
cannam@85 594 ** Solaris.
cannam@85 595 */
cannam@85 596
cannam@85 597 #if (defined (sun) && defined (unix)) /* ie Solaris */
cannam@85 598
cannam@85 599 #include <fcntl.h>
cannam@85 600 #include <sys/ioctl.h>
cannam@85 601 #include <sys/audioio.h>
cannam@85 602
cannam@85 603 #define SOLARIS_MAGIC MAKE_MAGIC ('S', 'o', 'l', 'a', 'r', 'i', 's', ' ')
cannam@85 604
cannam@85 605 typedef struct
cannam@85 606 { int magic ;
cannam@85 607 int fd ;
cannam@85 608 int channels ;
cannam@85 609 int samplerate ;
cannam@85 610 } SOLARIS_AUDIO_OUT ;
cannam@85 611
cannam@85 612 static AUDIO_OUT *solaris_open (int channels, int samplerate) ;
cannam@85 613 static void solaris_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) ;
cannam@85 614 static void solaris_close (AUDIO_OUT *audio_out) ;
cannam@85 615
cannam@85 616 static AUDIO_OUT *
cannam@85 617 solaris_open (int channels, int samplerate)
cannam@85 618 { SOLARIS_AUDIO_OUT *solaris_out ;
cannam@85 619 audio_info_t audio_info ;
cannam@85 620 int error ;
cannam@85 621
cannam@85 622 if ((solaris_out = malloc (sizeof (SOLARIS_AUDIO_OUT))) == NULL)
cannam@85 623 { perror ("solaris_open : malloc ") ;
cannam@85 624 exit (1) ;
cannam@85 625 } ;
cannam@85 626
cannam@85 627 solaris_out->magic = SOLARIS_MAGIC ;
cannam@85 628 solaris_out->channels = channels ;
cannam@85 629 solaris_out->samplerate = channels ;
cannam@85 630
cannam@85 631 /* open the audio device - write only, non-blocking */
cannam@85 632 if ((solaris_out->fd = open ("/dev/audio", O_WRONLY | O_NONBLOCK)) < 0)
cannam@85 633 { perror ("open (/dev/audio) failed") ;
cannam@85 634 exit (1) ;
cannam@85 635 } ;
cannam@85 636
cannam@85 637 /* Retrive standard values. */
cannam@85 638 AUDIO_INITINFO (&audio_info) ;
cannam@85 639
cannam@85 640 audio_info.play.sample_rate = samplerate ;
cannam@85 641 audio_info.play.channels = channels ;
cannam@85 642 audio_info.play.precision = 16 ;
cannam@85 643 audio_info.play.encoding = AUDIO_ENCODING_LINEAR ;
cannam@85 644 audio_info.play.gain = AUDIO_MAX_GAIN ;
cannam@85 645 audio_info.play.balance = AUDIO_MID_BALANCE ;
cannam@85 646
cannam@85 647 if ((error = ioctl (solaris_out->fd, AUDIO_SETINFO, &audio_info)))
cannam@85 648 { perror ("ioctl (AUDIO_SETINFO) failed") ;
cannam@85 649 exit (1) ;
cannam@85 650 } ;
cannam@85 651
cannam@85 652 return (AUDIO_OUT*) solaris_out ;
cannam@85 653 } /* solaris_open */
cannam@85 654
cannam@85 655 static void
cannam@85 656 solaris_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data)
cannam@85 657 { SOLARIS_AUDIO_OUT *solaris_out ;
cannam@85 658 static float float_buffer [BUFFER_LEN] ;
cannam@85 659 static short buffer [BUFFER_LEN] ;
cannam@85 660 int k, readcount ;
cannam@85 661
cannam@85 662 if ((solaris_out = (SOLARIS_AUDIO_OUT*) audio_out) == NULL)
cannam@85 663 { printf ("solaris_play : AUDIO_OUT is NULL.\n") ;
cannam@85 664 return ;
cannam@85 665 } ;
cannam@85 666
cannam@85 667 if (solaris_out->magic != SOLARIS_MAGIC)
cannam@85 668 { printf ("solaris_play : Bad magic number.\n") ;
cannam@85 669 return ;
cannam@85 670 } ;
cannam@85 671
cannam@85 672 while ((readcount = callback (callback_data, float_buffer, BUFFER_LEN / solaris_out->channels)))
cannam@85 673 { for (k = 0 ; k < readcount * solaris_out->channels ; k++)
cannam@85 674 buffer [k] = lrint (32767.0 * float_buffer [k]) ;
cannam@85 675 write (solaris_out->fd, buffer, readcount * solaris_out->channels * sizeof (short)) ;
cannam@85 676 } ;
cannam@85 677
cannam@85 678 return ;
cannam@85 679 } /* solaris_play */
cannam@85 680
cannam@85 681 static void
cannam@85 682 solaris_close (AUDIO_OUT *audio_out)
cannam@85 683 { SOLARIS_AUDIO_OUT *solaris_out ;
cannam@85 684
cannam@85 685 if ((solaris_out = (SOLARIS_AUDIO_OUT*) audio_out) == NULL)
cannam@85 686 { printf ("solaris_close : AUDIO_OUT is NULL.\n") ;
cannam@85 687 return ;
cannam@85 688 } ;
cannam@85 689
cannam@85 690 if (solaris_out->magic != SOLARIS_MAGIC)
cannam@85 691 { printf ("solaris_close : Bad magic number.\n") ;
cannam@85 692 return ;
cannam@85 693 } ;
cannam@85 694
cannam@85 695 memset (solaris_out, 0, sizeof (SOLARIS_AUDIO_OUT)) ;
cannam@85 696
cannam@85 697 free (solaris_out) ;
cannam@85 698
cannam@85 699 return ;
cannam@85 700 } /* solaris_close */
cannam@85 701
cannam@85 702 #endif /* Solaris */
cannam@85 703
cannam@85 704 /*==============================================================================
cannam@85 705 ** Main function.
cannam@85 706 */
cannam@85 707
cannam@85 708 AUDIO_OUT *
cannam@85 709 audio_open (int channels, int samplerate)
cannam@85 710 {
cannam@85 711 #if defined (__linux__)
cannam@85 712 return linux_open (channels, samplerate) ;
cannam@85 713 #elif (defined (__MACH__) && defined (__APPLE__))
cannam@85 714 return macosx_open (channels, samplerate) ;
cannam@85 715 #elif (defined (sun) && defined (unix))
cannam@85 716 return solaris_open (channels, samplerate) ;
cannam@85 717 #elif (defined (_WIN32) || defined (WIN32))
cannam@85 718 return win32_open (channels, samplerate) ;
cannam@85 719 #else
cannam@85 720 #warning "*** Playing sound not yet supported on this platform."
cannam@85 721 #warning "*** Please feel free to submit a patch."
cannam@85 722 printf ("Error : Playing sound not yet supported on this platform.\n") ;
cannam@85 723 return NULL ;
cannam@85 724 #endif
cannam@85 725
cannam@85 726
cannam@85 727 return NULL ;
cannam@85 728 } /* audio_open */
cannam@85 729
cannam@85 730 void
cannam@85 731 audio_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data)
cannam@85 732 {
cannam@85 733
cannam@85 734 if (callback == NULL)
cannam@85 735 { printf ("Error : bad callback pointer.\n") ;
cannam@85 736 return ;
cannam@85 737 } ;
cannam@85 738
cannam@85 739 if (audio_out == NULL)
cannam@85 740 { printf ("Error : bad audio_out pointer.\n") ;
cannam@85 741 return ;
cannam@85 742 } ;
cannam@85 743
cannam@85 744 if (callback_data == NULL)
cannam@85 745 { printf ("Error : bad callback_data pointer.\n") ;
cannam@85 746 return ;
cannam@85 747 } ;
cannam@85 748
cannam@85 749 #if defined (__linux__)
cannam@85 750 linux_play (callback, audio_out, callback_data) ;
cannam@85 751 #elif (defined (__MACH__) && defined (__APPLE__))
cannam@85 752 macosx_play (callback, audio_out, callback_data) ;
cannam@85 753 #elif (defined (sun) && defined (unix))
cannam@85 754 solaris_play (callback, audio_out, callback_data) ;
cannam@85 755 #elif (defined (_WIN32) || defined (WIN32))
cannam@85 756 win32_play (callback, audio_out, callback_data) ;
cannam@85 757 #else
cannam@85 758 #warning "*** Playing sound not yet supported on this platform."
cannam@85 759 #warning "*** Please feel free to submit a patch."
cannam@85 760 printf ("Error : Playing sound not yet supported on this platform.\n") ;
cannam@85 761 return ;
cannam@85 762 #endif
cannam@85 763
cannam@85 764 return ;
cannam@85 765 } /* audio_play */
cannam@85 766
cannam@85 767 void
cannam@85 768 audio_close (AUDIO_OUT *audio_out)
cannam@85 769 {
cannam@85 770 #if defined (__linux__)
cannam@85 771 linux_close (audio_out) ;
cannam@85 772 #elif (defined (__MACH__) && defined (__APPLE__))
cannam@85 773 macosx_close (audio_out) ;
cannam@85 774 #elif (defined (sun) && defined (unix))
cannam@85 775 solaris_close (audio_out) ;
cannam@85 776 #elif (defined (_WIN32) || defined (WIN32))
cannam@85 777 win32_close (audio_out) ;
cannam@85 778 #else
cannam@85 779 #warning "*** Playing sound not yet supported on this platform."
cannam@85 780 #warning "*** Please feel free to submit a patch."
cannam@85 781 printf ("Error : Playing sound not yet supported on this platform.\n") ;
cannam@85 782 return ;
cannam@85 783 #endif
cannam@85 784
cannam@85 785 return ;
cannam@85 786 } /* audio_close */
cannam@85 787
cannam@85 788 #else /* (HAVE_SNDFILE == 0) */
cannam@85 789
cannam@85 790 /* Do not have libsndfile installed so just return. */
cannam@85 791
cannam@85 792 AUDIO_OUT *
cannam@85 793 audio_open (int channels, int samplerate)
cannam@85 794 {
cannam@85 795 (void) channels ;
cannam@85 796 (void) samplerate ;
cannam@85 797
cannam@85 798 return NULL ;
cannam@85 799 } /* audio_open */
cannam@85 800
cannam@85 801 void
cannam@85 802 audio_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data)
cannam@85 803 {
cannam@85 804 (void) callback ;
cannam@85 805 (void) audio_out ;
cannam@85 806 (void) callback_data ;
cannam@85 807
cannam@85 808 return ;
cannam@85 809 } /* audio_play */
cannam@85 810
cannam@85 811 void
cannam@85 812 audio_close (AUDIO_OUT *audio_out)
cannam@85 813 {
cannam@85 814 audio_out = audio_out ;
cannam@85 815
cannam@85 816 return ;
cannam@85 817 } /* audio_close */
cannam@85 818
cannam@85 819 #endif /* HAVE_SNDFILE */
cannam@85 820