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.
Public Member Functions | Private Slots | Private Member Functions | Private Attributes | List of all members
FileSinkThread Class Reference

#include <filesinkthread.h>

Inherits QThread.

+ Collaboration diagram for FileSinkThread:

Public Member Functions

 FileSinkThread (std::ofstream *samplesStream, SampleSourceFifo *sampleFifo, QObject *parent=0)
 
 ~FileSinkThread ()
 
void startWork ()
 
void stopWork ()
 
void setSamplerate (int samplerate)
 
void setLog2Interpolation (int log2Interpolation)
 
void setBuffer (std::size_t chunksize)
 
bool isRunning () const
 
std::size_t getSamplesCount () const
 
void setSamplesCount (int samplesCount)
 
void connectTimer (const QTimer &timer)
 

Private Slots

void tick ()
 

Private Member Functions

void run ()
 

Private Attributes

QMutex m_startWaitMutex
 
QWaitCondition m_startWaiter
 
volatile bool m_running
 
std::ofstream * m_ofstream
 
std::size_t m_bufsize
 
unsigned int m_samplesChunkSize
 
SampleSourceFifom_sampleFifo
 
std::size_t m_samplesCount
 
int m_samplerate
 
int m_log2Interpolation
 
int m_throttlems
 
int m_maxThrottlems
 
QElapsedTimer m_elapsedTimer
 
bool m_throttleToggle
 
Interpolators< qint16, SDR_TX_SAMP_SZ, 16 > m_interpolators
 
int16_tm_buf
 

Detailed Description

Definition at line 38 of file filesinkthread.h.

Constructor & Destructor Documentation

◆ FileSinkThread()

FileSinkThread::FileSinkThread ( std::ofstream *  samplesStream,
SampleSourceFifo sampleFifo,
QObject *  parent = 0 
)

Definition at line 27 of file filesinkthread.cpp.

References m_ofstream.

27  :
28  QThread(parent),
29  m_running(false),
30  m_ofstream(samplesStream),
31  m_bufsize(0),
33  m_sampleFifo(sampleFifo),
34  m_samplesCount(0),
35  m_samplerate(0),
38  m_maxThrottlems(50),
39  m_throttleToggle(false),
40  m_buf(0)
41 {
42  assert(m_ofstream != 0);
43 }
#define FILESINK_THROTTLE_MS
volatile bool m_running
unsigned int m_samplesChunkSize
int16_t * m_buf
SampleSourceFifo * m_sampleFifo
std::size_t m_samplesCount
std::size_t m_bufsize
std::ofstream * m_ofstream

◆ ~FileSinkThread()

FileSinkThread::~FileSinkThread ( )

Definition at line 45 of file filesinkthread.cpp.

References m_buf, m_running, and stopWork().

46 {
47  if (m_running) {
48  stopWork();
49  }
50 
51  if (m_buf) delete[] m_buf;
52 }
volatile bool m_running
int16_t * m_buf
+ Here is the call graph for this function:

Member Function Documentation

◆ connectTimer()

void FileSinkThread::connectTimer ( const QTimer &  timer)

Definition at line 162 of file filesinkthread.cpp.

References tick().

Referenced by setSamplesCount(), and FileSinkOutput::start().

163 {
164  qDebug() << "FileSinkThread::connectTimer";
165  connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));
166 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getSamplesCount()

std::size_t FileSinkThread::getSamplesCount ( ) const
inline

Definition at line 51 of file filesinkthread.h.

References m_samplesCount.

Referenced by FileSinkOutput::handleMessage().

51 { return m_samplesCount; }
std::size_t m_samplesCount
+ Here is the caller graph for this function:

◆ isRunning()

bool FileSinkThread::isRunning ( ) const
inline

Definition at line 50 of file filesinkthread.h.

References m_running.

50 { return m_running; }
volatile bool m_running

◆ run()

void FileSinkThread::run ( )
private

