2014-11-22 22:33:32 -05:00
|
|
|
#include <DemodulatorMgr.h>
|
2014-12-10 00:34:27 -05:00
|
|
|
#include <sstream>
|
|
|
|
#include <algorithm>
|
2014-11-22 22:33:32 -05:00
|
|
|
|
|
|
|
DemodulatorInstance::DemodulatorInstance() :
|
2014-11-30 17:11:29 -05:00
|
|
|
t_Demod(NULL), t_Audio(NULL), threadQueueDemod(NULL), demodulatorThread(NULL) {
|
2014-11-26 21:05:19 -05:00
|
|
|
|
|
|
|
threadQueueDemod = new DemodulatorThreadInputQueue;
|
|
|
|
threadQueueCommand = new DemodulatorThreadCommandQueue;
|
|
|
|
demodulatorThread = new DemodulatorThread(threadQueueDemod);
|
|
|
|
demodulatorThread->setCommandQueue(threadQueueCommand);
|
2014-11-30 17:11:29 -05:00
|
|
|
audioInputQueue = new AudioThreadInputQueue;
|
|
|
|
audioThread = new AudioThread(audioInputQueue);
|
|
|
|
demodulatorThread->setAudioInputQueue(audioInputQueue);
|
2014-11-22 22:33:32 -05:00
|
|
|
}
|
2014-11-23 19:39:27 -05:00
|
|
|
|
|
|
|
DemodulatorInstance::~DemodulatorInstance() {
|
2014-11-30 17:11:29 -05:00
|
|
|
|
|
|
|
delete audioThread;
|
|
|
|
delete t_Audio;
|
|
|
|
|
|
|
|
delete audioInputQueue;
|
2014-11-23 19:39:27 -05:00
|
|
|
delete threadQueueDemod;
|
|
|
|
delete demodulatorThread;
|
2014-12-01 19:45:34 -05:00
|
|
|
#ifndef __APPLE__
|
2014-11-23 19:39:27 -05:00
|
|
|
delete t_Demod;
|
2014-12-01 19:45:34 -05:00
|
|
|
#endif
|
2014-11-23 19:39:27 -05:00
|
|
|
}
|
|
|
|
|
2014-11-22 22:33:32 -05:00
|
|
|
void DemodulatorInstance::setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue) {
|
|
|
|
demodulatorThread->setVisualOutputQueue(tQueue);
|
|
|
|
}
|
|
|
|
|
2014-11-26 21:05:19 -05:00
|
|
|
void DemodulatorInstance::run() {
|
|
|
|
if (t_Demod) {
|
2014-11-23 19:39:27 -05:00
|
|
|
terminate();
|
2014-11-30 17:11:29 -05:00
|
|
|
|
2014-11-23 19:39:27 -05:00
|
|
|
delete threadQueueDemod;
|
2014-11-22 22:33:32 -05:00
|
|
|
delete demodulatorThread;
|
2014-11-23 19:39:27 -05:00
|
|
|
delete t_Demod;
|
2014-11-30 17:11:29 -05:00
|
|
|
delete audioThread;
|
|
|
|
delete audioInputQueue;
|
|
|
|
delete t_Audio;
|
2014-11-22 22:33:32 -05:00
|
|
|
|
2014-11-26 21:05:19 -05:00
|
|
|
threadQueueDemod = new DemodulatorThreadInputQueue;
|
|
|
|
threadQueueCommand = new DemodulatorThreadCommandQueue;
|
|
|
|
demodulatorThread = new DemodulatorThread(threadQueueDemod);
|
|
|
|
demodulatorThread->setCommandQueue(threadQueueCommand);
|
2014-11-30 17:11:29 -05:00
|
|
|
|
|
|
|
audioInputQueue = new AudioThreadInputQueue;
|
|
|
|
audioThread = new AudioThread(audioInputQueue);
|
|
|
|
|
|
|
|
demodulatorThread->setAudioInputQueue(audioInputQueue);
|
2014-11-26 21:05:19 -05:00
|
|
|
}
|
2014-11-22 22:33:32 -05:00
|
|
|
|
2014-11-30 17:11:29 -05:00
|
|
|
t_Audio = new std::thread(&AudioThread::threadMain, audioThread);
|
2014-12-01 18:59:07 -05:00
|
|
|
|
|
|
|
#ifdef __APPLE__ // Already using pthreads, might as well do some custom init..
|
2014-12-10 00:34:27 -05:00
|
|
|
pthread_attr_t attr;
|
|
|
|
size_t size;
|
2014-12-01 18:59:07 -05:00
|
|
|
|
2014-12-10 00:34:27 -05:00
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setstacksize(&attr, 2048000);
|
|
|
|
pthread_attr_getstacksize(&attr, &size);
|
2014-12-01 18:59:07 -05:00
|
|
|
pthread_create(&t_Demod, &attr, &DemodulatorThread::pthread_helper, demodulatorThread);
|
2014-12-10 00:34:27 -05:00
|
|
|
pthread_attr_destroy(&attr);
|
2014-12-01 18:59:07 -05:00
|
|
|
|
2014-12-10 00:34:27 -05:00
|
|
|
std::cout << "Initialized demodulator stack size of " << size << std::endl;
|
2014-12-01 18:59:07 -05:00
|
|
|
|
|
|
|
#else
|
2014-11-22 22:33:32 -05:00
|
|
|
t_Demod = new std::thread(&DemodulatorThread::threadMain, demodulatorThread);
|
2014-12-01 18:59:07 -05:00
|
|
|
#endif
|
2014-11-22 22:33:32 -05:00
|
|
|
}
|
|
|
|
|
2014-11-26 21:05:19 -05:00
|
|
|
DemodulatorThreadCommandQueue *DemodulatorInstance::getCommandQueue() {
|
|
|
|
return threadQueueCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
DemodulatorThreadParameters &DemodulatorInstance::getParams() {
|
|
|
|
return demodulatorThread->getParams();
|
|
|
|
}
|
|
|
|
|
2014-11-23 19:39:27 -05:00
|
|
|
void DemodulatorInstance::terminate() {
|
2014-11-30 17:11:29 -05:00
|
|
|
std::cout << "Terminating demodulator thread.." << std::endl;
|
2014-11-23 19:39:27 -05:00
|
|
|
demodulatorThread->terminate();
|
2014-12-01 18:59:07 -05:00
|
|
|
#ifdef __APPLE__
|
|
|
|
pthread_join(t_Demod,NULL);
|
|
|
|
#else
|
2014-11-23 19:39:27 -05:00
|
|
|
t_Demod->join();
|
2014-12-01 18:59:07 -05:00
|
|
|
#endif
|
2014-11-30 17:11:29 -05:00
|
|
|
std::cout << "Terminating demodulator audio thread.." << std::endl;
|
|
|
|
audioThread->terminate();
|
|
|
|
t_Audio->join();
|
2014-11-23 19:39:27 -05:00
|
|
|
}
|
|
|
|
|
2014-12-10 00:34:27 -05:00
|
|
|
std::string DemodulatorInstance::getLabel() {
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DemodulatorInstance::setLabel(std::string labelStr) {
|
|
|
|
label = labelStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DemodulatorMgr::DemodulatorMgr() :
|
|
|
|
activeDemodulator(NULL), lastActiveDemodulator(NULL) {
|
2014-11-22 22:33:32 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
DemodulatorMgr::~DemodulatorMgr() {
|
2014-11-23 19:39:27 -05:00
|
|
|
terminateAll();
|
2014-11-22 22:33:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
DemodulatorInstance *DemodulatorMgr::newThread() {
|
|
|
|
DemodulatorInstance *newDemod = new DemodulatorInstance;
|
2014-12-10 00:34:27 -05:00
|
|
|
|
2014-11-22 22:33:32 -05:00
|
|
|
demods.push_back(newDemod);
|
2014-12-10 00:34:27 -05:00
|
|
|
|
|
|
|
std::stringstream label;
|
|
|
|
label << demods.size();
|
|
|
|
newDemod->setLabel(label.str());
|
|
|
|
|
2014-11-22 22:33:32 -05:00
|
|
|
return newDemod;
|
|
|
|
}
|
2014-11-23 19:39:27 -05:00
|
|
|
|
|
|
|
void DemodulatorMgr::terminateAll() {
|
|
|
|
while (demods.size()) {
|
|
|
|
DemodulatorInstance *d = demods.back();
|
|
|
|
demods.pop_back();
|
|
|
|
d->terminate();
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
}
|
2014-11-26 21:05:19 -05:00
|
|
|
|
|
|
|
std::vector<DemodulatorInstance *> &DemodulatorMgr::getDemodulators() {
|
|
|
|
return demods;
|
|
|
|
}
|
2014-11-30 18:54: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;
|
|
|
|
}
|
2014-12-10 00:34:27 -05:00
|
|
|
|
|
|
|
void DemodulatorMgr::setActiveDemodulator(DemodulatorInstance *demod, bool temporary) {
|
|
|
|
if (!temporary) {
|
|
|
|
if (activeDemodulator != NULL) {
|
|
|
|
lastActiveDemodulator = activeDemodulator;
|
|
|
|
} else {
|
|
|
|
lastActiveDemodulator = demod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
activeDemodulator = demod;
|
|
|
|
}
|
|
|
|
|
|
|
|
DemodulatorInstance *DemodulatorMgr::getActiveDemodulator() {
|
|
|
|
return activeDemodulator;
|
|
|
|
}
|
|
|
|
|
|
|
|
DemodulatorInstance *DemodulatorMgr::getLastActiveDemodulator() {
|
|
|
|
if (std::find(demods.begin(), demods.end(), lastActiveDemodulator) == demods.end()) {
|
|
|
|
lastActiveDemodulator = activeDemodulator;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastActiveDemodulator;
|
|
|
|
}
|