Code cleanup, bugfix and optimisation

This commit is contained in:
AlexandreRouma 2022-06-21 19:54:03 +02:00
parent f7c566f652
commit b943e6e869
13 changed files with 78 additions and 85 deletions

View File

@ -31,8 +31,8 @@ namespace dsp::demod {
_bandwidth = bandwidth; _bandwidth = bandwidth;
_samplerate = samplerate; _samplerate = samplerate;
carrierAgc.init(NULL, 1.0, agcAttack, agcDecay, 10e6, 10.0); carrierAgc.init(NULL, 1.0, agcAttack, agcDecay, 10e6, 10.0, INFINITY);
audioAgc.init(NULL, 1.0, agcAttack, agcDecay, 10e6, 10.0); audioAgc.init(NULL, 1.0, agcAttack, agcDecay, 10e6, 10.0, INFINITY);
dcBlock.init(NULL, dcBlockRate); dcBlock.init(NULL, dcBlockRate);
lpfTaps = taps::lowPass(bandwidth / 2.0, (bandwidth / 2.0) * 0.1, samplerate); lpfTaps = taps::lowPass(bandwidth / 2.0, (bandwidth / 2.0) * 0.1, samplerate);
lpf.init(NULL, lpfTaps); lpf.init(NULL, lpfTaps);

View File

@ -16,9 +16,10 @@ namespace dsp::demod {
void init(stream<complex_t>* in, double tone, double agcAttack, double agcDecay, double samplerate) { void init(stream<complex_t>* in, double tone, double agcAttack, double agcDecay, double samplerate) {
_tone = tone; _tone = tone;
_samplerate = samplerate;
xlator.init(NULL, tone, samplerate); xlator.init(NULL, tone, samplerate);
agc.init(NULL, 1.0, agcAttack, agcDecay, 10e6, 10.0); agc.init(NULL, 1.0, agcAttack, agcDecay, 10e6, 10.0, INFINITY);
if constexpr (std::is_same_v<T, float>) { if constexpr (std::is_same_v<T, float>) {
agc.out.free(); agc.out.free();
@ -31,7 +32,7 @@ namespace dsp::demod {
assert(base_type::_block_init); assert(base_type::_block_init);
std::lock_guard<std::recursive_mutex> lck(base_type::ctrlMtx); std::lock_guard<std::recursive_mutex> lck(base_type::ctrlMtx);
_tone = tone; _tone = tone;
xlator.setOffset(_tone); xlator.setOffset(_tone, _samplerate);
} }
void setAGCAttack(double attack) { void setAGCAttack(double attack) {
@ -49,7 +50,8 @@ namespace dsp::demod {
void setSamplerate(double samplerate) { void setSamplerate(double samplerate) {
assert(base_type::_block_init); assert(base_type::_block_init);
std::lock_guard<std::recursive_mutex> lck(base_type::ctrlMtx); std::lock_guard<std::recursive_mutex> lck(base_type::ctrlMtx);
xlator.setOffset(_tone, samplerate); _samplerate = samplerate;
xlator.setOffset(_tone, _samplerate);
} }
inline int process(int count, const complex_t* in, T* out) { inline int process(int count, const complex_t* in, T* out) {
@ -79,6 +81,7 @@ namespace dsp::demod {
private: private:
double _tone; double _tone;
double _samplerate;
dsp::channel::FrequencyXlator xlator; dsp::channel::FrequencyXlator xlator;
dsp::loop::AGC<float> agc; dsp::loop::AGC<float> agc;

View File

@ -26,7 +26,7 @@ namespace dsp::demod {
_samplerate = samplerate; _samplerate = samplerate;
xlator.init(NULL, getTranslation(), _samplerate); xlator.init(NULL, getTranslation(), _samplerate);
agc.init(NULL, 1.0, agcAttack, agcDecay, 10e6, 10.0); agc.init(NULL, 1.0, agcAttack, agcDecay, 10e6, 10.0, INFINITY);
if constexpr (std::is_same_v<T, float>) { if constexpr (std::is_same_v<T, float>) {
agc.out.free(); agc.out.free();

View File

@ -24,7 +24,7 @@ namespace demod {
class Demodulator { class Demodulator {
public: public:
virtual ~Demodulator() {} virtual ~Demodulator() {}
virtual void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) = 0; virtual void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) = 0;
virtual void start() = 0; virtual void start() = 0;
virtual void stop() = 0; virtual void stop() = 0;
virtual void showMenu() = 0; virtual void showMenu() = 0;

View File

@ -7,13 +7,13 @@ namespace demod {
public: public:
AM() {} AM() {}
AM(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { AM(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
init(name, config, input, bandwidth, outputChangeHandler, afbwChangeHandler, audioSR); init(name, config, input, bandwidth, audioSR);
} }
~AM() { stop(); } ~AM() { stop(); }
void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
this->name = name; this->name = name;
_config = config; _config = config;

View File

@ -7,15 +7,15 @@ namespace demod {
public: public:
CW() {} CW() {}
CW(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { CW(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
init(name, config, input, bandwidth, outputChangeHandler, afbwChangeHandler, audioSR); init(name, config, input, bandwidth, audioSR);
} }
~CW() { ~CW() {
stop(); stop();
} }
void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
this->name = name; this->name = name;
this->_config = config; this->_config = config;
this->afbwChangeHandler = afbwChangeHandler; this->afbwChangeHandler = afbwChangeHandler;

View File

@ -7,15 +7,15 @@ namespace demod {
public: public:
DSB() {} DSB() {}
DSB(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { DSB(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
init(name, config, input, bandwidth, outputChangeHandler, afbwChangeHandler, audioSR); init(name, config, input, bandwidth, audioSR);
} }
~DSB() { ~DSB() {
stop(); stop();
} }
void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
this->name = name; this->name = name;
_config = config; _config = config;

View File

@ -7,15 +7,15 @@ namespace demod {
public: public:
LSB() {} LSB() {}
LSB(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { LSB(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
init(name, config, input, bandwidth, outputChangeHandler, afbwChangeHandler, audioSR); init(name, config, input, bandwidth, audioSR);
} }
~LSB() { ~LSB() {
stop(); stop();
} }
void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
this->name = name; this->name = name;
_config = config; _config = config;

View File

@ -7,13 +7,13 @@ namespace demod {
public: public:
NFM() {} NFM() {}
NFM(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { NFM(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
init(name, config, input, bandwidth, outputChangeHandler, afbwChangeHandler, audioSR); init(name, config, input, bandwidth, audioSR);
} }
~NFM() { stop(); } ~NFM() { stop(); }
void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
this->name = name; this->name = name;
// Define structure // Define structure

View File

@ -7,15 +7,15 @@ namespace demod {
public: public:
RAW() {} RAW() {}
RAW(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { RAW(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
init(name, config, input, bandwidth, outputChangeHandler, afbwChangeHandler, audioSR); init(name, config, input, bandwidth, audioSR);
} }
~RAW() { ~RAW() {
stop(); stop();
} }
void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
this->name = name; this->name = name;
audioSampleRate = audioSR; audioSampleRate = audioSR;

View File

@ -8,15 +8,15 @@ namespace demod {
public: public:
USB() {} USB() {}
USB(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { USB(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
init(name, config, input, bandwidth, outputChangeHandler, afbwChangeHandler, audioSR); init(name, config, input, bandwidth, audioSR);
} }
~USB() { ~USB() {
stop(); stop();
} }
void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
this->name = name; this->name = name;
_config = config; _config = config;

View File

@ -7,15 +7,15 @@ namespace demod {
public: public:
WFM() {} WFM() {}
WFM(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { WFM(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
init(name, config, input, bandwidth, outputChangeHandler, afbwChangeHandler, audioSR); init(name, config, input, bandwidth, audioSR);
} }
~WFM() { ~WFM() {
stop(); stop();
} }
void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, EventHandler<dsp::stream<dsp::stereo_t>*> outputChangeHandler, EventHandler<float> afbwChangeHandler, double audioSR) { void init(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
this->name = name; this->name = name;
this->outputChangeHandler = outputChangeHandler; this->outputChangeHandler = outputChangeHandler;
_config = config; _config = config;

View File

@ -57,17 +57,6 @@ public:
selectedDemodID = config.conf[name]["selectedDemodId"]; selectedDemodID = config.conf[name]["selectedDemodId"];
config.release(created); config.release(created);
// Create demodulator instances
demods.fill(NULL);
demods[RADIO_DEMOD_WFM] = new demod::WFM();
demods[RADIO_DEMOD_NFM] = new demod::NFM();
demods[RADIO_DEMOD_AM] = new demod::AM();
demods[RADIO_DEMOD_USB] = new demod::USB();
demods[RADIO_DEMOD_LSB] = new demod::LSB();
demods[RADIO_DEMOD_DSB] = new demod::DSB();
demods[RADIO_DEMOD_CW] = new demod::CW();
demods[RADIO_DEMOD_RAW] = new demod::RAW();
// Initialize the VFO // Initialize the VFO
vfo = sigpath::vfoManager.createVFO(name, ImGui::WaterfallVFO::REF_CENTER, 0, 200000, 200000, 50000, 200000, false); vfo = sigpath::vfoManager.createVFO(name, ImGui::WaterfallVFO::REF_CENTER, 0, 200000, 200000, 50000, 200000, false);
onUserChangedBandwidthHandler.handler = vfoUserChangedBandwidthHandler; onUserChangedBandwidthHandler.handler = vfoUserChangedBandwidthHandler;
@ -85,30 +74,6 @@ public:
ifChain.addBlock(&squelch, false); ifChain.addBlock(&squelch, false);
ifChain.addBlock(&fmnr, false); ifChain.addBlock(&fmnr, false);
// Load configuration for and enabled all demodulators
EventHandler<dsp::stream<dsp::stereo_t>*> _demodOutputChangeHandler;
EventHandler<float> _demodAfbwChangedHandler;
_demodOutputChangeHandler.handler = demodOutputChangeHandler;
_demodOutputChangeHandler.ctx = this;
_demodAfbwChangedHandler.handler = demodAfbwChangedHandler;
_demodAfbwChangedHandler.ctx = this;
for (auto& demod : demods) {
if (!demod) { continue; }
// Default config
double bw = demod->getDefaultBandwidth();
if (!config.conf[name].contains(demod->getName())) {
config.conf[name][demod->getName()]["bandwidth"] = bw;
config.conf[name][demod->getName()]["snapInterval"] = demod->getDefaultSnapInterval();
config.conf[name][demod->getName()]["squelchLevel"] = MIN_SQUELCH;
config.conf[name][demod->getName()]["squelchEnabled"] = false;
}
bw = std::clamp<double>(bw, demod->getMinBandwidth(), demod->getMaxBandwidth());
// Initialize
demod->init(name, &config, ifChain.out, bw, _demodOutputChangeHandler, _demodAfbwChangedHandler, stream.getSampleRate());
}
// Initialize audio DSP chain // Initialize audio DSP chain
afChain.init(&dummyAudioStream); afChain.init(&dummyAudioStream);
@ -297,8 +262,45 @@ private:
if (!_this->enabled) { style::endDisabled(); } if (!_this->enabled) { style::endDisabled(); }
} }
demod::Demodulator* instantiateDemod(DemodID id) {
demod::Demodulator* demod = NULL;
switch (id) {
case DemodID::RADIO_DEMOD_NFM: demod = new demod::NFM(); break;
case DemodID::RADIO_DEMOD_WFM: demod = new demod::WFM(); break;
case DemodID::RADIO_DEMOD_AM: demod = new demod::AM(); break;
case DemodID::RADIO_DEMOD_DSB: demod = new demod::DSB(); break;
case DemodID::RADIO_DEMOD_USB: demod = new demod::USB(); break;
case DemodID::RADIO_DEMOD_CW: demod = new demod::CW(); break;
case DemodID::RADIO_DEMOD_LSB: demod = new demod::LSB(); break;
case DemodID::RADIO_DEMOD_RAW: demod = new demod::RAW(); break;
default: demod = NULL; break;
}
if (!demod) { return NULL; }
// Default config
double bw = demod->getDefaultBandwidth();
config.acquire();
if (!config.conf[name].contains(demod->getName())) {
config.conf[name][demod->getName()]["bandwidth"] = bw;
config.conf[name][demod->getName()]["snapInterval"] = demod->getDefaultSnapInterval();
config.conf[name][demod->getName()]["squelchLevel"] = MIN_SQUELCH;
config.conf[name][demod->getName()]["squelchEnabled"] = false;
config.release(true);
}
else {
config.release();
}
bw = std::clamp<double>(bw, demod->getMinBandwidth(), demod->getMaxBandwidth());
// Initialize
demod->init(name, &config, ifChain.out, bw, stream.getSampleRate());
return demod;
}
void selectDemodByID(DemodID id) { void selectDemodByID(DemodID id) {
demod::Demodulator* demod = demods[id]; auto startTime = std::chrono::high_resolution_clock::now();
demod::Demodulator* demod = instantiateDemod(id);
if (!demod) { if (!demod) {
spdlog::error("Demodulator {0} not implemented", id); spdlog::error("Demodulator {0} not implemented", id);
return; return;
@ -310,11 +312,16 @@ private:
config.acquire(); config.acquire();
config.conf[name]["selectedDemodId"] = id; config.conf[name]["selectedDemodId"] = id;
config.release(true); config.release(true);
auto endTime = std::chrono::high_resolution_clock::now();
spdlog::warn("Demod switch took {0} us", (std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime)).count());
} }
void selectDemod(demod::Demodulator* demod) { void selectDemod(demod::Demodulator* demod) {
// Stopcurrently selected demodulator and select new // Stopcurrently selected demodulator and select new
if (selectedDemod) { selectedDemod->stop(); } if (selectedDemod) {
selectedDemod->stop();
delete selectedDemod;
}
selectedDemod = demod; selectedDemod = demod;
// Give the demodulator the most recent audio SR // Give the demodulator the most recent audio SR
@ -553,22 +560,6 @@ private:
_this->setAudioSampleRate(sampleRate); _this->setAudioSampleRate(sampleRate);
} }
static void demodOutputChangeHandler(dsp::stream<dsp::stereo_t>* output, void* ctx) {
RadioModule* _this = (RadioModule*)ctx;
_this->afChain.setInput(output, [=](dsp::stream<dsp::stereo_t>* out){ _this->stream.setInput(out); });
}
static void demodAfbwChangedHandler(float output, void* ctx) {
RadioModule* _this = (RadioModule*)ctx;
float audioBW = std::min<float>(_this->selectedDemod->getMaxAFBandwidth(), _this->selectedDemod->getAFBandwidth(_this->bandwidth));
audioBW = std::min<float>(audioBW, _this->audioSampleRate / 2.0);
// _this->win.setCutoff(audioBW);
// _this->win.setTransWidth(audioBW);
// _this->resamp.block.updateWindow(&_this->win);
}
static void ifChainOutputChangeHandler(dsp::stream<dsp::complex_t>* output, void* ctx) { static void ifChainOutputChangeHandler(dsp::stream<dsp::complex_t>* output, void* ctx) {
RadioModule* _this = (RadioModule*)ctx; RadioModule* _this = (RadioModule*)ctx;
if (!_this->selectedDemod) { return; } if (!_this->selectedDemod) { return; }
@ -642,7 +633,6 @@ private:
SinkManager::Stream stream; SinkManager::Stream stream;
std::array<demod::Demodulator*, _RADIO_DEMOD_COUNT> demods;
demod::Demodulator* selectedDemod = NULL; demod::Demodulator* selectedDemod = NULL;
OptionList<std::string, DeemphasisMode> deempModes; OptionList<std::string, DeemphasisMode> deempModes;