cannam@85: /* cannam@85: ** Copyright (C) 1999-2011 Erik de Castro Lopo cannam@85: ** cannam@85: ** This program is free software; you can redistribute it and/or modify cannam@85: ** it under the terms of the GNU General Public License as published by cannam@85: ** the Free Software Foundation; either version 2 of the License, or cannam@85: ** (at your option) any later version. cannam@85: ** cannam@85: ** This program is distributed in the hope that it will be useful, cannam@85: ** but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@85: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@85: ** GNU General Public License for more details. cannam@85: ** cannam@85: ** You should have received a copy of the GNU General Public License cannam@85: ** along with this program; if not, write to the Free Software cannam@85: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cannam@85: */ cannam@85: cannam@85: #include cannam@85: #include cannam@85: #include cannam@85: #include cannam@85: cannam@85: #include cannam@85: cannam@85: #include "audio_out.h" cannam@85: cannam@85: #if (HAVE_SNDFILE) cannam@85: cannam@85: #include cannam@85: cannam@85: #include cannam@85: cannam@85: #define BUFFER_LEN (2048) cannam@85: cannam@85: #define MAKE_MAGIC(a,b,c,d,e,f,g,h) \ cannam@85: ((a) + ((b) << 1) + ((c) << 2) + ((d) << 3) + ((e) << 4) + ((f) << 5) + ((g) << 6) + ((h) << 7)) cannam@85: cannam@85: /*------------------------------------------------------------------------------ cannam@85: ** Linux/OSS functions for playing a sound. cannam@85: */ cannam@85: cannam@85: #if defined (__linux__) cannam@85: cannam@85: #include cannam@85: #include cannam@85: #include cannam@85: cannam@85: #define LINUX_MAGIC MAKE_MAGIC ('L', 'i', 'n', 'u', 'x', 'O', 'S', 'S') cannam@85: cannam@85: typedef struct cannam@85: { int magic ; cannam@85: int fd ; cannam@85: int channels ; cannam@85: } LINUX_AUDIO_OUT ; cannam@85: cannam@85: static AUDIO_OUT *linux_open (int channels, int samplerate) ; cannam@85: static void linux_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) ; cannam@85: static void linux_close (AUDIO_OUT *audio_out) ; cannam@85: cannam@85: cannam@85: static AUDIO_OUT * cannam@85: linux_open (int channels, int samplerate) cannam@85: { LINUX_AUDIO_OUT *linux_out ; cannam@85: int stereo, fmt, error ; cannam@85: cannam@85: if ((linux_out = malloc (sizeof (LINUX_AUDIO_OUT))) == NULL) cannam@85: { perror ("linux_open : malloc ") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: linux_out->magic = LINUX_MAGIC ; cannam@85: linux_out->channels = channels ; cannam@85: cannam@85: if ((linux_out->fd = open ("/dev/dsp", O_WRONLY, 0)) == -1) cannam@85: { perror ("linux_open : open ") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: stereo = 0 ; cannam@85: if (ioctl (linux_out->fd, SNDCTL_DSP_STEREO, &stereo) == -1) cannam@85: { /* Fatal error */ cannam@85: perror ("linux_open : stereo ") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: if (ioctl (linux_out->fd, SNDCTL_DSP_RESET, 0)) cannam@85: { perror ("linux_open : reset ") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: fmt = CPU_IS_BIG_ENDIAN ? AFMT_S16_BE : AFMT_S16_LE ; cannam@85: if (ioctl (linux_out->fd, SNDCTL_DSP_SETFMT, &fmt) != 0) cannam@85: { perror ("linux_open_dsp_device : set format ") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: if ((error = ioctl (linux_out->fd, SNDCTL_DSP_CHANNELS, &channels)) != 0) cannam@85: { perror ("linux_open : channels ") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: if ((error = ioctl (linux_out->fd, SNDCTL_DSP_SPEED, &samplerate)) != 0) cannam@85: { perror ("linux_open : sample rate ") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: if ((error = ioctl (linux_out->fd, SNDCTL_DSP_SYNC, 0)) != 0) cannam@85: { perror ("linux_open : sync ") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: return (AUDIO_OUT*) linux_out ; cannam@85: } /* linux_open */ cannam@85: cannam@85: static void cannam@85: linux_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) cannam@85: { LINUX_AUDIO_OUT *linux_out ; cannam@85: static float float_buffer [BUFFER_LEN] ; cannam@85: static short buffer [BUFFER_LEN] ; cannam@85: int k, readcount ; cannam@85: cannam@85: if ((linux_out = (LINUX_AUDIO_OUT*) audio_out) == NULL) cannam@85: { printf ("linux_play : AUDIO_OUT is NULL.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: if (linux_out->magic != LINUX_MAGIC) cannam@85: { printf ("linux_play : Bad magic number.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: while ((readcount = callback (callback_data, float_buffer, BUFFER_LEN / linux_out->channels))) cannam@85: { for (k = 0 ; k < readcount * linux_out->channels ; k++) cannam@85: buffer [k] = lrint (32767.0 * float_buffer [k]) ; cannam@85: (void) write (linux_out->fd, buffer, readcount * linux_out->channels * sizeof (short)) ; cannam@85: } ; cannam@85: cannam@85: return ; cannam@85: } /* linux_play */ cannam@85: cannam@85: static void cannam@85: linux_close (AUDIO_OUT *audio_out) cannam@85: { LINUX_AUDIO_OUT *linux_out ; cannam@85: cannam@85: if ((linux_out = (LINUX_AUDIO_OUT*) audio_out) == NULL) cannam@85: { printf ("linux_close : AUDIO_OUT is NULL.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: if (linux_out->magic != LINUX_MAGIC) cannam@85: { printf ("linux_close : Bad magic number.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: memset (linux_out, 0, sizeof (LINUX_AUDIO_OUT)) ; cannam@85: cannam@85: free (linux_out) ; cannam@85: cannam@85: return ; cannam@85: } /* linux_close */ cannam@85: cannam@85: #endif /* __linux__ */ cannam@85: cannam@85: /*------------------------------------------------------------------------------ cannam@85: ** Mac OS X functions for playing a sound. cannam@85: */ cannam@85: cannam@85: #if (defined (__MACH__) && defined (__APPLE__)) /* MacOSX */ cannam@85: cannam@85: #include cannam@85: #include cannam@85: cannam@85: #define MACOSX_MAGIC MAKE_MAGIC ('M', 'a', 'c', ' ', 'O', 'S', ' ', 'X') cannam@85: cannam@85: typedef struct cannam@85: { int magic ; cannam@85: AudioStreamBasicDescription format ; cannam@85: cannam@85: UInt32 buf_size ; cannam@85: AudioDeviceID device ; cannam@85: cannam@85: int channels ; cannam@85: int samplerate ; cannam@85: int buffer_size ; cannam@85: int done_playing ; cannam@85: cannam@85: get_audio_callback_t callback ; cannam@85: cannam@85: void *callback_data ; cannam@85: } MACOSX_AUDIO_OUT ; cannam@85: cannam@85: static AUDIO_OUT *macosx_open (int channels, int samplerate) ; cannam@85: static void macosx_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) ; cannam@85: static void macosx_close (AUDIO_OUT *audio_out) ; cannam@85: cannam@85: static OSStatus cannam@85: macosx_audio_out_callback (AudioDeviceID device, const AudioTimeStamp* current_time, cannam@85: const AudioBufferList* data_in, const AudioTimeStamp* time_in, cannam@85: AudioBufferList* data_out, const AudioTimeStamp* time_out, void* client_data) ; cannam@85: cannam@85: cannam@85: static AUDIO_OUT * cannam@85: macosx_open (int channels, int samplerate) cannam@85: { MACOSX_AUDIO_OUT *macosx_out ; cannam@85: OSStatus err ; cannam@85: size_t count ; cannam@85: cannam@85: if ((macosx_out = malloc (sizeof (MACOSX_AUDIO_OUT))) == NULL) cannam@85: { perror ("macosx_open : malloc ") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: macosx_out->magic = MACOSX_MAGIC ; cannam@85: macosx_out->channels = channels ; cannam@85: macosx_out->samplerate = samplerate ; cannam@85: cannam@85: macosx_out->device = kAudioDeviceUnknown ; cannam@85: cannam@85: /* get the default output device for the HAL */ cannam@85: count = sizeof (AudioDeviceID) ; cannam@85: if ((err = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultOutputDevice, cannam@85: &count, (void *) &(macosx_out->device))) != noErr) cannam@85: { printf ("AudioHardwareGetProperty failed.\n") ; cannam@85: free (macosx_out) ; cannam@85: return NULL ; cannam@85: } ; cannam@85: cannam@85: /* get the buffersize that the default device uses for IO */ cannam@85: count = sizeof (UInt32) ; cannam@85: if ((err = AudioDeviceGetProperty (macosx_out->device, 0, false, kAudioDevicePropertyBufferSize, cannam@85: &count, &(macosx_out->buffer_size))) != noErr) cannam@85: { printf ("AudioDeviceGetProperty (AudioDeviceGetProperty) failed.\n") ; cannam@85: free (macosx_out) ; cannam@85: return NULL ; cannam@85: } ; cannam@85: cannam@85: /* get a description of the data format used by the default device */ cannam@85: count = sizeof (AudioStreamBasicDescription) ; cannam@85: if ((err = AudioDeviceGetProperty (macosx_out->device, 0, false, kAudioDevicePropertyStreamFormat, cannam@85: &count, &(macosx_out->format))) != noErr) cannam@85: { printf ("AudioDeviceGetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ; cannam@85: free (macosx_out) ; cannam@85: return NULL ; cannam@85: } ; cannam@85: cannam@85: macosx_out->format.mSampleRate = samplerate ; cannam@85: macosx_out->format.mChannelsPerFrame = channels ; cannam@85: cannam@85: if ((err = AudioDeviceSetProperty (macosx_out->device, NULL, 0, false, kAudioDevicePropertyStreamFormat, cannam@85: sizeof (AudioStreamBasicDescription), &(macosx_out->format))) != noErr) cannam@85: { printf ("AudioDeviceSetProperty (kAudioDevicePropertyStreamFormat) failed.\n") ; cannam@85: free (macosx_out) ; cannam@85: return NULL ; cannam@85: } ; cannam@85: cannam@85: /* we want linear pcm */ cannam@85: if (macosx_out->format.mFormatID != kAudioFormatLinearPCM) cannam@85: { free (macosx_out) ; cannam@85: return NULL ; cannam@85: } ; cannam@85: cannam@85: macosx_out->done_playing = 0 ; cannam@85: cannam@85: /* Fire off the device. */ cannam@85: if ((err = AudioDeviceAddIOProc (macosx_out->device, macosx_audio_out_callback, cannam@85: (void *) macosx_out)) != noErr) cannam@85: { printf ("AudioDeviceAddIOProc failed.\n") ; cannam@85: free (macosx_out) ; cannam@85: return NULL ; cannam@85: } ; cannam@85: cannam@85: return (MACOSX_AUDIO_OUT *) macosx_out ; cannam@85: } /* macosx_open */ cannam@85: cannam@85: static void cannam@85: macosx_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) cannam@85: { MACOSX_AUDIO_OUT *macosx_out ; cannam@85: OSStatus err ; cannam@85: cannam@85: if ((macosx_out = (MACOSX_AUDIO_OUT*) audio_out) == NULL) cannam@85: { printf ("macosx_play : AUDIO_OUT is NULL.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: if (macosx_out->magic != MACOSX_MAGIC) cannam@85: { printf ("macosx_play : Bad magic number.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: /* Set the callback function and callback data. */ cannam@85: macosx_out->callback = callback ; cannam@85: macosx_out->callback_data = callback_data ; cannam@85: cannam@85: err = AudioDeviceStart (macosx_out->device, macosx_audio_out_callback) ; cannam@85: if (err != noErr) cannam@85: printf ("AudioDeviceStart failed.\n") ; cannam@85: cannam@85: while (macosx_out->done_playing == SF_FALSE) cannam@85: usleep (10 * 1000) ; /* 10 000 milliseconds. */ cannam@85: cannam@85: return ; cannam@85: } /* macosx_play */ cannam@85: cannam@85: static void cannam@85: macosx_close (AUDIO_OUT *audio_out) cannam@85: { MACOSX_AUDIO_OUT *macosx_out ; cannam@85: OSStatus err ; cannam@85: cannam@85: if ((macosx_out = (MACOSX_AUDIO_OUT*) audio_out) == NULL) cannam@85: { printf ("macosx_close : AUDIO_OUT is NULL.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: if (macosx_out->magic != MACOSX_MAGIC) cannam@85: { printf ("macosx_close : Bad magic number.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: cannam@85: if ((err = AudioDeviceStop (macosx_out->device, macosx_audio_out_callback)) != noErr) cannam@85: { printf ("AudioDeviceStop failed.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: err = AudioDeviceRemoveIOProc (macosx_out->device, macosx_audio_out_callback) ; cannam@85: if (err != noErr) cannam@85: { printf ("AudioDeviceRemoveIOProc failed.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: } /* macosx_close */ cannam@85: cannam@85: static OSStatus cannam@85: macosx_audio_out_callback (AudioDeviceID device, const AudioTimeStamp* current_time, cannam@85: const AudioBufferList* data_in, const AudioTimeStamp* time_in, cannam@85: AudioBufferList* data_out, const AudioTimeStamp* time_out, void* client_data) cannam@85: { MACOSX_AUDIO_OUT *macosx_out ; cannam@85: int k, size, sample_count, read_count ; cannam@85: float *buffer ; cannam@85: cannam@85: if ((macosx_out = (MACOSX_AUDIO_OUT*) client_data) == NULL) cannam@85: { printf ("macosx_play : AUDIO_OUT is NULL.\n") ; cannam@85: return 42 ; cannam@85: } ; cannam@85: cannam@85: if (macosx_out->magic != MACOSX_MAGIC) cannam@85: { printf ("macosx_play : Bad magic number.\n") ; cannam@85: return 42 ; cannam@85: } ; cannam@85: cannam@85: size = data_out->mBuffers [0].mDataByteSize ; cannam@85: sample_count = size / sizeof (float) / macosx_out->channels ; cannam@85: cannam@85: buffer = (float*) data_out->mBuffers [0].mData ; cannam@85: cannam@85: read_count = macosx_out->callback (macosx_out->callback_data, buffer, sample_count) ; cannam@85: cannam@85: if (read_count < sample_count) cannam@85: { memset (&(buffer [read_count]), 0, (sample_count - read_count) * sizeof (float)) ; cannam@85: macosx_out->done_playing = 1 ; cannam@85: } ; cannam@85: cannam@85: return noErr ; cannam@85: } /* macosx_audio_out_callback */ cannam@85: cannam@85: #endif /* MacOSX */ cannam@85: cannam@85: cannam@85: /*------------------------------------------------------------------------------ cannam@85: ** Win32 functions for playing a sound. cannam@85: ** cannam@85: ** This API sucks. Its needlessly complicated and is *WAY* too loose with cannam@85: ** passing pointers arounf in integers and and using char* pointers to cannam@85: ** point to data instead of short*. It plain sucks! cannam@85: */ cannam@85: cannam@85: #if (defined (_WIN32) || defined (WIN32)) cannam@85: cannam@85: #include cannam@85: #include cannam@85: cannam@85: #define WIN32_BUFFER_LEN (1<<15) cannam@85: #define WIN32_MAGIC MAKE_MAGIC ('W', 'i', 'n', '3', '2', 's', 'u', 'x') cannam@85: cannam@85: typedef struct cannam@85: { int magic ; cannam@85: cannam@85: HWAVEOUT hwave ; cannam@85: WAVEHDR whdr [2] ; cannam@85: cannam@85: HANDLE Event ; cannam@85: cannam@85: short short_buffer [WIN32_BUFFER_LEN / sizeof (short)] ; cannam@85: float float_buffer [WIN32_BUFFER_LEN / sizeof (short) / 2] ; cannam@85: cannam@85: int bufferlen, current ; cannam@85: cannam@85: int channels ; cannam@85: cannam@85: get_audio_callback_t callback ; cannam@85: cannam@85: void *callback_data ; cannam@85: } WIN32_AUDIO_OUT ; cannam@85: cannam@85: static AUDIO_OUT *win32_open (int channels, int samplerate) ; cannam@85: static void win32_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) ; cannam@85: static void win32_close (AUDIO_OUT *audio_out) ; cannam@85: cannam@85: static DWORD CALLBACK cannam@85: win32_audio_out_callback (HWAVEOUT hwave, UINT msg, DWORD data, DWORD param1, DWORD param2) ; cannam@85: cannam@85: static AUDIO_OUT* cannam@85: win32_open (int channels, int samplerate) cannam@85: { WIN32_AUDIO_OUT *win32_out ; cannam@85: cannam@85: WAVEFORMATEX wf ; cannam@85: int error ; cannam@85: cannam@85: if ((win32_out = malloc (sizeof (WIN32_AUDIO_OUT))) == NULL) cannam@85: { perror ("win32_open : malloc ") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: win32_out->magic = WIN32_MAGIC ; cannam@85: win32_out->channels = channels ; cannam@85: cannam@85: win32_out->current = 0 ; cannam@85: cannam@85: win32_out->Event = CreateEvent (0, FALSE, FALSE, 0) ; cannam@85: cannam@85: wf.nChannels = channels ; cannam@85: wf.nSamplesPerSec = samplerate ; cannam@85: wf.nBlockAlign = channels * sizeof (short) ; cannam@85: cannam@85: wf.wFormatTag = WAVE_FORMAT_PCM ; cannam@85: wf.cbSize = 0 ; cannam@85: wf.wBitsPerSample = 16 ; cannam@85: wf.nAvgBytesPerSec = wf.nBlockAlign * wf.nSamplesPerSec ; cannam@85: cannam@85: error = waveOutOpen (&(win32_out->hwave), WAVE_MAPPER, &wf, (DWORD) win32_audio_out_callback, cannam@85: (DWORD) win32_out, CALLBACK_FUNCTION) ; cannam@85: if (error) cannam@85: { puts ("waveOutOpen failed.") ; cannam@85: free (win32_out) ; cannam@85: return NULL ; cannam@85: } ; cannam@85: cannam@85: waveOutPause (win32_out->hwave) ; cannam@85: cannam@85: return (WIN32_AUDIO_OUT *) win32_out ; cannam@85: } /* win32_open */ cannam@85: cannam@85: static void cannam@85: win32_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) cannam@85: { WIN32_AUDIO_OUT *win32_out ; cannam@85: int error ; cannam@85: cannam@85: if ((win32_out = (WIN32_AUDIO_OUT*) audio_out) == NULL) cannam@85: { printf ("win32_play : AUDIO_OUT is NULL.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: if (win32_out->magic != WIN32_MAGIC) cannam@85: { printf ("win32_play : Bad magic number (%d %d).\n", win32_out->magic, WIN32_MAGIC) ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: /* Set the callback function and callback data. */ cannam@85: win32_out->callback = callback ; cannam@85: win32_out->callback_data = callback_data ; cannam@85: cannam@85: win32_out->whdr [0].lpData = (char*) win32_out->short_buffer ; cannam@85: win32_out->whdr [1].lpData = ((char*) win32_out->short_buffer) + sizeof (win32_out->short_buffer) / 2 ; cannam@85: cannam@85: win32_out->whdr [0].dwBufferLength = sizeof (win32_out->short_buffer) / 2 ; cannam@85: win32_out->whdr [1].dwBufferLength = sizeof (win32_out->short_buffer) / 2 ; cannam@85: cannam@85: win32_out->bufferlen = sizeof (win32_out->short_buffer) / 2 / sizeof (short) ; cannam@85: cannam@85: /* Prepare the WAVEHDRs */ cannam@85: if ((error = waveOutPrepareHeader (win32_out->hwave, &(win32_out->whdr [0]), sizeof (WAVEHDR)))) cannam@85: { printf ("waveOutPrepareHeader [0] failed : %08X\n", error) ; cannam@85: waveOutClose (win32_out->hwave) ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: if ((error = waveOutPrepareHeader (win32_out->hwave, &(win32_out->whdr [1]), sizeof (WAVEHDR)))) cannam@85: { printf ("waveOutPrepareHeader [1] failed : %08X\n", error) ; cannam@85: waveOutUnprepareHeader (win32_out->hwave, &(win32_out->whdr [0]), sizeof (WAVEHDR)) ; cannam@85: waveOutClose (win32_out->hwave) ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: waveOutRestart (win32_out->hwave) ; cannam@85: cannam@85: /* Fake 2 calls to the callback function to queue up enough audio. */ cannam@85: win32_audio_out_callback (0, MM_WOM_DONE, (DWORD) win32_out, 0, 0) ; cannam@85: win32_audio_out_callback (0, MM_WOM_DONE, (DWORD) win32_out, 0, 0) ; cannam@85: cannam@85: /* Wait for playback to finish. The callback notifies us when all cannam@85: ** wave data has been played. cannam@85: */ cannam@85: WaitForSingleObject (win32_out->Event, INFINITE) ; cannam@85: cannam@85: waveOutPause (win32_out->hwave) ; cannam@85: waveOutReset (win32_out->hwave) ; cannam@85: cannam@85: waveOutUnprepareHeader (win32_out->hwave, &(win32_out->whdr [0]), sizeof (WAVEHDR)) ; cannam@85: waveOutUnprepareHeader (win32_out->hwave, &(win32_out->whdr [1]), sizeof (WAVEHDR)) ; cannam@85: cannam@85: waveOutClose (win32_out->hwave) ; cannam@85: win32_out->hwave = 0 ; cannam@85: cannam@85: return ; cannam@85: } /* win32_play */ cannam@85: cannam@85: static void cannam@85: win32_close (AUDIO_OUT *audio_out) cannam@85: { WIN32_AUDIO_OUT *win32_out ; cannam@85: cannam@85: if ((win32_out = (WIN32_AUDIO_OUT*) audio_out) == NULL) cannam@85: { printf ("win32_close : AUDIO_OUT is NULL.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: if (win32_out->magic != WIN32_MAGIC) cannam@85: { printf ("win32_close : Bad magic number.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: memset (win32_out, 0, sizeof (WIN32_AUDIO_OUT)) ; cannam@85: cannam@85: free (win32_out) ; cannam@85: } /* win32_close */ cannam@85: cannam@85: static DWORD CALLBACK cannam@85: win32_audio_out_callback (HWAVEOUT hwave, UINT msg, DWORD data, DWORD param1, DWORD param2) cannam@85: { WIN32_AUDIO_OUT *win32_out ; cannam@85: int read_count, sample_count, k ; cannam@85: short *sptr ; cannam@85: cannam@85: /* cannam@85: ** I consider this technique of passing a pointer via an integer as cannam@85: ** fundamentally broken but thats the way microsoft has defined the cannam@85: ** interface. cannam@85: */ cannam@85: if ((win32_out = (WIN32_AUDIO_OUT*) data) == NULL) cannam@85: { printf ("win32_audio_out_callback : AUDIO_OUT is NULL.\n") ; cannam@85: return 1 ; cannam@85: } ; cannam@85: cannam@85: if (win32_out->magic != WIN32_MAGIC) cannam@85: { printf ("win32_audio_out_callback : Bad magic number (%d %d).\n", win32_out->magic, WIN32_MAGIC) ; cannam@85: return 1 ; cannam@85: } ; cannam@85: cannam@85: if (msg != MM_WOM_DONE) cannam@85: return 0 ; cannam@85: cannam@85: /* Do the actual audio. */ cannam@85: sample_count = win32_out->bufferlen ; cannam@85: cannam@85: read_count = win32_out->callback (win32_out->callback_data, win32_out->float_buffer, sample_count) ; cannam@85: cannam@85: sptr = (short*) win32_out->whdr [win32_out->current].lpData ; cannam@85: cannam@85: for (k = 0 ; k < read_count ; k++) cannam@85: sptr [k] = lrint (32767.0 * win32_out->float_buffer [k]) ; cannam@85: cannam@85: if (read_count > 0) cannam@85: { /* Fix buffer length is only a partial block. */ cannam@85: if (read_count * sizeof (short) < win32_out->bufferlen) cannam@85: win32_out->whdr [win32_out->current].dwBufferLength = read_count * sizeof (short) ; cannam@85: cannam@85: /* Queue the WAVEHDR */ cannam@85: waveOutWrite (win32_out->hwave, (LPWAVEHDR) &(win32_out->whdr [win32_out->current]), sizeof (WAVEHDR)) ; cannam@85: } cannam@85: else cannam@85: { /* Stop playback */ cannam@85: waveOutPause (win32_out->hwave) ; cannam@85: cannam@85: SetEvent (win32_out->Event) ; cannam@85: } ; cannam@85: cannam@85: win32_out->current = (win32_out->current + 1) % 2 ; cannam@85: cannam@85: return 0 ; cannam@85: } /* win32_audio_out_callback */ cannam@85: cannam@85: #endif /* Win32 */ cannam@85: cannam@85: /*------------------------------------------------------------------------------ cannam@85: ** Solaris. cannam@85: */ cannam@85: cannam@85: #if (defined (sun) && defined (unix)) /* ie Solaris */ cannam@85: cannam@85: #include cannam@85: #include cannam@85: #include cannam@85: cannam@85: #define SOLARIS_MAGIC MAKE_MAGIC ('S', 'o', 'l', 'a', 'r', 'i', 's', ' ') cannam@85: cannam@85: typedef struct cannam@85: { int magic ; cannam@85: int fd ; cannam@85: int channels ; cannam@85: int samplerate ; cannam@85: } SOLARIS_AUDIO_OUT ; cannam@85: cannam@85: static AUDIO_OUT *solaris_open (int channels, int samplerate) ; cannam@85: static void solaris_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) ; cannam@85: static void solaris_close (AUDIO_OUT *audio_out) ; cannam@85: cannam@85: static AUDIO_OUT * cannam@85: solaris_open (int channels, int samplerate) cannam@85: { SOLARIS_AUDIO_OUT *solaris_out ; cannam@85: audio_info_t audio_info ; cannam@85: int error ; cannam@85: cannam@85: if ((solaris_out = malloc (sizeof (SOLARIS_AUDIO_OUT))) == NULL) cannam@85: { perror ("solaris_open : malloc ") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: solaris_out->magic = SOLARIS_MAGIC ; cannam@85: solaris_out->channels = channels ; cannam@85: solaris_out->samplerate = channels ; cannam@85: cannam@85: /* open the audio device - write only, non-blocking */ cannam@85: if ((solaris_out->fd = open ("/dev/audio", O_WRONLY | O_NONBLOCK)) < 0) cannam@85: { perror ("open (/dev/audio) failed") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: /* Retrive standard values. */ cannam@85: AUDIO_INITINFO (&audio_info) ; cannam@85: cannam@85: audio_info.play.sample_rate = samplerate ; cannam@85: audio_info.play.channels = channels ; cannam@85: audio_info.play.precision = 16 ; cannam@85: audio_info.play.encoding = AUDIO_ENCODING_LINEAR ; cannam@85: audio_info.play.gain = AUDIO_MAX_GAIN ; cannam@85: audio_info.play.balance = AUDIO_MID_BALANCE ; cannam@85: cannam@85: if ((error = ioctl (solaris_out->fd, AUDIO_SETINFO, &audio_info))) cannam@85: { perror ("ioctl (AUDIO_SETINFO) failed") ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: return (AUDIO_OUT*) solaris_out ; cannam@85: } /* solaris_open */ cannam@85: cannam@85: static void cannam@85: solaris_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) cannam@85: { SOLARIS_AUDIO_OUT *solaris_out ; cannam@85: static float float_buffer [BUFFER_LEN] ; cannam@85: static short buffer [BUFFER_LEN] ; cannam@85: int k, readcount ; cannam@85: cannam@85: if ((solaris_out = (SOLARIS_AUDIO_OUT*) audio_out) == NULL) cannam@85: { printf ("solaris_play : AUDIO_OUT is NULL.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: if (solaris_out->magic != SOLARIS_MAGIC) cannam@85: { printf ("solaris_play : Bad magic number.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: while ((readcount = callback (callback_data, float_buffer, BUFFER_LEN / solaris_out->channels))) cannam@85: { for (k = 0 ; k < readcount * solaris_out->channels ; k++) cannam@85: buffer [k] = lrint (32767.0 * float_buffer [k]) ; cannam@85: write (solaris_out->fd, buffer, readcount * solaris_out->channels * sizeof (short)) ; cannam@85: } ; cannam@85: cannam@85: return ; cannam@85: } /* solaris_play */ cannam@85: cannam@85: static void cannam@85: solaris_close (AUDIO_OUT *audio_out) cannam@85: { SOLARIS_AUDIO_OUT *solaris_out ; cannam@85: cannam@85: if ((solaris_out = (SOLARIS_AUDIO_OUT*) audio_out) == NULL) cannam@85: { printf ("solaris_close : AUDIO_OUT is NULL.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: if (solaris_out->magic != SOLARIS_MAGIC) cannam@85: { printf ("solaris_close : Bad magic number.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: memset (solaris_out, 0, sizeof (SOLARIS_AUDIO_OUT)) ; cannam@85: cannam@85: free (solaris_out) ; cannam@85: cannam@85: return ; cannam@85: } /* solaris_close */ cannam@85: cannam@85: #endif /* Solaris */ cannam@85: cannam@85: /*============================================================================== cannam@85: ** Main function. cannam@85: */ cannam@85: cannam@85: AUDIO_OUT * cannam@85: audio_open (int channels, int samplerate) cannam@85: { cannam@85: #if defined (__linux__) cannam@85: return linux_open (channels, samplerate) ; cannam@85: #elif (defined (__MACH__) && defined (__APPLE__)) cannam@85: return macosx_open (channels, samplerate) ; cannam@85: #elif (defined (sun) && defined (unix)) cannam@85: return solaris_open (channels, samplerate) ; cannam@85: #elif (defined (_WIN32) || defined (WIN32)) cannam@85: return win32_open (channels, samplerate) ; cannam@85: #else cannam@85: #warning "*** Playing sound not yet supported on this platform." cannam@85: #warning "*** Please feel free to submit a patch." cannam@85: printf ("Error : Playing sound not yet supported on this platform.\n") ; cannam@85: return NULL ; cannam@85: #endif cannam@85: cannam@85: cannam@85: return NULL ; cannam@85: } /* audio_open */ cannam@85: cannam@85: void cannam@85: audio_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) cannam@85: { cannam@85: cannam@85: if (callback == NULL) cannam@85: { printf ("Error : bad callback pointer.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: if (audio_out == NULL) cannam@85: { printf ("Error : bad audio_out pointer.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: if (callback_data == NULL) cannam@85: { printf ("Error : bad callback_data pointer.\n") ; cannam@85: return ; cannam@85: } ; cannam@85: cannam@85: #if defined (__linux__) cannam@85: linux_play (callback, audio_out, callback_data) ; cannam@85: #elif (defined (__MACH__) && defined (__APPLE__)) cannam@85: macosx_play (callback, audio_out, callback_data) ; cannam@85: #elif (defined (sun) && defined (unix)) cannam@85: solaris_play (callback, audio_out, callback_data) ; cannam@85: #elif (defined (_WIN32) || defined (WIN32)) cannam@85: win32_play (callback, audio_out, callback_data) ; cannam@85: #else cannam@85: #warning "*** Playing sound not yet supported on this platform." cannam@85: #warning "*** Please feel free to submit a patch." cannam@85: printf ("Error : Playing sound not yet supported on this platform.\n") ; cannam@85: return ; cannam@85: #endif cannam@85: cannam@85: return ; cannam@85: } /* audio_play */ cannam@85: cannam@85: void cannam@85: audio_close (AUDIO_OUT *audio_out) cannam@85: { cannam@85: #if defined (__linux__) cannam@85: linux_close (audio_out) ; cannam@85: #elif (defined (__MACH__) && defined (__APPLE__)) cannam@85: macosx_close (audio_out) ; cannam@85: #elif (defined (sun) && defined (unix)) cannam@85: solaris_close (audio_out) ; cannam@85: #elif (defined (_WIN32) || defined (WIN32)) cannam@85: win32_close (audio_out) ; cannam@85: #else cannam@85: #warning "*** Playing sound not yet supported on this platform." cannam@85: #warning "*** Please feel free to submit a patch." cannam@85: printf ("Error : Playing sound not yet supported on this platform.\n") ; cannam@85: return ; cannam@85: #endif cannam@85: cannam@85: return ; cannam@85: } /* audio_close */ cannam@85: cannam@85: #else /* (HAVE_SNDFILE == 0) */ cannam@85: cannam@85: /* Do not have libsndfile installed so just return. */ cannam@85: cannam@85: AUDIO_OUT * cannam@85: audio_open (int channels, int samplerate) cannam@85: { cannam@85: (void) channels ; cannam@85: (void) samplerate ; cannam@85: cannam@85: return NULL ; cannam@85: } /* audio_open */ cannam@85: cannam@85: void cannam@85: audio_play (get_audio_callback_t callback, AUDIO_OUT *audio_out, void *callback_data) cannam@85: { cannam@85: (void) callback ; cannam@85: (void) audio_out ; cannam@85: (void) callback_data ; cannam@85: cannam@85: return ; cannam@85: } /* audio_play */ cannam@85: cannam@85: void cannam@85: audio_close (AUDIO_OUT *audio_out) cannam@85: { cannam@85: audio_out = audio_out ; cannam@85: cannam@85: return ; cannam@85: } /* audio_close */ cannam@85: cannam@85: #endif /* HAVE_SNDFILE */ cannam@85: