home *** CD-ROM | disk | FTP | other *** search
/ Deathday Collection / dday.bin / edit / dfe / sounds.pas < prev    next >
Pascal/Delphi Source File  |  1994-05-26  |  3KB  |  116 lines

  1. {****************************************************************************
  2. *                      The DOOM Hacker's Tool Kit                           *
  3. *****************************************************************************
  4. * Unit:       SOUNDS                                                           *
  5. * Purpose: Loading and Playing WAD file sounds                              *
  6. * Date:    4/28/94                                                          *
  7. * Author:  Joshua Jackson        Internet: joshjackson@delphi.com           *
  8. ****************************************************************************}
  9.  
  10. unit Sounds;
  11.  
  12. interface
  13.  
  14. uses Wad,WadDecl,Crt;
  15.  
  16. Type     PSoundDef=^TSoundDef;
  17.         TSoundDef=object
  18.             Constructor Init(WDir:PWadDirectory;SndName:ObjNameStr);
  19.             Procedure PlaySound;
  20.          Function IsComplete:boolean;
  21.             Procedure EndSound;
  22.             Destructor Done;
  23.          Private
  24.             SoundBuff:PSoundBuff;
  25.             IsPlaying:Boolean;
  26.             sbBuff:word;
  27.         end;
  28.  
  29. implementation
  30.  
  31. {$IFDEF DPMI}
  32.     uses DPMI,Sounder2,ObjCache;
  33. {$ELSE}
  34.     uses Sounder2,ObjCache;
  35. {$ENDIF}
  36.  
  37. type TBuff=Array[0..46080] of byte;
  38.  
  39. var     BuffPtr:^TBuff;
  40.  
  41. Constructor TSoundDef.Init(WDir:PWadDirectory;SndName:ObjNameStr);
  42.  
  43.     var     l:word;
  44.  
  45.     begin
  46.         l:=WDir^.FindObject(SndName);
  47.         if l=0 then begin
  48.             if TerminateOnWadError then begin
  49.                 TextMode(co80);
  50.                 writeln('ReadSound: Could not locate sound ID: ',SndName);
  51.                 halt(1);
  52.              end
  53.             else begin
  54.                 WadResult:=wrNoSound;
  55.                 exit;
  56.             end;
  57.         end;
  58.         seek(WDir^.WadFile,WDir^.DirEntry^[l].ObjStart);
  59.         New(SoundBuff);                        {Allocate New Sound Descriptor}
  60.       BlockRead(WDir^.WadFile,SoundBuff^,8);
  61.       GetMem(SoundBuff^.Sound, SoundBuff^.Samples);
  62.       BlockRead(WDir^.WadFile,SoundBuff^.Sound^,SoundBuff^.Samples);
  63.         IsPlaying:=False;
  64.     end;
  65.  
  66. Procedure TSoundDef.PlaySound;
  67.  
  68.     var    TempAddr:word;
  69.             MemSize:word;
  70.             BuffAddr:longint;
  71.  
  72.     begin
  73.         if IsPlaying then
  74.             EndSound;
  75.     {$IFDEF DPMI}
  76.         MemSize:=(SoundBuff^.Samples div 16) + 1;
  77.         AllocDosMem(MemSize,SbBuff,TempAddr);            {Allocate DMA Buffer}
  78.         BuffPtr:=ptr(SbBuff,0);
  79.         BuffAddr:=TempAddr;
  80.         BuffAddr:=longint(BuffAddr) shl 4;
  81.         move(SoundBuff^.Sound^[0],BuffPtr^[0],SoundBuff^.Samples);
  82.     {$ELSE}
  83.       BuffPtr:=@SoundBuff^.Sound^;
  84.         BuffAddr:=(longint(Seg(BuffPtr^)) shl 4) + Ofs(BuffPtr^);
  85.     {$ENDIF}
  86.         InitSB;
  87.         PlayBuff(SoundBuff,BuffAddr);
  88.         IsPlaying:=True;
  89.     end;
  90.  
  91. Function TSoundDef.IsComplete;
  92.  
  93.     begin
  94.        IsComplete:=DMA_Complete;
  95.    end;
  96.  
  97. Procedure TSoundDef.EndSound;
  98.  
  99.     begin
  100.         if not IsPlaying then
  101.             exit;
  102.         StopBuff;
  103.     {$IFDEF DPMI}
  104.         DeAllocDosMem(sbBuff);                        {Dispose DMA Buffer}
  105.     {$ENDIF}
  106.         IsPlaying:=False;
  107.     end;
  108.  
  109. Destructor TSoundDef.Done;
  110.  
  111.     begin
  112.         FreeMem(SoundBuff^.Sound,SoundBuff^.Samples); {Dispose Sound Data}
  113.         Dispose(SoundBuff);                {Dispose Sound Descriptor}
  114.     end;
  115.  
  116. end.