MMPhase.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:9k
- {========================================================================}
- {= (c) 1995-98 SwiftSoft Ronald Dittrich =}
- {========================================================================}
- {= All Rights Reserved =}
- {========================================================================}
- {= D 01099 Dresden = Tel.: +0351-8012255 =}
- {= Loewenstr.7a = info@swiftsoft.de =}
- {========================================================================}
- {= Actual versions on http://www.swiftsoft.de/mmtools.html =}
- {========================================================================}
- {= This code is for reference purposes only and may not be copied or =}
- {= distributed in any format electronic or otherwise except one copy =}
- {= for backup purposes. =}
- {= =}
- {= No Delphi Component Kit or Component individually or in a collection=}
- {= subclassed or otherwise from the code in this unit, or associated =}
- {= .pas, .dfm, .dcu, .asm or .obj files may be sold or distributed =}
- {= without express permission from SwiftSoft. =}
- {= =}
- {= For more licence informations please refer to the associated =}
- {= HelpFile. =}
- {========================================================================}
- {= $Date: 20.01.1998 - 18:00:00 $ =}
- {========================================================================}
- unit MMPhase;
- {$I COMPILER.INC}
- interface
- uses
- {$IFDEF WIN32}
- Windows,
- {$ELSE}
- WinTypes,
- WinProcs,
- {$ENDIF}
- SysUtils,
- Classes,
- Controls,
- MMSystem,
- MMRegs,
- MMObj,
- MMDSPObj,
- MMUtils,
- MMWaveIO,
- MMPCMSup,
- MMFX;
- type
- EMMPhaserError = class(Exception);
- {-- TMMPhaser ---------------------------------------------------------}
- TMMPhaser = class(TMMDSPComponent)
- private
- FEnabled : Boolean;
- FOpen : Boolean;
- FPPhaser : PPhaser;
- FMaxDelay : integer;
- FDelay : integer;
- FChannel : TMMChannel;
- FCleanup : Longint;
- procedure SetEnabled(aValue: Boolean);
- procedure SetDelays(index: integer; aValue: integer);
- procedure SetChannel(aValue: TMMChannel);
- protected
- procedure SetPWaveFormat(aValue: PWaveFormatEx); override;
- procedure Update; virtual;
- procedure Opened; override;
- procedure Closed; override;
- procedure Started; override;
- procedure BufferReady(lpwh: PWaveHdr); override;
- procedure BufferLoad(lpwh: PWaveHdr; var MoreBuffers: Boolean); override;
- public
- constructor Create(aOwner: TComponent); override;
- destructor Destroy; override;
- procedure Open;
- procedure Close;
- procedure Process(Buffer: PChar; Length: integer);
- function CleanUp(Buffer: PChar; Length: integer): Longint;
- published
- property Input;
- property Output;
- property Enabled: Boolean read FEnabled write SetEnabled default True;
- property MaxDelay: integer index 0 read FMaxDelay write SetDelays default 250;
- property Delay: integer index 1 read FDelay write SetDelays default 1;
- property Channel: TMMChannel read FChannel write SetChannel default chBoth;
- end;
- implementation
- {== TMMPhaser ============================================================}
- constructor TMMPhaser.Create(aOwner: TComponent);
- begin
- inherited Create(aOwner);
- FEnabled := True;
- FOpen := False;
- FPPhaser := nil;
- FMaxDelay := 250;
- FDelay := 1;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- destructor TMMPhaser.Destroy;
- begin
- Close;
- inherited Destroy;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.SetEnabled(aValue: Boolean);
- begin
- if (aValue <> FEnabled) then
- begin
- FEnabled := aValue;
- if FEnabled then Update;
- end;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.SetDelays(index: integer; aValue: integer);
- begin
- case index of
- 0: if (aValue = FMaxDelay) then exit else
- begin
- FMaxDelay := aValue;
- end;
- 1: if (aValue = FDelay) then exit else
- begin
- FDelay := MinMax(aValue,1,FMaxDelay);
- Update;
- end;
- end;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.SetChannel(aValue: TMMChannel);
- begin
- if (aValue <> FChannel) then
- begin
- FChannel := aValue;
- Update;
- end;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.SetPWaveFormat(aValue: PWaveFormatEx);
- begin
- if (aValue <> nil) then
- begin
- if not (csDesigning in ComponentState) then
- if not pcmIsValidFormat(aValue) then
- raise EMMPhaserError.Create(LoadResStr(IDS_INVALIDFORMAT));
- end;
- inherited SetPWaveFormat(aValue);
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.Open;
- begin
- if not FOpen then
- begin
- if pcmIsValidFormat(PWaveFormat) then
- begin
- FPPhaser := InitPhaser(PWaveFormat, FMaxDelay, True);
- if (FPPhaser = nil) then OutOfMemoryError
- else
- begin
- FOpen := True;
- Update;
- end;
- end;
- end;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.Close;
- begin
- if FOpen then
- begin
- FOpen := False;
- DonePhaser(FPPhaser);
- end;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.Process(Buffer: PChar; Length: integer);
- begin
- { process the buffer trough the delay line }
- if (FPPhaser <> nil) then DoPhaser(FPPhaser, Buffer, Length);
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- function TMMPhaser.CleanUp(Buffer: PChar; Length: integer): Longint;
- begin
- { process the remaining delayed bytes in the delay lines }
- if (FPPhaser <> nil) and (FCleanup > 0) then
- begin
- FCleanup := Max(FCleanup - Length,0);
- FillChar(Buffer^, Length, 0);
- DoPhaser(FPPhaser, Buffer, Length);
- end;
- { return the remaining bytes to process }
- Result := FCleanup;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.Update;
- begin
- { setup the phaser params }
- if FOpen then
- begin
- FCleanup := 0;
- if (FDelay > FCleanUp) then FCleanUp := FDelay;
- if (FCleanup > 0) then
- begin
- { convert cleanup time to bytes }
- FCleanup := wioTimeToSamples(PWaveFormat,FCleanup);
- end;
- { now update the delay lines }
- SetPhaser(FPPhaser, FDelay, ord(FChannel));
- end;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.Opened;
- begin
- Open;
- inherited Opened;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.Closed;
- begin
- Close;
- inherited Closed;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.Started;
- begin
- Update;
- inherited Started;
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.BufferReady(lpwh: PWaveHdr);
- begin
- if Enabled and FOpen then
- begin
- Process(lpwh^.lpData, lpwh^.dwBytesRecorded);
- end;
- inherited BufferReady(lpwh);
- end;
- {-- TMMPhaser ------------------------------------------------------------}
- procedure TMMPhaser.BufferLoad(lpwh: PWaveHdr; var MoreBuffers: Boolean);
- var
- aLength: Longint;
- begin
- inherited BufferLoad(lpwh, MoreBuffers);
- if Enabled and FOpen then
- begin
- if not MoreBuffers then
- begin
- aLength := lpwh^.dwBufferLength;
- if Cleanup(lpwh^.lpData, aLength) > 0 then MoreBuffers := True;
- lpwh^.dwBytesRecorded := aLength;
- end
- else Process(lpwh^.lpData, lpwh^.dwBytesRecorded);
- end;
- end;
- end.