Definition at line 149 of file filesinkthread.cpp.

References m_running, and m_startWaiter.

150 {
151  m_running = true;
152  m_startWaiter.wakeAll();
153 
154  while(m_running) // actual work is in the tick() function
155  {
156  sleep(1);
157  }
158 
159  m_running = false;
160 }
QWaitCondition m_startWaiter
volatile bool m_running

◆ setBuffer()

void FileSinkThread::setBuffer ( std::size_t  chunksize)

◆ setLog2Interpolation()

void FileSinkThread::setLog2Interpolation ( int  log2Interpolation)

Definition at line 116 of file filesinkthread.cpp.

References m_buf, m_log2Interpolation, m_running, m_samplerate, startWork(), and stopWork().

Referenced by FileSinkOutput::applySettings(), and FileSinkOutput::start().

117 {
118  if ((log2Interpolation < 0) || (log2Interpolation > 6))
119  {
120  return;
121  }
122 
123  if (log2Interpolation != m_log2Interpolation)
124  {
125  qDebug() << "FileSinkThread::setLog2Interpolation:"
126  << " new:" << log2Interpolation
127  << " old:" << m_log2Interpolation;
128 
129  bool wasRunning = false;
130 
131  if (m_running)
132  {
133  stopWork();
134  wasRunning = true;
135  }
136 
137  // resize output buffer
138  if (m_buf) delete[] m_buf;
139  m_buf = new int16_t[m_samplerate*(1<<log2Interpolation)*2];
140 
141  m_log2Interpolation = log2Interpolation;
142 
143  if (wasRunning) {
144  startWork();
145  }
146  }
147 }
short int16_t
Definition: rtptypes_win.h:43
volatile bool m_running
int16_t * m_buf
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setSamplerate()

void FileSinkThread::setSamplerate ( int  samplerate)

Definition at line 82 of file filesinkthread.cpp.

References m_buf, m_log2Interpolation, m_running, m_sampleFifo, m_samplerate, m_samplesChunkSize, m_throttlems, SampleSourceFifo::resize(), startWork(), and stopWork().

Referenced by FileSinkOutput::applySettings(), and FileSinkOutput::start().

83 {
84  if (samplerate != m_samplerate)
85  {
86  qDebug() << "FileSinkThread::setSamplerate:"
87  << " new:" << samplerate
88  << " old:" << m_samplerate;
89 
90  bool wasRunning = false;
91 
92  if (m_running)
93  {
94  stopWork();
95  wasRunning = true;
96  }
97 
98  // resize sample FIFO
99  if (m_sampleFifo) {
100  m_sampleFifo->resize(samplerate); // 1s buffer
101  }
102 
103  // resize output buffer
104  if (m_buf) delete[] m_buf;
105  m_buf = new int16_t[samplerate*(1<<m_log2Interpolation)*2];
106 
107  m_samplerate = samplerate;
108  m_samplesChunkSize = (m_samplerate * m_throttlems) / 1000;
109 
110  if (wasRunning) {
111  startWork();
112  }
113  }
114 }
short int16_t
Definition: rtptypes_win.h:43
volatile bool m_running
unsigned int m_samplesChunkSize
int16_t * m_buf
void resize(uint32_t size)
SampleSourceFifo * m_sampleFifo
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setSamplesCount()

void FileSinkThread::setSamplesCount ( int  samplesCount)
inline

Definition at line 52 of file filesinkthread.h.

References connectTimer(), and m_samplesCount.

52 { m_samplesCount = samplesCount; }
std::size_t m_samplesCount
+ Here is the call graph for this function:

◆ startWork()

void FileSinkThread::startWork ( )

Definition at line 54 of file filesinkthread.cpp.

References m_elapsedTimer, m_maxThrottlems, m_ofstream, m_running, m_startWaiter, and m_startWaitMutex.

