unit testcontrol;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,DirectXMediaPlayer,
  StdCtrls, ExtCtrls, ComCtrls,U_FTG2;

const BANDSSIZE=10;
const FTTSIZE=BANDSSIZE *2;
const BANDS=10;
const BANDSDESC : array[1..BANDS] of TParamEqDesc =  ((freq : 100;friendly : '100 Hz'),
                                                          (freq : 200;friendly : '200 Hz'),
                                                          (freq : 400;friendly : '400 Hz'),
                                                          (freq : 600;friendly : '600 Hz'),
                                                          (freq : 1000;friendly : '1000 Hz'),
                                                          (freq : 3000;friendly : '3000 Hz'),
                                                          (freq : 6000;friendly : '6000 Hz'),
                                                          (freq : 12000;friendly : '12000 Hz'),
                                                          (freq : 14000;friendly : '14000 Hz'),
                                                          (freq : 16000;friendly : '15000 Hz'));


const EFFECT=1;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Timer1: TTimer;
    Label1: TLabel;
    TrackBar1: TTrackBar;
    Label2: TLabel;
    ScrollBar1: TScrollBar;
    ScrollBar2: TScrollBar;
    ScrollBar3: TScrollBar;
    ScrollBar4: TScrollBar;
    ScrollBar5: TScrollBar;
    ScrollBar6: TScrollBar;
    ScrollBar7: TScrollBar;
    ScrollBar8: TScrollBar;
    ScrollBar9: TScrollBar;
    ScrollBar10: TScrollBar;
    Button3: TButton;
    ScrollBar11: TScrollBar;
    compon: TCheckBox;
    eqon: TCheckBox;
    Threshold: TScrollBar;
    Gain: TScrollBar;
    ratio: TScrollBar;
    attack: TScrollBar;
    release: TScrollBar;
    Image1: TImage;
    Timer2: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ScrollBar11Change(Sender: TObject);
    procedure eqonClick(Sender: TObject);
    procedure componClick(Sender: TObject);
    procedure ThresholdChange(Sender: TObject);
    procedure GotData(Sender : TObject);
    procedure Timer2Timer(Sender: TObject);
  private
    { Private declarations }
  procedure drawpeak;
  procedure drawspec;
  public
  nochange : boolean;
  p,x : dword;
  dp : integer;
  canceldraw : boolean;
  fci,fco : array of TComplex;
    { Public declarations }
fr : array [0..FTTSIZE] of single;
  end;

var
  Form1: TForm1;
  mp : TDXMP;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
mp.filename:='c:\tst.mp3';
mp.Open;
trackbar1.Max:=mp.length;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
timer1.Enabled:=true;
mp.play;
end;


procedure TForm1.Button3Click(Sender: TObject);
begin
timer1.Enabled:=false;
mp.Stop;
end;

procedure TForm1.componClick(Sender: TObject);
begin
if compon.checked=true then begin
  mp.IncludeCompressorinSetup:=true;
  mp.setcompressor(60-gain.Position,0-threshold.Position,ratio.position,attack.Position,release.position);
  end
  else begin
  mp.IncludeCompressorinSetup:=false;
  end;

end;

procedure TForm1.eqonClick(Sender: TObject);
begin
if eqon.Checked=false then mp.IncludeEqualizerinSetup:=false else begin
mp.IncludeEqualizerinSetup:=true;
with scrollbar1 do begin mp.seteqparam(tag,0-position);end;
with scrollbar2 do begin mp.seteqparam(tag,0-position);end;
with scrollbar3 do begin mp.seteqparam(tag,0-position);end;
with scrollbar4 do begin mp.seteqparam(tag,0-position);end;
with scrollbar5 do begin mp.seteqparam(tag,0-position);end;
with scrollbar6 do begin mp.seteqparam(tag,0-position);end;
with scrollbar7 do begin mp.seteqparam(tag,0-position);end;
with scrollbar8 do begin mp.seteqparam(tag,0-position);end;
with scrollbar9 do begin mp.seteqparam(tag,0-position);end;
with scrollbar10 do begin mp.seteqparam(tag,0-position);end;
end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
mp:= TDXMP.create(handle);
image1.Height:=255;
image1.Picture.Bitmap.PixelFormat:=pf24Bit;
image1.Picture.Bitmap.Height:=255;
image1.Picture.Bitmap.Width:=image1.Width;
mp.OnHaveData:=gotdata;
timer2.Interval:=10;//(1000 div 24);


end;

procedure TForm1.ScrollBar11Change(Sender: TObject);
begin
mp.volume:=0-scrollbar11.position;
end;

procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
if eqon.checked=false then exit;
mp.seteqparam(TScrollbar(sender).tag,0-TScrollbar(sender).position);
end;

procedure TForm1.ThresholdChange(Sender: TObject);
begin
mp.setcompressor(60-gain.Position,0-threshold.Position,Ratio.Position,attack.position,release.Position);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
seconds,minutes : integer;
sec : string;
begin
nochange:=true;
//trackbar1.Position:=mp.position;
nochange:=false;
 seconds:=mp.length;
 minutes:= seconds div 60;
 seconds:=seconds mod 60;
 sec := inttostr(seconds);
 if seconds<10 then sec:='0'+inttostr(seconds);
 label2.caption:=inttostr(minutes)+':'+sec;

 seconds:=mp.position;
 minutes:= seconds div 60;
 seconds:=seconds mod 60;
 sec := inttostr(seconds);
 if seconds<10 then sec:='0'+inttostr(seconds);
 //label1.caption:=inttostr(minutes)+':'+sec;
 label1.Caption:=inttostr(minutes)+':'+sec;


end;

procedure TForm1.DrawPeak;
var
ci : word;
cb : shortint;
p : pointer;
x : word;
xx : integer;
sl : pByteArray;
w : integer;
begin
p:=mp.PCMData;
image1.Canvas.Pen.Color:=clGreen;
image1.picture.bitmap.Canvas.brush.Color:=clblack;
image1.picture.bitmap.Canvas.Rectangle(0,0,image1.width,image1.height);
xx:=0;
x:=0;
image1.picture.bitmap.canvas.moveto(0,image1.Picture.Bitmap.Height div 2);

while (x < 3*image1.Width) and (canceldraw=false) do begin
move(ptr(dword(p)+dp)^,ci,sizeof(ci));
cb:=round((image1.height/65535)*ci);
if (128+cb<image1.height) then begin
  sl:=image1.picture.bitmap.scanline[128+cb];
  if xx mod 2 =0 then sl[x+2]:=0 else sl[x+2]:=255;
  if xx mod 2 =0 then sl[x+1]:=255 else sl[x+1]:=0;
  sl[x]:=0;
 // image1.picture.bitmap.canvas.LineTo(x,128-cb);
  end;
//image1.Canvas.Pixels[x,c]:=clRed;
inc(dp);
inc(dp);
x:=x+3;
inc(xx);
if (dp)>mp.PCMSize then exit;

end;
canceldraw:=false;
image1.Repaint;
end;

procedure TForm1.drawspec;
var
ci : word;
cb : shortint;
x : integer;
p : pointer;
w : word;
//h : integer;
xx : integer;
sl : pbytearray;
col : dword;
h : real;
m : real;
wid : real;
vcanv : tbitmap;
begin

vcanv:=tbitmap.create;
vcanv.Width:=image1.Picture.Width;
vcanv.Height:=image1.Picture.Height;
p:=mp.PCMData;
vcanv.Canvas.Pen.Color:=clGreen;
vcanv.Canvas.brush.Color:=clblack;
vcanv.Canvas.Rectangle(0,0,image1.width,image1.height);

wid:=image1.Width / (FTTSIZE) / 2;
m:=image1.height/(65535/8);


