home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vpdart01.zip / VIO / DART.INC next >
Text File  |  1998-07-18  |  12KB  |  356 lines

  1. const
  2. {DART}
  3.   MCI_BUFFER      = 62;
  4.   MCI_MIXSETUP    = 63;
  5.   MCI_MAX_COMMAND = 64;
  6.  
  7. {Constants for DART Callbacks}
  8.   MIX_STREAM_ERROR    = $00000080;
  9.   MIX_READ_COMPLETE   = $00000001;
  10.   MIX_WRITE_COMPLETE  = $00000002;
  11.  
  12. {DART constants}
  13.   MCI_MIXSETUP_INIT      = $00010000;
  14.   MCI_MIXSETUP_DEINIT    = $00020000;
  15.   MCI_MIXSETUP_QUERYMODE = $00040000;
  16.  
  17.   MCI_ALLOCATE_MEMORY    = $00040000;
  18.   MCI_DEALLOCATE_MEMORY  = $00080000;
  19.  
  20.   MIX_BUFFER_EOS         = $00000001;
  21.  
  22. {DART structures}
  23.  
  24. type
  25.   MCI_MIX_BUFFER = record
  26.     ulStructLength : ulong;     { Structure length              }
  27.     pBuffer        : pointer;   { Pointer to a buffer           }
  28.     ulBufferLength : ulong;     { Length of the buffer          }
  29.     ulFlags        : ulong;     { Flags                         }
  30.     ulUserParm     : ulong;     { User parameter                }
  31.     ulTime         : ulong;     { Device time in ms             }
  32.     ulReserved1    : ulong;
  33.     ulReserved2    : ulong;
  34.   end;
  35.  
  36.  PMCI_MIX_BUFFER = ^MCI_MIX_BUFFER;
  37.  
  38. {DART Callbacks}
  39.  
  40. type  
  41.  PMIXERPROC   = function(ulHandle : ulong; pBuffer : PMCI_MIX_BUFFER; ulFlags : ulong) : long; 
  42.  PMIXEREVENT  = function(ulStatus : ulong; pBuffer : PMCI_MIX_BUFFER; ulFlags : ulong) : long;
  43.  
  44.  MCI_MIXSETUP_PARMS = record
  45.    hwndCallback    : hwnd;       { Window handle                 }
  46.    ulBitsPerSample : ulong;      { Bits per sample               }
  47.    ulFormatTag     : ulong;      { Format tag                    }
  48.    ulSamplesPerSec : ulong;      { Sampling rate                 }
  49.    ulChannels      : ulong;      { Number of channels            }
  50.    ulFormatMode    : ulong;      { Play or record                }
  51.    ulDeviceType    : ulong;      { Device type                   }
  52.    ulMixHandle     : ulong;      { Mixer handle                  }
  53.    pmixWrite       : pmixerproc; { Entry point                   }
  54.    pmixRead        : pmixerproc; { Entry point                   }
  55.    pmixEvent       : pmixerevent;{ Entry point                   }
  56.    pExtendedInfo   : pvoid;      { Extended information          }
  57.    ulBufferSize    : ulong;      { Recommended buffer size       }
  58.    ulNumBuffers    : ulong;      { Recommended number of buffers } 
  59.  end;
  60.  
  61.  
  62.  PMCI_MIXSETUP_PARMS = ^MCI_MIXSETUP_PARMS;
  63.  
  64.  MCI_BUFFER_PARMS = record
  65.     hwndCallback   : hwnd;
  66.     ulStructLength : ulong;
  67.     ulNumBuffers   : ulong;
  68.     ulBufferSize   : ulong;
  69.     ulMinToStart   : ulong;
  70.     ulSrcStart     : ulong;
  71.     ulTgtStart     : ulong;
  72.     pBufList       : pvoid;
  73.   end;
  74.  
  75.  
  76.  
  77. var
  78.   usDeviceID        : USHORT;        (* Amp Mixer device id       *)
  79.   ulBufferCount     : ULONG;        (* Current file buffer       *)
  80.   ulNumBuffers        : ULONG;        (* Number of file buffers  *)
  81.   MixBuffers        : Array[0..MAX_BUFFERS-1] of MCI_MIX_BUFFER;    
  82.                         (* Device buffers       *)
  83.   MixSetupParms     : MCI_MIXSETUP_PARMS;    (* Mixer parameters       *)
  84.   BufferParms        : MCI_BUFFER_PARMS;    (* Device buffer parms       *)
  85.   GenericParms        : MCI_GENERIC_PARMS;
  86.  
  87. (***************************************************************************
  88.  * Name        : MyEvent
  89.  *
  90.  * Description : The address to this procedure is passed to the mixer
  91.  *         device in the MIX_SETUP_PARMS structure. The mixer
  92.  *         device then calls this procedure when it has expended
  93.  *         a buffer.
  94.  *
  95.  *         NOTE: This is a high priority thread. Too much code here
  96.  *               will bog the system down.
  97.  *
  98.  * Parameters  : ulStatus - Detailed error message
  99.  *         pBuffer  - Pointer to expended buffer
  100.  *         ulFlags  - Indicates the type of event
  101.  *
  102.  *
  103.  * Return      : BOOL    - TRUE    = failure
  104.  *            - FALSE = success
  105.  *
  106.  ***************************************************************************)
  107.  
  108. Function MyEvent ( ulStatus : ULONG;
  109.            pBuffer  : PMCI_MIX_BUFFER;
  110.            ulFlags  : ULONG ) : LONG; CDecl;
  111.  
  112. begin
  113.   case ulFlags of
  114.     MIX_STREAM_ERROR or MIX_READ_COMPLETE ,  (* error occur in device *)
  115.     MIX_STREAM_ERROR or MIX_WRITE_COMPLETE:  (* error occur in device *)
  116.       if ulStatus = ERROR_DEVICE_UNDERRUN then begin
  117.       (* handle ERROR_DEVICE_UNDERRUN or OVERRUN here
  118.        *)
  119.       end;
  120.  
  121.     MIX_READ_COMPLETE :              (* for recording *)
  122.       if  ulBufferCount >= ulNumBuffers then begin
  123.      mciSendCommand( usDeviceID,
  124.              MCI_STOP,
  125.              MCI_WAIT,
  126.              GenericParms,
  127.              0 );
  128.         {ResetPlayBack;}
  129.      end else begin
  130.     MixSetupParms.pmixRead( MixSetupParms.ulMixHandle,
  131.                 pBuffer, 1 );
  132.     inc(ulBufferCount);
  133.     end;
  134.  
  135.     MIX_WRITE_COMPLETE:       (* for playback  *)
  136.        if( ulBufferCount >= ulNumBuffers ) then begin
  137.      mciSendCommand( usDeviceID,
  138.              MCI_STOP,
  139.              MCI_WAIT,
  140.              GenericParms,
  141.              0 );
  142.          StopPlay:=true;
  143.        end else begin
  144.      MixSetupParms.pmixWrite( MixSetupParms.ulMixHandle,
  145.                    @MixBuffers[ulBufferCount], 1 );
  146.      inc(ulBufferCount);
  147.        end;
  148.   end; 
  149.   MyEvent:=LONG( TRUE );
  150. end; (* end MyEvent *)
  151.  
  152.  
  153. (***************************************************************************
  154.  * Name        : MciError
  155.  * Description : Display a message box with the string corresponding to
  156.  *         the passed mci error code.
  157.  * Parameters  : ulError = mci error code
  158.  * Return      : None
  159.  ***************************************************************************)
  160.  
  161. Procedure MciError( ulError : ULONG );
  162. var
  163.   szBuffer    : Array[ 0..STRING_LENGTH-1 ] OF Char;
  164.   rc        : ULONG;
  165. begin
  166.   rc := mciGetErrorString( ulError, szBuffer, STRING_LENGTH );
  167.   Writeln(szBuffer);
  168. end;
  169.  
  170.  
  171. function mciCommand(usDeviceID: uShort; usMessage: uShort; ulParam1: uLong;
  172.                 var Param2) : boolean;
  173. var
  174.   rc        : ULONG;
  175. begin
  176.   rc:=mciSendCommand(usDeviceID, usMessage, ulParam1, Param2, 0);
  177.   MciCommand := (rc=MCIERR_SUCCESS);
  178.   if rc <> MCIERR_SUCCESS then MciError( rc );
  179. end;
  180.  
  181.  
  182. function AmpMixOpen : boolean;
  183. var
  184.   AmpOpenParms : MCI_AMP_OPEN_PARMS;
  185. begin
  186.   (* open the mixer device *)
  187.   fillchar( AmpOpenParms, sizeof( MCI_AMP_OPEN_PARMS ), 0 );
  188.   AmpOpenParms.usDeviceID := (* USHORT *) 0;
  189.   AmpOpenParms.pszDeviceType := PChar(MCI_DEVTYPE_AUDIO_AMPMIX);
  190.  
  191.   AmpMixOpen:=mciCommand( 0, MCI_OPEN,
  192.                 MCI_WAIT or MCI_OPEN_TYPE_ID or MCI_OPEN_SHAREABLE,
  193.                 AmpOpenParms);
  194.   usDeviceID := AmpOpenParms.usDeviceID;
  195. end;
  196.  
  197.  
  198. (***************************************************************************
  199.  * Name        : Close
  200.  * Description : Deallocate memory and close the Amp-Mixer Device.
  201.  * Parameters  : None
  202.  * Return      : BOOL    - TRUE    = failure
  203.  *            - FALSE = success
  204.  ***************************************************************************)
  205.  
  206. function AmpMixClose : boolean;
  207. begin
  208.   AmpMixClose:=false;
  209.   if mciCommand( usDeviceID, MCI_BUFFER, MCI_WAIT or MCI_DEALLOCATE_MEMORY,
  210.                   BufferParms) then exit;
  211.   AmpMixClose:=mciCommand( usDeviceID, MCI_CLOSE, MCI_WAIT, GenericParms);
  212. end;
  213.  
  214.  
  215. (***************************************************************************
  216.  * Name        : ResetRecord
  217.  *
  218.  * Description : Using the information stored in the MixSetupParms data
  219.  *         structure, the Amp-Mixer device is opened for recording.
  220.  *         Enough device buffers are allocated to fill preallocated
  221.  *         application buffers. Recording is then started and will
  222.  *         continue until all of the device buffers are full ( or
  223.  *         until the user selects the STOP button.
  224.  *
  225.  *
  226.  * Parameters  : None
  227.  *
  228.  * Return      : BOOL    - TRUE    = failure
  229.  *            - FALSE = success
  230.  *
  231.  ***************************************************************************)
  232.  
  233. Function ResetRecord : Boolean;
  234. var
  235.   ConnectorParms    : MCI_CONNECTOR_PARMS;
  236.   AmpSetParms        : MCI_AMP_SET_PARMS;
  237.  
  238. begin
  239.   ResetRecord := false;
  240.   { Deinitialize the Amp-Mixer to reset for record mode }
  241.   if mciCommand( usDeviceID, MCI_MIXSETUP, MCI_WAIT or MCI_MIXSETUP_DEINIT,
  242.           MixSetupParms) then exit;
  243.  
  244.   (* Setup the mixer for record
  245.    * Note: MixSetupParms is a global data structure. Most of this
  246.    *       structure must be set before this point. The LoadFile
  247.    *       procedure is the first place that this is done.
  248.    *)
  249.   MixSetupParms.ulFormatMode := MCI_RECORD;
  250.  
  251.   if mciCommand( usDeviceID, MCI_MIXSETUP, MCI_WAIT or MCI_MIXSETUP_INIT,
  252.          MixSetupParms) then exit;
  253.  
  254.   { Zero out the device buffers. }
  255.   for ulBufferCount := 0 to ulNumBuffers-1 do
  256.     fillchar( MixBuffers[ulBufferCount].pBuffer^,
  257.           MixBuffers[ulBufferCount].ulBufferLength, 0 );
  258.  
  259.  { Set the connector to 'line in' }
  260.  fillchar( ConnectorParms, sizeof( MCI_CONNECTOR_PARMS ), 0 );
  261.  ConnectorParms.ulConnectorType := MCI_LINE_IN_CONNECTOR;
  262.   mciSendCommand( usDeviceID, MCI_CONNECTOR, 
  263.                 MCI_WAIT or MCI_ENABLE_CONNECTOR or MCI_CONNECTOR_TYPE,
  264.              ConnectorParms,0);
  265.  
  266.  (* Allow the user to hear what is being recorded
  267.   * by turning the monitor on *)
  268.  
  269.  fillchar( AmpSetParms, sizeof( MCI_AMP_SET_PARMS ), 0 );
  270.   AmpSetParms.ulItem := MCI_AMP_SET_MONITOR;
  271.   mciSendCommand( usDeviceID, MCI_SET, MCI_WAIT or MCI_SET_ON or MCI_SET_ITEM,
  272.            AmpSetParms,0);
  273.  
  274.   (* Reset the buffer counter   *)
  275.   ulBufferCount := 0;
  276.  
  277.   {Kick off the mixer.}
  278.   if ulNumBuffers < 2 then begin
  279.  
  280.   (* If there is only one buffer to read  then tell the mixer that
  281.    * this is the end of the stream. If this flag is not set then
  282.    * the mixer would not start with less than two buffers.
  283.    *)
  284.     MixBuffers[0].ulFlags := MIX_BUFFER_EOS;
  285.     MixSetupParms.pmixRead( MixSetupParms.ulMixHandle,
  286.                 @MixBuffers[ulBufferCount],
  287.                 1 );
  288.   end else
  289.     MixSetupParms.pmixRead( MixSetupParms.ulMixHandle, @MixBuffers,
  290.                 ulNumBuffers );
  291.   ResetRecord := true;
  292. end;
  293.  
  294.  
  295.  
  296.  
  297. (****************************************************************************
  298.  * Name        : ResetPlayBack
  299.  *
  300.  * Description : Reinit the Amp-Mix device for play-back, allocate  device
  301.  *         buffers, copy audio data to device buffers and kick off
  302.  *         the Amp-Mixer device.
  303.  *
  304.  * Parameters  : None
  305.  *
  306.  * Return      : None
  307.  *
  308.  ***************************************************************************)
  309. Function ResetPlayBack : Boolean;
  310. var
  311.   ConnectorParms    : MCI_CONNECTOR_PARMS;
  312.   AmpSetParms        : MCI_AMP_SET_PARMS;
  313.   rc            : ULONG;
  314.  
  315. begin
  316.   { Deinitialize the Amp-Mixer to reset for record mode }
  317.   ResetPlayBack:=false; 
  318.   if mciCommand( usDeviceID, MCI_MIXSETUP,
  319.                 MCI_WAIT or MCI_MIXSETUP_DEINIT, MixSetupParms) then exit;
  320.  
  321.   (* Setup the mixer for play-back
  322.    * Note: MixSetupParms is a global data structure. Most of this
  323.    *       structure must be set before this point. The LoadFile
  324.    *       procedure is the first place that this is done.
  325.    *)
  326.   MixSetupParms.ulFormatMode := MCI_PLAY;
  327.   if mciCommand( usDeviceID, MCI_MIXSETUP, MCI_WAIT or MCI_MIXSETUP_INIT, 
  328.                  MixSetupParms) then exit;
  329.   ResetPlayBack := true;
  330. end;
  331.  
  332.  
  333.  
  334. (****************************************************************************
  335.  * Name        : StartPlayBack
  336.  * Description : Copy all of the application buffers to device buffers
  337.  *         and kick off the Amp-Mixer device.
  338.  ***************************************************************************)
  339.  
  340. Procedure StartPlayBack;
  341. var
  342.   ulIndex    : ULONG;        (* Device buffer index         *)
  343.   ulCount    : ULONG;        (* Number of posts         *)
  344. begin
  345.   if ulNumBuffers > 1 then begin
  346.     ulBufferCount := 2;
  347.     { Write two buffers to kick off the amp mixer. }
  348.     MixSetupParms.pmixWrite( MixSetupParms.ulMixHandle, @MixBuffers, 2 );
  349.   end else begin
  350.     ulBufferCount := 1;
  351.     { Write one buffer. }
  352.     MixSetupParms.pmixWrite( MixSetupParms.ulMixHandle, @MixBuffers, 1 );
  353.   end;
  354. end;
  355.  
  356.