-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMundus.Diagnostics.SamplerGraph.pas
More file actions
99 lines (88 loc) · 2.25 KB
/
Mundus.Diagnostics.SamplerGraph.pas
File metadata and controls
99 lines (88 loc) · 2.25 KB
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
unit Mundus.Diagnostics.SamplerGraph;
interface
uses
Types,
Classes,
Graphics;
type
TSamplerGraph = class
private
FSamples: TArray<Integer>;
FAverages: TArray<Integer>;
FMax: Integer;
FStart: Integer;
procedure DrawValues(ATarget: TCanvas; ARect: TRect; const AValues: TArray<Integer>; AColor: TColor);
public
constructor Create;
destructor Destroy; override;
procedure AddSample(const ASanple: Integer);
procedure DrawGraph(ATarget: TCanvas; ARect: TRect);
end;
implementation
{ TSamplerGraph }
procedure TSamplerGraph.AddSample(const ASanple: Integer);
var
LAverage, LValue: Integer;
begin
FStart := (FStart + 1) mod Length(FSamples);
FSamples[FStart] := ASanple;
FMax := 0;
LAverage := 0;
for LValue in FSamples do
begin
Inc(LAverage, LValue);
if LValue > FMax then
FMax := LValue;
end;
LAverage := LAverage div Length(FSamples);
if LAverage > FMax then
FMax := LAverage;
FAverages[FStart] := LAverage;
end;
constructor TSamplerGraph.Create;
begin
SetLength(FSamples, 512);
SetLength(FAverages, 512);
end;
destructor TSamplerGraph.Destroy;
begin
inherited;
end;
procedure TSamplerGraph.DrawGraph(ATarget: TCanvas; ARect: TRect);
begin
DrawValues(ATarget, ARect, FSamples, clGreen);
DrawValues(ATarget, ARect, FAverages, clBlue);
ATarget.Pen.Color := clBlack;
ATarget.MoveTo(ARect.Left, ARect.Top);
ATarget.LineTo(ARect.Left, ARect.Bottom);
ATarget.LineTo(ARect.Right, ARect.Bottom);
ATarget.LineTo(ARect.Right, ARect.Top);
end;
procedure TSamplerGraph.DrawValues(ATarget: TCanvas; ARect: TRect;
const AValues: TArray<Integer>; AColor: TColor);
var
LEnd, LIndex, LY: Integer;
LX, LXStep, LYStep: Double;
begin
LEnd := (FStart - 1) mod Length(AValues);
if LEnd < 0 then
LEnd := High(AValues);
LIndex := FStart;
LX := ARect.Left;
LY := ARect.Bottom;
if FMax > 0 then
LYStep := ARect.Height / FMax
else
LYStep := 0;
ATarget.Pen.Width := 1;
ATarget.Pen.Color := AColor;
ATarget.MoveTo(Trunc(LX), LY);
LXStep := ARect.Width / Length(AValues);
repeat
LY := Trunc(ARect.Bottom - AValues[LIndex] * LYStep);
ATarget.LineTo(Trunc(LX), LY);
LIndex := (LIndex + 1) mod Length(AValues);
LX := LX + LXStep;
until LIndex = LEnd;
end;
end.