Files
Aerofoil/GpApp/Sound.cpp
2019-12-29 22:12:11 -05:00

470 lines
10 KiB
C++

//============================================================================
//----------------------------------------------------------------------------
// Sound.c
//----------------------------------------------------------------------------
//============================================================================
#include "PLResources.h"
#include "PLSound.h"
#include "Externs.h"
#include "SoundSync.h"
#include "VirtualDirectory.h"
#define kBaseBufferSoundID 1000
#define kMaxSounds 64
void CallBack0 (SndChannelPtr, SndCommand *);
void CallBack1 (SndChannelPtr, SndCommand *);
void CallBack2 (SndChannelPtr, SndCommand *);
PLError_t LoadBufferSounds (void);
void DumpBufferSounds (void);
PLError_t OpenSoundChannels (void);
PLError_t CloseSoundChannels (void);
SndCallBackUPP callBack0UPP, callBack1UPP, callBack2UPP;
SndChannelPtr channel0, channel1, channel2;
Ptr theSoundData[kMaxSounds];
short numSoundsLoaded;
Boolean soundLoaded[kMaxSounds], dontLoadSounds;
Boolean channelOpen, isSoundOn, failedSound;
//============================================================== Functions
//-------------------------------------------------------------- PlayPrioritySound
void PlayPrioritySound (short which, short priority)
{
short lowestPriority, whosLowest;
if (failedSound || dontLoadSounds)
return;
SoundSyncState ss = SoundSync_ReadAll();
if ((priority == kTriggerPriority) &&
((ss.priority0 == kTriggerPriority) ||
((ss.priority1 == kTriggerPriority)) ||
((ss.priority2 == kTriggerPriority))))
return;
whosLowest = 0;
lowestPriority = ss.priority0;
if (ss.priority1 < lowestPriority)
{
lowestPriority = ss.priority1;
whosLowest = 1;
}
if (ss.priority2 < lowestPriority)
{
lowestPriority = ss.priority2;
whosLowest = 2;
}
if (priority >= lowestPriority)
{
PlayExclusiveSoundChannel(whosLowest, which, lowestPriority, priority);
}
}
//-------------------------------------------------------------- FlushAnyTriggerPlaying
void FlushAnyTriggerPlaying (void)
{
SndCommand theCommand;
PLError_t theErr;
SoundSyncState ss = SoundSync_ReadAll();
if (ss.priority0 == kTriggerPriority)
{
theCommand.cmd = quietCmd;
theCommand.param1 = 0;
theCommand.param2 = 0;
theErr = SndDoImmediate(channel0, &theCommand);
theCommand.cmd = flushCmd;
theCommand.param1 = 0;
theCommand.param2 = 0;
theErr = SndDoImmediate(channel0, &theCommand);
}
if (ss.priority1 == kTriggerPriority)
{
theCommand.cmd = quietCmd;
theCommand.param1 = 0;
theCommand.param2 = 0;
theErr = SndDoImmediate(channel1, &theCommand);
theCommand.cmd = flushCmd;
theCommand.param1 = 0;
theCommand.param2 = 0;
theErr = SndDoImmediate(channel1, &theCommand);
}
if (ss.priority2 == kTriggerPriority)
{
theCommand.cmd = quietCmd;
theCommand.param1 = 0;
theCommand.param2 = 0;
theErr = SndDoImmediate(channel2, &theCommand);
theCommand.cmd = flushCmd;
theCommand.param1 = 0;
theCommand.param2 = 0;
theErr = SndDoImmediate(channel2, &theCommand);
}
}
//-------------------------------------------------------------- PlaySound0
void PlayExclusiveSoundChannel(short channelIndex, short soundID, short oldPriority, short newPriority)
{
SndCommand theCommand;
PLError_t theErr;
if (failedSound || dontLoadSounds)
return;
SndChannelPtr channel = nil;
switch (channelIndex)
{
case 0:
channel = channel0;
break;
case 1:
channel = channel2;
break;
case 2:
channel = channel2;
break;
default:
return;
}
theErr = PLErrors::kNone;
if (isSoundOn)
{
if (oldPriority != 0)
{
// Flush the queue and stop the channel, which will remove the pending callback
theCommand.cmd = flushCmd;
theCommand.param1 = 0;
theCommand.param2 = 0;
theErr = SndDoImmediate(channel, &theCommand);
theCommand.cmd = quietCmd;
theCommand.param1 = 0;
theCommand.param2 = 0;
theErr = SndDoImmediate(channel, &theCommand);
SoundSync_ClearPriority(channelIndex);
}
SoundSync_PutPriority(channelIndex, newPriority);
theCommand.cmd = bufferCmd;
theCommand.param1 = 0;
theCommand.param2 = (intptr_t)(theSoundData[soundID]);
theErr = SndDoCommand(channel, &theCommand, true);
theCommand.cmd = callBackCmd;
theCommand.param1 = 0;
theCommand.param2 = 0;
theErr = SndDoCommand(channel, &theCommand, true);
if (theErr != PLErrors::kNone)
SoundSync_ClearPriority(channelIndex);
}
}
//-------------------------------------------------------------- CallBack0
void CallBack0 (SndChannelPtr theChannel, SndCommand *theCommand)
{
SoundSync_ClearPriority(0);
}
//-------------------------------------------------------------- CallBack1
void CallBack1 (SndChannelPtr theChannel, SndCommand *theCommand)
{
SoundSync_ClearPriority(1);
}
//-------------------------------------------------------------- CallBack2
void CallBack2 (SndChannelPtr theChannel, SndCommand *theCommand)
{
SoundSync_ClearPriority(2);
}
//-------------------------------------------------------------- LoadTriggerSound
PLError_t LoadTriggerSound (short soundID)
{
Handle theSound;
long soundDataSize;
PLError_t theErr;
if ((dontLoadSounds) || (theSoundData[kMaxSounds - 1] != nil))
theErr = PLErrors::kFileNotFound;
else
{
// FlushAnyTriggerPlaying();
theErr = PLErrors::kNone;
theSound = GetResource('snd ', soundID);
if (theSound == nil)
{
theErr = PLErrors::kFileNotFound;
}
else
{
soundDataSize = GetHandleSize(theSound) - 20L;
theSoundData[kMaxSounds - 1] = NewPtr(soundDataSize);
if (theSoundData[kMaxSounds - 1] == nil)
{
DisposeHandle(theSound);
theErr = PLErrors::kOutOfMemory;
}
else
{
BlockMove((Ptr)((Byte*)(*theSound) + 20L), theSoundData[kMaxSounds - 1], soundDataSize);
DisposeHandle(theSound);
}
}
}
return (theErr);
}
//-------------------------------------------------------------- DumpTriggerSound
void DumpTriggerSound (void)
{
if (theSoundData[kMaxSounds - 1] != nil)
DisposePtr(theSoundData[kMaxSounds - 1]);
theSoundData[kMaxSounds - 1] = nil;
}
//-------------------------------------------------------------- LoadBufferSounds
PLError_t LoadBufferSounds (void)
{
Handle theSound;
long soundDataSize;
PLError_t theErr;
short i;
theErr = PLErrors::kNone;
for (i = 0; i < kMaxSounds - 1; i++)
{
theSound = GetResource('snd ', i + kBaseBufferSoundID);
if (theSound == nil)
return (PLErrors::kOutOfMemory);
soundDataSize = GetHandleSize(theSound) - 20L;
theSoundData[i] = NewPtr(soundDataSize);
if (theSoundData[i] == nil)
return (PLErrors::kOutOfMemory);
BlockMove((Ptr)((Byte*)(*theSound) + 20L), theSoundData[i], soundDataSize);
DisposeHandle(theSound);
}
theSoundData[kMaxSounds - 1] = nil;
return (theErr);
}
//-------------------------------------------------------------- DumpBufferSounds
void DumpBufferSounds (void)
{
short i;
for (i = 0; i < kMaxSounds; i++)
{
if (theSoundData[i] != nil)
DisposePtr(theSoundData[i]);
theSoundData[i] = nil;
}
}
//-------------------------------------------------------------- OpenSoundChannels
PLError_t OpenSoundChannels (void)
{
PLError_t theErr;
callBack0UPP = NewSndCallBackProc(CallBack0);
callBack1UPP = NewSndCallBackProc(CallBack1);
callBack2UPP = NewSndCallBackProc(CallBack2);
theErr = PLErrors::kNone;
if (channelOpen)
return (theErr);
theErr = SndNewChannel(&channel0,
sampledSynth, initNoInterp + initMono,
(SndCallBackUPP)callBack0UPP);
if (theErr == PLErrors::kNone)
channelOpen = true;
else
return (theErr);
theErr = SndNewChannel(&channel1,
sampledSynth, initNoInterp + initMono,
(SndCallBackUPP)callBack1UPP);
if (theErr == PLErrors::kNone)
channelOpen = true;
else
return (theErr);
theErr = SndNewChannel(&channel2,
sampledSynth, initNoInterp + initMono,
(SndCallBackUPP)callBack2UPP);
if (theErr == PLErrors::kNone)
channelOpen = true;
return (theErr);
}
//-------------------------------------------------------------- CloseSoundChannels
PLError_t CloseSoundChannels (void)
{
PLError_t theErr;
theErr = PLErrors::kNone;
if (!channelOpen)
return (theErr);
if (channel0 != nil)
theErr = SndDisposeChannel(channel0, true);
channel0 = nil;
if (channel1 != nil)
theErr = SndDisposeChannel(channel1, true);
channel1 = nil;
if (channel2 != nil)
theErr = SndDisposeChannel(channel2, true);
channel2 = nil;
if (theErr == PLErrors::kNone)
channelOpen = false;
DisposeSndCallBackUPP(callBack0UPP);
DisposeSndCallBackUPP(callBack1UPP);
DisposeSndCallBackUPP(callBack2UPP);
return (theErr);
}
//-------------------------------------------------------------- InitSound
void InitSound (void)
{
PLError_t theErr;
if (dontLoadSounds)
return;
failedSound = false;
channel0 = nil;
channel1 = nil;
channel2 = nil;
SoundSync_ClearPriority(0);
SoundSync_ClearPriority(1);
SoundSync_ClearPriority(2);
theErr = LoadBufferSounds();
if (theErr != PLErrors::kNone)
{
YellowAlert(kYellowFailedSound, theErr);
failedSound = true;
}
if (!failedSound)
{
theErr = OpenSoundChannels();
if (theErr != PLErrors::kNone)
{
YellowAlert(kYellowFailedSound, theErr);
failedSound = true;
}
}
}
//-------------------------------------------------------------- KillSound
void KillSound (void)
{
PLError_t theErr;
if (dontLoadSounds)
return;
DumpBufferSounds();
theErr = CloseSoundChannels();
}
//-------------------------------------------------------------- SoundBytesNeeded
long SoundBytesNeeded (void)
{
Handle theSound;
long totalBytes;
short i;
totalBytes = 0L;
SetResLoad(false);
for (i = 0; i < kMaxSounds - 1; i++)
{
theSound = GetResource('snd ', i + kBaseBufferSoundID);
if (theSound == nil)
{
SetResLoad(true);
return -1;
}
totalBytes += GetMaxResourceSize(theSound);
// ReleaseResource(theSound);
}
SetResLoad(true);
return totalBytes;
}
//-------------------------------------------------------------- TellHerNoSounds
void TellHerNoSounds (void)
{
#define kNoMemForSoundsAlert 1039
short hitWhat;
// CenterAlert(kNoMemForSoundsAlert);
hitWhat = Alert(kNoMemForSoundsAlert, nil);
}
//-------------------------------------------------------------- BitchAboutSM3
void BitchAboutSM3 (void)
{
#define kNoSoundManager3Alert 1030
short hitWhat;
// CenterAlert(kNoSoundManager3Alert);
hitWhat = Alert(kNoSoundManager3Alert, nil);
}