[comp.sys.mac.programmer] Sound Manager problems

grh@rhi.hi.is (Gisli Runar Hjaltason) (03/29/90)

I am trying to use the Sound Manager to play sounds asynchronously as I want
my program to be doing some things while the sounds are playing. For that
purpose I wrote a few simple routines to make this easier and they included
below. I face two problems and both seem to occur only under MultiFinder:
1) It seems that sometimes MyCallBack gets called while a background program is
   switched in, so that CurrentA5 points to another program's variables. Is that
   possible?!?! I know this can be solved by some hacking (e.g. storing the
   variable in the code), but it's not pretty.
2) The heap gets garbled after several sounds have been played (sometimes
   20-30). When I commented out the sound playing itself from the routines, then
   the heap didn't get garbled. Is there something suspicious in my code?

I would appreciate any advice on this matter....

Thanks in advance,

Gisli

####
Sound routines:

	VAR
		sDone:		BOOLEAN;

	PROCEDURE MyCallBack(chan: SndChannelPtr; cmd: SndCommand);

		BEGIN
			SetupA5;
			sDone := True;
			RestoreA5;
		END;

	FUNCTION MakeChannel: SndChannelPtr;

		VAR
			chan:		SndChannelPtr;

		BEGIN
			chan := NIL;
			IF SndNewChannel(chan, sampledSynth, 0, @MyCallBack) = noErr THEN
				MakeChannel := chan
			ELSE
				MakeChannel := NIL;
			sDone := True;
		END;

	PROCEDURE DisposeChannel(chan: SndChannelPtr);

		VAR
			errno:	OSErr;

		BEGIN
			IF chan <> NIL THEN
				errno := SndDisposeChannel(chan, True);
		END;

	PROCEDURE PlaySound(chan: SndChannelPtr; sound: Handle);

		VAR
			cmd:		SndCommand;

		BEGIN
			cmd.cmd := callBackCmd;
			IF (chan <> NIL) & (SndPlay(chan, sound, True) = noErr) &
						(SndDoCommand(chan, cmd, False) = noErr) THEN
				sDone := False;
		END;

	FUNCTION SoundDone: BOOLEAN;
		
		BEGIN
			SoundDone := sDone;
		END;