From 5f6b492478d5a81a7fcbc74119b97f6c1e46bad7 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Tue, 17 Nov 2015 23:23:23 -0500 Subject: [PATCH] Analog modems now somewhat functional - Still major memory leaks.. :) --- src/demod/DemodulatorInstance.cpp | 8 ++---- src/demod/DemodulatorPreThread.cpp | 9 ++++++- src/demod/DemodulatorThread.cpp | 36 +++++++++++++-------------- src/demod/DemodulatorThread.h | 4 ++- src/demod/DemodulatorWorkerThread.cpp | 10 ++++---- src/modules/modem/ModemAnalog.cpp | 1 + 6 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/demod/DemodulatorInstance.cpp b/src/demod/DemodulatorInstance.cpp index 8dffd44..3d90fa9 100644 --- a/src/demod/DemodulatorInstance.cpp +++ b/src/demod/DemodulatorInstance.cpp @@ -34,7 +34,7 @@ DemodulatorInstance::DemodulatorInstance() : pipeAudioData = new AudioThreadInputQueue; threadQueueControl = new DemodulatorThreadControlCommandQueue; - demodulatorThread = new DemodulatorThread(); + demodulatorThread = new DemodulatorThread(this); demodulatorThread->setInputQueue("IQDataInput",pipeIQDemodData); demodulatorThread->setInputQueue("ControlQueue",threadQueueControl); demodulatorThread->setOutputQueue("NotifyQueue",pipeDemodNotify); @@ -291,11 +291,7 @@ void DemodulatorInstance::setDemodulatorType(std::string demod_type_in) { checkBandwidth(); demodulatorPreThread->getParams().demodType = currentDemodType; } else if (demodulatorThread && threadQueueControl) { - DemodulatorThreadControlCommand command; - command.cmd = DemodulatorThreadControlCommand::DEMOD_THREAD_CMD_CTL_TYPE; - command.demodType = demod_type_in; - checkBandwidth(); - threadQueueControl->push(command); + demodulatorPreThread->setDemodType(currentDemodType); } } diff --git a/src/demod/DemodulatorPreThread.cpp b/src/demod/DemodulatorPreThread.cpp index 21cc402..acd18c9 100644 --- a/src/demod/DemodulatorPreThread.cpp +++ b/src/demod/DemodulatorPreThread.cpp @@ -64,6 +64,8 @@ void DemodulatorPreThread::run() { std::vector in_buf_data; std::vector out_buf_data; + setDemodType(params.demodType); + while (!terminated) { DemodulatorThreadIQData *inp; iqInputQueue->pop(inp); @@ -199,9 +201,11 @@ void DemodulatorPreThread::run() { switch (result.cmd) { case DemodulatorWorkerThreadResult::DEMOD_WORKER_THREAD_RESULT_FILTERS: - msresamp_crcf_destroy(iqResampler); if (result.iqResampler) { + if (iqResampler) { + msresamp_crcf_destroy(iqResampler); + } iqResampler = result.iqResampler; iqResampleRatio = result.iqResampleRatio; } @@ -224,6 +228,7 @@ void DemodulatorPreThread::run() { if (result.modemType != "") { demodType = result.modemType; + params.demodType = result.modemType; demodTypeChanged.store(false); } break; @@ -254,6 +259,8 @@ void DemodulatorPreThread::setDemodType(std::string demodType) { this->newDemodType = demodType; DemodulatorWorkerThreadCommand command(DemodulatorWorkerThreadCommand::DEMOD_WORKER_THREAD_CMD_MAKE_DEMOD); command.demodType = demodType; + command.bandwidth = params.bandwidth; + command.audioSampleRate = params.audioSampleRate; workerQueue->push(command); } diff --git a/src/demod/DemodulatorThread.cpp b/src/demod/DemodulatorThread.cpp index 000c52f..df7e89f 100644 --- a/src/demod/DemodulatorThread.cpp +++ b/src/demod/DemodulatorThread.cpp @@ -1,5 +1,6 @@ #include "CubicSDRDefs.h" #include "DemodulatorThread.h" +#include "DemodulatorInstance.h" #include #include @@ -11,8 +12,9 @@ #include #endif -DemodulatorThread::DemodulatorThread() : IOThread(), iqAutoGain(NULL), amOutputCeil(1), amOutputCeilMA(1), amOutputCeilMAA(1), audioSampleRate(0), squelchLevel(0), signalLevel(0), squelchEnabled(false), iqInputQueue(NULL), audioOutputQueue(NULL), audioVisOutputQueue(NULL), threadQueueControl(NULL), threadQueueNotify(NULL) { +DemodulatorThread::DemodulatorThread(DemodulatorInstance *parent) : IOThread(), iqAutoGain(NULL), amOutputCeil(1), amOutputCeilMA(1), amOutputCeilMAA(1), audioSampleRate(0), squelchLevel(0), signalLevel(0), squelchEnabled(false), iqInputQueue(NULL), audioOutputQueue(NULL), audioVisOutputQueue(NULL), threadQueueControl(NULL), threadQueueNotify(NULL), cModem(nullptr), cModemKit(nullptr) { + demodInstance = parent; muted.store(false); agcEnabled.store(false); @@ -122,6 +124,8 @@ void DemodulatorThread::run() { iqInputQueue->pop(inp); // std::lock_guard < std::mutex > lock(inp->m_mutex); + audioSampleRate = demodInstance->getAudioSampleRate(); + int bufSize = inp->data.size(); if (!bufSize) { @@ -131,7 +135,9 @@ void DemodulatorThread::run() { if (inp->modemKit && inp->modemKit != cModemKit) { - cModem->disposeKit(cModemKit); + if (cModemKit != nullptr) { + cModem->disposeKit(cModemKit); + } cModemKit = inp->modemKit; } @@ -174,6 +180,9 @@ void DemodulatorThread::run() { modemData.sampleRate = inp->sampleRate; modemData.data.assign(inputData->begin(), inputData->end()); + modemData.setRefCount(1); + + AudioThreadInput *ati = NULL; ati = outputBuffers.getBuffer(); @@ -506,11 +515,7 @@ void DemodulatorThread::run() { ati->peak = p; } } - } else if (ati) { - ati->decRefCount(); } - } else if (ati) { - ati->decRefCount(); } if (ati && audioVisOutputQueue != NULL && audioVisOutputQueue->empty()) { @@ -545,18 +550,18 @@ void DemodulatorThread::run() { } else { int numAudioWritten = ati->data.size(); ati_vis->channels = 1; - if (numAudioWritten > bufSize) { +// if (numAudioWritten > bufSize) { ati_vis->inputRate = audioSampleRate; if (num_vis > numAudioWritten) { num_vis = numAudioWritten; } ati_vis->data.assign(ati->data.begin(), ati->data.begin() + num_vis); - } else { - if (num_vis > bufSize) { - num_vis = bufSize; - } - ati_vis->data.assign(ati->data.begin(), ati->data.begin() + num_vis); - } +// } else { +// if (num_vis > bufSize) { +// num_vis = bufSize; +// } +// ati_vis->data.assign(ati->data.begin(), ati->data.begin() + num_vis); +// } // std::cout << "Signal: " << agc_crcf_get_signal_level(agc) << " -- " << agc_crcf_get_rssi(agc) << "dB " << std::endl; } @@ -574,8 +579,6 @@ void DemodulatorThread::run() { } if (!threadQueueControl->empty()) { -// int newDemodType = DEMOD_TYPE_NULL; - while (!threadQueueControl->empty()) { DemodulatorThreadControlCommand command; threadQueueControl->pop(command); @@ -587,9 +590,6 @@ void DemodulatorThread::run() { case DemodulatorThreadControlCommand::DEMOD_THREAD_CMD_CTL_SQUELCH_OFF: squelchEnabled = false; break; -// case DemodulatorThreadControlCommand::DEMOD_THREAD_CMD_CTL_TYPE: -// newDemodType = command.demodType; -// break; default: break; } diff --git a/src/demod/DemodulatorThread.h b/src/demod/DemodulatorThread.h index 6a0a526..cc08ac7 100644 --- a/src/demod/DemodulatorThread.h +++ b/src/demod/DemodulatorThread.h @@ -10,11 +10,12 @@ typedef ThreadQueue DemodulatorThreadOutputQueue; #define DEMOD_VIS_SIZE 2048 +class DemodulatorInstance; class DemodulatorThread : public IOThread { public: - DemodulatorThread(); + DemodulatorThread(DemodulatorInstance *parent); ~DemodulatorThread(); void onBindOutput(std::string name, ThreadQueueBase *threadQueue); @@ -45,6 +46,7 @@ public: //#endif protected: + DemodulatorInstance *demodInstance; ReBuffer outputBuffers; std::vector agcData; diff --git a/src/demod/DemodulatorWorkerThread.cpp b/src/demod/DemodulatorWorkerThread.cpp index edbc6c3..5eb7e93 100644 --- a/src/demod/DemodulatorWorkerThread.cpp +++ b/src/demod/DemodulatorWorkerThread.cpp @@ -3,7 +3,7 @@ #include DemodulatorWorkerThread::DemodulatorWorkerThread() : IOThread(), - commandQueue(NULL), resultQueue(NULL) { + commandQueue(NULL), resultQueue(NULL), cModem(nullptr), cModemKit(nullptr) { } DemodulatorWorkerThread::~DemodulatorWorkerThread() { @@ -41,7 +41,7 @@ void DemodulatorWorkerThread::run() { } - if (filterChanged && !terminated) { + if ((makeDemod || filterChanged) && !terminated) { DemodulatorWorkerThreadResult result(DemodulatorWorkerThreadResult::DEMOD_WORKER_THREAD_RESULT_FILTERS); float As = 60.0f; // stop-band attenuation [dB] @@ -55,14 +55,14 @@ void DemodulatorWorkerThread::run() { cModem = Modem::makeModem(filterCommand.demodType); cModemType = filterCommand.demodType; } - + result.modem = cModem; + if (filterCommand.bandwidth && filterCommand.audioSampleRate) { - result.modem = cModem; if (cModem != nullptr) { cModemKit = cModem->buildKit(filterCommand.bandwidth, filterCommand.audioSampleRate); } - result.modemKit = cModemKit; } + result.modemKit = cModemKit; if (filterCommand.bandwidth) { result.bandwidth = filterCommand.bandwidth; diff --git a/src/modules/modem/ModemAnalog.cpp b/src/modules/modem/ModemAnalog.cpp index 183a700..d50994d 100644 --- a/src/modules/modem/ModemAnalog.cpp +++ b/src/modules/modem/ModemAnalog.cpp @@ -74,5 +74,6 @@ void ModemAnalog::buildAudioOutput(ModemKitAnalog *akit, AudioThreadInput *audio msresamp_rrrf_execute(akit->audioResampler, &demodOutputData[0], demodOutputData.size(), &resampledOutputData[0], &numAudioWritten); audioOut->channels = 1; + audioOut->sampleRate = akit->audioSampleRate; audioOut->data.assign(resampledOutputData.begin(), resampledOutputData.begin() + numAudioWritten); }