forked from JvanKatwijk/qt-dab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio.cpp
executable file
·1706 lines (1551 loc) · 49.5 KB
/
radio.cpp
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
#
/*
* Copyright (C) 2013, 2014, 2015, 2016, 2017
* Jan van Katwijk ([email protected])
* Lazy Chair Computing
*
* This file is part of the Qt-DAB (formerly SDR-J, JSDR).
* Qt-DAB 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.
*
* Qt-DAB 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 Qt-DAB; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <QSettings>
#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include <QDateTime>
#include <QFile>
#include <QStringList>
#include <QStringListModel>
#include <QDir>
#include <fstream>
#include "dab-constants.h"
#include <iostream>
#include <numeric>
#include <unistd.h>
#include <vector>
#include "radio.h"
#include "band-handler.h"
#include "ensemble-printer.h"
#include "rawfiles.h"
#include "wavfiles.h"
#ifdef TCP_STREAMER
#include "tcp-streamer.h"
#elif QT_AUDIO
#include "Qt-audio.h"
#else
#include "audiosink.h"
#endif
#ifdef HAVE_RTLSDR
#include "rtlsdr-handler.h"
#endif
#ifdef HAVE_SDRPLAY
#include "sdrplay-handler.h"
#endif
#ifdef HAVE_ELAD_S1
#include "elad-handler.h"
#endif
#ifdef __MINGW32__
#ifdef HAVE_EXTIO
#include "extio-handler.h"
#endif
#endif
#ifdef HAVE_RTL_TCP
#include "rtl_tcp_client.h"
#endif
#ifdef HAVE_AIRSPY
#include "airspy-handler.h"
#endif
#ifdef HAVE_HACKRF
#include "hackrf-handler.h"
#endif
#include "ui_technical_data.h"
#include "spectrum-viewer.h"
#include "impulse-viewer.h"
#include "tii-viewer.h"
std::vector<size_t> get_cpu_times (void) {
std::ifstream proc_stat ("/proc/stat");
proc_stat. ignore (5, ' '); // Skip the 'cpu' prefix.
std::vector<size_t> times;
for (size_t time; proc_stat >> time; times. push_back (time));
return times;
}
bool get_cpu_times (size_t &idle_time, size_t &total_time) {
const std::vector <size_t> cpu_times = get_cpu_times ();
if (cpu_times. size () < 4)
return false;
idle_time = cpu_times [3];
total_time = std::accumulate (cpu_times. begin (), cpu_times. end (), 0);
return true;
}
/**
* We use the creation function merely to set up the
* user interface and make the connections between the
* gui elements and the handling agents. All real action
* is initiated by gui buttons
*/
RadioInterface::RadioInterface (QSettings *Si,
int32_t dataPort,
QWidget *parent):
QMainWindow (parent) {
int16_t latency;
int16_t k;
QString h;
dabSettings = Si;
running. store (false);
scanning = false;
tiiSwitch = false;
isSynced = UNSYNCED;
dabBand = BAND_III;
spectrumBuffer = new RingBuffer<std::complex<float>> (2 * 32768);
iqBuffer = new RingBuffer<std::complex<float>> (2 * 1536);
responseBuffer = new RingBuffer<float> (32768);
tiiBuffer = new RingBuffer<std::complex<float>> (32768);
audioBuffer = new RingBuffer<int16_t>(16 * 32768);
/** threshold is used in the phaseReference class
* as threshold for checking the validity of the correlation result
* 3 is a reasonable value
*/
threshold =
dabSettings -> value ("threshold", 3). toInt ();
//
// latency is used to allow different settings for different
// situations wrt the output buffering. For windows and the RPI
// this need to be a pretty large value (e.g. 5 to 10)
latency =
dabSettings -> value ("latency", 5). toInt ();
diff_length =
dabSettings -> value ("diff_length", DIFF_LENGTH). toInt ();
tii_delay =
dabSettings -> value ("tii_delay", 20). toInt ();
if (tii_delay < 20)
tii_delay = 20;
dataBuffer = new RingBuffer<uint8_t>(32768);
/// The default, most likely to be overruled
#ifdef _SEND_DATAGRAM_
ipAddress = dabSettings -> value ("ipAddress", "127.0.0.1"). toString ();
port = dabSettings -> value ("port", 8888). toInt ();
#endif
//
has_presetName = dabSettings -> value ("has-presetName", 1). toInt () != 0;
if (has_presetName)
presetName = dabSettings -> value ("presetname", ""). toString ();
currentName = QString ("");
saveSlides = dabSettings -> value ("saveSlides", 1). toInt ();
showSlides = dabSettings -> value ("showPictures", 1). toInt ();
if (saveSlides != 0)
set_picturePath ();
///////////////////////////////////////////////////////////////////////////
// The settings are done, now creation of the GUI parts
setupUi (this);
//
dataDisplay = new QFrame (nullptr);
techData. setupUi (dataDisplay);
dataDisplay -> hide ();
#ifdef __MINGW32__
techData. cpuLabel -> hide ();
techData. cpuMonitor -> hide ();
#endif
//
#ifdef DATA_STREAMER
dataStreamer = new tcpServer (dataPort);
#else
(void)dataPort;
#endif
// Where do we leave the audio out?
streamoutSelector -> hide ();
#ifdef TCP_STREAMER
soundOut = new tcpStreamer (20040);
#elif QT_AUDIO
soundOut = new Qt_Audio ();
#else
// just sound out
soundOut = new audioSink (latency);
((audioSink *)soundOut) -> setupChannels (streamoutSelector);
streamoutSelector -> show ();
bool err;
h = dabSettings -> value ("soundchannel", "default"). toString ();
k = streamoutSelector -> findText (h);
if (k != -1) {
streamoutSelector -> setCurrentIndex (k);
err = !((audioSink *)soundOut) -> selectDevice (k);
}
if ((k == -1) || err)
((audioSink *)soundOut) -> selectDefaultDevice ();
#endif
//
my_spectrumViewer = new spectrumViewer (this, dabSettings,
spectrumBuffer,
iqBuffer);
my_impulseViewer = new impulseViewer (this,
responseBuffer);
my_tiiViewer = new tiiViewer (this, tiiBuffer);
//
// restore some settings from previous incarnations
QString t =
dabSettings -> value ("dabBand", "VHF Band III"). toString ();
k = bandSelector -> findText (t);
if (k != -1)
bandSelector -> setCurrentIndex (k);
dabBand = bandSelector -> currentText () == "VHF Band III" ?
BAND_III : L_BAND;
theBand. setupChannels (channelSelector, dabBand);
dabMode = dabSettings -> value ("dabMode", "Mode 1"). toString ();
//
QPalette p = techData. ficError_display -> palette ();
p. setColor (QPalette::Highlight, Qt::green);
techData. ficError_display -> setPalette (p);
techData. frameError_display -> setPalette (p);
techData. rsError_display -> setPalette (p);
techData. aacError_display -> setPalette (p);
techData. rsError_display -> hide ();
techData. aacError_display -> hide ();
techData. motAvailable ->
setStyleSheet ("QLabel {background-color : red}");
//
//
sourceDumping = false;
dumpfilePointer = nullptr;
audioDumping = false;
audiofilePointer = nullptr;
ficBlocks = 0;
ficSuccess = 0;
pictureLabel = nullptr;
syncedLabel ->
setStyleSheet ("QLabel {background-color : red}");
ensemble.setStringList (Services);
ensembleDisplay -> setModel (&ensemble);
Services << " ";
ensemble. setStringList (Services);
ensembleDisplay -> setModel (&ensemble);
//
connect (streamoutSelector, SIGNAL (activated (int)),
this, SLOT (set_streamSelector (int)));
//
// display the version
QString v = "Qt-DAB -" + QString (CURRENT_VERSION);
QString versionText = "qt-dab version: " + QString(CURRENT_VERSION);
versionText += " Build on: " + QString(__TIMESTAMP__) + QString (" ") + QString (GITHASH);
versionName -> setText (v);
versionName -> setToolTip (versionText);
// and start the timer(s)
// The displaytimer is there to show the number of
// seconds running and handle - if available - the tii data
displayTimer. setInterval (1000);
connect (&displayTimer, SIGNAL (timeout (void)),
this, SLOT (updateTimeDisplay (void)));
displayTimer. start (1000);
numberofSeconds = 0;
//
// timer for scanning
signalTimer. setSingleShot (true);
signalTimer. setInterval (10000);
connect (&signalTimer, SIGNAL (timeout (void)),
this, SLOT (No_Signal_Found (void)));
#ifdef HAVE_SDRPLAY
deviceSelector -> addItem ("sdrplay");
#endif
#ifdef HAVE_RTLSDR
deviceSelector -> addItem ("dabstick");
#endif
#ifdef HAVE_AIRSPY
deviceSelector -> addItem ("airspy");
#endif
#ifdef HAVE_ELAD_S1
deviceSelector -> addItem ("elad-s1");
#endif
#ifdef HAVE_HACKRF
deviceSelector -> addItem ("hackrf");
#endif
#ifdef HAVE_EXTIO
deviceSelector -> addItem ("extio");
#endif
#ifdef HAVE_RTL_TCP
deviceSelector -> addItem ("rtl_tcp");
#endif
inputDevice = nullptr;
h =
dabSettings -> value ("device", "no device"). toString ();
k = deviceSelector -> findText (h);
// fprintf (stderr, "%d %s\n", k, h. toUtf8 (). data ());
if (k != -1) {
deviceSelector -> setCurrentIndex (k);
inputDevice = setDevice (deviceSelector -> currentText ());
}
my_dabProcessor = nullptr;
// if a device was selected, we just start, otherwise
// we wait until one is selected
if (inputDevice != nullptr)
emit doStart ();
else
connect (deviceSelector, SIGNAL (activated (QString)),
this, SLOT (doStart (QString)));
}
void RadioInterface::doStart (QString dev) {
inputDevice = setDevice (deviceSelector -> currentText ());
if (inputDevice == nullptr) {
// just in case someone wants to push all those nice buttons that
// are now connected to erroneous constructs
// Some buttons should not be touched before we have a device
disconnectGUI ();
return;
}
emit doStart ();
}
//
// when doStart is called, a device is available and selected
void RadioInterface::doStart (void) {
bool r = 0;
int32_t frequency;
QString h = dabSettings -> value ("channel", "12C"). toString ();
int k = channelSelector -> findText (h);
if (k != -1) {
channelSelector -> setCurrentIndex (k);
}
frequency = theBand. Frequency (dabBand,
channelSelector -> currentText ());
inputDevice -> setVFOFrequency (frequency);
r = inputDevice -> restartReader ();
qDebug ("Starting %d\n", r);
if (!r) {
QMessageBox::warning (this, tr ("Warning"),
tr ("Opening input stream failed\n"));
return; // give it another try
}
// Some buttons should not be touched before we have a device
connectGUI ();
// we avoided up till now connecting the channel selector
// to the slot since that function does a lot more that we
// do not want here
connect (channelSelector, SIGNAL (activated (const QString &)),
this, SLOT (selectChannel (const QString &)));
// and we connect the deviceSelector to a handler
disconnect (deviceSelector, SIGNAL (activated (QString)),
this, SLOT (doStart (QString)));
//
// Just to be sure we disconnect here.
// It would have been helpful to have a function
// testing whether or not a connection exists, we need a kind
// of "reset"
disconnect (deviceSelector, SIGNAL (activated (QString)),
this, SLOT (newDevice (QString)));
connect (deviceSelector, SIGNAL (activated (QString)),
this, SLOT (newDevice (QString)));
//
// It is time for some action
my_dabProcessor = new dabProcessor (this,
inputDevice,
convert (dabMode),
threshold,
diff_length,
tii_delay,
picturesPath,
responseBuffer,
spectrumBuffer,
iqBuffer,
tiiBuffer
);
if (has_presetName && (presetName != QString (""))) {
presetTimer. setSingleShot (true);
presetTimer. setInterval (8000);
connect (&presetTimer, SIGNAL (timeout (void)),
this, SLOT (setPresetStation (void)));
presetTimer. start (8000);
}
clearEnsemble (); // the display
my_dabProcessor -> start ();
running. store (true);
}
RadioInterface::~RadioInterface (void) {
fprintf (stderr, "radioInterface is deleted\n");
}
//
/**
* \brief At the end, we might save some GUI values
* The QSettings could have been the class variable as well
* as the parameter
*/
void RadioInterface::dumpControlState (QSettings *s) {
if (s == nullptr) // cannot happen
return;
if (has_presetName)
s -> setValue ("presetname", currentName);
s -> setValue ("device",
deviceSelector -> currentText ());
s -> setValue ("channel",
channelSelector -> currentText ());
s -> setValue ("soundchannel",
streamoutSelector -> currentText ());
s -> setValue ("dabBand", bandSelector -> currentText ());
s -> sync ();
}
//
//
///////////////////////////////////////////////////////////////////////////////
//
// The public slots are called from other places within the dab software
// so please provide some implementation, perhaps an empty one
//
// a slot called by the ofdmprocessor
void RadioInterface::set_CorrectorDisplay (int v) {
if (running. load ())
correctorDisplay -> display (v);
}
// If the ofdm processor has waited for a period of N frames
// to get a start of a synchronization,
// it sends a signal to the GUI handler
// If "scanning" is "on" we hop to the next frequency on
// the list
void RadioInterface::signalTimer_out (void) {
No_Signal_Found ();
}
void RadioInterface::No_Signal_Found (void) {
signalTimer. stop ();
if (!running. load () || !scanning)
return;
//
// we stop the thread from running,
// Increment the frequency
// and restart
my_dabProcessor -> stop ();
while (!my_dabProcessor -> isFinished ())
usleep (10000);
Increment_Channel ();
clearEnsemble ();
my_dabProcessor -> start ();
signalTimer. start (10000);
}
//
// In case the scanning button was pressed, we
// set it off as soon as we have a signal found
void RadioInterface::Yes_Signal_Found (void) {
if (!running. load ())
return;
signalTimer. stop ();
if (!scanning)
return;
set_Scanning ();
}
void RadioInterface::set_Scanning (void) {
if (!running. load ())
return;
presetTimer. stop ();
setStereo (false);
if (!running. load ())
return;
scanning = !scanning;
my_dabProcessor -> set_scanMode (scanning);
if (scanning) {
scanButton -> setText ("scanning");
Increment_Channel ();
signalTimer. start (10000);
}
else
scanButton -> setText ("Scan band");
}
//
// Increment channel is called during scanning.
// The approach taken here is to increment the current index
// in the combobox and select the new frequency.
// To avoid disturbance, we disconnect the combobox
// temporarily, since otherwise changing the channel would
// generate a signal
void RadioInterface::Increment_Channel (void) {
int32_t tunedFrequency;
if (!running. load ())
return;
int cc = channelSelector -> currentIndex ();
cc += 1;
if (cc >= channelSelector -> count ())
cc = 0;
//
// To avoid reaction of the system on setting a different value
disconnect (channelSelector, SIGNAL (activated (const QString &)),
this, SLOT (selectChannel (const QString &)));
channelSelector -> setCurrentIndex (cc);
tunedFrequency =
theBand. Frequency (dabBand, channelSelector -> currentText ());
inputDevice -> setVFOFrequency (tunedFrequency);
connect (channelSelector, SIGNAL (activated (const QString &)),
this, SLOT (selectChannel (const QString &)));
}
///////////////////////////////////////////////////////////////////////////
/**
* clearEnsemble
* on changing settings, we clear all things in the gui
* related to the ensemble.
* The function is called from "deep" within the handling code
* Potentially a dangerous approach, since the fic handler
* might run in a separate thread and generate data to be displayed
*/
void RadioInterface::clearEnsemble (void) {
if (!running. load ())
return;
// it obviously means: stop processing
my_dabProcessor -> clearEnsemble ();
my_dabProcessor -> reset ();
clear_showElements ();
}
//
// a slot, called by the fic/fib handlers
void RadioInterface::addtoEnsemble (const QString &s) {
if (!running. load ())
return;
Services << s;
Services. removeDuplicates ();
ensemble. setStringList (Services);
ensembleDisplay -> setModel (&ensemble);
}
//
/// a slot, called by the fib processor
void RadioInterface::nameofEnsemble (int id, const QString &v) {
QString s;
if (!running. load ())
return;
(void)v;
ensembleId -> display (id);
ensembleLabel = v;
ensembleName -> setText (v);
my_dabProcessor -> coarseCorrectorOff ();
Yes_Signal_Found ();
}
///////////////////////////////////////////////////////////////////////
/**
* \brief show_successRate
* a slot, called by the MSC handler to show the
* percentage of frames that could be handled
*/
void RadioInterface::show_frameErrors (int s) {
if (running. load ())
techData. frameError_display -> setValue (100 - 4 * s);
}
void RadioInterface::show_rsErrors (int s) {
if (running. load ())
techData. rsError_display -> setValue (100 - 4 * s);
}
void RadioInterface::show_aacErrors (int s) {
if (running. load ())
techData. aacError_display -> setValue (100 - 4 * s);
}
void RadioInterface::show_ficSuccess (bool b) {
if (!running. load ())
return;
if (b)
ficSuccess ++;
if (++ficBlocks >= 100) {
techData. ficError_display -> setValue (ficSuccess);
ficSuccess = 0;
ficBlocks = 0;
}
}
void RadioInterface::show_motHandling (bool b) {
if (!running. load ())
return;
if (b) {
techData. motAvailable ->
setStyleSheet ("QLabel {background-color : green}");
}
else {
techData. motAvailable ->
setStyleSheet ("QLabel {background-color : red}");
}
}
/// called from the ofdmDecoder, which computed this for each frame
void RadioInterface::show_snr (int s) {
if (running. load ())
snrDisplay -> display (s);
}
/// just switch a color, obviously GUI dependent, but called
// from the ofdmprocessor
void RadioInterface::setSynced (char b) {
if (!running. load ())
return;
if (isSynced == b)
return;
isSynced = b;
switch (isSynced) {
case SYNCED:
syncedLabel ->
setStyleSheet ("QLabel {background-color : green}");
break;
default:
syncedLabel ->
setStyleSheet ("QLabel {background-color : red}");
break;
}
}
// showLabel is triggered by the message handler
// the GUI may decide to ignore this
void RadioInterface::showLabel (QString s) {
if (running. load ())
dynamicLabel -> setText (s);
}
void RadioInterface::setStereo (bool s) {
if (!running. load ())
return;
if (s)
stereoLabel ->
setStyleSheet ("QLabel {background-color : green}");
else
stereoLabel ->
setStyleSheet ("QLabel {background-color : red}");
}
//
//////////////////////////////////////////////////////////////////////////
//
void checkDir (QString &s) {
int16_t ind = s. lastIndexOf (QChar ('/'));
int16_t i;
QString dir;
if (ind == -1) // no slash, no directory
return;
for (i = 0; i < ind; i ++)
dir. append (s [i]);
if (QDir (dir). exists ())
return;
QDir (). mkpath (dir);
}
// showMOT is triggered by the MOT handler,
// the GUI may decide to ignore the data sent
// since data is only sent whenever a data channel is selected
void RadioInterface::showMOT (QByteArray data,
int subtype, QString pictureName) {
const char *type;
if (!running. load ())
return;
if (pictureLabel == nullptr)
pictureLabel = new QLabel (nullptr);
type = subtype == 0 ? "GIF" :
subtype == 1 ? "JPG" :
// subtype == 1 ? "JPEG" :
subtype == 2 ? "BMP" : "PNG";
QPixmap p;
p. loadFromData (data, type);
if (saveSlides && (pictureName != QString (""))) {
pictureName = QDir::toNativeSeparators (pictureName);
FILE *x = fopen (pictureName. toLatin1 (). data (), "w+b");
if (x == nullptr)
fprintf (stderr, "cannot write file %s\n",
pictureName. toLatin1 (). data ());
else {
fprintf (stderr, "going to write file %s\n",
pictureName. toLatin1 (). data ());
(void)fwrite (data. data (), 1, data.length (), x);
fclose (x);
}
}
// pictureLabel -> setFrameRect (QRect (0, 0, p. height (), p. width ()));
if (showSlides) {
pictureLabel -> setPixmap (p);
pictureLabel -> show ();
}
}
//
// sendDatagram is triggered by the ip handler,
//
void RadioInterface::sendDatagram (int length) {
uint8_t localBuffer [length];
if (dataBuffer -> GetRingBufferReadAvailable () < length) {
fprintf (stderr, "Something went wrong\n");
return;
}
dataBuffer -> getDataFromBuffer (localBuffer, length);
#ifdef _SEND_DATAGRAM_
if (running. load ())
dataOut_socket. writeDatagram ((const char *)localBuffer, length,
QHostAddress (ipAddress),
port);
#endif
}
//
//
void RadioInterface::handle_tdcdata (int frametype, int length) {
#ifdef DATA_STREAMER
uint8_t localBuffer [length + 8];
#endif
if (!running. load ())
return;
if (dataBuffer -> GetRingBufferReadAvailable () < length) {
fprintf (stderr, "Something went wrong\n");
return;
}
#ifdef DATA_STREAMER
dataBuffer -> getDataFromBuffer (&localBuffer [8], length);
localBuffer [0] = 0xFF;
localBuffer [1] = 0x00;
localBuffer [2] = 0xFF;
localBuffer [3] = 0x00;
localBuffer [4] = (length & 0xFF) >> 8;
localBuffer [5] = length & 0xFF;
localBuffer [6] = 0x00;
localBuffer [7] = frametype == 0 ? 0 : 0xFF;
if (running. load ())
dataStreamer -> sendData (localBuffer, length + 8);
#endif
}
/**
* \brief changeinConfiguration
* No idea yet what to do, so just give up
* with what we were doing. The user will -eventually -
* see the new configuration from which he can select
*/
void RadioInterface::changeinConfiguration (void) {
if (running. load ()) {
soundOut -> stop ();
inputDevice -> stopReader ();
inputDevice -> resetBuffer ();
my_dabProcessor -> reset ();
clear_showElements ();
}
}
//
// In order to not overload with an enormous amount of
// signals, we trigger this function at most 10 times a second
//
void RadioInterface::newAudio (int amount, int rate) {
if (running. load ()) {
int16_t vec [amount];
while (audioBuffer -> GetRingBufferReadAvailable () > amount) {
audioBuffer -> getDataFromBuffer (vec, amount);
soundOut -> audioOut (vec, amount, rate);
}
}
}
//
// This function is only used in the Gui to clear
// the details of a selection
void RadioInterface::clear_showElements (void) {
if (!running. load ())
return;
Services = QStringList ();
ensemble. setStringList (Services);
ensembleDisplay -> setModel (&ensemble);
my_dabProcessor -> clearEnsemble ();
ensembleLabel = QString ();
ensembleName -> setText (ensembleLabel);
dynamicLabel -> setText ("");
// Then the various displayed items
ensembleName -> setText (" ");
techData. frameError_display -> setValue (0);
techData. rsError_display -> setValue (0);
techData. aacError_display -> setValue (0);
techData. ficError_display -> setValue (0);
techData. ensemble -> setText (QString (""));
techData. programName -> setText (QString (""));
techData. frequency -> display (0);
techData. bitrateDisplay -> display (0);
techData. startAddressDisplay -> display (0);
techData. lengthDisplay -> display (0);
techData. subChIdDisplay -> display (0);
// techData. protectionlevelDisplay -> display (0);
techData. uepField -> setText (QString (""));
techData. ASCTy -> setText (QString (""));
techData. language -> setText (QString (""));
techData. programType -> setText (QString (""));
techData. motAvailable ->
setStyleSheet ("QLabel {background-color : red}");
techData. transmitter_coordinates -> setText (" ");
snrDisplay -> display (0);
if (pictureLabel != nullptr)
delete pictureLabel;
pictureLabel = nullptr;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
/**
* \brief TerminateProcess
* Pretty critical, since there are many threads involved
* A clean termination is what is needed, regardless of the GUI
*/
void RadioInterface::TerminateProcess (void) {
running. store (false);
#ifdef DATA_STREAMER
fprintf (stderr, "going to close the dataStreamer\n");
delete dataStreamer;
#endif
displayTimer. stop ();
signalTimer. stop ();
presetTimer. stop ();
if (audioDumping) {
soundOut -> stopDumping ();
sf_close (audiofilePointer);
}
if (inputDevice != nullptr)
inputDevice -> stopReader (); // might be concurrent
if (my_dabProcessor != nullptr)
my_dabProcessor -> stop (); // definitely concurrent
soundOut -> stop ();
dataDisplay -> hide ();
// everything should be halted by now
dumpControlState (dabSettings);
delete soundOut;
if (inputDevice != nullptr)
delete inputDevice;
fprintf (stderr, "going to delete dabProcessor\n");
if (my_dabProcessor != nullptr)
delete my_dabProcessor;
fprintf (stderr, "deleted dabProcessor\n");
delete dataDisplay;
delete my_spectrumViewer;
delete my_tiiViewer;
delete my_impulseViewer;
if (pictureLabel != nullptr)
delete pictureLabel;
pictureLabel = nullptr; // signals may be pending, so careful
delete iqBuffer;
delete spectrumBuffer;
delete responseBuffer;
delete tiiBuffer;
// close ();
fprintf (stderr, ".. end the radio silences\n");
}
//
/**
* \brief selectChannel
* Depending on the GUI the user might select a channel
* or some magic will cause a channel to be selected
*/
void RadioInterface::selectChannel (QString s) {
int32_t tunedFrequency;
bool localRunning = running. load ();
presetTimer. stop ();
setStereo (false);
if (scanning)
set_Scanning (); // switch it off
if (tiiSwitch)
set_tiiSwitch (); // switch it off
if (localRunning) {
soundOut -> stop ();
inputDevice -> stopReader ();
}
clear_showElements ();
tunedFrequency = theBand. Frequency (dabBand, s);
inputDevice -> setVFOFrequency (tunedFrequency);
if (localRunning) {
if (!inputDevice -> restartReader ()) {
QMessageBox::warning (this, tr ("Warning"),
tr ("Device will not restart\n"));
return;
}
my_dabProcessor -> reset ();
running. store (true);
}
dabSettings -> setValue ("channel", s);
}
static size_t previous_idle_time = 0;
static size_t previous_total_time = 0;
void RadioInterface::updateTimeDisplay (void) {
if (!running. load ())
return;
numberofSeconds ++;
int16_t numberHours = numberofSeconds / 3600;
int16_t numberMinutes = (numberofSeconds / 60) % 60;
QString text = QString ("runtime ");
text. append (QString::number (numberHours));
text. append (" hr, ");
text. append (QString::number (numberMinutes));
text. append (" min");
timeDisplay -> setText (text);
#ifndef __MINGW32__
if ((numberofSeconds % 2) == 0) {
size_t idle_time, total_time;
get_cpu_times (idle_time, total_time);
const float idle_time_delta = idle_time - previous_idle_time;
const float total_time_delta = total_time - previous_total_time;
const float utilization = 100.0 * (1.0 - idle_time_delta / total_time_delta);
techData. cpuMonitor -> display (utilization);
previous_idle_time = idle_time;
previous_total_time = total_time;
}
#endif
// if ((numberofSeconds % 10) == 0) {
// int xxx = ((audioSink *)soundOut) -> missed ();
// fprintf (stderr, "missed %d\n", xxx);
// }
}
void RadioInterface::autoCorrector_on (void) {
// first the real stuff
if (!running. load ())
return;
clear_showElements ();
my_dabProcessor -> clearEnsemble ();
my_dabProcessor -> coarseCorrectorOn ();
my_dabProcessor -> reset ();
}
/**
* \brief setDevice
* setDevice is called during the start up phase
* of the dab program, and from within newDevice,
* that would/should have taken care of proper
* stopping service handling
*/
virtualInput *RadioInterface::setDevice (QString s) {
QString file;
virtualInput *inputDevice = nullptr;
/// OK, everything quiet, now let us see what to do
#ifdef HAVE_AIRSPY
if (s == "airspy") {
try {
inputDevice = new airspyHandler (dabSettings);
showButtons ();
}
catch (int e) {
QMessageBox::warning (this, tr ("Warning"),
tr ("Airspy or Airspy mini not found\n"));
return nullptr;
}
}
else
#endif
#ifdef HAVE_HACKRF
if (s == "hackrf") {