-
Notifications
You must be signed in to change notification settings - Fork 0
/
CWencoder.cs
1631 lines (1413 loc) · 54.4 KB
/
CWencoder.cs
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=================================================================
// CWencoder.cs
//=================================================================
// Copyright (C) 2011,2012 S56A YT7PWR
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//=================================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace CWExpert
{
unsafe public class CWEncode
{
#region DLL import
[DllImport("Receiver.dll", EntryPoint = "StartTimer")]
public static extern bool TimerInit(int period, int resolution);
[DllImport("Receiver.dll", EntryPoint = "StopTimer")]
public static extern void TimerStop();
[DllImport("Receiver.dll", EntryPoint = "TimerWait")]
public static extern void TimerWait();
[DllImport("kernel32.dll", EntryPoint = "EnterCriticalSection")]
public static extern void EnterCriticalSection(void* cs_ptr);
[DllImport("kernel32.dll", EntryPoint = "LeaveCriticalSection")]
public static extern void LeaveCriticalSection(void* cs_ptr);
[DllImport("kernel32.dll", EntryPoint = "InitializeCriticalSection")]
public static extern void InitializeCriticalSection(void* cs_ptr);
[DllImport("kernel32.dll", EntryPoint = "InitializeCriticalSectionAndSpinCount")]
public static extern int InitializeCriticalSectionAndSpinCount(void* cs_ptr, uint spincount);
[DllImport("kernel32.dll", EntryPoint = "DeleteCriticalSection")]
public static extern byte DeleteCriticalSection(void* cs_ptr);
[DllImport("Receiver.dll", EntryPoint = "NewCriticalSection")]
public static extern void* NewCriticalSection();
[DllImport("Receiver.dll", EntryPoint = "DestroyCriticalSection")]
public static extern void DestroyCriticalSection(void* cs_ptr);
#endregion
#region Enum
enum OscType
{
ComplexTone = 1,
floatTone = 0,
}
public enum CWToneState
{
CWTone_IDLE = 0,
CWTone_WAIT = 1,
CWTone_RISE = 2,
CWTone_STDY = 3,
CWTone_FALL = 4,
CWTone_HOLD = 5,
}
#endregion
#region structures
[StructLayout(LayoutKind.Sequential)]
public struct KeyerPrev
{
public int dit;
public int dah;
}
[StructLayout(LayoutKind.Sequential)]
public struct KeyerElement
{
public bool invtd; // insert inverted element
public bool psqam; // paddles squeezed after mid-element
public int curr; // -1 = nothing, 0 = dit, 1 = dah
public int iamb; // 0 = none, 1 = squeezed, 2 = released
public int last; // -1 = nothing, 0 = dit, 1 = dah
}
[StructLayout(LayoutKind.Sequential)]
public struct KeyerTimeout
{
public float beep;
public float dlay;
public float elem;
public float midl;
}
[StructLayout(LayoutKind.Sequential)]
public struct KeyerLogic
{
public int dlay_type; // 0 = none, 1 = interchar, 2 = interword
public KeyerTimeout timeout;
public KeyerElement element;
public KeyerFlag flag;
}
[StructLayout(LayoutKind.Sequential)]
public struct KeyerAutoSpace
{
public bool khar;
public bool word;
}
[StructLayout(LayoutKind.Sequential)]
public struct KeyerMemory
{
public bool dit;
public bool dah;
}
[StructLayout(LayoutKind.Sequential)]
public struct KeyerFlag
{
public bool iambic; // iambic or straight
public bool mdlmdB;
public KeyerMemory memory;
public KeyerAutoSpace autospace;
public KeyerPrev prev;
public bool init;
}
[StructLayout(LayoutKind.Sequential)]
public struct Keyer_state
{
public int debounce; // # seconds to read paddles
public int mode; // 0 = mode A, 1 = mode B
public int weight; // 15 -> 85%
public float wpm; // for iambic keyer
public KeyerFlag flag;
}
[StructLayout(LayoutKind.Sequential)]
public struct Rise
{
public float dur;
public float incr;
public int want;
public int have;
}
[StructLayout(LayoutKind.Sequential)]
public struct Fall
{
public float dur;
public float incr;
public int want;
public int have;
}
[StructLayout(LayoutKind.Sequential)]
public struct CWToneGen
{
public float curr;
public float gain;
public float mul;
public float scl;
public float sr;
public oscillator osc;
public oscillator mon_osc;
public double harmonic;
public double amplitude;
public Rise rise;
public Fall fall;
public int size;
public CWToneState stage;
}
[StructLayout(LayoutKind.Sequential)]
public struct IQ
{
public float phase;
public float gain;
}
[StructLayout(LayoutKind.Sequential)]
public struct OSC
{
public int size;
public ComplexF[] signalpoints;
public double Phase;
public double mon_phase;
public double Frequency;
public int OscillatorType;
}
[StructLayout(LayoutKind.Sequential)]
public struct oscillator
{
public double freq;
public double phase;
public OSC gen;
}
[StructLayout(LayoutKind.Sequential)]
public struct tx
{
public float samplerate;
public IQ iqfix;
public oscillator osc;
public CWToneGen cwt;
}
#endregion
#region constant
const int NO_TIMEOUTS_SCHED = (-2);
const int NO_ELEMENT = (-1);
const int DIT = (0);
const int DAH = (1);
const int MODE_A = (0);
const int MODE_B = (1);
const int NO_PADDLE_SQUEEZE = (0);
const int PADDLES_SQUEEZED = (1);
const int PADDLES_RELEASED = (2);
const int NO_DELAY = (0);
const int CHAR_SPACING_DELAY = (1);
const int WORD_SPACING_DELAY = (2);
const int DEBOUNCE_BUF_MAX_SIZE = (30);
#endregion
#region variable
delegate void CrossThreadSetText(string command, int channel_no, string out_txt);
public tx Transmiter;
public AutoResetEvent watchdog_event = new AutoResetEvent(false);
private AutoResetEvent play_event = new AutoResetEvent(false);
private AutoResetEvent audio_callback_event = new AutoResetEvent(false);
private AutoResetEvent end_event = new AutoResetEvent(false);
private bool play_sound = false;
private const double TWOPI = 6.28318530717856;
private const double M_PI = 3.14159265358928;
public bool run_transmiter = false;
private Thread CWEncode_thread;
private Thread sound_thread;
private string message = "";
private int TONE_SIZE;
private int TONE_DELAY = 4;
private Mutex transmit_mutex = new Mutex();
private Mutex tone_mutex = new Mutex();
private CWExpert MainForm;
private ComplexF[] zero_buffer;
private ComplexF[] iq_buffer;
private ComplexF[] mon_iq_buffer;
private int time_base; // Time base for millisecond timings. A standard 1WPM dit = 1200 ms mark, 1200 ms space
private int wpm; // Word WPM
private int ctime; // Character dit time (ms)
private const string errorCode = "........";
private int dottime;
private int dashtime;
delegate void CrossThreadCommand(string command, int param_1, string param_2);
public Thread Keyer;
public Thread watchdog;
public bool runKeyer = false;
public AutoResetEvent KeyerEvent = new AutoResetEvent(false);
public KeyerState keyer_state = KeyerState.Silence;
public Keyer_mode keyer_mode = Keyer_mode.Iambic;
public int SemiBreak;
private bool message_playing = false;
unsafe private static void* cs_audio;
private AutoResetEvent audio_event = new AutoResetEvent(false);
private Dictionary<char, string> Morse = new Dictionary<char, string>()
{
{ 'A', ".-" },
{ 'B', "-..." },
{ 'C', "-.-." },
{ 'D', "-.." },
{ 'E', "." },
{ 'F', "..-." },
{ 'G', "--." },
{ 'H', "...." },
{ 'I', ".." },
{ 'J', ".---" },
{ 'K', "-.-" },
{ 'L', ".-.." },
{ 'M', "--" },
{ 'N', "-." },
{ 'O', "---" },
{ 'P', ".--." },
{ 'Q', "--.-" },
{ 'R', ".-." },
{ 'S', "..." },
{ 'T', "-" },
{ 'U', "..-" },
{ 'V', "...-" },
{ 'W', ".--" },
{ 'X', "-..-" },
{ 'Y', "-.--" },
{ 'Z', "--.." },
{ '1', ".----" },
{ '2', "..---" },
{ '3', "...--" },
{ '4', "....-" },
{ '5', "....." },
{ '6', "-...." },
{ '7', "--..." },
{ '8', "---.." },
{ '9', "----." },
{ '0', "-----" },
{ '.', ".-.-.-" },
{ ',', "--..--" },
{ ':', "---..." },
{ ';', "-.-.-." },
{ '?', "..--.." },
{ '-', "-....-" },
{ '+', ".-.-." },
{ '/', "-..-." },
{ '=', "-...-" },
{ '@', ".--.-." },
{ '!', "-.-.--" },
{ '%', ".----." }, // \
{ '(', "-.--." },
{ ')', "-.--.-" },
{ '$', "...-..-" },
{ '&', ".-..." },
{ '"', ".-..-." },
{ '_', "..--.-" },
{ '\r', " " },
{ '\n', " " },
{ '\t', " " },
{ ' ', " " },
{ '#', "#" },
{ '*', "*" }
};
#endregion
#region properties
private bool tun = false;
public bool TUN
{
get { return tun; }
set
{
tun = value;
run_transmiter = false;
}
}
private int tx_off_delay = 400;
public int TXOffDelay
{
get { return tx_off_delay; }
set { tx_off_delay = value; }
}
private double tx_if_shift = 15000.0;
public double TXIfShift
{
get { return tx_if_shift; }
set { tx_if_shift = value; }
}
private int cw_speed = 25;
public int CWSpedd
{
get { return cw_speed; }
set
{
cw_speed = value;
wpm = cw_speed;
ctime = time_base / wpm;
dottime = ctime / TONE_DELAY;
dashtime = dottime * 3;
}
}
private float tx_phase = 0.0f;
public float TXPhase
{
set { tx_phase = 0.001f * value; }
}
private float tx_gain = 0.0f;
public float TXGain
{
set { tx_gain = (1.0f + 0.001f * value); }
}
private double mon_frequency = 700.0;
public double MonFreq
{
set
{
mon_frequency = value;
Transmiter.cwt.mon_osc.gen.Frequency = mon_frequency;
}
}
#endregion
#region constructor/destructor/init
public CWEncode(CWExpert main_form)
{
MainForm = main_form;
KeyerInit(MainForm.SetupForm.chkG59Iambic.Checked, (float)MainForm.SetupForm.udCWSpeed.Value);
//TONE_SIZE = 48 * TONE_DELAY * Audio.SampleRate / 48000;
time_base = 1200;
wpm = cw_speed;
ctime = time_base / wpm; // dit time
dottime = ctime / TONE_DELAY;
dashtime = dottime * 3;
Transmiter.osc.gen.Frequency = 0.0;
Transmiter.cwt.osc.phase = 0.0;
Transmiter.cwt.mon_osc.phase = 0.0;
Transmiter.cwt.sr = Audio.SampleRate;
Transmiter.cwt.osc.gen.Frequency = (TWOPI * tx_if_shift) / Transmiter.cwt.sr;
Transmiter.cwt.mon_osc.gen.Frequency = (TWOPI * mon_frequency) / Transmiter.cwt.sr;
MainForm.output_ring_buf.Restart();
MainForm.mon_ring_buf.Restart();
Transmiter = new tx();
CreateTransmiter();
zero_buffer = new ComplexF[2048];
iq_buffer = new ComplexF[2048];
mon_iq_buffer = new ComplexF[2048];
cs_audio = (void*)0x0;
cs_audio = NewCriticalSection();
if (InitializeCriticalSectionAndSpinCount(cs_audio, 0x00000080) == 0)
{
MessageBox.Show("CriticalSection Failed", "Error!");
}
}
~CWEncode()
{
try
{
if (cs_audio != null)
{
DeleteCriticalSection(cs_audio);
DestroyCriticalSection(cs_audio);
}
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
}
}
private bool CreateTransmiter()
{
try
{
newOSC(ref Transmiter.osc, Audio.BlockSize, OscType.ComplexTone, 0.0f, 0.0f, (float)Audio.SampleRate);
newCWToneGen(ref Transmiter.cwt.osc, 0.0f, (float)MainForm.SetupForm.udTXIfShift.Value, (float)MainForm.SetupForm.udCWRise.Value,
(float)MainForm.SetupForm.udCWFall.Value, TONE_SIZE, (float)Audio.SampleRate);
newCWToneGen(ref Transmiter.cwt.mon_osc, 0.0f, (float)MainForm.SetupForm.udMonitorFrequncy.Value, (float)MainForm.SetupForm.udCWRise.Value,
(float)MainForm.SetupForm.udCWFall.Value, TONE_SIZE, (float)Audio.SampleRate);
runKeyer = true;
Keyer = new Thread(new ThreadStart(KeyerThread));
Keyer.Name = "CW KeyThread";
Keyer.Priority = ThreadPriority.Highest;
Keyer.IsBackground = true;
Keyer.Start();
watchdog = new Thread(new ThreadStart(WatchdogThread));
watchdog.Name = "CW KeyThread watchdog";
watchdog.Priority = ThreadPriority.Highest;
watchdog.IsBackground = true;
watchdog.Start();
sound_thread = new Thread(new ThreadStart(SoundThread));
sound_thread.Name = "CW Encode Thread";
sound_thread.Priority = ThreadPriority.Normal;
sound_thread.IsBackground = true;
sound_thread.Start();
return true;
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
return false;
}
}
#endregion
#region misc function
public void Exchange_samples(ref ComplexF[] output, ref ComplexF[] monitor, int buflen)
{
if (MainForm.output_ring_buf.ReadSpace() < buflen)
{
while (MainForm.output_ring_buf.ReadSpace() < buflen)
{
if (run_transmiter)
{
audio_event.Set();
Thread.Sleep(1);
}
else
return;
}
}
EnterCriticalSection(cs_audio);
MainForm.output_ring_buf.Read(ref iq_buffer, buflen);
correctIQ(ref iq_buffer);
MainForm.mon_ring_buf.Read(ref mon_iq_buffer, buflen);
Array.Copy(iq_buffer, output, buflen);
Array.Copy(mon_iq_buffer, monitor, buflen);
LeaveCriticalSection(cs_audio);
}
public bool Start(string text)
{
try
{
if (run_transmiter)
{
run_transmiter = false;
Stop();
return true;
}
MainForm.Invoke(new CrossThreadCommand(MainForm.CommandCallback), "Message end", 0, "");
text = text.Replace("\n", " ");
text = text.Replace("\r", " ");
text = text.ToUpper();
MainForm.genesis.WriteToDevice(18, (long)Keyer_mode.CWX);
TONE_SIZE = 48 * TONE_DELAY * Audio.SampleRate / 48000;
time_base = 1200;
wpm = cw_speed;
ctime = time_base / wpm; // dit time
dottime = ctime / TONE_DELAY;
dashtime = dottime * 3;
Transmiter.osc.gen.Frequency = 0.0;
Transmiter.cwt.osc.phase = 0.0;
Transmiter.cwt.mon_osc.phase = 0.0;
Transmiter.cwt.sr = Audio.SampleRate;
Transmiter.cwt.osc.gen.Frequency = (2.0 * M_PI * tx_if_shift) / Transmiter.cwt.sr;
Transmiter.cwt.mon_osc.gen.Frequency = (2.0 * M_PI * mon_frequency) / Transmiter.cwt.sr;
MainForm.output_ring_buf.Restart();
MainForm.mon_ring_buf.Restart();
play_sound = false;
message = text;
run_transmiter = true;
audio_callback_event.Reset();
if (CWEncode_thread != null && CWEncode_thread.IsAlive)
{
audio_event.Set();
run_transmiter = false;
}
if (CWEncode_thread == null || !CWEncode_thread.IsAlive)
{
CWEncode_thread = new Thread(new ThreadStart(Play_message_thread));
CWEncode_thread.Name = "CW Encode Thread";
CWEncode_thread.Priority = ThreadPriority.Normal;
CWEncode_thread.IsBackground = true;
CWEncode_thread.Start();
}
else
CWEncode_thread.Abort();
return true;
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
run_transmiter = false;
return false;
}
}
public bool Stop()
{
try
{
audio_event.Set();
message_playing = false;
Transmiter.cwt.stage = CWToneState.CWTone_FALL;
run_transmiter = false;
audio_callback_event.Set();
end_event.Set();
play_event.Set();
MainForm.output_ring_buf.Reset();
MainForm.mon_ring_buf.Reset();
MainForm.MOX = false;
System.GC.Collect();
return true;
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
return false;
}
}
private string DotDash(string Text)
{
if (Text == null || Text == "")
{
Debug.Write("Empty message! \n");
return string.Empty;
}
Dictionary<char, string> dict = (true ? Morse : Morse);
string buf = "";
bool inProsign = false;
char b = ' ';
foreach (char c in Text.ToUpper())
{
if (c == '\\') // Prosign delimiter
{
b = '%';
}
else
b = c;
if (dict.ContainsKey(b))
{
buf += dict[b];
}
else
{
buf += "[" + b + "]"; // Print unknown character
}
if (b != ' ' && !inProsign && b != '#' && b != '*') // # last for repeat
buf += " ";
}
return buf;
}
private string DotDashChar(char c)
{
try
{
Dictionary<char, string> dict = (true ? Morse : Morse);
string buf = "";
bool inProsign = false;
char b = ' ';
if (c == '\\')
{
b = '%';
}
else
b = c;
if (dict.ContainsKey(b))
{
buf += dict[b];
}
else
{
buf += "[" + b + "]";
}
if (b != ' ' && !inProsign && b != '#' && b != '*')
buf += " ";
return buf;
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
return "";
}
}
#endregion
#region Play sound
private void Play_message_thread()
{
try
{
char last = 'x';
string msg = "";
message_playing = true;
MainForm.MOX = true;
while (run_transmiter)
{
if (tun)
{
while (tun && MainForm.MOX)
{
if (run_transmiter)
{
Dash();
while (MainForm.output_ring_buf.ReadSpace() >= 2048)
audio_event.WaitOne(50);
}
else
return;
}
run_transmiter = false;
MainForm.MOX = false;
message_playing = false;
play_event.Set();
MainForm.output_ring_buf.Reset();
MainForm.mon_ring_buf.Reset();
return;
}
char[] m = message.ToCharArray();
foreach (char c in m)
{
if (run_transmiter)
{
msg = DotDashChar(c);
SemiBreak = 0; // reset watchdog
foreach (char t in msg)
{
if (last == '.' || last == '-')
Space();
if (t == '.')
{
Dot();
}
else if (t == '-' || t == '*')
{
Dash();
}
else if (t == ' ')
Pause();
last = t;
if (MainForm.output_ring_buf.ReadSpace() >= 2048)
{
while ((MainForm.output_ring_buf.ReadSpace() >= 2048))
{
if (run_transmiter)
{
SemiBreak = 0; // reset watchdog
audio_event.WaitOne(5);
}
else
goto end;
}
}
}
MainForm.Invoke(new CrossThreadSetText(MainForm.CommandCallback),
"Set TX text", 0, c.ToString());
if (MainForm.keyboard.Visible)
MainForm.Invoke(new CrossThreadSetText(MainForm.keyboard.CommandCallback),
"Set Keyboard text", 0, "");
}
else
{
last = ' ';
if (MainForm.keyboard.Visible)
MainForm.Invoke(new CrossThreadSetText(MainForm.keyboard.CommandCallback),
"Reload Keyboard text", 0, "");
}
}
end:
run_transmiter = false;
if (last == '#')
{
MainForm.Invoke(new CrossThreadCommand(MainForm.CommandCallback), "Message repeat", 0, "");
if (MainForm.keyboard.Visible)
MainForm.Invoke(new CrossThreadSetText(MainForm.keyboard.CommandCallback),
"Reload Keyboard text", 0, message);
run_transmiter = false;
MainForm.output_ring_buf.Reset();
MainForm.mon_ring_buf.Reset();
}
else
{
MainForm.Invoke(new CrossThreadCommand(MainForm.CommandCallback), "Message end", 0, "");
if (MainForm.keyboard.Visible)
MainForm.Invoke(new CrossThreadSetText(MainForm.keyboard.CommandCallback),
"Set Keyboard text", 0, "");
}
}
MainForm.MOX = false;
message_playing = false;
play_event.Set();
run_transmiter = false;
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
}
}
private void Dot()
{
if (!play_sound)
{
CWToneOn();
play_sound = true;
}
for (int i = 0; i < dottime; i++)
{
if (run_transmiter)
{
play_event.Set();
audio_callback_event.WaitOne();
}
}
}
private void Dash()
{
if (!play_sound)
{
CWToneOn();
play_sound = true;
}
for (int i = 0; i < dashtime; i++)
{
if (run_transmiter)
{
play_event.Set();
audio_callback_event.WaitOne();
}
}
}
private void Space()
{
if (play_sound)
{
Transmiter.cwt.stage = CWToneState.CWTone_FALL;
}
for (int i = 0; i < dottime; i++)
{
if (run_transmiter)
{
play_event.Set();
audio_callback_event.WaitOne();
}
}
}
private void Pause()
{
if (play_sound)
{
Transmiter.cwt.stage = CWToneState.CWTone_FALL;
}
for (int i = 0; i < dottime * 2; i++)
{
if (run_transmiter)
{
play_event.Set();
audio_callback_event.WaitOne();
}
}
}
private void play_tone()
{
EnterCriticalSection(cs_audio);
MainForm.output_ring_buf.Write(Transmiter.cwt.osc.gen.signalpoints, (TONE_SIZE));
MainForm.mon_ring_buf.Write(Transmiter.cwt.mon_osc.gen.signalpoints, (TONE_SIZE));
LeaveCriticalSection(cs_audio);
}
private void play_silence()
{
EnterCriticalSection(cs_audio);
MainForm.output_ring_buf.Write(zero_buffer, TONE_SIZE);
MainForm.mon_ring_buf.Write(zero_buffer, TONE_SIZE);
LeaveCriticalSection(cs_audio);
}
private void SoundThread()
{
try
{
while (true)
{
play_event.WaitOne();
if (play_sound)
{
//MainForm.genesis.WriteToDevice(24, 1);
play_sound = CWTone();
EnterCriticalSection(cs_audio);
MainForm.output_ring_buf.Write(Transmiter.cwt.osc.gen.signalpoints, TONE_SIZE);
MainForm.mon_ring_buf.Write(Transmiter.cwt.mon_osc.gen.signalpoints, TONE_SIZE);
LeaveCriticalSection(cs_audio);
}
else
{
//MainForm.genesis.WriteToDevice(24, 0);
EnterCriticalSection(cs_audio);
MainForm.output_ring_buf.Write(zero_buffer, TONE_SIZE);
MainForm.mon_ring_buf.Write(zero_buffer, TONE_SIZE);
LeaveCriticalSection(cs_audio);
}
audio_callback_event.Set();
}
}
catch (Exception ex)
{
Debug.Write(ex.ToString());
}
}
private void correctIQ(ref ComplexF[] buffer)
{
int i;
for (i = 0; i < 2048; i++)
{
buffer[i].Im += tx_phase * buffer[i].Re;
buffer[i].Re *= tx_gain;
}
}
#endregion
#region CW generation
private bool newCWToneGen(ref oscillator osc,
float gain, // dB
float freq, // ms
float rise, // ms
float fall, // ms
int size, // samples
float samplerate) // samples/sec
{
setCWToneGenVals(ref Transmiter.cwt, gain, freq, rise, fall);
Transmiter.cwt.size = size;
Transmiter.cwt.sr = samplerate;
newOSC(ref osc, Transmiter.cwt.size, OscType.ComplexTone,
(double)freq, 0.0, Transmiter.cwt.sr);
return true;
}
private void setCWToneGenVals(ref CWToneGen cwt, float gain, float freq, float rise, float fall)
{
cwt.gain = gain;
cwt.osc.freq = freq;
cwt.rise.dur = rise;