19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__APPLE__) && defined(__MACH__)
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#  include <Carbon/Carbon.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335)
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#  include <Carbon.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#  include <Sound.h> /* SoundManager interface */
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#  include <Gestalt.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#  include <DriverServices.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(NewSndCallBackUPP) && (UNIVERSAL_INTERFACES_VERSION < 0x0335)
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(NewSndCallBackProc) /* avoid circular redefinition... */
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define NewSndCallBackUPP NewSndCallBackProc
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(NewSndCallBackUPP)
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define NewSndCallBackUPP NewSndCallBackProc
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysaudio.h"
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_romaudio.h"
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mac_CloseAudio(_THIS);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Mac_OpenAudio(_THIS, SDL_AudioSpec *spec);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mac_LockAudio(_THIS);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mac_UnlockAudio(_THIS);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void)
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(1);
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device)
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(device->hidden);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(device);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex)
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_AudioDevice *this;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Initialize all variables that we clean on shutdown */
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this ) {
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_memset(this, 0, (sizeof *this));
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        this->hidden = (struct SDL_PrivateAudioData *)
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_malloc((sizeof *this->hidden));
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( (this == NULL) || (this->hidden == NULL) ) {
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_OutOfMemory();
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( this ) {
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_free(this);
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(0);
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set the function pointers */
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->OpenAudio   = Mac_OpenAudio;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->CloseAudio  = Mac_CloseAudio;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->LockAudio   = Mac_LockAudio;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->UnlockAudio = Mac_UnlockAudio;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->free        = Audio_DeleteDevice;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __MACOSX__	/* Mac OS X uses threaded audio, so normal thread code is okay */
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->LockAudio   = NULL;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->UnlockAudio = NULL;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return this;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap SNDMGR_bootstrap = {
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"sndmgr", "MacOS SoundManager 3.0",
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Audio_Available, Audio_CreateDevice
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(TARGET_API_MAC_CARBON) || defined(USE_RYANS_SOUNDCODE)
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This works correctly on Mac OS X */
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma options align=power
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic volatile SInt32 audio_is_locked = 0;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic volatile SInt32 need_to_mix = 0;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic UInt8  *buffer[2];
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic volatile UInt32 running = 0;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CmpSoundHeader header;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic volatile Uint32 fill_me = 0;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void mix_buffer(SDL_AudioDevice *audio, UInt8 *buffer)
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   if ( ! audio->paused ) {
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __MACOSX__
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_mutexP(audio->mixer_lock);
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( audio->convert.needed ) {
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            audio->spec.callback(audio->spec.userdata,
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    (Uint8 *)audio->convert.buf,audio->convert.len);
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_ConvertAudio(&audio->convert);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( audio->convert.len_cvt != audio->spec.size ) {
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                /* Uh oh... probably crashes here */;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_memcpy(buffer, audio->convert.buf, audio->convert.len_cvt);
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            audio->spec.callback(audio->spec.userdata, buffer, audio->spec.size);
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __MACOSX__
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_mutexV(audio->mixer_lock);
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DecrementAtomic((SInt32 *) &need_to_mix);
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mac_LockAudio(_THIS)
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    IncrementAtomic((SInt32 *) &audio_is_locked);
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mac_UnlockAudio(_THIS)
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SInt32 oldval;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    oldval = DecrementAtomic((SInt32 *) &audio_is_locked);
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( oldval != 1 )  /* != 1 means audio is still locked. */
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Did we miss the chance to mix in an interrupt? Do it now. */
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( BitAndAtomic (0xFFFFFFFF, (UInt32 *) &need_to_mix) ) {
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /*
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         * Note that this could be a problem if you missed an interrupt
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         *  while the audio was locked, and get preempted by a second
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         *  interrupt here, but that means you locked for way too long anyhow.
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         */
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        mix_buffer (this, buffer[fill_me]);
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void callBackProc (SndChannel *chan, SndCommand *cmd_passed ) {
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   UInt32 play_me;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   SndCommand cmd;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   SDL_AudioDevice *audio = (SDL_AudioDevice *)chan->userInfo;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   IncrementAtomic((SInt32 *) &need_to_mix);
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   fill_me = cmd_passed->param2;  /* buffer that has just finished playing, so fill it */
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   play_me = ! fill_me;           /* filled buffer to play _now_ */
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   if ( ! audio->enabled ) {
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   }
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   /* queue previously mixed buffer for playback. */
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   header.samplePtr = (Ptr)buffer[play_me];
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   cmd.cmd = bufferCmd;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   cmd.param1 = 0;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   cmd.param2 = (long)&header;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   SndDoCommand (chan, &cmd, 0);
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   memset (buffer[fill_me], 0, audio->spec.size);
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   /*
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    * if audio device isn't locked, mix the next buffer to be queued in
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *  the memory block that just finished playing.
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    */
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   if ( ! BitAndAtomic(0xFFFFFFFF, (UInt32 *) &audio_is_locked) ) {
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      mix_buffer (audio, buffer[fill_me]);
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   }
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   /* set this callback to run again when current buffer drains. */
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   if ( running ) {
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      cmd.cmd = callBackCmd;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      cmd.param1 = 0;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      cmd.param2 = play_me;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SndDoCommand (chan, &cmd, 0);
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   }
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Mac_OpenAudio(_THIS, SDL_AudioSpec *spec) {
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   SndCallBackUPP callback;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   int sample_bits;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   int i;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   long initOptions;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   /* Very few conversions are required, but... */
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch (spec->format) {
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case AUDIO_S8:
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        spec->format = AUDIO_U8;
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case AUDIO_U16LSB:
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        spec->format = AUDIO_S16LSB;
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case AUDIO_U16MSB:
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        spec->format = AUDIO_S16MSB;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CalculateAudioSpec(spec);
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* initialize bufferCmd header */
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    memset (&header, 0, sizeof(header));
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    callback = (SndCallBackUPP) NewSndCallBackUPP (callBackProc);
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sample_bits = spec->size / spec->samples / spec->channels * 8;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr,
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"Audio format 0x%x, channels = %d, sample_bits = %d, frequency = %d\n",
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	spec->format, spec->channels, sample_bits, spec->freq);
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* DEBUG_AUDIO */
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    header.numChannels = spec->channels;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    header.sampleSize  = sample_bits;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    header.sampleRate  = spec->freq << 16;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    header.numFrames   = spec->samples;
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    header.encode      = cmpSH;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Note that we install the 16bitLittleEndian Converter if needed. */
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( spec->format == 0x8010 ) {
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        header.compressionID = fixedCompression;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        header.format = k16BitLittleEndianFormat;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* allocate 2 buffers */
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (i=0; i<2; i++) {
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       buffer[i] = (UInt8*)malloc (sizeof(UInt8) * spec->size);
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (buffer[i] == NULL) {
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         SDL_OutOfMemory();
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         return (-1);
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     memset (buffer[i], 0, spec->size);
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   }
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   /* Create the sound manager channel */
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    channel = (SndChannelPtr)SDL_malloc(sizeof(*channel));
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( channel == NULL ) {
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_OutOfMemory();
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(-1);
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( spec->channels >= 2 ) {
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        initOptions = initStereo;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        initOptions = initMono;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    channel->userInfo = (long)this;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    channel->qLength = 128;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SndNewChannel(&channel, sampledSynth, initOptions, callback) != noErr ) {
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("Unable to create audio channel");
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_free(channel);
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        channel = NULL;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(-1);
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   /* start playback */
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   {
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SndCommand cmd;
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      cmd.cmd = callBackCmd;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      cmd.param2 = 0;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      running = 1;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SndDoCommand (channel, &cmd, 0);
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   }
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   return 1;
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mac_CloseAudio(_THIS) {
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   int i;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   running = 0;
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   if (channel) {
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SndDisposeChannel (channel, true);
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      channel = NULL;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   }
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for ( i=0; i<2; ++i ) {
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( buffer[i] ) {
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_free(buffer[i]);
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            buffer[i] = NULL;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else /* !TARGET_API_MAC_CARBON && !USE_RYANS_SOUNDCODE */
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mac_LockAudio(_THIS)
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* no-op. */
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mac_UnlockAudio(_THIS)
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* no-op. */
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function is called by Sound Manager when it has exhausted one of
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   the buffers, so we'll zero it to silence and fill it with audio if
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   we're not paused.
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic pascal
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid sndDoubleBackProc (SndChannelPtr chan, SndDoubleBufferPtr newbuf)
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_AudioDevice *audio = (SDL_AudioDevice *)newbuf->dbUserInfo[0];
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* If audio is quitting, don't do anything */
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( ! audio->enabled ) {
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return;
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    memset (newbuf->dbSoundData, 0, audio->spec.size);
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    newbuf->dbNumFrames = audio->spec.samples;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( ! audio->paused ) {
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( audio->convert.needed ) {
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            audio->spec.callback(audio->spec.userdata,
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                (Uint8 *)audio->convert.buf,audio->convert.len);
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_ConvertAudio(&audio->convert);
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( audio->convert.len_cvt != audio->spec.size ) {
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                /* Uh oh... probably crashes here */;
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_memcpy(newbuf->dbSoundData, audio->convert.buf,
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            audio->convert.len_cvt);
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            audio->spec.callback(audio->spec.userdata,
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                (Uint8 *)newbuf->dbSoundData, audio->spec.size);
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    newbuf->dbFlags    |= dbBufferReady;
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DoubleBufferAudio_Available(void)
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int available;
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NumVersion sndversion;
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    long response;
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    available = 0;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sndversion = SndSoundManagerVersion();
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( sndversion.majorRev >= 3 ) {
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( Gestalt(gestaltSoundAttr, &response) == noErr ) {
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( (response & (1 << gestaltSndPlayDoubleBuffer)) ) {
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                available = 1;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( Gestalt(gestaltSoundAttr, &response) == noErr ) {
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( (response & (1 << gestaltHasASC)) ) {
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                available = 1;
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(available);
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Mac_CloseAudio(_THIS)
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( channel != NULL ) {
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Clean up the audio channel */
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SndDisposeChannel(channel, true);
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        channel = NULL;
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for ( i=0; i<2; ++i ) {
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( audio_buf[i] ) {
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_free(audio_buf[i]);
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            audio_buf[i] = NULL;
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Mac_OpenAudio(_THIS, SDL_AudioSpec *spec)
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SndDoubleBufferHeader2 audio_dbh;
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    long initOptions;
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int sample_bits;
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SndDoubleBackUPP doubleBackProc;
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Check to make sure double-buffered audio is available */
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( ! DoubleBufferAudio_Available() ) {
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("Sound manager doesn't support double-buffering");
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(-1);
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Very few conversions are required, but... */
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch (spec->format) {
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case AUDIO_S8:
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        spec->format = AUDIO_U8;
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case AUDIO_U16LSB:
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        spec->format = AUDIO_S16LSB;
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case AUDIO_U16MSB:
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        spec->format = AUDIO_S16MSB;
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CalculateAudioSpec(spec);
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* initialize the double-back header */
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(&audio_dbh, 0, sizeof(audio_dbh));
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    doubleBackProc = NewSndDoubleBackProc (sndDoubleBackProc);
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sample_bits = spec->size / spec->samples / spec->channels * 8;
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    audio_dbh.dbhNumChannels = spec->channels;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    audio_dbh.dbhSampleSize    = sample_bits;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    audio_dbh.dbhCompressionID = 0;
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    audio_dbh.dbhPacketSize    = 0;
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    audio_dbh.dbhSampleRate    = spec->freq << 16;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    audio_dbh.dbhDoubleBack    = doubleBackProc;
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    audio_dbh.dbhFormat    = 0;
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Note that we install the 16bitLittleEndian Converter if needed. */
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( spec->format == 0x8010 ) {
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        audio_dbh.dbhCompressionID = fixedCompression;
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        audio_dbh.dbhFormat = k16BitLittleEndianFormat;
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* allocate the 2 double-back buffers */
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for ( i=0; i<2; ++i ) {
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        audio_buf[i] = SDL_calloc(1, sizeof(SndDoubleBuffer)+spec->size);
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( audio_buf[i] == NULL ) {
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_OutOfMemory();
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return(-1);
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        audio_buf[i]->dbNumFrames = spec->samples;
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        audio_buf[i]->dbFlags = dbBufferReady;
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        audio_buf[i]->dbUserInfo[0] = (long)this;
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        audio_dbh.dbhBufferPtr[i] = audio_buf[i];
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Create the sound manager channel */
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    channel = (SndChannelPtr)SDL_malloc(sizeof(*channel));
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( channel == NULL ) {
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_OutOfMemory();
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(-1);
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( spec->channels >= 2 ) {
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        initOptions = initStereo;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        initOptions = initMono;
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    channel->userInfo = 0;
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    channel->qLength = 128;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SndNewChannel(&channel, sampledSynth, initOptions, 0L) != noErr ) {
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("Unable to create audio channel");
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_free(channel);
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        channel = NULL;
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(-1);
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Start playback */
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SndPlayDoubleBuffer(channel, (SndDoubleBufferHeaderPtr)&audio_dbh)
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                != noErr ) {
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("Unable to play double buffered audio");
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(-1);
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 1;
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* TARGET_API_MAC_CARBON || USE_RYANS_SOUNDCODE */
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
497