home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-08-28 | 4.8 KB | 210 lines | [TEXT/CWIE] |
- // ===========================================================================
- // USoundPlayer.cp © 1995, Éric Forget. All rights reserved.
- // ===========================================================================
- //
- // ************************************************************************
- // * *
- // * Before using this code you should read the "License Agreement" *
- // * document and agree with it. *
- // * *
- // ************************************************************************
- //
- // USoundPlayer is a wrapper class to make life easier when playing a
- // sound handle. You can play a sound by name, resource ID or SndListHandle.
- //
- // ---------------------------------------------------------------------------
- //
- // Instruction Notes:
- // -----------------
- //
- // Call one of the USoundPlayer::PlaySound() method. That's all!
- //
- // ---------------------------------------------------------------------------
-
- #include "USoundPlayer.h"
-
-
-
- // ---------------------------------------------------------------------------
- // • Static member
- // ---------------------------------------------------------------------------
-
- USoundPlayer *USoundPlayer::sSoundPlayer = nil;
- SndListHandle USoundPlayer::sSoundHandle = nil;
- SndChannelPtr USoundPlayer::sSoundChannel = nil;
-
-
-
- // ---------------------------------------------------------------------------
- // • Initialize()
- // ---------------------------------------------------------------------------
-
- void
- USoundPlayer::Initialize()
- {
- if(sSoundPlayer == nil) {
-
- sSoundPlayer = new USoundPlayer;
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • Dispose
- // ---------------------------------------------------------------------------
-
- void
- USoundPlayer::Dispose()
- {
- if(sSoundPlayer != nil) {
-
- delete sSoundPlayer;
- sSoundPlayer = nil;
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • PlaySound
- // ---------------------------------------------------------------------------
-
- void
- USoundPlayer::PlaySound(
- ConstStr255Param inSoundName)
- {
- SndListHandle theSoundH = (SndListHandle)::GetNamedResource('snd ', inSoundName);
-
-
- if(theSoundH) {
-
- PlaySoundHandle(theSoundH);
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • PlaySound
- // ---------------------------------------------------------------------------
-
- void
- USoundPlayer::PlaySound(
- ResIDT inSoundID)
- {
- SndListHandle theSoundH = (SndListHandle)::GetResource('snd ', inSoundID);
-
-
- if(theSoundH) {
-
- PlaySoundHandle(theSoundH);
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • PlaySound
- // ---------------------------------------------------------------------------
-
- void
- USoundPlayer::PlaySound(
- SndListHandle inSoundHandle)
- {
- PlaySoundHandle(inSoundHandle);
- }
-
-
- // ---------------------------------------------------------------------------
- // • SpendTime
- // ---------------------------------------------------------------------------
-
- void
- USoundPlayer::SpendTime(
- const EventRecord & /*inMacEvent*/)
- {
- SCStatus theStatus;
- OSErr err;
-
-
- err = ::SndChannelStatus(sSoundChannel, sizeof(SCStatus), &theStatus);
-
-
- if ((err != noErr) || (!theStatus.scChannelBusy)) {
-
- StopCurrentSound();
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • PlaySoundHandle
- // ---------------------------------------------------------------------------
-
- void
- USoundPlayer::PlaySoundHandle(
- SndListHandle inSoundHandle)
- {
- OSErr err = noErr;
-
-
- Initialize();
- StopCurrentSound();
-
- sSoundHandle = inSoundHandle;
- err = ::HandToHand((Handle *)&(sSoundHandle));
-
- if (!err) {
-
- if (!sSoundChannel) {
-
- sSoundChannel = (SndChannelPtr) ::NewPtrClear(sizeof(SndChannel));
-
- if (sSoundChannel != nil) {
-
- sSoundChannel->qLength = stdQLength;
- err = ::SndNewChannel(&sSoundChannel, sampledSynth, 0, nil);
-
- if (err) {
-
- ::DisposePtr((Ptr)sSoundChannel);
- sSoundChannel = nil;
- }
- }
- }
-
- if (sSoundHandle && sSoundChannel) {
-
- ::HLockHi((Handle)sSoundHandle);
- err = ::SndPlay(sSoundChannel, sSoundHandle,true);
- }
- if (err) {
-
- StopCurrentSound();
-
- } else {
-
- sSoundPlayer->StartIdling();
- }
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • StopCurrentSound
- // ---------------------------------------------------------------------------
-
- void
- USoundPlayer::StopCurrentSound()
- {
- if (sSoundChannel) {
-
- ::SndDisposeChannel(sSoundChannel, true);
- sSoundChannel = nil;
- }
-
- if (sSoundHandle) {
-
- ::DisposeHandle((Handle)sSoundHandle);
- sSoundHandle = nil;
- }
-
- sSoundPlayer->StopIdling();
- }