SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
bfmdemod.cpp
Go to the documentation of this file.
1 // Copyright (C) 2015 F4EXB //
3 // written by Edouard Griffiths //
4 // //
5 // This program is free software; you can redistribute it and/or modify //
6 // it under the terms of the GNU General Public License as published by //
7 // the Free Software Foundation as version 3 of the License, or //
8 // (at your option) any later version. //
9 // //
10 // This program is distributed in the hope that it will be useful, //
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
13 // GNU General Public License V3 for more details. //
14 // //
15 // You should have received a copy of the GNU General Public License //
16 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
18 
19 #include "boost/format.hpp"
20 #include <stdio.h>
21 #include <complex.h>
22 
23 #include <QTime>
24 #include <QDebug>
25 #include <QNetworkAccessManager>
26 #include <QNetworkReply>
27 #include <QBuffer>
28 
29 #include "SWGChannelSettings.h"
30 #include "SWGBFMDemodSettings.h"
31 #include "SWGChannelReport.h"
32 #include "SWGBFMDemodReport.h"
33 #include "SWGRDSReport.h"
34 
35 #include "audio/audiooutput.h"
36 #include "dsp/dspengine.h"
37 #include "dsp/downchannelizer.h"
39 #include "dsp/dspcommands.h"
40 #include "device/deviceapi.h"
41 #include "util/db.h"
42 
43 #include "rdsparser.h"
44 #include "bfmdemod.h"
45 
49 
50 const QString BFMDemod::m_channelIdURI = "sdrangel.channel.bfm";
51 const QString BFMDemod::m_channelId = "BFMDemod";
52 const Real BFMDemod::default_deemphasis = 50.0; // 50 us
53 const int BFMDemod::m_udpBlockSize = 512;
54 
56  ChannelAPI(m_channelIdURI, ChannelAPI::StreamSingleSink),
57  m_deviceAPI(deviceAPI),
58  m_inputSampleRate(384000),
59  m_inputFrequencyOffset(0),
60  m_audioFifo(250000),
61  m_settingsMutex(QMutex::Recursive),
62  m_pilotPLL(19000/384000, 50/384000, 0.01),
63  m_deemphasisFilterX(default_deemphasis * 48000 * 1.0e-6),
64  m_deemphasisFilterY(default_deemphasis * 48000 * 1.0e-6),
65  m_fmExcursion(default_excursion)
66 {
67  setObjectName(m_channelId);
68 
71 
72  m_magsq = 0.0f;
73  m_magsqSum = 0.0f;
74  m_magsqPeak = 0.0f;
75  m_magsqCount = 0;
76 
77  m_squelchLevel = 0;
78  m_squelchState = 0;
79 
82 
85 
88 
89  m_sampleSink = 0;
90  m_m1Arg = 0;
91 
92  m_rfFilter = new fftfilt(-50000.0 / 384000.0, 50000.0 / 384000.0, filtFftLen);
93 
97 
98  m_audioBuffer.resize(16384);
100 
102  applySettings(m_settings, true);
103 
104  m_channelizer = new DownChannelizer(this);
108 
109  m_networkManager = new QNetworkAccessManager();
110  connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
111 }
112 
114 {
115  disconnect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
116  delete m_networkManager;
117 
119 
122  delete m_threadedChannelizer;
123  delete m_channelizer;
124  delete m_rfFilter;
125 }
126 
127 void BFMDemod::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool firstOfBurst)
128 {
129  (void) firstOfBurst;
130  Complex ci, cs, cr;
131  fftfilt::cmplx *rf;
132  int rf_out;
133  double msq;
134  Real demod;
135 
136  m_sampleBuffer.clear();
137 
138  m_settingsMutex.lock();
139 
140  for (SampleVector::const_iterator it = begin; it != end; ++it)
141  {
142  Complex c(it->real() / SDR_RX_SCALEF, it->imag() / SDR_RX_SCALEF);
143  c *= m_nco.nextIQ();
144 
145  rf_out = m_rfFilter->runFilt(c, &rf); // filter RF before demod
146 
147  for (int i =0 ; i <rf_out; i++)
148  {
149  msq = rf[i].real()*rf[i].real() + rf[i].imag()*rf[i].imag();
150  m_magsqSum += msq;
151 
152  if (msq > m_magsqPeak) {
153  m_magsqPeak = msq;
154  }
155 
156  m_magsqCount++;
157 
158  if (msq >= m_squelchLevel)
159  {
160  if (m_squelchState < m_settings.m_rfBandwidth / 10) { // twice attack and decay rate
161  m_squelchState++;
162  }
163  }
164  else
165  {
166  if (m_squelchState > 0) {
167  m_squelchState--;
168  }
169  }
170 
171  if (m_squelchState > m_settings.m_rfBandwidth / 20) { // squelch open
172  demod = m_phaseDiscri.phaseDiscriminator(rf[i]);
173  } else {
174  demod = 0;
175  }
176 
177  if (!m_settings.m_showPilot) {
178  m_sampleBuffer.push_back(Sample(demod * SDR_RX_SCALEF, 0.0));
179  }
180 
182  {
183  //Complex r(demod * 2.0 * std::cos(3.0 * m_pilotPLLSamples[3]), 0.0);
184  Complex r(demod * 2.0 * std::cos(3.0 * m_pilotPLLSamples[3]), 0.0);
185 
187  {
188  bool bit;
189 
190  if (m_rdsDemod.process(cr.real(), bit))
191  {
192  if (m_rdsDecoder.frameSync(bit)) {
194  }
195  }
196 
198  }
199  }
200 
201  Real sampleStereo = 0.0f;
202 
203  // Process stereo if stereo mode is selected
204 
206  {
208 
209  if (m_settings.m_showPilot) {
210  m_sampleBuffer.push_back(Sample(m_pilotPLLSamples[1] * SDR_RX_SCALEF, 0.0)); // debug 38 kHz pilot
211  }
212 
214  {
215  // 1.17 * 0.7 = 0.819
216  Complex s(demod * m_pilotPLLSamples[1], demod * m_pilotPLLSamples[2]);
217 
219  {
220  sampleStereo = cs.real() + cs.imag();
222  }
223  }
224  else
225  {
226  Complex s(demod * 1.17 * m_pilotPLLSamples[1], 0);
227 
229  {
230  sampleStereo = cs.real();
232  }
233  }
234  }
235 
236  Complex e(demod, 0);
237 
239  {
241  {
242  Real deemph_l, deemph_r; // Pre-emphasis is applied on each channel before multiplexing
243  m_deemphasisFilterX.process(ci.real() + sampleStereo, deemph_l);
244  m_deemphasisFilterY.process(ci.real() - sampleStereo, deemph_r);
245  m_audioBuffer[m_audioBufferFill].l = (qint16)(deemph_l * (1<<12) * m_settings.m_volume);
246  m_audioBuffer[m_audioBufferFill].r = (qint16)(deemph_r * (1<<12) * m_settings.m_volume);
247  }
248  else
249  {
250  Real deemph;
251  m_deemphasisFilterX.process(ci.real(), deemph);
252  quint16 sample = (qint16)(deemph * (1<<12) * m_settings.m_volume);
253  m_audioBuffer[m_audioBufferFill].l = sample;
254  m_audioBuffer[m_audioBufferFill].r = sample;
255  }
256 
258 
259  if (m_audioBufferFill >= m_audioBuffer.size())
260  {
261  uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
262 
263  if(res != m_audioBufferFill) {
264  qDebug("BFMDemod::feed: %u/%u audio samples written", res, m_audioBufferFill);
265  }
266 
267  m_audioBufferFill = 0;
268  }
269 
271  }
272  }
273  }
274 
275  if (m_audioBufferFill > 0)
276  {
277  uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
278 
279  if (res != m_audioBufferFill) {
280  qDebug("BFMDemod::feed: %u/%u tail samples written", res, m_audioBufferFill);
281  }
282 
283  m_audioBufferFill = 0;
284  }
285 
286  if (m_sampleSink != 0) {
287  m_sampleSink->feed(m_sampleBuffer.begin(), m_sampleBuffer.end(), true);
288  }
289 
290  m_sampleBuffer.clear();
291 
292  m_settingsMutex.unlock();
293 }
294 
296 {
297  m_squelchState = 0;
298  m_audioFifo.clear();
301 }
302 
304 {
305 }
306 
308 {
310  {
312 
313  qDebug() << "BFMDemod::handleMessage: MsgChannelizerNotification:"
314  << " inputSampleRate: " << notif.getSampleRate()
315  << " inputFrequencyOffset: " << notif.getFrequencyOffset();
316 
318 
319  if (getMessageQueueToGUI())
320  {
322  getMessageQueueToGUI()->push(msg);
323  }
324 
325  return true;
326  }
327  else if (MsgConfigureChannelizer::match(cmd))
328  {
330 
331  qDebug() << "BFMDemod::handleMessage: MsgConfigureChannelizer: sampleRate: " << cfg.getSampleRate()
332  << " centerFrequency: " << cfg.getCenterFrequency();
333 
335  cfg.getSampleRate(),
336  cfg.getCenterFrequency());
337 
338  return true;
339  }
340  else if (MsgConfigureBFMDemod::match(cmd))
341  {
343  qDebug() << "BFMDemod::handleMessage: MsgConfigureBFMDemod";
344 
345  applySettings(cfg.getSettings(), cfg.getForce());
346 
347  return true;
348  }
349  else if (DSPConfigureAudio::match(cmd))
350  {
351  DSPConfigureAudio& cfg = (DSPConfigureAudio&) cmd;
352  uint32_t sampleRate = cfg.getSampleRate();
353 
354  qDebug() << "BFMDemod::handleMessage: DSPConfigureAudio:"
355  << " sampleRate: " << sampleRate;
356 
357  if (sampleRate != m_audioSampleRate) {
358  applyAudioSampleRate(sampleRate);
359  }
360 
361  return true;
362  }
364  {
365  return true;
366  }
367  else if (DSPSignalNotification::match(cmd))
368  {
369  return true;
370  }
371  else
372  {
373  qDebug() << "BFMDemod::handleMessage: passed: " << cmd.getIdentifier();
374 
375  if (m_sampleSink != 0)
376  {
377  return m_sampleSink->handleMessage(cmd);
378  }
379  else
380  {
381  return false;
382  }
383  }
384 }
385 
386 void BFMDemod::applyAudioSampleRate(int sampleRate)
387 {
388  qDebug("BFMDemod::applyAudioSampleRate: %d", sampleRate);
389 
390  m_settingsMutex.lock();
391 
395 
399 
400  m_deemphasisFilterX.configure(default_deemphasis * sampleRate * 1.0e-6);
401  m_deemphasisFilterY.configure(default_deemphasis * sampleRate * 1.0e-6);
402 
403  m_settingsMutex.unlock();
404 
405  m_audioSampleRate = sampleRate;
406 }
407 
408 void BFMDemod::applyChannelSettings(int inputSampleRate, int inputFrequencyOffset, bool force)
409 {
410  qDebug() << "BFMDemod::applyChannelSettings:"
411  << " inputSampleRate: " << inputSampleRate
412  << " inputFrequencyOffset: " << inputFrequencyOffset;
413 
414  if((inputFrequencyOffset != m_inputFrequencyOffset) ||
415  (inputSampleRate != m_inputSampleRate) || force)
416  {
417  m_nco.setFreq(-inputFrequencyOffset, inputSampleRate);
418  }
419 
420  if ((inputSampleRate != m_inputSampleRate) || force)
421  {
422  m_pilotPLL.configure(19000.0/inputSampleRate, 50.0/inputSampleRate, 0.01);
423 
424  m_settingsMutex.lock();
425 
426  m_interpolator.create(16, inputSampleRate, m_settings.m_afBandwidth);
428  m_interpolatorDistance = (Real) inputSampleRate / (Real) m_audioSampleRate;
429 
430  m_interpolatorStereo.create(16, inputSampleRate, m_settings.m_afBandwidth);
433 
434  m_interpolatorRDS.create(4, inputSampleRate, 600.0);
435  m_interpolatorRDSDistanceRemain = (Real) inputSampleRate / 250000.0;
436  m_interpolatorRDSDistance = (Real) inputSampleRate / 250000.0;
437 
438  Real lowCut = -(m_settings.m_rfBandwidth / 2.0) / inputSampleRate;
439  Real hiCut = (m_settings.m_rfBandwidth / 2.0) / inputSampleRate;
440  m_rfFilter->create_filter(lowCut, hiCut);
441  m_phaseDiscri.setFMScaling(inputSampleRate / m_fmExcursion);
442 
443  m_settingsMutex.unlock();
444  }
445 
446  m_inputSampleRate = inputSampleRate;
447  m_inputFrequencyOffset = inputFrequencyOffset;
448 }
449 
450 void BFMDemod::applySettings(const BFMDemodSettings& settings, bool force)
451 {
452  qDebug() << "BFMDemod::applySettings: MsgConfigureBFMDemod:"
453  << " m_inputFrequencyOffset: " << settings.m_inputFrequencyOffset
454  << " m_rfBandwidth: " << settings.m_rfBandwidth
455  << " m_afBandwidth: " << settings.m_afBandwidth
456  << " m_volume: " << settings.m_volume
457  << " m_squelch: " << settings.m_squelch
458  << " m_audioStereo: " << settings.m_audioStereo
459  << " m_lsbStereo: " << settings.m_lsbStereo
460  << " m_showPilot: " << settings.m_showPilot
461  << " m_rdsActive: " << settings.m_rdsActive
462  << " m_audioDeviceName: " << settings.m_audioDeviceName
463  << " m_useReverseAPI: " << settings.m_useReverseAPI
464  << " force: " << force;
465 
466  QList<QString> reverseAPIKeys;
467 
468  if ((settings.m_inputFrequencyOffset != m_settings.m_inputFrequencyOffset) || force) {
469  reverseAPIKeys.append("inputFrequencyOffset");
470  }
471  if ((settings.m_volume != m_settings.m_volume) || force) {
472  reverseAPIKeys.append("volume");
473  }
474  if ((settings.m_audioStereo != m_settings.m_audioStereo) || force) {
475  reverseAPIKeys.append("audioStereo");
476  }
477  if ((settings.m_lsbStereo != m_settings.m_lsbStereo) || force) {
478  reverseAPIKeys.append("lsbStereo");
479  }
480  if ((settings.m_showPilot != m_settings.m_showPilot) || force) {
481  reverseAPIKeys.append("showPilot");
482  }
483  if ((settings.m_rdsActive != m_settings.m_rdsActive) || force) {
484  reverseAPIKeys.append("rdsActive");
485  }
486 
487  if ((settings.m_audioStereo && (settings.m_audioStereo != m_settings.m_audioStereo)) || force)
488  {
490  }
491 
492  if ((settings.m_afBandwidth != m_settings.m_afBandwidth) || force)
493  {
494  reverseAPIKeys.append("afBandwidth");
495  m_settingsMutex.lock();
496 
500 
504 
508 
510 
511  m_settingsMutex.unlock();
512  }
513 
514  if ((settings.m_rfBandwidth != m_settings.m_rfBandwidth) || force)
515  {
516  reverseAPIKeys.append("rfBandwidth");
517  m_settingsMutex.lock();
518  Real lowCut = -(settings.m_rfBandwidth / 2.0) / m_inputSampleRate;
519  Real hiCut = (settings.m_rfBandwidth / 2.0) / m_inputSampleRate;
520  m_rfFilter->create_filter(lowCut, hiCut);
522  m_settingsMutex.unlock();
523  }
524 
525  if ((settings.m_squelch != m_settings.m_squelch) || force)
526  {
527  reverseAPIKeys.append("squelch");
528  m_squelchLevel = std::pow(10.0, settings.m_squelch / 10.0);
529  }
530 
531  if ((settings.m_audioDeviceName != m_settings.m_audioDeviceName) || force)
532  {
533  reverseAPIKeys.append("audioDeviceName");
535  int audioDeviceIndex = audioDeviceManager->getOutputDeviceIndex(settings.m_audioDeviceName);
536  //qDebug("AMDemod::applySettings: audioDeviceName: %s audioDeviceIndex: %d", qPrintable(settings.m_audioDeviceName), audioDeviceIndex);
537  audioDeviceManager->addAudioSink(&m_audioFifo, getInputMessageQueue(), audioDeviceIndex);
538  uint32_t audioSampleRate = audioDeviceManager->getOutputSampleRate(audioDeviceIndex);
539 
540  if (m_audioSampleRate != audioSampleRate) {
541  applyAudioSampleRate(audioSampleRate);
542  }
543  }
544 
545  if (settings.m_useReverseAPI)
546  {
547  bool fullUpdate = ((m_settings.m_useReverseAPI != settings.m_useReverseAPI) && settings.m_useReverseAPI) ||
552  webapiReverseSendSettings(reverseAPIKeys, settings, fullUpdate || force);
553  }
554 
555  m_settings = settings;
556 }
557 
558 QByteArray BFMDemod::serialize() const
559 {
560  return m_settings.serialize();
561 }
562 
563 bool BFMDemod::deserialize(const QByteArray& data)
564 {
565  if (m_settings.deserialize(data))
566  {
569  return true;
570  }
571  else
572  {
576  return false;
577  }
578 }
579 
582  QString& errorMessage)
583 {
584  (void) errorMessage;
586  response.getBfmDemodSettings()->init();
588  return 200;
589 }
590 
592  bool force,
593  const QStringList& channelSettingsKeys,
595  QString& errorMessage)
596 {
597  (void) errorMessage;
598  BFMDemodSettings settings = m_settings;
599  bool frequencyOffsetChanged = false;
600 
601  if (channelSettingsKeys.contains("inputFrequencyOffset"))
602  {
604  frequencyOffsetChanged = true;
605  }
606  if (channelSettingsKeys.contains("rfBandwidth")) {
607  settings.m_rfBandwidth = response.getBfmDemodSettings()->getRfBandwidth();
608  }
609  if (channelSettingsKeys.contains("afBandwidth")) {
610  settings.m_afBandwidth = response.getBfmDemodSettings()->getAfBandwidth();
611  }
612  if (channelSettingsKeys.contains("volume")) {
613  settings.m_volume = response.getBfmDemodSettings()->getVolume();
614  }
615  if (channelSettingsKeys.contains("squelch")) {
616  settings.m_squelch = response.getBfmDemodSettings()->getSquelch();
617  }
618  if (channelSettingsKeys.contains("audioStereo")) {
619  settings.m_audioStereo = response.getBfmDemodSettings()->getAudioStereo() != 0;
620  }
621  if (channelSettingsKeys.contains("lsbStereo")) {
622  settings.m_lsbStereo = response.getBfmDemodSettings()->getLsbStereo() != 0;
623  }
624  if (channelSettingsKeys.contains("showPilot")) {
625  settings.m_showPilot = response.getBfmDemodSettings()->getShowPilot() != 0;
626  }
627  if (channelSettingsKeys.contains("rdsActive")) {
628  settings.m_rdsActive = response.getBfmDemodSettings()->getRdsActive() != 0;
629  }
630  if (channelSettingsKeys.contains("rgbColor")) {
631  settings.m_rgbColor = response.getBfmDemodSettings()->getRgbColor();
632  }
633  if (channelSettingsKeys.contains("title")) {
634  settings.m_title = *response.getBfmDemodSettings()->getTitle();
635  }
636  if (channelSettingsKeys.contains("audioDeviceName")) {
637  settings.m_audioDeviceName = *response.getBfmDemodSettings()->getAudioDeviceName();
638  }
639  if (channelSettingsKeys.contains("useReverseAPI")) {
640  settings.m_useReverseAPI = response.getBfmDemodSettings()->getUseReverseApi() != 0;
641  }
642  if (channelSettingsKeys.contains("reverseAPIAddress")) {
644  }
645  if (channelSettingsKeys.contains("reverseAPIPort")) {
646  settings.m_reverseAPIPort = response.getBfmDemodSettings()->getReverseApiPort();
647  }
648  if (channelSettingsKeys.contains("reverseAPIDeviceIndex")) {
650  }
651  if (channelSettingsKeys.contains("reverseAPIChannelIndex")) {
653  }
654 
655  if (frequencyOffsetChanged)
656  {
658  requiredBW(settings.m_rfBandwidth), settings.m_inputFrequencyOffset);
659  m_inputMessageQueue.push(channelConfigMsg);
660  }
661 
662  MsgConfigureBFMDemod *msg = MsgConfigureBFMDemod::create(settings, force);
664 
665  qDebug("BFMDemod::webapiSettingsPutPatch: forward to GUI: %p", m_guiMessageQueue);
666  if (m_guiMessageQueue) // forward to GUI if any
667  {
668  MsgConfigureBFMDemod *msgToGUI = MsgConfigureBFMDemod::create(settings, force);
669  m_guiMessageQueue->push(msgToGUI);
670  }
671 
672  webapiFormatChannelSettings(response, settings);
673 
674  return 200;
675 }
676 
679  QString& errorMessage)
680 {
681  (void) errorMessage;
683  response.getBfmDemodReport()->init();
684  webapiFormatChannelReport(response);
685  return 200;
686 }
687 
689 {
691  response.getBfmDemodSettings()->setRfBandwidth(settings.m_rfBandwidth);
692  response.getBfmDemodSettings()->setAfBandwidth(settings.m_afBandwidth);
693  response.getBfmDemodSettings()->setVolume(settings.m_volume);
694  response.getBfmDemodSettings()->setSquelch(settings.m_squelch);
695  response.getBfmDemodSettings()->setAudioStereo(settings.m_audioStereo ? 1 : 0);
696  response.getBfmDemodSettings()->setLsbStereo(settings.m_lsbStereo ? 1 : 0);
697  response.getBfmDemodSettings()->setShowPilot(settings.m_showPilot ? 1 : 0);
698  response.getBfmDemodSettings()->setRdsActive(settings.m_rdsActive ? 1 : 0);
699  response.getBfmDemodSettings()->setRgbColor(settings.m_rgbColor);
700 
701  if (response.getBfmDemodSettings()->getTitle()) {
702  *response.getBfmDemodSettings()->getTitle() = settings.m_title;
703  } else {
704  response.getBfmDemodSettings()->setTitle(new QString(settings.m_title));
705  }
706 
707  if (response.getBfmDemodSettings()->getAudioDeviceName()) {
708  *response.getBfmDemodSettings()->getAudioDeviceName() = settings.m_audioDeviceName;
709  } else {
710  response.getBfmDemodSettings()->setAudioDeviceName(new QString(settings.m_audioDeviceName));
711  }
712 
713  response.getBfmDemodSettings()->setUseReverseApi(settings.m_useReverseAPI ? 1 : 0);
714 
715  if (response.getBfmDemodSettings()->getReverseApiAddress()) {
717  } else {
718  response.getBfmDemodSettings()->setReverseApiAddress(new QString(settings.m_reverseAPIAddress));
719  }
720 
724 }
725 
727 {
728  double magsqAvg, magsqPeak;
729  int nbMagsqSamples;
730  getMagSqLevels(magsqAvg, magsqPeak, nbMagsqSamples);
731 
732  response.getBfmDemodReport()->setChannelPowerDb(CalcDb::dbPower(magsqAvg));
733  response.getBfmDemodReport()->setSquelch(m_squelchState > 0 ? 1 : 0);
736  response.getBfmDemodReport()->setPilotLocked(getPilotLock() ? 1 : 0);
738 
740  {
743  }
744  else
745  {
746  response.getBfmDemodReport()->setRdsReport(0);
747  }
748 }
749 
751 {
752  report->setDemodStatus(round(getDemodQua()));
753  report->setDecodStatus(round(getDecoderQua()));
754  report->setRdsDemodAccumDb(CalcDb::dbPower(std::fabs(getDemodAcc())));
756  report->setPid(new QString(str(boost::format("%04X") % getRDSParser().m_pi_program_identification).c_str()));
757  report->setPiType(new QString(getRDSParser().pty_table[getRDSParser().m_pi_program_type].c_str()));
758  report->setPiCoverage(new QString(getRDSParser().coverage_area_codes[getRDSParser().m_pi_area_coverage_index].c_str()));
759  report->setProgServiceName(new QString(getRDSParser().m_g0_program_service_name));
760  report->setMusicSpeech(new QString((getRDSParser().m_g0_music_speech ? "Music" : "Speech")));
761  report->setMonoStereo(new QString((getRDSParser().m_g0_mono_stereo ? "Mono" : "Stereo")));
762  report->setRadioText(new QString(getRDSParser().m_g2_radiotext));
763  std::string time = str(boost::format("%4i-%02i-%02i %02i:%02i (%+.1fh)")\
764  % (1900 + getRDSParser().m_g4_year) % getRDSParser().m_g4_month % getRDSParser().m_g4_day % getRDSParser().m_g4_hours % getRDSParser().m_g4_minutes % getRDSParser().m_g4_local_time_offset);
765  report->setTime(new QString(time.c_str()));
766  report->setAltFrequencies(new QList<SWGSDRangel::SWGRDSReport_altFrequencies*>);
767 
768  for (std::set<double>::iterator it = getRDSParser().m_g0_alt_freq.begin(); it != getRDSParser().m_g0_alt_freq.end(); ++it)
769  {
770  if (*it > 76.0)
771  {
773  report->getAltFrequencies()->back()->setFrequency(*it);
774  }
775  }
776 }
777 
778 void BFMDemod::webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const BFMDemodSettings& settings, bool force)
779 {
781  swgChannelSettings->setDirection(0); // single sink (Rx)
782  swgChannelSettings->setOriginatorChannelIndex(getIndexInDeviceSet());
783  swgChannelSettings->setOriginatorDeviceSetIndex(getDeviceSetIndex());
784  swgChannelSettings->setChannelType(new QString("BFMDemod"));
785  swgChannelSettings->setBfmDemodSettings(new SWGSDRangel::SWGBFMDemodSettings());
786  SWGSDRangel::SWGBFMDemodSettings *swgBFMDemodSettings = swgChannelSettings->getBfmDemodSettings();
787 
788  // transfer data that has been modified. When force is on transfer all data except reverse API data
789 
790  if (channelSettingsKeys.contains("inputFrequencyOffset") || force) {
791  swgBFMDemodSettings->setInputFrequencyOffset(settings.m_inputFrequencyOffset);
792  }
793  if (channelSettingsKeys.contains("rfBandwidth") || force) {
794  swgBFMDemodSettings->setRfBandwidth(settings.m_rfBandwidth);
795  }
796  if (channelSettingsKeys.contains("afBandwidth") || force) {
797  swgBFMDemodSettings->setAfBandwidth(settings.m_afBandwidth);
798  }
799  if (channelSettingsKeys.contains("volume") || force) {
800  swgBFMDemodSettings->setVolume(settings.m_volume);
801  }
802  if (channelSettingsKeys.contains("squelch") || force) {
803  swgBFMDemodSettings->setSquelch(settings.m_squelch);
804  }
805  if (channelSettingsKeys.contains("audioStereo") || force) {
806  swgBFMDemodSettings->setAudioStereo(settings.m_audioStereo ? 1 : 0);
807  }
808  if (channelSettingsKeys.contains("lsbStereo") || force) {
809  swgBFMDemodSettings->setLsbStereo(settings.m_lsbStereo ? 1 : 0);
810  }
811  if (channelSettingsKeys.contains("showPilot") || force) {
812  swgBFMDemodSettings->setShowPilot(settings.m_showPilot ? 1 : 0);
813  }
814  if (channelSettingsKeys.contains("rdsActive") || force) {
815  swgBFMDemodSettings->setRdsActive(settings.m_rdsActive ? 1 : 0);
816  }
817  if (channelSettingsKeys.contains("rgbColor") || force) {
818  swgBFMDemodSettings->setRgbColor(settings.m_rgbColor);
819  }
820  if (channelSettingsKeys.contains("title") || force) {
821  swgBFMDemodSettings->setTitle(new QString(settings.m_title));
822  }
823  if (channelSettingsKeys.contains("audioDeviceName") || force) {
824  swgBFMDemodSettings->setAudioDeviceName(new QString(settings.m_audioDeviceName));
825  }
826 
827  QString channelSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/channel/%4/settings")
828  .arg(settings.m_reverseAPIAddress)
829  .arg(settings.m_reverseAPIPort)
830  .arg(settings.m_reverseAPIDeviceIndex)
831  .arg(settings.m_reverseAPIChannelIndex);
832  m_networkRequest.setUrl(QUrl(channelSettingsURL));
833  m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
834 
835  QBuffer *buffer=new QBuffer();
836  buffer->open((QBuffer::ReadWrite));
837  buffer->write(swgChannelSettings->asJson().toUtf8());
838  buffer->seek(0);
839 
840  // Always use PATCH to avoid passing reverse API settings
841  m_networkManager->sendCustomRequest(m_networkRequest, "PATCH", buffer);
842 
843  delete swgChannelSettings;
844 }
845 
846 void BFMDemod::networkManagerFinished(QNetworkReply *reply)
847 {
848  QNetworkReply::NetworkError replyError = reply->error();
849 
850  if (replyError)
851  {
852  qWarning() << "BFMDemod::networkManagerFinished:"
853  << " error(" << (int) replyError
854  << "): " << replyError
855  << ": " << reply->errorString();
856  return;
857  }
858 
859  QString answer = reply->readAll();
860  answer.chop(1); // remove last \n
861  qDebug("BFMDemod::networkManagerFinished: reply:\n%s", answer.toStdString().c_str());
862 }
void setAltFrequencies(QList< SWGRDSReport_altFrequencies *> *alt_frequencies)
void setRadioText(QString *radio_text)
void setOriginatorChannelIndex(qint32 originator_channel_index)
void addAudioSink(AudioFifo *audioFifo, MessageQueue *sampleSinkMessageQueue, int outputDeviceIndex=-1)
Add the audio sink.
fftfilt * m_rfFilter
Definition: bfmdemod.h:248
QString m_reverseAPIAddress
int getOutputSampleRate(int outputDeviceIndex=-1)
bool process(Real rdsSample, bool &bit)
Definition: rdsdemod.cpp:75
Complex nextIQ()
Return next complex sample.
Definition: nco.cpp:61
bool decimate(Real *distance, const Complex &next, Complex *result)
Definition: interpolator.h:38
void configure(MessageQueue *messageQueue, int sampleRate, int centerFrequency)
Real m_interpolatorRDSDistanceRemain
Definition: bfmdemod.h:245
Real m_pilotPLLSamples[4]
Definition: bfmdemod.h:271
void setReverseApiPort(qint32 reverse_api_port)
void setBfmDemodSettings(SWGBFMDemodSettings *bfm_demod_settings)
void setUseReverseApi(qint32 use_reverse_api)
void push(Message *message, bool emitSignal=true)
Push message onto queue.
void setProgServiceName(QString *prog_service_name)
void webapiFormatRDSReport(SWGSDRangel::SWGRDSReport *report)
Definition: bfmdemod.cpp:750
Fixed< IntType, IntBits > cos(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2271
BFMDemod(DeviceAPI *deviceAPI)
Definition: bfmdemod.cpp:55
bool deserialize(const QByteArray &data)
SWGBFMDemodReport * getBfmDemodReport()
void parseGroup(unsigned int *group)
Definition: rdsparser.cpp:374
void removeChannelSinkAPI(ChannelAPI *channelAPI, int streamIndex=0)
Definition: deviceapi.cpp:163
QMutex m_settingsMutex
Definition: bfmdemod.h:268
uint16_t m_reverseAPIChannelIndex
static double dbPower(double magsq, double floor=1e-12)
Definition: db.cpp:22
Interpolator m_interpolator
Interpolator between fixed demod bandwidth and audio bandwidth (rational)
Definition: bfmdemod.h:235
void addChannelSinkAPI(ChannelAPI *channelAPI, int streamIndex=0)
Definition: deviceapi.cpp:156
void setChannelSampleRate(qint32 channel_sample_rate)
static const QString m_channelIdURI
Definition: bfmdemod.h:206
quint32 m_audioSampleRate
Definition: bfmdemod.h:232
qint64 m_inputFrequencyOffset
QList< SWGRDSReport_altFrequencies * > * getAltFrequencies()
AudioVector m_audioBuffer
Definition: bfmdemod.h:262
void create(int phaseSteps, double sampleRate, double cutoff, double nbTapsPerPhase=4.5)
virtual bool deserialize(const QByteArray &data)
Definition: bfmdemod.cpp:563
MessageQueue * getInputMessageQueue()
Get the queue for asynchronous inbound communication.
void setFMScaling(Real fmScaling)
Definition: phasediscri.h:42
unsigned int * getGroup()
Definition: rdsdecoder.h:29
void configure(Real timeout)
Definition: filterrc.cpp:32
static MsgReportChannelSampleRateChanged * create(int sampleRate)
Definition: bfmdemod.h:110
int m_magsqCount
Definition: bfmdemod.h:259
void clear()
Definition: audiofifo.cpp:156
void create(int nTaps, double sampleRate, double cutoff)
Definition: lowpass.h:15
static const Real default_deemphasis
Definition: bfmdemod.h:279
Real getDemodAcc() const
Definition: bfmdemod.h:159
int getDeviceSetIndex() const
Definition: channelapi.h:89
void process(const Real &sample_in, Real &sample_out)
Definition: filterrc.cpp:45
int getOutputDeviceIndex(const QString &deviceName) const
void configure(Real freq, Real bandwidth, Real minsignal)
Definition: phaselock.cpp:87
int runFilt(const cmplx &in, cmplx **out)
Definition: fftfilt.cpp:260
void removeAudioSink(AudioFifo *audioFifo)
Remove the audio sink.
static const QString m_channelId
Definition: bfmdemod.h:207
void webapiFormatChannelReport(SWGSDRangel::SWGChannelReport &response)
Definition: bfmdemod.cpp:726
LowPassFilterRC m_deemphasisFilterY
Definition: bfmdemod.h:278
static MsgConfigureChannelizer * create(int sampleRate, int centerFrequency)
Definition: bfmdemod.h:88
void setAudioDeviceName(QString *audio_device_name)
virtual void start()
Definition: bfmdemod.cpp:295
void setDemodStatus(qint32 demod_status)
double m_magsqPeak
Definition: bfmdemod.h:258
virtual bool handleMessage(const Message &cmd)
Processing of a message. Returns true if message has actually been processed.
Definition: bfmdemod.cpp:307
void setChannelType(QString *channel_type)
virtual void feed(const SampleVector::const_iterator &begin, const SampleVector::const_iterator &end, bool positiveOnly)=0
virtual void feed(const SampleVector::const_iterator &begin, const SampleVector::const_iterator &end, bool po)
Definition: bfmdemod.cpp:127
void setOriginatorDeviceSetIndex(qint32 originator_device_set_index)
MessageQueue m_inputMessageQueue
Queue for asynchronous inbound communication.
Real m_interpolatorDistanceRemain
Definition: bfmdemod.h:237
DeviceAPI * m_deviceAPI
Definition: bfmdemod.h:225
uint16_t m_reverseAPIDeviceIndex
int m_squelchState
Definition: bfmdemod.h:252
std::complex< float > cmplx
Definition: fftfilt.h:21
static int requiredBW(int rfBW)
Definition: bfmdemod.h:197
unsigned int uint32_t
Definition: rtptypes_win.h:46
#define SDR_RX_SCALEF
Definition: dsptypes.h:33
ThreadedBasebandSampleSink * m_threadedChannelizer
Definition: bfmdemod.h:226
QByteArray serialize() const
void setReverseApiDeviceIndex(qint32 reverse_api_device_index)
void setRdsDemodFrequency(float rds_demod_frequency)
void applyAudioSampleRate(int sampleRate)
Definition: bfmdemod.cpp:386
MessageQueue * m_guiMessageQueue
Input message queue to the GUI.
virtual bool handleMessage(const Message &cmd)=0
Processing of a message. Returns true if message has actually been processed.
NCO m_nco
Definition: bfmdemod.h:234
AudioFifo m_audioFifo
Definition: bfmdemod.h:266
SWGBFMDemodSettings * getBfmDemodSettings()
#define MESSAGE_CLASS_DEFINITION(Name, BaseClass)
Definition: message.h:52
int m_inputSampleRate
Definition: bfmdemod.h:229
virtual int webapiSettingsPutPatch(bool force, const QStringList &channelSettingsKeys, SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
Definition: bfmdemod.cpp:591
void create_filter(float f1, float f2)
Definition: fftfilt.cpp:107
double m_magsqSum
Definition: bfmdemod.h:257
void setAudioSampleRate(qint32 audio_sample_rate)
int getSampleRate() const
Definition: dspcommands.h:390
static DSPEngine * instance()
Definition: dspengine.cpp:51
SampleVector m_sampleBuffer
Definition: bfmdemod.h:267
LowPassFilterRC m_deemphasisFilterX
Definition: bfmdemod.h:277
std::set< double > m_g0_alt_freq
Definition: rdsparser.h:67
void process(const std::vector< Real > &samples_in, std::vector< Real > &samples_out)
Definition: phaselock.cpp:148
void setBfmDemodReport(SWGBFMDemodReport *bfm_demod_report)
uint16_t m_reverseAPIPort
void setPilotPowerDb(float pilot_power_db)
int32_t i
Definition: decimators.h:244
void webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings &response, const BFMDemodSettings &settings)
Definition: bfmdemod.cpp:688
DownChannelizer * m_channelizer
Definition: bfmdemod.h:227
Real phaseDiscriminator(const Complex &sample)
Definition: phasediscri.h:51
static bool match(const Message *message)
Definition: message.cpp:45
void setDecodStatus(qint32 decod_status)
void setFreq(Real freq, Real sampleRate)
Definition: nco.cpp:49
Interpolator m_interpolatorStereo
Twin Interpolator for stereo subcarrier.
Definition: bfmdemod.h:239
void setRdsDemodAccumDb(float rds_demod_accum_db)
Real m_m1Arg
Definition: bfmdemod.h:254
int m_inputFrequencyOffset
Definition: bfmdemod.h:230
virtual void stop()
Definition: bfmdemod.cpp:303
Real m_fmExcursion
Definition: bfmdemod.h:281
void removeChannelSink(ThreadedBasebandSampleSink *sink, int streamIndex=0)
Remove a channel sink (Rx)
Definition: deviceapi.cpp:127
static const int m_udpBlockSize
Definition: bfmdemod.h:286
MessageQueue * getMessageQueueToGUI()
void applySettings(const BFMDemodSettings &settings, bool force=false)
Definition: bfmdemod.cpp:450
RDSParser m_rdsParser
Definition: bfmdemod.h:275
uint m_audioBufferFill
Definition: bfmdemod.h:263
virtual int webapiSettingsGet(SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
Definition: bfmdemod.cpp:580
virtual QString asJson() override
BFMDemodSettings m_settings
Definition: bfmdemod.h:231
void setChannelPowerDb(float channel_power_db)
static MsgConfigureBFMDemod * create(const BFMDemodSettings &settings, bool force)
Definition: bfmdemod.h:65
Real m_interpolatorRDSDistance
Definition: bfmdemod.h:244
AudioDeviceManager * getAudioDeviceManager()
Definition: dspengine.h:55
static const int filtFftLen
Definition: bfmdemod.h:249
Real getDemodQua() const
Definition: bfmdemod.h:160
void addChannelSink(ThreadedBasebandSampleSink *sink, int streamIndex=0)
Add a channel sink (Rx)
Definition: deviceapi.cpp:118
void setReverseApiAddress(QString *reverse_api_address)
void setMonoStereo(QString *mono_stereo)
Real m_interpolatorStereoDistanceRemain
Definition: bfmdemod.h:241
void setPilotLocked(qint32 pilot_locked)
Real getDecoderQua() const
Definition: bfmdemod.h:157
void setPiCoverage(QString *pi_coverage)
Real getDemodFclk() const
Definition: bfmdemod.h:161
Real getPilotLevel() const
Definition: bfmdemod.h:155
void getMagSqLevels(double &avg, double &peak, int &nbSamples)
Definition: bfmdemod.h:163
void webapiReverseSendSettings(QList< QString > &channelSettingsKeys, const BFMDemodSettings &settings, bool force)
Definition: bfmdemod.cpp:778
Lowpass< Real > m_lowpass
Definition: bfmdemod.h:247
virtual const char * getIdentifier() const
Definition: message.cpp:35
void setRdsReport(SWGRDSReport *rds_report)
bool frameSync(bool bit)
Definition: rdsdecoder.cpp:51
Interpolator m_interpolatorRDS
Twin Interpolator for stereo subcarrier.
Definition: bfmdemod.h:243
PhaseDiscriminators m_phaseDiscri
Definition: bfmdemod.h:284
void setInputFrequencyOffset(qint64 input_frequency_offset)
virtual QByteArray serialize() const
Definition: bfmdemod.cpp:558
double m_magsq
x^-1 real sample
Definition: bfmdemod.h:256
void setPiType(QString *pi_type)
QNetworkAccessManager * m_networkManager
Definition: bfmdemod.h:288
const BFMDemodSettings & getSettings() const
Definition: bfmdemod.h:62
void setAudioStereo(qint32 audio_stereo)
Real m_interpolatorDistance
Definition: bfmdemod.h:236
RDSDemod m_rdsDemod
Definition: bfmdemod.h:273
void setTime(QString *time)
void setPid(QString *pid)
RDSParser & getRDSParser()
Definition: bfmdemod.h:181
void applyChannelSettings(int inputSampleRate, int inputFrequencyOffset, bool force=false)
Definition: bfmdemod.cpp:408
int getIndexInDeviceSet() const
Definition: channelapi.h:87
virtual ~BFMDemod()
Definition: bfmdemod.cpp:113
std::complex< Real > Complex
Definition: dsptypes.h:43
Real m_interpolatorStereoDistance
Definition: bfmdemod.h:240
QNetworkRequest m_networkRequest
Definition: bfmdemod.h:289
int getSampleRate() const
Definition: bfmdemod.h:129
float Real
Definition: dsptypes.h:42
void setReverseApiChannelIndex(qint32 reverse_api_channel_index)
RDSDecoder m_rdsDecoder
Definition: bfmdemod.h:274
bool getPilotLock() const
Definition: bfmdemod.h:154
virtual int webapiReportGet(SWGSDRangel::SWGChannelReport &response, QString &errorMessage)
Definition: bfmdemod.cpp:677
RDSPhaseLock m_pilotPLL
Definition: bfmdemod.h:270
void networkManagerFinished(QNetworkReply *reply)
Definition: bfmdemod.cpp:846
BasebandSampleSink * m_sampleSink
Definition: bfmdemod.h:265
uint32_t write(const quint8 *data, uint32_t numSamples)
Definition: audiofifo.cpp:66
Real m_squelchLevel
Definition: bfmdemod.h:251
void setAfBandwidth(float af_bandwidth)
void setRfBandwidth(float rf_bandwidth)
void setMusicSpeech(QString *music_speech)