[comp.sys.mac.programmer] Playing sounds async: help

mithomas@bsu-cs.bsu.edu (Michael Thomas Niehaus) (06/29/89)

Help!  I am trying to get async sounds to work using the sound manager, but
I have a couple of questions:

1.  Why am I not supposed to leave an open sound channel for a period of
    time?
2.  Right now, I am allocating a new channel, then doing the SndPlay.  I
    can see that the processing right after that is getting done.  But it
    finishes before the sound.  So my SndDisposeChannel (or however it is
    spelled) will just sit there and wait until the sound is finished.  (Yes,
    I want it to do that -- I don't want to chop the sound off right in the
    middle of playing.)  I could check in the event loop to see if the
    sound is done, and if it is then dispose the channel.  But how can I
    find out if it is finished?

Mac programming is so much fun (seriously)...

-Michael

-- 
Michael Niehaus        UUCP: <backbones>!{iuvax,pur-ee}!bsu-cs!mithomas
Apple Student Rep      ARPA:  mithomas@bsu-cs.bsu.edu
Ball State University  AppleLink: ST0374 (from UUCP: st0374@applelink.apple.com)

lsr@Apple.COM (Larry Rosenstein) (06/30/89)

In article <7975@bsu-cs.bsu.edu> mithomas@bsu-cs.bsu.edu (Michael Thomas 
Niehaus) writes:

> 1.  Why am I not supposed to leave an open sound channel for a period of
>     time?

I think that when you create a sound channel, you get control of the sound 
hardware, which prevents anyone else from making a sound.  (According to 
the System 7.0 preliminary docs, this is supposed to be fixed.)

> 2.  Right now, I am allocating a new channel, then doing the SndPlay.  I
>     sound is done, and if it is then dispose the channel.  But how can I
>     find out if it is finished?

You need to put a call back command in the channel after you play the 
sound, and specify a call back routine when you create the channel.  Your 
call back routine will get called when the sound finishes.

Other things to watch out for. 

You can't reuse the sound channel, because once you play a snd resource 
with it, the channel gets linked with a synthesizer.

You must lock the snd handle if you are playing it asynchronously  SndPlay 
will lock it for the duration of the call only, which will be shorter than 
the duration of the sound (if you are using async sound).

Finally,  you can look at the sample unit that I wrote, which should be on 
Info-Mac.  (It also appeared recently on comp.sources.mac.)  It was 
written in Pascal and shows how to do all these things.

Larry Rosenstein, Apple Computer, Inc.
Object Specialist

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

phil@mva.cs.liv.ac.uk (06/30/89)

In article <7975@bsu-cs.bsu.edu>, mithomas@bsu-cs.bsu.edu (Michael Thomas Niehaus) writes:
> Help!  I am trying to get async sounds to work using the sound manager, but
> I have a couple of questions:
> 
> 1.  Why am I not supposed to leave an open sound channel for a period of
>     time?
> 2.  Right now, I am allocating a new channel, then doing the SndPlay.  I
>     can see that the processing right after that is getting done.  But it
>     finishes before the sound.  So my SndDisposeChannel (or however it is
>     spelled) will just sit there and wait until the sound is finished.  (Yes,
>     I want it to do that -- I don't want to chop the sound off right in the
>     middle of playing.)  I could check in the event loop to see if the
>     sound is done, and if it is then dispose the channel.  But how can I
>     find out if it is finished?
> 
> Mac programming is so much fun (seriously)...
> 
> -Michael
> 
> -- 
> Michael Niehaus        UUCP: <backbones>!{iuvax,pur-ee}!bsu-cs!mithomas
> Apple Student Rep      ARPA:  mithomas@bsu-cs.bsu.edu

Well, you can use the CallBackCmd to let you know when a sound has finished.
 What I have done is to have a global Boolean.  The CallBack calls a procedure
which sets up access to global variables (a callback is done at interrupt
time so the A5 world is not normally set up) sets the Boolean appropriately,
and then restores A5 to whatever it was.  Seems to work OK for me.  I have
lots of short sounds (actually individual words) which I make into sentences,
and I do some animation while the words are being spoken.  The soundplay
code I use was posted by someone to Usenet a while ago.  I added the callback
bit from information in the docs that were posted to Usenet to replace the
SoundManager chapter in Vol V.

By the way - I'm not recommending my method, but it does seem to work, for
now... Ho Hum...
Phil Jimmieson,           ***************************************************
Computer Science Dept.,   *                                                 *
Liverpool University,     * JANET : PHIL@UK.AC.LIV.CS.MVA                   *
PO Box 147                * ARPA  : PHIL%mva.cs.liv.ac.uk@cunyvm.cuny.edu   *
Liverpool  L69  3BX       *                                                 *
(UK) 051-794-3689         ***************************************************

mithomas@bsu-cs.bsu.edu (Michael Thomas Niehaus) (07/01/89)

In article <2587@internal.Apple.COM>, lsr@Apple.COM (Larry Rosenstein) writes:
> > 2.  Right now, I am allocating a new channel, then doing the SndPlay.  I
> >     sound is done, and if it is then dispose the channel.  But how can I
> >     find out if it is finished?
> 
> You need to put a call back command in the channel after you play the 
> sound, and specify a call back routine when you create the channel.  Your 
> call back routine will get called when the sound finishes.

That sounds easy enough -- until you try it.  Does anyone have a good
example of writing a call-back routine for the Sound Manager?  I have
attempted it, but it is a shameful piece of work that, well, doesn't work.
Ideal would be a little code segment that shows how to set declare/setup
the call back routine (function declaration / access to global variables)
and how to pass this to SndNewChannel.

> Other things to watch out for. 

> You must lock the snd handle if you are playing it asynchronously  SndPlay 
> will lock it for the duration of the call only, which will be shorter than 
> the duration of the sound (if you are using async sound).

I am fortunate enough not to have run into this problem yet.  My program
doesn't do much in the line of memory allocation.

> Finally,  you can look at the sample unit that I wrote, which should be on 
> Info-Mac.  (It also appeared recently on comp.sources.mac.)  It was 
> written in Pascal and shows how to do all these things.

I haven't received anything from comp.sources.mac for the last 3 months.
And I am stuck on a lowly UUCP site, so if someone would be kind enough to
mail it to me...

Thanks for all the help.

-Michael

-- 
Michael Niehaus        UUCP: <backbones>!{iuvax,pur-ee}!bsu-cs!mithomas
Apple Student Rep      ARPA:  mithomas@bsu-cs.bsu.edu
Ball State University  AppleLink: ST0374 (from UUCP: st0374@applelink.apple.com)