home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vpdart01.zip / VIO / play.pas < prev   
Pascal/Delphi Source File  |  1998-07-22  |  5KB  |  151 lines

  1. Program DAudio;
  2. {$S-}{&CDecl+}
  3. uses Use32,Os2Def,os2mm;
  4.  
  5. const
  6.   STRING_LENGTH  = 128;
  7.   MAX_BUFFERS    = 256;
  8.   StopPlay : boolean = false;
  9.  
  10. {$I DART.INC}
  11.  
  12. const
  13.   fPassedDevice         : BOOLEAN = TRUE;
  14.   fRecording            : BOOLEAN = FALSE;
  15.  
  16. var
  17.   rc            : ULONG;
  18.   WavFileName   : String;
  19.  
  20.  
  21. (***************************************************************************
  22.  * Name        : LoadFile
  23.  *
  24.  * Description : Allocate application buffers for audio file and load
  25.  *               audio file. These buffers will be copied to device
  26.  *               buffers by another thread.
  27.  *
  28.  *               Before this procedure loads the audio file, the global
  29.  *               MixSetupParms data structure is loaded with information
  30.  *               from the audio files header.
  31.  *
  32.  *
  33.  * Parameters  : CHAR szFilename[]     - Name of wave file to open
  34.  *
  35.  * Return      : BOOL   - TRUE  = failure
  36.  *                      - FALSE = success
  37.  *
  38.  ***************************************************************************)
  39.  
  40.  
  41. function AmpMixSetup(AudioHeader : OS2MM.MMAUDIOHEADER) : boolean;
  42. begin
  43.   (* Set the MixSetupParms data structure to match the loaded file.
  44.    * This is a global that is used to setup the mixer. *)
  45.  
  46.   fillchar( MixSetupParms, sizeof( MCI_MIXSETUP_PARMS ), 0 );
  47.    
  48.   MixSetupParms.ulBitsPerSample :=
  49.         AudioHeader.mmXWAVHeader.WAVEHeader.usBitsPerSample;
  50.   MixSetupParms.ulFormatTag :=
  51.         AudioHeader.mmXWAVHeader.WAVEHeader.usFormatTag;
  52.   MixSetupParms.ulSamplesPerSec :=
  53.         AudioHeader.mmXWAVHeader.WAVEHeader.ulSamplesPerSec;
  54.   MixSetupParms.ulChannels :=
  55.         AudioHeader.mmXWAVHeader.WAVEHeader.usChannels;
  56.  
  57.   (* Setup the mixer for playback of wave data *)
  58.   MixSetupParms.ulFormatMode := MCI_PLAY;
  59.   MixSetupParms.ulDeviceType := MCI_DEVTYPE_WAVEFORM_AUDIO;
  60.   MixSetupParms.pmixEvent    := MyEvent;
  61.  
  62.   AmpMixSetup := mciCommand( usDeviceID, MCI_MIXSETUP,
  63.                      MCI_WAIT or MCI_MIXSETUP_INIT,
  64.                      MixSetupParms);
  65. end;
  66.  
  67.  
  68. Function LoadFile( szFileName:PChar ) : Boolean;
  69. var
  70.   mmAudioHeader         : OS2MM.MMAUDIOHEADER;
  71.   hmmio                 : OS2MM.HMMIO;
  72.   lBytesRead            : LONG;
  73.   ulBufferOffset        : LONG;
  74.   rc, ulIndex           : ULONG;
  75.  
  76.  
  77. begin
  78.   LoadFile:=false;
  79.   (* Open the audio file. *)
  80.  
  81.   hmmio := mmioOpen( szFileName, nil, MMIO_READ or MMIO_DENYNONE );
  82.  
  83.   if hmmio=0 then begin
  84.     Writeln('Unable to open wave file');
  85.     LoadFile := false;
  86.     exit;
  87.   end;
  88.  
  89.   (* Get the audio file header. *)
  90.    mmioGetHeader( hmmio, @mmAudioHeader, sizeof( MMAUDIOHEADER ),
  91.                  @lBytesRead, 0, 0);
  92.   
  93.   if not AmpMixSetup(mmAudioHeader) then exit;
  94.  
  95.   (* Use the suggested buffer size provide by the mixer device
  96.    * and the size of the audio file to calculate the required
  97.    * number of Amp-Mixer buffers.
  98.    * Note: The result is rounded up 1 to make sure we get the
  99.    *       tail end of the file.
  100.    *)
  101.   ulNumBuffers :=
  102.         mmAudioHeader.mmXWAVHeader.XWAVHeaderInfo.ulAudioLengthInBytes
  103.         DIV MixSetupParms.ulBufferSize + 1;
  104.  
  105.  
  106.   (* Set up the BufferParms data structure and allocate
  107.    * device buffers from the Amp-Mixer *)
  108.  
  109.   BufferParms.ulNumBuffers := ulNumBuffers;
  110.   BufferParms.ulBufferSize := MixSetupParms.ulBufferSize;
  111.   BufferParms.pBufList := @MixBuffers;
  112.  
  113.   if not mciCommand( usDeviceID, MCI_BUFFER, MCI_WAIT or MCI_ALLOCATE_MEMORY,
  114.                      BufferParms) then exit;
  115.  
  116.   (* Fill all device buffers with data from the audio file.*)
  117.   for ulIndex := 0 to ulNumBuffers-1 do begin
  118.     fillchar( MixBuffers[ ulIndex ].pBuffer^, BufferParms.ulBufferSize, 0 );
  119.     MixBuffers[ ulIndex ].ulBufferLength := BufferParms.ulBufferSize;
  120.     rc := mmioRead ( hmmio, MixBuffers[ ulIndex ].pBuffer,
  121.                      MixBuffers[ ulIndex ].ulBufferLength );
  122.   end;
  123.  
  124.   (* Set the "end-of-stream" flag
  125.    *)
  126.   MixBuffers[ulNumBuffers - 1].ulFlags := MIX_BUFFER_EOS;
  127.   mmioClose( hmmio, 0 );
  128.   LoadFile := true;
  129. end;
  130.  
  131.   function StrPChar(var St : String) : Pchar;
  132.   begin
  133.     if Length(st)<255 then St[Length(St)+1]:=#0;
  134.     StrPChar:=@St[1];
  135.   end;
  136.  
  137. begin
  138.    if ParamCount=0 then writeln('Usage: play <filename.wav>');
  139.   (* Load the audio file and setup for playback. *)
  140.    if not AmpMixOpen then exit;
  141.    WavFileName:=ParamStr(1);
  142.    LoadFile(StrPChar(WavFileName));
  143.    StopPlay:=false;
  144.    StartPlayBack;
  145.    repeat until StopPlay;
  146.    {readln;}
  147.    AmpMixClose;
  148. end. (* end main *)
  149.  
  150.  
  151.