forked from VE3NEA/MorseRunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contest.pas
320 lines (264 loc) · 7.64 KB
/
Contest.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//------------------------------------------------------------------------------
//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 Contest;
interface
uses
SysUtils, SndTypes, Station, StnColl, MyStn, Math, Ini,
MovAvg, Mixers, VolumCtl, RndFunc, TypInfo, DxStn, DxOper, Log;
type
TContest = class
private
function DxCount: integer;
procedure SwapFilters;
public
BlockNumber: integer;
Me: TMyStation;
Stations: TStations;
Agc: TVolumeControl;
Filt, Filt2: TMovingAverage;
Modul: TModulator;
RitPhase: Single;
FStopPressed: boolean;
constructor Create;
destructor Destroy; override;
procedure Init;
function Minute: Single;
function GetAudio: TSingleArray;
procedure OnMeFinishedSending;
procedure OnMeStartedSending;
end;
var
Tst: TContest;
implementation
uses
Main;
{ TContest }
constructor TContest.Create;
begin
Me := TMyStation.CreateStation;
Stations := TStations.Create;
Filt := TMovingAverage.Create(nil);
Modul := TModulator.Create;
Agc := TVolumeControl.Create(nil);
Filt.Points := Round(0.7 * DEFAULTRATE / Ini.BandWidth);
Filt.Passes := 3;
Filt.SamplesInInput := Ini.BufSize;
Filt.GainDb := 10 * Log10(500/Ini.Bandwidth);
Filt2 := TMovingAverage.Create(nil);
Filt2.Passes := Filt.Passes;
Filt2.SamplesInInput := Filt.SamplesInInput;
Filt2.GainDb := Filt.GainDb;
Modul.SamplesPerSec := DEFAULTRATE;
Modul.CarrierFreq := Ini.Pitch;
Agc.NoiseInDb := 76;
Agc.NoiseOutDb := 76;
Agc.AttackSamples := 155; //AGC attack 5 ms
Agc.HoldSamples := 155;
Agc.AgcEnabled := true;
Init;
end;
destructor TContest.Destroy;
begin
Me.Free;
Filt.Free;
Filt2.Free;
Modul.Free;
inherited;
end;
procedure TContest.Init;
begin
Me.Init;
Stations.Clear;
BlockNumber := 0;
end;
function TContest.GetAudio: TSingleArray;
const
NOISEAMP = 6000;
var
ReIm: TReImArrays;
Blk: TSingleArray;
i, Stn: integer;
Bfo: Single;
Smg, Rfg: Single;
begin
//minimize audio output delay
SetLength(Result, 1);
Inc(BlockNumber);
if BlockNumber < 6 then Exit;
//complex noise
SetLengthReIm(ReIm, Ini.BufSize);
for i:=0 to High(ReIm.Re) do
begin
ReIm.Re[i] := 3 * NOISEAMP * (Random-0.5);
ReIm.Im[i] := 3 * NOISEAMP * (Random-0.5);
end;
//QRN
if Ini.Qrn then
begin
//background
for i:=0 to High(ReIm.Re) do
if Random < 0.01 then ReIm.Re[i] := 60 * NOISEAMP * (Random-0.5);
//burst
if Random < 0.01 then Stations.AddQrn;
end;
//QRM
if Ini.Qrm and (Random < 0.0002) then Stations.AddQrm;
//audio from stations
Blk := nil;
for Stn:=0 to Stations.Count-1 do
if Stations[Stn].State = stSending then
begin
Blk := Stations[Stn].GetBlock;
for i:=0 to High(Blk) do
begin
Bfo := Stations[Stn].Bfo - RitPhase - i * TWO_PI * Ini.Rit / DEFAULTRATE;
ReIm.Re[i] := ReIm.Re[i] + Blk[i] * Cos(Bfo);
ReIm.Im[i] := ReIm.Im[i] - Blk[i] * Sin(Bfo);
end;
end;
//Rit
RitPhase := RitPhase + Ini.BufSize * TWO_PI * Ini.Rit / DEFAULTRATE;
while RitPhase > TWO_PI do RitPhase := RitPhase - TWO_PI;
while RitPhase < -TWO_PI do RitPhase := RitPhase + TWO_PI;
//my audio
if Me.State = stSending then
begin
Blk := Me.GetBlock;
//self-mon. gain
Smg := Power(10, (MainForm.VolumeSlider1.Value - 0.75) * 4);
Rfg := 1;
for i:=0 to High(Blk) do
if Ini.Qsk
then
begin
if Rfg > (1 - Blk[i]/Me.Amplitude)
then Rfg := (1 - Blk[i]/Me.Amplitude)
else Rfg := Rfg * 0.997 + 0.003;
ReIm.Re[i] := Smg * Blk[i] + Rfg * ReIm.Re[i];
ReIm.Im[i] := Smg * Blk[i] + Rfg * ReIm.Im[i];
end
else
begin
ReIm.Re[i] := Smg * (Blk[i]);
ReIm.Im[i] := Smg * (Blk[i]);
end;
end;
//LPF
Filt2.Filter(ReIm);
ReIm := Filt.Filter(ReIm);
if (BlockNumber mod 10) = 0 then SwapFilters;
//mix up to Pitch frequency
Result := Modul.Modulate(ReIm);
//AGC
Result := Agc.Process(Result);
//save
with MainForm.AlWavFile1 do
if IsOpen then WriteFrom(@Result[0], nil, Ini.BufSize);
//timer tick
Me.Tick;
for Stn:=Stations.Count-1 downto 0 do Stations[Stn].Tick;
//if DX is done, write to log and kill
for i:=Stations.Count-1 downto 0 do
if Stations[i] is TDxStation then
with Stations[i] as TDxStation do
if (Oper.State = osDone) and (QsoList <> nil) and (MyCall = QsoList[High(QsoList)].Call)
then
begin
DataToLastQso;
with MainForm.RichEdit1.Lines do Delete(Count-1);
Log.CheckErr;
Log.LastQsoToScreen;
if Ini.RunMode = RmHst
then Log.UpdateStatsHst
else Log.UpdateStats;
end;
//show info
ShowRate;
MainForm.Panel2.Caption := FormatDateTime('hh:nn:ss', BlocksToSeconds(BlockNumber) / 86400);
if Ini.RunMode = rmPileUp then
MainForm.Panel4.Caption := Format('Pile-Up: %d', [DxCount]);
if (RunMode = rmSingle) and (DxCount = 0) then
begin
Me.Msg := [msgCq]; //no need to send cq in this mode
Stations.AddCaller.ProcessEvent(evMeFinished);
end
else if (RunMode = rmHst) and (DxCount < Activity) then
begin
Me.Msg := [msgCq];
for i:=DxCount+1 to Activity do
Stations.AddCaller.ProcessEvent(evMeFinished);
end;
if (BlocksToSeconds(BlockNumber) >= (Duration * 60)) or FStopPressed then
begin
if RunMode = rmHst then
begin
MainForm.Run(rmStop);
FStopPressed := false;
MainForm.PopupScoreHst;
end
else if (RunMode = rmWpx) and not FStopPressed then
begin
MainForm.Run(rmStop);
FStopPressed := false;
MainForm.PopupScoreWpx;
end
else
begin
MainForm.Run(rmStop);
FStopPressed := false;
end;
{
if (RunMode in [rmWpx, rmHst]) and not FStopPressed
then begin MainForm.Run(rmStop); MainForm.PopupScore; end
else MainForm.Run(rmStop);
}
end;
end;
function TContest.DxCount: integer;
var
i: integer;
begin
Result := 0;
for i:=Stations.Count-1 downto 0 do
if (Stations[i] is TDxStation) and
(TDxStation(Stations[i]).Oper.State <> osDone)
then Inc(Result);
end;
function TContest.Minute: Single;
begin
Result := BlocksToSeconds(BlockNumber) / 60;
end;
procedure TContest.OnMeFinishedSending;
var
i: integer;
begin
//the stations heard my CQ and want to call
if (not (RunMode in [rmSingle, RmHst])) then
if (msgCQ in Me.Msg) or
((QsoList <> nil) and (msgTU in Me.Msg) and (msgMyCall in Me.Msg))then
for i:=1 to RndPoisson(Activity / 2) do Stations.AddCaller;
//tell callers that I finished sending
for i:=Stations.Count-1 downto 0 do
Stations[i].ProcessEvent(evMeFinished);
end;
procedure TContest.OnMeStartedSending;
var
i: integer;
begin
//tell callers that I started sending
for i:=Stations.Count-1 downto 0 do
Stations[i].ProcessEvent(evMeStarted);
end;
procedure TContest.SwapFilters;
var
F: TMovingAverage;
begin
F := Filt;
Filt := Filt2;
Filt2 := F;
Filt2.Reset;
end;
end.