Mmplgdsp.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:18k
- {========================================================================}
- {= (c) 1995-98 SwiftSoft Ronald Dittrich =}
- {========================================================================}
- {= All Rights Reserved =}
- {========================================================================}
- {= D 01099 Dresden = Fax.: +49 (0)351-8037944 =}
- {= 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: 11.04.98 - 04:29:50 $ =}
- {========================================================================}
- unit MMPlgDSP;
- {$I COMPILER.INC}
- interface
- uses
- Windows,
- Messages,
- SysUtils,
- Forms,
- MMUtils,
- MMSystem,
- MMRegs;
- const
- DSP_HDRVER = $10;
- type
- TScaleLayer3 = array [0..1 , 0..31 , 0..17] of Single; (* Layer3 Amplification array *)
- TScaleLayer2 = array [0..1 , 0..31 , 0..35] of Single; (* Layer2 Amplification array *)
- TShortData = array [0 .. 20000] of Smallint; (* Sample array *)
- PShortData = ^TShortData; (* Pointer to Sample-Array *)
- PPlugInDSPModule = ^TPlugInDSPModule;
- TPlugInDSPModule = record (* PlugIn DSP-module type *)
- Description : PChar;
- hWNDParent : HWND; {parent window (filled in by Application)}
- hDLLInstance : HINST; {instance handle to this DLL (filled in by Application)}
- sRate : integer; {sample rate (filled in by Application)}
- nCh : integer; {number of channels (filled in by Application)}
- BlockSize : Integer; {BlockSize (filled in by Application)}
- Config : procedure(This_Mod: PPlugInDSPModule); cdecl;
- Init : function (This_Mod: PPlugInDSPModule): Integer; cdecl;
- Restart : function (This_Mod: PPlugInDSPModule): Integer; cdecl;
- ModifySamples : procedure(This_Mod: PPlugInDSPModule; Samples: PShortData); cdecl;
- ModifyLayer3XR : procedure(This_Mod: PPlugInDSPModule; var Xr: TScaleLayer3); cdecl;
- ModifyLayer2Subband : procedure(This_Mod: PPlugInDSPModule; var Subbands: TScaleLayer2); cdecl;
- Quit : procedure(This_Mod: PPlugInDSPModule); cdecl;
- UserData : Pointer;
- end;
- PPlugInDSPModule2 = ^TPlugInDSPModule2;
- TPlugInDSPModule2 = record (* PlugIn DSP-module type *)
- Description : PChar;
- hWNDParent : HWND; {parent window (filled in by Application)}
- hDLLInstance : HINST; {instance handle to this DLL (filled in by Application)}
- Config : procedure(This_Mod: PPlugInDSPModule2); cdecl;
- Init : function (This_Mod: PPlugInDSPModule2): Integer; cdecl;
- ModifySamples : function (This_Mod: PPlugInDSPModule2; Samples: PShortData; nSamples, bps, nch, srate: integer): integer; cdecl;
- Quit : procedure(This_Mod: PPlugInDSPModule2); cdecl;
- UserData : Pointer;
- end;
- PPlugInDSPHeader = ^TPlugInDSPheader;
- TPlugInDSPHeader = packed record (* PlugIn DSP Header *)
- Version : Integer; (* PlugIn DSP version ID , PlugIn checks this *)
- Description : PChar; (* Name of the DSP-plugin *)
- GetModule : function(Idx: Integer): Pointer; cdecl;
- end;
- PDSPModule = ^TDSPModule;
- TDSPModule = packed record
- Description : string;
- Active : Boolean;
- ModuleData : PPlugInDSPModule;
- ModuleData2 : PPlugInDSPModule2;
- end;
- PPlugInDSP = ^TPlugInDSP;
- TPlugInDSP = packed record
- DLLName : string;
- DLLInstance : THandle;
- Description : string;
- Modules : array[0..99] of TDSPModule;
- NumModules : integer;
- Open : Boolean;
- Header : PPlugInDSPHeader;
- end;
- function ReadPlugInDSP(FileName: string): PPlugInDSP;
- procedure FreePlugInDSP(var PlugIn: PPlugInDSP);
- function OpenPlugInDSP(PlugIn: PPlugInDSP; ParentWindow: HWND): Boolean;
- procedure ClosePlugInDSP(PlugIn: PPlugInDSP);
- procedure ConfigPlugInDSPModule(Module: TDSPModule);
- function InitPlugInDSPModule(var Module: TDSPModule; pwfx: PWaveFormatEx; BytesPerBlock: Longint): Boolean;
- function RestartPlugInDSPModule(var Module: TDSPModule; pwfx: PWaveFormatEx; BytesPerBlock: Longint): Boolean;
- function ModifySamplesPlugInDSP(var Module: TDSPModule; Buffer: PSmallint; nBytes: Longint; pwfx: PWaveFormatEx): Longint;
- procedure QuitPlugInDSPModule(var Module: TDSPModule);
- implementation
- type
- TGetHeader = function : PPlugInDSPHeader; cdecl;
- TGetModule = function (Which: Integer) : Pointer; cdecl;
- TConfig = procedure(This_Mod: Pointer); cdecl;
- TInit = function (This_Mod: Pointer): Integer; cdecl;
- TRestart = function (This_Mod: Pointer): Integer; cdecl;
- TQuit = procedure(This_Mod: Pointer); cdecl;
- TModifySamples = procedure(This_Mod: Pointer; Samples: PShortData); cdecl;
- TModifySamples2 = function (This_Mod: Pointer; Samples: PShortData; nSamples, bps, nch, srate: integer): integer; cdecl;
- TModifyLayer3XR = procedure(This_Mod: Pointer; var Xr : TscaleLayer3); cdecl;
- TModifyLayer2Subband = procedure(This_Mod: Pointer; var Subbands: TScaleLayer2); cdecl;
- {==============================================================================}
- function ReadPlugInDSP(FileName: string): PPlugInDSP;
- var
- hPlugIn : THandle;
- EntryPoint: TFarProc;
- GetHeader : TGetHeader;
- GetModule : TGetModule;
- Hdr : PPlugInDSPHeader;
- Module : Pointer;
- Ver2 : Boolean;
- begin
- Result := nil;
- hPlugIn := LoadLibrary(PChar(FileName));
- if (hPlugIn <> 0) then
- begin
- try
- Ver2 := False;
- EntryPoint := GetProcAddress(hPlugIn, 'winampDSPGetHeader');
- if (EntryPoint = nil) then
- EntryPoint := GetProcAddress(hPlugIn, 'MMPlugInDSPGetHeader');
- if (EntryPoint = nil) then
- begin
- EntryPoint := GetProcAddress(hPlugIn, 'winampDSPGetHeader2');
- if (EntryPoint <> nil) then Ver2 := True;
- end;
- if (EntryPoint <> nil) then
- begin
- @GetHeader := EntryPoint;
- Hdr := GetHeader; { Get PlugIn-header }
- if (Hdr <> nil) and (Hdr.Version >= DSP_HDRVER) then
- begin
- New(Result);
- with Result^ do
- begin
- DLLName := UpperCase(FileName);
- DLLInstance := 0;
- Description := StrPas(Hdr.Description);
- Header := nil;
- Open := False;
- GetModule := Hdr^.GetModule;
- NumModules := 0;
- repeat
- Module := GetModule(NumModules);
- if (Module <> nil) then
- begin
- if Ver2 then
- Modules[NumModules].Description := StrPas(PPlugInDSPModule2(Module).Description)
- else
- Modules[NumModules].Description := StrPas(PPlugInDSPModule(Module).Description);
- Modules[NumModules].ModuleData := nil;
- Modules[NumModules].ModuleData2 := nil;
- Modules[NumModules].Active := False;
- inc(NumModules);
- end;
- until (Module = nil) or (NumModules = 100);
- end;
- end;
- end;
- finally
- FreeLibrary(hPlugIn);
- end;
- end;
- end;
- {==============================================================================}
- function OpenPlugInDSP(PlugIn: PPlugInDSP; ParentWindow: HWND): Boolean;
- var
- EntryPoint: TFarProc;
- GetHeader : TGetHeader;
- GetModule : TGetModule;
- Hdr : PPlugInDSPHeader;
- Module : Pointer;
- Ver2 : Boolean;
- begin
- if (PlugIn <> nil) and not PlugIn.Open then
- begin
- PlugIn.DLLInstance := LoadLibrary(PChar(PlugIn.DLLname));
- if (PlugIn.DLLInstance <> 0) then
- begin
- try
- Ver2 := False;
- EntryPoint := GetProcAddress(PlugIn.DLLInstance, 'winampDSPGetHeader');
- if (EntryPoint = nil) then
- EntryPoint := GetProcAddress(PlugIn.DLLInstance, 'MMPlugInDSPGetHeader');
- if (EntryPoint = nil) then
- begin
- EntryPoint := GetProcAddress(PlugIn.DLLInstance, 'winampDSPGetHeader2');
- if (EntryPoint <> nil) then Ver2 := True;
- end;
- if (EntryPoint <> nil) then
- begin
- @GetHeader := EntryPoint;
- Hdr := GetHeader; { Get PlugIn-header }
- if (Hdr <> nil) and (Hdr.Version >= DSP_HDRVER) then
- begin
- with PlugIn^ do
- begin
- Header := Hdr;
- GetModule := Header^.GetModule;
- NumModules := 0;
- repeat
- Module := GetModule(NumModules);
- if (Module <> nil) then
- begin
- if Ver2 then
- begin
- PPlugInDSPModule2(Module).HWNDParent := ParentWindow;
- PPlugInDSPModule2(Module).hDLLInstance := PlugIn.DLLInstance;
- Modules[NumModules].ModuleData2 := Module;
- end
- else
- begin
- PPlugInDSPModule(Module).HWNDParent := ParentWindow;
- PPlugInDSPModule(Module).hDLLInstance := PlugIn.DLLInstance;
- Modules[NumModules].ModuleData := Module;
- end;
- Modules[NumModules].Active := False;
- inc(NumModules);
- end;
- until (Module = nil) or (NumModules = 100);
- end;
- PlugIn.Open := True;
- end;
- end;
- finally
- if not PlugIn.Open then
- begin
- FreeLibrary(PlugIn.DLLInstance);
- PlugIn.DLLInstance := 0;
- end;
- end;
- end;
- end;
- Result := (PlugIn <> nil) and PlugIn.Open;
- end;
- {==============================================================================}
- procedure ClosePlugInDSP(PlugIn: PPlugInDSP);
- var
- i: integer;
- begin
- try
- if (PlugIn <> nil) and PlugIn.Open then
- with PlugIn^ do
- begin
- for i := 0 to NumModules-1 do
- begin
- QuitPlugInDSPModule(Modules[i]);
- Modules[i].ModuleData := nil;
- Modules[i].ModuleData2:= nil;
- end;
- Header := nil;
- PlugIn.Open := False;
- if (DLLInstance <> 0) then
- begin
- FreeLibrary(DLLInstance);
- DLLInstance := 0;
- end;
- end;
- except
- // no exception please !!!
- end;
- end;
- {==============================================================================}
- procedure FreePlugInDSP(var PlugIn: PPlugInDSP);
- begin
- try
- if (PlugIn <> nil) then
- begin
- ClosePlugInDSP(PlugIn);
- Dispose(PlugIn);
- PlugIn := nil;
- end;
- except
- // no exception please !!!
- end;
- end;
- {==============================================================================}
- procedure ConfigPlugInDSPModule(Module: TDSPModule);
- begin
- try
- if (Module.ModuleData <> nil) then
- begin
- if assigned(Module.ModuleData.Config) then
- Module.ModuleData.Config(Module.ModuleData);
- end
- else if (Module.ModuleData2 <> nil) then
- begin
- if assigned(Module.ModuleData2.Config) then
- Module.ModuleData2.Config(Module.ModuleData2);
- end;
- except
- // no exception please !!!
- end;
- end;
- {==============================================================================}
- function InitPlugInDSPModule(var Module: TDSPModule; pwfx: PWaveFormatEx; BytesPerBlock: Longint): Boolean;
- begin
- try
- if not Module.Active and (BytesPerBlock > 0) then
- begin
- if (Module.ModuleData <> nil) then
- begin
- with Module do
- begin
- if (pwfx = nil) then
- begin
- ModuleData.sRate := 22050;
- ModuleData.nCh := 1;
- end
- else
- begin
- ModuleData.sRate := pwfx.nSamplesPerSec;
- ModuleData.nCh := pwfx^.nChannels;
- end;
- ModuleData.BlockSize := BytesPerBlock div (2*ModuleData.nCh);
- if assigned(Module.ModuleData.Init) then
- Active := Module.ModuleData.Init(Module.ModuleData) = 0;
- end;
- end
- else if (Module.ModuleData2 <> nil) then
- begin
- with Module do
- begin
- if assigned(Module.ModuleData2.Init) then
- begin
- Active := Module.ModuleData2.Init(Module.ModuleData2) = 0;
- end;
- end;
- end;
- end;
- Result := Module.Active;
- except
- Result := False;
- // no exception please !!!
- end;
- end;
- {==============================================================================}
- function RestartPlugInDSPModule(var Module: TDSPModule; pwfx: PWaveFormatEx; BytesPerBlock: Longint): Boolean;
- begin
- try
- Result := False;
- if Module.Active and (BytesPerBlock > 0) then
- begin
- if (Module.ModuleData <> nil) then
- begin
- with Module do
- begin
- if (pwfx = nil) then
- begin
- ModuleData.sRate := 22050;
- ModuleData.nCh := 1;
- end
- else
- begin
- ModuleData.sRate := pwfx.nSamplesPerSec;
- ModuleData.nCh := pwfx^.nChannels;
- end;
- ModuleData.BlockSize := BytesPerBlock div (2*ModuleData.nCh);
- if assigned(Module.ModuleData.Restart) then
- Result := Module.ModuleData.Restart(Module.ModuleData) = 0
- else
- Result := True;
- end;
- end
- else if (Module.ModuleData2 <> nil) then
- begin
- // TODO: ???
- Result := True;
- end;
- end;
- except
- Result := False;
- // no exception please !!!
- end;
- end;
- {==============================================================================}
- function ModifySamplesPlugInDSP(var Module: TDSPModule; Buffer: PSmallint; nBytes: Longint; pwfx: PWaveFormatEx): Longint;
- var
- bytesPS: integer;
- begin
- Result := 0;
- try
- bytesPS := (pwfx.wBitsPerSample div 8)*pwfx.nChannels;
- if (Module.ModuleData <> nil) then
- begin
- if Module.Active and assigned(Module.ModuleData.ModifySamples) then
- begin
- Module.ModuleData.ModifySamples(Module.ModuleData, Pointer(Buffer));
- Result := nBytes;
- end;
- end
- else if (Module.ModuleData2 <> nil) then
- begin
- if Module.Active and assigned(Module.ModuleData2.ModifySamples) then
- begin
- with pwfx^ do
- Result := Module.ModuleData2.ModifySamples(Module.ModuleData2, Pointer(Buffer),nBytes div bytesPS,wBitsPerSample,nChannels,nSamplesPerSec)*bytesPS;
- end;
- end
- except
- // no exception please !!!
- end;
- end;
- {==============================================================================}
- procedure QuitPlugInDSPModule(var Module: TDSPModule);
- begin
- try
- if (Module.ModuleData <> nil) then
- begin
- if Module.Active and assigned(Module.ModuleData.Quit) then
- Module.ModuleData.Quit(Module.ModuleData);
- end
- else if (Module.ModuleData2 <> nil) then
- begin
- if Module.Active and assigned(Module.ModuleData2.Quit) then
- Module.ModuleData2.Quit(Module.ModuleData2);
- end;
- Module.Active := False;
- except
- // no exception please !!!
- end;
- end;
- end.