Merge branch 'thread-data-optimization' of https://github.com/cjcliffe/CubicSDR into thread-data-optimization

This commit is contained in:
Charles J. Cliffe 2014-12-23 02:01:46 -05:00
commit 3f00148de8
17 changed files with 212 additions and 217 deletions

View File

@ -96,14 +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; SDRThreadIQData *iqData;
wxGetApp().getIQVisualQueue()->pop(iqData); wxGetApp().getIQVisualQueue()->pop(iqData);
if (iqData.data && 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.data; delete iqData;
} else { } else {
std::cout << "Incoming IQ data empty?" << std::endl; std::cout << "Incoming IQ data empty?" << std::endl;
} }
@ -111,20 +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 && 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.data; delete demodAudioData;
} else { } else {
std::cout << "Incoming Demodulator data empty?" << std::endl; std::cout << "Incoming Demodulator data empty?" << std::endl;
} }

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) {
} }

View File

@ -10,8 +10,7 @@ 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( inputQueue(inputQueue), audio_queue_ptr(0), underflow_count(0), terminated(false), active(false), gain(1.0), threadQueueNotify(threadQueueNotify) {
false) {
#ifdef __APPLE__ #ifdef __APPLE__
boundThreads = new std::vector<AudioThread *>; boundThreads = new std::vector<AudioThread *>;
#endif #endif
@ -65,44 +64,65 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu
continue; continue;
} }
if (srcmix->currentInput.channels == 0 || !srcmix->currentInput.data) { if (!srcmix->currentInput) {
if (srcmix->terminated) {
continue;
}
srcmix->inputQueue->pop(srcmix->currentInput);
srcmix->audio_queue_ptr = 0;
continue;
}
if (srcmix->currentInput->channels == 0 || !srcmix->currentInput->data.size()) {
if (!srcmix->inputQueue->empty()) { if (!srcmix->inputQueue->empty()) {
if (srcmix->currentInput.data) { if (srcmix->currentInput) {
delete srcmix->currentInput.data; delete srcmix->currentInput;
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;
} }
return 0; 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.data) { if (srcmix->currentInput) {
delete srcmix->currentInput.data; delete srcmix->currentInput;
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;
} }
if (srcmix->currentInput.data && srcmix->currentInput.data->size()) { if (srcmix->currentInput && srcmix->currentInput->data.size()) {
float v = (*srcmix->currentInput.data)[srcmix->audio_queue_ptr] * src->gain; 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.data) { if (srcmix->currentInput) {
delete srcmix->currentInput.data; delete srcmix->currentInput;
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;
} }
if (srcmix->currentInput.data && srcmix->currentInput.data->size()) { if (srcmix->currentInput && srcmix->currentInput->data.size()) {
out[i] = out[i] + (*srcmix->currentInput.data)[srcmix->audio_queue_ptr] * src->gain; out[i] = out[i] + srcmix->currentInput->data[srcmix->audio_queue_ptr] * src->gain;
} }
srcmix->audio_queue_ptr++; srcmix->audio_queue_ptr++;
} }
@ -123,11 +143,21 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu
if (status) { if (status) {
std::cout << "Audio buffer underflow.." << (src->underflow_count++) << std::endl; std::cout << "Audio buffer underflow.." << (src->underflow_count++) << std::endl;
} }
if (!src->currentInput) {
src->inputQueue->pop(src->currentInput);
src->audio_queue_ptr = 0;
return 0;
}
if (src->currentInput.channels == 0 || !src->currentInput.data) { if (src->currentInput->channels == 0 || !src->currentInput->data.size()) {
if (!src->inputQueue->empty()) { if (!src->inputQueue->empty()) {
if (src->currentInput.data) { if (src->currentInput) {
delete src->currentInput.data; delete src->currentInput;
src->currentInput = NULL;
}
if (src->terminated) {
return 1;
} }
src->inputQueue->pop(src->currentInput); src->inputQueue->pop(src->currentInput);
src->audio_queue_ptr = 0; src->audio_queue_ptr = 0;
@ -135,34 +165,35 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu
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.data) { if (src->currentInput) {
delete src->currentInput.data; delete src->currentInput;
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;
} }
if (src->currentInput.data && src->currentInput.data->size()) { if (src->currentInput && src->currentInput->data.size()) {
out[i * 2] = out[i * 2 + 1] = (*src->currentInput.data)[src->audio_queue_ptr] * src->gain; 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;
} }
if (src->currentInput.data && src->currentInput.data->size()) { if (src->currentInput && src->currentInput->data.size()) {
out[i] = (*src->currentInput.data)[src->audio_queue_ptr] * src->gain; out[i] = src->currentInput->data[src->audio_queue_ptr] * src->gain;
} }
src->audio_queue_ptr++; src->audio_queue_ptr++;
} }
@ -322,12 +353,12 @@ 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) {
while (!inputQueue->empty()) { // flush queue while (!inputQueue->empty()) { // flush queue
inputQueue->pop(dummy); inputQueue->pop(dummy);
if (dummy.data) { if (dummy) {
delete dummy.data; delete dummy;
} }
} }
deviceController[parameters.deviceId]->bindThread(this); deviceController[parameters.deviceId]->bindThread(this);
@ -335,8 +366,8 @@ void AudioThread::setActive(bool state) {
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.data) { if (dummy) {
delete dummy.data; delete dummy;
} }
} }
} }

