CubicSDR/src/demod/DemodulatorMgr.cpp

143 lines
3.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) {
2014-11-22 22:33:32 -05:00
}
DemodulatorMgr::~DemodulatorMgr() {
terminateAll();
2014-11-22 22:33:32 -05:00
}
DemodulatorInstance *DemodulatorMgr::newThread() {
DemodulatorInstance *newDemod = new DemodulatorInstance;
demods.push_back(newDemod);
std::stringstream label;
label << demods.size();
newDemod->setLabel(label.str());
return newDemod;
2014-11-22 22:33:32 -05:00
}
void DemodulatorMgr::terminateAll() {
while (demods.size()) {
DemodulatorInstance *d = demods.back();
deleteThread(d);
}
}
std::vector<DemodulatorInstance *> &DemodulatorMgr::getDemodulators() {
return demods;
}
2014-12-10 21:22:13 -05:00
void DemodulatorMgr::deleteThread(DemodulatorInstance *demod) {
std::vector<DemodulatorInstance *>::iterator i;
2014-12-10 21:22:13 -05:00
i = std::find(demods.begin(), demods.end(), demod);
2014-12-10 21:22:13 -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
if (i != demods.end()) {
demods.erase(i);
demod->terminate();
}
2014-12-11 19:07:21 -05:00
demods_deleted.push_back(demod);
2014-12-11 19:07:21 -05:00
garbageCollect();
2014-12-10 21:22:13 -05:00
}
std::vector<DemodulatorInstance *> *DemodulatorMgr::getDemodulatorsAt(int freq, int bandwidth) {
std::vector<DemodulatorInstance *> *foundDemods = new std::vector<DemodulatorInstance *>();
for (int i = 0, iMax = demods.size(); i < iMax; i++) {
DemodulatorInstance *testDemod = demods[i];
int freqTest = testDemod->getParams().frequency;
int bandwidthTest = testDemod->getParams().bandwidth;
int halfBandwidthTest = bandwidthTest / 2;
int halfBuffer = bandwidth / 2;
if ((freq <= (freqTest + halfBandwidthTest + halfBuffer)) && (freq >= (freqTest - halfBandwidthTest - halfBuffer))) {
foundDemods->push_back(testDemod);
}
}
return foundDemods;
}
void DemodulatorMgr::setActiveDemodulator(DemodulatorInstance *demod, bool temporary) {
if (!temporary) {
if (activeDemodulator != NULL) {
lastActiveDemodulator = activeDemodulator;
} else {
lastActiveDemodulator = demod;
}
}
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() {
return activeDemodulator;
}
DemodulatorInstance *DemodulatorMgr::getLastActiveDemodulator() {
if (std::find(demods.begin(), demods.end(), lastActiveDemodulator) == demods.end()) {
lastActiveDemodulator = activeDemodulator;
}
return lastActiveDemodulator;
}
2014-12-11 19:07:21 -05:00
void DemodulatorMgr::garbageCollect() {
if (demods_deleted.size()) {
std::vector<DemodulatorInstance *>::iterator i;
2014-12-11 19:07:21 -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
std::cout << "Garbage collected demodulator instance " << deleted->getLabel() << std::endl;
2014-12-11 19:07:21 -05:00
delete deleted;
return;
}
}
}
2014-12-11 19:07:21 -05:00
}