-
Notifications
You must be signed in to change notification settings - Fork 7
/
rm_oscillator.pas
443 lines (389 loc) · 9.74 KB
/
rm_oscillator.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
unit rm_oscillator;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, RadioModule, UComplex, formoscillator, RadioMessage;
type
TOscWaveFunction = procedure (P: PComplex; const Len: Integer) of object;
{ TRadioOscillator }
TRadioOscillator = class(TBackgroundRadioModule)
private
FFreq: Cardinal;
FSampleRate: Cardinal;
FCounter: Cardinal;
FPhase: Double;
FWaveFunc: TOscWaveFunction;
FRatio: Integer;
FUI: TOscillatorForm;
procedure GenSin(P: PComplex; const Len: Integer);
procedure GenRect(P: PComplex; const Len: Integer);
procedure GenTriangle(P: PComplex; const Len: Integer);
protected
procedure ProccessCustomMessage(const Msg: TRadioMessage; var Ret: Integer); override;
procedure DoReset; override;
function RMSetFrequency(const Msg: TRadioMessage; const Freq: Cardinal): Integer; override;
function RMSetSampleRate(const Msg: TRadioMessage; const Rate: Cardinal): Integer; override;
procedure ThreadFun(Thread: TGenericRadioThread); override;
procedure DoConfigure; override;
procedure Describe(Strs: TStrings); override;
procedure DoStopThreadFun; override;
procedure DoSyncDestroy; override;
public
constructor Create(RunQueue: TRadioRunQueue); override;
destructor Destroy; override;
end;
TOscRec = record
Freq: Integer;
PhaseDelta: Double;
SampleRate: Cardinal;
Phase: Double;
end;
{ TRadioFreqMixer }
TRadioFreqMixer = class(TRadioModule)
private
FExternalOsc: Boolean;
FBandIndex: Integer;
FFreq: Integer;
FOscFreq: Integer;
FPhaseDelta: Double;
FSampleRate: Cardinal;
FPhase: Double;
FMode: Integer;
procedure ComplexMix(const X: PComplex; const Len: Integer;
const P: PComplex);
procedure RealMix(const X: PComplex; const Len: Integer;
const P: PComplex);
procedure SetOscFreq(const Freq: Integer);
protected
function RMSetFrequency(const Msg: TRadioMessage; const Freq: Cardinal): Integer; override;
function RMSetSampleRate(const Msg: TRadioMessage; const Rate: Cardinal): Integer; override;
procedure ProccessCustomMessage(const Msg: TRadioMessage; var Ret: Integer); override;
procedure Describe(Strs: TStrings); override;
public
constructor Create(RunQueue: TRadioRunQueue); override;
destructor Destroy; override;
procedure ReceiveData(const P: PComplex; const Len: Integer); override;
end;
procedure InitSimpleOsc(var Osc: TOscRec; const Freq, SampleRate: Integer);
procedure SimpleOscMix(var Osc: TOscRec; P: PComplex; const Len: Integer);
procedure SimpleOscMixRe(var Osc: TOscRec; P: PComplex; const Len: Integer);
implementation
uses
Math, RadioSystem, SignalBasic;
procedure InitSimpleOsc(var Osc: TOscRec; const Freq, SampleRate: Integer);
begin
FillByte(Osc, SizeOf(Osc), 0);
Osc.Freq := Freq;
Osc.SampleRate := SampleRate;
if SampleRate > 0 then Osc.PhaseDelta := 2 * Pi * Freq / SampleRate;
end;
procedure SimpleOscMix(var Osc: TOscRec; P: PComplex; const Len: Integer);
var
J: Integer;
C: Integer;
V: Double;
T: Complex;
O: Double;
begin
O := Osc.PhaseDelta;
V := Osc.Phase;
for J := 0 to Len - 1 do
begin
T.re := Cos(V);
T.im := Sin(V);
P[J] := P[J] * T;
V := V + O;
end;
C := Trunc(V / (2 * Pi));
Osc.Phase := V - (2 * Pi * C);
end;
procedure SimpleOscMixRe(var Osc: TOscRec; P: PComplex; const Len: Integer);
var
J: Integer;
C: Integer;
V: Double;
O: Double;
begin
O := Osc.PhaseDelta;
V := Osc.Phase;
for J := 0 to Len - 1 do
begin
P[J].re := P[J].re * Cos(V);
P[J].im := P[J].re * Sin(V);
V := V + O;
end;
C := Trunc(V / (2 * Pi));
Osc.Phase := V - (2 * Pi * C);
end;
{ TRadioFreqMixer }
procedure TRadioFreqMixer.SetOscFreq(const Freq: Integer);
begin
FOscFreq := Integer(Freq);
FPhase := 0;
if FSampleRate > 0 then FPhaseDelta := 2 * Pi * (FFreq - FOscFreq) / FSampleRate;
end;
procedure TRadioFreqMixer.ComplexMix(const X: PComplex; const Len: Integer;
const P: PComplex);
var
T: Complex;
V: Double;
C: Integer;
J: Integer;
begin
V := FPhase;
for J := 0 to Len - 1 do
begin
T.re := Cos(V);
T.im := Sin(V);
X[J] := P[J] * T;
V := V + FPhaseDelta;
end;
C := Trunc(V / (2 * Pi));
FPhase := V - (2 * Pi * C);
end;
procedure TRadioFreqMixer.RealMix(const X: PComplex; const Len: Integer;
const P: PComplex);
var
T: Double;
V: Double;
C: Integer;
J: Integer;
begin
V := FPhase;
for J := 0 to Len - 1 do
begin
T := Cos(V);
X[J] := P[J] * T;
V := V + FPhaseDelta;
end;
C := Trunc(V / (2 * Pi));
FPhase := V - (2 * Pi * C);
end;
function TRadioFreqMixer.RMSetFrequency(const Msg: TRadioMessage;
const Freq: Cardinal): Integer;
begin
FFreq := Integer(Freq);
FPhase := 0;
if FSampleRate > 0 then FPhaseDelta := 2 * Pi * (FFreq - FOscFreq) / FSampleRate;
Result := 0;
end;
function TRadioFreqMixer.RMSetSampleRate(const Msg: TRadioMessage;
const Rate: Cardinal): Integer;
begin
FSampleRate := Rate;
FPhase := 0;
if FSampleRate > 0 then FPhaseDelta := 2 * Pi * (FFreq - FOscFreq) / FSampleRate;
Result := inherited;
end;
procedure TRadioFreqMixer.ProccessCustomMessage(const Msg: TRadioMessage;
var Ret: Integer);
begin
if Msg.Id = RM_SPECTRUM_BAND_SELECT_1 + FBandIndex then
begin
SetOscFreq(FFreq + ((Integer(Msg.ParamH) + Integer(Msg.ParamL)) div 2));
Ret := 0;
Exit;
end;
case Msg.Id of
RM_FREQMIXER_SET_FREQ:
begin
SetOscFreq(Msg.ParamH);
FMode := Msg.ParamL;
end;
RM_FREQMIXER_USE_BAND_SELECT: FBandIndex := Msg.ParamH
else
inherited
end;
end;
procedure TRadioFreqMixer.Describe(Strs: TStrings);
begin
if FExternalOsc then
Strs.Add(Format('^bExternal Osc.', []))
else
Strs.Add(Format('^bFrequency: ^n%s', [FormatFreq(FOscFreq)]));
end;
constructor TRadioFreqMixer.Create(RunQueue: TRadioRunQueue);
begin
inherited Create(RunQueue);
FHasConfig := False;
end;
destructor TRadioFreqMixer.Destroy;
begin
inherited Destroy;
end;
procedure TRadioFreqMixer.ReceiveData(const P: PComplex; const Len: Integer);
var
I: Integer;
X: PComplex;
begin
DefOutput.BufferSize := Len;
X := Alloc(DefOutput, I);
if not Assigned(X) then Exit;
if FMode = 0 then
ComplexMix(X, Len, P)
else
RealMix(X, Len, P);
DefOutput.Broadcast(I, FDataListeners);
end;
{ TRadioOscillator }
procedure TRadioOscillator.GenSin(P: PComplex; const Len: Integer);
var
J: Integer;
A: Double;
C: Integer;
V: Double;
begin
A := 2 * Pi * FFreq / FSampleRate;
V := FPhase;
for J := 0 to Len - 1 do
begin
P[J].re := Cos(V);
P[J].im := Sin(V);
V := V + A;
end;
C := Trunc(V / (2 * Pi));
FPhase := V - (2 * Pi * C);
end;
procedure TRadioOscillator.GenRect(P: PComplex; const Len: Integer);
var
J: Integer;
A: Double;
V: Double;
W: Double;
X: Double;
T: Double;
begin
X := FRatio / 101;
A := FFreq / FSampleRate;
V := FPhase;
FillByte(P^, Len * SizeOf(P^), 0);
for J := 0 to Len - 1 do
begin
W := V + A;
T := Frac(W);
P[J].re := IfThen(T <= X, 1, -1);
P[J].im := 0;
V := W;
end;
FPhase := Frac(V);
end;
procedure TRadioOscillator.GenTriangle(P: PComplex; const Len: Integer);
var
J: Integer;
A: Double;
V: Double;
W: Double;
X: Double;
T: Double;
begin
X := FRatio / 101;
A := FFreq / FSampleRate;
V := FPhase;
FillByte(P^, Len * SizeOf(P^), 0);
for J := 0 to Len - 1 do
begin
W := V + A;
T := Frac(W);
P[J].re := IfThen(T <= X, 2 * T / X - 1, -2 * (T - X) / (1 - X) + 1);
P[J].im := 0;
V := W;
end;
FPhase := Frac(V);
end;
procedure TRadioOscillator.ProccessCustomMessage(const Msg: TRadioMessage;
var Ret: Integer);
begin
case Msg.Id of
RM_OSC_WAVE:
begin
case Msg.ParamH of
SET_WAVE_RECT: FWaveFunc := @GenRect;
SET_WAVE_TRIANGLE: FWaveFunc := @GenTriangle;
else
FWaveFunc := @GenSin;
end;
FRatio := Max(1, Msg.ParamL mod 101);
GraphInvalidate;
end
else
inherited;
end;
end;
constructor TRadioOscillator.Create(RunQueue: TRadioRunQueue);
begin
inherited Create(RunQueue);
FWaveFunc := @GenSin;
FUI := TOscillatorForm.Create(nil);
FUI.Module := Self;
DefOutput.BufferSize := 1024 * 10;
end;
destructor TRadioOscillator.Destroy;
begin
inherited Destroy;
end;
procedure TRadioOscillator.DoReset;
begin
inherited;
FCounter := 0;
end;
function TRadioOscillator.RMSetFrequency(const Msg: TRadioMessage;
const Freq: Cardinal): Integer;
begin
FFreq := Freq;
Result := 0;
end;
function TRadioOscillator.{%H-}RMSetSampleRate(const Msg: TRadioMessage;
const Rate: Cardinal): Integer;
begin
FSampleRate := Rate;
Broadcast(Msg);
Result := 0;
end;
procedure TRadioOscillator.ThreadFun(Thread: TGenericRadioThread);
label
Wait;
var
I: Integer;
D: Double = 0.0;
C: Integer;
begin
if FSampleRate = 0 then goto Wait;
if Assigned(DefOutput.TryAlloc(I)) then
begin
C := DefOutput.BufferSize;
D := D + C / FSampleRate;
FWaveFunc(DefOutput.Buffer[I], C);
CancelDC(DefOutput.Buffer[I], C);
DefOutput.Broadcast(I, FDataListeners);
end;
Wait:
Sleep(10);
end;
procedure TRadioOscillator.DoConfigure;
begin
FUI.Show;
end;
procedure TRadioOscillator.Describe(Strs: TStrings);
begin
if FWaveFunc = @GenSin then
Strs.Add('^bWave: ^nSin')
else if FWaveFunc = @GenRect then
Strs.Add('^bWave: ^nRectangle')
else
Strs.Add('^bWave: ^nTriangle');
if FWaveFunc <> @GenSin then
Strs.Add(Format('^bDuty: ^n%d%', [FRatio]));
end;
procedure TRadioOscillator.DoStopThreadFun;
begin
// nop
end;
procedure TRadioOscillator.DoSyncDestroy;
begin
FUI.Free;
inherited DoSyncDestroy;
end;
initialization
RegisterModule(TRadioModuleClass(TRadioOscillator.ClassType));
RegisterModule(TRadioModuleClass(TRadioFreqMixer.ClassType));
end.