📄 audioio.pas
字号:
If (Not ContinueProcessing) Then
Begin
Result := FALSE;
Exit;
End;
{ If assigned, process the buffer, Notice that the Size returned may not
be the size sent, so reset the output size }
If Assigned(FOnFillBuffer) Then
Begin
NSize := N;
Result := FOnFillBuffer(WaveBuffer[idx], NSize);
WaveHdr[idx].dwBufferLength := NSize;
End
Else
Result := FALSE;
{ On a filled buffer, increment it }
If (Result) Then FilledBuffers := FilledBuffers + 1;
QueuedBuffers := FilledBuffers - ProcessedBuffers;
END;
{--------------------StopAtOnce-------------John Mertus---14-June--97--}
Procedure TAudioOut.StopAtOnce;
{ Write the buffer to the wave device and toggel buffer index. }
{ }
{**********************************************************************}
BEGIN
{ if the device isn't open, just return }
If (Not WaveDeviceOpen) Then Exit;
Active := False;
ContinueProcessing := FALSE;
{ stop playing }
waveOutReset(WaveHandle);
{ close the device and unprepare the headers }
CloseWaveDevice;
END;
{--------------------StopGracefully---------John Mertus---14-June--97--}
Procedure TAudioOut.StopGracefully;
{ Write the buffer to the wave device and toggel buffer index. }
{ }
{**********************************************************************}
BEGIN
{ if the device isn't open, just return }
If (Not WaveDeviceOpen) Then Exit;
ContinueProcessing := FALSE;
END;
{------------------BufferDone---------------John Mertus---14-June--97--}
Procedure TCallBackWinOut.BufferDone(Var Msg : TMessage);
{ This is called when a buffer si done playing }
{ }
{**********************************************************************}
BEGIN
With AudioComponent^ Do
Begin
ProcessedBuffers := ProcessedBuffers + 1;
QueuedBuffers := FilledBuffers - ProcessedBuffers;
Active := (QueuedBuffers > 0);
If (ReadBuffer(BufIndex, FBufferSize)) Then QueueBuffer;
If (Not Active) then
Begin
ContinueProcessing := FALSE;
CloseWaveDevice;
End;
End;
END;
{------------------WaveOpen-----------------John Mertus---14-June--97--}
Procedure TCallBackWinOut.WaveOpen(Var Msg : TMessage);
{ This is called at the termination of each buffer. }
{ }
{**********************************************************************}
BEGIN
If Assigned(AudioComponent.FonOpen) Then AudioComponent.FonOpen(Self);
END;
{------------------WaveClose----------------John Mertus---14-June--97--}
Procedure TCallBackWinOut.WaveClose(Var Msg : TMessage);
{ This is called at the termination of each buffer. }
{ }
{**********************************************************************}
BEGIN
If Assigned(AudioComponent.FonClose) Then AudioComponent.FonClose(Self);
END;
{-----------------ElapsedTime----------------John Mertus---14-June--97--}
Function TAudioIn.ElapsedTime : Real;
{ This function returns the time since start of playout. }
{ }
{**********************************************************************}
Var
pmmt : TMMTime;
BEGIN
If (Active) Then
Begin
pmmt.wType := TIME_SAMPLES;
If (waveInGetPosition(WaveHandle, @pmmt, Sizeof(TMMTime)) <> 0) Then
Result := 0
Else
Result := pmmt.sample/FrameRate;
End
Else
Result := 0;
END;
{-------------CloseWaveDevice----------------John Mertus---14-June--97--}
Procedure TAudioIn.CloseWaveDevice;
{ Closes the wave output device. }
{ }
{**********************************************************************}
Var
i : Integer;
BEGIN
{ unprepare the headers }
Active := FALSE;
For i := 0 to FNumBuffers-1 Do
waveInUnprepareHeader( WaveHandle, WaveHdr[i], sizeof(TWAVEHDR));
{ close the device }
waveInReset(WaveHandle);
waveInClose(WaveHandle);
WaveDeviceOpen := FALSE;
END;
{-------------SetupOutput--------------------John Mertus---14-June--97--}
Function TAudioIn.Setup(Var TS : TAudioIn) : Boolean;
{ This function just sets up the board for output. }
{ }
{**********************************************************************}
Var
iErr : Integer;
i : Integer;
BEGIN
{ if the device is still open, return error }
If (WaveDeviceOpen) Then
Begin
ErrorMessage := 'Wave Input device is already open';
Result := FALSE;
Exit;
End;
BufIndex := 0;
{ Now create the window component to handle the processing }
CallBackWin := TCallBackWinIn.CreateParented(TWinControl(Owner).Handle);
CallBackWin.Visible := FALSE;
CallBackWin.AudioComponent := @TS;
{ Open the device for playout }
{ Either go via interrupt or window }
iErr := WaveInOpen(@WaveHandle, FWaveDevice, @FWaveFmtEx, Integer(CallBackWin.Handle),
0, CALLBACK_WINDOW or WAVE_ALLOWSYNC );
If (iErr <> 0) Then
Begin
ErrorMessage := TWaveInGetErrorText(iErr);
Result := FALSE;
Exit;
End;
WaveDeviceOpen := TRUE;
{ Setup the buffers and headers }
If (Not InitWaveHeaders) Then
Begin
Result := FALSE;
Exit;
End;
{ Now Prepare the buffers for output }
For i := 0 to FNumBuffers-1 Do
Begin
iErr := WaveInPrepareHeader(WaveHandle, WaveHdr[i], sizeof(TWAVEHDR));
If (iErr <> 0) Then
Begin
ErrorMessage := TWaveInGetErrorText(iErr);
CloseWaveDevice;
Result := FALSE;
Exit;
End;
End;
{ Read in the buffers }
QueuedBuffers := 0;
ProcessedBuffers := 0;
FilledBuffers := 0;
ContinueProcessing := TRUE;
Active := TRUE;
Result := TRUE;
END;
{----------------QueueBuffer----------------John Mertus---14-June--97--}
Function TAudioIn.QueueBuffer : Boolean;
{ Write the buffer to the wave device and toggel buffer index. }
{ }
{**********************************************************************}
Var
iErr : Integer;
BEGIN
{ reset flags field (remove WHDR_DONE attribute) }
WaveHdr[bufindex].dwFlags := WHDR_PREPARED;
{ now queue the buffer for output }
iErr := waveInAddBuffer( WaveHandle, WaveHdr[bufindex], sizeof(TWAVEHDR));
If (iErr <> 0) Then
Begin
ErrorMessage := TWaveInGetErrorText(iErr);
StopGracefully;
Result := FALSE;
Exit;
End;
{ Advance index }
bufindex := (bufindex+1) mod FNumBuffers;
QueuedBuffers := QueuedBuffers + 1;
Result := TRUE;
END;
{-------------StartIt------------------------John Mertus---14-June--97--}
Function TAudioIn.StartIt : Boolean;
{ This function just starts the waveform playing }
{ }
{**********************************************************************}
Var
i, iErr : Integer;
BEGIN
{ start recording to first buffer }
iErr := WaveInStart(WaveHandle);
If (iErr <> 0) Then
begin
CloseWaveDevice;
ErrorMessage := 'Error starting wave record: ' + TWaveInGetErrorText(iErr);
Result := FALSE;
Exit;
end;
Active := TRUE;
{ Now we are ready to start the output }
For i := 0 to FNumBuffers - 1 Do
If (Not QueueBuffer) Then
Begin
CloseWaveDevice;
Result := FALSE;
Exit;
End;
Result := TRUE;
END;
{-----------------Start----------------------John Mertus---14-June--97--}
Function TAudioIn.Start(Var TS : TAudioIn) : Boolean;
{ This function first sets up the output and then starts it. }
{ }
{**********************************************************************}
BEGIN
Result := Setup(TS);
If (Not Result) Then Exit;
Result := StartIt;
If (Not Result) Then Exit;
END;
{-----------ProcessBuffer---------------------John Mertus---14-June--97--}
Function TAudioIn.ProcessBuffer(B : lpstr; N : Integer) : Boolean;
{ This is called whenver move buffer data is needed. }
{ }
{**********************************************************************}
BEGIN
{ Do not call the read buffer routine if we want to stop }
If (Not ContinueProcessing) Then
Begin
Result := FALSE;
Exit;
End;
{ N can change, but we dont' care }
If Assigned(FOnBufferFilled) Then
Begin
Result := FOnBufferFilled(B, N);
End
Else
Result := TRUE;
{ On a filled buffer, increment it }
If (Result) Then FilledBuffers := FilledBuffers + 1;
END;
{--------------------StopAtOnce-------------John Mertus---14-June--97--}
Procedure TAudioIn.StopAtOnce;
{ Write the buffer to the wave device and toggel buffer index. }
{ }
{**********************************************************************}
BEGIN
{ if the device isn't open, just return }
If (Not WaveDeviceOpen) Then Exit;
Active := False;
ContinueProcessing := FALSE;
{ stop playing }
waveInReset(WaveHandle);
{ close the device and unprepare the headers }
CloseWaveDevice;
END;
{--------------------StopGracefully---------John Mertus---14-June--97--}
Procedure TAudioIn.StopGracefully;
{ Write the buffer to the wave device and toggel buffer index. }
{ }
{**********************************************************************}
BEGIN
{ if the device isn't open, just return }
If (Not WaveDeviceOpen) Then Exit;
ContinueProcessing := FALSE;
END;
{------------------BufferFinished-----------John Mertus---14-June--97--}
Procedure TCallBackWinIn.BufferFinished(Var Msg : TMessage);
{ This is called when each buffer is filled. }
{ }
{**********************************************************************}
BEGIN
With AudioComponent^ Do
Begin
ProcessedBuffers := ProcessedBuffers + 1;
QueuedBuffers := QueuedBuffers - 1;
Active := (QueuedBuffers > 0);
If (ProcessBuffer(WaveBuffer[BufIndex], FBufferSize)) Then QueueBuffer;
If (Not Active) then
Begin
ContinueProcessing := FALSE;
CloseWaveDevice;
End;
End;
END;
{------------------WaveOpenIn---------------John Mertus---14-June--97--}
Procedure TCallBackWinIn.WaveOpenIn(Var Msg : TMessage);
{ This is called at the termination of each buffer. }
{ }
{**********************************************************************}
BEGIN
If Assigned(AudioComponent.FonOpen) Then AudioComponent.FonOpen(Self);
END;
{------------------WaveCloseIn----------------John Mertus---14-June--97--}
Procedure TCallBackWinIn.WaveCloseIn(Var Msg : TMessage);
{ This is called at the termination of each buffer. }
{ }
{**********************************************************************}
BEGIN
If Assigned(AudioComponent.FonClose) Then AudioComponent.FonClose(Self);
END;
End.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -