From a7913e32908409c537eb4aac6f45a4e58d881b19 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Tue, 23 Dec 2014 01:59:03 -0500 Subject: [PATCH] refactor remaining transaction objects --- src/AppFrame.cpp | 25 ++++---- src/audio/AudioThread.cpp | 104 +++++++++++++++++++++----------- src/audio/AudioThread.h | 8 +-- src/demod/DemodulatorThread.cpp | 17 +++--- src/demod/DemodulatorThread.h | 2 +- src/sdr/SDRPostThread.cpp | 54 ++++++++--------- src/sdr/SDRPostThread.h | 8 +-- src/sdr/SDRThread.cpp | 11 ++-- src/sdr/SDRThread.h | 6 +- 9 files changed, 130 insertions(+), 105 deletions(-) diff --git a/src/AppFrame.cpp b/src/AppFrame.cpp index f71556a..50a088c 100644 --- a/src/AppFrame.cpp +++ b/src/AppFrame.cpp @@ -96,14 +96,14 @@ void AppFrame::OnIdle(wxIdleEvent& event) { // std::this_thread::yield(); //#endif if (!wxGetApp().getIQVisualQueue()->empty()) { - SDRThreadIQData iqData; + SDRThreadIQData *iqData; wxGetApp().getIQVisualQueue()->pop(iqData); - if (iqData.data && iqData.data->size()) { - spectrumCanvas->setData(iqData.data); - waterfallCanvas->setData(iqData.data); + if (iqData && iqData->data.size()) { + spectrumCanvas->setData(&iqData->data); + waterfallCanvas->setData(&iqData->data); - delete iqData.data; + delete iqData; } else { std::cout << "Incoming IQ data empty?" << std::endl; } @@ -111,20 +111,19 @@ void AppFrame::OnIdle(wxIdleEvent& event) { } if (!wxGetApp().getAudioVisualQueue()->empty()) { - AudioThreadInput demodAudioData; + AudioThreadInput *demodAudioData; wxGetApp().getAudioVisualQueue()->pop(demodAudioData); - if (demodAudioData.data && demodAudioData.data->size()) { - - if (scopeCanvas->waveform_points.size() != demodAudioData.data->size()*2) { - scopeCanvas->waveform_points.resize(demodAudioData.data->size()*2); + if (demodAudioData && demodAudioData->data.size()) { + if (scopeCanvas->waveform_points.size() != demodAudioData->data.size()*2) { + scopeCanvas->waveform_points.resize(demodAudioData->data.size()*2); } - for (int i = 0, iMax = demodAudioData.data->size(); i < iMax; i++) { - scopeCanvas->waveform_points[i * 2 + 1] = (*demodAudioData.data)[i] * 0.5f; + 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] = ((double) i / (double) iMax); } - delete demodAudioData.data; + delete demodAudioData; } else { std::cout << "Incoming Demodulator data empty?" << std::endl; } diff --git a/src/audio/AudioThread.cpp b/src/audio/AudioThread.cpp index 706ce1c..1c55bc9 100644 --- a/src/audio/AudioThread.cpp +++ b/src/audio/AudioThread.cpp @@ -64,44 +64,65 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu 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->currentInput.data) { - delete srcmix->currentInput.data; + if (srcmix->currentInput) { + delete srcmix->currentInput; + srcmix->currentInput = NULL; + } + if (srcmix->terminated) { + continue; } srcmix->inputQueue->pop(srcmix->currentInput); 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++) { - if (srcmix->audio_queue_ptr >= srcmix->currentInput.data->size()) { - if (srcmix->currentInput.data) { - delete srcmix->currentInput.data; + if (srcmix->audio_queue_ptr >= srcmix->currentInput->data.size()) { + if (srcmix->currentInput) { + delete srcmix->currentInput; + srcmix->currentInput = NULL; + } + if (srcmix->terminated) { + continue; } srcmix->inputQueue->pop(srcmix->currentInput); srcmix->audio_queue_ptr = 0; } - if (srcmix->currentInput.data && srcmix->currentInput.data->size()) { - 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 + 1] += v; } srcmix->audio_queue_ptr++; } } else { - for (int i = 0, iMax = src->currentInput.channels * nBufferFrames; i < iMax; i++) { - if (srcmix->audio_queue_ptr >= srcmix->currentInput.data->size()) { - if (srcmix->currentInput.data) { - delete srcmix->currentInput.data; + for (int i = 0, iMax = src->currentInput->channels * nBufferFrames; i < iMax; i++) { + if (srcmix->audio_queue_ptr >= srcmix->currentInput->data.size()) { + if (srcmix->currentInput) { + delete srcmix->currentInput; + srcmix->currentInput = NULL; + } + if (srcmix->terminated) { + continue; } srcmix->inputQueue->pop(srcmix->currentInput); srcmix->audio_queue_ptr = 0; } - if (srcmix->currentInput.data && srcmix->currentInput.data->size()) { - 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++; } @@ -122,11 +143,21 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu if (status) { 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->currentInput.data) { - delete src->currentInput.data; + if (src->currentInput) { + delete src->currentInput; + src->currentInput = NULL; + } + if (src->terminated) { + return 1; } src->inputQueue->pop(src->currentInput); src->audio_queue_ptr = 0; @@ -134,34 +165,35 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu return 0; } - if (src->currentInput.channels == 1) { + if (src->currentInput->channels == 1) { for (int i = 0; i < nBufferFrames; i++) { - if (src->audio_queue_ptr >= src->currentInput.data->size()) { - if (src->currentInput.data) { - delete src->currentInput.data; + if (src->audio_queue_ptr >= src->currentInput->data.size()) { + if (src->currentInput) { + delete src->currentInput; + src->currentInput = NULL; } if (src->terminated) { - break; + return 1; } src->inputQueue->pop(src->currentInput); src->audio_queue_ptr = 0; } - if (src->currentInput.data && src->currentInput.data->size()) { - 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++; } } else { - for (int i = 0, iMax = src->currentInput.channels * nBufferFrames; i < iMax; i++) { - if (src->audio_queue_ptr >= src->currentInput.data->size()) { + for (int i = 0, iMax = src->currentInput->channels * nBufferFrames; i < iMax; i++) { + if (src->audio_queue_ptr >= src->currentInput->data.size()) { if (src->terminated) { - break; + return 1; } src->inputQueue->pop(src->currentInput); src->audio_queue_ptr = 0; } - if (src->currentInput.data && src->currentInput.data->size()) { - 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++; } @@ -321,12 +353,12 @@ bool AudioThread::isActive() { void AudioThread::setActive(bool state) { #ifdef __APPLE__ - AudioThreadInput dummy; + AudioThreadInput *dummy; if (state && !active) { while (!inputQueue->empty()) { // flush queue inputQueue->pop(dummy); - if (dummy.data) { - delete dummy.data; + if (dummy) { + delete dummy; } } deviceController[parameters.deviceId]->bindThread(this); @@ -334,8 +366,8 @@ void AudioThread::setActive(bool state) { deviceController[parameters.deviceId]->removeThread(this); while (!inputQueue->empty()) { // flush queue inputQueue->pop(dummy); - if (dummy.data) { - delete dummy.data; + if (dummy) { + delete dummy; } } } diff --git a/src/audio/AudioThread.h b/src/audio/AudioThread.h index f2446e4..3082f21 100644 --- a/src/audio/AudioThread.h +++ b/src/audio/AudioThread.h @@ -24,7 +24,7 @@ public: int sampleRate; 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 *data; + std::vector data; }; class AudioThreadCommand { @@ -49,13 +49,13 @@ public: int int_value; }; -typedef ThreadQueue AudioThreadInputQueue; +typedef ThreadQueue AudioThreadInputQueue; typedef ThreadQueue AudioThreadCommandQueue; class AudioThread { public: - AudioThreadInput currentInput; + AudioThreadInput *currentInput; AudioThreadInputQueue *inputQueue; std::atomic audio_queue_ptr; std::atomic underflow_count; diff --git a/src/demod/DemodulatorThread.cpp b/src/demod/DemodulatorThread.cpp index e66ec78..20826fd 100644 --- a/src/demod/DemodulatorThread.cpp +++ b/src/demod/DemodulatorThread.cpp @@ -76,10 +76,9 @@ void DemodulatorThread::threadMain() { unsigned int num_audio_written; msresamp_rrrf_execute(audio_resampler, demod_output, num_written, resampled_audio_output, &num_audio_written); - AudioThreadInput ati; - ati.channels = 1; - ati.data = new std::vector; - ati.data->assign(resampled_audio_output,resampled_audio_output+num_audio_written); + AudioThreadInput *ati = new AudioThreadInput; + ati->channels = 1; + ati->data.assign(resampled_audio_output,resampled_audio_output+num_audio_written); if (audioInputQueue != NULL) { if (!squelch_enabled || ((agc_crcf_get_signal_level(agc)) >= 0.1)) { @@ -88,22 +87,20 @@ void DemodulatorThread::threadMain() { } if (visOutQueue != NULL && visOutQueue->empty()) { - AudioThreadInput ati_vis; - ati_vis.channels = ati.channels; + AudioThreadInput *ati_vis = new AudioThreadInput; + ati_vis->channels = ati->channels; int num_vis = DEMOD_VIS_SIZE; if (num_audio_written > num_written) { if (num_vis > num_audio_written) { num_vis = num_audio_written; } - ati_vis.data = new std::vector; - 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 { if (num_vis > num_written) { num_vis = num_written; } - ati_vis.data = new std::vector; - ati_vis.data->assign(demod_output, demod_output + num_vis); + ati_vis->data.assign(demod_output, demod_output + num_vis); } visOutQueue->push(ati_vis); diff --git a/src/demod/DemodulatorThread.h b/src/demod/DemodulatorThread.h index 5327620..925580d 100644 --- a/src/demod/DemodulatorThread.h +++ b/src/demod/DemodulatorThread.h @@ -6,7 +6,7 @@ #include "DemodDefs.h" #include "AudioThread.h" -typedef ThreadQueue DemodulatorThreadOutputQueue; +typedef ThreadQueue DemodulatorThreadOutputQueue; #define DEMOD_VIS_SIZE 2048 diff --git a/src/sdr/SDRPostThread.cpp b/src/sdr/SDRPostThread.cpp index 6f2b84c..70062a8 100644 --- a/src/sdr/SDRPostThread.cpp +++ b/src/sdr/SDRPostThread.cpp @@ -50,25 +50,25 @@ void SDRPostThread::threadMain() { std::cout << "SDR post-processing thread started.." << std::endl; while (!terminated) { - SDRThreadIQData data_in; + SDRThreadIQData *data_in; iqDataInQueue.load()->pop(data_in); - if (data_in.data && data_in.data->size()) { - SDRThreadIQData dataOut; + if (data_in && data_in->data.size()) { + SDRThreadIQData *dataOut = new SDRThreadIQData; - dataOut.frequency = data_in.frequency; - dataOut.bandwidth = data_in.bandwidth; - dataOut.data = data_in.data; + dataOut->frequency = data_in->frequency; + dataOut->bandwidth = data_in->bandwidth; + dataOut->data.assign(data_in->data.begin(), data_in->data.end()); - 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; + 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); + dataOut->data[i * 2] = (signed char) floor(y.real * 127.0); + dataOut->data[i * 2 + 1] = (signed char) floor(y.imag * 127.0); } if (iqDataOutQueue != NULL) { @@ -76,9 +76,8 @@ void SDRPostThread::threadMain() { } if (iqVisualQueue != NULL && iqVisualQueue.load()->empty()) { - SDRThreadIQData visualDataOut; - visualDataOut.data = new std::vector; - visualDataOut.data->assign(dataOut.data->begin(), dataOut.data->begin() + (FFT_SIZE * 2)); + SDRThreadIQData *visualDataOut = new SDRThreadIQData; + visualDataOut->data.assign(dataOut->data.begin(), dataOut->data.begin() + (FFT_SIZE * 2)); iqVisualQueue.load()->push(visualDataOut); } @@ -110,34 +109,32 @@ void SDRPostThread::threadMain() { 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))) { + if (demod->getParams().frequency != data_in->frequency + && abs(data_in->frequency - demod->getParams().frequency) > (int) ((float) ((float) SRATE / 2.0))) { continue; } activeDemods++; } - bool demodActive = false; - if (demodulators.size()) { DemodulatorThreadIQData *demodDataOut = new DemodulatorThreadIQData; - demodDataOut->frequency = data_in.frequency; - demodDataOut->bandwidth = data_in.bandwidth; + demodDataOut->frequency = data_in->frequency; + demodDataOut->bandwidth = data_in->bandwidth; demodDataOut->setRefCount(activeDemods); - demodDataOut->data.assign(dataOut.data->begin(), dataOut.data->begin() + dataOut.data->size()); + demodDataOut->data.assign(dataOut->data.begin(), dataOut->data.begin() + dataOut->data.size()); std::vector::iterator i; for (i = demodulators.begin(); i != demodulators.end(); i++) { DemodulatorInstance *demod = *i; DemodulatorThreadInputQueue *demodQueue = demod->threadQueueDemod; - if (demod->getParams().frequency != data_in.frequency - && abs(data_in.frequency - demod->getParams().frequency) > (int) ((float) ((float) SRATE / 2.0))) { + if (demod->getParams().frequency != data_in->frequency + && abs(data_in->frequency - demod->getParams().frequency) > (int) ((float) ((float) SRATE / 2.0))) { if (demod->isActive()) { demod->setActive(false); DemodulatorThreadIQData *dummyDataOut = new DemodulatorThreadIQData; - dummyDataOut->frequency = data_in.frequency; - dummyDataOut->bandwidth = data_in.bandwidth; + dummyDataOut->frequency = data_in->frequency; + dummyDataOut->bandwidth = data_in->bandwidth; demodQueue->push(dummyDataOut); } } else if (!demod->isActive()) { @@ -157,7 +154,10 @@ void SDRPostThread::threadMain() { } } } - delete dataOut.data; + delete dataOut; + } + if (data_in) { + delete data_in; } } std::cout << "SDR post-processing thread done." << std::endl; @@ -165,6 +165,6 @@ void SDRPostThread::threadMain() { void SDRPostThread::terminate() { terminated = true; - SDRThreadIQData dummy; + SDRThreadIQData *dummy = new SDRThreadIQData; iqDataInQueue.load()->push(dummy); } diff --git a/src/sdr/SDRPostThread.h b/src/sdr/SDRPostThread.h index 928cf07..6846aa1 100644 --- a/src/sdr/SDRPostThread.h +++ b/src/sdr/SDRPostThread.h @@ -13,7 +13,7 @@ public: void setIQDataInQueue(SDRThreadIQDataQueue* iqDataQueue); void setIQDataOutQueue(SDRThreadIQDataQueue* iqDataQueue); - void setIQVisualQueue(SDRThreadIQDataQueue *iqVisQueue); + void setIQVisualQueue(SDRThreadIQDataQueue* iqVisQueue); void threadMain(); void terminate(); @@ -21,9 +21,9 @@ public: protected: uint32_t sample_rate; - std::atomic iqDataOutQueue; - std::atomic iqDataInQueue; - std::atomic iqVisualQueue; + std::atomic iqDataOutQueue; + std::atomic iqDataInQueue; + std::atomic iqVisualQueue; std::vector demodulators; std::vector demodulators_add; diff --git a/src/sdr/SDRThread.cpp b/src/sdr/SDRThread.cpp index adc10c9..93dddc4 100644 --- a/src/sdr/SDRThread.cpp +++ b/src/sdr/SDRThread.cpp @@ -158,20 +158,17 @@ void SDRThread::threadMain() { rtlsdr_read_sync(dev, buf, BUF_SIZE, &n_read); - std::vector *new_buffer = new std::vector; + SDRThreadIQData *dataOut = new SDRThreadIQData; + dataOut->frequency = frequency; + dataOut->bandwidth = bandwidth; 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; seconds += time_slice; - SDRThreadIQData dataOut; - dataOut.frequency = frequency; - dataOut.bandwidth = bandwidth; - dataOut.data = new_buffer; - if (iqDataOutQueue != NULL) { iqDataOutQueue.load()->push(dataOut); } diff --git a/src/sdr/SDRThread.h b/src/sdr/SDRThread.h index b813689..b917160 100644 --- a/src/sdr/SDRThread.h +++ b/src/sdr/SDRThread.h @@ -38,7 +38,7 @@ class SDRThreadIQData { public: unsigned int frequency; unsigned int bandwidth; - std::vector *data; + std::vector data; SDRThreadIQData() : frequency(0), bandwidth(0), data(NULL) { @@ -46,7 +46,7 @@ public: } SDRThreadIQData(unsigned int bandwidth, unsigned int frequency, std::vector *data) : - frequency(frequency), bandwidth(bandwidth), data(data) { + frequency(frequency), bandwidth(bandwidth) { } @@ -56,7 +56,7 @@ public: }; typedef ThreadQueue SDRThreadCommandQueue; -typedef ThreadQueue SDRThreadIQDataQueue; +typedef ThreadQueue SDRThreadIQDataQueue; class SDRThread { public: