FB II Compiler

PG PRO

Debugging

Memory

System

Mathematics

Resources

Disk I/O

Windows

Controls

Menus

Mouse

Keyboard

Text

Fonts

Drawing

Sound

Clipboard

Printing

Communication

ASM

Made with FB

SOUND

Use Async sound procedure


Heres my code for doing sounds and callbacks

DIM channel1&,channel2&:' pointers to the sound channels
DIM sndHndl1&,sndHndl2&:' for two sounds
DIM result,pigsCanFly
DIM sndLoop,doSound,soundsOn
DIM rect;8:' used for drawing on the screen
DIM gPt;0,mY,mX
'
DIM sndChanRec;24:' we allow 24 bytes for the sound channel record
DIM sndManRec;6:' used for info about the sound manager
'
END GLOBALS
'
LOCAL FN SetupChannel(whichSynth,InitParam)
  sndPtr&=0:' set this to zero to start with
  CB&=LINE "SndCallBack":' this is how we specify a callback routine
  OSErr=FN SNDNEWCHANNEL(sndPtr&,whichSynth,InitParam,CB&):' SND channel
  LONG IF OSErr<>_noErr:' make sure we didn't get an error
    sndPtr&=0:' we return a zero
  END IF
END FN=sndPtr&:' returns a sound channel pointer
'
LOCAL FN PlaySound(Hndl&,whichChannel&)
  LONG IF Hndl&:' make sure we got a valid handle
    LONG IF whichChannel&:' make sure there is a channel to play this in
      OSErr=FN SNDPLAY(whichChannel&,Hndl&,-1):' send play cmd and return
      soundsOn=_True:' mark as playing a sound
    END IF
  END IF
END FN
'
LOCAL FN SndCmdNow(whichChannel&,theCmd,pram1,pram2&)
  DIM SndCmd,SndParam1,SndParam2&
  SndCmd=theCmd
  SndParam1=pram1
  SndParam2&=pram2&
  OSErr=FN SNDDOIMMEDIATE(whichChannel&,SndCmd)
END FN
'
LOCAL FN SndCmdOnly(whichChannel&,theCmd,pram1,pram2&)
  DIM SndCmd,SndParam1,SndParam2&
  SndCmd=theCmd
  SndParam1=pram1
  SndParam2&=pram2&
  OSErr=FN SNDDOCOMMAND(whichChannel&,SndCmd,_False)
END FN
'
"SndCallBack"
ENTERPROC(chanPtr&,SndCmdPtr&):' what was the thing they smacked
'chanPtr& is the channel pointer that generated the callback
 'PRINT "Cmd Word :";{SndCmdPtr&}:' this is the callback command issued (13)
'PRINT "Cmd Pram1 (INT):";{SndCmdPtr&+2}:' read out the first parameter
'PRINT "Cmd Pram2 (LONG INT):";[SndCmdPtr&+4]:' read out second parameter
  LONG IF doSound:' do we want to do this routine
    SELECT {SndCmdPtr&+2}:' see if playing sound verse or chorus
      CASE _VerseSnd:' if the first
        INC(sndLoop):' add 1 to loop value
        LONG IF sndLoop<2
'FN ShowMessage$("Playing the Verse")
          FN PlaySound(sndHndl1&,channel1&):' go play this sound from this chan
          FN SndCmdOnly(channel1&,_callBackCmd,_VerseSnd,_VerseSnd)
        XELSE
'FN ShowMessage$("Playing the Chorus Part 1")
          FN PlaySound(sndHndl2&,channel1&):' go play this sound from this chan
          FN SndCmdOnly(channel1&,_callBackCmd,_ChorusSnd,_ChorusSnd)
        END IF
      CASE _ChorusSnd
        INC(sndLoop):' add 1 to sound loop
        LONG IF sndLoop>3
          sndLoop=1:' reset to the start of the loop
'FN ShowMessage$("Playing the Verse")
          FN PlaySound(sndHndl1&,channel1&):' go play this sound from this chan
          FN SndCmdOnly(channel1&,_callBackCmd,_VerseSnd,_VerseSnd)
        XELSE
'FN ShowMessage$("Repeating Chorus Part 2")
          FN PlaySound(sndHndl2&,channel1&):' go play this sound from this chan
          FN SndCmdOnly(channel1&,_callBackCmd,_ChorusSnd,_ChorusSnd)
        END IF
    END SELECT
  END IF
EXITPROC
'
LOCAL FN DisposeSndChannel(theChannel&):' get the channel and dispose of it
  LONG IF theChannel&:' make sure there was one allocated first
    OSErr=FN SNDDISPOSECHANNEL(theChannel&,0):' get rid of it now
  END IF
END FN
'
"BEGIN"
channel1&=FN SetupChannel(_sampledSynth,_initStereo):' go set it up
LONG IF channel1&:' make sure we got a channel
  FN ShowMessage$("Sound Channel ONE Allocated"):DELAY 1000
END IF
channel2&=FN SetupChannel(_sampledSynth,_initStereo):' go set it up
LONG IF channel2&:' make sure we got a channel
  FN ShowMessage$("Sound Channel TWO Allocated"):DELAY 1000
END IF
sndHndl1&=FN LoadSound(_VerseSnd)
LONG IF sndHndl1&:' see if we loaded the sound ok
  FN ShowMessage$("Verse loaded OK"):DELAY 1000
END IF
sndHndl2&=FN LoadSound(_ChorusSnd)
LONG IF sndHndl2&:' see if we loaded the sound ok
  FN ShowMessage$("Chorus loaded OK"):DELAY 1000
END IF
pigsCanFly=_False:' not yet they can't
doSound=_True:' we are allowed to make noise
soundsOn=_False:' and the sounds are presently off

Mel Patrick