Clean up some early prototype garbage..

This commit is contained in:
Charles J. Cliffe 2015-11-22 23:25:45 -05:00
parent c0eca0b2f3
commit 7b301fadc1
9 changed files with 156 additions and 197 deletions

View File

@ -14,9 +14,6 @@ class DemodulatorThreadCommand {
public:
enum DemodulatorThreadCommandEnum {
DEMOD_THREAD_CMD_NULL,
DEMOD_THREAD_CMD_SET_BANDWIDTH,
DEMOD_THREAD_CMD_SET_FREQUENCY,
DEMOD_THREAD_CMD_SET_AUDIO_RATE,
DEMOD_THREAD_CMD_DEMOD_PREPROCESS_TERMINATED,
DEMOD_THREAD_CMD_DEMOD_TERMINATED,
DEMOD_THREAD_CMD_AUDIO_TERMINATED
@ -124,23 +121,3 @@ typedef ThreadQueue<DemodulatorThreadIQData *> DemodulatorThreadInputQueue;
typedef ThreadQueue<DemodulatorThreadPostIQData *> DemodulatorThreadPostInputQueue;
typedef ThreadQueue<DemodulatorThreadCommand> DemodulatorThreadCommandQueue;
typedef ThreadQueue<DemodulatorThreadControlCommand> DemodulatorThreadControlCommandQueue;
class DemodulatorThreadParameters {
public:
long long frequency;
long long sampleRate;
unsigned int bandwidth; // set equal to disable second stage re-sampling?
unsigned int audioSampleRate;
std::string demodType;
DemodulatorThreadParameters() :
frequency(0), sampleRate(DEFAULT_SAMPLE_RATE), bandwidth(200000), audioSampleRate(0),
demodType("FM") {
}
~DemodulatorThreadParameters() {
}
};

View File

@ -12,22 +12,18 @@ DemodulatorInstance::DemodulatorInstance() :
muted.store(false);
tracking.store(false);
follow.store(false);
currentAudioSampleRate.store(0);
currentFrequency.store(0);
currentOutputDevice.store(-1);
currentAudioGain.store(1.0);
label = new std::string("Unnamed");
pipeIQInputData = new DemodulatorThreadInputQueue;
pipeIQDemodData = new DemodulatorThreadPostInputQueue;
pipeDemodCommand = new DemodulatorThreadCommandQueue;
pipeDemodNotify = new DemodulatorThreadCommandQueue;
demodulatorPreThread = new DemodulatorPreThread(this);
demodulatorPreThread->setInputQueue("IQDataInput",pipeIQInputData);
demodulatorPreThread->setOutputQueue("IQDataOutput",pipeIQDemodData);
demodulatorPreThread->setOutputQueue("NotifyQueue",pipeDemodNotify);
demodulatorPreThread->setInputQueue("CommandQueue",pipeDemodCommand);
pipeAudioData = new AudioThreadInputQueue;
threadQueueControl = new DemodulatorThreadControlCommandQueue;
@ -41,8 +37,6 @@ DemodulatorInstance::DemodulatorInstance() :
audioThread = new AudioThread();
audioThread->setInputQueue("AudioDataInput", pipeAudioData);
audioThread->setOutputQueue("NotifyQueue", pipeDemodNotify);
currentDemodType = demodulatorPreThread->getParams().demodType;
}
DemodulatorInstance::~DemodulatorInstance() {
@ -51,7 +45,6 @@ DemodulatorInstance::~DemodulatorInstance() {
delete demodulatorPreThread;
delete pipeIQInputData;
delete pipeIQDemodData;
delete pipeDemodCommand;
delete pipeDemodNotify;
delete threadQueueControl;
delete pipeAudioData;
@ -66,17 +59,8 @@ void DemodulatorInstance::run() {
return;
}
// while (!isTerminated()) {
// std::this_thread::sleep_for(std::chrono::milliseconds(1));
// }
currentFrequency = demodulatorPreThread->getParams().frequency;
currentAudioSampleRate = AudioThread::deviceSampleRate[getOutputDevice()];
demodulatorPreThread->getParams().audioSampleRate = currentAudioSampleRate;
setDemodulatorType(demodulatorPreThread->getParams().demodType);
t_Audio = new std::thread(&AudioThread::threadMain, audioThread);
#ifdef __APPLE__ // Already using pthreads, might as well do some custom init..
pthread_attr_t attr;
size_t size;
@ -112,10 +96,6 @@ void DemodulatorInstance::updateLabel(long long freq) {
setLabel(newLabel.str());
}
DemodulatorThreadCommandQueue *DemodulatorInstance::getCommandQueue() {
return pipeDemodCommand;
}
void DemodulatorInstance::terminate() {
std::cout << "Terminating demodulator audio thread.." << std::endl;
audioThread->terminate();
@ -254,27 +234,14 @@ int DemodulatorInstance::getOutputDevice() {
}
void DemodulatorInstance::setDemodulatorType(std::string demod_type_in) {
currentDemodType = demod_type_in;
if (currentDemodType == "I/Q") {
if (currentAudioSampleRate) {
setBandwidth(currentAudioSampleRate);
} else {
setBandwidth(AudioThread::deviceSampleRate[getOutputDevice()]);
}
}
setGain(getGain());
demodulatorPreThread->getParams().demodType = currentDemodType;
if (!active) {
demodulatorPreThread->setDemodType(currentDemodType);
} else if (demodulatorThread && threadQueueControl) {
demodulatorPreThread->setDemodType(currentDemodType);
if (demodulatorPreThread) {
demodulatorPreThread->setDemodType(demod_type_in);
}
}
std::string DemodulatorInstance::getDemodulatorType() {
return currentDemodType;
return demodulatorPreThread->getDemodType();
}
void DemodulatorInstance::setDemodulatorLock(bool demod_lock_in) {
@ -310,65 +277,34 @@ int DemodulatorInstance::getDemodulatorCons() {
}
void DemodulatorInstance::setBandwidth(int bw) {
if (!active && demodulatorPreThread != NULL) {
demodulatorPreThread->getParams().bandwidth = bw;
} else if (demodulatorPreThread && pipeDemodCommand) {
DemodulatorThreadCommand command;
command.cmd = DemodulatorThreadCommand::DEMOD_THREAD_CMD_SET_BANDWIDTH;
command.llong_value = bw;
pipeDemodCommand->push(command);
}
demodulatorPreThread->setBandwidth(bw);
}
int DemodulatorInstance::getBandwidth() {
return demodulatorPreThread->getParams().bandwidth;
return demodulatorPreThread->getBandwidth();
}
void DemodulatorInstance::setFrequency(long long freq) {
if ((freq - getBandwidth() / 2) <= 0) {
freq = getBandwidth() / 2;
}
if (!active) {
currentFrequency = freq;
demodulatorPreThread->getParams().frequency = currentFrequency;
} else if (demodulatorPreThread && pipeDemodCommand) {
DemodulatorThreadCommand command;
command.cmd = DemodulatorThreadCommand::DEMOD_THREAD_CMD_SET_FREQUENCY;
currentFrequency = freq;
command.llong_value = freq;
pipeDemodCommand->push(command);
}
demodulatorPreThread->setFrequency(freq);
}
long long DemodulatorInstance::getFrequency() {
if (!currentFrequency) {
currentFrequency = demodulatorPreThread->getParams().frequency;
}
return currentFrequency;
return demodulatorPreThread->getFrequency();
}
void DemodulatorInstance::setAudioSampleRate(int sampleRate) {
if (terminated) {
currentAudioSampleRate = sampleRate;
demodulatorPreThread->getParams().audioSampleRate = sampleRate;
} else if (demodulatorPreThread && pipeDemodCommand) {
DemodulatorThreadCommand command;
command.cmd = DemodulatorThreadCommand::DEMOD_THREAD_CMD_SET_AUDIO_RATE;
currentAudioSampleRate = sampleRate;
command.llong_value = sampleRate;
pipeDemodCommand->push(command);
}
if (currentDemodType == "I/Q") {
setBandwidth(currentAudioSampleRate);
}
demodulatorPreThread->setSampleRate(sampleRate);
}
int DemodulatorInstance::getAudioSampleRate() {
currentAudioSampleRate = audioThread->getSampleRate();
demodulatorPreThread->getParams().audioSampleRate = currentAudioSampleRate;
return currentAudioSampleRate;
if (!audioThread) {
return 0;
}
return audioThread->getSampleRate();
}

View File

@ -73,7 +73,7 @@ public:
void setAudioSampleRate(int sampleRate);
int getAudioSampleRate();
bool isFollow();
void setFollow(bool follow);
@ -89,7 +89,6 @@ protected:
DemodulatorThreadInputQueue* pipeIQInputData;
DemodulatorThreadPostInputQueue* pipeIQDemodData;
AudioThreadInputQueue *pipeAudioData;
DemodulatorThreadCommandQueue* pipeDemodCommand;
DemodulatorThreadCommandQueue* pipeDemodNotify;
DemodulatorPreThread *demodulatorPreThread;
DemodulatorThread *demodulatorThread;
@ -106,10 +105,7 @@ private:
std::atomic_bool squelch;
std::atomic_bool muted;
std::atomic_llong currentFrequency;
std::string currentDemodType;
std::atomic_int currentOutputDevice;
std::atomic_int currentAudioSampleRate;
std::atomic<float> currentAudioGain;
std::atomic_bool follow, tracking;
};

View File

@ -9,7 +9,7 @@
#include "CubicSDR.h"
#include "DemodulatorInstance.h"
DemodulatorPreThread::DemodulatorPreThread(DemodulatorInstance *parent) : IOThread(), iqResampler(NULL), iqResampleRatio(1), cModem(nullptr), cModemKit(nullptr), iqInputQueue(NULL), iqOutputQueue(NULL), threadQueueNotify(NULL), commandQueue(NULL)
DemodulatorPreThread::DemodulatorPreThread(DemodulatorInstance *parent) : IOThread(), iqResampler(NULL), iqResampleRatio(1), cModem(nullptr), cModemKit(nullptr), iqInputQueue(NULL), iqOutputQueue(NULL), threadQueueNotify(NULL)
{
initialized.store(false);
this->parent = parent;
@ -23,18 +23,20 @@ DemodulatorPreThread::DemodulatorPreThread(DemodulatorInstance *parent) : IOThre
workerThread = new DemodulatorWorkerThread();
workerThread->setInputQueue("WorkerCommandQueue",workerQueue);
workerThread->setOutputQueue("WorkerResultQueue",workerResults);
newSampleRate = currentSampleRate = 0;
newBandwidth = currentBandwidth = 0;
newAudioSampleRate = currentAudioSampleRate = 0;
newFrequency = currentFrequency = 0;
sampleRateChanged.store(false);
frequencyChanged.store(false);
bandwidthChanged.store(false);
audioSampleRateChanged.store(false);
}
void DemodulatorPreThread::initialize() {
iqResampleRatio = (double) (params.bandwidth) / (double) params.sampleRate;
float As = 60.0f; // stop-band attenuation [dB]
iqResampler = msresamp_crcf_create(iqResampleRatio, As);
initialized.store(true);
lastParams = params;
}
DemodulatorPreThread::~DemodulatorPreThread() {
@ -59,83 +61,86 @@ void DemodulatorPreThread::run() {
iqInputQueue = (DemodulatorThreadInputQueue*)getInputQueue("IQDataInput");
iqOutputQueue = (DemodulatorThreadPostInputQueue*)getOutputQueue("IQDataOutput");
threadQueueNotify = (DemodulatorThreadCommandQueue*)getOutputQueue("NotifyQueue");
commandQueue = ( DemodulatorThreadCommandQueue*)getInputQueue("CommandQueue");
std::vector<liquid_float_complex> in_buf_data;
std::vector<liquid_float_complex> out_buf_data;
setDemodType(params.demodType);
t_Worker = new std::thread(&DemodulatorWorkerThread::threadMain, workerThread);
while (!terminated) {
DemodulatorThreadIQData *inp;
iqInputQueue->pop(inp);
bool bandwidthChanged = false;
bool rateChanged = false;
DemodulatorThreadParameters tempParams = params;
if (!commandQueue->empty()) {
while (!commandQueue->empty()) {
DemodulatorThreadCommand command;
commandQueue->pop(command);
switch (command.cmd) {
case DemodulatorThreadCommand::DEMOD_THREAD_CMD_SET_BANDWIDTH:
if (command.llong_value < 1500) {
command.llong_value = 1500;
}
tempParams.bandwidth = command.llong_value;
bandwidthChanged = true;
break;
case DemodulatorThreadCommand::DEMOD_THREAD_CMD_SET_FREQUENCY:
params.frequency = tempParams.frequency = command.llong_value;
break;
case DemodulatorThreadCommand::DEMOD_THREAD_CMD_SET_AUDIO_RATE:
tempParams.audioSampleRate = (int)command.llong_value;
rateChanged = true;
break;
default:
break;
}
if (frequencyChanged.load()) {
currentFrequency = newFrequency;
frequencyChanged.store(false);
}
if (inp->sampleRate != currentSampleRate) {
newSampleRate = inp->sampleRate;
if (newSampleRate) {
sampleRateChanged.store(true);
}
}
if (inp->sampleRate != tempParams.sampleRate && inp->sampleRate) {
tempParams.sampleRate = inp->sampleRate;
rateChanged = true;
if (!newAudioSampleRate) {
newAudioSampleRate = parent->getAudioSampleRate();
if (newAudioSampleRate) {
audioSampleRateChanged.store(true);
}
} else if (parent->getAudioSampleRate() != newAudioSampleRate) {
int newRate;
if ((newRate = parent->getAudioSampleRate())) {
newAudioSampleRate = parent->getAudioSampleRate();
audioSampleRateChanged.store(true);
}
}
if (bandwidthChanged || rateChanged) {
if (demodTypeChanged.load() && (newSampleRate && newAudioSampleRate && newBandwidth)) {
DemodulatorWorkerThreadCommand command(DemodulatorWorkerThreadCommand::DEMOD_WORKER_THREAD_CMD_MAKE_DEMOD);
command.frequency = newFrequency;
command.sampleRate = newSampleRate;
command.demodType = newDemodType;
command.bandwidth = newBandwidth;
command.audioSampleRate = newAudioSampleRate;
workerQueue->push(command);
demodType = newDemodType;
sampleRateChanged.store(false);
audioSampleRateChanged.store(false);
demodTypeChanged.store(false);
} else if (cModemKit && cModem && (bandwidthChanged.load() || sampleRateChanged.load() || audioSampleRateChanged.load()) && (newSampleRate && newAudioSampleRate && newBandwidth)) {
DemodulatorWorkerThreadCommand command(DemodulatorWorkerThreadCommand::DEMOD_WORKER_THREAD_CMD_BUILD_FILTERS);
command.sampleRate = tempParams.sampleRate;
command.audioSampleRate = tempParams.audioSampleRate;
command.bandwidth = tempParams.bandwidth;
command.frequency = tempParams.frequency;
command.frequency = newFrequency;
command.sampleRate = newSampleRate;
command.bandwidth = newBandwidth;
command.audioSampleRate = newAudioSampleRate;
bandwidthChanged.store(false);
sampleRateChanged.store(false);
audioSampleRateChanged.store(false);
workerQueue->push(command);
}
if (!initialized) {
inp->decRefCount();
continue;
}
// Requested frequency is not center, shift it into the center!
if ((params.frequency - inp->frequency) != shiftFrequency || rateChanged) {
shiftFrequency = params.frequency - inp->frequency;
if ((currentFrequency - inp->frequency) != shiftFrequency) {
shiftFrequency = currentFrequency - inp->frequency;
if (abs(shiftFrequency) <= (int) ((double) (inp->sampleRate / 2) * 1.5)) {
nco_crcf_set_frequency(freqShifter, (2.0 * M_PI) * (((double) abs(shiftFrequency)) / ((double) inp->sampleRate)));
}
}
if (abs(shiftFrequency) > (int) ((double) (inp->sampleRate / 2) * 1.5)) {
if (cModem && cModemKit && abs(shiftFrequency) > (int) ((double) (inp->sampleRate / 2) * 1.5)) {
inp->decRefCount();
continue;
}
// std::lock_guard < std::mutex > lock(inp->m_mutex);
std::vector<liquid_float_complex> *data = &inp->data;
if (data->size() && (inp->sampleRate == params.sampleRate)) {
if (data->size() && (inp->sampleRate == currentSampleRate) && cModem && cModemKit) {
int bufSize = data->size();
if (in_buf_data.size() != bufSize) {
@ -184,7 +189,7 @@ void DemodulatorPreThread::run() {
resamp->modemType = demodType;
resamp->modem = cModem;
resamp->modemKit = cModemKit;
resamp->sampleRate = params.bandwidth;
resamp->sampleRate = currentBandwidth;
iqOutputQueue->push(resamp);
}
@ -198,7 +203,6 @@ void DemodulatorPreThread::run() {
switch (result.cmd) {
case DemodulatorWorkerThreadResult::DEMOD_WORKER_THREAD_RESULT_FILTERS:
if (result.iqResampler) {
if (iqResampler) {
msresamp_crcf_destroy(iqResampler);
@ -213,21 +217,23 @@ void DemodulatorPreThread::run() {
if (result.modemKit != nullptr) {
cModemKit = result.modemKit;
currentAudioSampleRate = cModemKit->audioSampleRate;
}
if (result.bandwidth) {
params.bandwidth = result.bandwidth;
currentBandwidth = result.bandwidth;
}
if (result.sampleRate) {
params.sampleRate = result.sampleRate;
currentSampleRate = result.sampleRate;
}
if (result.modemType != "") {
demodType = result.modemType;
params.demodType = result.modemType;
demodTypeChanged.store(false);
}
shiftFrequency = inp->frequency-1;
break;
default:
break;
@ -244,22 +250,8 @@ void DemodulatorPreThread::run() {
std::cout << "Demodulator preprocessor thread done." << std::endl;
}
DemodulatorThreadParameters &DemodulatorPreThread::getParams() {
return params;
}
void DemodulatorPreThread::setParams(DemodulatorThreadParameters &params_in) {
params = params_in;
}
void DemodulatorPreThread::setDemodType(std::string demodType) {
this->newDemodType = demodType;
DemodulatorWorkerThreadCommand command(DemodulatorWorkerThreadCommand::DEMOD_WORKER_THREAD_CMD_MAKE_DEMOD);
command.sampleRate = params.sampleRate;
command.demodType = demodType;
command.bandwidth = params.bandwidth;
command.audioSampleRate = params.audioSampleRate;
workerQueue->push(command);
newDemodType = demodType;
demodTypeChanged.store(true);
}
@ -270,6 +262,55 @@ std::string DemodulatorPreThread::getDemodType() {
return demodType;
}
void DemodulatorPreThread::setFrequency(long long freq) {
frequencyChanged.store(true);
newFrequency = freq;
}
long long DemodulatorPreThread::getFrequency() {
if (frequencyChanged.load()) {
return newFrequency;
}
return currentFrequency;
}
void DemodulatorPreThread::setSampleRate(long long sampleRate) {
sampleRateChanged.store(true);
newSampleRate = sampleRate;
}
long long DemodulatorPreThread::getSampleRate() {
if (sampleRateChanged.load()) {
return newSampleRate;
}
return currentSampleRate;
}
void DemodulatorPreThread::setBandwidth(int bandwidth) {
bandwidthChanged.store(true);
newBandwidth = bandwidth;
}
int DemodulatorPreThread::getBandwidth() {
// if (bandwidthChanged.load()) {
// return newBandwidth;
// }
return currentBandwidth;
}
void DemodulatorPreThread::setAudioSampleRate(int rate) {
audioSampleRateChanged.store(true);
newAudioSampleRate = rate;
}
int DemodulatorPreThread::getAudioSampleRate() {
if (audioSampleRateChanged.load()) {
return newAudioSampleRate;
}
return currentAudioSampleRate;
}
void DemodulatorPreThread::terminate() {
terminated = true;
DemodulatorThreadIQData *inp = new DemodulatorThreadIQData; // push dummy to nudge queue
@ -284,7 +325,6 @@ void DemodulatorPreThread::terminate() {
delete workerQueue;
}
Modem *DemodulatorPreThread::getModem() {
return cModem;
}

View File

@ -16,13 +16,22 @@ public:
~DemodulatorPreThread();
void run();
DemodulatorThreadParameters &getParams();
void setParams(DemodulatorThreadParameters &params_in);
void setDemodType(std::string demodType);
std::string getDemodType();
void setFrequency(long long sampleRate);
long long getFrequency();
void setSampleRate(long long sampleRate);
long long getSampleRate();
void setBandwidth(int bandwidth);
int getBandwidth();
void setAudioSampleRate(int rate);
int getAudioSampleRate();
void initialize();
void terminate();
@ -37,10 +46,14 @@ protected:
Modem *cModem;
ModemKit *cModemKit;
long long currentSampleRate, newSampleRate;
long long currentFrequency, newFrequency;
int currentBandwidth, newBandwidth;
int currentAudioSampleRate, newAudioSampleRate;
DemodulatorThreadParameters params;
DemodulatorThreadParameters lastParams;
std::atomic_bool sampleRateChanged, frequencyChanged, bandwidthChanged, audioSampleRateChanged;
nco_crcf freqShifter;
int shiftFrequency;
@ -58,5 +71,4 @@ protected:
DemodulatorThreadInputQueue* iqInputQueue;
DemodulatorThreadPostInputQueue* iqOutputQueue;
DemodulatorThreadCommandQueue* threadQueueNotify;
DemodulatorThreadCommandQueue* commandQueue;
};

View File

@ -12,7 +12,7 @@
#include <pthread.h>
#endif
DemodulatorThread::DemodulatorThread(DemodulatorInstance *parent) : IOThread(), audioSampleRate(0), squelchLevel(-100), signalLevel(-100), squelchEnabled(false), cModem(nullptr), cModemKit(nullptr), iqInputQueue(NULL), audioOutputQueue(NULL), audioVisOutputQueue(NULL), threadQueueControl(NULL), threadQueueNotify(NULL) {
DemodulatorThread::DemodulatorThread(DemodulatorInstance *parent) : IOThread(), squelchLevel(-100), signalLevel(-100), squelchEnabled(false), cModem(nullptr), cModemKit(nullptr), iqInputQueue(NULL), audioOutputQueue(NULL), audioVisOutputQueue(NULL), threadQueueControl(NULL), threadQueueNotify(NULL) {
demodInstance = parent;
muted.store(false);
@ -73,8 +73,6 @@ 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) {
@ -127,7 +125,7 @@ void DemodulatorThread::run() {
if (modemAnalog != nullptr) {
ati = outputBuffers.getBuffer();
ati->sampleRate = audioSampleRate;
ati->sampleRate = cModemKit->audioSampleRate;
ati->inputRate = inp->sampleRate;
ati->setRefCount(1);
}
@ -178,7 +176,7 @@ void DemodulatorThread::run() {
}
} else {
for (int i = 0; i < stereoSize / 2; i++) {
ati_vis->inputRate = audioSampleRate;
ati_vis->inputRate = cModemKit->audioSampleRate;
ati_vis->sampleRate = 36000;
ati_vis->data[i] = ati->data[i * 2];
ati_vis->data[i + stereoSize / 2] = ati->data[i * 2 + 1];
@ -189,7 +187,7 @@ void DemodulatorThread::run() {
ati_vis->channels = 1;
std::vector<float> *demodOutData = (modemAnalog != nullptr)?modemAnalog->getDemodOutputData():nullptr;
if ((numAudioWritten > bufSize) || (demodOutData == nullptr)) {
ati_vis->inputRate = audioSampleRate;
ati_vis->inputRate = cModemKit->audioSampleRate;
if (num_vis > numAudioWritten) {
num_vis = numAudioWritten;
}

View File

@ -43,7 +43,6 @@ protected:
ReBuffer<AudioThreadInput> outputBuffers;
std::atomic_bool muted;
int audioSampleRate;
std::atomic<float> squelchLevel;
std::atomic<float> signalLevel;

View File

@ -42,7 +42,9 @@ ModemKit *ModemFMStereo::buildKit(long long sampleRate, int audioSampleRate) {
ModemKitFMStereo *kit = new ModemKitFMStereo;
kit->audioResampleRatio = double(audioSampleRate) / double(sampleRate);
kit->sampleRate = sampleRate;
kit->audioSampleRate = audioSampleRate;
float As = 60.0f; // stop-band attenuation [dB]
kit->audioResampler = msresamp_rrrf_create(kit->audioResampleRatio, As);

View File

@ -29,6 +29,5 @@ public:
void setHoverAlpha(float hoverAlpha);
private:
DemodulatorThreadParameters defaultDemodParams;
float hoverAlpha;
};