Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
authorSam Lantinga <slouken@libsdl.org>
Sat, 21 Aug 2004 02:06:30 +0000
changeset 935f8d5ddc7aef1
parent 934 af585d6efec8
child 936 84f930aebaeb
Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
configure.in
src/audio/Makefile.am
src/audio/SDL_audio.c
src/audio/SDL_sysaudio.h
src/audio/macosx/.cvsignore
src/audio/macosx/Makefile.am
src/audio/macosx/SDL_coreaudio.c
src/audio/macosx/SDL_coreaudio.h
src/cdrom/macosx/AudioFilePlayer.cpp
src/cdrom/macosx/AudioFilePlayer.h
src/cdrom/macosx/AudioFileReaderThread.cpp
src/cdrom/macosx/CDPlayer.cpp
     1.1 --- a/configure.in	Fri Aug 20 22:35:23 2004 +0000
     1.2 +++ b/configure.in	Sat Aug 21 02:06:30 2004 +0000
     1.3 @@ -332,6 +332,7 @@
     1.4          ],[
     1.5          have_dmedia=yes
     1.6          ])
     1.7 +        AC_MSG_RESULT($have_dmedia)
     1.8          # Set up files for the audio library
     1.9          if test x$have_dmedia = xyes; then
    1.10              CFLAGS="$CFLAGS -DDMEDIA_SUPPORT"
    1.11 @@ -489,6 +490,29 @@
    1.12      fi
    1.13  }
    1.14  
    1.15 +dnl Check whether we want to use CoreAudio
    1.16 +CheckCoreAudio()
    1.17 +{
    1.18 +    if test x$enable_audio = xyes; then
    1.19 +        AC_MSG_CHECKING(for CoreAudio audio support)
    1.20 +        have_coreaudio=no
    1.21 +        AC_TRY_COMPILE([
    1.22 +          #include <AudioUnit/AudioUnit.h>
    1.23 +        ],[
    1.24 +          AudioUnitInputCallback callback;
    1.25 +        ],[
    1.26 +        have_coreaudio=yes
    1.27 +        ])
    1.28 +        AC_MSG_RESULT($have_coreaudio)
    1.29 +        # Set up files for the audio library
    1.30 +        if test x$have_coreaudio = xyes; then
    1.31 +            CFLAGS="$CFLAGS -DCOREAUDIO_SUPPORT"
    1.32 +            AUDIO_SUBDIRS="$AUDIO_SUBDIRS macosx"
    1.33 +            AUDIO_DRIVERS="$AUDIO_DRIVERS macosx/libaudio_macosx.la"
    1.34 +        fi
    1.35 +    fi
    1.36 +}
    1.37 +
    1.38  dnl See if we can use x86 assembly blitters
    1.39  # NASM is available from: http://nasm.octium.net/
    1.40  CheckNASM()
    1.41 @@ -2507,10 +2531,15 @@
    1.42          ARCH=macosx
    1.43          CheckDummyVideo
    1.44          CheckDiskAudio
    1.45 +        CheckCoreAudio
    1.46          CheckQUARTZ
    1.47          CheckMacGL
    1.48          CheckPTHREAD
    1.49          CheckSIGACTION
    1.50 +        # If either the audio or CD driver is used, add the AudioUnit framework
    1.51 +        if test x$enable_audio = xyes -o x$enable_cdrom = xyes; then
    1.52 +            SYSTEM_LIBS="$SYSTEM_LIBS -framework AudioToolbox -framework AudioUnit"
    1.53 +        fi
    1.54          # Set up files for the audio library
    1.55          if test x$enable_audio = xyes; then
    1.56              AUDIO_SUBDIRS="$AUDIO_SUBDIRS macrom"
    1.57 @@ -2526,7 +2555,7 @@
    1.58          if test x$enable_cdrom = xyes; then
    1.59              CDROM_SUBDIRS="$CDROM_SUBDIRS macosx"
    1.60              CDROM_DRIVERS="$CDROM_DRIVERS macosx/libcdrom_macosx.la"
    1.61 -            SYSTEM_LIBS="$SYSTEM_LIBS -framework AudioToolbox -framework AudioUnit -lstdc++"
    1.62 +            SYSTEM_LIBS="$SYSTEM_LIBS -lstdc++"
    1.63          fi
    1.64          # Set up files for the thread library
    1.65          if test x$enable_threads = xyes; then
    1.66 @@ -2766,6 +2795,7 @@
    1.67  src/audio/dmedia/Makefile
    1.68  src/audio/dsp/Makefile
    1.69  src/audio/esd/Makefile
    1.70 +src/audio/macosx/Makefile
    1.71  src/audio/macrom/Makefile
    1.72  src/audio/mint/Makefile
    1.73  src/audio/mme/Makefile
     2.1 --- a/src/audio/Makefile.am	Fri Aug 20 22:35:23 2004 +0000
     2.2 +++ b/src/audio/Makefile.am	Sat Aug 21 02:06:30 2004 +0000
     2.3 @@ -15,6 +15,7 @@
     2.4  	dmedia	\
     2.5  	dsp	\
     2.6  	esd	\
     2.7 +	macosx	\
     2.8  	macrom	\
     2.9  	mint	\
    2.10  	mme	\
     3.1 --- a/src/audio/SDL_audio.c	Fri Aug 20 22:35:23 2004 +0000
     3.2 +++ b/src/audio/SDL_audio.c	Sat Aug 21 02:06:30 2004 +0000
     3.3 @@ -77,6 +77,9 @@
     3.4  #ifdef __BEOS__
     3.5  	&BAUDIO_bootstrap,
     3.6  #endif
     3.7 +#ifdef COREAUDIO_SUPPORT
     3.8 +	&COREAUDIO_bootstrap,
     3.9 +#endif
    3.10  #if defined(macintosh) || TARGET_API_MAC_CARBON
    3.11  	&SNDMGR_bootstrap,
    3.12  #endif
     4.1 --- a/src/audio/SDL_sysaudio.h	Fri Aug 20 22:35:23 2004 +0000
     4.2 +++ b/src/audio/SDL_sysaudio.h	Sat Aug 21 02:06:30 2004 +0000
     4.3 @@ -144,6 +144,9 @@
     4.4  #ifdef __BEOS__
     4.5  extern AudioBootStrap BAUDIO_bootstrap;
     4.6  #endif
     4.7 +#ifdef COREAUDIO_SUPPORT
     4.8 +extern AudioBootStrap COREAUDIO_bootstrap;
     4.9 +#endif
    4.10  #if defined(macintosh) || TARGET_API_MAC_CARBON
    4.11  extern AudioBootStrap SNDMGR_bootstrap;
    4.12  #endif
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/audio/macosx/.cvsignore	Sat Aug 21 02:06:30 2004 +0000
     5.3 @@ -0,0 +1,6 @@
     5.4 +Makefile.in
     5.5 +Makefile
     5.6 +.libs
     5.7 +*.o
     5.8 +*.lo
     5.9 +*.la
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/audio/macosx/Makefile.am	Sat Aug 21 02:06:30 2004 +0000
     6.3 @@ -0,0 +1,9 @@
     6.4 +
     6.5 +## Makefile.am for SDL using the Mac OS X CoreAudio driver
     6.6 +
     6.7 +noinst_LTLIBRARIES = libaudio_macosx.la
     6.8 +libaudio_macosx_la_SOURCES = $(SRCS)
     6.9 +
    6.10 +# The SDL audio driver sources
    6.11 +SRCS =	SDL_coreaudio.c	\
    6.12 +	SDL_coreaudio.h
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/audio/macosx/SDL_coreaudio.c	Sat Aug 21 02:06:30 2004 +0000
     7.3 @@ -0,0 +1,287 @@
     7.4 +/*
     7.5 +    SDL - Simple DirectMedia Layer
     7.6 +    Copyright (C) 1997-2004 Sam Lantinga
     7.7 +
     7.8 +    This library is free software; you can redistribute it and/or
     7.9 +    modify it under the terms of the GNU Library General Public
    7.10 +    License as published by the Free Software Foundation; either
    7.11 +    version 2 of the License, or (at your option) any later version.
    7.12 +
    7.13 +    This library is distributed in the hope that it will be useful,
    7.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    7.16 +    Library General Public License for more details.
    7.17 +
    7.18 +    You should have received a copy of the GNU Library General Public
    7.19 +    License along with this library; if not, write to the Free
    7.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    7.21 +
    7.22 +    Sam Lantinga
    7.23 +    slouken@libsdl.org
    7.24 +*/
    7.25 +
    7.26 +#ifdef SAVE_RCSID
    7.27 +static char rcsid =
    7.28 + "@(#) $Id$";
    7.29 +#endif
    7.30 +
    7.31 +#include <AudioUnit/AudioUnit.h>
    7.32 +
    7.33 +#include <assert.h>
    7.34 +#include <stdlib.h>
    7.35 +#include <stdio.h>
    7.36 +#include <string.h>
    7.37 +
    7.38 +#include "SDL_endian.h"
    7.39 +#include "SDL_audio.h"
    7.40 +#include "SDL_audio_c.h"
    7.41 +#include "SDL_audiomem.h"
    7.42 +#include "SDL_sysaudio.h"
    7.43 +#include "SDL_coreaudio.h"
    7.44 +
    7.45 +
    7.46 +/* Audio driver functions */
    7.47 +
    7.48 +static int Core_OpenAudio(_THIS, SDL_AudioSpec *spec);
    7.49 +static void Core_WaitAudio(_THIS);
    7.50 +static void Core_PlayAudio(_THIS);
    7.51 +static Uint8 *Core_GetAudioBuf(_THIS);
    7.52 +static void Core_CloseAudio(_THIS);
    7.53 +
    7.54 +/* Audio driver bootstrap functions */
    7.55 +
    7.56 +static int Audio_Available(void)
    7.57 +{
    7.58 +    return(1);
    7.59 +}
    7.60 +
    7.61 +static void Audio_DeleteDevice(SDL_AudioDevice *device)
    7.62 +{
    7.63 +    free(device->hidden);
    7.64 +    free(device);
    7.65 +}
    7.66 +
    7.67 +static SDL_AudioDevice *Audio_CreateDevice(int devindex)
    7.68 +{
    7.69 +    SDL_AudioDevice *this;
    7.70 +
    7.71 +    /* Initialize all variables that we clean on shutdown */
    7.72 +    this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
    7.73 +    if ( this ) {
    7.74 +        memset(this, 0, (sizeof *this));
    7.75 +        this->hidden = (struct SDL_PrivateAudioData *)
    7.76 +                malloc((sizeof *this->hidden));
    7.77 +    }
    7.78 +    if ( (this == NULL) || (this->hidden == NULL) ) {
    7.79 +        SDL_OutOfMemory();
    7.80 +        if ( this ) {
    7.81 +            free(this);
    7.82 +        }
    7.83 +        return(0);
    7.84 +    }
    7.85 +    memset(this->hidden, 0, (sizeof *this->hidden));
    7.86 +
    7.87 +    /* Set the function pointers */
    7.88 +    this->OpenAudio = Core_OpenAudio;
    7.89 +    this->WaitAudio = Core_WaitAudio;
    7.90 +    this->PlayAudio = Core_PlayAudio;
    7.91 +    this->GetAudioBuf = Core_GetAudioBuf;
    7.92 +    this->CloseAudio = Core_CloseAudio;
    7.93 +
    7.94 +    this->free = Audio_DeleteDevice;
    7.95 +
    7.96 +    return this;
    7.97 +}
    7.98 +
    7.99 +AudioBootStrap COREAUDIO_bootstrap = {
   7.100 +    "coreaudio", "Mac OS X CoreAudio",
   7.101 +    Audio_Available, Audio_CreateDevice
   7.102 +};
   7.103 +
   7.104 +/* The CoreAudio callback */
   7.105 +static OSStatus     audioCallback (void                             *inRefCon, 
   7.106 +                                    AudioUnitRenderActionFlags      inActionFlags,
   7.107 +                                    const AudioTimeStamp            *inTimeStamp, 
   7.108 +                                    UInt32                          inBusNumber, 
   7.109 +                                    AudioBuffer                     *ioData)
   7.110 +{
   7.111 +    SDL_AudioDevice *this = (SDL_AudioDevice *)inRefCon;
   7.112 +    UInt32 remaining, len;
   7.113 +    void *ptr;
   7.114 +
   7.115 +    /* Only do anything if audio is enabled and not paused */
   7.116 +    if ( ! this->enabled || this->paused ) {
   7.117 +        memset(ioData->mData, this->spec.silence, ioData->mDataByteSize);
   7.118 +        return 0;
   7.119 +    }
   7.120 +    
   7.121 +    /* No SDL conversion should be needed here, ever, since we accept
   7.122 +       any input format in OpenAudio, and leave the conversion to CoreAudio.
   7.123 +     */
   7.124 +    assert(!this->convert.needed);
   7.125 +    assert(this->spec.channels == ioData->mNumberChannels);
   7.126 +    
   7.127 +    remaining = ioData->mDataByteSize;
   7.128 +    ptr = ioData->mData;
   7.129 +    while (remaining > 0) {
   7.130 +        if (bufferOffset >= bufferSize) {
   7.131 +            /* Generate the data */
   7.132 +            memset(buffer, this->spec.silence, bufferSize);
   7.133 +            SDL_mutexP(this->mixer_lock);
   7.134 +            (*this->spec.callback)(this->spec.userdata,
   7.135 +                        buffer, bufferSize);
   7.136 +            SDL_mutexV(this->mixer_lock);
   7.137 +            bufferOffset = 0;
   7.138 +        }
   7.139 +        
   7.140 +        len = bufferSize - bufferOffset;
   7.141 +        if (len > remaining)
   7.142 +            len = remaining;
   7.143 +        memcpy(ptr, buffer + bufferOffset, len);
   7.144 +        ptr += len;
   7.145 +        remaining -= len;
   7.146 +        bufferOffset += len;
   7.147 +    }
   7.148 +    
   7.149 +    return 0;
   7.150 +}
   7.151 +
   7.152 +/* Dummy functions -- we don't use thread-based audio */
   7.153 +void Core_WaitAudio(_THIS)
   7.154 +{
   7.155 +    return;
   7.156 +}
   7.157 +
   7.158 +void Core_PlayAudio(_THIS)
   7.159 +{
   7.160 +    return;
   7.161 +}
   7.162 +
   7.163 +Uint8 *Core_GetAudioBuf(_THIS)
   7.164 +{
   7.165 +    return(NULL);
   7.166 +}
   7.167 +
   7.168 +void Core_CloseAudio(_THIS)
   7.169 +{
   7.170 +    OSStatus result;
   7.171 +    AudioUnitInputCallback callback;
   7.172 +
   7.173 +    /* stop processing the audio unit */
   7.174 +    result = AudioOutputUnitStop (outputAudioUnit);
   7.175 +    if (result != noErr) {
   7.176 +        SDL_SetError("Core_CloseAudio: AudioOutputUnitStop");
   7.177 +        return;
   7.178 +    }
   7.179 +
   7.180 +    /* Remove the input callback */
   7.181 +    callback.inputProc = 0;
   7.182 +    callback.inputProcRefCon = 0;
   7.183 +    result = AudioUnitSetProperty (outputAudioUnit, 
   7.184 +                        kAudioUnitProperty_SetInputCallback, 
   7.185 +                        kAudioUnitScope_Input, 
   7.186 +                        0,
   7.187 +                        &callback, 
   7.188 +                        sizeof(callback));
   7.189 +    if (result != noErr) {
   7.190 +        SDL_SetError("Core_CloseAudio: AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)");
   7.191 +        return;
   7.192 +    }
   7.193 +
   7.194 +    result = CloseComponent(outputAudioUnit);
   7.195 +    if (result != noErr) {
   7.196 +        SDL_SetError("Core_CloseAudio: CloseComponent");
   7.197 +        return;
   7.198 +    }
   7.199 +    
   7.200 +    free(buffer);
   7.201 +}
   7.202 +
   7.203 +#define CHECK_RESULT(msg) \
   7.204 +    if (result != noErr) { \
   7.205 +        SDL_SetError("Failed to start CoreAudio: " msg); \
   7.206 +        return -1; \
   7.207 +    }
   7.208 +
   7.209 +
   7.210 +int Core_OpenAudio(_THIS, SDL_AudioSpec *spec)
   7.211 +{
   7.212 +    OSStatus result = noErr;
   7.213 +    Component comp;
   7.214 +    ComponentDescription desc;
   7.215 +    AudioUnitInputCallback callback;
   7.216 +    AudioStreamBasicDescription requestedDesc;
   7.217 +
   7.218 +    /* Setup a AudioStreamBasicDescription with the requested format */
   7.219 +    requestedDesc.mFormatID = kAudioFormatLinearPCM;
   7.220 +    requestedDesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
   7.221 +    requestedDesc.mChannelsPerFrame = spec->channels;
   7.222 +    requestedDesc.mSampleRate = spec->freq;
   7.223 +    
   7.224 +    requestedDesc.mBitsPerChannel = spec->format & 0xFF;
   7.225 +    if (spec->format & 0x8000)
   7.226 +        requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
   7.227 +    if (spec->format & 0x1000)
   7.228 +        requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
   7.229 +
   7.230 +    requestedDesc.mFramesPerPacket = 1;
   7.231 +    requestedDesc.mBytesPerFrame = requestedDesc.mBitsPerChannel * requestedDesc.mChannelsPerFrame / 8;
   7.232 +    requestedDesc.mBytesPerPacket = requestedDesc.mBytesPerFrame * requestedDesc.mFramesPerPacket;
   7.233 +
   7.234 +
   7.235 +    /* Locate the default output audio unit */
   7.236 +    desc.componentType = kAudioUnitComponentType;
   7.237 +    desc.componentSubType = kAudioUnitSubType_Output;
   7.238 +    desc.componentManufacturer = kAudioUnitID_DefaultOutput;
   7.239 +    desc.componentFlags = 0;
   7.240 +    desc.componentFlagsMask = 0;
   7.241 +    
   7.242 +    comp = FindNextComponent (NULL, &desc);
   7.243 +    if (comp == NULL) {
   7.244 +        SDL_SetError ("Failed to start CoreAudio: FindNextComponent returned NULL");
   7.245 +        return -1;
   7.246 +    }
   7.247 +    
   7.248 +    /* Open & initialize the default output audio unit */
   7.249 +    result = OpenAComponent (comp, &outputAudioUnit);
   7.250 +    CHECK_RESULT("OpenAComponent")
   7.251 +
   7.252 +    result = AudioUnitInitialize (outputAudioUnit);
   7.253 +    CHECK_RESULT("AudioUnitInitialize")
   7.254 +                
   7.255 +    /* Set the input format of the audio unit. */
   7.256 +    result = AudioUnitSetProperty (outputAudioUnit,
   7.257 +                               kAudioUnitProperty_StreamFormat,
   7.258 +                               kAudioUnitScope_Input,
   7.259 +                               0,
   7.260 +                               &requestedDesc,
   7.261 +                               sizeof (requestedDesc));
   7.262 +    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)")
   7.263 +
   7.264 +    /* Set the audio callback */
   7.265 +    callback.inputProc = audioCallback;
   7.266 +    callback.inputProcRefCon = this;
   7.267 +    result = AudioUnitSetProperty (outputAudioUnit, 
   7.268 +                        kAudioUnitProperty_SetInputCallback, 
   7.269 +                        kAudioUnitScope_Input, 
   7.270 +                        0,
   7.271 +                        &callback, 
   7.272 +                        sizeof(callback));
   7.273 +    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)")
   7.274 +
   7.275 +    /* Calculate the final parameters for this audio specification */
   7.276 +    SDL_CalculateAudioSpec(spec);
   7.277 +    
   7.278 +    /* Allocate a sample buffer */
   7.279 +    bufferOffset = bufferSize = this->spec.size;
   7.280 +    buffer = malloc(bufferSize);
   7.281 +    assert(buffer);
   7.282 +    
   7.283 +    /* Finally, start processing of the audio unit */
   7.284 +    result = AudioOutputUnitStart (outputAudioUnit);
   7.285 +    CHECK_RESULT("AudioOutputUnitStart")    
   7.286 +    
   7.287 +
   7.288 +    /* We're running! */
   7.289 +    return(1);
   7.290 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/audio/macosx/SDL_coreaudio.h	Sat Aug 21 02:06:30 2004 +0000
     8.3 @@ -0,0 +1,49 @@
     8.4 +/*
     8.5 +    SDL - Simple DirectMedia Layer
     8.6 +    Copyright (C) 1997-2004 Sam Lantinga
     8.7 +
     8.8 +    This library is free software; you can redistribute it and/or
     8.9 +    modify it under the terms of the GNU Library General Public
    8.10 +    License as published by the Free Software Foundation; either
    8.11 +    version 2 of the License, or (at your option) any later version.
    8.12 +
    8.13 +    This library is distributed in the hope that it will be useful,
    8.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    8.16 +    Library General Public License for more details.
    8.17 +
    8.18 +    You should have received a copy of the GNU Library General Public
    8.19 +    License along with this library; if not, write to the Free
    8.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    8.21 +
    8.22 +    Sam Lantinga
    8.23 +    slouken@libsdl.org
    8.24 +*/
    8.25 +
    8.26 +#ifdef SAVE_RCSID
    8.27 +static char rcsid =
    8.28 + "@(#) $Id$";
    8.29 +#endif
    8.30 +
    8.31 +#ifndef _SDL_coreaudio_h
    8.32 +#define _SDL_coreaudio_h
    8.33 +
    8.34 +#include "SDL_sysaudio.h"
    8.35 +
    8.36 +/* Hidden "this" pointer for the video functions */
    8.37 +#define _THIS	SDL_AudioDevice *this
    8.38 +
    8.39 +struct SDL_PrivateAudioData {
    8.40 +	AudioUnit outputAudioUnit;
    8.41 +	void *buffer;
    8.42 +	UInt32 bufferOffset;
    8.43 +	UInt32 bufferSize;
    8.44 +};
    8.45 +
    8.46 +/* Old variable names */
    8.47 +#define outputAudioUnit		(this->hidden->outputAudioUnit)
    8.48 +#define buffer		(this->hidden->buffer)
    8.49 +#define bufferOffset		(this->hidden->bufferOffset)
    8.50 +#define bufferSize		(this->hidden->bufferSize)
    8.51 +
    8.52 +#endif /* _SDL_coreaudio_h */
     9.1 --- a/src/cdrom/macosx/AudioFilePlayer.cpp	Fri Aug 20 22:35:23 2004 +0000
     9.2 +++ b/src/cdrom/macosx/AudioFilePlayer.cpp	Sat Aug 21 02:06:30 2004 +0000
     9.3 @@ -67,30 +67,29 @@
     9.4  
     9.5  OSStatus    AudioFileManager::Render (AudioBuffer &ioData)
     9.6  {
     9.7 -    OSStatus result = AudioConverterFillBuffer(mParentConverter, 
     9.8 -                                    AudioFileManager::ACInputProc, 
     9.9 -                                    this, 
    9.10 -                                    &ioData.mDataByteSize, 
    9.11 -                                    ioData.mData);
    9.12 -    if (result) {
    9.13 -        SDL_SetError ("AudioConverterFillBuffer:%ld\n", result);
    9.14 -        mParent.DoNotification (result);
    9.15 -    } else {
    9.16 -        mByteCounter += ioData.mDataByteSize / 2;
    9.17 -        AfterRender();
    9.18 -    }
    9.19 +    OSStatus result = noErr;
    9.20 +    
    9.21 +	if (mBufferOffset >= mBufferSize) {
    9.22 +		result = GetFileData(&mTmpBuffer, &mBufferSize);
    9.23 +		if (result) {
    9.24 +			SDL_SetError ("AudioConverterFillBuffer:%ld\n", result);
    9.25 +			mParent.DoNotification (result);
    9.26 +			return result;
    9.27 +		}
    9.28 +
    9.29 +		mBufferOffset = 0;
    9.30 +	}
    9.31 +    	
    9.32 +    if (ioData.mDataByteSize > mBufferSize - mBufferOffset)
    9.33 +    	ioData.mDataByteSize = mBufferSize - mBufferOffset;
    9.34 +    ioData.mData = (char *)mTmpBuffer + mBufferOffset;
    9.35 +    mBufferOffset += ioData.mDataByteSize;
    9.36 +    
    9.37 +	mByteCounter += ioData.mDataByteSize;
    9.38 +	AfterRender();
    9.39      return result;
    9.40  }
    9.41  
    9.42 -OSStatus    AudioFileManager::ACInputProc (AudioConverterRef            inAudioConverter,
    9.43 -                                            UInt32*                     outDataSize,
    9.44 -                                            void**                      outData,
    9.45 -                                            void*                       inUserData)
    9.46 -{
    9.47 -    AudioFileManager* THIS = (AudioFileManager*)inUserData;
    9.48 -    return THIS->GetFileData(outData, outDataSize);
    9.49 -}
    9.50 -
    9.51  AudioFileManager::~AudioFileManager ()
    9.52  {
    9.53      if (mFileBuffer) {
    9.54 @@ -102,7 +101,6 @@
    9.55  AudioFilePlayer::AudioFilePlayer (const FSRef           *inFileRef)
    9.56      : mConnected (false),
    9.57        mAudioFileManager (0),
    9.58 -      mConverter (0),
    9.59        mNotifier (0),
    9.60        mStartFrame (0)
    9.61  {
    9.62 @@ -124,39 +122,14 @@
    9.63                                                  bytesPerSecond);
    9.64  }
    9.65  
    9.66 -// you can put a rate scalar here to play the file faster or slower
    9.67 -// by multiplying the same rate by the desired factor 
    9.68 -// eg fileSampleRate * 2 -> twice as fast
    9.69 -// before you create the AudioConverter
    9.70 -void    AudioFilePlayer::SetDestination (AudioUnit  &inDestUnit, 
    9.71 -                                         int         inBusNumber)
    9.72 +void    AudioFilePlayer::SetDestination (AudioUnit  &inDestUnit)
    9.73  {
    9.74      if (mConnected) throw static_cast<OSStatus>(-1); //can't set dest if already engaged
    9.75   
    9.76      mPlayUnit = inDestUnit;
    9.77 -    mBusNumber = inBusNumber;
    9.78  
    9.79      OSStatus result = noErr;
    9.80      
    9.81 -    if (mConverter) {
    9.82 -        result = AudioConverterDispose (mConverter);
    9.83 -            THROW_RESULT("AudioConverterDispose")
    9.84 -    }
    9.85 -    
    9.86 -    AudioStreamBasicDescription     destDesc;
    9.87 -    UInt32  size = sizeof (destDesc);
    9.88 -    result = AudioUnitGetProperty (inDestUnit,
    9.89 -                                   kAudioUnitProperty_StreamFormat,
    9.90 -                                   kAudioUnitScope_Input,
    9.91 -                                   inBusNumber,
    9.92 -                                   &destDesc,
    9.93 -                                   &size);
    9.94 -        THROW_RESULT("AudioUnitGetProperty")
    9.95 -
    9.96 -#if DEBUG
    9.97 -    printf("Destination format:\n");
    9.98 -    PrintStreamDesc (&destDesc);
    9.99 -#endif
   9.100  
   9.101          //we can "down" cast a component instance to a component
   9.102      ComponentDescription desc;
   9.103 @@ -171,19 +144,14 @@
   9.104          THROW_RESULT("BAD COMPONENT")
   9.105      }
   9.106  
   9.107 -    
   9.108 -    result = AudioConverterNew (&mFileDescription, &destDesc, &mConverter);
   9.109 -        THROW_RESULT("AudioConverterNew")
   9.110 -
   9.111 -#if 0
   9.112 -    // this uses the better quality SRC
   9.113 -    UInt32 srcID = kAudioUnitSRCAlgorithm_Polyphase;
   9.114 -    result = AudioConverterSetProperty(mConverter,
   9.115 -                    kAudioConverterSampleRateConverterAlgorithm, 
   9.116 -                    sizeof(srcID), 
   9.117 -                    &srcID);
   9.118 -        THROW_RESULT("AudioConverterSetProperty")
   9.119 -#endif
   9.120 +    /* Set the input format of the audio unit. */
   9.121 +    result = AudioUnitSetProperty (inDestUnit,
   9.122 +                               kAudioUnitProperty_StreamFormat,
   9.123 +                               kAudioUnitScope_Input,
   9.124 +                               0,
   9.125 +                               &mFileDescription,
   9.126 +                               sizeof (mFileDescription));
   9.127 +        THROW_RESULT("AudioUnitSetProperty")
   9.128  }
   9.129  
   9.130  void    AudioFilePlayer::SetStartFrame (int frame)
   9.131 @@ -220,22 +188,18 @@
   9.132          FSClose (mForkRefNum);
   9.133          mForkRefNum = 0;
   9.134      }
   9.135 -
   9.136 -    if (mConverter) {
   9.137 -        AudioConverterDispose (mConverter);
   9.138 -        mConverter = 0;
   9.139 -    }
   9.140  }
   9.141  
   9.142  void    AudioFilePlayer::Connect()
   9.143  {
   9.144  #if DEBUG
   9.145 -    printf ("Connect:%x,%ld, engaged=%d\n", (int)mPlayUnit, mBusNumber, (mConnected ? 1 : 0));
   9.146 +    printf ("Connect:%x, engaged=%d\n", (int)mPlayUnit, (mConnected ? 1 : 0));
   9.147  #endif
   9.148      if (!mConnected)
   9.149      {           
   9.150 -        mAudioFileManager->Connect(mConverter);
   9.151 -                
   9.152 +        mAudioFileManager->DoConnect();
   9.153 +
   9.154 +
   9.155          // set the render callback for the file data to be supplied to the sound converter AU
   9.156          mInputCallback.inputProc = AudioFileManager::FileInputProc;
   9.157          mInputCallback.inputProcRefCon = mAudioFileManager;
   9.158 @@ -243,7 +207,7 @@
   9.159          OSStatus result = AudioUnitSetProperty (mPlayUnit, 
   9.160                              kAudioUnitProperty_SetInputCallback, 
   9.161                              kAudioUnitScope_Input, 
   9.162 -                            mBusNumber,
   9.163 +                            0,
   9.164                              &mInputCallback, 
   9.165                              sizeof(mInputCallback));
   9.166          THROW_RESULT("AudioUnitSetProperty")
   9.167 @@ -272,7 +236,7 @@
   9.168  void    AudioFilePlayer::Disconnect ()
   9.169  {
   9.170  #if DEBUG
   9.171 -    printf ("Disconnect:%x,%ld, engaged=%d\n", (int)mPlayUnit, mBusNumber, (mConnected ? 1 : 0));
   9.172 +    printf ("Disconnect:%x,%ld, engaged=%d\n", (int)mPlayUnit, 0, (mConnected ? 1 : 0));
   9.173  #endif
   9.174      if (mConnected)
   9.175      {
   9.176 @@ -283,7 +247,7 @@
   9.177          OSStatus result = AudioUnitSetProperty (mPlayUnit, 
   9.178                                          kAudioUnitProperty_SetInputCallback, 
   9.179                                          kAudioUnitScope_Input, 
   9.180 -                                        mBusNumber,
   9.181 +                                        0,
   9.182                                          &mInputCallback, 
   9.183                                          sizeof(mInputCallback));
   9.184          if (result) 
    10.1 --- a/src/cdrom/macosx/AudioFilePlayer.h	Fri Aug 20 22:35:23 2004 +0000
    10.2 +++ b/src/cdrom/macosx/AudioFilePlayer.h	Sat Aug 21 02:06:30 2004 +0000
    10.3 @@ -31,7 +31,6 @@
    10.4  
    10.5  #include <CoreServices/CoreServices.h>
    10.6  
    10.7 -#include <AudioToolbox/AudioConverter.h>
    10.8  #include <AudioUnit/AudioUnit.h>
    10.9  
   10.10  #include "SDL_error.h"
   10.11 @@ -65,8 +64,7 @@
   10.12      
   10.13      ~AudioFilePlayer();
   10.14  
   10.15 -    void            SetDestination (AudioUnit                   &inDestUnit, 
   10.16 -                                int                             inBusNumber);
   10.17 +    void            SetDestination (AudioUnit &inDestUnit);
   10.18      
   10.19      void            SetNotifier (AudioFilePlayNotifier inNotifier, void *inRefCon)
   10.20      {
   10.21 @@ -88,26 +86,18 @@
   10.22      
   10.23      bool            IsConnected () const { return mConnected; }
   10.24  
   10.25 -    UInt32          GetBusNumber () const { return mBusNumber; }
   10.26 -    
   10.27      AudioUnit       GetDestUnit () const { return mPlayUnit; }
   10.28      
   10.29 -    AudioConverterRef   GetAudioConverter() const { return mConverter; }
   10.30 -
   10.31  #if DEBUG    
   10.32      void            Print() const 
   10.33      {
   10.34 -        printf ("Destination Bus:%ld\n", GetBusNumber());
   10.35          printf ("Is Connected:%s\n", (IsConnected() ? "true" : "false"));
   10.36          printf ("- - - - - - - - - - - - - - \n");
   10.37      }
   10.38  #endif
   10.39 -
   10.40 -    const AudioStreamBasicDescription&      GetFileFormat() const { return mFileDescription; }
   10.41      
   10.42  private:
   10.43      AudioUnit                       mPlayUnit;
   10.44 -    UInt32                          mBusNumber;
   10.45      SInt16                          mForkRefNum;
   10.46      
   10.47      AudioUnitInputCallback          mInputCallback;
   10.48 @@ -117,7 +107,6 @@
   10.49      bool                            mConnected;
   10.50      
   10.51      AudioFileManager*               mAudioFileManager;
   10.52 -    AudioConverterRef               mConverter;
   10.53      
   10.54      AudioFilePlayNotifier           mNotifier;
   10.55      void*                           mRefCon;
   10.56 @@ -141,15 +130,11 @@
   10.57      ~AudioFileManager();
   10.58      
   10.59      
   10.60 -    void                Connect (AudioConverterRef inConverter) 
   10.61 -    {
   10.62 -        mParentConverter = inConverter;
   10.63 -        DoConnect();
   10.64 -    }
   10.65 -
   10.66          // this method should NOT be called by an object of this class
   10.67          // as it is called by the parent's Disconnect() method
   10.68      void                Disconnect ();
   10.69 +    
   10.70 +    void                DoConnect ();
   10.71  
   10.72      OSStatus            Read(char *buffer, UInt32 *len);
   10.73  
   10.74 @@ -165,7 +150,6 @@
   10.75     
   10.76  protected:
   10.77      AudioFilePlayer&    mParent;
   10.78 -    AudioConverterRef   mParentConverter;
   10.79      SInt16              mForkRefNum;
   10.80      SInt64              mAudioDataOffset;
   10.81      
   10.82 @@ -179,6 +163,10 @@
   10.83      
   10.84      int                 mNumTimesAskedSinceFinished;
   10.85  
   10.86 +
   10.87 +	void*               mTmpBuffer;
   10.88 +	UInt32              mBufferSize;
   10.89 +	UInt32              mBufferOffset;
   10.90  public:
   10.91      const UInt32        mChunkSize;
   10.92      SInt64              mFileLength;
   10.93 @@ -190,8 +178,6 @@
   10.94      OSStatus            Render (AudioBuffer &ioData);
   10.95      
   10.96      OSStatus            GetFileData (void** inOutData, UInt32 *inOutDataSize);
   10.97 -    
   10.98 -    void                DoConnect ();
   10.99          
  10.100      void                AfterRender ();
  10.101  
  10.102 @@ -201,10 +187,6 @@
  10.103                                          const AudioTimeStamp            *inTimeStamp, 
  10.104                                          UInt32                          inBusNumber, 
  10.105                                          AudioBuffer                     *ioData);
  10.106 -    static OSStatus     ACInputProc (AudioConverterRef          inAudioConverter,
  10.107 -                                            UInt32*                     outDataSize,
  10.108 -                                            void**                      outData,
  10.109 -                                            void*                       inUserData);
  10.110  };
  10.111  
  10.112  
    11.1 --- a/src/cdrom/macosx/AudioFileReaderThread.cpp	Fri Aug 20 22:35:23 2004 +0000
    11.2 +++ b/src/cdrom/macosx/AudioFileReaderThread.cpp	Sat Aug 21 02:06:30 2004 +0000
    11.3 @@ -287,6 +287,8 @@
    11.4        mLockUnsuccessful (false),
    11.5        mIsEngaged (false),
    11.6  
    11.7 +      mBufferSize (inChunkSize),
    11.8 +      mBufferOffset (inChunkSize),
    11.9        mChunkSize (inChunkSize),
   11.10        mFileLength (inFileLength),
   11.11        mReadFilePosition (0),
   11.12 @@ -364,7 +366,7 @@
   11.13      
   11.14      if (mReadFromFirstBuffer == mWriteToFirstBuffer) {
   11.15          #if DEBUG
   11.16 -        printf ("* * * * * * * Can't keep up with reading file:%ld\n", mParent.GetBusNumber());
   11.17 +        printf ("* * * * * * * Can't keep up with reading file\n");
   11.18          #endif
   11.19          
   11.20          mParent.DoNotification (kAudioFilePlayErr_FilePlayUnderrun);
    12.1 --- a/src/cdrom/macosx/CDPlayer.cpp	Fri Aug 20 22:35:23 2004 +0000
    12.2 +++ b/src/cdrom/macosx/CDPlayer.cpp	Sat Aug 21 02:06:30 2004 +0000
    12.3 @@ -68,8 +68,6 @@
    12.4  
    12.5  static OSStatus CheckInit ();
    12.6  
    12.7 -static OSStatus MatchAUFormats (AudioUnit theUnit, UInt32 theInputBus);
    12.8 -
    12.9  static void     FilePlayNotificationHandler (void* inRefCon, OSStatus inStatus);
   12.10  
   12.11  static int      RunCallBackThread (void* inRefCon);
   12.12 @@ -423,7 +421,7 @@
   12.13              throw (-3);
   12.14          }
   12.15              
   12.16 -        thePlayer->SetDestination(theUnit, 0);
   12.17 +        thePlayer->SetDestination(theUnit);
   12.18          
   12.19          if (startFrame >= 0)
   12.20              thePlayer->SetStartFrame (startFrame);
   12.21 @@ -605,16 +603,6 @@
   12.22              THROW_RESULT("CheckInit: AudioUnitInitialize")
   12.23          
   12.24                      
   12.25 -        // In this case we first want to get the output format of the OutputUnit
   12.26 -        // Then we set that as the input format. Why?
   12.27 -        // So that only a single conversion process is done
   12.28 -        // when SetDestination is called it will get the input format of the
   12.29 -        // unit its supplying data to. This defaults to 44.1K, stereo, so if
   12.30 -        // the device is not that, then we lose a possibly rendering of data
   12.31 -        
   12.32 -        result = MatchAUFormats (theUnit, 0);
   12.33 -            THROW_RESULT("CheckInit: MatchAUFormats")
   12.34 -    
   12.35          playBackWasInit = true;
   12.36      }
   12.37      catch (...)
   12.38 @@ -625,29 +613,6 @@
   12.39      return 0;
   12.40  }
   12.41  
   12.42 -
   12.43 -static OSStatus MatchAUFormats (AudioUnit theUnit, UInt32 theInputBus)
   12.44 -{
   12.45 -    AudioStreamBasicDescription theDesc;
   12.46 -    UInt32 size = sizeof (theDesc);
   12.47 -    OSStatus result = AudioUnitGetProperty (theUnit,
   12.48 -                                            kAudioUnitProperty_StreamFormat,
   12.49 -                                            kAudioUnitScope_Output,
   12.50 -                                            0,
   12.51 -                                            &theDesc,
   12.52 -                                            &size);
   12.53 -        THROW_RESULT("MatchAUFormats: AudioUnitGetProperty")
   12.54 -
   12.55 -    result = AudioUnitSetProperty (theUnit,
   12.56 -                                   kAudioUnitProperty_StreamFormat,
   12.57 -                                   kAudioUnitScope_Input,
   12.58 -                                   theInputBus,
   12.59 -                                   &theDesc,
   12.60 -                                   size);
   12.61 -    
   12.62 -    return result;
   12.63 -}
   12.64 -
   12.65  static void FilePlayNotificationHandler(void * inRefCon, OSStatus inStatus)
   12.66  {
   12.67      if (inStatus == kAudioFilePlay_FileIsFinished) {