Referenced by FileSinkOutput::handleMessage(), setLog2Interpolation(), setSamplerate(), and FileSinkOutput::start().

55 {
56  qDebug() << "FileSinkThread::startWork: ";
57 
58  if (m_ofstream->is_open())
59  {
60  qDebug() << "FileSinkThread::startWork: file stream open, starting...";
61  m_maxThrottlems = 0;
62  m_startWaitMutex.lock();
63  m_elapsedTimer.start();
64  start();
65  while(!m_running)
66  m_startWaiter.wait(&m_startWaitMutex, 100);
67  m_startWaitMutex.unlock();
68  }
69  else
70  {
71  qDebug() << "FileSinkThread::startWork: file stream closed, not starting.";
72  }
73 }
QWaitCondition m_startWaiter
volatile bool m_running
QElapsedTimer m_elapsedTimer
QMutex m_startWaitMutex
std::ofstream * m_ofstream
+ Here is the caller graph for this function:

◆ stopWork()

void FileSinkThread::stopWork ( )

Definition at line 75 of file filesinkthread.cpp.

References m_running.

Referenced by FileSinkOutput::handleMessage(), setLog2Interpolation(), setSamplerate(), FileSinkOutput::stop(), and ~FileSinkThread().

76 {
77  qDebug() << "FileSinkThread::stopWork";
78  m_running = false;
79  wait();
80 }
volatile bool m_running
+ Here is the caller graph for this function:

◆ tick

void FileSinkThread::tick ( )
privateslot

Definition at line 168 of file filesinkthread.cpp.

References Interpolators< T, SdrBits, OutputBits >::interpolate16_cen(), Interpolators< T, SdrBits, OutputBits >::interpolate2_cen(), Interpolators< T, SdrBits, OutputBits >::interpolate32_cen(), Interpolators< T, SdrBits, OutputBits >::interpolate4_cen(), Interpolators< T, SdrBits, OutputBits >::interpolate64_cen(), Interpolators< T, SdrBits, OutputBits >::interpolate8_cen(), m_buf, m_elapsedTimer, m_interpolators, m_log2Interpolation, m_ofstream, m_running, m_sampleFifo, m_samplerate, m_samplesChunkSize, m_samplesCount, m_throttlems, m_throttleToggle, leansdr::min(), and SampleSourceFifo::readAdvance().

Referenced by connectTimer().

169 {
170  if (m_running)
171  {
172  qint64 throttlems = m_elapsedTimer.restart();
173 
174  if (throttlems != m_throttlems)
175  {
176  m_throttlems = throttlems;
179  }
180 
181 // if (m_throttlems > m_maxThrottlems)
182 // {
183 // qDebug("FileSinkThread::tick: m_maxThrottlems: %d", m_maxThrottlems);
184 // m_maxThrottlems = m_throttlems;
185 // }
186 
187  SampleVector::iterator readUntil;
188 
190  SampleVector::iterator beginRead = readUntil - m_samplesChunkSize;
192 
193  if (m_log2Interpolation == 0)
194  {
195  m_ofstream->write(reinterpret_cast<char*>(&(*beginRead)), m_samplesChunkSize*sizeof(Sample));
196  }
197  else
198  {
199  int chunkSize = std::min((int) m_samplesChunkSize, m_samplerate);
200 
201  switch (m_log2Interpolation)
202  {
203  case 1:
204  m_interpolators.interpolate2_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2);
205  break;
206  case 2:
207  m_interpolators.interpolate4_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2);
208  break;
209  case 3:
210  m_interpolators.interpolate8_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2);
211  break;
212  case 4:
213  m_interpolators.interpolate16_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2);
214  break;
215  case 5:
216  m_interpolators.interpolate32_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2);
217  break;
218  case 6:
219  m_interpolators.interpolate64_cen(&beginRead, m_buf, chunkSize*(1<<m_log2Interpolation)*2);
220  break;
221  default:
222  break;
223  }
224 
225  m_ofstream->write(reinterpret_cast<char*>(m_buf), m_samplesChunkSize*(1<<m_log2Interpolation)*2*sizeof(int16_t));
226  }
227 
228  }
229 }
short int16_t
Definition: rtptypes_win.h:43
void interpolate64_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
volatile bool m_running
void interpolate32_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void interpolate8_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
unsigned int m_samplesChunkSize
int16_t * m_buf
void interpolate2_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void readAdvance(SampleVector::iterator &readUntil, unsigned int nbSamples)
QElapsedTimer m_elapsedTimer
SampleSourceFifo * m_sampleFifo
std::size_t m_samplesCount
Interpolators< qint16, SDR_TX_SAMP_SZ, 16 > m_interpolators
void interpolate16_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
void interpolate4_cen(SampleVector::iterator *it, T *buf, qint32 len, bool invertIQ=false)
std::ofstream * m_ofstream
T min(const T &x, const T &y)
Definition: framework.h:440
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_buf

