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.
sdrplaythread.cpp
Go to the documentation of this file.
1 // Copyright (C) 2016 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 #include <stdio.h>
19 #include <errno.h>
20 #include "sdrplaythread.h"
21 #include "dsp/samplesinkfifo.h"
22 
23 SDRPlayThread::SDRPlayThread(mirisdr_dev_t* dev, SampleSinkFifo* sampleFifo, QObject* parent) :
24  QThread(parent),
25  m_running(false),
26  m_dev(dev),
27  m_convertBuffer(SDRPLAY_INIT_NBSAMPLES),
28  m_sampleFifo(sampleFifo),
29  m_samplerate(288000),
30  m_log2Decim(0),
31  m_fcPos(0)
32 {
33 }
34 
36 {
37  stopWork();
38 }
39 
41 {
42  m_startWaitMutex.lock();
43  start();
44  while(!m_running)
45  m_startWaiter.wait(&m_startWaitMutex, 100);
46  m_startWaitMutex.unlock();
47 }
48 
50 {
51  m_running = false;
52  wait();
53 }
54 
55 void SDRPlayThread::setSamplerate(int samplerate)
56 {
57  m_samplerate = samplerate;
58 }
59 
60 void SDRPlayThread::setLog2Decimation(unsigned int log2_decim)
61 {
62  m_log2Decim = log2_decim;
63 }
64 
65 void SDRPlayThread::setFcPos(int fcPos)
66 {
67  m_fcPos = fcPos;
68 }
69 
71 {
72  int res;
73 
74  m_running = true;
75  m_startWaiter.wakeAll();
76 
77  while (m_running)
78  {
79  if ((res = mirisdr_read_async(m_dev, &SDRPlayThread::callbackHelper, this, 32, SDRPLAY_INIT_NBSAMPLES)) < 0)
80  {
81  qCritical("SDRPlayThread::run: async read error: rc %d: %s", res, strerror(errno));
82  break;
83  }
84  }
85 
86  m_running = false;
87 }
88 
89 void SDRPlayThread::callbackHelper(unsigned char* buf, uint32_t len, void* ctx)
90 {
91  SDRPlayThread* thread = (SDRPlayThread*) ctx;
92  thread->callback((const qint16*) buf, len/2);
93 }
94 
95 void SDRPlayThread::callback(const qint16* buf, qint32 len)
96 {
97  SampleVector::iterator it = m_convertBuffer.begin();
98 
99  if (m_log2Decim == 0)
100  {
101  m_decimators.decimate1(&it, buf, len);
102  }
103  else
104  {
105  if (m_fcPos == 0) // Infradyne
106  {
107  switch (m_log2Decim)
108  {
109  case 1:
110  m_decimators.decimate2_inf(&it, buf, len);
111  break;
112  case 2:
113  m_decimators.decimate4_inf(&it, buf, len);
114  break;
115  case 3:
116  m_decimators.decimate8_inf(&it, buf, len);
117  break;
118  case 4:
119  m_decimators.decimate16_inf(&it, buf, len);
120  break;
121  case 5:
122  m_decimators.decimate32_inf(&it, buf, len);
123  break;
124  case 6:
125  m_decimators.decimate64_inf(&it, buf, len);
126  break;
127  default:
128  break;
129  }
130  }
131  else if (m_fcPos == 1) // Supradyne
132  {
133  switch (m_log2Decim)
134  {
135  case 1:
136  m_decimators.decimate2_sup(&it, buf, len);
137  break;
138  case 2:
139  m_decimators.decimate4_sup(&it, buf, len);
140  break;
141  case 3:
142  m_decimators.decimate8_sup(&it, buf, len);
143  break;
144  case 4:
145  m_decimators.decimate16_sup(&it, buf, len);
146  break;
147  case 5:
148  m_decimators.decimate32_sup(&it, buf, len);
149  break;
150  case 6:
151  m_decimators.decimate64_sup(&it, buf, len);
152  break;
153  default:
154  break;
155  }
156  }
157  else // Centered
158  {
159  switch (m_log2Decim)
160  {
161  case 1:
162  m_decimators.decimate2_cen(&it, buf, len);
163  break;
164  case 2:
165  m_decimators.decimate4_cen(&it, buf, len);
166  break;
167  case 3:
168  m_decimators.decimate8_cen(&it, buf, len);
169  break;
170  case 4:
171  m_decimators.decimate16_cen(&it, buf, len);
172  break;
173  case 5:
174  m_decimators.decimate32_cen(&it, buf, len);
175  break;
176  case 6:
177  m_decimators.decimate64_cen(&it, buf, len);
178  break;
179  default:
180  break;
181  }
182  }
183  }
184 
185  m_sampleFifo->write(m_convertBuffer.begin(), it);
186 
187  if(!m_running)
188  {
189  mirisdr_cancel_async(m_dev);
190  }
191 }
192 
void setSamplerate(int samplerate)
void decimate2_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:498
void decimate64_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:2975
mirisdr_dev_t * m_dev
Definition: sdrplaythread.h:48
void decimate64_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:3040
void decimate2_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:526
unsigned int m_log2Decim
Definition: sdrplaythread.h:53
void decimate2_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:554
uint write(const quint8 *data, uint count)
QWaitCondition m_startWaiter
Definition: sdrplaythread.h:45
void decimate8_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:941
void decimate64_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:2418
void decimate32_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:1592
void decimate32_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:1902
unsigned int uint32_t
Definition: rtptypes_win.h:46
void decimate4_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:682
void decimate8_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:1057
void decimate8_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:825
#define SDRPLAY_INIT_NBSAMPLES
Definition: sdrplaythread.h:28
void decimate16_sup(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:1300
void setLog2Decimation(unsigned int log2_decim)
SDRPlayThread(mirisdr_dev_t *dev, SampleSinkFifo *sampleFifo, QObject *parent=NULL)
void decimate4_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:782
QMutex m_startWaitMutex
Definition: sdrplaythread.h:44
SampleVector m_convertBuffer
Definition: sdrplaythread.h:49
void decimate4_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:582
SampleSinkFifo * m_sampleFifo
Definition: sdrplaythread.h:50
void callback(const qint16 *buf, qint32 len)
void decimate16_inf(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:1117
static void callbackHelper(unsigned char *buf, uint32_t len, void *ctx)
void setFcPos(int fcPos)
void decimate16_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:1483
void decimate32_cen(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:2212
void decimate1(SampleVector::iterator *it, const T *buf, qint32 len)
Definition: decimators.h:462
Decimators< qint32, qint16, SDR_RX_SAMP_SZ, 12 > m_decimators
Definition: sdrplaythread.h:56