1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| unit Unit2;
interface
uses windows,graphics,controls;
Const RangedB = 100;
Log_10 = 2.30258509299405;
type TAxisInfo=record
fromX,toX,fromY,toY:Double;
end;
TPercent=0..99;
CONST
PixelCountMax = 32768;
TYPE
pRGBTripleArray = ^TRGBTripleArray;
TRGBTripleArray = ARRAY[0..PixelCountMax-1] OF TRGBTriple;
procedure DessineWav(WavFileName:string;WavMapArray:TImageList;SpectroMap:TBitmap;var WavAxisInfo,SpectroAxisInfo:TAxisInfo;FFTLength:cardinal;OverLapPercent:TPercent;AHeight,AWidth:Integer);
implementation
uses unitFourrier,unitLoadWav, SysUtils,types,math;
// Maps magnitudes in the range [-RangedB .. 0] dB to palette index values in the range [0 .. Rangeindex-1]
// and computes and returns the index value which corresponds to passed-in magnitude Mag
function MapToPixelindex(Mag,RangedB:Double;Rangeindex:Cardinal):Double;
var LevelIndB:Double;
begin
if Mag=0
then Result:=0
else begin
LevelIndB:=20*Ln(Mag)/Log_10;
if LevelIndB<-RangedB
then Result:=0
else Result:=Rangeindex*(LevelIndB+RangedB)/RangedB;
end;
end;
PROCEDURE HSVtoRGB (CONST H,S,V: Real; VAR R,G,B: Real);
VAR
f : Real;
i : INTEGER;
hTemp: Real; // since H is CONST parameter
p,q,t: Real;
BEGIN
IF S = 0.0 // color is on black-and-white center line
THEN BEGIN
IF IsNaN(H)
THEN BEGIN
R := V; // achromatic: shades of gray
G := V;
B := V
END;
//ELSE RAISE EColorError.Create('HSVtoRGB: S = 0 and H has a value');
END
ELSE BEGIN // chromatic color
IF H = 360.0 // 360 degrees same as 0 degrees
THEN hTemp := 0.0
ELSE hTemp := H;
hTemp := hTemp / 60; // h is now IN [0,6)
i := TRUNC(hTemp); // largest integer <= h
f := hTemp - i; // fractional part of h
p := V * (1.0 - S);
q := V * (1.0 - (S * f));
t := V * (1.0 - (S * (1.0 - f)));
CASE i OF
0: BEGIN R := V; G := t; B := p END;
1: BEGIN R := q; G := V; B := p END;
2: BEGIN R := p; G := V; B := t END;
3: BEGIN R := p; G := q; B := V END;
4: BEGIN R := t; G := p; B := V END;
5: BEGIN R := V; G := p; B := q END
END
END
END {HSVtoRGB};
Function PaletteValue(x:integer):TRGBTriple;
var r4,u:Double;
r,g,b:real;
begin
HSVtoRGB(x,255,255,r,g,b);
result.rgbtRed:=byte(round(R));
result.rgbtGreen:=byte(round(G));
result.rgbtBlue:=byte(round(b));
End;
procedure DessineWav(WavFileName:string;WavMapArray:TImageList;SpectroMap:TBitmap;var WavAxisInfo,SpectroAxisInfo:TAxisInfo;FFTLength:cardinal;OverLapPercent:TPercent;AHeight,AWidth:Integer);
var NumChannels,BitsPerSample,SampleRate,NumSamples:Cardinal;
Y:TDDArray;
c:Word;
NFFT,Numcols,col:Cardinal;
Overlap:Double;
ColIncrement:Integer;
TimeslotWidth,TimeslotIncrement:Double;
ComplexData:TComplexArray;
Magnitudes:TArrayOfDouble;
Pixelmatrix:TBBArray;
xx,yy,UnPixel:integer;
PointList:array of TPoint;
ABitmap:TBitmap;
position:integer;
couleur:integer;
P :pRGBTripleArray;
begin
if not FileExists(WavFilename) then Exit;
if LoadWaveFile(WavFileName, Y, NumChannels,BitsPerSample,SampleRate,NumSamples) then
begin
WavAxisInfo.FromX:=0;
WavAxisInfo.ToX:=NumSamples/SampleRate;
SetLength(PointList,NumSamples);
WavMapArray.Clear;
WavMapArray.Height:=AHeight;
WavMapArray.Width:=AWidth;
for c:=0 to NumChannels - 1 do
begin
ABitmap:=TBitmap.Create;
ABitmap.PixelFormat:=pf24bit;
ABitmap.Height:=AHeight;
ABitmap.Width:=AWidth;
ABitmap.Canvas.FillRect(Bounds(0,0,AWidth,AHeight));
//dessine la courbe paramétrique
for xx:=0 to NumSamples-1 do
begin
PointList[xx].X:=(xx*AWidth) div NumSamples;
PointList[xx].Y:=round((Y[xx,c]+1)*AHeight/2);
end;
ABitmap.Canvas.Polyline(PointList);
WavMapArray.Add(ABitmap,nil);
ABitmap:=nil;
end;
NFFT:=FFTLength;
Overlap:=OverLapPercent/100;
ColIncrement:=round(NFFT*(1-Overlap));
Numcols:=NumSamples div ColIncrement;
while (((Numcols-1)*ColIncrement+NFFT)>NumSamples) do Numcols:=Numcols-1;
SetLength(ComplexData,NFFT);
SetLength(Magnitudes,NFFT);
SetLength(Pixelmatrix,(NFFT div 2),Numcols);
for col:= 0 to Numcols-1 do
begin
for c:=0 to NFFT-1 do
begin
ComplexData[c].b:=0;
ComplexData[c].a:=Y[col*ColIncrement+c,0]*Hanning(NFFT,c);
end;
FourierTransform(ComplexData,true);
MagnitudeSpectrum(ComplexData,W0Hanning,Magnitudes);
for c:=0 To ((NFFT div 2) - 1) do
Pixelmatrix[c, col]:=Round(MapToPixelindex(Magnitudes[c], RangedB, 360));
end;
SpectroMap.Height:=(NFFT div 2);
SpectroMap.width:=Numcols;
SpectroAxisInfo.fromX:=WavAxisInfo.fromX;
SpectroAxisInfo.toX:=WavAxisInfo.toX;
SpectroAxisInfo.fromY:=0;
SpectroAxisInfo.toY:=SampleRate/2;
TimeslotWidth:=NFFT/SampleRate;
TimeslotIncrement:=ColIncrement/SampleRate;
for yy:=0 to SpectroMap.height-1 do
begin
P:=SpectroMap.ScanLine[yy];
for xx:=0 to SpectroMap.width-1 do
begin
couleur:=Pixelmatrix[yy,xx];
P[xx]:=PaletteValue(couleur);
end;
end;
end;
end;
end. |
Partager