int16_t* FileSinkThread::m_buf
private

Definition at line 75 of file filesinkthread.h.

Referenced by setLog2Interpolation(), setSamplerate(), tick(), and ~FileSinkThread().

◆ m_bufsize

std::size_t FileSinkThread::m_bufsize
private

Definition at line 62 of file filesinkthread.h.

◆ m_elapsedTimer

QElapsedTimer FileSinkThread::m_elapsedTimer
private

Definition at line 71 of file filesinkthread.h.

Referenced by startWork(), and tick().

◆ m_interpolators

Interpolators<qint16, SDR_TX_SAMP_SZ, 16> FileSinkThread::m_interpolators
private

Definition at line 74 of file filesinkthread.h.

Referenced by tick().

◆ m_log2Interpolation

int FileSinkThread::m_log2Interpolation
private

Definition at line 68 of file filesinkthread.h.

Referenced by setLog2Interpolation(), setSamplerate(), and tick().

◆ m_maxThrottlems

int FileSinkThread::m_maxThrottlems
private

Definition at line 70 of file filesinkthread.h.

Referenced by startWork().

◆ m_ofstream

std::ofstream* FileSinkThread::m_ofstream
private

Definition at line 61 of file filesinkthread.h.

Referenced by FileSinkThread(), startWork(), and tick().

◆ m_running

volatile bool FileSinkThread::m_running
private

◆ m_sampleFifo

SampleSourceFifo* FileSinkThread::m_sampleFifo
private

Definition at line 64 of file filesinkthread.h.

Referenced by setSamplerate(), and tick().

◆ m_samplerate

int FileSinkThread::m_samplerate
private

Definition at line 67 of file filesinkthread.h.

Referenced by setLog2Interpolation(), setSamplerate(), and tick().

◆ m_samplesChunkSize

unsigned int FileSinkThread::m_samplesChunkSize
private

Definition at line 63 of file filesinkthread.h.

Referenced by setSamplerate(), and tick().

◆ m_samplesCount

std::size_t FileSinkThread::m_samplesCount
private

Definition at line 65 of file filesinkthread.h.

Referenced by getSamplesCount(), setSamplesCount(), and tick().

◆ m_startWaiter

QWaitCondition FileSinkThread::m_startWaiter
private

Definition at line 58 of file filesinkthread.h.

Referenced by run(), and startWork().

◆ m_startWaitMutex

QMutex FileSinkThread::m_startWaitMutex
private

Definition at line 57 of file filesinkthread.h.

Referenced by startWork().

◆ m_throttlems

int FileSinkThread::m_throttlems
private

Definition at line 69 of file filesinkthread.h.

Referenced by setSamplerate(), and tick().

◆ m_throttleToggle

bool FileSinkThread::m_throttleToggle
private

Definition at line 72 of file filesinkthread.h.

Referenced by tick().


The documentation for this class was generated from the following files: