home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / PNL Libraries / MyPlayAsyncSound.p < prev    next >
Encoding:
Text File  |  1995-10-23  |  2.2 KB  |  111 lines  |  [TEXT/CWIE]

  1. unit MyPlayAsyncSound;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Memory;
  7.  
  8.     procedure StartupPlayAsyncSound;
  9.     procedure PlayAsyncSound (theSound: Handle);
  10.     procedure PlayAsyncSoundID (id: integer);
  11.  
  12. implementation
  13.  
  14.     uses
  15.         Resources, Sound, PreserveA5, MyStartup;
  16.  
  17.     var
  18.         playing: boolean;
  19.         finished: boolean;
  20.         sound: handle;
  21.         chan: SndChannelPtr;
  22.         gChanCallBackProc:SndCallBackUPP;
  23.  
  24. { Called at interupt level! }
  25.     procedure ChanCallBack (chan: SndChannelPtr; cmd: SndCommand);
  26.         var
  27.             old_a5:Ptr;
  28.     begin
  29.         chan := chan; { Unused }
  30.         cmd := cmd; { Unused }
  31.         old_a5 := SetPreservedA5;
  32.         finished := true;
  33.         RestoreA5(old_a5);
  34.     end;
  35.  
  36.     procedure FinishSound;
  37.         var
  38.             junk: OSErr;
  39.     begin
  40.         HUnlock(sound);
  41.         playing := false;
  42.         finished := false;
  43.         junk := SndDisposeChannel(chan, false);
  44.         chan := nil;
  45.     end;
  46.  
  47.     procedure PlayAsyncSound (theSound: Handle);
  48.         var
  49.             err, junk: OSErr;
  50.             myWish: SndCommand;
  51.     begin
  52.         if (theSound <> nil) & (theSound^ <> nil) & not playing then begin
  53.             chan^.qLength := stdQLength;
  54.             err := SndNewChannel(chan, 0, 0, gChanCallBackProc);
  55.             if err = noErr then begin
  56.                 playing := true;
  57.                 HLock(theSound);
  58.                 junk := SndPlay(chan, SndListHandle(theSound), true);
  59.  
  60.                 { set up a sound mgr command block }
  61.                 myWish.cmd := callBackCmd;
  62.                 myWish.param1 := 0;
  63.                 myWish.param2 := 0;
  64.                 junk := SndDoCommand(chan, myWish, false);
  65. { If any of these commands return with an error, we aren't going to get anywhere anyway }
  66.             end;
  67.         end;
  68.     end;
  69.  
  70.     procedure PlayAsyncSoundID (id: integer);
  71.         var
  72.             sound: handle;
  73.     begin
  74.         if not playing then begin
  75.             sound := GetResource('snd ', id);
  76.             PlayAsyncSound(sound);
  77.         end;
  78.     end;
  79.  
  80.     function InitPlayAsyncSound(var msg: integer): OSStatus;
  81.     begin
  82.         msg := msg; { Unused }
  83.         gChanCallBackProc:=NewSndCallBackProc(@ChanCallBack);
  84.         playing := false;
  85.         finished := false;
  86.         sound := nil;
  87.         chan := nil;
  88.         InitPlayAsyncSound := noErr;
  89.     end;
  90.  
  91.     procedure FinishPlayAsyncSound;
  92.     begin
  93.         if playing then begin
  94.             FinishSound;
  95.         end;
  96.     end;
  97.  
  98.     procedure IdleAsyncSound;
  99.     begin
  100.         if finished then begin
  101.             FinishSound;
  102.         end;
  103.     end;
  104.  
  105.     procedure StartupPlayAsyncSound;
  106.     begin
  107.         StartupPreserveA5;
  108.         SetStartup(InitPlayAsyncSound, IdleAsyncSound, 60, FinishPlayAsyncSound);
  109.     end;
  110.     
  111. end.