  | |  | And another stupid question... (playing sounds) | And another stupid question... (playing sounds) 2003-12-12 - By Jeff Hester
Back Dennis (and others),
This is part of the sound code that we use for Carbon apps...
playSound is used for playing sounds that have been loaded from resources using loadSounds(). PlaySoundFromHandle is used to play a sound that I load, play, and then release the Handle myself. InitSoundEngine is obviously called once at the beginning of the program followed by closeSoundEngine at program exit.
Is this the best sound playing code. I'm sure it's not. It's worked great for us so far targeting Carbon.
I'm confident other suggestions or recommendations will come from experts on this list.
Hope this helps.
-- Jeff
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- --- Have some balloon fun with our newest game, Pop the Wheezil -- Temple Tantrum for OS X coming soon!! -- Ground Zero Software, Inc. http://www.GroundZeroSW.com/ Ground Zero Hosting http://www.GroundZeroHosting.com/ ## Master your domain. ## -- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---
void initSoundEngine(void) { gSoundAvailable = true;
numberOfChannels = 0;
for (i = 0;i < kSoundChannelsWanted;i++) { if (SndNewChannel(&mySoundChannels[i],sampledSynth,initMono,nil) == noErr) numberOfChannels++; else { errorHandler(kCreatingSoundChannelError); } } if (numberOfChannels <= 0) { numberOfChannels = 0; gSoundAvailable = false; gPlaySound = false; } }
void closeSoundEngine(void) { if (!gSoundAvailable) // If sound channels never were allocated exit. return;
for (i = 0;i < numberOfChannels;i++) { SndDisposeChannel(mySoundChannels[i],true); }
setVolumeLevel(gCurrentVolumeLevel); // My routine for setting the volume back.
}
Handle getSoundFromFile(short soundID) { short soundFile,savedResFile; Handle theSound; if (!gPlaySound) return(nil); savedResFile = CurResFile();
soundFile = openSoundFile(); UseResFile(soundFile); if (ResError()) errorHandler(kCantUseSoundsFile);
theSound = GetResource('snd ',soundID); // Check for error. Sound was not found or we didn't have the memory to load it. if (ResError() == memFullErr) // Out of memory errorHandler(kOutOfMemoryForSound); else if (ResError()) // Some other memory. Probably file not found. errorHandler(kSoundNotFound); else // We have the sound. Let's go ahead and lock it. { HLock(theSound); if (MemError()) errorHandler(kUnableToHLockHiSound);
DetachResource(theSound); if (ResError()) errorHandler(kUnableToDetachSound); }
CloseResFile(soundFile); if (ResError()) errorHandler(kCantCloseSoundFile);
UseResFile(savedResFile); if (ResError()) errorHandler(kCantReturnAfterSoundFile);
return theSound; }
Boolean soundIsPlaying(void) { if (!gSoundAvailable) return(false); for (i = 0;i < numberOfChannels;i++) // Loop through all open sound channels to see if { // if one of the channels is busy. if (thisChannelBusy(i)) return(true); } return(false); // None of the sound channels were busy. }
Boolean thisChannelBusy(short thisChannel) { SCStatus status; OSErr theErr; Boolean busy; if (!gPlaySound) return(false);
busy = false; theErr = SndChannelStatus(mySoundChannels[thisChannel],sizeof(SCStatus),&status); if (theErr == noErr) busy = status.scChannelBusy; return busy; }
void waitTillSoundIsFinished(void) { if (!gPlaySound) return; while (soundIsPlaying()); }
short playSoundFromHandle(Handle theSound,short stereoPosition) { short i;
if (!gPlaySound) return (0);
for (i = 0;i < numberOfChannels;i++) { if (!thisChannelBusy(i)) { SndPlay(mySoundChannels[i],(SndListHandle)theSound,true); return(i); // Tell me which channel the sound is being played on. } } }
void quietThisChannel(short theChannel) { SndCommand theCmd; if (!gPlaySound) return; theCmd.param1 = 0; theCmd.param2 = 0; theCmd.cmd = quietCmd; SndDoImmediate(mySoundChannels[theChannel],&theCmd); theCmd.cmd = flushCmd; SndDoImmediate(mySoundChannels[theChannel],&theCmd); }
void stopAllSounds(void) { short i; if (!gPlaySound) return; for (i = 0;i < numberOfChannels;i++) { if (thisChannelBusy(i)) quietThisChannel(i); } }
void loadSounds(void) // This loads many of the sounds into memory. This will prevent having { // to load them from disk each time they need to be played. short soundFile,savedResFile; savedResFile = CurResFile(); soundFile = openSoundFile(); UseResFile(soundFile); if (ResError()) errorHandler(kCantUseSoundsFile);
// Load each sound and check for error. // Repeat from here ... menuSelectionSound = GetResource('snd ',kMenuSelectionSound);
if (ResError() == memFullErr) // Out of memory errorHandler(kOutOfMemoryForSound); else if (ResError()) // Some other memory. Probably file not found. errorHandler(kSoundNotFound); else // We have the sound. Let's go ahead and lock it. { HLockHi(menuSelectionSound); if (MemError()) errorHandler(kUnableToHLockHiSound);
DetachResource(menuSelectionSound); if (ResError()) errorHandler(kUnableToDetachSound); }
//... to here for each sound resource you want to load.
CloseResFile(soundFile); if (ResError()) errorHandler(kCantCloseSoundFile);
UseResFile(savedResFile); if (ResError()) errorHandler(kCantReturnAfterSoundFile); }
void disposeOfSounds(void) // This disposes of all sounds that were loaded. { waitTillSoundIsFinished(); // Repeat from here... if (menuSelectionSound) { HUnlock(menuSelectionSound); if (MemError()) errorHandler(kUnableToUnlockSound); DisposeHandle(menuSelectionSound); if (MemError()) errorHandler(kUnableToReleaseSound); } // ... To here for each sound that you loaded from a resource. }
short playSound(short whichSound,short stereoPosition) { short theChannel; Boolean checkChannels; OSErr soundErr = noErr; if (!gPlaySound) return;
// First we must find an open channel.
theChannel = 0; checkChannels = true; while (checkChannels) { if (!thisChannelBusy(theChannel)) { checkChannels = false; // We found and open channel } else // We continue looking by incrementing the channels { theChannel++; if (theChannel >= numberOfChannels) // We've gone too far. Let's remove the sound { // in the oldest channel (0) and use it. checkChannels = false; theChannel = 0; quietThisChannel(theChannel); } } }
// Now that we have a channel let's play the sound we want and set stereo values we need. switch(whichSound) { case kThiefGrabsSound: soundErr = SndPlay(mySoundChannels[theChannel],(SndListHandle)thiefGrabsSound,true); break; // Add as many case statements for each sound you want to play.
}
if (soundErr != noErr) errorHandler(kSoundPlayError); return(theChannel); // We return the channel in case we want to change the stereo position. } // End of PlaySound __ ____ ____ ____ ____ ____ ____ ____ ____ ____ mac-games-dev mailing list | mac-games-dev@(protected) Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/mac -games-dev Do not post admin requests to the list. They will be ignored.
Earn $52 per hosting referral at Lunarpages.
|
|
 |