-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan2track.pas
204 lines (151 loc) · 5.14 KB
/
scan2track.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
unit scan2track;
{$include 'compileroptions.inc'}
interface
uses
Classes, SysUtils, Types, Math, Graphics, GraphType, FPCanvas, FPImage, FPWritePNG, MTProcs, fgl,
utils, inputscan, FilterIIRLPBessel, FilterIIRHPBessel;
const
CSampleDecoderBits = 10;
CSampleDecoderMax = 1 shl (CSampleDecoderBits - 1);
type
TScan2Track = class;
TSampleEvent = function(Sender: TScan2Track; X, Y, Percent: Double; Finished: Boolean): Boolean of object;
{ TScan2Track }
TScan2Track = class
private
FScan: TInputScan;
FOutputWAVFileName: String;
FOnSample: TSampleEvent;
FBitsPerSample: Integer;
FSampleRate: Integer;
FPointsPerRevolution: Integer;
FRadiansPerRevolutionPoint: Double;
function DecodeSample(radius, angleSin, angleCos: Double): Double;
public
constructor Create(ASampleRate: Integer = 48000; ABitsPerSample: Integer = 16; ADPI: Integer = 2400);
destructor Destroy; override;
procedure LoadPNG;
procedure EvalTrack;
property OnSample: TSampleEvent read FOnSample write FOnSample;
property OutputWAVFileName: String read FOutputWAVFileName write FOutputWAVFileName;
property Scan: TInputScan read FScan;
property SampleRate: Integer read FSampleRate;
property BitsPerSample: Integer read FBitsPerSample;
property PointsPerRevolution: Integer read FPointsPerRevolution;
property RadiansPerRevolutionPoint: Double read FRadiansPerRevolutionPoint;
end;
implementation
{ TScan2Track }
constructor TScan2Track.Create(ASampleRate: Integer; ABitsPerSample: Integer; ADPI: Integer);
begin
FSampleRate := ASampleRate;
FBitsPerSample := ABitsPerSample;
FPointsPerRevolution := Round(FSampleRate / C45RpmRevolutionsPerSecond);
FRadiansPerRevolutionPoint := Pi * 2.0 / FPointsPerRevolution;
FScan := TInputScan.Create(ADPI);
end;
destructor TScan2Track.Destroy;
begin
FScan.Free;
inherited Destroy;
end;
function TScan2Track.DecodeSample(radius, angleSin, angleCos: Double): Double;
var
ismp, upCnt: Integer;
r, px, py, cxa, sample, sampleMiddle, upAcc: Double;
smpBuf: array[-CSampleDecoderMax .. CSampleDecoderMax - 1] of Double;
begin
cxa := C45RpmRecordingGrooveWidth * Scan.DPI / (CSampleDecoderMax - 0.5);
r := radius + CSampleDecoderMax * cxa;
px := angleCos * r + Scan.Center.X;
py := angleSin * r * Scan.SkewY + Scan.Center.Y;
if not Scan.InRangePointD(py, px) then
Exit(0.0);
sampleMiddle := 0.0;
for ismp := -CSampleDecoderMax to CSampleDecoderMax - 1 do
begin
r := radius + (ismp + 0.5) * cxa;
px := angleCos * r + Scan.Center.X;
py := angleSin * r * Scan.SkewY + Scan.Center.Y;
sample := Scan.GetPointD_Sinc(Scan.Image, py, px);
sampleMiddle += sample;
smpBuf[ismp] := sample;
end;
sampleMiddle /= CSampleDecoderMax * 2.0;
upAcc := 0;
upCnt := 0;
for ismp := -CSampleDecoderMax to CSampleDecoderMax - 1 do
if smpBuf[ismp] >= sampleMiddle then
begin
upAcc += ismp + 0.5;
Inc(upCnt);
end;
Result := DivDef(upAcc, (CSampleDecoderMax - 0.5) * upCnt, 0.0);
end;
procedure TScan2Track.EvalTrack;
var
samples: TSmallIntDynArray;
procedure StoreSample(fsmp: Double; pos: Integer);
var
smp: SmallInt;
begin
smp := Make16BitSample(fsmp);
while pos >= Length(samples) do
SetLength(samples, Ceil(Length(samples) * cPhi));
samples[pos] := smp;
end;
var
radius, rOuter, sn, cs, ox, oy, fbRatio, fSmp, ffSmp, pct: Double;
iSample, iLut: Integer;
validSample: Boolean;
fltSample: TFilterIIRHPBessel;
sinCosLut: TSinCosDynArray;
begin
WriteLn('EvalTrack');
SetLength(samples, FSampleRate);
fltSample := TFilterIIRHPBessel.Create(nil);
try
fltSample.FreqCut1 := CLowCutoffFreq;
fltSample.SampleRate := FSampleRate;
fltSample.Order := 1;
BuildSinCosLUT(FPointsPerRevolution, sinCosLut, Scan.GrooveStartAngle, -2.0 * Pi);
fbRatio := CutoffToFeedbackRatio(CLowCutoffFreq, FSampleRate) * C45RpmRecordingGrooveWidth * Scan.DPI;
rOuter := C45RpmOuterSize * 0.5 * Scan.DPI;
iSample := 0;
iLut := 0;
radius := Scan.FirstGrooveRadius;
repeat
cs := sinCosLut[iLut].Cos;
sn := sinCosLut[iLut].Sin;
Inc(iLut);
if iLut >= FPointsPerRevolution then
iLut := 0;
fSmp := DecodeSample(radius, sn, cs);
radius += fSmp * fbRatio;
ffSmp := fltSample.FilterFilter(fSmp);
StoreSample(ffSmp, iSample);
Inc(iSample);
validSample := InRange(radius, Scan.ConcentricGrooveRadius, rOuter);
if Assigned(FOnSample) then
begin
ox := cs * radius;
oy := sn * radius * Scan.SkewY;
pct := (Sqrt(Sqr(ox) + Sqr(oy)) - Scan.ConcentricGrooveRadius) / (Scan.FirstGrooveRadius - Scan.ConcentricGrooveRadius);
pct := EnsureRange(1.0 - pct, 0.0, 1.0) * 100.0;
validSample := FOnSample(Self, ox + Scan.Center.X, oy + Scan.Center.Y, pct, not validSample) and validSample;
end;
until not validSample;
SetLength(samples, iSample);
CreateWAV(1, 16, FSampleRate, FOutputWAVFileName, samples);
finally
fltSample.Free;
end;
WriteLn;
WriteLn('Done!');
end;
procedure TScan2Track.LoadPNG;
begin
Scan.LoadPNG;
Scan.FindTrack(FSampleRate);
end;
end.