Merge pull request #19 from cjcliffe/thread-data-optimization

Thread data optimization
This commit is contained in:
Charles J. Cliffe 2014-12-26 17:13:27 -05:00
commit 5f04baf873
25 changed files with 562 additions and 281 deletions

View File

@ -96,12 +96,14 @@ void AppFrame::OnIdle(wxIdleEvent& event) {
// std::this_thread::yield(); // std::this_thread::yield();
//#endif //#endif
if (!wxGetApp().getIQVisualQueue()->empty()) { if (!wxGetApp().getIQVisualQueue()->empty()) {
SDRThreadIQData iqData; DemodulatorThreadIQData *iqData;
wxGetApp().getIQVisualQueue()->pop(iqData); wxGetApp().getIQVisualQueue()->pop(iqData);
if (iqData.data.size()) { if (iqData && iqData->data.size()) {
spectrumCanvas->setData(&iqData.data); spectrumCanvas->setData(&iqData->data);
waterfallCanvas->setData(&iqData.data); waterfallCanvas->setData(&iqData->data);
delete iqData;
} else { } else {
std::cout << "Incoming IQ data empty?" << std::endl; std::cout << "Incoming IQ data empty?" << std::endl;
} }
@ -109,19 +111,19 @@ void AppFrame::OnIdle(wxIdleEvent& event) {
} }
if (!wxGetApp().getAudioVisualQueue()->empty()) { if (!wxGetApp().getAudioVisualQueue()->empty()) {
AudioThreadInput demodAudioData; AudioThreadInput *demodAudioData;
wxGetApp().getAudioVisualQueue()->pop(demodAudioData); wxGetApp().getAudioVisualQueue()->pop(demodAudioData);
if (demodAudioData.data.size()) { if (demodAudioData && demodAudioData->data.size()) {
if (scopeCanvas->waveform_points.size() != demodAudioData->data.size()*2) {
if (scopeCanvas->waveform_points.size() != demodAudioData.data.size()*2) { scopeCanvas->waveform_points.resize(demodAudioData->data.size()*2);
scopeCanvas->waveform_points.resize(demodAudioData.data.size()*2);
} }
for (int i = 0, iMax = demodAudioData.data.size(); i < iMax; i++) { for (int i = 0, iMax = demodAudioData->data.size(); i < iMax; i++) {
scopeCanvas->waveform_points[i * 2 + 1] = demodAudioData.data[i] * 0.5f; scopeCanvas->waveform_points[i * 2 + 1] = demodAudioData->data[i] * 0.5f;
scopeCanvas->waveform_points[i * 2] = ((double) i / (double) iMax); scopeCanvas->waveform_points[i * 2] = ((double) i / (double) iMax);
} }
delete demodAudioData;
} else { } else {
std::cout << "Incoming Demodulator data empty?" << std::endl; std::cout << "Incoming Demodulator data empty?" << std::endl;
} }

View File

@ -30,7 +30,7 @@ bool CubicSDR::OnInit() {
sdrPostThread = new SDRPostThread(); sdrPostThread = new SDRPostThread();
iqPostDataQueue = new SDRThreadIQDataQueue; iqPostDataQueue = new SDRThreadIQDataQueue;
iqVisualQueue = new SDRThreadIQDataQueue; iqVisualQueue = new DemodulatorThreadInputQueue;
iqVisualQueue->set_max_num_items(1); iqVisualQueue->set_max_num_items(1);
sdrThread->setIQDataOutQueue(iqPostDataQueue); sdrThread->setIQDataOutQueue(iqPostDataQueue);
@ -115,7 +115,7 @@ DemodulatorThreadOutputQueue* CubicSDR::getAudioVisualQueue() {
return audioVisualQueue; return audioVisualQueue;
} }
SDRThreadIQDataQueue* CubicSDR::getIQVisualQueue() { DemodulatorThreadInputQueue* CubicSDR::getIQVisualQueue() {
return iqVisualQueue; return iqVisualQueue;
} }

View File

@ -20,8 +20,7 @@
class CubicSDR: public wxApp { class CubicSDR: public wxApp {
public: public:
CubicSDR() : CubicSDR() :
m_glContext(NULL), t_PostSDR(NULL), t_SDR(NULL), audioVisualQueue(NULL), threadCmdQueueSDR(NULL), iqVisualQueue(NULL), frequency( m_glContext(NULL), frequency(DEFAULT_FREQ), sdrThread(NULL), sdrPostThread(NULL), threadCmdQueueSDR(NULL), iqVisualQueue(NULL), iqPostDataQueue(NULL), audioVisualQueue(NULL), t_SDR(NULL), t_PostSDR(NULL) {
DEFAULT_FREQ), sdrPostThread(NULL), iqPostDataQueue(NULL), sdrThread(NULL) {
} }
@ -34,7 +33,7 @@ public:
int getFrequency(); int getFrequency();
DemodulatorThreadOutputQueue* getAudioVisualQueue(); DemodulatorThreadOutputQueue* getAudioVisualQueue();
SDRThreadIQDataQueue* getIQVisualQueue(); DemodulatorThreadInputQueue* getIQVisualQueue();
DemodulatorMgr &getDemodMgr(); DemodulatorMgr &getDemodMgr();
void bindDemodulator(DemodulatorInstance *demod); void bindDemodulator(DemodulatorInstance *demod);
@ -51,8 +50,8 @@ private:
SDRPostThread *sdrPostThread; SDRPostThread *sdrPostThread;
SDRThreadCommandQueue* threadCmdQueueSDR; SDRThreadCommandQueue* threadCmdQueueSDR;
SDRThreadIQDataQueue* iqVisualQueue;
SDRThreadIQDataQueue* iqPostDataQueue; SDRThreadIQDataQueue* iqPostDataQueue;
DemodulatorThreadInputQueue* iqVisualQueue;
DemodulatorThreadOutputQueue* audioVisualQueue; DemodulatorThreadOutputQueue* audioVisualQueue;
std::thread *t_SDR; std::thread *t_SDR;

View File

@ -11,3 +11,25 @@
#define DEFAULT_FREQ 98900000 #define DEFAULT_FREQ 98900000
#define AUDIO_FREQUENCY 44100 #define AUDIO_FREQUENCY 44100
#include <mutex>
#include <atomic>
class ReferenceCounter {
public:
mutable std::mutex m_mutex;
void setRefCount(int rc) {
refCount.store(rc);
}
void decRefCount() {
refCount.store(refCount.load()-1);
}
int getRefCount() {
return refCount.load();
}
protected:
std::atomic<int> refCount;
};

View File

@ -10,8 +10,8 @@ std::map<int, std::thread *> AudioThread::deviceThread;
#endif #endif
AudioThread::AudioThread(AudioThreadInputQueue *inputQueue, DemodulatorThreadCommandQueue* threadQueueNotify) : AudioThread::AudioThread(AudioThreadInputQueue *inputQueue, DemodulatorThreadCommandQueue* threadQueueNotify) :
inputQueue(inputQueue), terminated(false), audio_queue_ptr(0), underflow_count(0), threadQueueNotify(threadQueueNotify), gain(1.0), active( currentInput(NULL), inputQueue(inputQueue), audio_queue_ptr(0), underflow_count(0), terminated(false), active(false), gain(1.0), threadQueueNotify(
false) { threadQueueNotify) {
#ifdef __APPLE__ #ifdef __APPLE__
boundThreads = new std::vector<AudioThread *>; boundThreads = new std::vector<AudioThread *>;
#endif #endif
@ -65,31 +65,68 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu
continue; continue;
} }
if (srcmix->currentInput.channels == 0) { if (!srcmix->currentInput) {
if (!srcmix->inputQueue->empty()) { if (srcmix->terminated) {
continue;
}
srcmix->inputQueue->pop(srcmix->currentInput); srcmix->inputQueue->pop(srcmix->currentInput);
srcmix->audio_queue_ptr = 0;
continue;
}
std::lock_guard < std::mutex > lock(srcmix->currentInput->m_mutex);
if (srcmix->currentInput->channels == 0 || !srcmix->currentInput->data.size()) {
if (!srcmix->inputQueue->empty()) {
if (srcmix->currentInput) {
srcmix->currentInput->decRefCount();
srcmix->currentInput = NULL;
}
if (srcmix->terminated) {
continue;
}
srcmix->inputQueue->pop(srcmix->currentInput);
srcmix->audio_queue_ptr = 0;
} }
continue; continue;
} }
if (srcmix->currentInput.channels == 1) { if (srcmix->currentInput->channels == 1) {
for (int i = 0; i < nBufferFrames; i++) { for (int i = 0; i < nBufferFrames; i++) {
if (srcmix->audio_queue_ptr >= srcmix->currentInput.data.size()) { if (srcmix->audio_queue_ptr >= srcmix->currentInput->data.size()) {
if (srcmix->currentInput) {
srcmix->currentInput->decRefCount();
srcmix->currentInput = NULL;
}
if (srcmix->terminated) {
continue;
}
srcmix->inputQueue->pop(srcmix->currentInput); srcmix->inputQueue->pop(srcmix->currentInput);
srcmix->audio_queue_ptr = 0; srcmix->audio_queue_ptr = 0;
} }
float v = srcmix->currentInput.data[srcmix->audio_queue_ptr] * src->gain; if (srcmix->currentInput && srcmix->currentInput->data.size()) {
float v = srcmix->currentInput->data[srcmix->audio_queue_ptr] * src->gain;
out[i * 2] += v; out[i * 2] += v;
out[i * 2 + 1] += v; out[i * 2 + 1] += v;
}
srcmix->audio_queue_ptr++; srcmix->audio_queue_ptr++;
} }
} else { } else {
for (int i = 0, iMax = src->currentInput.channels * nBufferFrames; i < iMax; i++) { for (int i = 0, iMax = src->currentInput->channels * nBufferFrames; i < iMax; i++) {
if (srcmix->audio_queue_ptr >= srcmix->currentInput.data.size()) { if (srcmix->audio_queue_ptr >= srcmix->currentInput->data.size()) {
if (srcmix->currentInput) {
srcmix->currentInput->decRefCount();
srcmix->currentInput = NULL;
}
if (srcmix->terminated) {
continue;
}
srcmix->inputQueue->pop(srcmix->currentInput); srcmix->inputQueue->pop(srcmix->currentInput);
srcmix->audio_queue_ptr = 0; srcmix->audio_queue_ptr = 0;
} }
out[i] = out[i] + srcmix->currentInput.data[srcmix->audio_queue_ptr] * src->gain; if (srcmix->currentInput && srcmix->currentInput->data.size()) {
out[i] = out[i] + srcmix->currentInput->data[srcmix->audio_queue_ptr] * src->gain;
}
srcmix->audio_queue_ptr++; srcmix->audio_queue_ptr++;
} }
} }
@ -110,35 +147,59 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu
std::cout << "Audio buffer underflow.." << (src->underflow_count++) << std::endl; std::cout << "Audio buffer underflow.." << (src->underflow_count++) << std::endl;
} }
if (src->currentInput.channels == 0) { if (!src->currentInput) {
if (!src->inputQueue->empty()) {
src->inputQueue->pop(src->currentInput); src->inputQueue->pop(src->currentInput);
src->audio_queue_ptr = 0;
return 0;
}
std::lock_guard < std::mutex > lock(src->currentInput->m_mutex);
if (src->currentInput->channels == 0 || !src->currentInput->data.size()) {
if (!src->inputQueue->empty()) {
if (src->currentInput) {
src->currentInput->decRefCount();
src->currentInput = NULL;
}
if (src->terminated) {
return 1;
}
src->inputQueue->pop(src->currentInput);
src->audio_queue_ptr = 0;
} }
return 0; return 0;
} }
if (src->currentInput.channels == 1) { if (src->currentInput->channels == 1) {
for (int i = 0; i < nBufferFrames; i++) { for (int i = 0; i < nBufferFrames; i++) {
if (src->audio_queue_ptr >= src->currentInput.data.size()) { if (src->audio_queue_ptr >= src->currentInput->data.size()) {
if (src->currentInput) {
src->currentInput->decRefCount();
src->currentInput = NULL;
}
if (src->terminated) { if (src->terminated) {
break; return 1;
} }
src->inputQueue->pop(src->currentInput); src->inputQueue->pop(src->currentInput);
src->audio_queue_ptr = 0; src->audio_queue_ptr = 0;
} }
out[i * 2] = out[i * 2 + 1] = src->currentInput.data[src->audio_queue_ptr] * src->gain; if (src->currentInput && src->currentInput->data.size()) {
out[i * 2] = out[i * 2 + 1] = src->currentInput->data[src->audio_queue_ptr] * src->gain;
}
src->audio_queue_ptr++; src->audio_queue_ptr++;
} }
} else { } else {
for (int i = 0, iMax = src->currentInput.channels * nBufferFrames; i < iMax; i++) { for (int i = 0, iMax = src->currentInput->channels * nBufferFrames; i < iMax; i++) {
if (src->audio_queue_ptr >= src->currentInput.data.size()) { if (src->audio_queue_ptr >= src->currentInput->data.size()) {
if (src->terminated) { if (src->terminated) {
break; return 1;
} }
src->inputQueue->pop(src->currentInput); src->inputQueue->pop(src->currentInput);
src->audio_queue_ptr = 0; src->audio_queue_ptr = 0;
} }
out[i] = src->currentInput.data[src->audio_queue_ptr] * src->gain; if (src->currentInput && src->currentInput->data.size()) {
out[i] = src->currentInput->data[src->audio_queue_ptr] * src->gain;
}
src->audio_queue_ptr++; src->audio_queue_ptr++;
} }
} }
@ -297,16 +358,22 @@ bool AudioThread::isActive() {
void AudioThread::setActive(bool state) { void AudioThread::setActive(bool state) {
#ifdef __APPLE__ #ifdef __APPLE__
AudioThreadInput dummy; AudioThreadInput *dummy;
if (state && !active) { if (state && !active) {
deviceController[parameters.deviceId]->bindThread(this);
while (!inputQueue->empty()) { // flush queue while (!inputQueue->empty()) { // flush queue
inputQueue->pop(dummy); inputQueue->pop(dummy);
if (dummy) {
delete dummy;
} }
}
deviceController[parameters.deviceId]->bindThread(this);
} else if (!state && active) { } else if (!state && active) {
deviceController[parameters.deviceId]->removeThread(this); deviceController[parameters.deviceId]->removeThread(this);
while (!inputQueue->empty()) { // flush queue while (!inputQueue->empty()) { // flush queue
inputQueue->pop(dummy); inputQueue->pop(dummy);
if (dummy) {
delete dummy;
}
} }
} }
#endif #endif

View File

@ -18,21 +18,21 @@
#include "RtAudio.h" #include "RtAudio.h"
#include "DemodDefs.h" #include "DemodDefs.h"
class AudioThreadInput { class AudioThreadInput: public ReferenceCounter {
public: public:
int frequency; int frequency;
int sampleRate; int sampleRate;
int channels; int channels;
std::vector<float> data;
AudioThreadInput(): frequency(0), sampleRate(0), channels(0) { AudioThreadInput() :
frequency(0), sampleRate(0), channels(0) {
} }
~AudioThreadInput() { ~AudioThreadInput() {
} }
std::vector<float> data;
}; };
class AudioThreadCommand { class AudioThreadCommand {
@ -49,13 +49,13 @@ public:
int int_value; int int_value;
}; };
typedef ThreadQueue<AudioThreadInput> AudioThreadInputQueue; typedef ThreadQueue<AudioThreadInput *> AudioThreadInputQueue;
typedef ThreadQueue<AudioThreadCommand> AudioThreadCommandQueue; typedef ThreadQueue<AudioThreadCommand> AudioThreadCommandQueue;
class AudioThread { class AudioThread {
public: public:
AudioThreadInput currentInput; AudioThreadInput *currentInput;
AudioThreadInputQueue *inputQueue; AudioThreadInputQueue *inputQueue;
std::atomic<unsigned int> audio_queue_ptr; std::atomic<unsigned int> audio_queue_ptr;
std::atomic<unsigned int> underflow_count; std::atomic<unsigned int> underflow_count;

View File

@ -4,11 +4,11 @@
#include "CubicSDRDefs.h" #include "CubicSDRDefs.h"
#include "liquid/liquid.h" #include "liquid/liquid.h"
#include <atomic>
#include <mutex>
enum DemodulatorType { enum DemodulatorType {
DEMOD_TYPE_NULL, DEMOD_TYPE_NULL, DEMOD_TYPE_AM, DEMOD_TYPE_FM, DEMOD_TYPE_LSB, DEMOD_TYPE_USB
DEMOD_TYPE_AM,
DEMOD_TYPE_FM,
DEMOD_TYPE_LSB, DEMOD_TYPE_USB
}; };
class DemodulatorThread; class DemodulatorThread;
@ -24,12 +24,12 @@ public:
}; };
DemodulatorThreadCommand() : DemodulatorThreadCommand() :
cmd(DEMOD_THREAD_CMD_NULL), int_value(0), context(NULL) { cmd(DEMOD_THREAD_CMD_NULL), context(NULL), int_value(0) {
} }
DemodulatorThreadCommand(DemodulatorThreadCommandEnum cmd) : DemodulatorThreadCommand(DemodulatorThreadCommandEnum cmd) :
cmd(cmd), int_value(0), context(NULL) { cmd(cmd), context(NULL), int_value(0) {
} }
@ -41,9 +41,7 @@ public:
class DemodulatorThreadControlCommand { class DemodulatorThreadControlCommand {
public: public:
enum DemodulatorThreadControlCommandEnum { enum DemodulatorThreadControlCommandEnum {
DEMOD_THREAD_CMD_CTL_NULL, DEMOD_THREAD_CMD_CTL_NULL, DEMOD_THREAD_CMD_CTL_SQUELCH_AUTO, DEMOD_THREAD_CMD_CTL_SQUELCH_OFF
DEMOD_THREAD_CMD_CTL_SQUELCH_AUTO,
DEMOD_THREAD_CMD_CTL_SQUELCH_OFF
}; };
DemodulatorThreadControlCommand() : DemodulatorThreadControlCommand() :
@ -53,29 +51,23 @@ public:
DemodulatorThreadControlCommandEnum cmd; DemodulatorThreadControlCommandEnum cmd;
}; };
class DemodulatorThreadIQData { class DemodulatorThreadIQData: public ReferenceCounter {
public: public:
unsigned int frequency; unsigned int frequency;
unsigned int bandwidth; unsigned int bandwidth;
std::vector<signed char> data; std::vector<liquid_float_complex> data;
DemodulatorThreadIQData() : DemodulatorThreadIQData() :
frequency(0), bandwidth(0) { frequency(0), bandwidth(0) {
} }
DemodulatorThreadIQData(unsigned int bandwidth, unsigned int frequency,
std::vector<signed char> data) :
data(data), frequency(frequency), bandwidth(bandwidth) {
}
~DemodulatorThreadIQData() { ~DemodulatorThreadIQData() {
} }
}; };
class DemodulatorThreadPostIQData { class DemodulatorThreadPostIQData: public ReferenceCounter {
public: public:
std::vector<liquid_float_complex> data; std::vector<liquid_float_complex> data;
float audio_resample_ratio; float audio_resample_ratio;
@ -83,7 +75,8 @@ public:
float resample_ratio; float resample_ratio;
msresamp_crcf resampler; msresamp_crcf resampler;
DemodulatorThreadPostIQData(): audio_resample_ratio(0), audio_resampler(NULL), resample_ratio(0), resampler(NULL) { DemodulatorThreadPostIQData() :
audio_resample_ratio(0), audio_resampler(NULL), resample_ratio(0), resampler(NULL) {
} }
@ -92,24 +85,21 @@ public:
} }
}; };
class DemodulatorThreadAudioData: public ReferenceCounter {
class DemodulatorThreadAudioData {
public: public:
unsigned int frequency; unsigned int frequency;
unsigned int sampleRate; unsigned int sampleRate;
unsigned char channels; unsigned char channels;
std::vector<float> data; std::vector<float> *data;
DemodulatorThreadAudioData() : DemodulatorThreadAudioData() :
sampleRate(0), frequency(0), channels(0) { frequency(0), sampleRate(0), channels(0), data(NULL) {
} }
DemodulatorThreadAudioData(unsigned int frequency, unsigned int sampleRate, DemodulatorThreadAudioData(unsigned int frequency, unsigned int sampleRate, std::vector<float> *data) :
std::vector<float> data) : frequency(frequency), sampleRate(sampleRate), channels(1), data(data) {
data(data), sampleRate(sampleRate), frequency(frequency), channels(
1) {
} }
@ -118,12 +108,11 @@ public:
} }
}; };
typedef ThreadQueue<DemodulatorThreadIQData> DemodulatorThreadInputQueue; typedef ThreadQueue<DemodulatorThreadIQData *> DemodulatorThreadInputQueue;
typedef ThreadQueue<DemodulatorThreadPostIQData> DemodulatorThreadPostInputQueue; typedef ThreadQueue<DemodulatorThreadPostIQData *> DemodulatorThreadPostInputQueue;
typedef ThreadQueue<DemodulatorThreadCommand> DemodulatorThreadCommandQueue; typedef ThreadQueue<DemodulatorThreadCommand> DemodulatorThreadCommandQueue;
typedef ThreadQueue<DemodulatorThreadControlCommand> DemodulatorThreadControlCommandQueue; typedef ThreadQueue<DemodulatorThreadControlCommand> DemodulatorThreadControlCommandQueue;
class DemodulatorThreadParameters { class DemodulatorThreadParameters {
public: public:
unsigned int frequency; unsigned int frequency;

View File

@ -77,12 +77,12 @@ DemodulatorThreadParameters &DemodulatorInstance::getParams() {
} }
void DemodulatorInstance::terminate() { void DemodulatorInstance::terminate() {
std::cout << "Terminating demodulator preprocessor thread.." << std::endl;
demodulatorPreThread->terminate();
std::cout << "Terminating demodulator thread.." << std::endl;
demodulatorThread->terminate();
std::cout << "Terminating demodulator audio thread.." << std::endl; std::cout << "Terminating demodulator audio thread.." << std::endl;
audioThread->terminate(); audioThread->terminate();
std::cout << "Terminating demodulator thread.." << std::endl;
demodulatorThread->terminate();
std::cout << "Terminating demodulator preprocessor thread.." << std::endl;
demodulatorPreThread->terminate();
} }
std::string DemodulatorInstance::getLabel() { std::string DemodulatorInstance::getLabel() {
@ -146,7 +146,6 @@ void DemodulatorInstance::setActive(bool state) {
audioThread->setActive(state); audioThread->setActive(state);
} }
void DemodulatorInstance::squelchAuto() { void DemodulatorInstance::squelchAuto() {
DemodulatorThreadControlCommand command; DemodulatorThreadControlCommand command;
command.cmd = DemodulatorThreadControlCommand::DEMOD_THREAD_CMD_CTL_SQUELCH_AUTO; command.cmd = DemodulatorThreadControlCommand::DEMOD_THREAD_CMD_CTL_SQUELCH_AUTO;

View File

@ -49,16 +49,11 @@ public:
bool isActive(); bool isActive();
void setActive(bool state); void setActive(bool state);
void squelchAuto(); void squelchAuto();bool isSquelchEnabled();
bool isSquelchEnabled();
void setSquelchEnabled(bool state); void setSquelchEnabled(bool state);
private: private:
std::atomic<std::string *> label; std::atomic<std::string *> label;bool terminated;bool demodTerminated;bool audioTerminated;bool preDemodTerminated;
bool terminated;
bool demodTerminated;
bool audioTerminated;
bool preDemodTerminated;
std::atomic<bool> active; std::atomic<bool> active;
std::atomic<bool> squelch; std::atomic<bool> squelch;
}; };

View File

@ -10,7 +10,8 @@
DemodulatorPreThread::DemodulatorPreThread(DemodulatorThreadInputQueue* pQueueIn, DemodulatorThreadPostInputQueue* pQueueOut, DemodulatorPreThread::DemodulatorPreThread(DemodulatorThreadInputQueue* pQueueIn, DemodulatorThreadPostInputQueue* pQueueOut,
DemodulatorThreadControlCommandQueue *threadQueueControl, DemodulatorThreadCommandQueue* threadQueueNotify) : DemodulatorThreadControlCommandQueue *threadQueueControl, DemodulatorThreadCommandQueue* threadQueueNotify) :
inputQueue(pQueueIn), postInputQueue(pQueueOut), terminated(false), initialized(false), audio_resampler(NULL), resample_ratio(1), audio_resample_ratio( inputQueue(pQueueIn), postInputQueue(pQueueOut), terminated(false), initialized(false), audio_resampler(NULL), resample_ratio(1), audio_resample_ratio(
1), resampler(NULL), commandQueue(NULL), fir_filter(NULL), audioInputQueue(NULL), threadQueueNotify(threadQueueNotify), threadQueueControl(threadQueueControl) { 1), resampler(NULL), commandQueue(NULL), fir_filter(NULL), audioInputQueue(NULL), threadQueueNotify(threadQueueNotify), threadQueueControl(
threadQueueControl) {
float kf = 0.5; // modulation factor float kf = 0.5; // modulation factor
fdem = freqdem_create(kf); fdem = freqdem_create(kf);
@ -99,8 +100,15 @@ void DemodulatorPreThread::threadMain() {
} }
std::cout << "Demodulator preprocessor thread started.." << std::endl; std::cout << "Demodulator preprocessor thread started.." << std::endl;
std::deque<DemodulatorThreadPostIQData *> buffers;
std::deque<DemodulatorThreadPostIQData *>::iterator buffers_i;
std::vector<liquid_float_complex> in_buf_data;
std::vector<liquid_float_complex> out_buf_data;
while (!terminated) { while (!terminated) {
DemodulatorThreadIQData inp; DemodulatorThreadIQData *inp;
inputQueue->pop(inp); inputQueue->pop(inp);
bool bandwidthChanged = false; bool bandwidthChanged = false;
@ -144,9 +152,9 @@ void DemodulatorPreThread::threadMain() {
} }
// Requested frequency is not center, shift it into the center! // Requested frequency is not center, shift it into the center!
if (inp.frequency != params.frequency) { if (inp->frequency != params.frequency) {
if ((params.frequency - inp.frequency) != shift_freq) { if ((params.frequency - inp->frequency) != shift_freq) {
shift_freq = params.frequency - inp.frequency; shift_freq = params.frequency - inp->frequency;
if (abs(shift_freq) <= (int) ((float) (SRATE / 2) * 1.5)) { if (abs(shift_freq) <= (int) ((float) (SRATE / 2) * 1.5)) {
nco_crcf_set_frequency(nco_shift, (2.0 * M_PI) * (((float) abs(shift_freq)) / ((float) SRATE))); nco_crcf_set_frequency(nco_shift, (2.0 * M_PI) * (((float) abs(shift_freq)) / ((float) SRATE)));
} }
@ -157,21 +165,25 @@ void DemodulatorPreThread::threadMain() {
continue; continue;
} }
std::vector<signed char> *data = &inp.data; // std::lock_guard < std::mutex > lock(inp->m_mutex);
std::vector<liquid_float_complex> *data = &inp->data;
if (data->size()) { if (data->size()) {
int bufSize = data->size() / 2; int bufSize = data->size();
liquid_float_complex in_buf_data[bufSize]; if (in_buf_data.size() != bufSize) {
liquid_float_complex out_buf_data[bufSize]; if (in_buf_data.capacity() < bufSize) {
in_buf_data.reserve(bufSize);
liquid_float_complex *in_buf = in_buf_data; out_buf_data.reserve(bufSize);
liquid_float_complex *out_buf = out_buf_data;
liquid_float_complex *temp_buf = NULL;
for (int i = 0; i < bufSize; i++) {
in_buf[i].real = (float) (*data)[i * 2] / 127.0f;
in_buf[i].imag = (float) (*data)[i * 2 + 1] / 127.0f;
} }
in_buf_data.resize(bufSize);
out_buf_data.resize(bufSize);
}
in_buf_data.assign(inp->data.begin(),inp->data.end());
liquid_float_complex *in_buf = &in_buf_data[0];
liquid_float_complex *out_buf = &out_buf_data[0];
liquid_float_complex *temp_buf = NULL;
if (shift_freq != 0) { if (shift_freq != 0) {
if (shift_freq < 0) { if (shift_freq < 0) {
@ -184,19 +196,35 @@ void DemodulatorPreThread::threadMain() {
out_buf = temp_buf; out_buf = temp_buf;
} }
DemodulatorThreadPostIQData resamp; DemodulatorThreadPostIQData *resamp = NULL;
resamp.data.resize(bufSize);
firfilt_crcf_execute_block(fir_filter, in_buf, bufSize, &resamp.data[0]); for (buffers_i = buffers.begin(); buffers_i != buffers.end(); buffers_i++) {
if ((*buffers_i)->getRefCount() <= 0) {
resamp = (*buffers_i);
break;
}
}
resamp.audio_resample_ratio = audio_resample_ratio; if (resamp == NULL) {
resamp.audio_resampler = audio_resampler; resamp = new DemodulatorThreadPostIQData;
resamp.resample_ratio = resample_ratio; buffers.push_back(resamp);
resamp.resampler = resampler; }
resamp->setRefCount(1);
resamp->data.assign(in_buf, in_buf + bufSize);
// firfilt_crcf_execute_block(fir_filter, in_buf, bufSize, &((*resamp.data)[0]));
resamp->audio_resample_ratio = audio_resample_ratio;
resamp->audio_resampler = audio_resampler;
resamp->resample_ratio = resample_ratio;
resamp->resampler = resampler;
postInputQueue->push(resamp); postInputQueue->push(resamp);
} }
inp->decRefCount();
if (!workerResults->empty()) { if (!workerResults->empty()) {
while (!workerResults->empty()) { while (!workerResults->empty()) {
DemodulatorWorkerThreadResult result; DemodulatorWorkerThreadResult result;
@ -225,6 +253,13 @@ void DemodulatorPreThread::threadMain() {
} }
} }
while (!buffers.empty()) {
DemodulatorThreadPostIQData *iqDataDel = buffers.front();
buffers.pop_front();
std::lock_guard < std::mutex > lock(iqDataDel->m_mutex);
delete iqDataDel;
}
std::cout << "Demodulator preprocessor thread done." << std::endl; std::cout << "Demodulator preprocessor thread done." << std::endl;
DemodulatorThreadCommand tCmd(DemodulatorThreadCommand::DEMOD_THREAD_CMD_DEMOD_PREPROCESS_TERMINATED); DemodulatorThreadCommand tCmd(DemodulatorThreadCommand::DEMOD_THREAD_CMD_DEMOD_PREPROCESS_TERMINATED);
tCmd.context = this; tCmd.context = this;
@ -233,7 +268,7 @@ void DemodulatorPreThread::threadMain() {
void DemodulatorPreThread::terminate() { void DemodulatorPreThread::terminate() {
terminated = true; terminated = true;
DemodulatorThreadIQData inp; // push dummy to nudge queue DemodulatorThreadIQData *inp = new DemodulatorThreadIQData; // push dummy to nudge queue
inputQueue->push(inp); inputQueue->push(inp);
workerThread->terminate(); workerThread->terminate();
} }

View File

@ -32,7 +32,6 @@ public:
threadQueueControl = tQueue; threadQueueControl = tQueue;
} }
DemodulatorThreadParameters &getParams() { DemodulatorThreadParameters &getParams() {
return params; return params;
} }

View File

@ -6,8 +6,10 @@
#include <pthread.h> #include <pthread.h>
#endif #endif
DemodulatorThread::DemodulatorThread(DemodulatorThreadPostInputQueue* pQueue, DemodulatorThreadControlCommandQueue *threadQueueControl, DemodulatorThreadCommandQueue* threadQueueNotify) : DemodulatorThread::DemodulatorThread(DemodulatorThreadPostInputQueue* pQueue, DemodulatorThreadControlCommandQueue *threadQueueControl,
postInputQueue(pQueue), visOutQueue(NULL), terminated(false), audioInputQueue(NULL), threadQueueNotify(threadQueueNotify), threadQueueControl(threadQueueControl), agc(NULL), squelch_enabled(false), squelch_level(0), squelch_tolerance(0) { DemodulatorThreadCommandQueue* threadQueueNotify) :
postInputQueue(pQueue), visOutQueue(NULL), audioInputQueue(NULL), agc(NULL), terminated(false), threadQueueNotify(threadQueueNotify), threadQueueControl(
threadQueueControl), squelch_level(0), squelch_tolerance(0), squelch_enabled(false) {
float kf = 0.5; // modulation factor float kf = 0.5; // modulation factor
fdem = freqdem_create(kf); fdem = freqdem_create(kf);
@ -35,71 +37,115 @@ void DemodulatorThread::threadMain() {
agc_crcf_set_bandwidth(agc, 1e-3f); agc_crcf_set_bandwidth(agc, 1e-3f);
std::cout << "Demodulator thread started.." << std::endl; std::cout << "Demodulator thread started.." << std::endl;
while (!terminated) {
DemodulatorThreadPostIQData inp;
postInputQueue->pop(inp);
int bufSize = inp.data.size(); std::deque<AudioThreadInput *> buffers;
std::deque<AudioThreadInput *>::iterator buffers_i;
std::vector<liquid_float_complex> resampled_data;
std::vector<liquid_float_complex> agc_data;
std::vector<float> demod_output;
std::vector<float> resampled_audio_output;
while (!terminated) {
DemodulatorThreadPostIQData *inp;
postInputQueue->pop(inp);
std::lock_guard < std::mutex > lock(inp->m_mutex);
int bufSize = inp->data.size();
if (!bufSize) { if (!bufSize) {
inp->decRefCount();
continue; continue;
} }
if (resampler == NULL) { if (resampler == NULL) {
resampler = inp.resampler; resampler = inp->resampler;
audio_resampler = inp.audio_resampler; audio_resampler = inp->audio_resampler;
} else if (resampler != inp.resampler) { } else if (resampler != inp->resampler) {
msresamp_crcf_destroy(resampler); msresamp_crcf_destroy(resampler);
msresamp_rrrf_destroy(audio_resampler); msresamp_rrrf_destroy(audio_resampler);
resampler = inp.resampler; resampler = inp->resampler;
audio_resampler = inp.audio_resampler; audio_resampler = inp->audio_resampler;
} }
int out_size = ceil((float) (bufSize) * inp.resample_ratio); int out_size = ceil((float) (bufSize) * inp->resample_ratio);
liquid_float_complex resampled_data[out_size];
liquid_float_complex agc_data[out_size]; if (agc_data.size() != out_size) {
if (agc_data.capacity() < out_size) {
agc_data.reserve(out_size);
resampled_data.reserve(out_size);
}
agc_data.resize(out_size);
resampled_data.resize(out_size);
}
unsigned int num_written; unsigned int num_written;
msresamp_crcf_execute(resampler, &inp.data[0], bufSize, resampled_data, &num_written); msresamp_crcf_execute(resampler, &(inp->data[0]), bufSize, &resampled_data[0], &num_written);
agc_crcf_execute_block(agc, resampled_data, num_written, agc_data); agc_crcf_execute_block(agc, &resampled_data[0], num_written, &agc_data[0]);
float audio_resample_ratio = inp.audio_resample_ratio; float audio_resample_ratio = inp->audio_resample_ratio;
float demod_output[num_written];
freqdem_demodulate_block(fdem, agc_data, num_written, demod_output); if (demod_output.size() != num_written) {
if (demod_output.capacity() < num_written) {
demod_output.reserve(num_written);
}
demod_output.resize(num_written);
}
freqdem_demodulate_block(fdem, &agc_data[0], num_written, &demod_output[0]);
int audio_out_size = ceil((float) (num_written) * audio_resample_ratio); int audio_out_size = ceil((float) (num_written) * audio_resample_ratio);
float resampled_audio_output[audio_out_size];
if (audio_out_size != resampled_audio_output.size()) {
if (resampled_audio_output.capacity() < audio_out_size) {
resampled_audio_output.reserve(audio_out_size);
}
resampled_audio_output.resize(audio_out_size);
}
unsigned int num_audio_written; unsigned int num_audio_written;
msresamp_rrrf_execute(audio_resampler, demod_output, num_written, resampled_audio_output, &num_audio_written); msresamp_rrrf_execute(audio_resampler, &demod_output[0], num_written, &resampled_audio_output[0], &num_audio_written);
AudioThreadInput ati;
ati.channels = 1;
ati.data.assign(resampled_audio_output,resampled_audio_output+num_audio_written);
if (audioInputQueue != NULL) { if (audioInputQueue != NULL) {
if (!squelch_enabled || ((agc_crcf_get_signal_level(agc)) >= 0.1)) { if (!squelch_enabled || ((agc_crcf_get_signal_level(agc)) >= 0.1)) {
AudioThreadInput *ati = NULL;
for (buffers_i = buffers.begin(); buffers_i != buffers.end(); buffers_i++) {
if ((*buffers_i)->getRefCount() <= 0) {
ati = (*buffers_i);
break;
}
}
if (ati == NULL) {
ati = new AudioThreadInput;
buffers.push_back(ati);
}
ati->setRefCount(1);
ati->channels = 1;
ati->data.assign(resampled_audio_output.begin(), resampled_audio_output.begin() + num_audio_written);
audioInputQueue->push(ati); audioInputQueue->push(ati);
} }
} }
if (visOutQueue != NULL && visOutQueue->empty()) { if (visOutQueue != NULL && visOutQueue->empty()) {
AudioThreadInput ati_vis; AudioThreadInput *ati_vis = new AudioThreadInput;
ati_vis.channels = ati.channels; ati_vis->channels = 1;
int num_vis = DEMOD_VIS_SIZE; int num_vis = DEMOD_VIS_SIZE;
if (num_audio_written > num_written) { if (num_audio_written > num_written) {
if (num_vis > num_audio_written) { if (num_vis > num_audio_written) {
num_vis = num_audio_written; num_vis = num_audio_written;
} }
ati_vis.data.assign(ati.data.begin(), ati.data.begin()+num_vis); ati_vis->data.assign(resampled_audio_output.begin(), resampled_audio_output.begin() + num_vis);
} else { } else {
if (num_vis > num_written) { if (num_vis > num_written) {
num_vis = num_written; num_vis = num_written;
} }
ati_vis.data.assign(demod_output, demod_output + num_vis); ati_vis->data.assign(demod_output.begin(), demod_output.begin() + num_vis);
} }
visOutQueue->push(ati_vis); visOutQueue->push(ati_vis);
@ -128,6 +174,7 @@ void DemodulatorThread::threadMain() {
} }
} }
inp->decRefCount();
} }
if (resampler != NULL) { if (resampler != NULL) {
@ -139,6 +186,13 @@ void DemodulatorThread::threadMain() {
agc_crcf_destroy(agc); agc_crcf_destroy(agc);
while (!buffers.empty()) {
AudioThreadInput *audioDataDel = buffers.front();
buffers.pop_front();
std::lock_guard < std::mutex > lock(audioDataDel->m_mutex);
delete audioDataDel;
}
std::cout << "Demodulator thread done." << std::endl; std::cout << "Demodulator thread done." << std::endl;
DemodulatorThreadCommand tCmd(DemodulatorThreadCommand::DEMOD_THREAD_CMD_DEMOD_TERMINATED); DemodulatorThreadCommand tCmd(DemodulatorThreadCommand::DEMOD_THREAD_CMD_DEMOD_TERMINATED);
tCmd.context = this; tCmd.context = this;
@ -147,6 +201,6 @@ void DemodulatorThread::threadMain() {
void DemodulatorThread::terminate() { void DemodulatorThread::terminate() {
terminated = true; terminated = true;
DemodulatorThreadPostIQData inp; // push dummy to nudge queue DemodulatorThreadPostIQData *inp = new DemodulatorThreadPostIQData; // push dummy to nudge queue
postInputQueue->push(inp); postInputQueue->push(inp);
} }

View File

@ -6,14 +6,15 @@
#include "DemodDefs.h" #include "DemodDefs.h"
#include "AudioThread.h" #include "AudioThread.h"
typedef ThreadQueue<AudioThreadInput> DemodulatorThreadOutputQueue; typedef ThreadQueue<AudioThreadInput *> DemodulatorThreadOutputQueue;
#define DEMOD_VIS_SIZE 2048 #define DEMOD_VIS_SIZE 2048
class DemodulatorThread { class DemodulatorThread {
public: public:
DemodulatorThread(DemodulatorThreadPostInputQueue* pQueueIn, DemodulatorThreadControlCommandQueue *threadQueueControl, DemodulatorThreadCommandQueue* threadQueueNotify); DemodulatorThread(DemodulatorThreadPostInputQueue* pQueueIn, DemodulatorThreadControlCommandQueue *threadQueueControl,
DemodulatorThreadCommandQueue* threadQueueNotify);
~DemodulatorThread(); ~DemodulatorThread();
#ifdef __APPLE__ #ifdef __APPLE__
@ -53,6 +54,5 @@ protected:
DemodulatorThreadCommandQueue* threadQueueNotify; DemodulatorThreadCommandQueue* threadQueueNotify;
DemodulatorThreadControlCommandQueue *threadQueueControl; DemodulatorThreadControlCommandQueue *threadQueueControl;
float squelch_level; float squelch_level;
float squelch_tolerance; float squelch_tolerance;bool squelch_enabled;
bool squelch_enabled;
}; };

View File

@ -22,17 +22,19 @@ public:
}; };
DemodulatorWorkerThreadResult() : DemodulatorWorkerThreadResult() :
cmd(DEMOD_WORKER_THREAD_RESULT_NULL), audioSampleRate(0), bandwidth(0), inputRate(0), fir_filter(NULL), resampler(NULL), resample_ratio( cmd(DEMOD_WORKER_THREAD_RESULT_NULL), fir_filter(NULL), resampler(NULL), resample_ratio(0), audio_resampler(NULL), audio_resample_ratio(
0), audio_resampler(NULL), audio_resample_ratio(0) { 0), inputRate(0), bandwidth(0), audioSampleRate(0) {
} }
DemodulatorWorkerThreadResult(DemodulatorThreadResultEnum cmd) : DemodulatorWorkerThreadResult(DemodulatorThreadResultEnum cmd) :
cmd(cmd), audioSampleRate(0), bandwidth(0), inputRate(0), fir_filter(NULL), resampler(NULL), resample_ratio(0), audio_resampler(NULL), audio_resample_ratio( cmd(cmd), fir_filter(NULL), resampler(NULL), resample_ratio(0), audio_resampler(NULL), audio_resample_ratio(0), inputRate(0), bandwidth(
0) { 0), audioSampleRate(0) {
} }
DemodulatorThreadResultEnum cmd;
firfilt_crcf fir_filter; firfilt_crcf fir_filter;
msresamp_crcf resampler; msresamp_crcf resampler;
float resample_ratio; float resample_ratio;
@ -43,7 +45,6 @@ public:
unsigned int bandwidth; unsigned int bandwidth;
unsigned int audioSampleRate; unsigned int audioSampleRate;
DemodulatorThreadResultEnum cmd;
}; };
class DemodulatorWorkerThreadCommand { class DemodulatorWorkerThreadCommand {
@ -62,12 +63,12 @@ public:
} }
DemodulatorThreadCommandEnum cmd;
unsigned int frequency; unsigned int frequency;
unsigned int inputRate; unsigned int inputRate;
unsigned int bandwidth; unsigned int bandwidth;
unsigned int audioSampleRate; unsigned int audioSampleRate;
DemodulatorThreadCommandEnum cmd;
}; };
typedef ThreadQueue<DemodulatorWorkerThreadCommand> DemodulatorThreadWorkerCommandQueue; typedef ThreadQueue<DemodulatorWorkerThreadCommand> DemodulatorThreadWorkerCommandQueue;

View File

@ -1,17 +1,19 @@
#include "SDRPostThread.h" #include "SDRPostThread.h"
#include "CubicSDRDefs.h" #include "CubicSDRDefs.h"
#include <vector>
#include "CubicSDR.h" #include "CubicSDR.h"
#include <vector>
#include <deque>
SDRPostThread::SDRPostThread() : SDRPostThread::SDRPostThread() :
iqDataInQueue(NULL), iqDataOutQueue(NULL), iqVisualQueue(NULL), terminated(false), dcFilter(NULL), sample_rate(SRATE) { sample_rate(SRATE), iqDataOutQueue(NULL), iqDataInQueue(NULL), iqVisualQueue(NULL), terminated(false), dcFilter(NULL) {
} }
SDRPostThread::~SDRPostThread() { SDRPostThread::~SDRPostThread() {
} }
void SDRPostThread::bindDemodulator(DemodulatorInstance *demod) { void SDRPostThread::bindDemodulator(DemodulatorInstance *demod) {
demodulators.push_back(demod); demodulators_add.push_back(demod);
} }
void SDRPostThread::removeDemodulator(DemodulatorInstance *demod) { void SDRPostThread::removeDemodulator(DemodulatorInstance *demod) {
@ -19,22 +21,16 @@ void SDRPostThread::removeDemodulator(DemodulatorInstance *demod) {
return; return;
} }
std::vector<DemodulatorInstance *>::iterator i; demodulators_remove.push_back(demod);
i = std::find(demodulators.begin(), demodulators.end(), demod);
if (i != demodulators.end()) {
demodulators.erase(i);
}
} }
void SDRPostThread::setIQDataInQueue(SDRThreadIQDataQueue* iqDataQueue) { void SDRPostThread::setIQDataInQueue(SDRThreadIQDataQueue* iqDataQueue) {
iqDataInQueue = iqDataQueue; iqDataInQueue = iqDataQueue;
} }
void SDRPostThread::setIQDataOutQueue(SDRThreadIQDataQueue* iqDataQueue) { void SDRPostThread::setIQDataOutQueue(DemodulatorThreadInputQueue* iqDataQueue) {
iqDataOutQueue = iqDataQueue; iqDataOutQueue = iqDataQueue;
} }
void SDRPostThread::setIQVisualQueue(SDRThreadIQDataQueue *iqVisQueue) { void SDRPostThread::setIQVisualQueue(DemodulatorThreadInputQueue *iqVisQueue) {
iqVisualQueue = iqVisQueue; iqVisualQueue = iqVisQueue;
} }
@ -51,77 +47,156 @@ void SDRPostThread::threadMain() {
dcFilter = iirfilt_crcf_create_dc_blocker(0.0005); dcFilter = iirfilt_crcf_create_dc_blocker(0.0005);
liquid_float_complex x, y;
std::cout << "SDR post-processing thread started.." << std::endl; std::cout << "SDR post-processing thread started.." << std::endl;
std::deque<DemodulatorThreadIQData *> buffers;
std::deque<DemodulatorThreadIQData *>::iterator buffers_i;
std::vector<liquid_float_complex> fpData;
std::vector<liquid_float_complex> dataOut;
while (!terminated) { while (!terminated) {
SDRThreadIQData data_in; SDRThreadIQData *data_in;
iqDataInQueue.load()->pop(data_in); iqDataInQueue.load()->pop(data_in);
// std::lock_guard < std::mutex > lock(data_in->m_mutex);
if (data_in.data.size()) { if (data_in && data_in->data.size()) {
SDRThreadIQData dataOut; int dataSize = data_in->data.size()/2;
if (dataSize > fpData.capacity()) {
dataOut.frequency = data_in.frequency; fpData.reserve(dataSize);
dataOut.bandwidth = data_in.bandwidth; dataOut.reserve(dataSize);
dataOut.data = data_in.data;
for (int i = 0, iMax = dataOut.data.size() / 2; i < iMax; i++) {
x.real = (float) dataOut.data[i * 2] / 127.0;
x.imag = (float) dataOut.data[i * 2 + 1] / 127.0;
iirfilt_crcf_execute(dcFilter, x, &y);
dataOut.data[i * 2] = (signed char) floor(y.real * 127.0);
dataOut.data[i * 2 + 1] = (signed char) floor(y.imag * 127.0);
} }
if (dataSize != fpData.size()) {
fpData.resize(dataSize);
dataOut.resize(dataSize);
}
for (int i = 0, iMax = dataSize; i < iMax; i++) {
fpData[i].real = (float) data_in->data[i * 2] / 127.0;
fpData[i].imag = (float) data_in->data[i * 2 + 1] / 127.0;
}
iirfilt_crcf_execute_block(dcFilter, &fpData[0], dataSize, &dataOut[0]);
if (iqDataOutQueue != NULL) { if (iqDataOutQueue != NULL) {
iqDataOutQueue.load()->push(dataOut); DemodulatorThreadIQData *pipeDataOut = new DemodulatorThreadIQData;
pipeDataOut->frequency = data_in->frequency;
pipeDataOut->bandwidth = data_in->bandwidth;
pipeDataOut->data.assign(dataOut.begin(), dataOut.end());
iqDataOutQueue.load()->push(pipeDataOut);
} }
if (iqVisualQueue != NULL) { if (iqVisualQueue != NULL && iqVisualQueue.load()->empty()) {
if (iqVisualQueue.load()->empty()) { DemodulatorThreadIQData *visualDataOut = new DemodulatorThreadIQData;
iqVisualQueue.load()->push(dataOut); visualDataOut->data.assign(dataOut.begin(), dataOut.begin() + FFT_SIZE);
iqVisualQueue.load()->push(visualDataOut);
} }
if (demodulators_add.size()) {
while (!demodulators_add.empty()) {
demodulators.push_back(demodulators_add.back());
demodulators_add.pop_back();
}
}
if (demodulators_remove.size()) {
while (!demodulators_remove.empty()) {
DemodulatorInstance *demod = demodulators_remove.back();
demodulators_remove.pop_back();
std::vector<DemodulatorInstance *>::iterator i = std::find(demodulators.begin(), demodulators.end(), demod);
if (i != demodulators.end()) {
demodulators.erase(i);
}
}
}
int activeDemods = 0;
bool pushedData = false;
if (demodulators.size()) {
std::vector<DemodulatorInstance *>::iterator i;
for (i = demodulators.begin(); i != demodulators.end(); i++) {
DemodulatorInstance *demod = *i;
if (demod->getParams().frequency != data_in->frequency
&& abs(data_in->frequency - demod->getParams().frequency) > (int) ((float) ((float) SRATE / 2.0))) {
continue;
}
activeDemods++;
} }
if (demodulators.size()) { if (demodulators.size()) {
DemodulatorThreadIQData dummyDataOut;
dummyDataOut.frequency = data_in.frequency; DemodulatorThreadIQData *demodDataOut = NULL;
dummyDataOut.bandwidth = data_in.bandwidth;
DemodulatorThreadIQData demodDataOut; for (buffers_i = buffers.begin(); buffers_i != buffers.end(); buffers_i++) {
demodDataOut.frequency = data_in.frequency; if ((*buffers_i)->getRefCount() <= 0) {
demodDataOut.bandwidth = data_in.bandwidth; demodDataOut = (*buffers_i);
demodDataOut.data = data_in.data; break;
}
}
if (demodDataOut == NULL) {
demodDataOut = new DemodulatorThreadIQData;
buffers.push_back(demodDataOut);
}
// std::lock_guard < std::mutex > lock(demodDataOut->m_mutex);
demodDataOut->frequency = data_in->frequency;
demodDataOut->bandwidth = data_in->bandwidth;
demodDataOut->setRefCount(activeDemods);
demodDataOut->data.assign(dataOut.begin(), dataOut.end());
std::vector<DemodulatorInstance *>::iterator i; std::vector<DemodulatorInstance *>::iterator i;
for (i = demodulators.begin(); i != demodulators.end(); i++) { for (i = demodulators.begin(); i != demodulators.end(); i++) {
DemodulatorInstance *demod = *i; DemodulatorInstance *demod = *i;
DemodulatorThreadInputQueue *demodQueue = demod->threadQueueDemod; DemodulatorThreadInputQueue *demodQueue = demod->threadQueueDemod;
if (demod->getParams().frequency != data_in.frequency if (demod->getParams().frequency != data_in->frequency
&& abs(data_in.frequency - demod->getParams().frequency) > (int) ((float) ((float) SRATE / 2.0) * 1.15)) { && abs(data_in->frequency - demod->getParams().frequency) > (int) ((float) ((float) SRATE / 2.0))) {
if (demod->isActive()) { if (demod->isActive()) {
demod->setActive(false); demod->setActive(false);
DemodulatorThreadIQData *dummyDataOut = new DemodulatorThreadIQData;
dummyDataOut->frequency = data_in->frequency;
dummyDataOut->bandwidth = data_in->bandwidth;
demodQueue->push(dummyDataOut); demodQueue->push(dummyDataOut);
continue;
} }
} else if (!demod->isActive()) { } else if (!demod->isActive()) {
demod->setActive(true); demod->setActive(true);
} }
if (!demod->isActive()) {
continue;
}
demodQueue->push(demodDataOut); demodQueue->push(demodDataOut);
pushedData = true;
}
if (!pushedData) {
demodDataOut->setRefCount(0);
} }
} }
} }
} }
data_in->decRefCount();
}
while (!buffers.empty()) {
DemodulatorThreadIQData *demodDataDel = buffers.front();
buffers.pop_front();
// std::lock_guard < std::mutex > lock(demodDataDel->m_mutex);
// delete demodDataDel;
}
std::cout << "SDR post-processing thread done." << std::endl; std::cout << "SDR post-processing thread done." << std::endl;
} }
void SDRPostThread::terminate() { void SDRPostThread::terminate() {
terminated = true; terminated = true;
SDRThreadIQData dummy; SDRThreadIQData *dummy = new SDRThreadIQData;
iqDataInQueue.load()->push(dummy); iqDataInQueue.load()->push(dummy);
} }

View File

@ -12,8 +12,8 @@ public:
void removeDemodulator(DemodulatorInstance *demod); void removeDemodulator(DemodulatorInstance *demod);
void setIQDataInQueue(SDRThreadIQDataQueue* iqDataQueue); void setIQDataInQueue(SDRThreadIQDataQueue* iqDataQueue);
void setIQDataOutQueue(SDRThreadIQDataQueue* iqDataQueue); void setIQDataOutQueue(DemodulatorThreadInputQueue* iqDataQueue);
void setIQVisualQueue(SDRThreadIQDataQueue *iqVisQueue); void setIQVisualQueue(DemodulatorThreadInputQueue* iqVisQueue);
void threadMain(); void threadMain();
void terminate(); void terminate();
@ -21,11 +21,13 @@ public:
protected: protected:
uint32_t sample_rate; uint32_t sample_rate;
std::atomic<SDRThreadIQDataQueue*> iqDataOutQueue;
std::atomic<SDRThreadIQDataQueue *> iqDataInQueue; std::atomic<SDRThreadIQDataQueue *> iqDataInQueue;
std::atomic<SDRThreadIQDataQueue*> iqVisualQueue; std::atomic<DemodulatorThreadInputQueue *> iqDataOutQueue;
std::atomic<DemodulatorThreadInputQueue *> iqVisualQueue;
std::vector<DemodulatorInstance *> demodulators; std::vector<DemodulatorInstance *> demodulators;
std::vector<DemodulatorInstance *> demodulators_add;
std::vector<DemodulatorInstance *> demodulators_remove;
std::atomic<bool> terminated; std::atomic<bool> terminated;
iirfilt_crcf dcFilter; iirfilt_crcf dcFilter;
}; };

View File

@ -128,6 +128,10 @@ void SDRThread::threadMain() {
double seconds = 0.0; double seconds = 0.0;
std::cout << "SDR thread started.." << std::endl; std::cout << "SDR thread started.." << std::endl;
std::deque<SDRThreadIQData *> buffers;
std::deque<SDRThreadIQData *>::iterator buffers_i;
while (!terminated) { while (!terminated) {
SDRThreadCommandQueue *cmdQueue = m_pQueue.load(); SDRThreadCommandQueue *cmdQueue = m_pQueue.load();
@ -145,6 +149,8 @@ void SDRThread::threadMain() {
freq_changed = true; freq_changed = true;
new_freq = command.int_value; new_freq = command.int_value;
break; break;
default:
break;
} }
} }
@ -156,24 +162,52 @@ void SDRThread::threadMain() {
rtlsdr_read_sync(dev, buf, BUF_SIZE, &n_read); rtlsdr_read_sync(dev, buf, BUF_SIZE, &n_read);
std::vector<signed char> new_buffer; SDRThreadIQData *dataOut = NULL;
for (buffers_i = buffers.begin(); buffers_i != buffers.end(); buffers_i++) {
if ((*buffers_i)->getRefCount() <= 0) {
dataOut = (*buffers_i);
break;
}
}
if (dataOut == NULL) {
dataOut = new SDRThreadIQData;
buffers.push_back(dataOut);
}
// std::lock_guard < std::mutex > lock(dataOut->m_mutex);
dataOut->setRefCount(1);
dataOut->frequency = frequency;
dataOut->bandwidth = bandwidth;
if (dataOut->data.capacity() < n_read) {
dataOut->data.reserve(n_read);
}
if (dataOut->data.size() != n_read) {
dataOut->data.resize(n_read);
}
for (int i = 0; i < n_read; i++) { for (int i = 0; i < n_read; i++) {
new_buffer.push_back(buf[i] - 127); dataOut->data[i] = buf[i] - 127;
} }
double time_slice = (double) n_read / (double) sample_rate; double time_slice = (double) n_read / (double) sample_rate;
seconds += time_slice; seconds += time_slice;
SDRThreadIQData dataOut;
dataOut.frequency = frequency;
dataOut.bandwidth = bandwidth;
dataOut.data = new_buffer;
if (iqDataOutQueue != NULL) { if (iqDataOutQueue != NULL) {
iqDataOutQueue.load()->push(dataOut); iqDataOutQueue.load()->push(dataOut);
} }
} }
while (!buffers.empty()) {
SDRThreadIQData *iqDataDel = buffers.front();
buffers.pop_front();
// std::lock_guard < std::mutex > lock(iqDataDel->m_mutex);
// delete iqDataDel;
}
std::cout << "SDR thread done." << std::endl; std::cout << "SDR thread done." << std::endl;
} }

View File

@ -34,7 +34,7 @@ public:
int int_value; int int_value;
}; };
class SDRThreadIQData { class SDRThreadIQData : public ReferenceCounter {
public: public:
unsigned int frequency; unsigned int frequency;
unsigned int bandwidth; unsigned int bandwidth;
@ -45,8 +45,8 @@ public:
} }
SDRThreadIQData(unsigned int bandwidth, unsigned int frequency, std::vector<signed char> data) : SDRThreadIQData(unsigned int bandwidth, unsigned int frequency, std::vector<signed char> *data) :
data(data), frequency(frequency), bandwidth(bandwidth) { frequency(frequency), bandwidth(bandwidth) {
} }
@ -56,7 +56,7 @@ public:
}; };
typedef ThreadQueue<SDRThreadCommand> SDRThreadCommandQueue; typedef ThreadQueue<SDRThreadCommand> SDRThreadCommandQueue;
typedef ThreadQueue<SDRThreadIQData> SDRThreadIQDataQueue; typedef ThreadQueue<SDRThreadIQData *> SDRThreadIQDataQueue;
class SDRThread { class SDRThread {
public: public:

View File

@ -5,7 +5,7 @@
#include <algorithm> #include <algorithm>
GLFontChar::GLFontChar() : GLFontChar::GLFontChar() :
id(0), x(0), y(0), width(0), height(0), xadvance(0), xoffset(0), yoffset(0), index(0), aspect(1) { id(0), x(0), y(0), width(0), height(0), xoffset(0), yoffset(0), xadvance(0), aspect(1), index(0) {
} }
@ -96,7 +96,7 @@ int GLFontChar::getIndex() {
} }
GLFont::GLFont() : GLFont::GLFont() :
numCharacters(0), imageHeight(0), imageWidth(0), base(0), lineHeight(0), texId(0), loaded(false) { numCharacters(0), lineHeight(0), base(0), imageWidth(0), imageHeight(0), loaded(false), texId(0) {
} }
@ -289,7 +289,7 @@ void GLFont::loadFont(std::string fontFile) {
unsigned int ofs = 0; unsigned int ofs = 0;
for (char_i = characters.begin(); char_i != characters.end(); char_i++) { for (char_i = characters.begin(); char_i != characters.end(); char_i++) {
int charId = (*char_i).first; // int charId = (*char_i).first;
GLFontChar *fchar = (*char_i).second; GLFontChar *fchar = (*char_i).second;
float faspect = fchar->getAspect(); float faspect = fchar->getAspect();

View File

@ -5,14 +5,12 @@
class MouseTracker { class MouseTracker {
public: public:
MouseTracker(wxWindow *target) : MouseTracker(wxWindow *target) :
target(target), mouseX(0), mouseY(0), lastMouseX(0), lastMouseY(0), originMouseX(0), originMouseY(0), deltaMouseX(0), deltaMouseY(0), isMouseDown( mouseX(0), mouseY(0), lastMouseX(0), lastMouseY(0), originMouseX(0), originMouseY(0), deltaMouseX(0), deltaMouseY(0), vertDragLock(false), horizDragLock(false), isMouseDown(false), isMouseInView(false), target(target) {
false), vertDragLock(false), horizDragLock(false), isMouseInView(false) {
} }
MouseTracker() : MouseTracker() :
target(NULL), mouseX(0), mouseY(0), lastMouseX(0), lastMouseY(0), originMouseX(0), originMouseY(0), deltaMouseX(0), deltaMouseY(0), isMouseDown( mouseX(0), mouseY(0), lastMouseX(0), lastMouseY(0), originMouseX(0), originMouseY(0), deltaMouseX(0), deltaMouseY(0), vertDragLock(false), horizDragLock(false), isMouseDown(false), isMouseInView(false), target(NULL) {
false), vertDragLock(false), horizDragLock(false), isMouseInView(false) {
} }
@ -43,10 +41,10 @@ public:
private: private:
float mouseX, mouseY; float mouseX, mouseY;
float lastMouseX, lastMouseY; float lastMouseX, lastMouseY;
float deltaMouseX, deltaMouseY;
float originMouseX, originMouseY; float originMouseX, originMouseY;
float deltaMouseX, deltaMouseY;
bool isMouseDown, isMouseInView;
bool vertDragLock, horizDragLock; bool vertDragLock, horizDragLock;
bool isMouseDown, isMouseInView;
wxWindow *target; wxWindow *target;
}; };

View File

@ -5,7 +5,7 @@
#include <mmsystem.h> #include <mmsystem.h>
#endif #endif
Timer::Timer(void) : time_elapsed(0), system_milliseconds(0), start_time(0), end_time(0), last_update(0), paused_time(0), offset(0), paused_state(false), num_updates(0), lock_state(0), lock_rate(0) Timer::Timer(void) : time_elapsed(0), system_milliseconds(0), start_time(0), end_time(0), last_update(0), num_updates(0), paused_time(0), offset(0), paused_state(false), lock_state(0), lock_rate(0)
{ {
} }

View File

@ -29,7 +29,7 @@ SpectrumCanvas::SpectrumCanvas(wxWindow *parent, int *attribList) :
wxGLCanvas(parent, wxID_ANY, attribList, wxDefaultPosition, wxDefaultSize, wxGLCanvas(parent, wxID_ANY, attribList, wxDefaultPosition, wxDefaultSize,
wxFULL_REPAINT_ON_RESIZE), parent(parent), frameTimer(0) { wxFULL_REPAINT_ON_RESIZE), parent(parent), frameTimer(0) {
int in_block_size = BUF_SIZE / 2; int in_block_size = FFT_SIZE;
int out_block_size = FFT_SIZE; int out_block_size = FFT_SIZE;
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * in_block_size); in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * in_block_size);
@ -73,16 +73,16 @@ void SpectrumCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) {
SwapBuffers(); SwapBuffers();
} }
void SpectrumCanvas::setData(std::vector<signed char> *data) { void SpectrumCanvas::setData(std::vector<liquid_float_complex> *data) {
if (data && data->size()) { if (data && data->size()) {
if (spectrum_points.size() < FFT_SIZE * 2) { if (spectrum_points.size() < FFT_SIZE * 2) {
spectrum_points.resize(FFT_SIZE * 2); spectrum_points.resize(FFT_SIZE * 2);
} }
for (int i = 0; i < BUF_SIZE / 2; i++) { for (int i = 0; i < FFT_SIZE; i++) {
in[i][0] = (float) (*data)[i * 2] / 127.0f; in[i][0] = (*data)[i].real;
in[i][1] = (float) (*data)[i * 2 + 1] / 127.0f; in[i][1] = (*data)[i].imag;
} }
fftw_execute(plan); fftw_execute(plan);

View File

@ -17,7 +17,7 @@ public:
SpectrumCanvas(wxWindow *parent, int *attribList = NULL); SpectrumCanvas(wxWindow *parent, int *attribList = NULL);
~SpectrumCanvas(); ~SpectrumCanvas();
void setData(std::vector<signed char> *data); void setData(std::vector<liquid_float_complex> *data);
private: private:
void OnPaint(wxPaintEvent& event); void OnPaint(wxPaintEvent& event);

View File

@ -30,10 +30,10 @@ wxEND_EVENT_TABLE()
WaterfallCanvas::WaterfallCanvas(wxWindow *parent, int *attribList) : WaterfallCanvas::WaterfallCanvas(wxWindow *parent, int *attribList) :
wxGLCanvas(parent, wxID_ANY, attribList, wxDefaultPosition, wxDefaultSize, wxGLCanvas(parent, wxID_ANY, attribList, wxDefaultPosition, wxDefaultSize,
wxFULL_REPAINT_ON_RESIZE), parent(parent), frameTimer(0), dragState(WF_DRAG_NONE), nextDragState(WF_DRAG_NONE), shiftDown( wxFULL_REPAINT_ON_RESIZE), parent(parent), frameTimer(0), activeDemodulatorBandwidth(0), activeDemodulatorFrequency(0), dragState(
false), altDown(false), ctrlDown(false), activeDemodulatorBandwidth(0), activeDemodulatorFrequency(0) { WF_DRAG_NONE), nextDragState(WF_DRAG_NONE), shiftDown(false), altDown(false), ctrlDown(false) {
int in_block_size = BUF_SIZE / 2; int in_block_size = FFT_SIZE;
int out_block_size = FFT_SIZE; int out_block_size = FFT_SIZE;
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * in_block_size); in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * in_block_size);
@ -77,7 +77,8 @@ void WaterfallCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) {
DemodulatorInstance *activeDemodulator = wxGetApp().getDemodMgr().getActiveDemodulator(); DemodulatorInstance *activeDemodulator = wxGetApp().getDemodMgr().getActiveDemodulator();
DemodulatorInstance *lastActiveDemodulator = wxGetApp().getDemodMgr().getLastActiveDemodulator(); DemodulatorInstance *lastActiveDemodulator = wxGetApp().getDemodMgr().getLastActiveDemodulator();
bool isNew = shiftDown || (wxGetApp().getDemodMgr().getLastActiveDemodulator() && !wxGetApp().getDemodMgr().getLastActiveDemodulator()->isActive()); bool isNew = shiftDown
|| (wxGetApp().getDemodMgr().getLastActiveDemodulator() && !wxGetApp().getDemodMgr().getLastActiveDemodulator()->isActive());
if (mTracker.mouseInView()) { if (mTracker.mouseInView()) {
if (nextDragState == WF_DRAG_RANGE) { if (nextDragState == WF_DRAG_RANGE) {
@ -163,13 +164,21 @@ void WaterfallCanvas::OnKeyDown(wxKeyEvent& event) {
switch (event.GetKeyCode()) { switch (event.GetKeyCode()) {
case WXK_RIGHT: case WXK_RIGHT:
freq = wxGetApp().getFrequency(); freq = wxGetApp().getFrequency();
if (shiftDown) {
freq += SRATE * 10;
} else {
freq += SRATE / 2; freq += SRATE / 2;
}
wxGetApp().setFrequency(freq); wxGetApp().setFrequency(freq);
((wxFrame*) parent)->GetStatusBar()->SetStatusText(wxString::Format(wxT("Set center frequency: %i"), freq)); ((wxFrame*) parent)->GetStatusBar()->SetStatusText(wxString::Format(wxT("Set center frequency: %i"), freq));
break; break;
case WXK_LEFT: case WXK_LEFT:
freq = wxGetApp().getFrequency(); freq = wxGetApp().getFrequency();
if (shiftDown) {
freq -= SRATE * 10;
} else {
freq -= SRATE / 2; freq -= SRATE / 2;
}
wxGetApp().setFrequency(freq); wxGetApp().setFrequency(freq);
((wxFrame*) parent)->GetStatusBar()->SetStatusText(wxString::Format(wxT("Set center frequency: %i"), freq)); ((wxFrame*) parent)->GetStatusBar()->SetStatusText(wxString::Format(wxT("Set center frequency: %i"), freq));
break; break;
@ -197,16 +206,16 @@ void WaterfallCanvas::OnKeyDown(wxKeyEvent& event) {
} }
} }
void WaterfallCanvas::setData(std::vector<signed char> *data) { void WaterfallCanvas::setData(std::vector<liquid_float_complex> *data) {
if (data && data->size()) { if (data && data->size()) {
if (spectrum_points.size() < FFT_SIZE * 2) { if (spectrum_points.size() < FFT_SIZE * 2) {
spectrum_points.resize(FFT_SIZE * 2); spectrum_points.resize(FFT_SIZE * 2);
} }
for (int i = 0; i < BUF_SIZE / 2; i++) { for (int i = 0; i < FFT_SIZE; i++) {
in[i][0] = (float) (*data)[i * 2] / 127.0f; in[i][0] = (*data)[i].real;
in[i][1] = (float) (*data)[i * 2 + 1] / 127.0f; in[i][1] = (*data)[i].imag;
} }
fftw_execute(plan); fftw_execute(plan);
@ -431,7 +440,8 @@ void WaterfallCanvas::mouseReleased(wxMouseEvent& event) {
altDown = event.AltDown(); altDown = event.AltDown();
ctrlDown = event.ControlDown(); ctrlDown = event.ControlDown();
bool isNew = shiftDown || (wxGetApp().getDemodMgr().getLastActiveDemodulator() && !wxGetApp().getDemodMgr().getLastActiveDemodulator()->isActive()); bool isNew = shiftDown
|| (wxGetApp().getDemodMgr().getLastActiveDemodulator() && !wxGetApp().getDemodMgr().getLastActiveDemodulator()->isActive());
mTracker.setVertDragLock(false); mTracker.setVertDragLock(false);
mTracker.setHorizDragLock(false); mTracker.setHorizDragLock(false);

View File

@ -21,7 +21,7 @@ public:
WaterfallCanvas(wxWindow *parent, int *attribList = NULL); WaterfallCanvas(wxWindow *parent, int *attribList = NULL);
~WaterfallCanvas(); ~WaterfallCanvas();
void setData(std::vector<signed char> *data); void setData(std::vector<liquid_float_complex> *data);
int GetFrequencyAt(float x); int GetFrequencyAt(float x);
private: private: