~hmatuschek/+junk/sdr-wspr-package

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
#include "sources.hh"
#include <QGroupBox>
#include <QFormLayout>
#include <QLabel>
#include <QPushButton>
#include <QFileDialog>


using namespace sdr;

/* ********************************************************************************************* *
 * Implementation of Input
 * ********************************************************************************************* */
WSPRSource::WSPRSource(double F, double Fbfo, QObject *parent)
  : QObject(parent), _F(F), _Fbfo(Fbfo)
{
  // pass...
}

double
WSPRSource::frequency() const {
  return _F;
}

void
WSPRSource::setFrequency(double F) {
  _F = F;
}

double
WSPRSource::bfoFrequency() const {
  return _Fbfo;
}

void
WSPRSource::setBfoFrequency(double F) {
  _Fbfo = F;
}


/* ********************************************************************************************* *
 * Implementation of RTLInput
 * ********************************************************************************************* */
WsprRtlSource::WsprRtlSource(double F, double Fbfo, QObject *parent)
  : WSPRSource(F, Fbfo, parent), _Fif(100e6), _Fcorr(0), _src(0), _cast(),
    _baseband(0, _Fbfo, 800, 31, 1 , 12e3), _demod(), _queue(Queue::get()), _view(0)
{
  try {
    std::cerr << "Tune to " << int(_Fif+_F+_Fcorr-_Fbfo) << "Hz" << std::endl;
    _src = new RTLSource(_Fif+_F+_Fcorr-_Fbfo, 240000);
    _src->connect(&_cast, true);
    _queue.addStart(_src, &RTLSource::start);
    _queue.addStop(_src, &RTLSource::stop);
  } catch (sdr::ConfigError &err) {
    LogMessage msg(LOG_INFO);
    msg << "Can not create RTL device: " << err.what();
    Logger::get().log(msg);
  }
  _cast.connect(&_baseband, true);
  _baseband.connect(&_demod, true);
}

WsprRtlSource::~WsprRtlSource() {
  if (_src) {
    _queue.remStart(_src);
    _queue.remStop(_src);
    delete _src;
  }
  if (_view) { _view->deleteLater(); }
}

QWidget *
WsprRtlSource::createView() {
  if (0 == _view) { _view = new WsprRtlSourceView(this); }
  return _view;
}

sdr::Source *
WsprRtlSource::source() {
  return &_demod;
}


double
WsprRtlSource::frequency() const {
  return _F;
}

void
WsprRtlSource::setFrequency(double F) {
  _F = F;
  if (_src ) {
    std::cerr << "Tune to " << int(_Fif+_F+_Fcorr-_Fbfo) << "Hz" << std::endl;
    _src->setFrequency(_Fif+_F+_Fcorr-_Fbfo);
    emit frequencyChanged(_F);
  }
}

double
WsprRtlSource::freqCorrection() const {
  return _Fcorr;
}

void
WsprRtlSource::setFreqCorrection(double F) {
  _Fcorr = F;
  if (_src) {
    std::cerr << "Tune to " << int(_Fif+_F+_Fcorr-_Fbfo) << "Hz" << std::endl;
    _src->setFrequency(_Fif+_F+_Fcorr-_Fbfo);
    emit frequencyCorrectionChanged(_Fcorr);
  }
}

double
WsprRtlSource::bfoFrequency() const {
  return _Fbfo;
}

void
WsprRtlSource::setBfoFrequency(double F) {
  _Fbfo = F;
  if (_src) {
    _src->setFrequency(_Fif+_F+_Fcorr-_Fbfo);
  }
  _baseband.setCenterFrequency(_Fbfo);
}

bool
WsprRtlSource::sourceAGCEnabled() const {
  if (_src) {
    return _src->agcEnabled();
  }
  return false;
}

void
WsprRtlSource::enableSourceAGC(bool enabled) {
  if (_src) {
    _src->enableAGC(enabled);
  }
}


/* ********************************************************************************************* *
 * Implementation of RTLInputView
 * ********************************************************************************************* */
