-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtncpanel.cpp
More file actions
1569 lines (1420 loc) · 56.8 KB
/
Copy pathtncpanel.cpp
File metadata and controls
1569 lines (1420 loc) · 56.8 KB
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
#include <QtCore/QDebug>
#include <QtCore/qmath.h>
#include <QtCore/QDir>
#include <QtCore/QSettings>
#include <QtCore/QBuffer>
#include <QtCore/QDateTime>
#include <QtCore/QThread>
#include <QtCore/QSemaphore>
#include <QtGui/QBitmap>
#include <QtGui/QPainter>
#include <QtWidgets/QGraphicsWidget>
#include <QtCore/QMessageLogContext>
#include "utils.hpp"
#include "vumeterwidget.hpp"
#include "tncsetup.hpp"
#include "modem.hpp"
#include "tncpanel.hpp"
enum DispType
{
Waterfall,
Spectrum,
Disabled
};
enum Mode
{
Narrow,
Wide
};
class TNCPanel::MainPrivate
{
public:
// Booleans
bool blnFormInitialized;
bool blnSCCapturing;
bool blnStartCodec;
bool blnGraphicsCleared;
bool blnSoundStreamPlay;
bool blnLatencyCalculated;
bool blnPullMode;
bool blnInitialized;
bool bullAudPullMode;
quint8 intChannels;
quint32 length;
quint32 slices;
quint32 intRxsmplrate;
quint32 intTxsmplrate;
// Integers
quint16 intRxVolume;
quint16 intTxDrive;
quint32 intRepeatCnt;
quint32 intCaptureBufferSize;
quint32 intPTTOnCount;
quint32 intPTTOffCount;
qint32 intNextCaptureOffset;
qint32 intBMPSpectrumWidth;
qint32 intBMPSpectrumHeight;
qint32 intNotifySize; //!< 2048 bytes or 1024 16 bit samples (nominally every 85.3333 ms @ 12000 sample rate) May be possible to go down to 1024 byte buffer which reduces the latency by 43 ms
qint32 intMeasuredTRLatency;
qint32 intAverageTRLatency;
qint32 intLatencyCalcs;
QVarLengthArray<qint32> intSamples;
// Doubles
double dblPhase;
double dblPTTOnLevelSum;
double dblPTTOffLevelSum;
double maxdb;
double range;
double avg;
QVarLengthArray<double> dblPhaseInc;
QVarLengthArray<double> dblCPPhaseOffset;
QVarLengthArray<double> dblCarFreq;
// Bytes
QByteArray bytLastACKedDataFrameType;
QByteArray bytToSend;
QByteArray bytSymToSend;
QByteArray bytHostIBData_CmdBuffer;
QByteArray mywindowstate;
QByteArray mywindowgeometry;
// Strings
QString strTCPIPConnectionID;
QString strFault;
QString strCaptureDev;
QString strPlayDev;
QString appver;
// Dates
QDateTime dttPTTApply;
QDateTime dttPTTRelease;
QDateTime dttNextPlay;
QDateTime dttLastSoundCardSample;
QDateTime dttCodecStarted;
QDateTime dttNextGraphicsReset;
QList<QAudioDeviceInfo> cllCaptureDevices; //!< CaptureDevicesCollection
QList<QAudioDeviceInfo> cllPlaybackDevices; //!< PlaybackDevicesCollection
QAudioDeviceInfo devSelectedPlaybackDevice; // Device Info Selected
QSemaphore stcPositionNotify;
DispType m_disptype;
Mode m_mode;
QTimer *tmrPoll;
QTimer *tmrStartCODEC;
QBuffer *objPlayback; // for .wav playback
};
TNCPanel::TNCPanel() : QDialog()
{
// put a note in the log
qInfo() << "TNC panel initializing";
// initialize the global control structures
// initialize local vars
mwd = new MainPrivate; // create local var definitions
mwd->blnInitialized = false;
mcb = new ModemControlBlock;
initializeLocalVars(); // must come first to init all local vars
// create GUI
ui = new Ui::TNCPanel;
ui->setupUi(this);
menubar = new QMenuBar(this);
fileMenu = menubar->addMenu(tr("&File"));
setupMenu = menubar->addMenu(tr("&Setup"));
helpMenu = menubar->addMenu(tr("&Help"));
fileMenu->addSeparator();
actionFileExit = new QAction(tr("E&xit"), this);
actionSetupTNC = new QAction(tr("&TNC"), this);
actionSetupRadio = new QAction(tr("&Radio"), this);
actionHelpIndex = new QAction(tr("I&ndex"), this);
setupMenu->addAction(actionSetupRadio);
setupMenu->addAction(actionSetupTNC);
helpMenu->addAction(actionHelpIndex);
fileMenu->addAction(actionFileExit);
connect(actionFileExit, SIGNAL(triggered(bool)),this,SLOT(slotClose()));
connect(actionSetupTNC, SIGNAL(triggered(bool)),this,SLOT(slotSetupTNC(bool)));
// connect(actionSetupRadio,SIGNAL(triggered(bool)),this,SLOT(slotSetupRadio(bool)));
connect(ui->ctrlVol,SIGNAL(sliderMoved(int)),this,SLOT(slotVolChgd(int)));
ui->vumeter->setMinimum(0);
ui->vumeter->setMaximum(100);
// fill device lists
qInfo() << "finding available audio devices";
mwd->cllCaptureDevices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
mwd->cllPlaybackDevices = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
// change display to setup defaults
QString title = tr("ARDOP Virtual TNC v");
title += QApplication::applicationVersion().trimmed();
this->setWindowTitle(title);
ui->retranslateUi(this);
}
TNCPanel::~TNCPanel()
{
// do an orderly shutdown to avoid memory leaks
// delete mwd->audioInput;
// delete audioIOObject;
// delete mwd->audioIO;
delete ui;
delete mwd;
}
// ------------------------------------------------
// GUI EVENT HANDLERS
// ------------------------------------------------
void TNCPanel::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type())
{
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void TNCPanel::closeEvent(QCloseEvent *)
{
// called before deconstructor
try
{
if ( objTNCSetup ) objTNCSetup = 0;
}
catch (...)
{
// don't do anything
}
mwd->mywindowgeometry = saveGeometry();
writeSettings();
}
void TNCPanel::showEvent(QShowEvent *event)
{
// after load and before display
// or after repaint
Q_UNUSED(event) // to avoid warnings
QDir dir;
QString path = "../Logs";
path = QDir::fromNativeSeparators(path);
if ( !mwd->blnFormInitialized )
{
#ifdef DEBUG_TNC
qDebug() << "Continuing Form Init";
qDebug() << "Be sure Logs and Wav folders exist - if not create them";
#endif
if ( !dir.exists(path) )
{
dir.mkdir(path);
}
path = "../Wav";
if ( !dir.exists(path) )
{
dir.mkdir(path);
}
strWavDirectory = path + "/";
strWavDirectory = QDir::fromNativeSeparators(strWavDirectory);
#ifdef DEBUG_TNC
qDebug() << "Folders Verified...continuing by setting milestone timestamp";
#endif
mwd->dttLastSoundCardSample = QDateTime::currentDateTime();
QString strFault = "";
mwd->dttNextGraphicsReset = QDateTime::currentDateTime().addSecs(3600);
#ifdef DEBUG_TNC
qDebug() << "Checking for form init";
#endif
strFault = "";
if ( !mwd->blnInitialized )
{
createLocalObjects();
readSettings();
initializeFromIni();
initializeAudio();
mwd->blnInitialized = true;
}
}
}
// ------------------------------------------------
// PUBLIC SLOT HANDLERS
// ------------------------------------------------
// ------------------------------------------------
// PUBLIC METHODS
// ------------------------------------------------
/*
bool TNCPanel::KeyPTT(bool blnPTT)
{
// Returns TRUE if successful False otherwise
// if ( blnLastPTT && !blnPTT ) objProtocol->dttStartRTMeasure = QDateTime::currentDateTime(); // start a measurement on release of PTT.
// the following is used in T>R Latency measurements
// If PTT goes Active clear the counts and
// sums for PTT Latency Measurement.
if ( blnPTT )
{
mwd->dttPTTApply = QDateTime::currentDateTime();
ui->ledBZY->setPixmap(QIcon(":image/redled.png"));
}
else
{
mwd->dttPTTRelease = QDateTime::currentDateTime();
mwd->blnLatencyCalculated = false;
ui->ledBZY->setPixmap(QIcon(":image/greyled.png"));
}
if ( !RCB->RadioControl )
objHI->SendCommandToHost("PTT " + QString("%1").arg(blnPTT).toUpper());
else if ( objRadio )
objRadio->PTT(blnPTT);
if ( mcb->DebugLog ) qDebug() << "[Main.KeyPTT] " << blnPTT;
blnLastPTT = blnPTT;
return true;
} // KeyPTT()
*/
void TNCPanel::setRadioMenuEnabled(bool b)
{
actionSetupRadio->setEnabled(b);
}
/* To play a .wav file
bool TNCPanel::PlaySoundStream()
{
// Plays the .wav stream with the selected Playback device
// Returns True if no errors False otherwise...
// Updated 0.5.0.5 July 21, 2016 to add Synclock to memwavestream
// Todo: replace next line with RxBuffer.
// static bufPlaybackFlags As New Microsoft.DirectX.DirectSound.BufferDescription
bool blnExit = false;
qint32 intTrace = 0;
QDateTime dttStartPlay;
Status stcStatus = NULL;
if ( !blnCodecStarted ) return false;
// SyncLock memWaveStreamLock
// memWaveStream Class
//
// A stream is an abstraction of a sequence of bytes.
// This sequence of bytes may come from a file, a
// TCP/IP socket, or in this case, memory. A stream
// is represented, aptly, by the Stream class.
// The Stream class provides a generic view of a
// sequence of bytes. A Stream class forms the
// abstract class of all other streams. That is,
// you cannot directly create an instance of the
// Stream class. Instead, the Stream class is
// implemented by the following classes:
//
//
// BufferedStream: provides a buffering layer on
// another stream to improve performance.
// FileStream: provides a way to read and write files.
// MemoryStream: provides a stream using memory
// as the backing store.
// NetworkStream: provides a way to access data on the network.
// CryptoStream: provides a way to supply data
// for cryptographic transformation.
// The Qt Framework handles data streams to I/O devices (even
// memory) by using the generic QIODevice class.
// This class will provide functionality to data streams in a
// generic way regardless of the actual device.
if ( !mwd->memWaveStream )
{
qCritical("[Main.PlaySoundStream] memWaveStream is nothing");
blnExit = true;
}
else if ( m_soundIsPlaying )
{
qCritical("[Main.PlaySoundStream] Sound is already playing, Call AbortWaveStream");
AbortSoundStream();
}
// End SyncLock
if ( blnExit ) return false;
intTrace = 1;
try
{
// We are using AudioIO for input and output instead of
// platform defined devices. The MCB contains the name
// of the current device names.
// devSelectedPlaybackDevice.SetCooperativeLevel(Me.Handle, CooperativeLevel.Priority)
KeyPTT(true); // Activate PTT before starting sound play
if ( !bufPlaybackFlags.ControlVolume )
{
// The following flags required to allow
// playing when not in focus and
// to allow adjusting the volume...
intTrace = 2;
// bufPlaybackFlags.Flags = CType(BufferDescriptionFlags.GlobalFocus + BufferDescriptionFlags.ControlVolume, BufferDescriptionFlags)
}
intTrace = 3;
}
catch (exception ex)
{
qCritical("[Main.PlaySoundStream] Trace =" + QString("%1").arg(intTrace) + " Kill PTT exception: " + ex.what());
KeyPTT(false);
mwd->blnSoundStreamPlay = false;
return false;
}
// SyncLock memWaveStreamLock
try
{
memWaveStream.Seek(0, SeekOrigin.Begin) ' reset the pointer to the origin
intTrace = 4
If Not IsNothing(objPlayback) Then
objPlayback.Dispose()
objPlayback = Nothing
End If
objPlayback = New SecondaryBuffer(memWaveStream, bufPlaybackFlags, devSelectedPlaybackDevice)
intTrace = 5
objPlayback.Volume = Math.Min(-5000 + 50 * MCB.DriveLevel, 0) ' -5000=off, 0=full volume
intTrace = 6
objPlayback.Play(0, BufferPlayFlags.Default)
intTrace = 7
dttTestStart = Now
dttStartPlay = Now
intTrace = 8
If MCB.DebugLog Then Logs.WriteDebug("[PlaySoundStream] " & strLastWavStream)
ui->lblXmtFrame->setText(strLastWavStream);
ui->lblXmtFrame->setStyleSheet("QLabel {background-color: ColorLightSalmon;}");
queTNCStatus.enqueue(stcStatus);
intTrace = 9;
if ( mcb->DebugLog ) qDebug("[Main.PlaySoundStream] set blnSoundStreamPlay to True");
mwd->blnSoundStreamPlay = true;
blnExit = false;
}
catch (exception ex)
{
qCritical("[Main.PlaySoundStream] Trace =" + QString("%1").arg(intTrace) + " Kill PTT exception: " + ex.what());
blnExit = true;
KeyPTT(false);
mwd->blnSoundStreamPlay = false;
}
//End SyncLock
return !blnExit;
} // PlaySoundStream()
*/
/*
void TNCPanel::SendTestFrame(QVarLengthArray<qint32> intFilteredSamples, QString strFileStream, qint32 intRepeats)
{
strLastWavStream = strFileStream;
ClearTuningStats();
ClearQualityStats();
objMod->CreateWaveStream(intFilteredSamples);
RxState = SearchingForLeader;
if ( !m_soundIsPlaying )
{
PlaySoundStream();
dblMaxLeaderSN = 0;
mwd->intRepeatCnt = intRepeats;
}
} // SendTestFrame()
*/
/*
void TNCPanel::SendFrame(QVarLengthArray<qint32> intFilteredSamples, QString strLastFileStream, qint32 intDelayMs = 0)
{
QDateTime dttStartWait = QDateTime::currentDateTime();
if ( m_soundIsPlaying )
{
bool tim = QDateTime::currentDateTime().addMSecs(-dttStartWait)/1000 < 10;
while ( m_soundIsPlaying && tim) // wait 10 sec max
{
QThread::sleep(20);
}
}
mwd->dttNextPlay = QDateTime::currentDateTime().addMSecs(intDelayMs);
objMod->CreateWaveStream(intFilteredSamples);
if ( mcb->DebugLog ) qDebug("[Main.SendFrame] Send " + strLastFileStream + " delayed " + QString("%1").arg(intDelayMs) + " ms");
blnFramePending = true;
} // SendFrame()
*/
/*
void TNCPanel::SendID(bool blnEnableCWID)
{
QString strFilename = "";
QByteArray bytEncodedBytes;
QByteArray bytIDSent;
if ( mcb->GridSquare.isEmpty() )
{
bytEncodedBytes = objMod->Encode4FSKIDFrame(mcb->Callsign, "NO-GS", strFilename);
bytIDSent = GetBytes(" " + mcb->Callsign + ":[NO-GS] ");
}
else
{
bytEncodedBytes = objMod->Encode4FSKIDFrame(mcb->Callsign, mcb->GridSquare, strFilename);
bytIDSent = GetBytes(" " & mcb->Callsign + ":[" + mcb->GridSquare + "] ");
}
objProtocol->AddTagToDataAndSendToHost(bytIDSent, "IDF");
mwd->intSamples = objMod->Mod4FSKData(48, bytEncodedBytes);
if ( mcb->CWID )
{
mwd->intSamples.reserve(mwd->intSamples.length() + 4800);
QVarLengthArray<qint32> intCWID;
qint32 intPtr = mwd->intSamples.length();
objMod->CWID("DE " + mcb->Callsign, intCWID, false);
mwd->intSamples.reserve(mwd->intSamples.length() + intCWID.length());
ArrayCopy(intCWID, 0, mwd->intSamples, intPtr, intCWID.length());
strLastWavStream += " + CWID";
}
if ( m_soundIsPlaying )
{
objMod->CreateWaveStream(mwd->intSamples);
PlaySoundStream();
}
} // SendID()
*/
/*
void TNCPanel::Update4FSKConstellation(QVarLengthArray<qint32> *intToneMags, qint32 *intQuality)
{
Q_UNUSED(intToneMags)
Q_UNUSED(intQuality)
// Subroutine to update bmpConstellation plot for 4FSK modes...
if ( intToneMags->size() < 40 ) return;
try
{
qint32 intToneSum = 0;
qint32 intMagMax = 0;
qint32 yCenter = 0;
qint32 xCenter = 0;
qint32 intRad = 0;
quint32 x = 0;
qint32 y = 0;
double dblRad = 0.0;
double dblPi4 = 0.25 * M_PI;
double dblDistanceSum = 0.0;
double dblPlotRotation = 0.0;
Status stcStatus;
QColor clrPixel;
QImage *bmpConstellation = new QImage(QSize(89, 89),QImage::Format_ARGB32);
// Draw the axis and paint the black background area
yCenter = int((bmpConstellation.height() - 2) / 2);
xCenter = int((bmpConstellation.width() - 2) / 2);
for ( x = 0; x <= (bmpConstellation.width() - 1); ++i )
{
for ( y = 0; y <= (bmpConstellation.height() - 1); ++y )
{
if ( (y == yCenter) || (x == xCenter) ) bmpConstellation->setPixel(QPoint(x, y), ColorDeepSkyBlue);
}
}
for ( i = 0; i <= (intToneMags->size() - 1); i += 4 ) // for the number of symbols represented by intToneMags
{
intToneSum = intToneMags(i) + intToneMags(i + 1) + intToneMags(i + 2) + intToneMags(i + 3);
if ( (intToneMags(i) > intToneMags(i + 1)) &&
(intToneMags(i) > intToneMags(i + 2)) &&
(intToneMags(i) > intToneMags(i + 3))
)
{
if ( intToneSum > 0 )
{
qint32 xx = 42 - (intToneMags(i + 1) +
intToneMags(i + 2) +
intToneMags(i + 3)) /
(intToneSum / 80);
intRad = qMax(5,xx);
if (intRad < 15 )
clrPixel = ColorTomato;
else if ( intRad < 30 )
clrPixel = ColorGold;
else
clrPixel = ColorLime;
bmpConstellation->setPixel(xCenter + intRad, yCenter + 1, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter + intRad, yCenter - 1, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter + intRad, yCenter + 2, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter + intRad, yCenter - 2, clrPixel); // don't plot on top of axis
}
else if ( (intToneMags(i + 1) > intToneMags(i)) &&
(intToneMags(i + 1) > intToneMags(i + 2)) &&
(intToneMags(i + 1) > intToneMags(i + 3))
)
{
if ( intToneSum > 0 )
{
quint32 xx = 42 - (intToneMags(i) +
intToneMags(i + 2) +
intToneMags(i + 3)) /
(intToneSum / 80);
intRad = qMax(5, xx);
if ( intRad < 15 )
clrPixel = ColorTomato;
else if ( intRad < 30 )
clrPixel = ColorGold;
else
clrPixel = ColorLime;
bmpConstellation->setPixel(xCenter + 1, yCenter + intRad, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter - 1, yCenter + intRad, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter + 2, yCenter + intRad, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter - 2, yCenter + intRad, clrPixel); // don't plot on top of axis
}
}
else if ( (intToneMags(i + 2) > intToneMags(i)) &&
(intToneMags(i + 2) > intToneMags(i + 1)) &&
(intToneMags(i + 2) > intToneMags(i + 3))
)
{
if ( intToneSum > 0 )
{
qint32 xx = 42 - (intToneMags(i + 1) +
intToneMags(i) +
intToneMags(i + 3)) /
(intToneSum / 80);
intRad = qMax(5,xx);
if ( intRad < 15 )
clrPixel = ColorTomato;
else if ( intRad < 30 )
clrPixel = ColorGold;
else
clrPixel = ColorLime;
bmpConstellation->setPixel(xCenter - intRad, yCenter + 1, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter - intRad, yCenter - 1, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter - intRad, yCenter + 2, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter - intRad, yCenter - 2, clrPixel); // don't plot on top of axis
}
}
else
{
if ( intToneSum > 0 )
{
try
{
qint32 xx = 42 - (intToneMags(i + 1) +
intToneMags(i + 2) +
intToneMags(i)) /
(intToneSum / 80);
intRad = qMax(5,xx);
}
catch (...)
{
intRad = 0;
}
if ( intRad < 15 )
clrPixel = ColorTomato;
else if ( intRad < 30 )
clrPixel = ColorGold;
else
clrPixel = ColorLime;
bmpConstellation->setPixel(xCenter + 1, yCenter - intRad, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter - 1, yCenter - intRad, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter + 2, yCenter - intRad, clrPixel); // don't plot on top of axis
bmpConstellation->setPixel(xCenter - 2, yCenter - intRad, clrPixel); // don't plot on top of axis
}
}
dblDistanceSum += (43 - intRad);
}
qint32 xx = int(100 - 2.4 * (dblDistanceSum / (intToneMags->size() / 4)));
intQuality = qMax(0,xx); // factor 2.4 emperically chosen for calibration (Qual range 25 to 100)
stcStatus.ControlName = "lblQuality";
QString msg = "4FSK Quality: ";
msg += QString("%1").arg(intQuality);
stcStatus.Text = msg;
queTNCStatus.enqueue(stcStatus);
if ( mcb->AccumulateStats )
{
++stcQualityStats.int4FSKQualityCnts;
stcQualityStats.int4FSKQuality += intQuality;
}
stcStatus.ControlName = "ConstellationPlot";
queTNCStatus.enqueue(stcStatus);
}
catch (...)
{
QString msg = "[Main.Update4FSKConstellation] Err: ";
msg += QString("%1").arg(ex);
qFatal << msg;
}
} // Update4FSKConstellation()
*/
/*
void TNCPanel::UpdatePhaseConstellation(QVarLengthArray<qint16> *intPhases, QVarLengthArray<qint16> *intMag, QString strMod, qint32 *intQuality, bool blnQAM = false)
{
// Subroutine to update bmpConstellation plot for PSK modes...
// Skip plotting and calulations of intPSKPhase(0) as this
// is a reference phase (9/30/2014)
try
Dim intPSKPhase As Integer = CInt(strMod.Substring(0, 1))
Dim dblPhaseError As Double
Dim dblPhaseErrorSum As Double
Dim intPSKIndex As Int32
Dim intX, intY, intP As Int32
Dim intRadInner, intRadOuter As Int32
Dim dblRad As Double = 0
Dim dblAvgRad As Double = 0
Dim dblAvgRadOuter, dblAvgRadInner As Double
Dim intMagMax As Double = 0
Dim dblPi4 As Double = 0.25 * Math.PI
Dim dbPhaseStep As Double = 2 * Math.PI / intPSKPhase
Dim dblRadErrorInner, dblRadErrorOuter As Double
Dim dblPlotRotation As Double = 0
Dim stcStatus As Status
Dim yCenter, xCenter As Integer
Select Case intPSKPhase
Case 4 : intPSKIndex = 0
Case 8 : intPSKIndex = 1
End Select
bmpConstellation = New Bitmap(89, 89)
' Draw the axis and paint the black background area
yCenter = (bmpConstellation.Height - 1) \ 2
xCenter = (bmpConstellation.Width - 1) \ 2
For x As Integer = 0 To bmpConstellation.Width - 1
For y As Integer = 0 To bmpConstellation.Height - 1
If y = yCenter Or x = xCenter Then
bmpConstellation.SetPixel(x, y, Color.Tomato)
End If
Next y
Next x
If blnQAM Then
intPSKPhase = 8
intPSKIndex = 1
dbPhaseStep = 2 * Math.PI / intPSKPhase
For j As Integer = 1 To intMag.Length - 1 ' skip the magnitude of the reference in calculation
intMagMax = Math.Max(intMagMax, intMag(j)) ' find the max magnitude to auto scale
Next j
For k As Integer = 1 To intMag.Length - 1
If intMag(k) < 0.75 * intMagMax Then
dblAvgRadInner += intMag(k) : intRadInner += 1
Else
dblAvgRadOuter += intMag(k) : intRadOuter += 1
End If
Next k
dblAvgRadInner = dblAvgRadInner / intRadInner
dblAvgRadOuter = dblAvgRadOuter / intRadOuter
Else
For j As Integer = 1 To intMag.Length - 1 ' skip the magnitude of the reference in calculation
intMagMax = Math.Max(intMagMax, intMag(j)) ' find the max magnitude to auto scale
dblAvgRad += intMag(j)
Next j
End If
dblAvgRad = dblAvgRad / (intMag.Length - 1) ' the average radius
' For i As Integer = 0 To intPhases.Length - 1
For i As Integer = 1 To intPhases.Length - 1 ' Don't plot the first phase (reference)
dblRad = 40 * intMag(i) / intMagMax ' scale the radius dblRad based on intMagMax
intX = CInt(xCenter + dblRad * Math.Cos(dblPlotRotation + intPhases(i) / 1000))
intY = CInt(yCenter + dblRad * Math.Sin(dblPlotRotation + intPhases(i) / 1000))
intP = CInt(Math.Round(0.001 * intPhases(i) / dbPhaseStep))
' compute the Phase and Raduius errors
If intMag(i) > (dblAvgRadInner + dblAvgRadOuter) / 2 Then
dblRadErrorOuter += Abs(dblAvgRadOuter - intMag(i))
Else
dblRadErrorInner += Abs(dblAvgRadInner - intMag(i))
End If
dblPhaseError = Abs(((0.001 * intPhases(i)) - intP * dbPhaseStep)) ' always positive and < .5 * dblPhaseStep
dblPhaseErrorSum += dblPhaseError
If intX <> xCenter And intY <> yCenter Then bmpConstellation.SetPixel(intX, intY, Color.Yellow) ' don't plot on top of axis
Next i
If blnQAM Then
' include Radius error for QAM ...Lifted from WINMOR....may need work
intQuality = CInt(Math.Max(0, (1 - (dblRadErrorInner / (intRadInner * dblAvgRadInner) + dblRadErrorOuter / (intRadOuter * dblAvgRadOuter))) * (100 - 200 * (dblPhaseErrorSum / (intPhases.Length - 1)) / dbPhaseStep)))
If mcb->AccumulateStats Then
stcQualityStats.intQAMQualityCnts += 1
stcQualityStats.intQAMQuality += intQuality
stcQualityStats.intQAMSymbolsDecoded += intPhases.Length
End If
Else
' This gives good quality with probable seccessful decoding threshold around quality value of 60 to 70
intQuality = Math.Max(0, CInt((100 - 200 * (dblPhaseErrorSum / (intPhases.Length - 1)) / dbPhaseStep))) ' ignore radius error for (PSK) but include for QAM
If mcb->AccumulateStats Then
stcQualityStats.intPSKQualityCnts(intPSKIndex) += 1
stcQualityStats.intPSKQuality(intPSKIndex) += intQuality
stcQualityStats.intPSKSymbolsDecoded += intPhases.Length
End If
End If
stcStatus.ControlName = "lblQuality"
stcStatus.Text = strMod & " Quality: " & intQuality.ToString
queTNCStatus.Enqueue(stcStatus)
stcStatus.ControlName = "ConstellationPlot"
queTNCStatus.Enqueue(stcStatus)
Catch ex As Exception
Logs.Exception("[Main.UpdatePhaseConstellation] Err: " & ex.ToString)
End Try
} // UpdatePhaseConstellation()
*/
// ------------------------------------------------
// END PUBLIC FUNCTIONS
// ------------------------------------------------
// ------------------------------------------------
// PRIVATE SLOTS
// ------------------------------------------------
void TNCPanel::slotAudioInputOK() { ui->ledSND->setEnabled(true); }
void TNCPanel::slotVolChgd(int v)
{
// QAudioInput needs a real value
// so we convert the int value from the
// panel volume control to a local real value
qreal m_vol = 0.0;
m_vol = v / 100;
if ( v > 1.0 ) m_vol = 1.0;
audioInput->setVolume(m_vol);
}
void TNCPanel::slotReadMore()
{
if ( !audioInput ) return;
qint64 len = audioInput->bytesReady();
if ( len > BufferSize ) len = BufferSize;
qint64 l = m_input->read(m_buffer.data(), len);
if ( l > 0 ) audioIOObject->write(m_buffer.constData(), l);
}
void TNCPanel::slotUpdateVU()
{
quint16 v = 0;
v = audioIOObject->level() * 100;
#ifdef DEBUG_AUDIO
// qDebug() << "Lvl: " << QString("%1").arg(v);
#endif
if ( v > 100 ) v = 100;
ui->vumeter->setValue(v);
}
void TNCPanel::slotClose()
{
// capture panel geometry, state, and MCB/RCB with settings
mwd->mywindowgeometry = saveGeometry();
// stop the codecs and make a graceful exit
// save all settings to .ini
writeSettings();
close();
}
void TNCPanel::slotAudioErr(QString heading,QString details) { qCritical() << heading << details; }
void TNCPanel::slotSetupTNC(bool b)
{
Q_UNUSED(b)
QApplication::setOverrideCursor(Qt::WaitCursor);
QApplication::processEvents();
TncSetup dlgTNCSetup;
dlgTNCSetup.setControlBlock(mcb);
int accepted = dlgTNCSetup.exec();
if ( accepted == QDialog::Accepted )
{
qDebug() << "Setup settings accepted...sound restarting.";
audioIOObject->stop();
audioInput->stop();
audioInput->disconnect(this);
delete audioInput;
readSettings();
initializeFromIni();
initializeAudio();
}
else
{
qDebug() << "Setup settings rejected...sound not restarted.";
}
}
/*
void TNCPanel::slotSetupRadio(bool b)
{
Q_UNUSED(b)
RadioSetup dlgRadioSetup;
dlgRadioSetup.exec();
}
*/
void TNCPanel::slotDispTypeChgd()
{
if ( mwd->m_disptype == Waterfall )
{
// display spectrum waterfall
}
else if ( mwd->m_disptype == Spectrum )
{
// display audio spectrum graph
}
else
{
// no display
// ui->wfwidget->setVisible(false);
}
}
// ----------------------------------------------------
// FRIENDLY LOCAL FUNCTIONS
// ----------------------------------------------------
/*
bool TNCPanel::startCodec()
{
// This delay is necessary for reliable startup
// especially following stopCodec()
QThread::sleep(100);
qDebug() << "Waking after sleep(100)";
// no need for thread locking here because this is the only
// thread that executes this code.
mwd->dttLastSoundCardSample = QDateTime::currentDateTime();
bool blnSpectrumSave = mcb->DisplaySpectrum;
bool blnWaterfallSave = mcb->DisplayWaterfall;
QDateTime dttStartWait = mwd->dttLastSoundCardSample;
mcb->DisplayWaterfall = false;
mcb->DisplaySpectrum = false;
blnCodecStarted = true;
objProtocol->ARDOPState = DISC;
// insurance for PTT off
mcb->PTT = false;
if ( objRadio ) objRadio->PTT(false);
//
mcb->DisplayWaterfall = blnWaterfallSave;
mcb->DisplaySpectrum = blnSpectrumSave;
mwd->dttCodecStarted = QDateTime::currentDateTime();
QBrush brush;
brush.setColor(ColorLightGreen);
brush.setStyle(Qt::SolidPattern);
ui->lblICF->setText(tr("CODEC Start OK"));
mwd->blnGraphicsCleared = false;
qInfo() << tr(" - Start Codec succeeded ");
//inititializeNotifications();
// start the input device
mwd->blnStartCodec = true;
return blnCodecStarted;
} // startCodec()
*/
/*
bool TNCPanel::stopCodec()
{
// Function to stop the Codec (sound card capture)
bool blnStopCodec = false;
Q_UNUSED(blnStopCodec) // to eliminate warnings in release compile
qInfo() << tr("Stop thrNotify with blnSCCapturing = False");
mwd->blnSCCapturing = false;
// this should end the wait thread if it is still running
this->thread()->sleep(200);
if ( this->thread()->isRunning() )
{
qWarning() << tr("Extending wait 100 ms for stopCodec()");
this->thread()->sleep(100);
}
// Stop the buffer
// if ( engine )
// {
// engine->stopCapture();
// engine = 0;
// }
if ( !mwd->devCaptureDevice.isNull() ) mwd->devCaptureDevice = QAudioDeviceInfo::defaultInputDevice();
if ( !mwd->devSelectedPlaybackDevice.isNull() ) mwd->devSelectedPlaybackDevice = QAudioDeviceInfo::defaultOutputDevice();
qInfo() << tr("Stopping Codec ");
QBrush brush;
brush.setColor(ColorDarkGray);
brush.setStyle(Qt::SolidPattern);
ui->lblICF->setText(tr("CODEC Stopped"));
// objProtocol.SetARDOPProtocolState(OFFLINE);
// tmrStartCODEC->stop();
return true;
} // StopCodec
*/
/*
void TNCPanel::clearSpectrum()
{
// Subroutine to initialize the Spectrum/Waterfall display to black.
// if ( mwd->bmpSpectrum ) mwd->bmpSpectrum = 0;
// mwd->graFrequency = ui->lblWaterfall;
// mwd->graFrequency.setAutoFillBackground(QColor::black());
// mwd->intBMPSpectrumWidth = 256;
// mwd->intBMPSpectrumHeight = 62;
// mwd->bmpSpectrum = new QBitmap(intBMPSpectrumWidth, intBMPSpectrumHeight);
// ui->wfdisplay
} // clearSpectrum()
*/
void TNCPanel::initializeFromIni()
{
qInfo() << "Restoring settings read from .ini";
restoreGeometry(mwd->mywindowgeometry);
// set input device to the system default
if ( mwd->strCaptureDev == "" ||
mwd->strCaptureDev == "default"
)
{
devCaptureDevice = QAudioDeviceInfo::defaultInputDevice();