View File

@ -24,7 +24,7 @@ public:
int sampleRate; int sampleRate;
int channels; int channels;
AudioThreadInput(): frequency(0), sampleRate(0), channels(0), data(NULL) { AudioThreadInput(): frequency(0), sampleRate(0), channels(0) {
} }
@ -32,7 +32,7 @@ public:
} }
std::vector<float> *data; 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

@ -26,12 +26,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) {
} }
@ -59,62 +59,45 @@ class DemodulatorThreadIQData {
public: public:
unsigned int frequency; unsigned int frequency;
unsigned int bandwidth; unsigned int bandwidth;
std::vector<signed char> *data; std::vector<signed char> data;
std::atomic<int> *refCount;
DemodulatorThreadIQData() : DemodulatorThreadIQData() :
frequency(0), bandwidth(0), data(NULL), refCount(NULL) { frequency(0), bandwidth(0), refCount(0) {
} }
DemodulatorThreadIQData(const DemodulatorThreadIQData& o) { void setRefCount(int rc) {
frequency = o.frequency; refCount.store(rc);
bandwidth = o.bandwidth;
data = o.data;
refCount = o.refCount;
}
void setRefCount(std::atomic<int> *rc) {
refCount = rc;
} }
void cleanup() { void decRefCount() {
if (refCount) { refCount.store(refCount.load()-1);
refCount->store(refCount->load()-1); }
if (refCount->load() == 0) {
delete data; int getRefCount() {
data = NULL; return refCount.load();
delete refCount;
refCount = NULL;
}
}
} }
~DemodulatorThreadIQData() { ~DemodulatorThreadIQData() {
} }
private:
std::atomic<int> refCount;
}; };
class DemodulatorThreadPostIQData { class DemodulatorThreadPostIQData {
public: public:
std::vector<liquid_float_complex> *data; std::vector<liquid_float_complex> data;
float audio_resample_ratio; float audio_resample_ratio;
msresamp_rrrf audio_resampler; msresamp_rrrf audio_resampler;
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), data(NULL) { DemodulatorThreadPostIQData(): audio_resample_ratio(0), audio_resampler(NULL), resample_ratio(0), resampler(NULL) {
} }
DemodulatorThreadPostIQData(const DemodulatorThreadPostIQData &o) {
audio_resample_ratio = o.audio_resample_ratio;
audio_resampler = o.audio_resampler;
resample_ratio = o.resample_ratio;
resampler = o.resampler;
data = o.data;
}
~DemodulatorThreadPostIQData() { ~DemodulatorThreadPostIQData() {
} }
@ -130,14 +113,13 @@ public:
std::vector<float> *data; std::vector<float> *data;
DemodulatorThreadAudioData() : DemodulatorThreadAudioData() :
sampleRate(0), frequency(0), channels(0), data(NULL) { 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) :
data(data), sampleRate(sampleRate), frequency(frequency), channels( frequency(frequency), sampleRate(sampleRate), channels(1), data(data) {
1) {
} }
@ -146,8 +128,8 @@ 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;

View File

@ -100,7 +100,7 @@ void DemodulatorPreThread::threadMain() {
std::cout << "Demodulator preprocessor thread started.." << std::endl; std::cout << "Demodulator preprocessor thread started.." << std::endl;
while (!terminated) { while (!terminated) {
DemodulatorThreadIQData inp; DemodulatorThreadIQData *inp;
inputQueue->pop(inp); inputQueue->pop(inp);
bool bandwidthChanged = false; bool bandwidthChanged = false;
@ -144,9 +144,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,8 +157,8 @@ void DemodulatorPreThread::threadMain() {
continue; continue;
} }
std::vector<signed char> *data = inp.data; std::vector<signed char> *data = &inp->data;
if (data && data->size()) { if (data->size()) {
int bufSize = data->size() / 2; int bufSize = data->size() / 2;
liquid_float_complex in_buf_data[bufSize]; liquid_float_complex in_buf_data[bufSize];
@ -184,20 +184,21 @@ void DemodulatorPreThread::threadMain() {
out_buf = temp_buf; out_buf = temp_buf;
} }
DemodulatorThreadPostIQData resamp; DemodulatorThreadPostIQData *resamp = new DemodulatorThreadPostIQData;
resamp.data = new std::vector<liquid_float_complex>; resamp->data.assign(in_buf,in_buf+bufSize);
// resamp.data->resize(bufSize);
resamp.data->assign(in_buf,in_buf+bufSize);
// firfilt_crcf_execute_block(fir_filter, in_buf, bufSize, &((*resamp.data)[0])); // firfilt_crcf_execute_block(fir_filter, in_buf, bufSize, &((*resamp.data)[0]));
resamp.audio_resample_ratio = audio_resample_ratio; resamp->audio_resample_ratio = audio_resample_ratio;
resamp.audio_resampler = audio_resampler; resamp->audio_resampler = audio_resampler;
resamp.resample_ratio = resample_ratio; resamp->resample_ratio = resample_ratio;
resamp.resampler = resampler; resamp->resampler = resampler;
postInputQueue->push(resamp); postInputQueue->push(resamp);
inp.cleanup(); inp->decRefCount();
if (inp->getRefCount()<=0) {
delete inp;
}
} }
if (!workerResults->empty()) { if (!workerResults->empty()) {
@ -236,7 +237,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

@ -7,7 +7,7 @@
#endif #endif
DemodulatorThread::DemodulatorThread(DemodulatorThreadPostInputQueue* pQueue, DemodulatorThreadControlCommandQueue *threadQueueControl, DemodulatorThreadCommandQueue* threadQueueNotify) : DemodulatorThread::DemodulatorThread(DemodulatorThreadPostInputQueue* pQueue, DemodulatorThreadControlCommandQueue *threadQueueControl, DemodulatorThreadCommandQueue* threadQueueNotify) :
postInputQueue(pQueue), visOutQueue(NULL), terminated(false), audioInputQueue(NULL), threadQueueNotify(threadQueueNotify), threadQueueControl(threadQueueControl), agc(NULL), squelch_enabled(false), squelch_level(0), squelch_tolerance(0) { 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);
@ -36,40 +36,36 @@ void DemodulatorThread::threadMain() {
std::cout << "Demodulator thread started.." << std::endl; std::cout << "Demodulator thread started.." << std::endl;
while (!terminated) { while (!terminated) {
DemodulatorThreadPostIQData inp; DemodulatorThreadPostIQData *inp;
postInputQueue->pop(inp); postInputQueue->pop(inp);
if (!inp.data) { int bufSize = inp->data.size();
continue;
}
int bufSize = inp.data->size();
if (!bufSize) { if (!bufSize) {
delete inp.data; delete inp;
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 resampled_data[out_size];
liquid_float_complex agc_data[out_size]; liquid_float_complex agc_data[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, &num_written);
agc_crcf_execute_block(agc, resampled_data, num_written, agc_data); agc_crcf_execute_block(agc, resampled_data, num_written, agc_data);
float audio_resample_ratio = inp.audio_resample_ratio; float audio_resample_ratio = inp->audio_resample_ratio;
float demod_output[num_written]; float demod_output[num_written];
freqdem_demodulate_block(fdem, agc_data, num_written, demod_output); freqdem_demodulate_block(fdem, agc_data, num_written, demod_output);
@ -80,10 +76,9 @@ void DemodulatorThread::threadMain() {
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, num_written, resampled_audio_output, &num_audio_written);
AudioThreadInput ati; AudioThreadInput *ati = new AudioThreadInput;
ati.channels = 1; ati->channels = 1;
ati.data = new std::vector<float>; ati->data.assign(resampled_audio_output,resampled_audio_output+num_audio_written);
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)) {
@ -92,22 +87,20 @@ void DemodulatorThread::threadMain() {
} }
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 = ati->channels;
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 = new std::vector<float>; ati_vis->data.assign(ati->data.begin(), ati->data.begin()+num_vis);
ati_vis.data->assign(ati.data->begin(), ati.data->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 = new std::vector<float>; ati_vis->data.assign(demod_output, demod_output + num_vis);
ati_vis.data->assign(demod_output, demod_output + num_vis);
} }
visOutQueue->push(ati_vis); visOutQueue->push(ati_vis);
@ -136,7 +129,7 @@ void DemodulatorThread::threadMain() {
} }
} }
delete inp.data; delete inp;
} }
if (resampler != NULL) { if (resampler != NULL) {
@ -156,6 +149,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,7 +6,7 @@
#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

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) { 0), audio_resampler(NULL), audio_resample_ratio(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) { 0), inputRate(0), bandwidth(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

@ -4,7 +4,7 @@
#include "CubicSDR.h" #include "CubicSDR.h"
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() {
@ -39,7 +39,7 @@ void SDRPostThread::threadMain() {
#ifdef __APPLE__ #ifdef __APPLE__
pthread_t tID = pthread_self(); // ID of this thread pthread_t tID = pthread_self(); // ID of this thread
int priority = sched_get_priority_max( SCHED_FIFO) - 1; int priority = sched_get_priority_max( SCHED_FIFO) - 1;
sched_param prio = {priority}; // scheduling priority of thread sched_param prio = { priority }; // scheduling priority of thread
pthread_setschedparam(tID, SCHED_FIFO, &prio); pthread_setschedparam(tID, SCHED_FIFO, &prio);
#endif #endif
@ -50,25 +50,25 @@ void SDRPostThread::threadMain() {
std::cout << "SDR post-processing thread started.." << std::endl; std::cout << "SDR post-processing thread started.." << std::endl;
while (!terminated) { while (!terminated) {
SDRThreadIQData data_in; SDRThreadIQData *data_in;
iqDataInQueue.load()->pop(data_in); iqDataInQueue.load()->pop(data_in);
if (data_in.data && data_in.data->size()) { if (data_in && data_in->data.size()) {
SDRThreadIQData dataOut; SDRThreadIQData *dataOut = new SDRThreadIQData;
dataOut.frequency = data_in.frequency; dataOut->frequency = data_in->frequency;
dataOut.bandwidth = data_in.bandwidth; dataOut->bandwidth = data_in->bandwidth;
dataOut.data = data_in.data; dataOut->data.assign(data_in->data.begin(), data_in->data.end());
for (int i = 0, iMax = dataOut.data->size() / 2; i < iMax; i++) { for (int i = 0, iMax = dataOut->data.size() / 2; i < iMax; i++) {
x.real = (float) (*dataOut.data)[i * 2] / 127.0; x.real = (float) dataOut->data[i * 2] / 127.0;
x.imag = (float) (*dataOut.data)[i * 2 + 1] / 127.0; x.imag = (float) dataOut->data[i * 2 + 1] / 127.0;
iirfilt_crcf_execute(dcFilter, x, &y); iirfilt_crcf_execute(dcFilter, x, &y);
(*dataOut.data)[i * 2] = (signed char) floor(y.real * 127.0); dataOut->data[i * 2] = (signed char) floor(y.real * 127.0);
(*dataOut.data)[i * 2 + 1] = (signed char) floor(y.imag * 127.0); dataOut->data[i * 2 + 1] = (signed char) floor(y.imag * 127.0);
} }
if (iqDataOutQueue != NULL) { if (iqDataOutQueue != NULL) {
@ -76,13 +76,11 @@ void SDRPostThread::threadMain() {
} }
if (iqVisualQueue != NULL && iqVisualQueue.load()->empty()) { if (iqVisualQueue != NULL && iqVisualQueue.load()->empty()) {
SDRThreadIQData visualDataOut; SDRThreadIQData *visualDataOut = new SDRThreadIQData;
visualDataOut.data = new std::vector<signed char>; visualDataOut->data.assign(dataOut->data.begin(), dataOut->data.begin() + (FFT_SIZE * 2));
visualDataOut.data->assign(dataOut.data->begin(), dataOut.data->begin() + (FFT_SIZE * 2));
iqVisualQueue.load()->push(visualDataOut); iqVisualQueue.load()->push(visualDataOut);
} }
if (demodulators_add.size()) { if (demodulators_add.size()) {
while (!demodulators_add.empty()) { while (!demodulators_add.empty()) {
demodulators.push_back(demodulators_add.back()); demodulators.push_back(demodulators_add.back());
@ -104,7 +102,6 @@ void SDRPostThread::threadMain() {
int activeDemods = 0; int activeDemods = 0;
bool pushedData = false; bool pushedData = false;
std::atomic<int> *c = new std::atomic<int>;
if (demodulators.size()) { if (demodulators.size()) {
@ -112,37 +109,32 @@ void SDRPostThread::threadMain() {
for (i = demodulators.begin(); i != demodulators.end(); i++) { for (i = demodulators.begin(); i != demodulators.end(); i++) {
DemodulatorInstance *demod = *i; DemodulatorInstance *demod = *i;
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))) { && abs(data_in->frequency - demod->getParams().frequency) > (int) ((float) ((float) SRATE / 2.0))) {
continue; continue;
} }
activeDemods++; activeDemods++;
} }
c->store(activeDemods);
bool demodActive = false;
if (demodulators.size()) { if (demodulators.size()) {
DemodulatorThreadIQData dummyDataOut; DemodulatorThreadIQData *demodDataOut = new DemodulatorThreadIQData;
dummyDataOut.frequency = data_in.frequency; demodDataOut->frequency = data_in->frequency;
dummyDataOut.bandwidth = data_in.bandwidth; demodDataOut->bandwidth = data_in->bandwidth;
dummyDataOut.data = NULL; demodDataOut->setRefCount(activeDemods);
DemodulatorThreadIQData demodDataOut; demodDataOut->data.assign(dataOut->data.begin(), dataOut->data.begin() + dataOut->data.size());
demodDataOut.frequency = data_in.frequency;
demodDataOut.bandwidth = data_in.bandwidth;
demodDataOut.setRefCount(c);
demodDataOut.data = data_in.data;
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))) { && 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);
} }
} else if (!demod->isActive()) { } else if (!demod->isActive()) {
@ -156,22 +148,23 @@ void SDRPostThread::threadMain() {
demodQueue->push(demodDataOut); demodQueue->push(demodDataOut);
pushedData = true; pushedData = true;
} }
if (!pushedData) {
delete demodDataOut;
}
} }
} }
delete dataOut;
if (!pushedData) { }
delete dataOut.data; if (data_in) {
delete c; delete data_in;
}
} }
} }
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

@ -13,7 +13,7 @@ public:
void setIQDataInQueue(SDRThreadIQDataQueue* iqDataQueue); void setIQDataInQueue(SDRThreadIQDataQueue* iqDataQueue);
void setIQDataOutQueue(SDRThreadIQDataQueue* iqDataQueue); void setIQDataOutQueue(SDRThreadIQDataQueue* iqDataQueue);
void setIQVisualQueue(SDRThreadIQDataQueue *iqVisQueue); void setIQVisualQueue(SDRThreadIQDataQueue* iqVisQueue);
void threadMain(); void threadMain();
void terminate(); void terminate();
@ -21,9 +21,9 @@ public:
protected: protected:
uint32_t sample_rate; uint32_t sample_rate;
std::atomic<SDRThreadIQDataQueue*> iqDataOutQueue; std::atomic<SDRThreadIQDataQueue *> iqDataOutQueue;
std::atomic<SDRThreadIQDataQueue*> iqDataInQueue; std::atomic<SDRThreadIQDataQueue *> iqDataInQueue;
std::atomic<SDRThreadIQDataQueue*> iqVisualQueue; std::atomic<SDRThreadIQDataQueue *> iqVisualQueue;
std::vector<DemodulatorInstance *> demodulators; std::vector<DemodulatorInstance *> demodulators;
std::vector<DemodulatorInstance *> demodulators_add; std::vector<DemodulatorInstance *> demodulators_add;

View File

@ -145,6 +145,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,20 +158,17 @@ 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 = new std::vector<signed char>; SDRThreadIQData *dataOut = new SDRThreadIQData;
dataOut->frequency = frequency;
dataOut->bandwidth = bandwidth;
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.push_back(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);
} }

View File

@ -38,7 +38,7 @@ class SDRThreadIQData {
public: public:
unsigned int frequency; unsigned int frequency;
unsigned int bandwidth; unsigned int bandwidth;
std::vector<signed char> *data; std::vector<signed char> data;
SDRThreadIQData() : SDRThreadIQData() :
frequency(0), bandwidth(0), data(NULL) { frequency(0), bandwidth(0), data(NULL) {
@ -46,7 +46,7 @@ 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

@ -30,8 +30,7 @@ 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(WF_DRAG_NONE), nextDragState(WF_DRAG_NONE), shiftDown(false), altDown(false), ctrlDown(false) {
false), altDown(false), ctrlDown(false), activeDemodulatorBandwidth(0), activeDemodulatorFrequency(0) {
int in_block_size = FFT_SIZE; int in_block_size = FFT_SIZE;
int out_block_size = FFT_SIZE; int out_block_size = FFT_SIZE;