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.
freedvdemod.h
Go to the documentation of this file.
1 // Copyright (C) 2019 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 #ifndef INCLUDE_FREEDVDEMOD_H
19 #define INCLUDE_FREEDVDEMOD_H
20 
21 #include <vector>
22 
23 #include <QMutex>
24 #include <QNetworkRequest>
25 
26 #include "dsp/basebandsamplesink.h"
27 #include "channel/channelapi.h"
28 #include "dsp/ncof.h"
29 #include "dsp/interpolator.h"
30 #include "dsp/fftfilt.h"
31 #include "dsp/agc.h"
32 #include "audio/audiofifo.h"
33 #include "audio/audioresampler.h"
34 #include "util/message.h"
35 #include "util/doublebufferfifo.h"
36 
37 #include "freedvdemodsettings.h"
38 
39 #define ssbFftLen 1024
40 #define agcTarget 3276.8 // -10 dB amplitude => -20 dB power: center of normal signal
41 
42 class QNetworkAccessManager;
43 class QNetworkReply;
44 class DeviceAPI;
46 class DownChannelizer;
47 
48 struct freedv;
49 
50 class FreeDVDemod : public BasebandSampleSink, public ChannelAPI {
51  Q_OBJECT
52 public:
55 
56  public:
57  const FreeDVDemodSettings& getSettings() const { return m_settings; }
58  bool getForce() const { return m_force; }
59 
60  static MsgConfigureFreeDVDemod* create(const FreeDVDemodSettings& settings, bool force)
61  {
62  return new MsgConfigureFreeDVDemod(settings, force);
63  }
64 
65  private:
67  bool m_force;
68 
69  MsgConfigureFreeDVDemod(const FreeDVDemodSettings& settings, bool force) :
70  Message(),
71  m_settings(settings),
72  m_force(force)
73  { }
74  };
75 
76  class MsgResyncFreeDVDemod : public Message {
78 
79  public:
81  return new MsgResyncFreeDVDemod();
82  }
83 
84  private:
86  {}
87  };
88 
91 
92  public:
93  int getSampleRate() const { return m_sampleRate; }
94  int getCenterFrequency() const { return m_centerFrequency; }
95 
96  static MsgConfigureChannelizer* create(int sampleRate, int centerFrequency)
97  {
98  return new MsgConfigureChannelizer(sampleRate, centerFrequency);
99  }
100 
101  private:
104 
105  MsgConfigureChannelizer(int sampleRate, int centerFrequency) :
106  Message(),
107  m_sampleRate(sampleRate),
108  m_centerFrequency(centerFrequency)
109  { }
110  };
111 
112  FreeDVDemod(DeviceAPI *deviceAPI);
113  virtual ~FreeDVDemod();
114  virtual void destroy() { delete this; }
115  void setSampleSink(BasebandSampleSink* sampleSink) { m_sampleSink = sampleSink; }
116 
117  void configure(MessageQueue* messageQueue,
118  Real Bandwidth,
119  Real LowCutoff,
120  Real volume,
121  int spanLog2,
122  bool audioBinaural,
123  bool audioFlipChannels,
124  bool dsb,
125  bool audioMute,
126  bool agc,
127  bool agcClamping,
128  int agcTimeLog2,
129  int agcPowerThreshold,
130  int agcThresholdGate);
131 
132  virtual void feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool positiveOnly);
133  virtual void start();
134  virtual void stop();
135  virtual bool handleMessage(const Message& cmd);
136 
137  virtual void getIdentifier(QString& id) { id = objectName(); }
138  virtual void getTitle(QString& title) { title = m_settings.m_title; }
139  virtual qint64 getCenterFrequency() const { return m_settings.m_inputFrequencyOffset; }
140 
141  virtual QByteArray serialize() const;
142  virtual bool deserialize(const QByteArray& data);
143 
144  virtual int getNbSinkStreams() const { return 1; }
145  virtual int getNbSourceStreams() const { return 0; }
146 
147  virtual qint64 getStreamCenterFrequency(int streamIndex, bool sinkElseSource) const
148  {
149  (void) streamIndex;
150  (void) sinkElseSource;
152  }
153 
156  double getMagSq() const { return m_magsq; }
157  bool getAudioActive() const { return m_audioActive; }
158 
159  void getMagSqLevels(double& avg, double& peak, int& nbSamples)
160  {
161  if (m_magsqCount > 0)
162  {
166  }
167 
170  nbSamples = m_magsqCount == 0 ? 1 : m_magsqCount;
171 
172  m_magsqSum = 0.0f;
173  m_magsqPeak = 0.0f;
174  m_magsqCount = 0;
175  }
176 
177  void getSNRLevels(double& avg, double& peak, int& nbSamples);
178  int getBER() const { return m_freeDVStats.m_ber; }
180  bool isSync() const { return m_freeDVStats.m_sync; }
181 
182  virtual int webapiSettingsGet(
184  QString& errorMessage);
185 
186  virtual int webapiSettingsPutPatch(
187  bool force,
188  const QStringList& channelSettingsKeys,
190  QString& errorMessage);
191 
192  virtual int webapiReportGet(
194  QString& errorMessage);
195 
196  static const QString m_channelIdURI;
197  static const QString m_channelId;
198 
199 signals:
206  void levelInChanged(qreal rmsLevel, qreal peakLevel, int numSamples);
207 
208 private:
210  {
212  m_magsq(1e-12),
213  m_magsqPeak(1e-12)
214  {}
215  double m_magsq;
216  double m_magsqPeak;
217  };
218 
219  struct FreeDVStats
220  {
221  FreeDVStats();
222  void init();
223  void collect(struct freedv *freedv);
224 
225  bool m_sync;
226  float m_snrEst;
232  int m_ber;
236  };
237 
238  struct FreeDVSNR
239  {
240  FreeDVSNR();
241  void accumulate(float snrdB);
242 
243  double m_sum;
244  float m_peak;
245  int m_n;
246  bool m_reset;
247  };
248 
249  struct LevelRMS
250  {
251  LevelRMS();
252  void accumulate(float fsample);
253 
254  double m_sum;
255  float m_peak;
256  int m_n;
257  bool m_reset;
258  };
259 
262 
263  public:
264  Real getBandwidth() const { return m_Bandwidth; }
265  Real getLoCutoff() const { return m_LowCutoff; }
266  Real getVolume() const { return m_volume; }
267  int getSpanLog2() const { return m_spanLog2; }
268  bool getAudioBinaural() const { return m_audioBinaural; }
269  bool getAudioFlipChannels() const { return m_audioFlipChannels; }
270  bool getDSB() const { return m_dsb; }
271  bool getAudioMute() const { return m_audioMute; }
272  bool getAGC() const { return m_agc; }
273  bool getAGCClamping() const { return m_agcClamping; }
274  int getAGCTimeLog2() const { return m_agcTimeLog2; }
275  int getAGCPowerThershold() const { return m_agcPowerThreshold; }
276  int getAGCThersholdGate() const { return m_agcThresholdGate; }
277 
279  Real LowCutoff,
280  Real volume,
281  int spanLog2,
282  bool audioBinaural,
283  bool audioFlipChannels,
284  bool dsb,
285  bool audioMute,
286  bool agc,
287  bool agcClamping,
288  int agcTimeLog2,
289  int agcPowerThreshold,
290  int agcThresholdGate)
291  {
293  Bandwidth,
294  LowCutoff,
295  volume,
296  spanLog2,
297  audioBinaural,
298  audioFlipChannels,
299  dsb,
300  audioMute,
301  agc,
302  agcClamping,
303  agcTimeLog2,
304  agcPowerThreshold,
305  agcThresholdGate);
306  }
307 
308  private:
315  bool m_dsb;
317  bool m_agc;
322 
324  Real LowCutoff,
325  Real volume,
326  int spanLog2,
327  bool audioBinaural,
328  bool audioFlipChannels,
329  bool dsb,
330  bool audioMute,
331  bool agc,
332  bool agcClamping,
333  int agcTimeLog2,
334  int agcPowerThreshold,
335  int agcThresholdGate) :
336  Message(),
337  m_Bandwidth(Bandwidth),
338  m_LowCutoff(LowCutoff),
339  m_volume(volume),
340  m_spanLog2(spanLog2),
341  m_audioBinaural(audioBinaural),
342  m_audioFlipChannels(audioFlipChannels),
343  m_dsb(dsb),
344  m_audioMute(audioMute),
345  m_agc(agc),
346  m_agcClamping(agcClamping),
347  m_agcTimeLog2(agcTimeLog2),
348  m_agcPowerThreshold(agcPowerThreshold),
349  m_agcThresholdGate(agcThresholdGate)
350  { }
351  };
352 
357 
370  double m_magsq;
371  double m_magsqSum;
372  double m_magsqPeak;
379 
385 
388 
392 
393  QNetworkAccessManager *m_networkManager;
394  QNetworkRequest m_networkRequest;
395 
396  struct freedv *m_freeDV;
399  int m_nin;
401  int m_iModem;
409 
411 
412  void pushSampleToDV(int16_t sample);
413  void pushSampleToAudio(int16_t sample);
414  void applyChannelSettings(int inputSampleRate, int inputFrequencyOffset, bool force = false);
415  void applySettings(const FreeDVDemodSettings& settings, bool force = false);
416  void applyAudioSampleRate(int sampleRate);
418  void processOneSample(Complex &ci);
421  void webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const FreeDVDemodSettings& settings, bool force);
422 
423 private slots:
424  void networkManagerFinished(QNetworkReply *reply);
425 };
426 
427 #endif // INCLUDE_FREEDVDEMOD_H
int16_t * m_speechOut
Definition: freedvdemod.h:402
short int16_t
Definition: rtptypes_win.h:43
MagSqLevelsStore m_magSqLevelStore
Definition: freedvdemod.h:374
virtual void getTitle(QString &title)
Definition: freedvdemod.h:138
QMutex m_settingsMutex
Definition: freedvdemod.h:410
virtual void getIdentifier(QString &id)
Definition: freedvdemod.h:137
DeviceAPI * m_deviceAPI
Definition: freedvdemod.h:353
static MsgResyncFreeDVDemod * create()
Definition: freedvdemod.h:80
Real m_lowCutoff
Definition: freedvdemod.h:359
Interpolator m_interpolator
Definition: freedvdemod.h:381
std::vector< Sample > SampleVector
Definition: dsptypes.h:96
void getSNRLevels(double &avg, double &peak, int &nbSamples)
std::vector< AudioSample > AudioVector
Definition: dsptypes.h:98
float getFrequencyOffset() const
Definition: freedvdemod.h:179
void configure(MessageQueue *messageQueue, Real Bandwidth, Real LowCutoff, Real volume, int spanLog2, bool audioBinaural, bool audioFlipChannels, bool dsb, bool audioMute, bool agc, bool agcClamping, int agcTimeLog2, int agcPowerThreshold, int agcThresholdGate)
virtual bool deserialize(const QByteArray &data)
virtual qint64 getCenterFrequency() const
Applies to a default stream.
Definition: freedvdemod.h:139
AudioFifo m_audioFifo
Definition: freedvdemod.h:391
void pushSampleToAudio(int16_t sample)
bool m_agcActive
Definition: freedvdemod.h:376
static MsgConfigureFreeDVDemod * create(const FreeDVDemodSettings &settings, bool force)
Definition: freedvdemod.h:60
void levelInChanged(qreal rmsLevel, qreal peakLevel, int numSamples)
bool getAudioActive() const
Definition: freedvdemod.h:157
double getMagSq() const
Definition: freedvdemod.h:156
void applyChannelSettings(int inputSampleRate, int inputFrequencyOffset, bool force=false)
AudioResampler m_audioResampler
Definition: freedvdemod.h:404
FreeDVStats m_freeDVStats
Definition: freedvdemod.h:405
virtual int getNbSinkStreams() const
Definition: freedvdemod.h:144
void pushSampleToDV(int16_t sample)
virtual int webapiReportGet(SWGSDRangel::SWGChannelReport &response, QString &errorMessage)
int16_t * m_modIn
Definition: freedvdemod.h:403
void applyAudioSampleRate(int sampleRate)
void applySettings(const FreeDVDemodSettings &settings, bool force=false)
Definition: ncof.h:24
static const QString m_channelId
Definition: freedvdemod.h:197
int m_inputSampleRate
Definition: freedvdemod.h:364
virtual ~FreeDVDemod()
fftfilt::cmplx m_sum
Definition: freedvdemod.h:362
int m_ber
estimated BER (b/s)
Definition: freedvdemod.h:232
int m_magsqCount
Definition: freedvdemod.h:373
BasebandSampleSink * m_sampleSink
Definition: freedvdemod.h:386
MsgConfigureFreeDVDemodPrivate(Real Bandwidth, Real LowCutoff, Real volume, int spanLog2, bool audioBinaural, bool audioFlipChannels, bool dsb, bool audioMute, bool agc, bool agcClamping, int agcTimeLog2, int agcPowerThreshold, int agcThresholdGate)
Definition: freedvdemod.h:323
AudioVector m_audioBuffer
Definition: freedvdemod.h:389
uint32_t m_modemSampleRate
Definition: freedvdemod.h:365
int getBER() const
Definition: freedvdemod.h:178
FreeDVDemod(DeviceAPI *deviceAPI)
static const QString m_channelIdURI
Definition: freedvdemod.h:196
QNetworkAccessManager * m_networkManager
Definition: freedvdemod.h:393
virtual qint64 getStreamCenterFrequency(int streamIndex, bool sinkElseSource) const
Definition: freedvdemod.h:147
DoubleBufferFIFO< fftfilt::cmplx > m_squelchDelayLine
Definition: freedvdemod.h:377
std::complex< float > cmplx
Definition: fftfilt.h:21
void getMagSqLevels(double &avg, double &peak, int &nbSamples)
Definition: freedvdemod.h:159
unsigned int uint32_t
Definition: rtptypes_win.h:46
double m_magsqSum
Definition: freedvdemod.h:371
virtual void destroy()
Definition: freedvdemod.h:114
void webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings &response, const FreeDVDemodSettings &settings)
FreeDVDemodSettings m_settings
Definition: freedvdemod.h:356
uint32_t m_audioSampleRate
Definition: freedvdemod.h:367
double m_magsq
Definition: freedvdemod.h:370
ThreadedBasebandSampleSink * m_threadedChannelizer
Definition: freedvdemod.h:354
Real m_interpolatorDistanceRemain
Definition: freedvdemod.h:383
void applyFreeDVMode(FreeDVDemodSettings::FreeDVMode mode)
MsgConfigureFreeDVDemod(const FreeDVDemodSettings &settings, bool force)
Definition: freedvdemod.h:69
uint32_t getModemSampleRate() const
Definition: freedvdemod.h:155
void webapiFormatChannelReport(SWGSDRangel::SWGChannelReport &response)
static MsgConfigureChannelizer * create(int sampleRate, int centerFrequency)
Definition: freedvdemod.h:96
int m_levelInNbSamples
Definition: freedvdemod.h:408
#define MESSAGE_CLASS_DECLARATION
Definition: message.h:43
int m_nSpeechSamples
Definition: freedvdemod.h:397
MsgConfigureChannelizer(int sampleRate, int centerFrequency)
Definition: freedvdemod.h:105
bool m_audioActive
True if an audio signal is produced (no AGC or AGC and above threshold)
Definition: freedvdemod.h:378
uint m_audioBufferFill
Definition: freedvdemod.h:390
int m_nMaxModemSamples
Definition: freedvdemod.h:398
const FreeDVDemodSettings & getSettings() const
Definition: freedvdemod.h:57
FreeDVSNR m_freeDVSNR
Definition: freedvdemod.h:406
Real m_interpolatorDistance
Definition: freedvdemod.h:382
uint32_t m_berFrameCount
count of frames for BER estimation
Definition: freedvdemod.h:234
int m_inputFrequencyOffset
Definition: freedvdemod.h:368
bool m_audioMute
Definition: freedvdemod.h:369
void setSampleSink(BasebandSampleSink *sampleSink)
Definition: freedvdemod.h:115
QNetworkRequest m_networkRequest
Definition: freedvdemod.h:394
virtual void start()
virtual void stop()
double m_magsqPeak
Definition: freedvdemod.h:372
LevelRMS m_levelIn
Definition: freedvdemod.h:407
uint32_t m_speechSampleRate
Definition: freedvdemod.h:366
uint32_t getAudioSampleRate() const
Definition: freedvdemod.h:154
int m_undersampleCount
Definition: freedvdemod.h:363
bool isSync() const
Definition: freedvdemod.h:180
virtual QByteArray serialize() const
struct freedv * m_freeDV
Definition: freedvdemod.h:396
virtual int getNbSourceStreams() const
Definition: freedvdemod.h:145
uint32_t m_fps
frames per second
Definition: freedvdemod.h:235
virtual int webapiSettingsGet(SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
virtual int webapiSettingsPutPatch(bool force, const QStringList &channelSettingsKeys, SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
SimpleAGC< 4800 > m_simpleAGC
Definition: freedvdemod.h:375
Real m_hiCutoff
Definition: freedvdemod.h:358
static MsgConfigureFreeDVDemodPrivate * create(Real Bandwidth, Real LowCutoff, Real volume, int spanLog2, bool audioBinaural, bool audioFlipChannels, bool dsb, bool audioMute, bool agc, bool agcClamping, int agcTimeLog2, int agcPowerThreshold, int agcThresholdGate)
Definition: freedvdemod.h:278
std::complex< Real > Complex
Definition: dsptypes.h:43
SampleVector m_sampleBuffer
Definition: freedvdemod.h:387
float Real
Definition: dsptypes.h:42
void processOneSample(Complex &ci)
fftfilt * SSBFilter
Definition: freedvdemod.h:384
virtual void feed(const SampleVector::const_iterator &begin, const SampleVector::const_iterator &end, bool positiveOnly)
void networkManagerFinished(QNetworkReply *reply)
virtual bool handleMessage(const Message &cmd)
Processing of a message. Returns true if message has actually been processed.
void webapiReverseSendSettings(QList< QString > &channelSettingsKeys, const FreeDVDemodSettings &settings, bool force)
DownChannelizer * m_channelizer
Definition: freedvdemod.h:355