forked from VE3NEA/MorseRunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Station.pas
235 lines (185 loc) · 5.84 KB
/
Station.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
//------------------------------------------------------------------------------
//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 Station;
interface
uses
SysUtils, Classes, Math, SndTypes, Ini, MorseKey;
const
NEVER = MAXINT;
type
TStationMessage = (msgNone, msgCQ, msgNR, msgTU, msgMyCall, msgHisCall,
msgB4, msgQm, msgNil, msgGarbage, msgR_NR, msgR_NR2, msgDeMyCall1, msgDeMyCall2,
msgDeMyCallNr1, msgDeMyCallNr2, msgNrQm, msgLongCQ, msgMyCallNr2,
msgQrl, msgQrl2, msqQsy, msgAgn);
TStationMessages = set of TStationMessage;
TStationState = (stListening, stCopying, stPreparingToSend, stSending);
TStationEvent = (evTimeout, evMsgSent, evMeStarted, evMeFinished);
TStation = class (TCollectionItem)
private
FBfo: Single;
dPhi: Single;
FPitch: integer;
function GetBfo: Single;
procedure SetPitch(const Value: integer);
protected
SendPos: integer;
TimeOut: integer;
NrWithError: boolean;
function NrAsText: string;
public
Amplitude: Single;
Wpm: integer;
Envelope: TSingleArray;
State: TStationState;
NR, RST: integer;
MyCall, HisCall: string;
Msg: TStationMessages;
MsgText: string;
constructor CreateStation;
procedure Tick;
function GetBlock: TSingleArray; virtual;
procedure ProcessEvent(AEvent: TStationEvent); virtual; abstract;
procedure SendMsg(AMsg: TStationMessage);
procedure SendText(AMsg: string); virtual;
procedure SendMorse(AMorse: string);
property Pitch: integer read FPitch write SetPitch;
property Bfo: Single read GetBfo;
end;
implementation
{ TStation }
constructor TStation.CreateStation;
begin
inherited Create(nil);
end;
function TStation.GetBfo: Single;
begin
Result := FBfo;
FBfo := FBfo + dPhi;
if FBfo > TWO_PI then FBfo := FBfo - TWO_PI;
end;
function TStation.GetBlock: TSingleArray;
begin
Result := Copy(Envelope, SendPos, Ini.BufSize);
//advance TX buffer
Inc(SendPos, Ini.BufSize);
if SendPos = Length(Envelope) then Envelope := nil;
end;
procedure TStation.SendMsg(AMsg: TStationMessage);
begin
if Envelope = nil then Msg := [];
if AMsg = msgNone then begin State := stListening; Exit; End;
Include(Msg, AMsg);
case AMsg of
msgCQ: SendText('CQ <my> TEST');
msgNR: SendText('<#>');
msgTU: SendText('TU');
msgMyCall: SendText('<my>');
msgHisCall: SendText('<his>');
msgB4: SendText('QSO B4');
msgQm: SendText('?');
msgNil: SendText('NIL');
msgR_NR: SendText('R <#>');
msgR_NR2: SendText('R <#> <#>');
msgDeMyCall1: SendText('DE <my>');
msgDeMyCall2: SendText('DE <my> <my>');
msgDeMyCallNr1: SendText('DE <my> <#>');
msgDeMyCallNr2: SendText('DE <my> <my> <#>');
msgMyCallNr2: SendText('<my> <my> <#>');
msgNrQm: SendText('NR?');
msgLongCQ: SendText('CQ CQ TEST <my> <my> TEST');
msgQrl: SendText('QRL?');
msgQrl2: SendText('QRL? QRL?');
msqQsy: SendText('<his> QSY QSY');
msgAgn: SendText('AGN');
end;
end;
procedure TStation.SendText(AMsg: string);
begin
if Pos('<#>', AMsg) > 0 then
begin
//with error
AMsg := StringReplace(AMsg, '<#>', NrAsText, []);
//error cleared
AMsg := StringReplace(AMsg, '<#>', NrAsText, [rfReplaceAll]);
end;
AMsg := StringReplace(AMsg, '<my>', MyCall, [rfReplaceAll]);
{
if CallsFromKeyer
then AMsg := StringReplace(AMsg, '<his>', ' ', [rfReplaceAll])
else AMsg := StringReplace(AMsg, '<his>', HisCall, [rfReplaceAll]);
}
if MsgText <> ''
then MsgText := MsgText + ' ' + AMsg
else MsgText := AMsg;
SendMorse(Keyer.Encode(MsgText));
end;
procedure TStation.SendMorse(AMorse: string);
var
i: integer;
begin
if Envelope = nil then
begin
SendPos := 0;
FBfo := 0;
end;
Keyer.Wpm := Wpm;
Keyer.MorseMsg := AMorse;
Envelope := Keyer.Envelope;
for i:=0 to High(Envelope) do Envelope[i] := Envelope[i] * Amplitude;
State := stSending;
TimeOut := NEVER;
end;
procedure TStation.SetPitch(const Value: integer);
begin
FPitch := Value;
dPhi := TWO_PI * FPitch / DEFAULTRATE;
end;
procedure TStation.Tick;
begin
//just finished sending
if (State = stSending) and (Envelope = nil) then
begin
MsgText := '';
State := stListening;
ProcessEvent(evMsgSent);
end
//check timeout
else if State <> stSending then
begin
if TimeOut > -1 then Dec(TimeOut);
if TimeOut = 0 then ProcessEvent(evTimeout);
end;
end;
function TStation.NrAsText: string;
var
Idx: integer;
begin
Result := Format('%d%.3d', [RST, NR]);
if NrWithError then
begin
Idx := Length(Result);
if not (Result[Idx] in ['2'..'7']) then Dec(Idx);
if Result[Idx] in ['2'..'7'] then
begin
if Random < 0.5 then Dec(Result[Idx]) else Inc(Result[Idx]);
Result := Result + Format('EEEEE %.3d', [NR]);
end;
NrWithError := false;
end;
Result := StringReplace(Result, '599', '5NN', [rfReplaceAll]);
if Ini.RunMode <> rmHst then
begin
Result := StringReplace(Result, '000', 'TTT', [rfReplaceAll]);
Result := StringReplace(Result, '00', 'TT', [rfReplaceAll]);
if Random < 0.4
then Result := StringReplace(Result, '0', 'O', [rfReplaceAll])
else if Random < 0.97
then Result := StringReplace(Result, '0', 'T', [rfReplaceAll]);
if Random < 0.97
then Result := StringReplace(Result, '9', 'N', [rfReplaceAll]);
end;
end;
end.