refactor remaining transaction objects

This commit is contained in:
Charles J. Cliffe 2014-12-23 01:59:03 -05:00
parent 8ce3065bce
commit a7913e3290
9 changed files with 130 additions and 105 deletions

View File

@ -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;
}

View File

@ -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->inputQueue->empty()) {
if (srcmix->currentInput.data) {
delete srcmix->currentInput.data;
if (!srcmix->currentInput) {
if (srcmix->terminated) {
continue;
}
srcmix->inputQueue->pop(srcmix->currentInput);
srcmix->audio_queue_ptr = 0;
}
return 0;
continue;
}
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->currentInput->channels == 0 || !srcmix->currentInput->data.size()) {
if (!srcmix->inputQueue->empty()) {
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;
continue;
}
if (srcmix->currentInput->channels == 1) {
for (int i = 0; i < nBufferFrames; 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 && 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++;
}
@ -123,10 +144,20 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu
std::cout << "Audio buffer underflow.." << (src->underflow_count++) << std::endl;
}
if (src->currentInput.channels == 0 || !src->currentInput.data) {
if (!src->currentInput) {
src->inputQueue->pop(src->currentInput);
src->audio_queue_ptr = 0;
return 0;
}
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;
}
}
}

View File

@ -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<float> *data;
std::vector<float> data;
};
class AudioThreadCommand {
@ -49,13 +49,13 @@ public:
int int_value;
};
typedef ThreadQueue<AudioThreadInput> AudioThreadInputQueue;
typedef ThreadQueue<AudioThreadInput *> AudioThreadInputQueue;
typedef ThreadQueue<AudioThreadCommand> AudioThreadCommandQueue;
class AudioThread {
public:
AudioThreadInput currentInput;
AudioThreadInput *currentInput;
AudioThreadInputQueue *inputQueue;
std::atomic<unsigned int> audio_queue_ptr;
std::atomic<unsigned int> underflow_count;

View File

@ -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<float>;
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<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 {
if (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);

View File

@ -6,7 +6,7 @@
#include "DemodDefs.h"
#include "AudioThread.h"
typedef ThreadQueue<AudioThreadInput> DemodulatorThreadOutputQueue;
typedef ThreadQueue<AudioThreadInput *> DemodulatorThreadOutputQueue;
#define DEMOD_VIS_SIZE 2048

View File

@ -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<signed char>;
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<DemodulatorInstance *>::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);
}

View File

@ -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<SDRThreadIQDataQueue*> iqDataOutQueue;
std::atomic<SDRThreadIQDataQueue*> iqDataInQueue;
std::atomic<SDRThreadIQDataQueue*> iqVisualQueue;
std::atomic<SDRThreadIQDataQueue *> iqDataOutQueue;
std::atomic<SDRThreadIQDataQueue *> iqDataInQueue;
std::atomic<SDRThreadIQDataQueue *> iqVisualQueue;
std::vector<DemodulatorInstance *> demodulators;
std::vector<DemodulatorInstance *> demodulators_add;

View File

@ -158,20 +158,17 @@ void SDRThread::threadMain() {
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++) {
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);
}

View File

@ -38,7 +38,7 @@ class SDRThreadIQData {
public:
unsigned int frequency;
unsigned int bandwidth;
std::vector<signed char> *data;
std::vector<signed char> data;
SDRThreadIQData() :
frequency(0), bandwidth(0), data(NULL) {
@ -46,7 +46,7 @@ public:
}
SDRThreadIQData(unsigned int bandwidth, unsigned int frequency, std::vector<signed char> *data) :
frequency(frequency), bandwidth(bandwidth), data(data) {
frequency(frequency), bandwidth(bandwidth) {
}
@ -56,7 +56,7 @@ public:
};
typedef ThreadQueue<SDRThreadCommand> SDRThreadCommandQueue;
typedef ThreadQueue<SDRThreadIQData> SDRThreadIQDataQueue;
typedef ThreadQueue<SDRThreadIQData *> SDRThreadIQDataQueue;
class SDRThread {
public: