CubicSDR/src/demod/DemodulatorPreThread.cpp

296 lines
10 KiB
C++
Raw Normal View History

2014-11-16 16:51:45 -05:00
#include "CubicSDRDefs.h"
#include <vector>
#ifdef __APPLE__
2014-12-11 19:07:21 -05:00
#include <pthread.h>
#endif
#include "DemodulatorPreThread.h"
2015-01-11 17:08:16 -05:00
#include "CubicSDR.h"
2015-11-20 22:29:04 -05:00
DemodulatorPreThread::DemodulatorPreThread() : IOThread(), iqResampler(NULL), iqResampleRatio(1), cModem(nullptr), cModemKit(nullptr), iqInputQueue(NULL), iqOutputQueue(NULL), threadQueueNotify(NULL), commandQueue(NULL)
{
2015-07-21 00:59:18 -04:00
initialized.store(false);
freqShifter = nco_crcf_create(LIQUID_VCO);
shiftFrequency = 0;
2014-11-30 23:33:55 -05:00
workerQueue = new DemodulatorThreadWorkerCommandQueue;
workerResults = new DemodulatorThreadWorkerResultQueue;
workerThread = new DemodulatorWorkerThread();
workerThread->setInputQueue("WorkerCommandQueue",workerQueue);
workerThread->setOutputQueue("WorkerResultQueue",workerResults);
}
void DemodulatorPreThread::initialize() {
2015-01-11 17:08:16 -05:00
iqResampleRatio = (double) (params.bandwidth) / (double) params.sampleRate;
float As = 60.0f; // stop-band attenuation [dB]
2014-11-16 16:51:45 -05:00
iqResampler = msresamp_crcf_create(iqResampleRatio, As);
2015-11-17 20:47:00 -05:00
initialized.store(true);
lastParams = params;
2014-11-16 16:51:45 -05:00
}
DemodulatorPreThread::~DemodulatorPreThread() {
2014-11-16 16:51:45 -05:00
}
2015-07-29 20:57:02 -04:00
void DemodulatorPreThread::run() {
#ifdef __APPLE__
pthread_t tID = pthread_self(); // ID of this thread
int priority = sched_get_priority_max( SCHED_FIFO) - 1;
sched_param prio = {priority}; // scheduling priority of thread
pthread_setschedparam(tID, SCHED_FIFO, &prio);
#endif
if (!initialized) {
initialize();
}
std::cout << "Demodulator preprocessor thread started.." << std::endl;
2014-12-24 01:28:33 -05:00
2015-07-28 22:14:48 -04:00
ReBuffer<DemodulatorThreadPostIQData> buffers;
2014-12-24 01:28:33 -05:00
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) {
2014-12-22 23:27:52 -05:00
DemodulatorThreadIQData *inp;
iqInputQueue->pop(inp);
2014-11-16 16:51:45 -05:00
2014-11-30 23:33:55 -05:00
bool bandwidthChanged = false;
2015-01-11 17:08:16 -05:00
bool rateChanged = false;
DemodulatorThreadParameters tempParams = params;
2014-11-30 23:33:55 -05:00
if (!commandQueue->empty()) {
while (!commandQueue->empty()) {
DemodulatorThreadCommand command;
commandQueue->pop(command);
switch (command.cmd) {
2014-11-30 23:33:55 -05:00
case DemodulatorThreadCommand::DEMOD_THREAD_CMD_SET_BANDWIDTH:
if (command.llong_value < 1500) {
command.llong_value = 1500;
}
2015-01-11 17:08:16 -05:00
if (command.llong_value > params.sampleRate) {
tempParams.bandwidth = params.sampleRate;
} else {
tempParams.bandwidth = command.llong_value;
}
2014-11-30 23:33:55 -05:00
bandwidthChanged = true;
break;
2014-11-30 23:33:55 -05:00
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;
}
}
2015-01-11 17:08:16 -05:00
}
2015-01-23 02:09:37 -05:00
if (inp->sampleRate != tempParams.sampleRate && inp->sampleRate) {
2015-01-11 17:08:16 -05:00
tempParams.sampleRate = inp->sampleRate;
rateChanged = true;
}
2015-01-11 17:08:16 -05:00
if (bandwidthChanged || rateChanged) {
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;
2014-11-30 23:33:55 -05:00
2015-01-11 17:08:16 -05:00
workerQueue->push(command);
}
if (!initialized) {
inp->decRefCount();
continue;
}
// Requested frequency is not center, shift it into the center!
2015-03-26 22:45:52 -04:00
if ((params.frequency - inp->frequency) != shiftFrequency || rateChanged) {
shiftFrequency = params.frequency - 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)) {
inp->decRefCount();
continue;
}
2014-12-24 01:28:33 -05:00
// std::lock_guard < std::mutex > lock(inp->m_mutex);
2014-12-26 16:15:35 -05:00
std::vector<liquid_float_complex> *data = &inp->data;
2015-01-11 17:08:16 -05:00
if (data->size() && (inp->sampleRate == params.sampleRate)) {
2014-12-26 16:15:35 -05:00
int bufSize = data->size();
if (in_buf_data.size() != bufSize) {
if (in_buf_data.capacity() < bufSize) {
in_buf_data.reserve(bufSize);
out_buf_data.reserve(bufSize);
}
in_buf_data.resize(bufSize);
out_buf_data.resize(bufSize);
}
in_buf_data.assign(inp->data.begin(), inp->data.end());
2014-12-26 16:15:35 -05:00
liquid_float_complex *in_buf = &in_buf_data[0];
liquid_float_complex *out_buf = &out_buf_data[0];
liquid_float_complex *temp_buf = NULL;
if (shiftFrequency != 0) {
if (shiftFrequency < 0) {
nco_crcf_mix_block_up(freqShifter, in_buf, out_buf, bufSize);
} else {
nco_crcf_mix_block_down(freqShifter, in_buf, out_buf, bufSize);
}
temp_buf = in_buf;
in_buf = out_buf;
out_buf = temp_buf;
}
2014-11-16 16:51:45 -05:00
2015-07-28 22:14:48 -04:00
DemodulatorThreadPostIQData *resamp = buffers.getBuffer();
2014-12-24 01:28:33 -05:00
int out_size = ceil((double) (bufSize) * iqResampleRatio) + 512;
if (resampledData.size() != out_size) {
if (resampledData.capacity() < out_size) {
resampledData.reserve(out_size);
}
resampledData.resize(out_size);
}
unsigned int numWritten;
msresamp_crcf_execute(iqResampler, in_buf, bufSize, &resampledData[0], &numWritten);
resamp->setRefCount(1);
resamp->data.assign(resampledData.begin(), resampledData.begin() + numWritten);
resamp->modemType = demodType;
2015-11-17 19:32:47 -05:00
resamp->modem = cModem;
resamp->modemKit = cModemKit;
2015-01-11 17:08:16 -05:00
resamp->sampleRate = params.bandwidth;
2014-11-16 16:51:45 -05:00
iqOutputQueue->push(resamp);
2014-11-16 16:51:45 -05:00
}
inp->decRefCount();
if (!terminated && !workerResults->empty()) {
while (!workerResults->empty()) {
DemodulatorWorkerThreadResult result;
workerResults->pop(result);
switch (result.cmd) {
case DemodulatorWorkerThreadResult::DEMOD_WORKER_THREAD_RESULT_FILTERS:
2015-01-23 02:09:37 -05:00
if (result.iqResampler) {
if (iqResampler) {
msresamp_crcf_destroy(iqResampler);
}
2015-01-23 02:09:37 -05:00
iqResampler = result.iqResampler;
iqResampleRatio = result.iqResampleRatio;
}
2015-11-17 20:47:00 -05:00
if (result.modem != nullptr) {
cModem = result.modem;
}
if (result.modemKit != nullptr) {
cModemKit = result.modemKit;
}
2015-11-17 19:32:47 -05:00
if (result.bandwidth) {
2015-01-23 02:09:37 -05:00
params.bandwidth = result.bandwidth;
}
if (result.sampleRate) {
2015-01-23 02:09:37 -05:00
params.sampleRate = result.sampleRate;
}
if (result.modemType != "") {
demodType = result.modemType;
params.demodType = result.modemType;
demodTypeChanged.store(false);
}
break;
default:
break;
}
}
}
2014-11-16 16:51:45 -05:00
}
2015-07-28 22:14:48 -04:00
buffers.purge();
DemodulatorThreadCommand tCmd(DemodulatorThreadCommand::DEMOD_THREAD_CMD_DEMOD_PREPROCESS_TERMINATED);
2014-12-11 19:07:21 -05:00
tCmd.context = this;
threadQueueNotify->push(tCmd);
std::cout << "Demodulator preprocessor thread done." << std::endl;
2014-11-16 16:51:45 -05:00
}
2015-11-17 20:47:00 -05:00
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.demodType = demodType;
command.bandwidth = params.bandwidth;
command.audioSampleRate = params.audioSampleRate;
workerQueue->push(command);
demodTypeChanged.store(true);
2015-11-17 20:47:00 -05:00
}
std::string DemodulatorPreThread::getDemodType() {
if (demodTypeChanged.load()) {
return newDemodType;
}
2015-11-17 20:47:00 -05:00
return demodType;
}
void DemodulatorPreThread::terminate() {
terminated = true;
DemodulatorThreadIQData *inp = new DemodulatorThreadIQData; // push dummy to nudge queue
iqInputQueue->push(inp);
DemodulatorWorkerThreadCommand command(DemodulatorWorkerThreadCommand::DEMOD_WORKER_THREAD_CMD_NULL);
workerQueue->push(command);
workerThread->terminate();
t_Worker->join();
delete t_Worker;
delete workerThread;
delete workerResults;
delete workerQueue;
}
Modem *DemodulatorPreThread::getModem() {
return cModem;
}
ModemKit *DemodulatorPreThread::getModemKit() {
return cModemKit;
}