[comp.sys.mac.programmer] SoundPlay in Async mode

milo@ndmath.UUCP (Greg Corson) (04/08/89)

Would anyone happen to have a code fragment that illustrates how to setup
the required callback routines so you can have SoundPlay play a sound in the
background (async) mode?

The documentation I have (the revised IM chapter) isn't clear at all on how
to do this.

I'm working on a game and have to play sounds like gunshots, screams...etc
without interrupting the screen animation.  I need to know the proper way to

1. open the channel and specify a callback
2. play a snd resource
3. have the callback set a global flag
4. have my main event loop close the channel when the flag is set.

Thanks!

Greg Corson
19141 Summers Drive
South Bend, IN 46637
(219) 277-5306 
{pur-ee,rutgers,uunet}!iuvax!ndmath!milo
 

han@Apple.COM (Byron Han, wyl E. coyote ) (04/10/89)

In article <1354@ndmath.UUCP> milo@ndmath.UUCP (Greg Corson) writes:
>
>Would anyone happen to have a code fragment that illustrates how to setup
>the required callback routines so you can have SoundPlay play a sound in the
>background (async) mode?
>
>1. open the channel and specify a callback
>2. play a snd resource
>3. have the callback set a global flag
>4. have my main event loop close the channel when the flag is set.

This is the code I am using.  There is NO guarantee or warranty with this
code.  Use at your own risk!  This is NOT Apple DTS sanctioned code.

1. the main application needs to keep a global variable is a SndChannelPtr.
Initialize it to NIL.

2. Pass in the handle to the snd  resource that you want to play.

This should bootstrap to you greater understanding of the world of the 
Sound Manager.

PROCEDURE PlayIt(VAR chan: SndChannelPtr; theSound: Handle);
VAR
    err: OSErr;
    myWish: SndCommand;

BEGIN
    IF chan <> NIL THEN   {cancel sound in progress}
    BEGIN
        err := SndDisposeChannel(chan, TRUE);  {TRUE means now}
        IF err <> noErr THEN
        BEGIN
            DebugStr('Cannot dispose of old channel');
            Exit(PlayIt);
        END
        ELSE
            chan := NIL;
    END;
  
    err := SndNewChannel(chan, 0, 0, NIL);
    IF err <> noErr THEN
    BEGIN
        DebugStr('Cannot allocate new channel');
        Exit(PlayIt);
    END;
    
    IF theSound <> NIL THEN {we have a handle}
        IF theSound^ <> NIL THEN {and is not a nil handle }
        BEGIN
            err:=SndPlay(chan, theSound, TRUE);     
            IF err <> noErr THEN
            BEGIN
                DebugStr('Cannot play sound');
                Exit(PlayIt);
            END;

            WITH myWish DO   { set up a sound mgr command block }
            BEGIN
               cmd := freeCmd;  {to free self when done }
               param1 := 0;
               param2 := 0;
            END; {with}

            err := SndDoCommand(chan, myWish, FALSE);
 
            IF err <> noErr THEN
            BEGIN
                DebugStr('cannot add command');
                Exit(PlayIt);
            END;
        END;
END;
+-----------------------------------------------------------------------------+
| Disclaimer: Apple has no connection with my postings.                       |
+-----------------------------------------------------------------------------+ 
Byron Han, Communications Architect      Cereal, anyone?  :-)  A1!
Apple Computer, Inc.                     -------------------------------------
20525 Mariani Ave, MS27Y                 Internet: han@apple.COM
Cupertino, CA 95014                      UUCP:{sun,voder,nsc,decwrl}!apple!han
--------------------------------------   GENIE: BYRONHAN
ATTnet: 408-974-6450   Applelink: HAN1   CompuServe: 72167,1664
------------------------------------------------------------------------------

lsr@Apple.COM (Larry Rosenstein) (04/11/89)

The Info-Mac archives should have an MPW Pascal unit that I wrote, which 
implements asynchronous SndPlay.  The main things to keep in mind:

(1) You have to create a private sound channel.  You should dispose of 
this channel as soon as you are done with it, otherwise no other program 
can make a sound.

(2) You use the channel call back feature to find out when the sound is 
finished.

(3) You have to lock down the snd resource before playing it.  The Sound 
Manager locks the resource for the duration of the SndPlay call, but when 
playing asynchronous sounds, this is a trivial length of time.

The unit I wrote does all of this stuff.

Larry Rosenstein, Apple Computer, Inc.
Object Specialist

Internet: lsr@Apple.com   UUCP: {nsc, sun}!apple!lsr
AppleLink: Rosenstein1