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.
udpsource.h
Go to the documentation of this file.
1 // Copyright (C) 2017 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 PLUGINS_CHANNELTX_UDPSINK_UDPSOURCE_H_
19 #define PLUGINS_CHANNELTX_UDPSINK_UDPSOURCE_H_
20 
21 #include <QObject>
22 #include <QNetworkRequest>
23 
25 #include "channel/channelapi.h"
26 #include "dsp/basebandsamplesink.h"
27 #include "dsp/interpolator.h"
28 #include "dsp/movingaverage.h"
29 #include "dsp/nco.h"
30 #include "dsp/fftfilt.h"
31 #include "util/message.h"
32 
33 #include "udpsourcesettings.h"
34 #include "udpsourceudphandler.h"
35 
36 class QNetworkAccessManager;
37 class QNetworkReply;
38 class DeviceAPI;
40 class UpChannelizer;
41 
42 class UDPSource : public BasebandSampleSource, public ChannelAPI {
43  Q_OBJECT
44 
45 public:
46  class MsgConfigureUDPSource : public Message {
48 
49  public:
50  const UDPSourceSettings& getSettings() const { return m_settings; }
51  bool getForce() const { return m_force; }
52 
53  static MsgConfigureUDPSource* create(const UDPSourceSettings& settings, bool force)
54  {
55  return new MsgConfigureUDPSource(settings, force);
56  }
57 
58  private:
60  bool m_force;
61 
62  MsgConfigureUDPSource(const UDPSourceSettings& settings, bool force) :
63  Message(),
64  m_settings(settings),
65  m_force(force)
66  {
67  }
68  };
69 
72 
73  public:
74  int getSampleRate() const { return m_sampleRate; }
75  int getCenterFrequency() const { return m_centerFrequency; }
76 
77  static MsgConfigureChannelizer* create(int sampleRate, int centerFrequency)
78  {
79  return new MsgConfigureChannelizer(sampleRate, centerFrequency);
80  }
81 
82  private:
85 
86  MsgConfigureChannelizer(int sampleRate, int centerFrequency) :
87  Message(),
88  m_sampleRate(sampleRate),
89  m_centerFrequency(centerFrequency)
90  { }
91  };
92 
93  UDPSource(DeviceAPI *deviceAPI);
94  virtual ~UDPSource();
95  virtual void destroy() { delete this; }
96 
97  void setSpectrumSink(BasebandSampleSink* spectrum) { m_spectrum = spectrum; }
98 
99  virtual void start();
100  virtual void stop();
101  virtual void pull(Sample& sample);
102  virtual bool handleMessage(const Message& cmd);
103 
104  virtual void getIdentifier(QString& id) { id = objectName(); }
105  virtual void getTitle(QString& title) { title = m_settings.m_title; }
106  virtual qint64 getCenterFrequency() const { return m_settings.m_inputFrequencyOffset; }
107 
108  virtual QByteArray serialize() const;
109  virtual bool deserialize(const QByteArray& data);
110 
111  virtual int getNbSinkStreams() const { return 1; }
112  virtual int getNbSourceStreams() const { return 0; }
113 
114  virtual qint64 getStreamCenterFrequency(int streamIndex, bool sinkElseSource) const
115  {
116  (void) streamIndex;
117  (void) sinkElseSource;
119  }
120 
121  virtual int webapiSettingsGet(
123  QString& errorMessage);
124 
125  virtual int webapiSettingsPutPatch(
126  bool force,
127  const QStringList& channelSettingsKeys,
129  QString& errorMessage);
130 
131  virtual int webapiReportGet(
133  QString& errorMessage);
134 
135  double getMagSq() const { return m_magsq; }
136  double getInMagSq() const { return m_inMagsq; }
138  bool getSquelchOpen() const { return m_squelchOpen; }
139 
140  void setSpectrum(bool enabled);
141  void resetReadIndex();
142 
143  static const QString m_channelIdURI;
144  static const QString m_channelId;
145 
146 signals:
153  void levelChanged(qreal rmsLevel, qreal peakLevel, int numSamples);
154 
155 private slots:
156  void networkManagerFinished(QNetworkReply *reply);
157 
158 private:
159  class MsgUDPSourceSpectrum : public Message {
161 
162  public:
163  bool getEnabled() const { return m_enabled; }
164 
165  static MsgUDPSourceSpectrum* create(bool enabled)
166  {
167  return new MsgUDPSourceSpectrum(enabled);
168  }
169 
170  private:
171  bool m_enabled;
172 
173  MsgUDPSourceSpectrum(bool enabled) :
174  Message(),
175  m_enabled(enabled)
176  { }
177  };
178 
179  class MsgResetReadIndex : public Message {
181 
182  public:
183 
185  {
186  return new MsgResetReadIndex();
187  }
188 
189  private:
190 
192  Message()
193  { }
194  };
195 
199 
204 
206 
209 
215 
220 
221  double m_magsq;
222  double m_inMagsq;
225 
230 
233  double m_levelSum;
235 
240 
241  float m_modPhasor;
245 
246  QNetworkAccessManager *m_networkManager;
247  QNetworkRequest m_networkRequest;
248 
250 
251  static const int m_sampleRateAverageItems = 17;
252  static const int m_ssbFftLen = 1024;
253 
254  void applyChannelSettings(int basebandSampleRate, int outputSampleRate, int inputFrequencyOffset, bool force = false);
255  void applySettings(const UDPSourceSettings& settings, bool force = false);
256  void modulateSample();
257  void calculateLevel(Real sample);
258  void calculateLevel(Complex sample);
259 
262  void webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const UDPSourceSettings& settings, bool force);
263 
264  inline void calculateSquelch(double value)
265  {
266  if ((!m_settings.m_squelchEnabled) || (value > m_squelch))
267  {
268  if (m_squelchThreshold == 0)
269  {
270  m_squelchOpen = true;
271  }
272  else
273  {
274  if (m_squelchOpenCount < m_squelchThreshold)
275  {
276  m_squelchOpenCount++;
277  }
278  else
279  {
280  m_squelchCloseCount = m_squelchThreshold;
281  m_squelchOpen = true;
282  }
283  }
284  }
285  else
286  {
287  if (m_squelchThreshold == 0)
288  {
289  m_squelchOpen = false;
290  }
291  else
292  {
293  if (m_squelchCloseCount > 0)
294  {
295  m_squelchCloseCount--;
296  }
297  else
298  {
299  m_squelchOpenCount = 0;
300  m_squelchOpen = false;
301  }
302  }
303  }
304  }
305 
306  inline void initSquelch(bool open)
307  {
308  if (open)
309  {
310  m_squelchOpen = true;
311  m_squelchOpenCount = m_squelchThreshold;
312  m_squelchCloseCount = m_squelchThreshold;
313  }
314  else
315  {
316  m_squelchOpen = false;
317  m_squelchOpenCount = 0;
318  m_squelchCloseCount = 0;
319  }
320  }
321 
322  inline void readMonoSample(qint16& t)
323  {
324 
325  if (m_settings.m_stereoInput)
326  {
327  AudioSample a;
328  m_udpHandler.readSample(a);
329  t = ((a.l + a.r) * m_settings.m_gainIn) / 2;
330  }
331  else
332  {
333  m_udpHandler.readSample(t);
334  t *= m_settings.m_gainIn;
335  }
336  }
337 };
338 
339 
340 
341 
342 #endif /* PLUGINS_CHANNELTX_UDPSINK_UDPSOURCE_H_ */
float m_modPhasor
Phasor for FM modulation.
Definition: udpsource.h:241
virtual bool deserialize(const QByteArray &data)
Definition: udpsource.cpp:657
double getInMagSq() const
Definition: udpsource.h:136
int m_basebandSampleRate
Definition: udpsource.h:200
std::vector< Sample > SampleVector
Definition: dsptypes.h:96
int m_spectrumChunkCounter
Definition: udpsource.h:214
QNetworkRequest m_networkRequest
Definition: udpsource.h:247
virtual int webapiSettingsPutPatch(bool force, const QStringList &channelSettingsKeys, SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
Definition: udpsource.cpp:685
Complex * m_SSBFilterBuffer
Definition: udpsource.h:243
virtual void destroy()
Definition: udpsource.h:95
static const QString m_channelId
Definition: udpsource.h:144
void applySettings(const UDPSourceSettings &settings, bool force=false)
Definition: udpsource.cpp:492
MsgUDPSourceSpectrum(bool enabled)
Definition: udpsource.h:173
void initSquelch(bool open)
Definition: udpsource.h:306
virtual void stop()
Definition: udpsource.cpp:108
int m_inputFrequencyOffset
Definition: udpsource.h:202
Real m_squelch
Definition: udpsource.h:205
static MsgResetReadIndex * create()
Definition: udpsource.h:184
static const QString m_channelIdURI
Definition: udpsource.h:143
const UDPSourceSettings & getSettings() const
Definition: udpsource.h:50
virtual void pull(Sample &sample)
Definition: udpsource.cpp:113
static const int m_sampleRateAverageItems
Definition: udpsource.h:251
virtual int webapiReportGet(SWGSDRangel::SWGChannelReport &response, QString &errorMessage)
Definition: udpsource.cpp:792
int32_t getBufferGauge() const
void networkManagerFinished(QNetworkReply *reply)
Definition: udpsource.cpp:947
int m_SSBFilterBufferIndex
Definition: udpsource.h:244
Real m_outputSampleRate
Definition: udpsource.h:201
Real m_interpolatorDistanceRemain
Definition: udpsource.h:218
BasebandSampleSink * m_spectrum
Definition: udpsource.h:210
UDPSourceSettings m_settings
Definition: udpsource.h:203
bool getSquelchOpen() const
Definition: udpsource.h:138
static const int m_ssbFftLen
Definition: udpsource.h:252
Complex m_modSample
Definition: udpsource.h:208
int m_squelchThreshold
Definition: udpsource.h:239
QNetworkAccessManager * m_networkManager
Definition: udpsource.h:246
MsgConfigureChannelizer(int sampleRate, int centerFrequency)
Definition: udpsource.h:86
QMutex m_settingsMutex
Definition: udpsource.h:249
void webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings &response, const UDPSourceSettings &settings)
Definition: udpsource.cpp:803
MovingAverage< double > m_inMovingAverage
Definition: udpsource.h:224
MovingAverage< double > m_movingAverage
Definition: udpsource.h:223
void setSpectrum(bool enabled)
Definition: udpsource.cpp:450
virtual void getTitle(QString &title)
Definition: udpsource.h:105
double m_sampleRateSum
Definition: udpsource.h:228
double m_magsq
Definition: udpsource.h:221
void readMonoSample(qint16 &t)
Definition: udpsource.h:322
void webapiReverseSendSettings(QList< QString > &channelSettingsKeys, const UDPSourceSettings &settings, bool force)
Definition: udpsource.cpp:858
void webapiFormatChannelReport(SWGSDRangel::SWGChannelReport &response)
Definition: udpsource.cpp:849
Real m_actualInputSampleRate
sample rate with UDP buffer skew compensation
Definition: udpsource.h:227
void readSample(qint16 &t)
audio mono
int m_spectrumChunkSize
Definition: udpsource.h:213
UDPSourceUDPHandler m_udpHandler
Definition: udpsource.h:226
int32_t getBufferGauge() const
Definition: udpsource.h:137
Definition: nco.h:25
qint16 r
Definition: dsptypes.h:92
int m_levelNbSamples
Definition: udpsource.h:234
int m_levelCalcCount
Definition: udpsource.h:231
double m_levelSum
Definition: udpsource.h:233
fftfilt * m_SSBFilter
Complex filter for SSB modulation.
Definition: udpsource.h:242
bool m_squelchOpen
Definition: udpsource.h:236
virtual ~UDPSource()
Definition: udpsource.cpp:90
#define MESSAGE_CLASS_DECLARATION
Definition: message.h:43
virtual int webapiSettingsGet(SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
Definition: udpsource.cpp:674
bool m_spectrumEnabled
Definition: udpsource.h:211
virtual int getNbSinkStreams() const
Definition: udpsource.h:111
int int32_t
Definition: rtptypes_win.h:45
virtual int getNbSourceStreams() const
Definition: udpsource.h:112
void setSpectrumSink(BasebandSampleSink *spectrum)
Definition: udpsource.h:97
virtual void getIdentifier(QString &id)
Definition: udpsource.h:104
int m_squelchOpenCount
Definition: udpsource.h:237
int m_squelchCloseCount
Definition: udpsource.h:238
virtual qint64 getCenterFrequency() const
Applies to a default stream.
Definition: udpsource.h:106
NCO m_carrierNco
Definition: udpsource.h:207
bool m_interpolatorConsumed
Definition: udpsource.h:219
void resetReadIndex()
Definition: udpsource.cpp:456
virtual qint64 getStreamCenterFrequency(int streamIndex, bool sinkElseSource) const
Definition: udpsource.h:114
virtual bool handleMessage(const Message &cmd)
Processing of a message. Returns true if message has actually been processed.
Definition: udpsource.cpp:330
int m_sampleRateAvgCounter
Definition: udpsource.h:229
DeviceAPI * m_deviceAPI
Definition: udpsource.h:196
qint16 l
Definition: dsptypes.h:91
UDPSource(DeviceAPI *deviceAPI)
Definition: udpsource.cpp:44
static MsgUDPSourceSpectrum * create(bool enabled)
Definition: udpsource.h:165
virtual void start()
Definition: udpsource.cpp:102
void applyChannelSettings(int basebandSampleRate, int outputSampleRate, int inputFrequencyOffset, bool force=false)
Definition: udpsource.cpp:462
double getMagSq() const
Definition: udpsource.h:135
Real m_interpolatorDistance
Definition: udpsource.h:217
Interpolator m_interpolator
Definition: udpsource.h:216
void levelChanged(qreal rmsLevel, qreal peakLevel, int numSamples)
double m_inMagsq
Definition: udpsource.h:222
ThreadedBasebandSampleSource * m_threadedChannelizer
Definition: udpsource.h:197
SampleVector m_sampleBuffer
Definition: udpsource.h:212
Real m_peakLevel
Definition: udpsource.h:232
void calculateSquelch(double value)
Definition: udpsource.h:264
void modulateSample()
Definition: udpsource.cpp:159
std::complex< Real > Complex
Definition: dsptypes.h:43
UpChannelizer * m_channelizer
Definition: udpsource.h:198
float Real
Definition: dsptypes.h:42
MsgConfigureUDPSource(const UDPSourceSettings &settings, bool force)
Definition: udpsource.h:62
void calculateLevel(Real sample)
Definition: udpsource.cpp:291
static MsgConfigureUDPSource * create(const UDPSourceSettings &settings, bool force)
Definition: udpsource.h:53
virtual QByteArray serialize() const
Definition: udpsource.cpp:652
static MsgConfigureChannelizer * create(int sampleRate, int centerFrequency)
Definition: udpsource.h:77
UDPSourceSettings m_settings
Definition: udpsource.h:59