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.
freedvmod.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 PLUGINS_CHANNELTX_MODFREEDV_FREEDVMOD_H_
19 #define PLUGINS_CHANNELTX_MODFREEDV_FREEDVMOD_H_
20 
21 #include <vector>
22 #include <iostream>
23 #include <fstream>
24 
25 #include <QMutex>
26 #include <QNetworkRequest>
27 
29 #include "channel/channelapi.h"
30 #include "dsp/basebandsamplesink.h"
31 #include "dsp/ncof.h"
32 #include "dsp/interpolator.h"
33 #include "util/movingaverage.h"
34 #include "dsp/agc.h"
35 #include "dsp/fftfilt.h"
36 #include "dsp/cwkeyer.h"
37 #include "audio/audiofifo.h"
38 #include "audio/audioresampler.h"
39 #include "util/message.h"
40 
41 #include "freedvmodsettings.h"
42 
43 class QNetworkAccessManager;
44 class QNetworkReply;
45 class DeviceAPI;
47 class UpChannelizer;
48 
49 struct freedv;
50 
51 class FreeDVMod : public BasebandSampleSource, public ChannelAPI {
52  Q_OBJECT
53 
54 public:
55  class MsgConfigureFreeDVMod : public Message {
57 
58  public:
59  const FreeDVModSettings& getSettings() const { return m_settings; }
60  bool getForce() const { return m_force; }
61 
62  static MsgConfigureFreeDVMod* create(const FreeDVModSettings& settings, bool force)
63  {
64  return new MsgConfigureFreeDVMod(settings, force);
65  }
66 
67  private:
69  bool m_force;
70 
71  MsgConfigureFreeDVMod(const FreeDVModSettings& settings, bool force) :
72  Message(),
73  m_settings(settings),
74  m_force(force)
75  { }
76  };
77 
80 
81  public:
82  int getSampleRate() const { return m_sampleRate; }
83  int getCenterFrequency() const { return m_centerFrequency; }
84 
85  static MsgConfigureChannelizer* create(int sampleRate, int centerFrequency)
86  {
87  return new MsgConfigureChannelizer(sampleRate, centerFrequency);
88  }
89 
90  private:
93 
94  MsgConfigureChannelizer(int sampleRate, int centerFrequency) :
95  Message(),
96  m_sampleRate(sampleRate),
97  m_centerFrequency(centerFrequency)
98  { }
99  };
100 
102  {
104 
105  public:
106  const QString& getFileName() const { return m_fileName; }
107 
108  static MsgConfigureFileSourceName* create(const QString& fileName)
109  {
110  return new MsgConfigureFileSourceName(fileName);
111  }
112 
113  private:
114  QString m_fileName;
115 
116  MsgConfigureFileSourceName(const QString& fileName) :
117  Message(),
118  m_fileName(fileName)
119  { }
120  };
121 
123  {
125 
126  public:
127  int getPercentage() const { return m_seekPercentage; }
128 
129  static MsgConfigureFileSourceSeek* create(int seekPercentage)
130  {
131  return new MsgConfigureFileSourceSeek(seekPercentage);
132  }
133 
134  protected:
136 
137  MsgConfigureFileSourceSeek(int seekPercentage) :
138  Message(),
139  m_seekPercentage(seekPercentage)
140  { }
141  };
142 
145 
146  public:
147 
149  {
151  }
152 
153  private:
154 
156  Message()
157  { }
158  };
159 
161  {
163 
164  public:
165  std::size_t getSamplesCount() const { return m_samplesCount; }
166 
167  static MsgReportFileSourceStreamTiming* create(std::size_t samplesCount)
168  {
169  return new MsgReportFileSourceStreamTiming(samplesCount);
170  }
171 
172  protected:
173  std::size_t m_samplesCount;
174 
175  MsgReportFileSourceStreamTiming(std::size_t samplesCount) :
176  Message(),
177  m_samplesCount(samplesCount)
178  { }
179  };
180 
183 
184  public:
185  int getSampleRate() const { return m_sampleRate; }
186  quint32 getRecordLength() const { return m_recordLength; }
187 
188  static MsgReportFileSourceStreamData* create(int sampleRate,
189  quint32 recordLength)
190  {
191  return new MsgReportFileSourceStreamData(sampleRate, recordLength);
192  }
193 
194  protected:
196  quint32 m_recordLength;
197 
199  quint32 recordLength) :
200  Message(),
201  m_sampleRate(sampleRate),
202  m_recordLength(recordLength)
203  { }
204  };
205 
206  //=================================================================
207 
208  FreeDVMod(DeviceAPI *deviceAPI);
209  ~FreeDVMod();
210  virtual void destroy() { delete this; }
211 
212  void setSpectrumSampleSink(BasebandSampleSink* sampleSink) { m_sampleSink = sampleSink; }
213 
214  virtual void pull(Sample& sample);
215  virtual void pullAudio(int nbSamples);
216  virtual void start();
217  virtual void stop();
218  virtual bool handleMessage(const Message& cmd);
219 
220  virtual void getIdentifier(QString& id) { id = objectName(); }
221  virtual void getTitle(QString& title) { title = m_settings.m_title; }
222  virtual qint64 getCenterFrequency() const { return m_settings.m_inputFrequencyOffset; }
223 
224  virtual QByteArray serialize() const;
225  virtual bool deserialize(const QByteArray& data);
226 
227  virtual int getNbSinkStreams() const { return 1; }
228  virtual int getNbSourceStreams() const { return 0; }
229 
230  virtual qint64 getStreamCenterFrequency(int streamIndex, bool sinkElseSource) const
231  {
232  (void) streamIndex;
233  (void) sinkElseSource;
235  }
236 
237  virtual int webapiSettingsGet(
239  QString& errorMessage);
240 
241  virtual int webapiSettingsPutPatch(
242  bool force,
243  const QStringList& channelSettingsKeys,
245  QString& errorMessage);
246 
247  virtual int webapiReportGet(
249  QString& errorMessage);
250 
253  double getMagSq() const { return m_magsq; }
254  Real getLowCutoff() const { return m_lowCutoff; }
255  Real getHiCutoff() const { return m_hiCutoff; }
256 
257  CWKeyer *getCWKeyer() { return &m_cwKeyer; }
258 
259  static const QString m_channelIdURI;
260  static const QString m_channelId;
261 
262 signals:
269  void levelChanged(qreal rmsLevel, qreal peakLevel, int numSamples);
270 
271 
272 private:
273  enum RateState {
276  };
277 
281 
289 
300  static const int m_ssbFftLen;
301 
304 
308 
309  double m_magsq;
311 
316 
318 
319  std::ifstream m_ifstream;
320  QString m_fileName;
321  quint64 m_fileSize;
322  quint32 m_recordLength;
324 
329 
330  QNetworkAccessManager *m_networkManager;
331  QNetworkRequest m_networkRequest;
332 
333  struct freedv *m_freeDV;
337  int m_iModem;
342 
343  static const int m_levelNbSamples;
344 
345  void applyAudioSampleRate(int sampleRate);
346  void applyChannelSettings(int basebandSampleRate, int outputSampleRate, int inputFrequencyOffset, bool force = false);
347  void applySettings(const FreeDVModSettings& settings, bool force = false);
349  void pullAF(Complex& sample);
350  void calculateLevel(Complex& sample);
351  void calculateLevel(qint16& sample);
352  void modulateSample();
353  void openFileStream();
354  void seekFileStream(int seekPercentage);
357  void webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const FreeDVModSettings& settings, bool force);
358  void webapiReverseSendCWSettings(const CWKeyerSettings& settings);
359 
360 private slots:
361  void networkManagerFinished(QNetworkReply *reply);
362 };
363 
364 
365 #endif /* PLUGINS_CHANNELTX_MODFREEDV_FREEDVMOD_H_ */
short int16_t
Definition: rtptypes_win.h:43
QNetworkRequest m_networkRequest
Definition: freedvmod.h:331
void webapiReverseSendSettings(QList< QString > &channelSettingsKeys, const FreeDVModSettings &settings, bool force)
Definition: freedvmod.cpp:1014
CWKeyer * getCWKeyer()
Definition: freedvmod.h:257
std::vector< Sample > SampleVector
Definition: dsptypes.h:96
Real m_interpolatorDistance
Definition: freedvmod.h:294
virtual void start()
Definition: freedvmod.cpp:417
static MsgConfigureFileSourceSeek * create(int seekPercentage)
Definition: freedvmod.h:129
NCOF m_toneNco
Definition: freedvmod.h:291
std::vector< AudioSample > AudioVector
Definition: dsptypes.h:98
FreeDVMod(DeviceAPI *deviceAPI)
Definition: freedvmod.cpp:57
static MsgConfigureFreeDVMod * create(const FreeDVModSettings &settings, bool force)
Definition: freedvmod.h:62
int m_inputSampleRate
speech (input) sample rate (fixed 8000 S/s)
Definition: freedvmod.h:323
void applyFreeDVMode(FreeDVModSettings::FreeDVMode mode)
Definition: freedvmod.cpp:610
quint32 m_audioSampleRate
Definition: freedvmod.h:312
static MsgConfigureFileSourceStreamTiming * create()
Definition: freedvmod.h:148
const QString & getFileName() const
Definition: freedvmod.h:106
Real m_interpolatorDistanceRemain
Definition: freedvmod.h:295
CWKeyer m_cwKeyer
Definition: freedvmod.h:328
int16_t * m_speechIn
Definition: freedvmod.h:338
virtual qint64 getStreamCenterFrequency(int streamIndex, bool sinkElseSource) const
Definition: freedvmod.h:230
virtual void destroy()
Definition: freedvmod.h:210
int m_nNomModemSamples
Definition: freedvmod.h:335
Real getHiCutoff() const
Definition: freedvmod.h:255
Real m_hiCutoff
Definition: freedvmod.h:287
int m_iModem
Definition: freedvmod.h:337
AudioResampler m_audioResampler
Definition: freedvmod.h:341
virtual int getNbSinkStreams() const
Definition: freedvmod.h:227
Definition: ncof.h:24
int m_SSBFilterBufferIndex
Definition: freedvmod.h:299
uint32_t getAudioSampleRate() const
Definition: freedvmod.h:251
Complex m_modSample
Definition: freedvmod.h:292
void networkManagerFinished(QNetworkReply *reply)
Definition: freedvmod.cpp:1121
void applySettings(const FreeDVModSettings &settings, bool force=false)
Definition: freedvmod.cpp:738
void pullAF(Complex &sample)
Definition: freedvmod.cpp:203
void webapiFormatChannelReport(SWGSDRangel::SWGChannelReport &response)
Definition: freedvmod.cpp:1007
int m_inputFrequencyOffset
Definition: freedvmod.h:285
uint m_audioBufferFill
Definition: freedvmod.h:314
virtual int webapiSettingsPutPatch(bool force, const QStringList &channelSettingsKeys, SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
Definition: freedvmod.cpp:848
ThreadedBasebandSampleSource * m_threadedChannelizer
Definition: freedvmod.h:279
static const int m_ssbFftLen
Definition: freedvmod.h:300
virtual void getTitle(QString &title)
Definition: freedvmod.h:221
virtual int webapiSettingsGet(SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
Definition: freedvmod.cpp:837
const FreeDVModSettings & getSettings() const
Definition: freedvmod.h:59
void setSpectrumSampleSink(BasebandSampleSink *sampleSink)
Definition: freedvmod.h:212
static MsgReportFileSourceStreamData * create(int sampleRate, quint32 recordLength)
Definition: freedvmod.h:188
int m_undersampleCount
Definition: freedvmod.h:306
quint32 m_recordLength
record length in seconds computed from file size
Definition: freedvmod.h:322
static MsgReportFileSourceStreamTiming * create(std::size_t samplesCount)
Definition: freedvmod.h:167
virtual void getIdentifier(QString &id)
Definition: freedvmod.h:220
int m_iSpeech
Definition: freedvmod.h:336
float m_scaleFactor
divide by this amount to scale from int16 to float in [-1.0, 1.0] interval
Definition: freedvmod.h:340
bool m_interpolatorConsumed
Definition: freedvmod.h:296
MsgConfigureFreeDVMod(const FreeDVModSettings &settings, bool force)
Definition: freedvmod.h:71
std::complex< float > cmplx
Definition: fftfilt.h:21
unsigned int uint32_t
Definition: rtptypes_win.h:46
fftfilt::cmplx m_sum
Definition: freedvmod.h:305
DeviceAPI * m_deviceAPI
Definition: freedvmod.h:278
FreeDVModSettings m_settings
Definition: freedvmod.h:288
void applyChannelSettings(int basebandSampleRate, int outputSampleRate, int inputFrequencyOffset, bool force=false)
Definition: freedvmod.cpp:580
double getMagSq() const
Definition: freedvmod.h:253
Interpolator m_interpolator
Definition: freedvmod.h:293
int m_outputSampleRate
Definition: freedvmod.h:283
void openFileStream()
Definition: freedvmod.cpp:530
virtual void stop()
Definition: freedvmod.cpp:426
std::ifstream m_ifstream
Definition: freedvmod.h:319
static const QString m_channelId
Definition: freedvmod.h:260
void seekFileStream(int seekPercentage)
Definition: freedvmod.cpp:554
static const int m_levelNbSamples
Definition: freedvmod.h:343
#define MESSAGE_CLASS_DECLARATION
Definition: message.h:43
AudioVector m_audioBuffer
Definition: freedvmod.h:313
MovingAverageUtil< double, double, 16 > m_movingAverage
Definition: freedvmod.h:310
QMutex m_settingsMutex
Definition: freedvmod.h:317
QNetworkAccessManager * m_networkManager
Definition: freedvmod.h:330
MsgConfigureFileSourceName(const QString &fileName)
Definition: freedvmod.h:116
AudioFifo m_audioFifo
Definition: freedvmod.h:315
int m_sumCount
Definition: freedvmod.h:307
Real getLowCutoff() const
Definition: freedvmod.h:254
MsgReportFileSourceStreamData(int sampleRate, quint32 recordLength)
Definition: freedvmod.h:198
quint64 m_fileSize
raw file size (bytes)
Definition: freedvmod.h:321
void webapiReverseSendCWSettings(const CWKeyerSettings &settings)
Definition: freedvmod.cpp:1090
virtual void pullAudio(int nbSamples)
Definition: freedvmod.cpp:181
void applyAudioSampleRate(int sampleRate)
Definition: freedvmod.cpp:567
SampleVector m_sampleBuffer
Definition: freedvmod.h:303
void modulateSample()
Definition: freedvmod.cpp:194
MsgConfigureFileSourceSeek(int seekPercentage)
Definition: freedvmod.h:137
virtual int webapiReportGet(SWGSDRangel::SWGChannelReport &response, QString &errorMessage)
Definition: freedvmod.cpp:949
virtual void pull(Sample &sample)
Definition: freedvmod.cpp:142
double m_magsq
Definition: freedvmod.h:309
int16_t * m_modOut
Definition: freedvmod.h:339
struct freedv * m_freeDV
Definition: freedvmod.h:333
NCOF m_carrierNco
Definition: freedvmod.h:290
virtual int getNbSourceStreams() const
Definition: freedvmod.h:228
void levelChanged(qreal rmsLevel, qreal peakLevel, int numSamples)
static MsgConfigureChannelizer * create(int sampleRate, int centerFrequency)
Definition: freedvmod.h:85
MsgConfigureChannelizer(int sampleRate, int centerFrequency)
Definition: freedvmod.h:94
void calculateLevel(Complex &sample)
Definition: freedvmod.cpp:375
virtual bool handleMessage(const Message &cmd)
Processing of a message. Returns true if message has actually been processed.
Definition: freedvmod.cpp:430
MsgReportFileSourceStreamTiming(std::size_t samplesCount)
Definition: freedvmod.h:175
static const QString m_channelIdURI
Definition: freedvmod.h:259
int m_seekPercentage
percentage of seek position from the beginning 0..100
Definition: freedvmod.h:135
void webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings &response, const FreeDVModSettings &settings)
Definition: freedvmod.cpp:960
virtual bool deserialize(const QByteArray &data)
Definition: freedvmod.cpp:820
static MsgConfigureFileSourceName * create(const QString &fileName)
Definition: freedvmod.h:108
virtual QByteArray serialize() const
Definition: freedvmod.cpp:815
int m_modemSampleRate
Definition: freedvmod.h:284
QString m_fileName
Definition: freedvmod.h:320
std::complex< Real > Complex
Definition: dsptypes.h:43
Real m_levelSum
Definition: freedvmod.h:327
uint32_t getModemSampleRate() const
Definition: freedvmod.h:252
fftfilt * m_SSBFilter
Definition: freedvmod.h:297
float Real
Definition: dsptypes.h:42
int m_basebandSampleRate
Definition: freedvmod.h:282
Real m_lowCutoff
Definition: freedvmod.h:286
UpChannelizer * m_channelizer
Definition: freedvmod.h:280
virtual qint64 getCenterFrequency() const
Applies to a default stream.
Definition: freedvmod.h:222
int m_nSpeechSamples
Definition: freedvmod.h:334
BasebandSampleSink * m_sampleSink
Definition: freedvmod.h:302
Real m_peakLevel
Definition: freedvmod.h:326
Complex * m_SSBFilterBuffer
Definition: freedvmod.h:298
FreeDVModSettings m_settings
Definition: freedvmod.h:68
quint32 m_levelCalcCount
Definition: freedvmod.h:325