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.
filesource.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_FILESOURCE_FILESOURCE_H_
19 #define PLUGINS_CHANNELTX_FILESOURCE_FILESOURCE_H_
20 
21 #include <ctime>
22 #include <iostream>
23 #include <fstream>
24 
25 #include <QObject>
26 #include <QString>
27 #include <QByteArray>
28 #include <QTimer>
29 #include <QNetworkRequest>
30 
32 #include "channel/channelapi.h"
33 #include "util/message.h"
34 #include "util/movingaverage.h"
35 #include "filesourcesettings.h"
36 
38 class UpChannelizer;
39 class DeviceAPI;
40 class FileSourceThread;
41 class QNetworkAccessManager;
42 class QNetworkReply;
43 
44 class FileSource : public BasebandSampleSource, public ChannelAPI {
45  Q_OBJECT
46 
47 public:
50 
51  public:
52  int getLog2Interp() const { return m_log2Interp; }
53  int getFilterChainHash() const { return m_filterChainHash; }
54 
55  static MsgConfigureChannelizer* create(unsigned int m_log2Interp, unsigned int m_filterChainHash) {
56  return new MsgConfigureChannelizer(m_log2Interp, m_filterChainHash);
57  }
58 
59  private:
60  unsigned int m_log2Interp;
61  unsigned int m_filterChainHash;
62 
63  MsgConfigureChannelizer(unsigned int log2Interp, unsigned int filterChainHash) :
64  Message(),
65  m_log2Interp(log2Interp),
66  m_filterChainHash(filterChainHash)
67  { }
68  };
69 
72 
73  public:
74  const FileSourceSettings& getSettings() const { return m_settings; }
75  bool getForce() const { return m_force; }
76 
77  static MsgConfigureFileSource* create(const FileSourceSettings& settings, bool force)
78  {
79  return new MsgConfigureFileSource(settings, force);
80  }
81 
82  private:
84  bool m_force;
85 
86  MsgConfigureFileSource(const FileSourceSettings& settings, bool force) :
87  Message(),
88  m_settings(settings),
89  m_force(force)
90  { }
91  };
92 
95 
96  public:
97  static MsgSampleRateNotification* create(int sampleRate) {
98  return new MsgSampleRateNotification(sampleRate);
99  }
100 
101  int getSampleRate() const { return m_sampleRate; }
102 
103  private:
104 
105  MsgSampleRateNotification(int sampleRate) :
106  Message(),
107  m_sampleRate(sampleRate)
108  { }
109 
111  };
112 
115 
116  public:
117  const QString& getFileName() const { return m_fileName; }
118 
119  static MsgConfigureFileSourceName* create(const QString& fileName)
120  {
121  return new MsgConfigureFileSourceName(fileName);
122  }
123 
124  private:
125  QString m_fileName;
126 
127  MsgConfigureFileSourceName(const QString& fileName) :
128  Message(),
129  m_fileName(fileName)
130  { }
131  };
132 
135 
136  public:
137  bool isWorking() const { return m_working; }
138 
139  static MsgConfigureFileSourceWork* create(bool working)
140  {
141  return new MsgConfigureFileSourceWork(working);
142  }
143 
144  private:
145  bool m_working;
146 
148  Message(),
149  m_working(working)
150  { }
151  };
152 
155 
156  public:
157 
159  {
161  }
162 
163  private:
164 
166  Message()
167  { }
168  };
169 
172 
173  public:
174  int getMillis() const { return m_seekMillis; }
175 
176  static MsgConfigureFileSourceSeek* create(int seekMillis)
177  {
178  return new MsgConfigureFileSourceSeek(seekMillis);
179  }
180 
181  protected:
183 
184  MsgConfigureFileSourceSeek(int seekMillis) :
185  Message(),
186  m_seekMillis(seekMillis)
187  { }
188  };
189 
192 
193  public:
194  bool getAcquisition() const { return m_acquisition; }
195 
196  static MsgReportFileSourceAcquisition* create(bool acquisition)
197  {
198  return new MsgReportFileSourceAcquisition(acquisition);
199  }
200 
201  protected:
203 
204  MsgReportFileSourceAcquisition(bool acquisition) :
205  Message(),
206  m_acquisition(acquisition)
207  { }
208  };
209 
210  class MsgPlayPause : public Message {
212 
213  public:
214  bool getPlayPause() const { return m_playPause; }
215 
216  static MsgPlayPause* create(bool playPause) {
217  return new MsgPlayPause(playPause);
218  }
219 
220  protected:
222 
223  MsgPlayPause(bool playPause) :
224  Message(),
225  m_playPause(playPause)
226  { }
227  };
228 
231 
232  public:
233  int getSampleRate() const { return m_sampleRate; }
234  quint32 getSampleSize() const { return m_sampleSize; }
235  quint64 getCenterFrequency() const { return m_centerFrequency; }
236  quint64 getStartingTimeStamp() const { return m_startingTimeStamp; }
237  quint64 getRecordLength() const { return m_recordLength; }
238 
239  static MsgReportFileSourceStreamData* create(int sampleRate,
240  quint32 sampleSize,
241  quint64 centerFrequency,
242  quint64 startingTimeStamp,
243  quint64 recordLength)
244  {
245  return new MsgReportFileSourceStreamData(sampleRate, sampleSize, centerFrequency, startingTimeStamp, recordLength);
246  }
247 
248  protected:
250  quint32 m_sampleSize;
253  quint64 m_recordLength;
254 
256  quint32 sampleSize,
257  quint64 centerFrequency,
258  quint64 startingTimeStamp,
259  quint64 recordLength) :
260  Message(),
261  m_sampleRate(sampleRate),
262  m_sampleSize(sampleSize),
263  m_centerFrequency(centerFrequency),
264  m_startingTimeStamp(startingTimeStamp),
265  m_recordLength(recordLength)
266  { }
267  };
268 
271 
272  public:
273  quint64 getSamplesCount() const { return m_samplesCount; }
274 
275  static MsgReportFileSourceStreamTiming* create(quint64 samplesCount)
276  {
277  return new MsgReportFileSourceStreamTiming(samplesCount);
278  }
279 
280  protected:
281  quint64 m_samplesCount;
282 
283  MsgReportFileSourceStreamTiming(quint64 samplesCount) :
284  Message(),
285  m_samplesCount(samplesCount)
286  { }
287  };
288 
289  class MsgReportHeaderCRC : public Message {
291 
292  public:
293  bool isOK() const { return m_ok; }
294 
295  static MsgReportHeaderCRC* create(bool ok) {
296  return new MsgReportHeaderCRC(ok);
297  }
298 
299  protected:
300  bool m_ok;
301 
303  Message(),
304  m_ok(ok)
305  { }
306  };
307 
308  FileSource(DeviceAPI *deviceAPI);
309  ~FileSource();
310 
311  virtual void destroy() { delete this; }
312 
313  virtual void pull(Sample& sample);
314  virtual void pullAudio(int nbSamples);
315  virtual void start();
316  virtual void stop();
317  virtual bool handleMessage(const Message& cmd);
318 
319  virtual void getIdentifier(QString& id) { id = objectName(); }
320  virtual void getTitle(QString& title) { title = m_settings.m_title; }
321  virtual qint64 getCenterFrequency() const { return 0; }
322 
323  virtual int getNbSinkStreams() const { return 0; }
324  virtual int getNbSourceStreams() const { return 1; }
325 
326  virtual qint64 getStreamCenterFrequency(int streamIndex, bool sinkElseSource) const
327  {
328  (void) streamIndex;
329  (void) sinkElseSource;
330  return 0;
331  }
332 
333  virtual QByteArray serialize() const;
334  virtual bool deserialize(const QByteArray& data);
335 
336  virtual int webapiSettingsGet(
338  QString& errorMessage);
339 
340  virtual int webapiSettingsPutPatch(
341  bool force,
342  const QStringList& channelSettingsKeys,
344  QString& errorMessage);
345 
346  virtual int webapiReportGet(
348  QString& errorMessage);
349 
351  void setCenterFrequency(uint64_t centerFrequency) { m_centerFrequency = centerFrequency; }
352 
354  void setSampleRate(uint32_t sampleRate) { m_sampleRate = sampleRate; }
355 
356  quint64 getSamplesCount() const { return m_samplesCount; }
357  double getMagSq() const { return m_magsq; }
358 
359  void getMagSqLevels(double& avg, double& peak, int& nbSamples)
360  {
361  if (m_magsqCount > 0)
362  {
366  }
367 
370  nbSamples = m_magsqCount == 0 ? 1 : m_magsqCount;
371 
372  m_magsqSum = 0.0f;
373  m_magsqPeak = 0.0f;
374  m_magsqCount = 0;
375  }
376 
377  static const QString m_channelIdURI;
378  static const QString m_channelId;
379 
380 private:
382  {
384  m_magsq(1e-12),
385  m_magsqPeak(1e-12)
386  {}
387  double m_magsq;
388  double m_magsqPeak;
389  };
390 
392  QMutex m_mutex;
396  std::ifstream m_ifstream;
397  QString m_fileName;
398  quint32 m_sampleSize;
402  quint64 m_samplesCount;
405  quint64 m_recordLength;
408  bool m_running;
409  QNetworkAccessManager *m_networkManager;
410  QNetworkRequest m_networkRequest;
411 
412  double m_linearGain;
413  double m_magsq;
414  double m_magsqSum;
415  double m_magsqPeak;
419 
420  void openFileStream();
421  void seekFileStream(int seekMillis);
422  void handleEOF();
423  void applySettings(const FileSourceSettings& settings, bool force = false);
428  void webapiReverseSendSettings(QList<QString>& channelSettingsKeys, const FileSourceSettings& settings, bool force);
429 
430 private slots:
431  void networkManagerFinished(QNetworkReply *reply);
432 };
433 
434 #endif // PLUGINS_CHANNELTX_FILESOURCE_FILESOURCE_H_
void applySettings(const FileSourceSettings &settings, bool force=false)
Definition: filesource.cpp:465
quint64 m_startingTimeStamp
Definition: filesource.h:406
void setCenterFrequency(uint64_t centerFrequency)
Definition: filesource.h:351
QNetworkAccessManager * m_networkManager
Definition: filesource.h:409
void webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings &response, const FileSourceSettings &settings)
Definition: filesource.cpp:596
static MsgReportHeaderCRC * create(bool ok)
Definition: filesource.h:295
uint32_t m_sampleRate
Definition: filesource.h:403
FileSourceSettings m_settings
Definition: filesource.h:83
quint32 m_sampleSize
Definition: filesource.h:398
static MsgReportFileSourceAcquisition * create(bool acquisition)
Definition: filesource.h:196
DeviceAPI * m_deviceAPI
Definition: filesource.h:391
QString m_fileName
Definition: filesource.h:397
MsgReportFileSourceStreamTiming(quint64 samplesCount)
Definition: filesource.h:283
void setSampleRate(uint32_t sampleRate)
Definition: filesource.h:354
quint64 getSamplesCount() const
Definition: filesource.h:356
bool getPlayPause() const
Definition: filesource.h:214
virtual qint64 getStreamCenterFrequency(int streamIndex, bool sinkElseSource) const
Definition: filesource.h:326
void networkManagerFinished(QNetworkReply *reply)
Definition: filesource.cpp:703
uint32_t m_fileSampleRate
Definition: filesource.h:401
MsgReportFileSourceStreamData(int sampleRate, quint32 sampleSize, quint64 centerFrequency, quint64 startingTimeStamp, quint64 recordLength)
Definition: filesource.h:255
virtual void destroy()
Definition: filesource.h:311
bool m_running
Definition: filesource.h:408
virtual QByteArray serialize() const
Definition: filesource.cpp:332
virtual int webapiReportGet(SWGSDRangel::SWGChannelReport &response, QString &errorMessage)
Definition: filesource.cpp:585
MsgConfigureFileSource(const FileSourceSettings &settings, bool force)
Definition: filesource.h:86
virtual qint64 getCenterFrequency() const
Applies to a default stream.
Definition: filesource.h:321
MsgConfigureFileSourceName(const QString &fileName)
Definition: filesource.h:127
void calculateFrequencyOffset()
Definition: filesource.cpp:509
UpChannelizer * m_channelizer
Definition: filesource.h:394
virtual int getNbSourceStreams() const
Definition: filesource.h:324
void validateFilterChainHash(FileSourceSettings &settings)
Definition: filesource.cpp:498
virtual void pull(Sample &sample)
Definition: filesource.cpp:104
std::ifstream m_ifstream
Definition: filesource.h:396
__int64 int64_t
Definition: rtptypes_win.h:47
QNetworkRequest m_networkRequest
Definition: filesource.h:410
virtual int getNbSinkStreams() const
Definition: filesource.h:323
void handleEOF()
Definition: filesource.cpp:439
unsigned int uint32_t
Definition: rtptypes_win.h:46
double m_magsq
Definition: filesource.h:413
MsgConfigureChannelizer(unsigned int log2Interp, unsigned int filterChainHash)
Definition: filesource.h:63
void getMagSqLevels(double &avg, double &peak, int &nbSamples)
Definition: filesource.h:359
static MsgConfigureFileSourceSeek * create(int seekMillis)
Definition: filesource.h:176
static MsgConfigureFileSource * create(const FileSourceSettings &settings, bool force)
Definition: filesource.h:77
void seekFileStream(int seekMillis)
Definition: filesource.cpp:425
#define MESSAGE_CLASS_DECLARATION
Definition: message.h:43
virtual void getTitle(QString &title)
Definition: filesource.h:320
static const QString m_channelId
Definition: filesource.h:378
virtual int webapiSettingsPutPatch(bool force, const QStringList &channelSettingsKeys, SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
Definition: filesource.cpp:526
static MsgReportFileSourceStreamTiming * create(quint64 samplesCount)
Definition: filesource.h:275
virtual void getIdentifier(QString &id)
Definition: filesource.h:319
virtual bool deserialize(const QByteArray &data)
Definition: filesource.cpp:337
static MsgPlayPause * create(bool playPause)
Definition: filesource.h:216
quint64 m_recordLength
record length in seconds computed from file size
Definition: filesource.h:405
QMutex m_mutex
Definition: filesource.h:392
double getMagSq() const
Definition: filesource.h:357
virtual void stop()
Definition: filesource.cpp:208
MsgReportFileSourceAcquisition(bool acquisition)
Definition: filesource.h:204
static MsgConfigureFileSourceStreamTiming * create()
Definition: filesource.h:158
static MsgConfigureChannelizer * create(unsigned int m_log2Interp, unsigned int m_filterChainHash)
Definition: filesource.h:55
double m_magsqSum
Definition: filesource.h:414
QTimer m_masterTimer
Definition: filesource.h:407
int64_t m_frequencyOffset
Definition: filesource.h:400
ThreadedBasebandSampleSource * m_threadedChannelizer
Definition: filesource.h:393
quint64 m_centerFrequency
Definition: filesource.h:399
virtual bool handleMessage(const Message &cmd)
Processing of a message. Returns true if message has actually been processed.
Definition: filesource.cpp:220
const FileSourceSettings & getSettings() const
Definition: filesource.h:74
const QString & getFileName() const
Definition: filesource.h:117
int m_seekMillis
millis of seek position from the beginning 0..1000
Definition: filesource.h:182
void openFileStream()
Definition: filesource.cpp:355
virtual void pullAudio(int nbSamples)
Definition: filesource.cpp:191
static MsgSampleRateNotification * create(int sampleRate)
Definition: filesource.h:97
FileSourceSettings m_settings
Definition: filesource.h:395
virtual void start()
Definition: filesource.cpp:196
uint32_t m_deviceSampleRate
Definition: filesource.h:404
double m_linearGain
Definition: filesource.h:412
virtual int webapiSettingsGet(SWGSDRangel::SWGChannelSettings &response, QString &errorMessage)
Definition: filesource.cpp:515
int m_magsqCount
Definition: filesource.h:416
static MsgConfigureFileSourceName * create(const QString &fileName)
Definition: filesource.h:119
static MsgReportFileSourceStreamData * create(int sampleRate, quint32 sampleSize, quint64 centerFrequency, quint64 startingTimeStamp, quint64 recordLength)
Definition: filesource.h:239
void webapiReverseSendSettings(QList< QString > &channelSettingsKeys, const FileSourceSettings &settings, bool force)
Definition: filesource.cpp:656
MovingAverageUtil< Real, double, 16 > m_movingAverage
Definition: filesource.h:418
MagSqLevelsStore m_magSqLevelStore
Definition: filesource.h:417
MsgPlayPause(bool playPause)
Definition: filesource.h:223
static const QString m_channelIdURI
Definition: filesource.h:377
void webapiFormatChannelReport(SWGSDRangel::SWGChannelReport &response)
Definition: filesource.cpp:622
FileSource(DeviceAPI *deviceAPI)
Definition: filesource.cpp:62
double m_magsqPeak
Definition: filesource.h:415
static MsgConfigureFileSourceWork * create(bool working)
Definition: filesource.h:139
quint64 m_samplesCount
Definition: filesource.h:402
unsigned __int64 uint64_t
Definition: rtptypes_win.h:48