⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmplugin.pas

📁 一套及时通讯的原码
💻 PAS
📖 第 1 页 / 共 4 页
字号:
{========================================================================}
{=                (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:31:10 $                                        =}
{========================================================================}
unit MMPlugIn;

{$I COMPILER.INC}

{.$DEFINE _MMDEBUG}

interface

uses
{$IFDEF WIN32}
    Windows,
{$ELSE}
    WinTypes,
    WinProcs,
{$ENDIF}
    Messages,
    SysUtils,
    Classes,
    Controls,
    Forms,
    MMSystem,
    MMRegs,
    MMObj,
    MMDSPObj,
    MMUtils,
    MMWaveIO,
    MMPCMSup,
    MMPlgDSP,
    MMPlgVis,
    MMMulDiv,
    MMMath,
    SyncObjs,
    MMFFT
    {$IFDEF _MMDEBUG}
    ,MMDebug
    {$ENDIF}
    ;

const
   {$IFDEF CBUILDER3} {$EXTERNALSYM FFTLen} {$ENDIF}
   FFTLen  = 1024;
   {$IFDEF CBUILDER3} {$EXTERNALSYM TIMEOUT} {$ENDIF}
   TIMEOUT = 5000;

type
   EMMPlugInError = class(Exception);

   {-- TMMDSPPlugin -----------------------------------------------------------}
   TMMDSPPlugin = class(TMMDSPComponent)
   private
      FDataSection   : TMMCriticalSection;
      FEnabled       : Boolean;
      FOpen          : Boolean;
      FStarted       : Boolean;
      FModuleIdx     : integer;
      FDSPPlugIn     : PPlugInDSP;
      FDataSectionOK : Boolean;
      FWaveHdr       : TMMWaveHdr;
      FBytesRead     : Longint;
      FMoreBuffers   : Boolean;
      FDone          : Boolean;

      function  GetLoaded: Boolean;
      function  GetActive: Boolean;
      function  GetNumModules: integer;
      procedure ReadFromInput(lpwh: PWaveHdr; var MoreBuffers: Boolean);

   protected
      procedure ChangeDesigning(aValue: Boolean); override;
      procedure SetPWaveFormat(aValue: PWaveFormatEx); override;
      procedure Opened; override;
      procedure Started; override;
      procedure Stopped; override;
      procedure Reseting; override;
      procedure Closed; override;
      procedure BufferLoad(lpwh: PWaveHdr; var MoreBuffers: Boolean); override;
      procedure BufferReady(lpwh: PWaveHdr); override;

   public
      constructor Create(aOwner: TComponent); override;
      destructor Destroy; override;

      property  PlugIn: PPlugInDSP read FDSPPlugIn;

      function  LoadPlugIn(FileName: TFileName): Boolean;// get all information about the PlugIn
      procedure OpenPlugInEx(Owner: THandle);
      procedure OpenPlugIn;                              // Loads the PlugIn in Memory
      procedure FreePlugIn;                              // remov the PlugIn from Memory


      procedure SelectModule(idx: integer);              // Select a module in the PlugIn
      procedure ConfigModule;                            // configure the module

      procedure ActivateModule;                          // Prepare the Module (maybe show a window)
      procedure ResetModule;                             // Reset the Module
      procedure CloseModule;                             // Close the Module (maybe remove a window)

      property NumModules: integer read GetNumModules;

      property IsLoaded: Boolean read GetLoaded;
      property IsActive: Boolean read GetActive;
      
   published
      property Input;
      property Output;
      property Enabled: Boolean read FEnabled write FEnabled default True;
   end;

   {-- TMMPluginThread --------------------------------------------------------}
   TMMPlugInThread = class(TMMDSPThread)
   private
      procedure SetFPU;
      procedure RestoreFPU;
      procedure Execute; override;
   end;

   PSpectrumData = ^TSpectrumData;
   TSpectrumData = array[0..1,0..575] of integer; { 0=Left;1=Right }
   PWaveFormData = ^TWaveFormData;
   TWaveformData = array[0..1,0..575] of Smallint;{ 0=Left;1=Right }

   TMMVISPlugInDataEvent = procedure(Sender: TObject; lpWaveData: PWaveFormData; lpSpectrumData: PSpectrumData) of object;

   {-- TMMVISPlugin -----------------------------------------------------------}
   TMMVISPlugin = class(TMMDSPComponent)
   private
      FHandle          : THandle;
      FNotifyHandle    : THandle;
      FOpen            : Boolean;
      FStarted         : Boolean;
      FPaused          : Boolean;
      FModuleIndex     : integer;
      FModuleIdx       : integer;
      FVISPlugIn       : PPlugInVIS;
      FInterval        : Integer;
      FEnabled         : Boolean;
      FFileName        : string;

      FpFFT            : PFFTReal;    { the instance for the FFT               }
      FWinBuf          : PIntArray;
      FBuffer          : PChar;
      FBufferBytes     : Longint;
      FWaveBytesDone   : Longint;
      FFFTBytesDone    : Longint;
      FReading         : Boolean;

      FWaveFormData    : TWaveFormData;
      FSpectrumData    : TSpectrumData;

      FPriority        : TThreadPriority;
      FPlugInThread    : TMMPlugInThread;
      FDataSection     : TMMCriticalSection;
      FRenderSection   : TMMCriticalSection;
      FGeneralEvent    : THandle;
      FCloseEvent      : THandle;
      FNotifyEvent     : THandle;
      FRenderEvent     : THandle;
      FOnOpen          : TNotifyEvent;
      FOnClose         : TNotifyEvent;
      FOnData          : TMMVISPlugInDataEvent;
      FOnPlugInActivate: TNotifyEvent;
      FOnPlugInQuit    : TNotifyEvent;

      procedure SetOnData(Event: TMMVISPlugInDataEvent);
      procedure SetEnabled(aValue: Boolean);
      procedure SetPriority(aValue: TThreadPriority);
      function  GetActive: Boolean;
      function  GetLoaded: Boolean;
      function  GetNumModules: integer;
      procedure NotifyHandler(Var Msg: TMessage );

      procedure Init;

      procedure InitThread;
      procedure DoneThread;
      procedure SyncRender;
      procedure Render(TryOnly: Boolean);

   protected
      procedure ChangeDesigning(aValue: Boolean); override;
      procedure SetPWaveFormat(aValue: PWaveFormatEx); override;
      procedure Opened; override;
      procedure Closed; override;
      procedure Started; override;
      procedure Stopped; override;
      procedure Paused; override;
      procedure Restarted; override;
      procedure BufferReady(lpwh: PWaveHdr); override;

   public
      constructor Create(aOwner: TComponent); override;
      destructor  Destroy; override;

      property  PlugIn: PPlugInVIS read FVISPlugIn;

      function  LoadPlugIn(FileName: TFileName): Boolean;// get all information about the PlugIn
      procedure OpenPlugIn;                              // Loads the PlugIn in Memory
      procedure FreePlugIn;                              // remov the PlugIn from Memory

      procedure SelectModule(idx: integer);              // Select a module in the PlugIn
      procedure ConfigModule;                            // configure the module

      procedure ActivateModule;                          // Prepare the Module (maybe show a window)
      procedure CloseModule;                             // Close the Module (maybe remove a window)

      property NumModules: integer read GetNumModules;

      property IsActive: Boolean read GetActive;
      property IsLoaded: Boolean read GetLoaded;

      procedure Trigger;

   published
      property OnOpen : TNotifyEvent read FOnOpen write FOnOpen;
      property OnClose: TNotifyEvent read FOnClose write FOnClose;
      property OnData : TMMVISPlugInDataEvent read FOnData write SetOnData;
      property OnPlugInActivate: TNotifyEvent read FOnPlugInActivate write FOnPlugInActivate;
      property OnPlugInQuit: TNotifyEvent read FOnPlugInQuit write FOnPlugInQuit;

      property Input;
      property Interval: Integer  read FInterval write FInterval default -1;
      property Enabled: Boolean read FEnabled write SetEnabled default True;
      property Priority: TThreadPriority read FPriority write SetPriority default tpNormal;
 end;

procedure Register;

implementation

{$R MMPLUGIN.D32}

procedure Register;
begin
   RegisterComponents('MMDSP',[TMMDSPPlugIn,TMMVISPlugIn]);
end;

const
   CM_VISPLUGIN_ENABLE     = WM_USER+100;
   CM_VISPLUGIN_LOAD       = WM_USER+101;
   CM_VISPLUGIN_OPEN       = WM_USER+102;
   CM_VISPLUGIN_FREE       = WM_USER+103;
   CM_VISPLUGIN_ACTIVATE   = WM_USER+104;
   CM_VISPLUGIN_CLOSE      = WM_USER+105;

   CM_VISPLUGIN_ONACTIVATE = WM_USER+106;
   CM_VISPLUGIN_ONCLOSE    = WM_USER+107;

{== TMMDSPPlugin ============================================================}
constructor TMMDSPPlugin.Create(aOwner: TComponent);
begin
   inherited Create(aOwner);

   FEnabled      := True;
   FOpen         := False;
   FStarted      := False;
   FModuleIdx    := 0;
   FDSPPlugIn    := nil;
   FDataSectionOK:= False;

   if not (csDesigning in ComponentState) then
   begin
      FDataSection := TMMCriticalSection.Create;
      FDataSectionOK := True;
   end;
end;

{-- TMMDSPPlugin ------------------------------------------------------------}
destructor TMMDSPPlugin.Destroy;
begin
   if FDataSectionOK then
   begin
      FreePlugIn;
      FDataSection.Free;
   end;

   inherited Destroy;
end;

{-- TMMDSPPlugin ------------------------------------------------------------}
procedure TMMDSPPlugIn.ChangeDesigning(aValue: Boolean);
begin
   inherited ChangeDesigning(aValue);

   if not (csDesigning in ComponentState) then
   begin
      FDataSection := TMMCriticalSection.Create;
      FDataSectionOK := True;
   end;
end;

{-- TMMDSPPlugin ------------------------------------------------------------}
procedure TMMDSPPlugin.SetPWaveFormat(aValue: PWaveFormatEx);
begin
   if (aValue <> nil) then
   begin
      if not (csDesigning in ComponentState) then
         if not pcmIsValidFormat(aValue) or (aValue.wBitsPerSample <> 16) then
            raise EMMPluginError.Create(LoadResStr(IDS_INVALIDFORMAT));
   end;

   inherited SetPWaveFormat(aValue);
end;

{-- TMMDSPPlugin ------------------------------------------------------------}
procedure TMMDSPPlugin.Opened;
begin
   inherited Opened;

   if not FOpen then
   begin
      if pcmIsValidFormat(PWaveFormat) and (PWaveFormat^.wBitsPerSample = 16) then
      begin
         FWaveHdr.wh.dwBufferLength := BufferSize;
         FWaveHdr.wh.lpData := GlobalAllocMem(2*BufferSize);

         FOpen := True;

         OpenPlugIn;
      end;
   end;
end;

{-- TMMDSPPlugin ------------------------------------------------------------}
procedure TMMDSPPlugin.Closed;
begin
   if FOpen then
   begin
      FOpen := False;

      GlobalFreeMem(Pointer(FWaveHdr.wh.lpData));
   end;

   inherited Closed;
end;

{-- TMMDSPPlugin ------------------------------------------------------------}
procedure TMMDSPPlugin.Started;
begin
   if not FStarted then
   begin
      ResetModule;
      FStarted     := True;
   end;

   inherited Started;
end;

{-- TMMDSPPlugin ------------------------------------------------------------}
procedure TMMDSPPlugin.Stopped;
begin
   if FStarted then
   begin
      FStarted := False;
   end;

   inherited Stopped;
end;

{-- TMMDSPPlugin ------------------------------------------------------------}
procedure TMMDSPPlugin.Reseting;
begin
   ResetModule;

   inherited Reseting;
end;

{-- TMMDSPPlugin ------------------------------------------------------------}
function TMMDSPPlugin.GetLoaded: Boolean;
begin

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -