CubicSDR/src/demod/DemodulatorMgr.cpp

226 lines
6.3 KiB
C++
Raw Normal View History

2014-11-22 22:33:32 -05:00
#include <DemodulatorMgr.h>
#include <sstream>
#include <algorithm>
2014-12-10 21:22:13 -05:00
#include "CubicSDR.h"
#include <string>
#include <sstream>
2014-11-22 22:33:32 -05:00
DemodulatorMgr::DemodulatorMgr() :
activeDemodulator(NULL), lastActiveDemodulator(NULL), activeVisualDemodulator(NULL), lastBandwidth(DEFAULT_DEMOD_BW), lastDemodType(
DEFAULT_DEMOD_TYPE), lastGain(1.0), lastSquelch(0), lastSquelchEnabled(false), lastStereo(false) {
2014-11-22 22:33:32 -05:00
}
DemodulatorMgr::~DemodulatorMgr() {
2014-12-16 21:30:03 -05:00
terminateAll();
2014-11-22 22:33:32 -05:00
}
DemodulatorInstance *DemodulatorMgr::newThread() {
2014-12-16 21:30:03 -05:00
DemodulatorInstance *newDemod = new DemodulatorInstance;
2014-12-16 21:30:03 -05:00
demods.push_back(newDemod);
2014-12-16 21:30:03 -05:00
std::stringstream label;
label << demods.size();
newDemod->setLabel(label.str());
2014-12-16 21:30:03 -05:00
return newDemod;
2014-11-22 22:33:32 -05:00
}
void DemodulatorMgr::terminateAll() {
2014-12-16 21:30:03 -05:00
while (demods.size()) {
DemodulatorInstance *d = demods.back();
2015-01-10 22:45:39 -05:00
demods.pop_back();
2015-01-10 12:27:03 -05:00
wxGetApp().removeDemodulator(d);
2014-12-16 21:30:03 -05:00
deleteThread(d);
}
}
std::vector<DemodulatorInstance *> &DemodulatorMgr::getDemodulators() {
2014-12-16 21:30:03 -05:00
return demods;
}
2014-12-10 21:22:13 -05:00
void DemodulatorMgr::deleteThread(DemodulatorInstance *demod) {
2014-12-16 21:30:03 -05:00
std::vector<DemodulatorInstance *>::iterator i;
2014-12-10 21:22:13 -05:00
2014-12-16 21:30:03 -05:00
i = std::find(demods.begin(), demods.end(), demod);
2014-12-10 21:22:13 -05:00
2014-12-16 21:30:03 -05:00
if (activeDemodulator == demod) {
activeDemodulator = NULL;
}
if (lastActiveDemodulator == demod) {
lastActiveDemodulator = NULL;
}
if (activeVisualDemodulator == demod) {
activeVisualDemodulator = NULL;
}
2014-12-10 21:22:13 -05:00
2014-12-16 21:30:03 -05:00
if (i != demods.end()) {
demods.erase(i);
}
2015-01-10 12:27:03 -05:00
demod->terminate();
2014-12-11 19:07:21 -05:00
2014-12-16 21:30:03 -05:00
demods_deleted.push_back(demod);
2014-12-11 19:07:21 -05:00
2014-12-16 21:30:03 -05:00
garbageCollect();
2014-12-10 21:22:13 -05:00
}
std::vector<DemodulatorInstance *> *DemodulatorMgr::getDemodulatorsAt(long long freq, int bandwidth) {
2014-12-16 21:30:03 -05:00
std::vector<DemodulatorInstance *> *foundDemods = new std::vector<DemodulatorInstance *>();
2014-12-16 21:30:03 -05:00
for (int i = 0, iMax = demods.size(); i < iMax; i++) {
DemodulatorInstance *testDemod = demods[i];
long long freqTest = testDemod->getFrequency();
long long bandwidthTest = testDemod->getBandwidth();
long long halfBandwidthTest = bandwidthTest / 2;
long long halfBuffer = bandwidth / 2;
if ((freq <= (freqTest + ((testDemod->getDemodulatorType() != DEMOD_TYPE_LSB)?halfBandwidthTest:0) + halfBuffer)) && (freq >= (freqTest - ((testDemod->getDemodulatorType() != DEMOD_TYPE_USB)?halfBandwidthTest:0) - halfBuffer))) {
2014-12-16 21:30:03 -05:00
foundDemods->push_back(testDemod);
}
}
2014-12-16 21:30:03 -05:00
return foundDemods;
}
void DemodulatorMgr::setActiveDemodulator(DemodulatorInstance *demod, bool temporary) {
2014-12-16 21:30:03 -05:00
if (!temporary) {
if (activeDemodulator != NULL) {
lastActiveDemodulator = activeDemodulator;
} else {
lastActiveDemodulator = demod;
}
updateLastState();
2014-12-16 21:30:03 -05:00
}
if (activeVisualDemodulator) {
activeVisualDemodulator->setVisualOutputQueue(NULL);
}
if (demod) {
demod->setVisualOutputQueue(wxGetApp().getAudioVisualQueue());
activeVisualDemodulator = demod;
} else {
DemodulatorInstance *last = getLastActiveDemodulator();
if (last) {
last->setVisualOutputQueue(wxGetApp().getAudioVisualQueue());
}
activeVisualDemodulator = last;
}
activeDemodulator = demod;
garbageCollect();
}
DemodulatorInstance *DemodulatorMgr::getActiveDemodulator() {
if (activeDemodulator && !activeDemodulator->isActive()) {
activeDemodulator = getLastActiveDemodulator();
}
2014-12-16 21:30:03 -05:00
return activeDemodulator;
}
DemodulatorInstance *DemodulatorMgr::getLastActiveDemodulator() {
updateLastState();
2014-12-16 21:30:03 -05:00
return lastActiveDemodulator;
}
2014-12-11 19:07:21 -05:00
void DemodulatorMgr::garbageCollect() {
2014-12-16 21:30:03 -05:00
if (demods_deleted.size()) {
std::vector<DemodulatorInstance *>::iterator i;
2014-12-11 19:07:21 -05:00
2014-12-16 21:30:03 -05:00
for (i = demods_deleted.begin(); i != demods_deleted.end(); i++) {
if ((*i)->isTerminated()) {
DemodulatorInstance *deleted = (*i);
demods_deleted.erase(i);
2014-12-11 19:07:21 -05:00
2014-12-16 21:30:03 -05:00
std::cout << "Garbage collected demodulator instance " << deleted->getLabel() << std::endl;
2014-12-11 19:07:21 -05:00
2014-12-16 21:30:03 -05:00
delete deleted;
return;
}
}
}
2014-12-11 19:07:21 -05:00
}
void DemodulatorMgr::updateLastState() {
if (std::find(demods.begin(), demods.end(), lastActiveDemodulator) == demods.end()) {
if (activeDemodulator && activeDemodulator->isActive()) {
lastActiveDemodulator = activeDemodulator;
} else if (activeDemodulator && !activeDemodulator->isActive()){
activeDemodulator = NULL;
lastActiveDemodulator = NULL;
}
}
if (lastActiveDemodulator && !lastActiveDemodulator->isActive()) {
lastActiveDemodulator = NULL;
}
if (lastActiveDemodulator) {
lastBandwidth = lastActiveDemodulator->getBandwidth();
lastDemodType = lastActiveDemodulator->getDemodulatorType();
lastSquelchEnabled = lastActiveDemodulator->isSquelchEnabled();
lastSquelch = lastActiveDemodulator->getSquelchLevel();
lastGain = lastActiveDemodulator->getGain();
lastStereo = lastActiveDemodulator->isStereo();
}
}
int DemodulatorMgr::getLastBandwidth() const {
return lastBandwidth;
}
void DemodulatorMgr::setLastBandwidth(int lastBandwidth) {
if (lastBandwidth < 1500) {
lastBandwidth = 1500;
} else if (lastBandwidth > wxGetApp().getSampleRate()) {
lastBandwidth = wxGetApp().getSampleRate();
}
this->lastBandwidth = lastBandwidth;
}
int DemodulatorMgr::getLastDemodulatorType() const {
return lastDemodType;
}
void DemodulatorMgr::setLastDemodulatorType(int lastDemodType) {
this->lastDemodType = lastDemodType;
}
float DemodulatorMgr::getLastGain() const {
return lastGain;
}
void DemodulatorMgr::setLastGain(float lastGain) {
this->lastGain = lastGain;
}
float DemodulatorMgr::getLastSquelchLevel() const {
return lastSquelch;
}
void DemodulatorMgr::setLastSquelchLevel(float lastSquelch) {
this->lastSquelch = lastSquelch;
}
bool DemodulatorMgr::isLastSquelchEnabled() const {
return lastSquelchEnabled;
}
void DemodulatorMgr::setLastSquelchEnabled(bool lastSquelchEnabled) {
this->lastSquelchEnabled = lastSquelchEnabled;
}
bool DemodulatorMgr::isLastStereo() const {
return lastStereo;
}
void DemodulatorMgr::setLastStereo(bool lastStereo) {
this->lastStereo = lastStereo;
}