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
| unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
System.Math, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, bass, Vcl.StdCtrls, Vcl.Grids,
Vcl.ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
l1: TLabel;
Button2: TButton;
sg1: TStringGrid;
rg1: TRadioGroup;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Déclarations privées }
procedure Error(msg: string);
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
Channel : HRECORD; // recording channel
implementation
{$R *.dfm}
var
MasterFreq0,
PeakFreq0: double;
rec: boolean;
const
SAMPLERATE = 44100;
//---------------------------------------------------------
// Recording callback - not doing anything with the data
function DuffRecording(handle : HRECORD; const Buffer : Pointer; Length : DWORD; user : Pointer) : Boolean; stdcall;
begin
Result := True; // continue recording
end;
procedure TForm1.Button1Click(Sender: TObject);
var
PeakBin: Integer;
Rate: single;
PeakVal: single;
FFT, temp: array [0..4095] of Single;
var
AStream: HSTREAM;
i: Integer;
begin
rec:=true;
rate:=48000;
// start recording (48000hz mono 16-bit)
PeakBin := 0;
PeakVal := 0.0;
case rg1.ItemIndex of
0:
begin
repeat
Channel := BASS_RecordStart(48000, 1, 0, @DuffRecording, NIL);
if Channel = 0 then
begin
Error('Can''t start recording');
Halt;
end;
if BASS_ChannelGetData(Channel, @temp, BASS_DATA_FFT4096 or BASS_DATA_FFT_REMOVEDC)=-1 then
break; // get FFT data (4096 sample)
for i:=0 to 2047 do
fft[i]:=fft[i]+temp[i];
application.processmessages
until not rec;
for i := 1 to 2046 do
begin
sg1.Cells[0,i]:=i.tostring;
sg1.Cells[1,i]:=fft[i].ToString;
if (Peakval < FFT[i]) then // found a new peak
begin
PeakVal := FFT[i];
PeakBin := i;
end;
end;
end;
1:
begin
repeat
Channel := BASS_RecordStart(48000, 1, 0, @DuffRecording, NIL);
if Channel = 0 then
begin
Error('Can''t start recording');
Halt;
end;
if BASS_ChannelGetData(Channel, @temp, BASS_DATA_FFT_complex or BASS_DATA_FFT_REMOVEDC)=-1 then // get FFT data (complexes)
break;
for i:=0 to 2047 do
fft[i]:=fft[i]+temp[i];
application.processmessages
until not rec;
i:=0;
while i<4094 do
begin
sg1.Cells[0, i div 2]:=(i div 2).tostring;
sg1.Cells[1, i div 2]:=fft[i].ToString;
sg1.Cells[2, i div 2]:=fft[i+1].ToString;
sg1.Cells[3, i div 2]:=hypot(FFT[i], FFT[i+1]).ToString;
// calcul direct de l'amplitude du signal en faisant racine carrée (real*reel + imag*imag)
if (Peakval < hypot(FFT[i], FFT[i+1])) then // found a new peak
begin
PeakVal := hypot(FFT[i], FFT[i+1]);
PeakBin := i div 2;
end;
inc(i, 2);
end;
end;
end;
PeakFreq0 := PeakBin * (Rate / 2048); // get frequency of peak bin
if (MasterFreq0 < PeakFreq0) then
MasterFreq0 := PeakFreq0;
l1.caption:='Rang: '+peakbin.ToString+ ' '+masterfreq0.ToString+'Hz';
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
rec:=false;
end;
procedure TForm1.Error(msg: string);
begin
MessageBox(Handle, PChar(msg + #13#10 + '(error code: ' + IntToStr(BASS_ErrorGetCode) + ')'), nil, 0);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
if (HIWORD(BASS_GetVersion) <> BASSVERSION) then begin
Error('Mauvaise version de la librairie BASS !');
Halt;
end;
// initialize BASS recording (default device)
if not BASS_RecordInit(-1) then
begin
Error('Can''t initialize device');
Halt;
end;
end;
end. |
Partager