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.h
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 #ifndef INCLUDE_BFMDEMOD_H
20 #define INCLUDE_BFMDEMOD_H
21 
22 #include <vector>
23 
24 #include <QMutex>
25 #include <QNetworkRequest>
26 
27 #include "dsp/basebandsamplesink.h"
28 #include "channel/channelapi.h"
29 #include "dsp/nco.h"
30 #include "dsp/interpolator.h"
31 #include "dsp/lowpass.h"
32 #include "dsp/movingaverage.h"
33 #include "dsp/fftfilt.h"
34 #include "dsp/phaselock.h"
35 #include "dsp/filterrc.h"
36 #include "dsp/phasediscri.h"
37 #include "audio/audiofifo.h"
38 #include "util/message.h"
39 
40 #include "rdsparser.h"
41 #include "rdsdecoder.h"
42 #include "rdsdemod.h"
43 #include "bfmdemodsettings.h"
44 
45 class QNetworkAccessManager;
46 class QNetworkReply;
47 class DeviceAPI;
49 class DownChannelizer;
50 
51 namespace SWGSDRangel {
52  class SWGRDSReport;
53 }
54 
55 class BFMDemod : public BasebandSampleSink, public ChannelAPI {
56  Q_OBJECT
57 public:
58  class MsgConfigureBFMDemod : public Message {
60 
61  public:
62  const BFMDemodSettings& getSettings() const { return m_settings; }
63  bool getForce() const { return m_force; }
64 
65  static MsgConfigureBFMDemod* create(const BFMDemodSettings& settings, bool force)
66  {
67  return new MsgConfigureBFMDemod(settings, force);
68  }
69 
70  private:
72  bool m_force;
73 
74  MsgConfigureBFMDemod(const BFMDemodSettings& settings, bool force) :
75  Message(),
76  m_settings(settings),
77  m_force(force)
78  { }
79  };
80 
83 
84  public:
85  int getSampleRate() const { return m_sampleRate; }
86  int getCenterFrequency() const { return m_centerFrequency; }
87 
88  static MsgConfigureChannelizer* create(int sampleRate, int centerFrequency)
89  {
90  return new MsgConfigureChannelizer(sampleRate, centerFrequency);
91  }
92 
93  private:
96 
97  MsgConfigureChannelizer(int sampleRate, int centerFrequency) :
98  Message(),
99  m_sampleRate(sampleRate),
100  m_centerFrequency(centerFrequency)
101  { }
102  };
103 
106 
107  public:
108  int getSampleRate() const { return m_sampleRate; }
109 
111  {
112  return new MsgReportChannelSampleRateChanged(sampleRate);
113  }
114 
115  private:
117 
119  Message(),
120  m_sampleRate(sampleRate)
121  { }
122  };
123 
124  BFMDemod(DeviceAPI *deviceAPI);
125  virtual ~BFMDemod();
126  virtual void destroy() { delete this; }
127  void setSampleSink(BasebandSampleSink* sampleSink) { m_sampleSink = sampleSink; }
128 
129  int getSampleRate() const { return m_inputSampleRate; }
130  virtual void feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool po);
131  virtual void start();
132  virtual void stop();
133  virtual bool handleMessage(const Message& cmd);
134 
135  virtual void getIdentifier(QString& id) { id = objectName(); }
136  virtual void getTitle(QString& title) { title = m_settings.m_title; }
137  virtual qint64 getCenterFrequency() const { return m_settings.m_inputFrequencyOffset; }
138 
139  virtual QByteArray serialize() const;
140  virtual bool deserialize(const QByteArray& data);
141 
142  virtual int getNbSinkStreams() const { return 1; }
143  virtual int getNbSourceStreams() const { return 0; }
144 
145  virtual qint64 getStreamCenterFrequency(int streamIndex, bool sinkElseSource) const
146  {
147  (void) streamIndex;
148  (void) sinkElseSource;
149  return m_settings.m_inputFrequencyOffset;
150  }
151 
152  double getMagSq() const { return m_magsq; }
153 
154  bool getPilotLock() const { return m_pilotPLL.locked(); }
155  Real getPilotLevel() const { return m_pilotPLL.get_pilot_level(); }
156 
157  Real getDecoderQua() const { return m_rdsDecoder.m_qua; }
158  bool getDecoderSynced() const { return m_rdsDecoder.synced(); }
159  Real getDemodAcc() const { return m_rdsDemod.m_report.acc; }
160  Real getDemodQua() const { return m_rdsDemod.m_report.qua; }
161  Real getDemodFclk() const { return m_rdsDemod.m_report.fclk; }
162 
163  void getMagSqLevels(double& avg, double& peak, int& nbSamples)
164  {
165  if (m_magsqCount > 0)
166  {
167  m_magsq = m_magsqSum / m_magsqCount;
168  m_magSqLevelStore.m_magsq = m_magsq;
169  m_magSqLevelStore.m_magsqPeak = m_magsqPeak;
170  }
171 
172  avg = m_magSqLevelStore.m_magsq;
173  peak = m_magSqLevelStore.m_magsqPeak;
174  nbSamples = m_magsqCount == 0 ? 1 : m_magsqCount;
175 
176  m_magsqSum = 0.0f;
177  m_magsqPeak = 0.0f;
178  m_magsqCount = 0;
179  }
180 
181  RDSParser& getRDSParser() { return m_rdsParser; }
182 
183  virtual int webapiSettingsGet(
185  QString& errorMessage);
186 
187  virtual int webapiSettingsPutPatch(
188  bool force,
189  const QStringList& channelSettingsKeys,
191  QString& errorMessage);
192 
193  virtual int webapiReportGet(
195  QString& errorMessage);
196 
197  static int requiredBW(int rfBW)
198  {
199  if (rfBW <= 48000) {
200  return 48000;
201  } else {
202  return (3*rfBW)/2;
203  }
204  }
205 
206  static const QString m_channelIdURI;
207  static const QString m_channelId;
208 
209 private:
211  {
213  m_magsq(1e-12),
214  m_magsqPeak(1e-12)
215  {}
216  double m_magsq;
217  double m_magsqPeak;
218  };
219 
220  enum RateState {
222  RSRunning
223  };
224 
228 
233 
238 
242 
246 
249  static const int filtFftLen = 1024;
250 
253 
255 
256  double m_magsq;
257  double m_magsqSum;
258  double m_magsqPeak;
261 
264 
269 
271  Real m_pilotPLLSamples[4];
272 
276 
279  static const Real default_deemphasis;
280 
282  static const int default_excursion = 750000; // +/- 75 kHz
283 
285 
286  static const int m_udpBlockSize;
287 
288  QNetworkAccessManager *m_networkManager;
289  QNetworkRequest m_networkRequest;
290 
291  void applyAudioSampleRate(int sampleRate);
292  void applyChannelSettings(int inputSampleRate, int inputFrequencyOffset, bool force = false);
293  void applySettings(const BFMDemodSettings& settings, bool force = false);
294 
295  void webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings& response, const BFMDemodSettings& settings);
296  void webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response);
297  void webapiFormatRDSReport(SWGSDRangel::SWGRDSReport *report);
298  void webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const BFMDemodSettings& settings, bool force);
299 
300 private slots:
301  void networkManagerFinished(QNetworkReply *reply);
302 };
303 
304 #endif // INCLUDE_BFMDEMOD_H
virtual void destroy()
Definition: bfmdemod.h:126
MsgConfigureChannelizer(int sampleRate, int centerFrequency)
Definition: bfmdemod.h:97
virtual void getTitle(QString &title)
Definition: bfmdemod.h:136
fftfilt * m_rfFilter
Definition: bfmdemod.h:248
std::vector< Sample > SampleVector
Definition: dsptypes.h:96
std::vector< AudioSample > AudioVector
Definition: dsptypes.h:98
bool getDecoderSynced() const
Definition: bfmdemod.h:158
Real m_interpolatorRDSDistanceRemain
Definition: bfmdemod.h:245
MagSqLevelsStore m_magSqLevelStore
Definition: bfmdemod.h:260
QMutex m_settingsMutex
Definition: bfmdemod.h:268
Interpolator m_interpolator
Interpolator between fixed demod bandwidth and audio bandwidth (rational)
Definition: bfmdemod.h:235
static const QString m_channelIdURI
Definition: bfmdemod.h:206
quint32 m_audioSampleRate
Definition: bfmdemod.h:232
AudioVector m_audioBuffer
Definition: bfmdemod.h:262
virtual qint64 getStreamCenterFrequency(int streamIndex, bool sinkElseSource) const
Definition: bfmdemod.h:145
static MsgReportChannelSampleRateChanged * create(int sampleRate)
Definition: bfmdemod.h:110
int m_magsqCount
Definition: bfmdemod.h:259
static const Real default_deemphasis
Definition: bfmdemod.h:279
Real getDemodAcc() const
Definition: bfmdemod.h:159
static const QString m_channelId
Definition: bfmdemod.h:207
virtual qint64 getCenterFrequency() const
Applies to a default stream.
Definition: bfmdemod.h:137
LowPassFilterRC m_deemphasisFilterY
Definition: bfmdemod.h:278
static MsgConfigureChannelizer * create(int sampleRate, int centerFrequency)
Definition: bfmdemod.h:88
virtual int getNbSinkStreams() const
Definition: bfmdemod.h:142
double m_magsqPeak
Definition: bfmdemod.h:258
Real m_interpolatorDistanceRemain
Definition: bfmdemod.h:237
DeviceAPI * m_deviceAPI
Definition: bfmdemod.h:225
int m_squelchState
Definition: bfmdemod.h:252
static int requiredBW(int rfBW)
Definition: bfmdemod.h:197
void setSampleSink(BasebandSampleSink *sampleSink)
Definition: bfmdemod.h:127
ThreadedBasebandSampleSink * m_threadedChannelizer
Definition: bfmdemod.h:226
BFMDemodSettings m_settings
Definition: bfmdemod.h:71
NCO m_nco
Definition: bfmdemod.h:234
AudioFifo m_audioFifo
Definition: bfmdemod.h:266
Definition: nco.h:25
int m_inputSampleRate
Definition: bfmdemod.h:229
double m_magsqSum
Definition: bfmdemod.h:257
SampleVector m_sampleBuffer
Definition: bfmdemod.h:267
MsgConfigureBFMDemod(const BFMDemodSettings &settings, bool force)
Definition: bfmdemod.h:74
LowPassFilterRC m_deemphasisFilterX
Definition: bfmdemod.h:277
#define MESSAGE_CLASS_DECLARATION
Definition: message.h:43
DownChannelizer * m_channelizer
Definition: bfmdemod.h:227
double getMagSq() const
Definition: bfmdemod.h:152
Interpolator m_interpolatorStereo
Twin Interpolator for stereo subcarrier.
Definition: bfmdemod.h:239
Real m_m1Arg
Definition: bfmdemod.h:254
int m_inputFrequencyOffset
Definition: bfmdemod.h:230
Real m_fmExcursion
Definition: bfmdemod.h:281
static const int m_udpBlockSize
Definition: bfmdemod.h:286
virtual int getNbSourceStreams() const
Definition: bfmdemod.h:143
RDSParser m_rdsParser
Definition: bfmdemod.h:275
uint m_audioBufferFill
Definition: bfmdemod.h:263
BFMDemodSettings m_settings
Definition: bfmdemod.h:231
static MsgConfigureBFMDemod * create(const BFMDemodSettings &settings, bool force)
Definition: bfmdemod.h:65
Real m_interpolatorRDSDistance
Definition: bfmdemod.h:244
Real getDemodQua() const
Definition: bfmdemod.h:160
Real m_interpolatorStereoDistanceRemain
Definition: bfmdemod.h:241
Real getDecoderQua() const
Definition: bfmdemod.h:157
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
Lowpass< Real > m_lowpass
Definition: bfmdemod.h:247
Interpolator m_interpolatorRDS
Twin Interpolator for stereo subcarrier.
Definition: bfmdemod.h:243
PhaseDiscriminators m_phaseDiscri
Definition: bfmdemod.h:284
double m_magsq
x^-1 real sample
Definition: bfmdemod.h:256
QNetworkAccessManager * m_networkManager
Definition: bfmdemod.h:288
const BFMDemodSettings & getSettings() const
Definition: bfmdemod.h:62
Real m_interpolatorDistance
Definition: bfmdemod.h:236
RDSDemod m_rdsDemod
Definition: bfmdemod.h:273
RDSParser & getRDSParser()
Definition: bfmdemod.h:181
virtual void getIdentifier(QString &id)
Definition: bfmdemod.h:135
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
RDSDecoder m_rdsDecoder
Definition: bfmdemod.h:274
bool getPilotLock() const
Definition: bfmdemod.h:154
RDSPhaseLock m_pilotPLL
Definition: bfmdemod.h:270
BasebandSampleSink * m_sampleSink
Definition: bfmdemod.h:265
Real m_squelchLevel
Definition: bfmdemod.h:251