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.
amdemod.cpp
Go to the documentation of this file.
1 // Copyright (C) 2015-2018 Edouard Griffiths, F4EXB. //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include "amdemod.h"
19 
20 #include <QTime>
21 #include <QDebug>
22 #include <QNetworkAccessManager>
23 #include <QNetworkReply>
24 #include <QBuffer>
25 
26 #include <stdio.h>
27 #include <complex.h>
28 
29 #include "SWGChannelSettings.h"
30 #include "SWGAMDemodSettings.h"
31 #include "SWGChannelReport.h"
32 #include "SWGAMDemodReport.h"
33 
34 #include "dsp/downchannelizer.h"
35 #include "audio/audiooutput.h"
36 #include "dsp/dspengine.h"
38 #include "dsp/dspcommands.h"
39 #include "dsp/devicesamplemimo.h"
40 #include "dsp/fftfilt.h"
41 #include "device/deviceapi.h"
42 #include "util/db.h"
43 #include "util/stepfunctions.h"
44 
47 
48 const QString AMDemod::m_channelIdURI = "sdrangel.channel.amdemod";
49 const QString AMDemod::m_channelId = "AMDemod";
50 const int AMDemod::m_udpBlockSize = 512;
51 
53  ChannelAPI(m_channelIdURI, ChannelAPI::StreamSingleSink),
54  m_deviceAPI(deviceAPI),
55  m_inputSampleRate(48000),
56  m_inputFrequencyOffset(0),
57  m_running(false),
58  m_squelchOpen(false),
59  m_squelchDelayLine(9600),
60  m_magsqSum(0.0f),
61  m_magsqPeak(0.0f),
62  m_magsqCount(0),
63  m_volumeAGC(0.003),
64  m_syncAMAGC(12000, 0.1, 1e-2),
65  m_audioFifo(48000),
66  m_settingsMutex(QMutex::Recursive)
67 {
68  setObjectName(m_channelId);
69 
70  m_audioBuffer.resize(1<<14);
72 
73  m_magsq = 0.0;
74 
77  DSBFilter = new fftfilt((2.0f * m_settings.m_rfBandwidth) / m_audioSampleRate, 2 * 1024);
80  m_syncAMAGC.resize(12000, 6000, 0.1);
81 
84 
85  m_channelizer = new DownChannelizer(this);
89 
90  m_pllFilt.create(101, m_audioSampleRate, 200.0);
91  m_pll.computeCoefficients(0.05, 0.707, 1000);
93 
94  m_networkManager = new QNetworkAccessManager();
95  connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
96 }
97 
99 {
100  disconnect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*)));
101  delete m_networkManager;
105  delete m_threadedChannelizer;
106  delete m_channelizer;
107  delete DSBFilter;
108  delete SSBFilter;
109 }
110 
112 {
114 }
115 
116 void AMDemod::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool firstOfBurst)
117 {
118  (void) firstOfBurst;
119  Complex ci;
120 
121  if (!m_running) {
122  return;
123  }
124 
125  m_settingsMutex.lock();
126 
127  for (SampleVector::const_iterator it = begin; it != end; ++it)
128  {
129  Complex c(it->real(), it->imag());
130  c *= m_nco.nextIQ();
131 
132  if (m_interpolatorDistance < 1.0f) // interpolate
133  {
135  {
136  processOneSample(ci);
138  }
139  }
140  else // decimate
141  {
143  {
144  processOneSample(ci);
146  }
147  }
148  }
149 
150  if (m_audioBufferFill > 0)
151  {
152  uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
153 
154  if (res != m_audioBufferFill)
155  {
156  qDebug("AMDemod::feed: %u/%u tail samples written", res, m_audioBufferFill);
157  }
158 
159  m_audioBufferFill = 0;
160  }
161 
162  m_settingsMutex.unlock();
163 }
164 
166 {
167  Real re = ci.real() / SDR_RX_SCALEF;
168  Real im = ci.imag() / SDR_RX_SCALEF;
169  Real magsq = re*re + im*im;
170  m_movingAverage(magsq);
172  m_magsqSum += magsq;
173 
174  if (magsq > m_magsqPeak)
175  {
176  m_magsqPeak = magsq;
177  }
178 
179  m_magsqCount++;
180 
181  m_squelchDelayLine.write(magsq);
182 
183  if (m_magsq < m_squelchLevel)
184  {
185  if (m_squelchCount > 0) {
186  m_squelchCount--;
187  }
188  }
189  else
190  {
191  if (m_squelchCount < m_audioSampleRate / 10) {
192  m_squelchCount++;
193  }
194  }
195 
196  qint16 sample;
197 
199 
201  {
202  Real demod;
203 
204  if (m_settings.m_pll)
205  {
206  std::complex<float> s(re, im);
207  s = m_pllFilt.filter(s);
208  m_pll.feed(s.real(), s.imag());
209  float yr = re * m_pll.getImag() - im * m_pll.getReal();
210  float yi = re * m_pll.getReal() + im * m_pll.getImag();
211 
212  fftfilt::cmplx *sideband;
213  std::complex<float> cs(yr, yi);
214  int n_out;
215 
217  n_out = DSBFilter->runDSB(cs, &sideband, false);
218  } else {
219  n_out = SSBFilter->runSSB(cs, &sideband, m_settings.m_syncAMOperation == AMDemodSettings::SyncAMUSB, false);
220  }
221 
222  for (int i = 0; i < n_out; i++)
223  {
224  float agcVal = m_syncAMAGC.feedAndGetValue(sideband[i]);
225  fftfilt::cmplx z = sideband[i] * agcVal; // * m_syncAMAGC.getStepValue();
226 
228  m_syncAMBuff[i] = (z.real() + z.imag());
230  m_syncAMBuff[i] = (z.real() + z.imag());
231  } else {
232  m_syncAMBuff[i] = (z.real() + z.imag());
233  }
234 
235 // if (m_settings.m_syncAMOperation == AMDemodSettings::SyncAMDSB) {
236 // m_syncAMBuff[i] = (sideband[i].real() + sideband[i].imag())/2.0f;
237 // } else if (m_settings.m_syncAMOperation == AMDemodSettings::SyncAMUSB) {
238 // m_syncAMBuff[i] = (sideband[i].real() + sideband[i].imag());
239 // } else {
240 // m_syncAMBuff[i] = (sideband[i].real() + sideband[i].imag());
241 // }
242 
243  m_syncAMBuffIndex = 0;
244  }
245 
247  demod = m_syncAMBuff[m_syncAMBuffIndex++]*4.0f; // mos pifometrico
248 // demod = m_syncAMBuff[m_syncAMBuffIndex++]*(SDR_RX_SCALEF/602.0f);
249 // m_volumeAGC.feed(demod);
250 // demod /= (10.0*m_volumeAGC.getValue());
251  }
252  else
253  {
255  m_volumeAGC.feed(demod);
256  demod = (demod - m_volumeAGC.getValue()) / m_volumeAGC.getValue();
257  }
258 
260  {
261  demod = m_bandpass.filter(demod);
262  demod /= 301.0f;
263  }
264  else
265  {
266  demod = m_lowpass.filter(demod);
267  }
268 
269  Real attack = (m_squelchCount - 0.05f * m_audioSampleRate) / (0.05f * m_audioSampleRate);
270  sample = demod * StepFunctions::smootherstep(attack) * (m_audioSampleRate/24) * m_settings.m_volume;
271  }
272  else
273  {
274  sample = 0;
275  }
276 
277  m_audioBuffer[m_audioBufferFill].l = sample;
278  m_audioBuffer[m_audioBufferFill].r = sample;
280 
281  if (m_audioBufferFill >= m_audioBuffer.size())
282  {
283  uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
284 
285  if (res != m_audioBufferFill)
286  {
287  qDebug("AMDemod::processOneSample: %u/%u audio samples written", res, m_audioBufferFill);
288  m_audioFifo.clear();
289  }
290 
291  m_audioBufferFill = 0;
292  }
293 }
294 
296 {
297  qDebug("AMDemod::start");
298  m_squelchCount = 0;
299  m_audioFifo.clear();
301  m_running = true;
302 }
303 
305 {
306  qDebug("AMDemod::stop");
307  m_running = false;
308 }
309 
311 {
313  {
315 
316  qDebug() << "AMDemod::handleMessage: MsgChannelizerNotification:"
317  << " inputSampleRate: " << notif.getSampleRate()
318  << " inputFrequencyOffset: " << notif.getFrequencyOffset();
319 
321 
322  return true;
323  }
324  else if (MsgConfigureChannelizer::match(cmd))
325  {
327 
328  qDebug() << "AMDemod::handleMessage: MsgConfigureChannelizer:"
329  << " sampleRate: " << cfg.getSampleRate()
330  << " inputFrequencyOffset: " << cfg.getCenterFrequency();
331 
333  cfg.getSampleRate(),
334  cfg.getCenterFrequency());
335 
336  return true;
337  }
338  else if (MsgConfigureAMDemod::match(cmd))
339  {
341  qDebug() << "AMDemod::handleMessage: MsgConfigureAMDemod";
342  applySettings(cfg.getSettings(), cfg.getForce());
343 
344  return true;
345  }
347  {
349  const QThread *thread = cfg.getThread();
350  qDebug("AMDemod::handleMessage: BasebandSampleSink::MsgThreadedSink: %p", thread);
351  return true;
352  }
353  else if (DSPSignalNotification::match(cmd))
354  {
355  return true;
356  }
357  else if (DSPConfigureAudio::match(cmd))
358  {
359  DSPConfigureAudio& cfg = (DSPConfigureAudio&) cmd;
360  uint32_t sampleRate = cfg.getSampleRate();
361 
362  qDebug() << "AMDemod::handleMessage: DSPConfigureAudio:"
363  << " sampleRate: " << sampleRate;
364 
365  if (sampleRate != m_audioSampleRate) {
366  applyAudioSampleRate(sampleRate);
367  }
368 
369  return true;
370  }
371  else
372  {
373  return false;
374  }
375 }
376 
377 void AMDemod::applyAudioSampleRate(int sampleRate)
378 {
379  qDebug("AMDemod::applyAudioSampleRate: sampleRate: %d m_inputSampleRate: %d", sampleRate, m_inputSampleRate);
380 
382  sampleRate, m_settings.m_inputFrequencyOffset);
383  m_inputMessageQueue.push(channelConfigMsg);
384 
385  m_settingsMutex.lock();
386 
390  m_bandpass.create(301, sampleRate, 300.0, m_settings.m_rfBandwidth / 2.0f);
391  m_lowpass.create(301, sampleRate, m_settings.m_rfBandwidth / 2.0f);
392  m_audioFifo.setSize(sampleRate);
393  m_squelchDelayLine.resize(sampleRate/5);
394  DSBFilter->create_dsb_filter((2.0f * m_settings.m_rfBandwidth) / (float) sampleRate);
395  m_pllFilt.create(101, sampleRate, 200.0);
396 
397  if (m_settings.m_pll) {
398  m_volumeAGC.resizeNew(sampleRate, 0.003);
399  } else {
400  m_volumeAGC.resizeNew(sampleRate/10, 0.003);
401  }
402 
403  m_syncAMAGC.resize(sampleRate/4, sampleRate/8, 0.1);
404  m_pll.setSampleRate(sampleRate);
405 
406  m_settingsMutex.unlock();
407  m_audioSampleRate = sampleRate;
408 }
409 
410 void AMDemod::applyChannelSettings(int inputSampleRate, int inputFrequencyOffset, bool force)
411 {
412  qDebug() << "AMDemod::applyChannelSettings:"
413  << " inputSampleRate: " << inputSampleRate
414  << " inputFrequencyOffset: " << inputFrequencyOffset
415  << " m_audioSampleRate: " << m_audioSampleRate;
416 
417  if ((m_inputFrequencyOffset != inputFrequencyOffset) ||
418  (m_inputSampleRate != inputSampleRate) || force)
419  {
420  m_nco.setFreq(-inputFrequencyOffset, inputSampleRate);
421  }
422 
423  if ((m_inputSampleRate != inputSampleRate) || force)
424  {
425  m_settingsMutex.lock();
426  m_interpolator.create(16, inputSampleRate, m_settings.m_rfBandwidth / 2.2f);
428  m_interpolatorDistance = (Real) inputSampleRate / (Real) m_audioSampleRate;
429  m_settingsMutex.unlock();
430  }
431 
432  m_inputSampleRate = inputSampleRate;
433  m_inputFrequencyOffset = inputFrequencyOffset;
434 }
435 
436 void AMDemod::applySettings(const AMDemodSettings& settings, bool force)
437 {
438  qDebug() << "AMDemod::applySettings:"
439  << " m_inputFrequencyOffset: " << settings.m_inputFrequencyOffset
440  << " m_rfBandwidth: " << settings.m_rfBandwidth
441  << " m_volume: " << settings.m_volume
442  << " m_squelch: " << settings.m_squelch
443  << " m_audioMute: " << settings.m_audioMute
444  << " m_bandpassEnable: " << settings.m_bandpassEnable
445  << " m_audioDeviceName: " << settings.m_audioDeviceName
446  << " m_pll: " << settings.m_pll
447  << " m_syncAMOperation: " << (int) settings.m_syncAMOperation
448  << " m_streamIndex: " << settings.m_streamIndex
449  << " m_useReverseAPI: " << settings.m_useReverseAPI
450  << " m_reverseAPIAddress: " << settings.m_reverseAPIAddress
451  << " m_reverseAPIPort: " << settings.m_reverseAPIPort
452  << " m_reverseAPIDeviceIndex: " << settings.m_reverseAPIDeviceIndex
453  << " m_reverseAPIChannelIndex: " << settings.m_reverseAPIChannelIndex
454  << " force: " << force;
455 
456  QList<QString> reverseAPIKeys;
457 
458  if((m_settings.m_rfBandwidth != settings.m_rfBandwidth) ||
459  (m_settings.m_bandpassEnable != settings.m_bandpassEnable) || force)
460  {
461  m_settingsMutex.lock();
465  m_bandpass.create(301, m_audioSampleRate, 300.0, settings.m_rfBandwidth / 2.0f);
466  m_lowpass.create(301, m_audioSampleRate, settings.m_rfBandwidth / 2.0f);
467  DSBFilter->create_dsb_filter((2.0f * settings.m_rfBandwidth) / (float) m_audioSampleRate);
468  m_settingsMutex.unlock();
469 
470  if ((m_settings.m_rfBandwidth != settings.m_rfBandwidth) || force) {
471  reverseAPIKeys.append("rfBandwidth");
472  }
473  if ((m_settings.m_bandpassEnable != settings.m_bandpassEnable) || force) {
474  reverseAPIKeys.append("bandpassEnable");
475  }
476  }
477 
478  if ((m_settings.m_squelch != settings.m_squelch) || force)
479  {
481  reverseAPIKeys.append("squelch");
482  }
483 
484  if ((settings.m_audioDeviceName != m_settings.m_audioDeviceName) || force)
485  {
487  int audioDeviceIndex = audioDeviceManager->getOutputDeviceIndex(settings.m_audioDeviceName);
488  //qDebug("AMDemod::applySettings: audioDeviceName: %s audioDeviceIndex: %d", qPrintable(settings.m_audioDeviceName), audioDeviceIndex);
489  audioDeviceManager->addAudioSink(&m_audioFifo, getInputMessageQueue(), audioDeviceIndex);
490  uint32_t audioSampleRate = audioDeviceManager->getOutputSampleRate(audioDeviceIndex);
491 
492  if (m_audioSampleRate != audioSampleRate) {
493  applyAudioSampleRate(audioSampleRate);
494  }
495 
496  reverseAPIKeys.append("audioDeviceName");
497  }
498 
499  if ((m_settings.m_pll != settings.m_pll) || force)
500  {
501  if (settings.m_pll)
502  {
504  m_syncAMBuffIndex = 0;
505  }
506  else
507  {
509  }
510 
511  reverseAPIKeys.append("pll");
512  reverseAPIKeys.append("syncAMOperation");
513  }
514 
515  if ((m_settings.m_syncAMOperation != settings.m_syncAMOperation) || force)
516  {
517  m_syncAMBuffIndex = 0;
518  reverseAPIKeys.append("pll");
519  reverseAPIKeys.append("syncAMOperation");
520  }
521 
522  if ((m_settings.m_inputFrequencyOffset != settings.m_inputFrequencyOffset) || force) {
523  reverseAPIKeys.append("inputFrequencyOffset");
524  }
525 
526  if ((m_settings.m_audioMute != settings.m_audioMute) || force) {
527  reverseAPIKeys.append("audioMute");
528  }
529 
530  if ((m_settings.m_volume != settings.m_volume) || force) {
531  reverseAPIKeys.append("volume");
532  }
533 
534  if (m_settings.m_streamIndex != settings.m_streamIndex)
535  {
536  if (m_deviceAPI->getSampleMIMO()) // change of stream is possible for MIMO devices only
537  {
542  // apply stream sample rate to itself
544  }
545 
546  reverseAPIKeys.append("streamIndex");
547  }
548 
549  if (settings.m_useReverseAPI)
550  {
551  bool fullUpdate = ((m_settings.m_useReverseAPI != settings.m_useReverseAPI) && settings.m_useReverseAPI) ||
556  webapiReverseSendSettings(reverseAPIKeys, settings, fullUpdate || force);
557  }
558 
559  m_settings = settings;
560 }
561 
562 QByteArray AMDemod::serialize() const
563 {
564  return m_settings.serialize();
565 }
566 
567 bool AMDemod::deserialize(const QByteArray& data)
568 {
569  if (m_settings.deserialize(data))
570  {
573  return true;
574  }
575  else
576  {
580  return false;
581  }
582 }
583 
586  QString& errorMessage)
587 {
588  (void) errorMessage;
590  response.getAmDemodSettings()->init();
592  return 200;
593 }
594 
596  bool force,
597  const QStringList& channelSettingsKeys,
599  QString& errorMessage)
600 {
601  (void) errorMessage;
602  AMDemodSettings settings = m_settings;
603  bool frequencyOffsetChanged = false;
604 
605  if (channelSettingsKeys.contains("audioMute")) {
606  settings.m_audioMute = response.getAmDemodSettings()->getAudioMute() != 0;
607  }
608  if (channelSettingsKeys.contains("inputFrequencyOffset"))
609  {
611  frequencyOffsetChanged = true;
612  }
613  if (channelSettingsKeys.contains("rfBandwidth")) {
614  settings.m_rfBandwidth = response.getAmDemodSettings()->getRfBandwidth();
615  }
616  if (channelSettingsKeys.contains("rgbColor")) {
617  settings.m_rgbColor = response.getAmDemodSettings()->getRgbColor();
618  }
619  if (channelSettingsKeys.contains("squelch")) {
620  settings.m_squelch = response.getAmDemodSettings()->getSquelch();
621  }
622  if (channelSettingsKeys.contains("title")) {
623  settings.m_title = *response.getAmDemodSettings()->getTitle();
624  }
625  if (channelSettingsKeys.contains("volume")) {
626  settings.m_volume = response.getAmDemodSettings()->getVolume();
627  }
628  if (channelSettingsKeys.contains("bandpassEnable")) {
629  settings.m_bandpassEnable = response.getAmDemodSettings()->getBandpassEnable() != 0;
630  }
631  if (channelSettingsKeys.contains("audioDeviceName")) {
632  settings.m_audioDeviceName = *response.getAmDemodSettings()->getAudioDeviceName();
633  }
634 
635  if (channelSettingsKeys.contains("pll")) {
636  settings.m_pll = response.getAmDemodSettings()->getPll();
637  }
638 
639  if (channelSettingsKeys.contains("syncAMOperation")) {
640  qint32 syncAMOperationCode = response.getAmDemodSettings()->getSyncAmOperation();
641  settings.m_syncAMOperation = syncAMOperationCode < 0 ?
642  AMDemodSettings::SyncAMDSB : syncAMOperationCode > 2 ?
644  }
645 
646  if (channelSettingsKeys.contains("streamIndex")) {
647  settings.m_streamIndex = response.getAmDemodSettings()->getStreamIndex();
648  }
649  if (channelSettingsKeys.contains("useReverseAPI")) {
650  settings.m_useReverseAPI = response.getAmDemodSettings()->getUseReverseApi() != 0;
651  }
652  if (channelSettingsKeys.contains("reverseAPIAddress")) {
654  }
655  if (channelSettingsKeys.contains("reverseAPIPort")) {
656  settings.m_reverseAPIPort = response.getAmDemodSettings()->getReverseApiPort();
657  }
658  if (channelSettingsKeys.contains("reverseAPIDeviceIndex")) {
660  }
661  if (channelSettingsKeys.contains("reverseAPIChannelIndex")) {
663  }
664 
665  if (frequencyOffsetChanged)
666  {
669  m_inputMessageQueue.push(channelConfigMsg);
670  }
671 
672  MsgConfigureAMDemod *msg = MsgConfigureAMDemod::create(settings, force);
674 
675  qDebug("AMDemod::webapiSettingsPutPatch: forward to GUI: %p", m_guiMessageQueue);
676  if (m_guiMessageQueue) // forward to GUI if any
677  {
678  MsgConfigureAMDemod *msgToGUI = MsgConfigureAMDemod::create(settings, force);
679  m_guiMessageQueue->push(msgToGUI);
680  }
681 
682  webapiFormatChannelSettings(response, settings);
683 
684  return 200;
685 }
686 
689  QString& errorMessage)
690 {
691  (void) errorMessage;
693  response.getAmDemodReport()->init();
694  webapiFormatChannelReport(response);
695  return 200;
696 }
697 
699 {
700  response.getAmDemodSettings()->setAudioMute(settings.m_audioMute ? 1 : 0);
702  response.getAmDemodSettings()->setRfBandwidth(settings.m_rfBandwidth);
703  response.getAmDemodSettings()->setRgbColor(settings.m_rgbColor);
704  response.getAmDemodSettings()->setSquelch(settings.m_squelch);
705  response.getAmDemodSettings()->setVolume(settings.m_volume);
706  response.getAmDemodSettings()->setBandpassEnable(settings.m_bandpassEnable ? 1 : 0);
707 
708  if (response.getAmDemodSettings()->getTitle()) {
709  *response.getAmDemodSettings()->getTitle() = settings.m_title;
710  } else {
711  response.getAmDemodSettings()->setTitle(new QString(settings.m_title));
712  }
713 
714  if (response.getAmDemodSettings()->getAudioDeviceName()) {
715  *response.getAmDemodSettings()->getAudioDeviceName() = settings.m_audioDeviceName;
716  } else {
717  response.getAmDemodSettings()->setAudioDeviceName(new QString(settings.m_audioDeviceName));
718  }
719 
720  response.getAmDemodSettings()->setPll(settings.m_pll ? 1 : 0);
723  response.getAmDemodSettings()->setUseReverseApi(settings.m_useReverseAPI ? 1 : 0);
724 
725  if (response.getAmDemodSettings()->getReverseApiAddress()) {
727  } else {
728  response.getAmDemodSettings()->setReverseApiAddress(new QString(settings.m_reverseAPIAddress));
729  }
730 
734 }
735 
737 {
738  double magsqAvg, magsqPeak;
739  int nbMagsqSamples;
740  getMagSqLevels(magsqAvg, magsqPeak, nbMagsqSamples);
741 
742  response.getAmDemodReport()->setChannelPowerDb(CalcDb::dbPower(magsqAvg));
743  response.getAmDemodReport()->setSquelch(m_squelchOpen ? 1 : 0);
746 }
747 
748 void AMDemod::webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const AMDemodSettings& settings, bool force)
749 {
751  swgChannelSettings->setDirection(0); // Single sink (Rx)
752  swgChannelSettings->setOriginatorChannelIndex(getIndexInDeviceSet());
753  swgChannelSettings->setOriginatorDeviceSetIndex(getDeviceSetIndex());
754  swgChannelSettings->setChannelType(new QString("AMDemod"));
755  swgChannelSettings->setAmDemodSettings(new SWGSDRangel::SWGAMDemodSettings());
756  SWGSDRangel::SWGAMDemodSettings *swgAMDemodSettings = swgChannelSettings->getAmDemodSettings();
757 
758  // transfer data that has been modified. When force is on transfer all data except reverse API data
759 
760  if (channelSettingsKeys.contains("audioMute") || force) {
761  swgAMDemodSettings->setAudioMute(settings.m_audioMute ? 1 : 0);
762  }
763  if (channelSettingsKeys.contains("inputFrequencyOffset") || force) {
764  swgAMDemodSettings->setInputFrequencyOffset(settings.m_inputFrequencyOffset);
765  }
766  if (channelSettingsKeys.contains("rfBandwidth") || force) {
767  swgAMDemodSettings->setRfBandwidth(settings.m_rfBandwidth);
768  }
769  if (channelSettingsKeys.contains("rgbColor") || force) {
770  swgAMDemodSettings->setRgbColor(settings.m_rgbColor);
771  }
772  if (channelSettingsKeys.contains("squelch") || force) {
773  swgAMDemodSettings->setSquelch(settings.m_squelch);
774  }
775  if (channelSettingsKeys.contains("title") || force) {
776  swgAMDemodSettings->setTitle(new QString(settings.m_title));
777  }
778  if (channelSettingsKeys.contains("volume") || force) {
779  swgAMDemodSettings->setVolume(settings.m_volume);
780  }
781  if (channelSettingsKeys.contains("bandpassEnable") || force) {
782  swgAMDemodSettings->setBandpassEnable(settings.m_bandpassEnable ? 1 : 0);
783  }
784  if (channelSettingsKeys.contains("audioDeviceName") || force) {
785  swgAMDemodSettings->setAudioDeviceName(new QString(settings.m_audioDeviceName));
786  }
787  if (channelSettingsKeys.contains("pll") || force) {
788  swgAMDemodSettings->setPll(settings.m_pll);
789  }
790  if (channelSettingsKeys.contains("syncAMOperation") || force) {
791  swgAMDemodSettings->setSyncAmOperation((int) settings.m_syncAMOperation);
792  }
793  if (channelSettingsKeys.contains("streamIndex") || force) {
794  swgAMDemodSettings->setStreamIndex(settings.m_streamIndex);
795  }
796 
797  QString channelSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/channel/%4/settings")
798  .arg(settings.m_reverseAPIAddress)
799  .arg(settings.m_reverseAPIPort)
800  .arg(settings.m_reverseAPIDeviceIndex)
801  .arg(settings.m_reverseAPIChannelIndex);
802  m_networkRequest.setUrl(QUrl(channelSettingsURL));
803  m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
804 
805  QBuffer *buffer=new QBuffer();
806  buffer->open((QBuffer::ReadWrite));
807  buffer->write(swgChannelSettings->asJson().toUtf8());
808  buffer->seek(0);
809 
810  // Always use PATCH to avoid passing reverse API settings
811  m_networkManager->sendCustomRequest(m_networkRequest, "PATCH", buffer);
812 
813  delete swgChannelSettings;
814 }
815 
816 void AMDemod::networkManagerFinished(QNetworkReply *reply)
817 {
818  QNetworkReply::NetworkError replyError = reply->error();
819 
820  if (replyError)
821  {
822  qWarning() << "AMDemod::networkManagerFinished:"
823  << " error(" << (int) replyError
824  << "): " << replyError
825  << ": " << reply->errorString();
826  return;
827  }
828 
829  QString answer = reply->readAll();
830  answer.chop(1); // remove last \n
831  qDebug("AMDemod::networkManagerFinished: reply:\n%s", answer.toStdString().c_str());
832 }
void resize(int size)
Real m_syncAMBuff[2 *1024]
Definition: amdemod.h:215
void setOriginatorChannelIndex(qint32 originator_channel_index)
void addAudioSink(AudioFifo *audioFifo, MessageQueue *sampleSinkMessageQueue, int outputDeviceIndex=-1)
Add the audio sink.
void applyChannelSettings(int inputSampleRate, int inputFrequencyOffset, bool force=false)
Definition: amdemod.cpp:410
virtual void start()
Definition: amdemod.cpp:295
int getOutputSampleRate(int outputDeviceIndex=-1)
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)
void webapiReverseSendSettings(QList< QString > &channelSettingsKeys, const AMDemodSettings &settings, bool force)
Definition: amdemod.cpp:748
void push(Message *message, bool emitSignal=true)
Push message onto queue.
int m_magsqCount
Definition: amdemod.h:204
uint32_t getNumberOfDeviceStreams() const
Definition: amdemod.cpp:111
QNetworkAccessManager * m_networkManager
Definition: amdemod.h:225
MagAGC m_syncAMAGC
Definition: amdemod.h:217
void removeChannelSinkAPI(ChannelAPI *channelAPI, int streamIndex=0)
Definition: deviceapi.cpp:163
double m_magsqSum
Definition: amdemod.h:202
void create(int nTaps, double sampleRate, double lowCutoff, double highCutoff)
Definition: bandpass.h:15
void setThresholdEnable(bool enable)
Definition: agc.cpp:83
static double dbPower(double magsq, double floor=1e-12)
Definition: db.cpp:22
void addChannelSinkAPI(ChannelAPI *channelAPI, int streamIndex=0)
Definition: deviceapi.cpp:156
void getMagSqLevels(double &avg, double &peak, int &nbSamples)
Definition: amdemod.h:143
void create(int phaseSteps, double sampleRate, double cutoff, double nbTapsPerPhase=4.5)
void setAudioDeviceName(QString *audio_device_name)
MessageQueue * getInputMessageQueue()
Get the queue for asynchronous inbound communication.
AMDemodSettings m_settings
Definition: amdemod.h:188
bool interpolate(Real *distance, const Complex &next, Complex *result)
Definition: interpolator.h:53
void clear()
Definition: audiofifo.cpp:156
void create(int nTaps, double sampleRate, double cutoff)
Definition: lowpass.h:15
int getDeviceSetIndex() const
Definition: channelapi.h:89
void webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings &response, const AMDemodSettings &settings)
Definition: amdemod.cpp:698
int getOutputDeviceIndex(const QString &deviceName) const
static MsgConfigureAMDemod * create(const AMDemodSettings &settings, bool force)
Definition: amdemod.h:58
DeviceAPI * m_deviceAPI
Definition: amdemod.h:182
virtual QByteArray serialize() const
Definition: amdemod.cpp:562
void removeAudioSink(AudioFifo *audioFifo)
Remove the audio sink.
SWGAMDemodReport * getAmDemodReport()
virtual bool deserialize(const QByteArray &data)
Definition: amdemod.cpp:567
double m_magsq
Definition: amdemod.h:201
Type filter(Type sample)
Definition: bandpass.h:77
uint32_t m_squelchCount
Definition: amdemod.h:198
void setChannelPowerDb(float channel_power_db)
void setSyncAmOperation(qint32 sync_am_operation)
void webapiFormatChannelReport(SWGSDRangel::SWGChannelReport &response)
Definition: amdemod.cpp:736
static float smootherstep(float x)
Definition: stepfunctions.h:24
fftfilt * SSBFilter
Definition: amdemod.h:214
uint16_t m_reverseAPIChannelIndex
void setChannelType(QString *channel_type)
bool m_running
Definition: amdemod.h:190
void setOriginatorDeviceSetIndex(qint32 originator_device_set_index)
void setReverseApiAddress(QString *reverse_api_address)
MessageQueue m_inputMessageQueue
Queue for asynchronous inbound communication.
AudioFifo m_audioFifo
Definition: amdemod.h:221
Real getValue()
Definition: agc.h:107
fftfilt * DSBFilter
Definition: amdemod.h:213
double feedAndGetValue(const Complex &ci)
Definition: agc.cpp:108
std::complex< float > cmplx
Definition: fftfilt.h:21
void write(const T &element)
unsigned int uint32_t
Definition: rtptypes_win.h:46
#define SDR_RX_SCALEF
Definition: dsptypes.h:33
MovingAverageUtil< Real, double, 16 > m_movingAverage
Definition: amdemod.h:207
int m_streamIndex
MIMO channel. Not relevant when connected to SI (single Rx).
void feed(Real value)
Definition: agc.h:116
MessageQueue * m_guiMessageQueue
Input message queue to the GUI.
void resize(int historySize, int stepLength, Real R)
Definition: agc.cpp:65
Real m_interpolatorDistance
Definition: amdemod.h:194
QMutex m_settingsMutex
Definition: amdemod.h:228
Lowpass< Real > m_lowpass
Definition: amdemod.h:210
#define MESSAGE_CLASS_DEFINITION(Name, BaseClass)
Definition: message.h:52
uint16_t m_reverseAPIDeviceIndex
int getSampleRate() const
Definition: dspcommands.h:390
AMDemod(DeviceAPI *deviceAPI)
Definition: amdemod.cpp:52
SWGAMDemodSettings * getAmDemodSettings()
static DSPEngine * instance()
Definition: dspengine.cpp:51
void setAmDemodSettings(SWGAMDemodSettings *am_demod_settings)
const AMDemodSettings & getSettings() const
Definition: amdemod.h:55
void setUseReverseApi(qint32 use_reverse_api)
void setBandpassEnable(qint32 bandpass_enable)
int m_inputFrequencyOffset
Definition: amdemod.h:187
QByteArray serialize() const
int32_t i
Definition: decimators.h:244
void setRfBandwidth(float rf_bandwidth)
virtual bool handleMessage(const Message &cmd)
Processing of a message. Returns true if message has actually been processed.
Definition: amdemod.cpp:310
Interpolator m_interpolator
Definition: amdemod.h:193
SyncAMOperation m_syncAMOperation
virtual int webapiReportGet(SWGSDRangel::SWGChannelReport &response, QString &errorMessage)
Definition: amdemod.cpp:687
QString m_audioDeviceName
static bool match(const Message *message)
Definition: message.cpp:45
uint32_t m_audioSampleRate
Definition: amdemod.h:189
virtual void feed(const SampleVector::const_iterator &begin, const SampleVector::const_iterator &end, bool po)
Definition: amdemod.cpp:116
void setFreq(Real freq, Real sampleRate)
Definition: nco.cpp:49
bool deserialize(const QByteArray &data)
void setInputFrequencyOffset(qint64 input_frequency_offset)
ThreadedBasebandSampleSink * m_threadedChannelizer
Definition: amdemod.h:183
void removeChannelSink(ThreadedBasebandSampleSink *sink, int streamIndex=0)
Remove a channel sink (Rx)
Definition: deviceapi.cpp:127
DoubleBufferFIFO< Real > m_squelchDelayLine
Definition: amdemod.h:200
DownChannelizer * m_channelizer
Definition: amdemod.h:184
Fixed< IntType, IntBits > sqrt(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2283
virtual int webapiSettingsPutPatch(bool force, const QStringList &channelSettingsKeys, SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
Definition: amdemod.cpp:595
int m_inputSampleRate
Definition: amdemod.h:186
double m_magsqPeak
Definition: amdemod.h:203
void applySettings(const AMDemodSettings &settings, bool force=false)
Definition: amdemod.cpp:436
virtual QString asJson() override
static const QString m_channelId
Definition: amdemod.h:164
void processOneSample(Complex &ci)
Definition: amdemod.cpp:165
static MsgConfigureChannelizer * create(int sampleRate, int centerFrequency)
Definition: amdemod.h:81
void applyAudioSampleRate(int sampleRate)
Definition: amdemod.cpp:377
Lowpass< std::complex< float > > m_pllFilt
Definition: amdemod.h:211
T & readBack(int delay)
AudioDeviceManager * getAudioDeviceManager()
Definition: dspengine.h:55
void addChannelSink(ThreadedBasebandSampleSink *sink, int streamIndex=0)
Add a channel sink (Rx)
Definition: deviceapi.cpp:118
AudioVector m_audioBuffer
Definition: amdemod.h:219
void setChannelSampleRate(qint32 channel_sample_rate)
QString m_reverseAPIAddress
bool setSize(uint32_t numSamples)
Definition: audiofifo.cpp:59
Type filter(Type sample)
Definition: lowpass.h:55
uint32_t m_audioBufferFill
Definition: amdemod.h:220
void resizeNew(uint32_t newSize, Real initial, Real cutoff=0, Real clip=0)
Definition: agc.h:95
virtual int webapiSettingsGet(SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
Definition: amdemod.cpp:584
void setAmDemodReport(SWGAMDemodReport *am_demod_report)
PhaseLockComplex m_pll
Definition: amdemod.h:212
virtual void stop()
Definition: amdemod.cpp:304
uint16_t m_reverseAPIPort
int runSSB(const cmplx &in, cmplx **out, bool usb, bool getDC=true)
Definition: fftfilt.cpp:284
~AMDemod()
Definition: amdemod.cpp:98
void setReverseApiChannelIndex(qint32 reverse_api_channel_index)
void setAudioSampleRate(qint32 audio_sample_rate)
void networkManagerFinished(QNetworkReply *reply)
Definition: amdemod.cpp:816
qint32 m_inputFrequencyOffset
Real m_squelchLevel
Definition: amdemod.h:197
NCO m_nco
Definition: amdemod.h:192
virtual int getSourceSampleRate(int index) const =0
Sample rate exposed by the source at index.
Bandpass< Real > m_bandpass
Definition: amdemod.h:209
void setAudioMute(qint32 audio_mute)
uint32_t getNbSourceStreams() const
Definition: deviceapi.h:170
void setReverseApiDeviceIndex(qint32 reverse_api_device_index)
Real m_interpolatorDistanceRemain
Definition: amdemod.h:195
QNetworkRequest m_networkRequest
Definition: amdemod.h:226
void computeCoefficients(Real wn, Real zeta, Real K)
float getImag() const
int getIndexInDeviceSet() const
Definition: channelapi.h:87
void setReverseApiPort(qint32 reverse_api_port)
uint32_t m_syncAMBuffIndex
Definition: amdemod.h:216
std::complex< Real > Complex
Definition: dsptypes.h:43
double asDouble() const
Definition: movingaverage.h:57
static const QString m_channelIdURI
Definition: amdemod.h:163
DeviceSampleMIMO * getSampleMIMO()
Return pointer to the device sample MIMO or nullptr.
Definition: deviceapi.cpp:231
int runDSB(const cmplx &in, cmplx **out, bool getDC=true)
Definition: fftfilt.cpp:327
float Real
Definition: dsptypes.h:42
void setStreamIndex(qint32 stream_index)
static double powerFromdB(double powerdB)
Definition: db.cpp:36
void feed(float re, float im)
bool m_squelchOpen
Definition: amdemod.h:199
float getReal() const
uint32_t write(const quint8 *data, uint32_t numSamples)
Definition: audiofifo.cpp:66
SimpleAGC< 4800 > m_volumeAGC
Definition: amdemod.h:208
void create_dsb_filter(float f2)
Definition: fftfilt.cpp:148
void setSampleRate(unsigned int sampleRate)
static const int m_udpBlockSize
Definition: amdemod.h:223