-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathradio.cpp
More file actions
executable file
·582 lines (540 loc) · 17.3 KB
/
Copy pathradio.cpp
File metadata and controls
executable file
·582 lines (540 loc) · 17.3 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
#
/*
* Copyright (C) 2020
* Jan van Katwijk (J.vanKatwijk@gmail.com)
* Lazy Chair Computing
*
* This file is part of DRM+-receiver
*
* DRM+ receiver 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.
*
* DRM+ receiver 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 DRM+ receiver; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <unistd.h>
#include <QSettings>
#include <QDebug>
#include <QDateTime>
#include <QObject>
#include <QDir>
#include <QColor>
#include <QMessageBox>
#include <QFileDialog>
#include "radio.h"
#include "fft-scope.h"
#include "audiosink.h"
#include "popup-keypad.h"
#include "program-list.h"
//
// devices
#include "device-handler.h"
#include "wavfiles.h"
#include "rawfiles-192-8.h"
#include "rawfiles-192-16.h"
#include "rawfiles-192-32.h"
#ifdef HAVE_RTLSDR
#include "rtlsdr-handler.h"
#endif
#ifdef HAVE_SDRPLAY_V2
#include "sdrplay-handler-v2.h"
#endif
#ifdef HAVE_SDRPLAY_V3
#include "sdrplay-handler-v3.h"
#endif
#ifdef HAVE_AIRSPY
#include "airspy-handler.h"
#endif
#ifdef HAVE_HACKRF
#include "hackrf-handler.h"
#endif
#ifdef HAVE_SOAPY
#include "soapy-handler.h"
#endif
#ifdef HAVE_LIME
#include "lime-handler.h"
#endif
//
// the decoder
#include "drm-decoder.h"
#include "fm-decoder.h"
QString FrequencytoString (int32_t freq) {
if (freq < 10)
return QString ('0' + (uint8_t)(freq % 10));
return
FrequencytoString (freq / 10). append (QString ('0' + (uint8_t)(freq % 10)));
}
//
// inputrate = 192K, wide enough to show - if available -
// the DRM+ signal and its surroundings
RadioInterface::RadioInterface (QSettings *sI,
QString stationList,
int inputRate,
QWidget *parent):
QMainWindow (parent) {
this -> settings = sI;
this -> stationList = stationList;
this -> inputRate = inputRate;
setupUi (this);
QPalette *p = new QPalette;
p -> setColor (QPalette::WindowText, Qt::white);
frequencyDisplay -> setPalette (*p);
// and some buffers
// in comes:
inputData = new RingBuffer<std::complex<float> > (2048 * 1024);
bufferData = new RingBuffer<std::complex<float>> (1024 * 1024);
audioData = new RingBuffer<std::complex<float>> (32768);
deviceSelector -> addItem ("wav files");
deviceSelector -> addItem ("raw files");
deviceSelector -> addItem ("raw files 16");
deviceSelector -> addItem ("raw files 32");
//
#ifdef HAVE_SDRPLAY_V2
deviceSelector -> addItem ("sdrplay");
#endif
#ifdef HAVE_SDRPLAY_V3
deviceSelector -> addItem ("sdrplay-v3");
#endif
#ifdef HAVE_RTLSDR
deviceSelector -> addItem ("dabstick");
#endif
#ifdef HAVE_AIRSPY
deviceSelector -> addItem ("airspy");
#endif
#ifdef HAVE_HACKRF
deviceSelector -> addItem ("hackrf");
#endif
#ifdef HAVE_SOAPY
deviceSelector -> addItem ("soapy");
#endif
#ifdef HAVE_LIME
deviceSelector -> addItem ("limeSDR");
#endif
displaySize = 1024;
scopeWidth = inputRate;
theBand. currentOffset = 0;
// scope and settings
hfScopeSlider -> setValue (50);
hfScope = new fftScope (hfSpectrum,
displaySize,
kHz (1), // scale
inputRate,
hfScopeSlider -> value (),
8);
hfScope -> set_bitDepth (12); // default
hfScope -> setScope (94000 * 1000, 0);
mykeyPad = new keyPad (this);
delayCount = 0;
secondsTimer. setInterval (1000);
connect (&secondsTimer, SIGNAL (timeout (void)),
this, SLOT (updateTime (void)));
connect (decoderSelect, SIGNAL (activated (const QString &)),
this , SLOT (handle_decoderSelect (const QString &)));
secondsTimer. start (1000);
audioHandler = new audioSink (48000, 16384);
outTable = new int16_t
[audioHandler -> numberofDevices () + 1];
for (int i = 0; i < audioHandler -> numberofDevices (); i ++)
outTable [i] = -1;
try {
setupSoundOut (streamOutSelector, audioHandler,
48000, outTable);
} catch (int e) {
QMessageBox::warning (this, tr ("sdr"),
tr ("Opening audio failed\n"));
abort ();
}
audioHandler -> selectDefaultDevice ();
connect (deviceSelector, SIGNAL (activated (const QString &)),
this, SLOT (doStart (const QString &)));
theDevice = nullptr;
theDecoder = nullptr;
dumpfilePointer = nullptr;
myList = nullptr;
copyrightLabel -> setToolTip (footText ());
}
// The end of all
RadioInterface::~RadioInterface () {
secondsTimer. stop ();
// delete mykeyPad;
// delete myList;
}
QString RadioInterface::footText () {
QString versionText = "drmPlus- version: " + QString(CURRENT_VERSION);
versionText += "Copyright J van Katwijk, J. vanKatwijk@gmail.com\n";
versionText += "Rights of Qt, fftw, portaudio, libsamplerate and libsndfile gratefully acknowledged";
versionText += "Rights of other contribuants gratefully acknowledged\n";
versionText += " Build on: " + QString(__TIMESTAMP__) + QString (" ") + QString (GITHASH);
return versionText;
}
//
// To keep things simple, we start after a device is selected
// then we run, no re-selection of device is possible.
void RadioInterface::doStart (const QString &s) {
theDevice = setDevice (s, inputData);
if (theDevice == nullptr)
return;
hfScope -> set_bitDepth (theDevice -> bitDepth ());
deviceSelector -> hide ();
connect (hfScope,
SIGNAL (clickedwithLeft (int)),
this,
SLOT (adjustFrequency_khz (int)));
connect (hfScopeSlider, SIGNAL (valueChanged (int)),
this, SLOT (set_hfscopeLevel (int)));
// else we start the handle
connect (freqButton, SIGNAL (clicked (void)),
this, SLOT (handle_freqButton (void)));
connect (freqSave, SIGNAL (clicked (void)),
this, SLOT (set_freqSave (void)));
myList = new programList (this, stationList);
myList -> show ();
myLine = NULL;
connect (theDevice, SIGNAL (dataAvailable (int)),
this, SLOT (sampleHandler (int)));
connect (frequencyBackwards, SIGNAL (clicked ()),
this, SLOT (handle_frequencyBackwards ()));
connect (frequencyForwards, SIGNAL (clicked ()),
this, SLOT (handle_frequencyForwards ()));
connect (dumpButton, SIGNAL (clicked ()),
this, SLOT (handle_dumpButton ()));
theDevice -> restartReader ();
if (decoderSelect -> currentText () == "drm+")
theDecoder = new drmDecoder (this, bufferData, audioData);
else
theDecoder = new fmDecoder (this, bufferData, audioData);
}
// If the user quits before selecting a device ....
void RadioInterface::handle_quitButton (void) {
if (theDecoder != nullptr)
delete theDecoder;
if (theDevice != NULL) {
theDevice -> stopReader ();
disconnect (theDevice, SIGNAL (dataAvailable (int)),
this, SLOT (sampleHandler (int)));
delete theDevice;
}
sleep (1);
if (myList != nullptr) {
myList -> saveTable ();
delete myList;
}
}
//
void RadioInterface::handle_freqButton (void) {
if (mykeyPad -> isVisible ())
mykeyPad -> hidePad ();
else
mykeyPad -> showPad ();
}
// setFrequency is called from the frequency panel
// as well as from inside to change VFO and offset
void RadioInterface::setFrequency (uint32_t frequency) {
uint32_t VFOFrequency = frequency;
theDevice -> setVFOFrequency (VFOFrequency);
theBand. currentOffset = 0;
hfScope -> setScope (VFOFrequency, theBand. currentOffset);
QString ff = FrequencytoString (frequency);
frequencyDisplay -> display (ff);
}
//
// adjustFrequency is called whenever clicking the mouse
void RadioInterface::adjustFrequency_khz (int32_t n) {
adjustFrequency_hz (1000 * n);
}
void RadioInterface::adjustFrequency_hz (int32_t n) {
int32_t newFreq = theDevice -> getVFOFrequency () +
theBand. currentOffset + n;
setFrequency (newFreq);
hfScope -> setScope (theDevice -> getVFOFrequency (), 0);
QString ff = FrequencytoString ((int32_t)
(theDevice -> getVFOFrequency ()));
frequencyDisplay -> display (ff);
}
//
void RadioInterface::set_freqSave (void) {
if (myLine == NULL)
myLine = new QLineEdit ();
myLine -> show ();
connect (myLine, SIGNAL (returnPressed (void)),
this, SLOT (handle_myLine (void)));
}
void RadioInterface::handle_myLine (void) {
int32_t freq = theDevice -> getVFOFrequency () + theBand. currentOffset;
QString programName = myLine -> text ();
myList -> addRow (programName, QString::number (freq / Khz (1)));
disconnect (myLine, SIGNAL (returnPressed (void)),
this, SLOT (handle_myLine (void)));
myLine -> hide ();
}
//////////////////////////////////////////////////////////////////
//
#define SEGMENT_SIZE 1024
void RadioInterface::sampleHandler (int amount) {
std::complex<float> buffer [SEGMENT_SIZE];
float dumpBuffer [2 * SEGMENT_SIZE];
int i, j;
(void)amount;
while (inputData -> GetRingBufferReadAvailable () > SEGMENT_SIZE) {
int a = inputData -> getDataFromBuffer (buffer, SEGMENT_SIZE);
hfScope -> addElements (buffer, SEGMENT_SIZE);
bufferData -> putDataIntoBuffer (buffer, a);
if (dumpfilePointer != NULL) {
for (i = 0; i < SEGMENT_SIZE; i ++) {
dumpBuffer [2 * i] = real (buffer [i]);
dumpBuffer [2 * i + 1] = imag (buffer [i]);
}
sf_writef_float (dumpfilePointer, dumpBuffer, SEGMENT_SIZE);
}
}
}
//
//
void RadioInterface::set_hfscopeLevel (int level) {
hfScope -> setLevel (level);
}
void RadioInterface::updateTime (void) {
QDateTime currentTime = QDateTime::currentDateTime ();
timeDisplay -> setText (currentTime.
toString (QString ("dd.MM.yy:hh:mm:ss")));
}
#include <QCloseEvent>
void RadioInterface::closeEvent (QCloseEvent *event) {
QMessageBox::StandardButton resultButton =
QMessageBox::question (this, "dabRadio",
tr("Are you sure?\n"),
QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes);
if (resultButton != QMessageBox::Yes) {
event -> ignore();
} else {
handle_quitButton ();
event -> accept ();
}
}
// do not forget that ocnt starts with 1, due
// to Qt list conventions
void RadioInterface::setupSoundOut (QComboBox *streamOutSelector,
audioSink *our_audioSink,
int32_t cardRate,
int16_t *table) {
uint16_t ocnt = 1;
uint16_t i;
for (i = 0; i < our_audioSink -> numberofDevices (); i ++) {
const char *so =
our_audioSink -> outputChannelwithRate (i, cardRate);
if (so != NULL) {
streamOutSelector -> insertItem (ocnt, so, QVariant (i));
table [ocnt] = i;
ocnt ++;
}
}
qDebug () << "added items to combobox";
if (ocnt == 1)
throw (22);
}
void RadioInterface::processAudio (int amount, int rate) {
std::complex<float> buffer [rate / 10];
(void)amount;
// usleep (1000);
while (audioData -> GetRingBufferReadAvailable () >
(uint32_t)rate / 10) {
audioData -> getDataFromBuffer (buffer, rate / 10);
audioHandler -> putSamples (buffer, rate / 10);
}
}
deviceHandler *RadioInterface::setDevice (const QString &s,
RingBuffer<std::complex<float>> *b) {
QString file;
deviceHandler *inputDevice = nullptr;
/// OK, everything quiet, now let us see what to do
#ifdef HAVE_AIRSPY
if (s == "airspy") {
try {
inputDevice = new airspyHandler (drmSettings, b);
}
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") {
try {
inputDevice = new hackrfHandler (this, inputRate, b);
}
catch (int e) {
QMessageBox::warning (this, tr ("Warning"),
tr ("hackrf not found\n"));
return nullptr;
}
}
else
#endif
#ifdef HAVE_LIME
if (s == "limeSDR") {
try {
inputDevice = new limeHandler (this, inputRate, b);
}
catch (int e) {
QMessageBox::warning (this, tr ("Warning"),
tr ("no lime device found\n"));
return nullptr;
}
}
else
#endif
#ifdef HAVE_SDRPLAY_V2
if (s == "sdrplay") {
try {
inputDevice = new sdrplayHandler_v2 (settings,
this, inputRate, b);
}
catch (int e) {
QMessageBox::warning (this, tr ("Warning"),
tr ("SDRplay: no library or device\n"));
return nullptr;
}
}
else
#endif
#ifdef HAVE_SDRPLAY_V3
if (s == "sdrplay-v3") {
try {
inputDevice = new sdrplayHandler_v3 (drmSettings, b);
}
catch (int e) {
QMessageBox::warning (this, tr ("Warning"),
tr ("SDRplay: no library or device\n"));
return nullptr;
}
}
else
#endif
#ifdef HAVE_RTLSDR
if (s == "dabstick") {
try {
inputDevice = new rtlsdrHandler (this, inputRate, b);
}
catch (int e) {
QMessageBox::warning (this, tr ("Warning"),
tr ("DAB stick not found! Please use one with RTL2832U or similar chipset!\n"));
fprintf (stderr, "error = %d\n", e);
return nullptr;
}
}
else
#endif
if (s == "wav files") {
try {
inputDevice = new wavFiles (this, inputRate, b);
freqButton -> hide ();
mykeyPad -> hidePad ();
}
catch (int e) {
QMessageBox::warning (this, tr ("Warning"),
tr ("file not found"));
return nullptr;
}
}
else
if (s == "raw files 16") {
try {
inputDevice = new rawFiles_16 (this, inputRate, b);
freqButton -> hide ();
mykeyPad -> hidePad ();
}
catch (int e) {
QMessageBox::warning (this, tr ("Warning"),
tr ("file not found"));
return nullptr;
}
}
else
if (s == "raw files") {
try {
inputDevice = new rawFiles_8 (this, inputRate, b);
freqButton -> hide ();
mykeyPad -> hidePad ();
}
catch (int e) {
QMessageBox::warning (this, tr ("Warning"),
tr ("file not found"));
return nullptr;
}
}
else
if (s == "raw files 32") {
try {
inputDevice = new rawFiles_32 (this, inputRate, b);
freqButton -> hide ();
mykeyPad -> hidePad ();
}
catch (int e) {
QMessageBox::warning (this, tr ("Warning"),
tr ("file not found"));
return nullptr;
}
}
else {
fprintf (stderr, "unknown device, failing\n");
return nullptr;
}
return inputDevice;
}
void RadioInterface::handle_decoderSelect (const QString &s) {
if (theDevice == nullptr)
return; // nothing started yet
theDevice -> stopReader ();
delete theDecoder;
if (s == "drm+")
theDecoder = new drmDecoder (this, bufferData, audioData);
else
theDecoder = new fmDecoder (this, bufferData, audioData);
theDevice -> restartReader ();
}
void RadioInterface::handle_frequencyBackwards () {
adjustFrequency_khz (-50);
}
void RadioInterface::handle_frequencyForwards () {
adjustFrequency_khz (50);
}
void RadioInterface::handle_dumpButton (void) {
SF_INFO *sf_info = (SF_INFO *)alloca (sizeof (SF_INFO));
if (dumpfilePointer != NULL) {
sf_close (dumpfilePointer);
dumpfilePointer = NULL;
dumpButton -> setText ("dump");
return;
}
QString file = QFileDialog::getSaveFileName (this,
tr ("Save file ..."),
QDir::homePath (),
tr ("PCM wave file (*.wav)"));
if (file == QString (""))
return;
file = QDir::toNativeSeparators (file);
if (!file.endsWith (".wav", Qt::CaseInsensitive))
file.append (".wav");
sf_info -> samplerate = inputRate;
sf_info -> channels = 2;
sf_info -> format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
dumpfilePointer = sf_open (file. toUtf8 (). data (),
SFM_WRITE, sf_info);
if (dumpfilePointer == NULL) {
qDebug () << "Cannot open " << file. toUtf8 (). data ();
return;
}
dumpButton -> setText ("WRITING");
}