Apply source formatting for demodulator thread

This commit is contained in:
Charles J. Cliffe 2015-11-18 21:13:04 -05:00
parent ea005014ae
commit 10aa9f86dd
2 changed files with 63 additions and 70 deletions

View File

@ -13,13 +13,15 @@
#endif #endif
DemodulatorThread::DemodulatorThread(DemodulatorInstance *parent) : IOThread(), iqAutoGain(NULL), audioSampleRate(0), squelchLevel(0), signalLevel(0), squelchEnabled(false), iqInputQueue(NULL), audioOutputQueue(NULL), audioVisOutputQueue(NULL), threadQueueControl(NULL), threadQueueNotify(NULL), cModem(nullptr), cModemKit(nullptr) { DemodulatorThread::DemodulatorThread(DemodulatorInstance *parent) : IOThread(), iqAutoGain(NULL), audioSampleRate(0), squelchLevel(0), signalLevel(0), squelchEnabled(false), iqInputQueue(NULL), audioOutputQueue(NULL), audioVisOutputQueue(NULL), threadQueueControl(NULL), threadQueueNotify(NULL), cModem(nullptr), cModemKit(nullptr) {
demodInstance = parent; demodInstance = parent;
muted.store(false); muted.store(false);
agcEnabled.store(false); agcEnabled.store(false);
} }
DemodulatorThread::~DemodulatorThread() { DemodulatorThread::~DemodulatorThread() {
} }
void DemodulatorThread::onBindOutput(std::string name, ThreadQueueBase *threadQueue) { void DemodulatorThread::onBindOutput(std::string name, ThreadQueueBase *threadQueue) {
@ -35,36 +37,36 @@ void DemodulatorThread::run() {
sched_param prio = {priority}; // scheduling priority of thread sched_param prio = {priority}; // scheduling priority of thread
pthread_setschedparam(tID, SCHED_FIFO, &prio); pthread_setschedparam(tID, SCHED_FIFO, &prio);
#endif #endif
// Automatic IQ gain // Automatic IQ gain
iqAutoGain = agc_crcf_create(); iqAutoGain = agc_crcf_create();
agc_crcf_set_bandwidth(iqAutoGain, 0.1); agc_crcf_set_bandwidth(iqAutoGain, 0.1);
ReBuffer<AudioThreadInput> audioVisBuffers; ReBuffer<AudioThreadInput> audioVisBuffers;
std::cout << "Demodulator thread started.." << std::endl; std::cout << "Demodulator thread started.." << std::endl;
iqInputQueue = (DemodulatorThreadPostInputQueue*)getInputQueue("IQDataInput"); iqInputQueue = (DemodulatorThreadPostInputQueue*)getInputQueue("IQDataInput");
audioOutputQueue = (AudioThreadInputQueue*)getOutputQueue("AudioDataOutput"); audioOutputQueue = (AudioThreadInputQueue*)getOutputQueue("AudioDataOutput");
threadQueueControl = (DemodulatorThreadControlCommandQueue *)getInputQueue("ControlQueue"); threadQueueControl = (DemodulatorThreadControlCommandQueue *)getInputQueue("ControlQueue");
threadQueueNotify = (DemodulatorThreadCommandQueue*)getOutputQueue("NotifyQueue"); threadQueueNotify = (DemodulatorThreadCommandQueue*)getOutputQueue("NotifyQueue");
ModemIQData modemData; ModemIQData modemData;
while (!terminated) { while (!terminated) {
DemodulatorThreadPostIQData *inp; DemodulatorThreadPostIQData *inp;
iqInputQueue->pop(inp); iqInputQueue->pop(inp);
// std::lock_guard < std::mutex > lock(inp->m_mutex); // std::lock_guard < std::mutex > lock(inp->m_mutex);
audioSampleRate = demodInstance->getAudioSampleRate(); audioSampleRate = demodInstance->getAudioSampleRate();
int bufSize = inp->data.size(); int bufSize = inp->data.size();
if (!bufSize) { if (!bufSize) {
inp->decRefCount(); inp->decRefCount();
continue; continue;
} }
if (inp->modemKit && inp->modemKit != cModemKit) { if (inp->modemKit && inp->modemKit != cModemKit) {
if (cModemKit != nullptr) { if (cModemKit != nullptr) {
cModem->disposeKit(cModemKit); cModem->disposeKit(cModemKit);
@ -90,23 +92,23 @@ void DemodulatorThread::run() {
agcData.resize(bufSize); agcData.resize(bufSize);
agcAMData.resize(bufSize); agcAMData.resize(bufSize);
} }
agc_crcf_execute_block(iqAutoGain, &(inp->data[0]), bufSize, &agcData[0]); agc_crcf_execute_block(iqAutoGain, &(inp->data[0]), bufSize, &agcData[0]);
float currentSignalLevel = 0; float currentSignalLevel = 0;
currentSignalLevel = ((60.0 / fabs(agc_crcf_get_rssi(iqAutoGain))) / 15.0 - signalLevel); currentSignalLevel = ((60.0 / fabs(agc_crcf_get_rssi(iqAutoGain))) / 15.0 - signalLevel);
if (agc_crcf_get_signal_level(iqAutoGain) > currentSignalLevel) { if (agc_crcf_get_signal_level(iqAutoGain) > currentSignalLevel) {
currentSignalLevel = agc_crcf_get_signal_level(iqAutoGain); currentSignalLevel = agc_crcf_get_signal_level(iqAutoGain);
} }
std::vector<liquid_float_complex> *inputData; std::vector<liquid_float_complex> *inputData;
if (agcEnabled) { if (agcEnabled) {
inputData = &agcData; inputData = &agcData;
} else { } else {
inputData = &inp->data; inputData = &inp->data;
} }
modemData.sampleRate = inp->sampleRate; modemData.sampleRate = inp->sampleRate;
@ -119,15 +121,15 @@ void DemodulatorThread::run() {
ati->sampleRate = audioSampleRate; ati->sampleRate = audioSampleRate;
ati->inputRate = inp->sampleRate; ati->inputRate = inp->sampleRate;
ati->setRefCount(1); ati->setRefCount(1);
cModem->demodulate(cModemKit, &modemData, ati); cModem->demodulate(cModemKit, &modemData, ati);
if (currentSignalLevel > signalLevel) { if (currentSignalLevel > signalLevel) {
signalLevel = signalLevel + (currentSignalLevel - signalLevel) * 0.5; signalLevel = signalLevel + (currentSignalLevel - signalLevel) * 0.5;
} else { } else {
signalLevel = signalLevel + (currentSignalLevel - signalLevel) * 0.05; signalLevel = signalLevel + (currentSignalLevel - signalLevel) * 0.05;
} }
if (audioOutputQueue != NULL) { if (audioOutputQueue != NULL) {
if (ati && (!squelchEnabled || (signalLevel >= squelchLevel))) { if (ati && (!squelchEnabled || (signalLevel >= squelchLevel))) {
std::vector<float>::iterator data_i; std::vector<float>::iterator data_i;
@ -140,10 +142,10 @@ void DemodulatorThread::run() {
} }
} }
} }
if (ati && audioVisOutputQueue != NULL && audioVisOutputQueue->empty()) { if (ati && audioVisOutputQueue != NULL && audioVisOutputQueue->empty()) {
AudioThreadInput *ati_vis = audioVisBuffers.getBuffer(); AudioThreadInput *ati_vis = audioVisBuffers.getBuffer();
ati_vis->setRefCount(1); ati_vis->setRefCount(1);
ati_vis->sampleRate = inp->sampleRate; ati_vis->sampleRate = inp->sampleRate;
ati_vis->inputRate = inp->sampleRate; ati_vis->inputRate = inp->sampleRate;
@ -154,9 +156,9 @@ void DemodulatorThread::run() {
if (stereoSize > DEMOD_VIS_SIZE * 2) { if (stereoSize > DEMOD_VIS_SIZE * 2) {
stereoSize = DEMOD_VIS_SIZE * 2; stereoSize = DEMOD_VIS_SIZE * 2;
} }
ati_vis->data.resize(stereoSize); ati_vis->data.resize(stereoSize);
if (inp->modemType == "I/Q") { if (inp->modemType == "I/Q") {
for (int i = 0; i < stereoSize / 2; i++) { for (int i = 0; i < stereoSize / 2; i++) {
ati_vis->data[i] = agcData[i].real * 0.75; ati_vis->data[i] = agcData[i].real * 0.75;
@ -173,25 +175,25 @@ void DemodulatorThread::run() {
} else { } else {
int numAudioWritten = ati->data.size(); int numAudioWritten = ati->data.size();
ati_vis->channels = 1; ati_vis->channels = 1;
// if (numAudioWritten > bufSize) { // if (numAudioWritten > bufSize) {
ati_vis->inputRate = audioSampleRate; ati_vis->inputRate = audioSampleRate;
if (num_vis > numAudioWritten) { if (num_vis > numAudioWritten) {
num_vis = numAudioWritten; num_vis = numAudioWritten;
} }
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 { // } else {
// if (num_vis > bufSize) { // if (num_vis > bufSize) {
// num_vis = bufSize; // num_vis = bufSize;
// } // }
// ati_vis->data.assign(ati->data.begin(), ati->data.begin() + num_vis); // 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; // std::cout << "Signal: " << agc_crcf_get_signal_level(agc) << " -- " << agc_crcf_get_rssi(agc) << "dB " << std::endl;
} }
audioVisOutputQueue->push(ati_vis); audioVisOutputQueue->push(ati_vis);
} }
if (ati != NULL) { if (ati != NULL) {
if (!muted.load()) { if (!muted.load()) {
@ -200,38 +202,37 @@ void DemodulatorThread::run() {
ati->setRefCount(0); ati->setRefCount(0);
} }
} }
if (!threadQueueControl->empty()) { if (!threadQueueControl->empty()) {
while (!threadQueueControl->empty()) { while (!threadQueueControl->empty()) {
DemodulatorThreadControlCommand command; DemodulatorThreadControlCommand command;
threadQueueControl->pop(command); threadQueueControl->pop(command);
switch (command.cmd) { switch (command.cmd) {
case DemodulatorThreadControlCommand::DEMOD_THREAD_CMD_CTL_SQUELCH_ON: case DemodulatorThreadControlCommand::DEMOD_THREAD_CMD_CTL_SQUELCH_ON:
squelchEnabled = true; squelchEnabled = true;
break; break;
case DemodulatorThreadControlCommand::DEMOD_THREAD_CMD_CTL_SQUELCH_OFF: case DemodulatorThreadControlCommand::DEMOD_THREAD_CMD_CTL_SQUELCH_OFF:
squelchEnabled = false; squelchEnabled = false;
break; break;
default: default:
break; break;
} }
} }
} }
inp->decRefCount(); inp->decRefCount();
} }
// end while !terminated // end while !terminated
outputBuffers.purge(); outputBuffers.purge();
if (audioVisOutputQueue && !audioVisOutputQueue->empty()) { if (audioVisOutputQueue && !audioVisOutputQueue->empty()) {
AudioThreadInput *dummy_vis; AudioThreadInput *dummy_vis;
audioVisOutputQueue->pop(dummy_vis); audioVisOutputQueue->pop(dummy_vis);
} }
audioVisBuffers.purge(); audioVisBuffers.purge();
DemodulatorThreadCommand tCmd(DemodulatorThreadCommand::DEMOD_THREAD_CMD_DEMOD_TERMINATED); DemodulatorThreadCommand tCmd(DemodulatorThreadCommand::DEMOD_THREAD_CMD_DEMOD_TERMINATED);
tCmd.context = this; tCmd.context = this;
threadQueueNotify->push(tCmd); threadQueueNotify->push(tCmd);
@ -254,14 +255,13 @@ void DemodulatorThread::setMuted(bool muted) {
} }
void DemodulatorThread::setAGC(bool state) { void DemodulatorThread::setAGC(bool state) {
agcEnabled.store(state); agcEnabled.store(state);
} }
bool DemodulatorThread::getAGC() { bool DemodulatorThread::getAGC() {
return agcEnabled.load(); return agcEnabled.load();
} }
float DemodulatorThread::getSignalLevel() { float DemodulatorThread::getSignalLevel() {
return signalLevel.load(); return signalLevel.load();
} }

View File

@ -33,13 +33,6 @@ public:
void setSquelchLevel(float signal_level_in); void setSquelchLevel(float signal_level_in);
float getSquelchLevel(); float getSquelchLevel();
//
//#ifdef __APPLE__
// static void *pthread_helper(void *context) {
// return ((DemodulatorThread *) context)->threadMain();
// }
//#endif
protected: protected:
DemodulatorInstance *demodInstance; DemodulatorInstance *demodInstance;
ReBuffer<AudioThreadInput> outputBuffers; ReBuffer<AudioThreadInput> outputBuffers;