WsprRtlSourceView::WsprRtlSourceView(WsprRtlSource *input, QWidget *parent)
  : QWidget(parent), _input(input)
{
  _Fcorr = new QLineEdit(QString::number(_input->freqCorrection()));
  QDoubleValidator *fval = new QDoubleValidator();
  _Fcorr->setValidator(fval);

  _rtl_agc = new QCheckBox();
  _rtl_agc->setChecked(_input->sourceAGCEnabled());

  QFormLayout *layout = new QFormLayout();
  layout->addRow("Freq. cor. (Hz)", _Fcorr);
  layout->addRow("AGC", _rtl_agc);
  setLayout(layout);

  QObject::connect(_Fcorr, SIGNAL(returnPressed()), this, SLOT(onFreqCorrEdited()));
  QObject::connect(_rtl_agc, SIGNAL(toggled(bool)), this, SLOT(onSrcAGCToggled(bool)));
}

void
WsprRtlSourceView::onRXFreqCorrChanged(double F) {
  _input->setFreqCorrection(F);
}

void
WsprRtlSourceView::onFreqCorrEdited() {
  bool ok;
  double F = _Fcorr->text().toDouble(&ok);
  if (ok) { _input->setFreqCorrection(F); }
}

void
WsprRtlSourceView::onSrcAGCToggled(bool enabled) {
  _input->enableSourceAGC(enabled);
}


/* ********************************************************************************************* *
 * Implementation of AudioInput
 * ********************************************************************************************* */
WsprAudioSource::WsprAudioSource(double F, double Fbfo, QObject *parent)
  : WSPRSource(F, Fbfo, parent), _src(12e3, 512), _view(0)
{
  sdr::Queue::get().addIdle(&_src, &PortSource<int16_t>::next);
}

WsprAudioSource::~WsprAudioSource() {
  sdr::Queue::get().remIdle(&_src);
  if (_view) { _view->deleteLater(); }
}

QWidget *
WsprAudioSource::createView() {
  if (0 == _view) { _view = new QLabel("No Settings for audio source."); }
  return _view;
}

sdr::Source *
WsprAudioSource::source() {
  return &_src;
}


/* ********************************************************************************************* *
 * Implementation of WsprFileSource
 * ********************************************************************************************* */
WsprFileSource::WsprFileSource(double F, double Fbfo, QObject *parent)
  : WSPRSource(F, Fbfo, parent), _src(), _autocast(), _subsampler(12e3), _view(0)
{
  _src.connect(&_autocast);
  _autocast.connect(&_subsampler, true);
  sdr::Queue::get().addIdle(&_src, &WavSource::next);
}

WsprFileSource::~WsprFileSource() {
  sdr::Queue::get().remIdle(&_src);
  if (_view) { _view->deleteLater(); }
}

QWidget *
WsprFileSource::createView() {
  if (0 == _view) {
    _view = new WsprFileSourceView(this);
  }
  return _view;
}

sdr::Source *
WsprFileSource::source() {
  return &_src;
}

bool
WsprFileSource::openFile(const QString &filename) {
  _src.open(filename.toStdString());
  return _src.isOpen();
}


/* ********************************************************************************************* *
 * Implementation of WsprFileSourceView
 * ********************************************************************************************* */
WsprFileSourceView::WsprFileSourceView(WsprFileSource *src, QWidget *parent)
  : QWidget(parent), _source(src)
{
  _filename = new QLineEdit();
  QPushButton *select = new QPushButton("...");
  QHBoxLayout *hbox = new QHBoxLayout();
  hbox->addWidget(_filename, 1);
  hbox->addWidget(select, 0);

  QFormLayout *layout = new QFormLayout();
  layout->addRow("File", hbox);
  setLayout(layout);

  QObject::connect(select, SIGNAL(clicked()), this, SLOT(onSelectClicked()));
  QObject::connect(_filename, SIGNAL(returnPressed()), this, SLOT(onFileSelected()));
}

void
WsprFileSourceView::onSelectClicked() {
  QString path = QFileDialog::getOpenFileName(0, "Select file", "", "Wave (*.wav)");
  if ("" == path) { return; }
  _filename->setText(path);
  onFileSelected();
}

void
WsprFileSourceView::onFileSelected() {
  bool isRunning = sdr::Queue::get().isRunning();
  if (isRunning) { sdr::Queue::get().stop(); sdr::Queue::get().wait(); }
  _source->openFile(_filename->text());
  if (isRunning) { sdr::Queue::get().start(); }
}