x:=0;
while x<(FTTSIZE) div 2 do begin
if canceldraw=true then break;
if fco[x].r<>0 then  vcanv.Canvas.rectangle(round(x*wid),image1.Height-round(fr[x]*m),round((x*wid)+wid),image1.Height-round(fr[x]*m-1));
inc(x);
end;


for x := 0 to FTTSIZE do begin
move(ptr(dword(p)+dp)^,w,sizeof(w));
fci[x].r:=w;
fci[x].i:=0;
fco[x].r:=0;
fco[x].i:=0;
inc(dp);
inc(dp);
end;

CFTG(fci,fco,fci,FTTSIZE);


x:=0;
xx:=0;
  vcanv.Canvas.Brush.Style:=bssolid;
  vcanv.Canvas.Brush.color:=clRed;




while x<=(FTTSIZE) div 2 do begin

{
if (h<=(BANDSDESC[1].freq)) then fr[1]:=round(fr[1]+fco[x].r*m);
if (h>=BANDSDESC[1].freq) and (h<=BANDSDESC[2].freq) then fr[2]:=round(fr[2]+fco[x].r*m);
if (h>=BANDSDESC[2].freq) and (h<=BANDSDESC[3].freq) then fr[3]:=round(fr[3]+fco[x].r*m);
if (h>=BANDSDESC[3].freq) and (h<=BANDSDESC[4].freq) then fr[4]:=round(fr[4]+fco[x].r*m);
if (h>=BANDSDESC[4].freq) and (h<=BANDSDESC[5].freq) then fr[5]:=round(fr[5]+fco[x].r*m);
if (h>=BANDSDESC[5].freq) and (h<=BANDSDESC[6].freq) then fr[6]:=round(fr[6]+fco[x].r*m);
if (h>=BANDSDESC[6].freq) and (h<=BANDSDESC[7].freq) then fr[7]:=round(fr[7]+fco[x].r*m);
if (h>=BANDSDESC[7].freq) and (h<=BANDSDESC[8].freq) then fr[8]:=round(fr[8]+fco[x].r*m);
if (h>=BANDSDESC[8].freq) and (h<=BANDSDESC[9].freq) then fr[9]:=round(fr[9]+fco[x].r*m);
if (h>=BANDSDESC[9].freq) and (h<=BANDSDESC[10].freq) then fr[10]:=round(fr[10]+fco[x].r*m);
if (h>=BANDSDESC[10].freq) then fr[11]:=round(fr[11]+fco[x].r*m);   }

if fco[x].r<0 then begin
fco[x].r:=0-fco[x].r;
end;

h:=44100/(fttsize);
fr[x]:=fco[x].r;
//fr[xx]:=cos(h)*fco[x].i;
inc(x);
if canceldraw=true then break;
end;
x:=1;
while x<=((FTTSIZE) div 2) do begin
if canceldraw=true then break;
if fco[x].r<>0 then  vcanv.Canvas.rectangle(round((x-1)*wid),image1.Height-round(fr[x]*m),round(((x-1)*wid)+wid),image1.Height);
inc(x);
end;

image1.Picture.Bitmap.Canvas.Draw(0,0,vcanv);
vcanv.free;
image1.repaint;

canceldraw:=false;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
if effect=0 then drawpeak else drawspec ;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
if nochange=true then exit;
timer1.Enabled:=false;
if p<>trackbar1.position then mp.position:=trackbar1.Position;
p:=trackbar1.position;
timer1.Enabled:=true;
end;

procedure TForm1.GotData(Sender: TObject);
var
x,xx : integer;
p : pointer;
w : word;
begin
dp:=0;
canceldraw:=true;
if timer2.enabled=false then timer2.Enabled:=true;
setlength(fci,mp.PCMSize+1);
setlength(fco,mp.PCMSize+1);
p:=mp.PCMData;
xx:=0;
canceldraw:=false;
drawspec;
end;

end.