home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / nastroje / d5 / DDX_SDK.ZIP / DDXDemo.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-05  |  7KB  |  258 lines

  1. unit DDXDemo;
  2.  
  3. {
  4.   This unit implements a DirectX plugin (also known as DirectShow filter)
  5.   in Delphi.
  6.   This unit will be the main unit exported by our dll, and must use
  7.   the DelDXBase unit.
  8.   The associated C++ dll
  9. }
  10.  
  11. interface
  12.  
  13. uses
  14.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  15.   StdCtrls, ComCtrls,
  16.   DDXBase;
  17.  
  18. const
  19.   {*** change the name, short description and optional help file}
  20.   EffectName     = 'DDXDemo';
  21.   EffectDesc     = 'DDX Demo';
  22.   EffectHelpFile = 'ddxddemo.htm'; {doesn't really matter if there isn't one}
  23.  
  24.   {*** change the name of the properties class}
  25.   PropertiesName = 'DDXDemo_Settings';
  26.  
  27.   {*** IT IS CRUCIAL that each new project has NEW, FRESH GUIDS!!! }
  28.   {*** DO NOT USE THESE ONES!!!}
  29.   {*** Use the NewGUIDs utility to generate them }
  30.   EffectGUID     = '{EFD5F1C0-51AF-11D5-A607-444553540000}';
  31.   PropertiesGUID = '{EFD5F1C1-51AF-11D5-A607-444553540000}';
  32.  
  33.  
  34. type
  35.  
  36.   { this form is the editor form for the plugin }
  37.   TDDXDemoForm = class(TForm)
  38.     TrackBar1: TTrackBar;
  39.     Label1: TLabel;
  40.     TrackBar2: TTrackBar;
  41.     Label2: TLabel;
  42.     procedure TrackBar1Change(Sender: TObject);
  43.     procedure TrackBar2Change(Sender: TObject);
  44.   private
  45.     { Private declarations }
  46.     fPlugin : TDelphiDirectXPluginBase;
  47.   public
  48.     { Public declarations }
  49.     property Plugin : TDelphiDirectXPluginBase read fPlugin;
  50.   end;
  51.  
  52.   { This is the actual effect class, which should be
  53.     descended from TDelphiDirectXPluginBase }
  54.   TMyDelphiDirectXPlugin = class (TDelphiDirectXPluginBase)
  55.   public
  56.     constructor create; override;
  57.     { the reasons for overriding create:
  58.         (a) to enforce the number of parameters (zero)
  59.         (b) to allow you to specify the channel modes
  60.             and sample types you are prepared to support.
  61.         (c) and of course, you need to specify default
  62.             starting up values for your effect's parameters
  63.         (d) and allocate any buffers / working variables etc. }
  64.  
  65.     destructor destroy; override;
  66.     { you'd override this to destroy any buffers ou allocated in create }
  67.  
  68.     procedure CompleteConnectEditor; override;
  69.     { This gets called after WE have been connected to the editor form.
  70.       But the editor form is not yet connected to US.
  71.       So we override this to make our editor form aware of the effect. }
  72.  
  73.     procedure DisplaySetParameter (
  74.            aParamNum : integer;
  75.            aValue    : single); override;
  76.     { This gets called after the system has modified one of the parameters,
  77.       and we override it so that we can update the associated control
  78.       on our editor form. }
  79.  
  80.     function Transform (
  81.                aInBuffer  : pointer;
  82.                aInSamples : integer;
  83.                aOutBuffer : pointer;
  84.                aOutSamples: integer;
  85.                aProcessed : pInteger)
  86.                           : integer; override;
  87.     { THis is where the audio work gets done }
  88.  
  89.     {here is a really cool way to access parameters from the array by name}
  90.     property Volume : single index 0 read GetParam write SetParam;
  91.     property Pan    : single index 1 read GetParam write SetParam;
  92.   end;
  93.  
  94.  
  95. const
  96.   {*** You must define a class reference constant to your plugin class: }
  97.   DelphiPluginClass     : TDelphiPluginClassRef     = TMyDelphiDirectXPlugin;
  98.   DelphiEditorFormClass : TDelphiEditorFormClassRef = TDDXDemoForm;
  99.  
  100. var
  101.   DDXDemoForm: TDDXDemoForm;
  102.  
  103. implementation
  104.  
  105. {$R *.DFM}
  106.  
  107.  
  108. constructor TMyDelphiDirectXPlugin.create;
  109. begin
  110.   inherited create;
  111.   ChannelModes := [cmMonoToMono, cmMonoToStereo, cmStereoToStereo];
  112.  
  113.   SampleTypes  := [stFloat32, stInteger16];
  114.  
  115.   Volume := 0.7;
  116.   Pan    := 0.5;
  117. end;
  118.  
  119. destructor TMyDelphiDirectXPlugin.destroy;
  120. begin
  121.   inherited destroy;
  122. end;
  123.  
  124. procedure TMyDelphiDirectXPlugin.CompleteConnectEditor;
  125. begin
  126.   inherited CompleteConnectEditor;
  127.   TDDXDemoForm(fEditorForm).fPlugin := self;
  128.   TDDXDemoForm(fEditorForm).TrackBar1.position := round (10 * Volume);
  129.   TDDXDemoForm(fEditorForm).TrackBar2.position := round (10 * Pan);
  130. end;
  131.  
  132. procedure TMyDelphiDirectXPlugin.DisplaySetParameter (
  133.             aParamNum : integer;
  134.             aValue    : single);
  135. begin
  136.   case aParamNum of
  137.     0 : TDDXDemoForm(fEditorForm).TrackBar1.position := round (10 * Volume);
  138.     1 : TDDXDemoForm(fEditorForm).TrackBar2.position := round (10 * Pan);
  139.     {for as many parameters as you are have, up to a max of 15}
  140.   end;
  141. end;
  142.  
  143.  
  144. function TMyDelphiDirectXPlugin.Transform (
  145.            aInBuffer  : pointer;
  146.            aInSamples : integer;
  147.            aOutBuffer : pointer;
  148.            aOutSamples: integer;
  149.            aProcessed : pInteger)
  150.                       : integer;
  151. var
  152.   inbufstep  : ^single;
  153.   outbufstep : ^single;
  154.  
  155.   inbufstepI : ^smallint;
  156.   outbufstepI: ^smallint;
  157.  
  158.   sctr       : integer;
  159.   value      : single;
  160.  
  161.   doPan      : boolean;
  162. begin
  163.   { mono-mono, stereo-stereo: aProcessed is the same as aInSamples
  164.   mono-stereo: aProcessed is 2 * aInSamples }
  165.   aProcessed^ := aInSamples * OutputChannels div InputChannels;
  166.  
  167.   result := 0;
  168.   inbufstep  := aInBuffer;
  169.   outbufstep := aOutBuffer;
  170.   inbufstepI := aInBuffer;
  171.   outbufstepI:= aOutBuffer;
  172.  
  173.   doPan := OutputChannels = 2;
  174.  
  175.   sctr := aInSamples;
  176.  
  177.   while sctr > 0 do begin
  178.  
  179.     if SampleType = stFloat32 then begin
  180.       value := Volume * inbufstep^;
  181.       if DoPan
  182.         then outbufstep^ := (1-pan) * value
  183.         else outbufstep^ := value;
  184.       inc (inbufstep); inc (outbufstep);
  185.     end else begin
  186.       value := Volume * inbufstepI^;
  187.       if DoPan
  188.         then outbufstep^ := round ((1-pan) * value)
  189.         else outbufstep^ := round (value);
  190.       inc (inbufstepI); inc (outbufstepI);
  191.     end;
  192.     dec (sctr);
  193.  
  194.     if InputChannels = 2 then begin
  195.       if SampleType = stFloat32 then begin
  196.         value := Volume * inbufstep^;
  197.         inc (inbufstep);
  198.       end else begin
  199.         value := Volume * inbufstepI^;
  200.         inc (inbufstepI);
  201.       end;
  202.       {we do NOT dec the counter again - nSamples is number or mono OR stereo samples}
  203.     end;
  204.  
  205.     if OutputChannels = 2 then begin
  206.       if SampleType = stFloat32 then begin
  207.         outbufstep^ := pan * value;
  208.         inc (outbufstep);
  209.       end else begin
  210.         outbufstepI^ := round (pan * value);
  211.         inc (outbufstepI);
  212.       end;
  213.     end;
  214.  
  215.   end;
  216. end;
  217.  
  218. procedure TDDXDemoForm.TrackBar1Change(Sender: TObject);
  219. begin
  220.   TMyDelphiDirectXPlugin (fPlugin).Volume := TrackBar1.position / 10;
  221. end;
  222.  
  223.  
  224. procedure TDDXDemoForm.TrackBar2Change(Sender: TObject);
  225. begin
  226.   TMyDelphiDirectXPlugin (fPlugin).Pan := TrackBar2.position / 10;
  227. end;
  228.  
  229. function GetClsNam (aWindow : HWND) : string;
  230. var buff : array [0..127] of char;
  231. begin
  232.   GetClassName(aWindow, buff, 127);
  233.   result := StrPas (buff);
  234. end;
  235. function RectToStr(aRect : trect) : string;
  236. begin
  237.   result :=
  238.     inttostr (aRect.left)  + ',' +
  239.     inttostr (aRect.top)   + ',' +
  240.     inttostr (aRect.right) + ',' +
  241.     inttostr (aRect.bottom);
  242. end;
  243.  
  244. initialization
  245.   { you MUST call this to load some global variables
  246.     used by the procedural dll interface }
  247.   RegisterEffect (
  248.     EffectName,
  249.     EffectDesc,
  250.     EffectHelpFile,
  251.     PropertiesName,
  252.     EffectGUID,
  253.     PropertiesGUID,
  254.     DelphiPluginClass,
  255.     DelphiEditorFormClass);
  256. end.
  257.  
  258.