forked from VE3NEA/MorseRunner
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Qsb.pas
90 lines (71 loc) · 1.89 KB
/
Qsb.pas
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
//------------------------------------------------------------------------------
//This Source Code Form is subject to the terms of the Mozilla Public
//License, v. 2.0. If a copy of the MPL was not distributed with this
//file, You can obtain one at http://mozilla.org/MPL/2.0/.
//------------------------------------------------------------------------------
unit Qsb;
interface
uses
QuickAvg, SndTypes;
type
TQsb = class
private
Filt: TQuickAverage;
FGain: Single;
FBandwidth: Single;
function NewGain: Single;
procedure SetBandwidth(const Value: Single);
public
QsbLevel: Single;
constructor Create;
destructor Destroy; override;
procedure ApplyTo(Arr: TSingleArray);
property Bandwidth: Single read FBandwidth write SetBandwidth;
end;
implementation
uses
SysUtils, RndFunc, Math, Ini;
constructor TQsb.Create;
begin
Filt := TQuickAverage.Create(nil);
Filt.Passes := 3;
QsbLevel := 1;//0.5 + 0.5 * RndUShaped;
Bandwidth := 0.1;
end;
destructor TQsb.Destroy;
begin
FreeAndNil(Filt);
inherited;
end;
function TQsb.NewGain: Single;
begin
with Filt.Filter(RndUniform, RndUniform) do
Result := Sqrt((Sqr(Re) + Sqr(Im)) * 3 * Filt.Points);
Result := Result * QsbLevel + (1-QsbLevel);
end;
procedure TQsb.SetBandwidth(const Value: Single);
var
i: integer;
begin
FBandwidth := Value;
Filt.Points := Ceil(0.37 * DEFAULTRATE / ((Ini.BufSize div 4) * Value));
for i:=0 to Filt.Points*3 do FGain := NewGain;
end;
procedure TQsb.ApplyTo(Arr: TSingleArray);
var
b, i: integer;
dG: Single;
BlkCnt: integer;
begin
BlkCnt := Length(Arr) div (Ini.BufSize div 4);
for b:=0 to BlkCnt-1 do
begin
dG := (NewGain - FGain) / (Ini.BufSize div 4);
for i:=0 to (Ini.BufSize div 4)-1 do
begin
Arr[b * (Ini.BufSize div 4) + i] := Arr[b * (Ini.BufSize div 4) + i] * FGain;
FGain := FGain + dG;
end;
end;
end;
end.