SDRPlusPlus/decoder_modules/radio/src/radio_module.h

700 lines
26 KiB
C
Raw Normal View History

2021-12-03 19:46:09 +01:00
#pragma once
#include <imgui.h>
#include <module.h>
#include <gui/gui.h>
#include <gui/style.h>
#include <signal_path/signal_path.h>
#include <config.h>
2021-12-07 02:13:23 +01:00
#include <dsp/chain.h>
2022-06-26 03:42:22 +02:00
#include <dsp/noise_reduction/noise_blanker.h>
2022-06-15 16:08:28 +02:00
#include <dsp/noise_reduction/fm_if.h>
#include <dsp/noise_reduction/squelch.h>
#include <dsp/multirate/rational_resampler.h>
#include <dsp/filter/deephasis.h>
#include <core.h>
#include <utils/optionlist.h>
#include "radio_interface.h"
#include "demod.h"
2021-12-03 19:46:09 +01:00
ConfigManager config;
#define CONCAT(a, b) ((std::string(a) + b).c_str())
2021-12-03 19:46:09 +01:00
std::map<DeemphasisMode, double> deempTaus = {
{ DEEMP_MODE_22US, 22e-6 },
{ DEEMP_MODE_50US, 50e-6 },
{ DEEMP_MODE_75US, 75e-6 }
2021-12-04 04:49:51 +01:00
};
2021-12-24 21:42:04 +01:00
std::map<IFNRPreset, double> ifnrTaps = {
{ IFNR_PRESET_NOAA_APT, 9},
{ IFNR_PRESET_VOICE, 15 },
{ IFNR_PRESET_NARROW_BAND, 31 },
{ IFNR_PRESET_BROADCAST, 32 }
};
class RadioModule : public ModuleManager::Instance {
2021-12-03 19:46:09 +01:00
public:
RadioModule(std::string name) {
2021-12-03 19:46:09 +01:00
this->name = name;
// Initialize option lists
deempModes.define("None", DEEMP_MODE_NONE);
deempModes.define("22us", DEEMP_MODE_22US);
deempModes.define("50us", DEEMP_MODE_50US);
deempModes.define("75us", DEEMP_MODE_75US);
2021-12-24 21:42:04 +01:00
ifnrPresets.define("NOAA APT", IFNR_PRESET_NOAA_APT);
ifnrPresets.define("Voice", IFNR_PRESET_VOICE);
ifnrPresets.define("Narrow Band", IFNR_PRESET_NARROW_BAND);
2021-12-03 19:46:09 +01:00
// Initialize the config if it doesn't exist
2021-12-04 19:59:30 +01:00
bool created = false;
2021-12-03 19:46:09 +01:00
config.acquire();
if (!config.conf.contains(name)) {
config.conf[name]["selectedDemodId"] = 1;
2021-12-04 19:59:30 +01:00
created = true;
2021-12-03 19:46:09 +01:00
}
selectedDemodID = config.conf[name]["selectedDemodId"];
2021-12-04 19:59:30 +01:00
config.release(created);
2021-12-03 19:46:09 +01:00
// Initialize the VFO
vfo = sigpath::vfoManager.createVFO(name, ImGui::WaterfallVFO::REF_CENTER, 0, 200000, 200000, 50000, 200000, false);
onUserChangedBandwidthHandler.handler = vfoUserChangedBandwidthHandler;
onUserChangedBandwidthHandler.ctx = this;
vfo->wtfVFO->onUserChangedBandwidth.bindHandler(&onUserChangedBandwidthHandler);
2021-12-07 02:13:23 +01:00
// Initialize IF DSP chain
ifChainOutputChanged.ctx = this;
ifChainOutputChanged.handler = ifChainOutputChangeHandler;
2022-06-15 16:08:28 +02:00
ifChain.init(vfo->output);
2021-12-03 19:46:09 +01:00
2022-06-26 03:42:22 +02:00
nb.init(NULL, 500.0 / 24000.0, 10.0);
2022-06-15 16:08:28 +02:00
fmnr.init(NULL, 32);
squelch.init(NULL, MIN_SQUELCH);
2021-12-07 02:13:23 +01:00
2022-06-26 03:42:22 +02:00
ifChain.addBlock(&nb, false);
2022-06-15 16:08:28 +02:00
ifChain.addBlock(&squelch, false);
ifChain.addBlock(&fmnr, false);
2021-12-07 02:13:23 +01:00
// Initialize audio DSP chain
2022-06-15 16:08:28 +02:00
afChain.init(&dummyAudioStream);
2021-12-03 19:46:09 +01:00
2022-06-15 16:08:28 +02:00
resamp.init(NULL, 250000.0, 48000.0);
deemp.init(NULL, 50e-6, 48000.0);
2021-12-07 02:13:23 +01:00
2022-06-15 16:08:28 +02:00
afChain.addBlock(&resamp, true);
afChain.addBlock(&deemp, false);
2021-12-07 02:13:23 +01:00
// Initialize the sink
srChangeHandler.ctx = this;
srChangeHandler.handler = sampleRateChangeHandler;
2022-06-15 16:08:28 +02:00
stream.init(afChain.out, &srChangeHandler, audioSampleRate);
2021-12-07 02:13:23 +01:00
sigpath::sinkManager.registerStream(name, &stream);
2021-12-03 19:46:09 +01:00
// Select the demodulator
selectDemodByID((DemodID)selectedDemodID);
2021-12-07 02:13:23 +01:00
// Start IF chain
ifChain.start();
// Start AF chain
afChain.start();
2021-12-04 17:46:48 +01:00
// Start stream, the rest was started when selecting the demodulator
2021-12-03 19:46:09 +01:00
stream.start();
// Register the menu
2021-12-04 17:46:48 +01:00
gui::menu.registerEntry(name, menuHandler, this, this);
// Register the module interface
core::modComManager.registerInterface("radio", name, moduleInterfaceHandler, this);
2021-12-03 19:46:09 +01:00
}
~RadioModule() {
2021-12-03 19:46:09 +01:00
gui::menu.removeEntry(name);
2021-12-04 19:59:30 +01:00
stream.stop();
if (enabled) {
2021-12-07 02:13:23 +01:00
disable();
2021-12-04 19:59:30 +01:00
}
sigpath::sinkManager.unregisterStream(name);
2021-12-03 19:46:09 +01:00
}
void postInit() {}
void enable() {
enabled = true;
2021-12-04 17:46:48 +01:00
if (!vfo) {
vfo = sigpath::vfoManager.createVFO(name, ImGui::WaterfallVFO::REF_CENTER, 0, 200000, 200000, 50000, 200000, false);
vfo->wtfVFO->onUserChangedBandwidth.bindHandler(&onUserChangedBandwidthHandler);
2021-12-04 17:46:48 +01:00
}
2022-06-15 16:08:28 +02:00
ifChain.setInput(vfo->output, [=](dsp::stream<dsp::complex_t>* out){ ifChainOutputChangeHandler(out, this); });
ifChain.start();
2021-12-04 17:46:48 +01:00
selectDemodByID((DemodID)selectedDemodID);
afChain.start();
2021-12-03 19:46:09 +01:00
}
void disable() {
enabled = false;
2021-12-07 02:13:23 +01:00
ifChain.stop();
2021-12-04 17:46:48 +01:00
if (selectedDemod) { selectedDemod->stop(); }
2021-12-07 02:13:23 +01:00
afChain.stop();
2021-12-04 17:46:48 +01:00
if (vfo) { sigpath::vfoManager.deleteVFO(vfo); }
vfo = NULL;
2021-12-03 19:46:09 +01:00
}
bool isEnabled() {
return enabled;
}
std::string name;
enum DemodID {
RADIO_DEMOD_NFM,
RADIO_DEMOD_WFM,
RADIO_DEMOD_AM,
RADIO_DEMOD_DSB,
RADIO_DEMOD_USB,
RADIO_DEMOD_CW,
RADIO_DEMOD_LSB,
RADIO_DEMOD_RAW,
_RADIO_DEMOD_COUNT,
};
private:
static void menuHandler(void* ctx) {
RadioModule* _this = (RadioModule*)ctx;
2021-12-03 19:46:09 +01:00
if (!_this->enabled) { style::beginDisabled(); }
float menuWidth = ImGui::GetContentRegionAvail().x;
2021-12-03 19:46:09 +01:00
ImGui::BeginGroup();
ImGui::Columns(4, CONCAT("RadioModeColumns##_", _this->name), false);
if (ImGui::RadioButton(CONCAT("NFM##_", _this->name), _this->selectedDemodID == 0) && _this->selectedDemodID != 0) {
2021-12-03 19:46:09 +01:00
_this->selectDemodByID(RADIO_DEMOD_NFM);
}
if (ImGui::RadioButton(CONCAT("WFM##_", _this->name), _this->selectedDemodID == 1) && _this->selectedDemodID != 1) {
_this->selectDemodByID(RADIO_DEMOD_WFM);
}
ImGui::NextColumn();
if (ImGui::RadioButton(CONCAT("AM##_", _this->name), _this->selectedDemodID == 2) && _this->selectedDemodID != 2) {
_this->selectDemodByID(RADIO_DEMOD_AM);
}
if (ImGui::RadioButton(CONCAT("DSB##_", _this->name), _this->selectedDemodID == 3) && _this->selectedDemodID != 3) {
2021-12-03 19:46:09 +01:00
_this->selectDemodByID(RADIO_DEMOD_DSB);
}
ImGui::NextColumn();
if (ImGui::RadioButton(CONCAT("USB##_", _this->name), _this->selectedDemodID == 4) && _this->selectedDemodID != 4) {
_this->selectDemodByID(RADIO_DEMOD_USB);
}
if (ImGui::RadioButton(CONCAT("CW##_", _this->name), _this->selectedDemodID == 5) && _this->selectedDemodID != 5) {
_this->selectDemodByID(RADIO_DEMOD_CW);
};
ImGui::NextColumn();
if (ImGui::RadioButton(CONCAT("LSB##_", _this->name), _this->selectedDemodID == 6) && _this->selectedDemodID != 6) {
_this->selectDemodByID(RADIO_DEMOD_LSB);
}
if (ImGui::RadioButton(CONCAT("RAW##_", _this->name), _this->selectedDemodID == 7) && _this->selectedDemodID != 7) {
_this->selectDemodByID(RADIO_DEMOD_RAW);
};
ImGui::Columns(1, CONCAT("EndRadioModeColumns##_", _this->name), false);
ImGui::EndGroup();
2021-12-04 04:49:51 +01:00
if (!_this->bandwidthLocked) {
ImGui::LeftLabel("Bandwidth");
ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX());
if (ImGui::InputFloat(("##_radio_bw_" + _this->name).c_str(), &_this->bandwidth, 1, 100, "%.0f")) {
_this->bandwidth = std::clamp<float>(_this->bandwidth, _this->minBandwidth, _this->maxBandwidth);
_this->setBandwidth(_this->bandwidth);
}
}
2021-12-03 19:46:09 +01:00
// VFO snap interval
2021-12-04 04:49:51 +01:00
ImGui::LeftLabel("Snap Interval");
2021-12-03 19:46:09 +01:00
ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX());
2021-12-04 04:49:51 +01:00
if (ImGui::InputInt(("##_radio_snap_" + _this->name).c_str(), &_this->snapInterval, 1, 100)) {
if (_this->snapInterval < 1) { _this->snapInterval = 1; }
_this->vfo->setSnapInterval(_this->snapInterval);
config.acquire();
config.conf[_this->name][_this->selectedDemod->getName()]["snapInterval"] = _this->snapInterval;
config.release(true);
}
// Deemphasis mode
2021-12-04 04:49:51 +01:00
if (_this->deempAllowed) {
ImGui::LeftLabel("De-emphasis");
ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX());
if (ImGui::Combo(("##_radio_wfm_deemp_" + _this->name).c_str(), &_this->deempId, _this->deempModes.txt)) {
_this->setDeemphasisMode(_this->deempModes[_this->deempId]);
2021-12-04 04:49:51 +01:00
}
}
2022-06-26 03:42:22 +02:00
// Noise blanker
if (ImGui::Checkbox(("Noise blanker (W.I.P.)##_radio_nb_ena_" + _this->name).c_str(), &_this->nbEnabled)) {
_this->setNBEnabled(_this->nbEnabled);
}
if (!_this->nbEnabled && _this->enabled) { style::beginDisabled(); }
ImGui::SameLine();
ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX());
if (ImGui::SliderFloat(("##_radio_nb_lvl_" + _this->name).c_str(), &_this->nbLevel, _this->MIN_NB, _this->MAX_NB, "%.3fdB")) {
_this->setNBLevel(_this->nbLevel);
}
if (!_this->nbEnabled && _this->enabled) { style::endDisabled(); }
// Squelch
2021-12-04 04:49:51 +01:00
if (ImGui::Checkbox(("Squelch##_radio_sqelch_ena_" + _this->name).c_str(), &_this->squelchEnabled)) {
_this->setSquelchEnabled(_this->squelchEnabled);
}
if (!_this->squelchEnabled && _this->enabled) { style::beginDisabled(); }
ImGui::SameLine();
ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX());
if (ImGui::SliderFloat(("##_radio_sqelch_lvl_" + _this->name).c_str(), &_this->squelchLevel, _this->MIN_SQUELCH, _this->MAX_SQUELCH, "%.3fdB")) {
_this->setSquelchLevel(_this->squelchLevel);
2021-12-03 19:46:09 +01:00
}
2021-12-04 04:49:51 +01:00
if (!_this->squelchEnabled && _this->enabled) { style::endDisabled(); }
2021-12-08 02:10:41 +01:00
// FM IF Noise Reduction
if (_this->FMIFNRAllowed) {
if (ImGui::Checkbox(("IF Noise Reduction##_radio_fmifnr_ena_" + _this->name).c_str(), &_this->FMIFNREnabled)) {
2021-12-08 02:10:41 +01:00
_this->setFMIFNREnabled(_this->FMIFNREnabled);
}
2021-12-24 21:42:04 +01:00
if (_this->selectedDemodID == RADIO_DEMOD_NFM) {
if (!_this->FMIFNREnabled && _this->enabled) { style::beginDisabled(); }
ImGui::SameLine();
ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX());
if (ImGui::Combo(("##_radio_fmifnr_ena_" + _this->name).c_str(), &_this->fmIFPresetId, _this->ifnrPresets.txt)) {
_this->setIFNRPreset(_this->ifnrPresets[_this->fmIFPresetId]);
}
if (!_this->FMIFNREnabled && _this->enabled) { style::endDisabled(); }
}
2021-12-08 02:10:41 +01:00
}
// Demodulator specific menu
2021-12-04 04:49:51 +01:00
_this->selectedDemod->showMenu();
2021-12-03 19:46:09 +01:00
if (!_this->enabled) { style::endDisabled(); }
}
2022-06-21 19:54:03 +02:00
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;
}
2021-12-03 19:46:09 +01:00
void selectDemodByID(DemodID id) {
2022-06-21 19:54:03 +02:00
auto startTime = std::chrono::high_resolution_clock::now();
demod::Demodulator* demod = instantiateDemod(id);
2021-12-03 19:46:09 +01:00
if (!demod) {
spdlog::error("Demodulator {0} not implemented", id);
return;
}
selectedDemodID = id;
selectDemod(demod);
2021-12-07 02:13:23 +01:00
2021-12-04 19:59:30 +01:00
// Save config
config.acquire();
config.conf[name]["selectedDemodId"] = id;
config.release(true);
2022-06-21 19:54:03 +02:00
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());
2021-12-03 19:46:09 +01:00
}
void selectDemod(demod::Demodulator* demod) {
// Stopcurrently selected demodulator and select new
2022-06-22 05:20:40 +02:00
afChain.setInput(&dummyAudioStream, [=](dsp::stream<dsp::stereo_t>* out){ stream.setInput(out); });
2022-06-21 19:54:03 +02:00
if (selectedDemod) {
selectedDemod->stop();
delete selectedDemod;
}
2021-12-03 19:46:09 +01:00
selectedDemod = demod;
2021-12-04 17:46:48 +01:00
// Give the demodulator the most recent audio SR
selectedDemod->AFSampRateChanged(audioSampleRate);
2021-12-07 02:13:23 +01:00
// Set the demodulator's input
2022-06-15 16:08:28 +02:00
selectedDemod->setInput(ifChain.out);
2021-12-07 02:13:23 +01:00
// Set AF chain's input
2022-06-15 16:08:28 +02:00
afChain.setInput(selectedDemod->getOutput(), [=](dsp::stream<dsp::stereo_t>* out){ stream.setInput(out); });
2021-12-07 02:13:23 +01:00
2021-12-03 19:46:09 +01:00
// Load config
bandwidth = selectedDemod->getDefaultBandwidth();
2021-12-04 04:49:51 +01:00
minBandwidth = selectedDemod->getMinBandwidth();
maxBandwidth = selectedDemod->getMaxBandwidth();
bandwidthLocked = selectedDemod->getBandwidthLocked();
2021-12-03 19:46:09 +01:00
snapInterval = selectedDemod->getDefaultSnapInterval();
squelchLevel = MIN_SQUELCH;
2021-12-04 17:46:48 +01:00
deempAllowed = selectedDemod->getDeempAllowed();
deempId = deempModes.valueId((DeemphasisMode)selectedDemod->getDefaultDeemphasisMode());
2021-12-04 04:49:51 +01:00
squelchEnabled = false;
2021-12-07 02:13:23 +01:00
postProcEnabled = selectedDemod->getPostProcEnabled();
2021-12-08 02:10:41 +01:00
FMIFNRAllowed = selectedDemod->getFMIFNRAllowed();
FMIFNREnabled = false;
2021-12-24 21:42:04 +01:00
fmIFPresetId = ifnrPresets.valueId(IFNR_PRESET_VOICE);
2021-12-10 22:25:22 +01:00
nbAllowed = selectedDemod->getNBAllowed();
nbEnabled = false;
nbLevel = 0.0f;
2022-06-26 03:42:22 +02:00
double ifSamplerate = selectedDemod->getIFSampleRate();
2021-12-10 22:25:22 +01:00
config.acquire();
2021-12-08 02:10:41 +01:00
if (config.conf[name][selectedDemod->getName()].contains("bandwidth")) {
2021-12-03 19:46:09 +01:00
bandwidth = config.conf[name][selectedDemod->getName()]["bandwidth"];
bandwidth = std::clamp<double>(bandwidth, minBandwidth, maxBandwidth);
}
if (config.conf[name][selectedDemod->getName()].contains("snapInterval")) {
snapInterval = config.conf[name][selectedDemod->getName()]["snapInterval"];
}
if (config.conf[name][selectedDemod->getName()].contains("squelchLevel")) {
squelchLevel = config.conf[name][selectedDemod->getName()]["squelchLevel"];
}
2021-12-04 04:49:51 +01:00
if (config.conf[name][selectedDemod->getName()].contains("squelchEnabled")) {
squelchEnabled = config.conf[name][selectedDemod->getName()]["squelchEnabled"];
}
if (config.conf[name][selectedDemod->getName()].contains("deempMode")) {
if (!config.conf[name][selectedDemod->getName()]["deempMode"].is_string()) {
config.conf[name][selectedDemod->getName()]["deempMode"] = deempModes.key(deempId);
}
std::string deempOpt = config.conf[name][selectedDemod->getName()]["deempMode"];
if (deempModes.keyExists(deempOpt)) {
deempId = deempModes.keyId(deempOpt);
}
2021-12-04 04:49:51 +01:00
}
2021-12-08 02:10:41 +01:00
if (config.conf[name][selectedDemod->getName()].contains("FMIFNREnabled")) {
FMIFNREnabled = config.conf[name][selectedDemod->getName()]["FMIFNREnabled"];
}
2021-12-24 21:42:04 +01:00
if (config.conf[name][selectedDemod->getName()].contains("fmifnrPreset")) {
std::string presetOpt = config.conf[name][selectedDemod->getName()]["fmifnrPreset"];
if (ifnrPresets.keyExists(presetOpt)) {
fmIFPresetId = ifnrPresets.keyId(presetOpt);
}
}
2021-12-10 22:25:22 +01:00
if (config.conf[name][selectedDemod->getName()].contains("noiseBlankerEnabled")) {
nbEnabled = config.conf[name][selectedDemod->getName()]["noiseBlankerEnabled"];
}
if (config.conf[name][selectedDemod->getName()].contains("noiseBlankerLevel")) {
nbLevel = config.conf[name][selectedDemod->getName()]["noiseBlankerLevel"];
}
config.release();
2021-12-03 19:46:09 +01:00
// Configure VFO
if (vfo) {
2021-12-04 17:46:48 +01:00
vfo->setBandwidthLimits(minBandwidth, maxBandwidth, selectedDemod->getBandwidthLocked());
2021-12-03 19:46:09 +01:00
vfo->setReference(selectedDemod->getVFOReference());
vfo->setSnapInterval(snapInterval);
2022-06-26 03:42:22 +02:00
vfo->setSampleRate(ifSamplerate, bandwidth);
2021-12-03 19:46:09 +01:00
}
// Configure bandwidth
setBandwidth(bandwidth);
2022-06-26 03:42:22 +02:00
// Configure noise blanker
nb.setRate(500.0 / ifSamplerate);
setNBLevel(nbLevel);
setNBEnabled(nbEnabled);
2021-12-08 02:10:41 +01:00
// Configure FM IF Noise Reduction
2021-12-24 21:42:04 +01:00
setIFNRPreset((selectedDemodID == RADIO_DEMOD_NFM) ? ifnrPresets[fmIFPresetId] : IFNR_PRESET_BROADCAST);
2021-12-08 02:10:41 +01:00
setFMIFNREnabled(FMIFNRAllowed ? FMIFNREnabled : false);
// Configure squelch
2022-06-26 03:42:22 +02:00
setSquelchLevel(squelchLevel);
2021-12-04 04:49:51 +01:00
setSquelchEnabled(squelchEnabled);
2021-12-03 19:46:09 +01:00
2021-12-07 02:13:23 +01:00
// Configure AF chain
2021-12-04 17:46:48 +01:00
if (postProcEnabled) {
// Configure resampler
2021-12-07 02:13:23 +01:00
afChain.stop();
2022-06-15 16:08:28 +02:00
resamp.setInSamplerate(selectedDemod->getAFSampleRate());
2021-12-04 17:46:48 +01:00
setAudioSampleRate(audioSampleRate);
2022-06-15 16:08:28 +02:00
afChain.enableBlock(&resamp, [=](dsp::stream<dsp::stereo_t>* out){ stream.setInput(out); });
2021-12-04 17:46:48 +01:00
// Configure deemphasis
setDeemphasisMode(deempModes[deempId]);
2021-12-07 02:13:23 +01:00
}
else {
// Disable everything if post processing is disabled
2022-06-15 16:08:28 +02:00
afChain.disableAllBlocks([=](dsp::stream<dsp::stereo_t>* out){ stream.setInput(out); });
2021-12-04 04:49:51 +01:00
}
2021-12-03 19:46:09 +01:00
// Start new demodulator
selectedDemod->start();
}
2021-12-04 17:46:48 +01:00
2021-12-03 19:46:09 +01:00
void setBandwidth(double bw) {
bw = std::clamp<double>(bw, minBandwidth, maxBandwidth);
2021-12-03 19:46:09 +01:00
bandwidth = bw;
if (!selectedDemod) { return; }
2021-12-04 04:49:51 +01:00
vfo->setBandwidth(bandwidth);
2021-12-03 19:46:09 +01:00
selectedDemod->setBandwidth(bandwidth);
2021-12-07 02:13:23 +01:00
2021-12-03 19:46:09 +01:00
config.acquire();
config.conf[name][selectedDemod->getName()]["bandwidth"] = bandwidth;
config.release(true);
}
void setAudioSampleRate(double sr) {
audioSampleRate = sr;
if (!selectedDemod) { return; }
2021-12-04 17:46:48 +01:00
selectedDemod->AFSampRateChanged(audioSampleRate);
if (!postProcEnabled) {
2021-12-07 02:13:23 +01:00
// If postproc is disabled, IF SR = AF SR
2021-12-04 17:46:48 +01:00
minBandwidth = selectedDemod->getMinBandwidth();
maxBandwidth = selectedDemod->getMaxBandwidth();
bandwidth = selectedDemod->getIFSampleRate();
vfo->setBandwidthLimits(minBandwidth, maxBandwidth, selectedDemod->getBandwidthLocked());
vfo->setSampleRate(selectedDemod->getIFSampleRate(), bandwidth);
return;
}
2021-12-07 02:13:23 +01:00
afChain.stop();
2022-06-26 03:42:22 +02:00
// Configure resampler
2022-06-15 16:08:28 +02:00
resamp.setOutSamplerate(audioSampleRate);
2021-12-04 04:49:51 +01:00
2021-12-07 02:13:23 +01:00
// Configure deemphasis sample rate
2022-06-15 16:08:28 +02:00
deemp.setSamplerate(audioSampleRate);
2021-12-07 02:13:23 +01:00
afChain.start();
2021-12-04 17:46:48 +01:00
}
void setDeemphasisMode(DeemphasisMode mode) {
deempId = deempModes.valueId(mode);
if (!postProcEnabled || !selectedDemod) { return; }
bool deempEnabled = (mode != DEEMP_MODE_NONE);
2022-06-15 16:08:28 +02:00
if (deempEnabled) { deemp.setTau(deempTaus[mode]); }
afChain.setBlockEnabled(&deemp, deempEnabled, [=](dsp::stream<dsp::stereo_t>* out){ stream.setInput(out); });
// Save config
config.acquire();
config.conf[name][selectedDemod->getName()]["deempMode"] = deempModes.key(deempId);
config.release(true);
2021-12-04 04:49:51 +01:00
}
2022-06-26 03:42:22 +02:00
void setNBEnabled(bool enable) {
nbEnabled = enable;
if (!selectedDemod) { return; }
ifChain.setBlockEnabled(&nb, nbEnabled, [=](dsp::stream<dsp::complex_t>* out){ selectedDemod->setInput(out); });
// Save config
config.acquire();
config.conf[name][selectedDemod->getName()]["noiseBlankerEnabled"] = nbEnabled;
config.release(true);
}
void setNBLevel(float level) {
nbLevel = std::clamp<float>(level, MIN_NB, MAX_NB);
nb.setLevel(nbLevel);
// Save config
config.acquire();
config.conf[name][selectedDemod->getName()]["noiseBlankerLevel"] = nbLevel;
config.release(true);
}
2021-12-04 04:49:51 +01:00
void setSquelchEnabled(bool enable) {
squelchEnabled = enable;
if (!selectedDemod) { return; }
2022-06-15 16:08:28 +02:00
ifChain.setBlockEnabled(&squelch, squelchEnabled, [=](dsp::stream<dsp::complex_t>* out){ selectedDemod->setInput(out); });
// Save config
config.acquire();
config.conf[name][selectedDemod->getName()]["squelchEnabled"] = squelchEnabled;
config.release(true);
}
void setSquelchLevel(float level) {
squelchLevel = std::clamp<float>(level, MIN_SQUELCH, MAX_SQUELCH);
2022-06-15 16:08:28 +02:00
squelch.setLevel(squelchLevel);
// Save config
config.acquire();
config.conf[name][selectedDemod->getName()]["squelchLevel"] = squelchLevel;
config.release(true);
2021-12-03 19:46:09 +01:00
}
2021-12-08 02:10:41 +01:00
void setFMIFNREnabled(bool enabled) {
FMIFNREnabled = enabled;
if (!selectedDemod) { return; }
2022-06-15 16:08:28 +02:00
ifChain.setBlockEnabled(&fmnr, FMIFNREnabled, [=](dsp::stream<dsp::complex_t>* out){ selectedDemod->setInput(out); });
2021-12-08 02:10:41 +01:00
// Save config
config.acquire();
config.conf[name][selectedDemod->getName()]["FMIFNREnabled"] = FMIFNREnabled;
config.release(true);
}
2021-12-24 21:42:04 +01:00
void setIFNRPreset(IFNRPreset preset) {
// Don't save if in broadcast mode
if (preset == IFNR_PRESET_BROADCAST) {
if (!selectedDemod) { return; }
2022-06-15 16:08:28 +02:00
fmnr.setBins(ifnrTaps[preset]);
2021-12-24 21:42:04 +01:00
return;
}
fmIFPresetId = ifnrPresets.valueId(preset);
if (!selectedDemod) { return; }
2022-06-15 16:08:28 +02:00
fmnr.setBins(ifnrTaps[preset]);
2021-12-24 21:42:04 +01:00
// Save config
config.acquire();
config.conf[name][selectedDemod->getName()]["fmifnrPreset"] = ifnrPresets.key(fmIFPresetId);
config.release(true);
}
2021-12-03 19:46:09 +01:00
static void vfoUserChangedBandwidthHandler(double newBw, void* ctx) {
RadioModule* _this = (RadioModule*)ctx;
2021-12-03 19:46:09 +01:00
_this->setBandwidth(newBw);
}
static void sampleRateChangeHandler(float sampleRate, void* ctx) {
RadioModule* _this = (RadioModule*)ctx;
2021-12-03 19:46:09 +01:00
_this->setAudioSampleRate(sampleRate);
}
2021-12-04 04:49:51 +01:00
2021-12-07 02:13:23 +01:00
static void ifChainOutputChangeHandler(dsp::stream<dsp::complex_t>* output, void* ctx) {
RadioModule* _this = (RadioModule*)ctx;
2021-12-07 02:13:23 +01:00
if (!_this->selectedDemod) { return; }
_this->selectedDemod->setInput(output);
}
static void moduleInterfaceHandler(int code, void* in, void* out, void* ctx) {
RadioModule* _this = (RadioModule*)ctx;
if (!_this->enabled || !_this->selectedDemod) { return; }
// Execute commands
if (code == RADIO_IFACE_CMD_GET_MODE && out) {
int* _out = (int*)out;
*_out = _this->selectedDemodID;
}
else if (code == RADIO_IFACE_CMD_SET_MODE && in) {
int* _in = (int*)in;
_this->selectDemodByID((DemodID)*_in);
}
else if (code == RADIO_IFACE_CMD_GET_BANDWIDTH && out) {
float* _out = (float*)out;
*_out = _this->bandwidth;
}
else if (code == RADIO_IFACE_CMD_SET_BANDWIDTH && in) {
float* _in = (float*)in;
if (_this->bandwidthLocked) { return; }
_this->setBandwidth(*_in);
}
else if (code == RADIO_IFACE_CMD_GET_SQUELCH_ENABLED && out) {
bool* _out = (bool*)out;
*_out = _this->squelchEnabled;
}
else if (code == RADIO_IFACE_CMD_SET_SQUELCH_ENABLED && in) {
bool* _in = (bool*)in;
_this->setSquelchEnabled(*_in);
}
else if (code == RADIO_IFACE_CMD_GET_SQUELCH_LEVEL && out) {
float* _out = (float*)out;
*_out = _this->squelchLevel;
}
else if (code == RADIO_IFACE_CMD_SET_SQUELCH_LEVEL && in) {
float* _in = (float*)in;
_this->setSquelchLevel(*_in);
}
else {
return;
}
// Success
return;
}
2021-12-07 02:13:23 +01:00
// Handlers
2021-12-03 19:46:09 +01:00
EventHandler<double> onUserChangedBandwidthHandler;
2021-12-07 02:13:23 +01:00
EventHandler<float> srChangeHandler;
EventHandler<dsp::stream<dsp::complex_t>*> ifChainOutputChanged;
EventHandler<dsp::stream<dsp::stereo_t>*> afChainOutputChanged;
VFOManager::VFO* vfo = NULL;
2021-12-03 19:46:09 +01:00
2021-12-07 02:13:23 +01:00
// IF chain
2022-06-15 16:08:28 +02:00
dsp::chain<dsp::complex_t> ifChain;
2022-06-26 03:42:22 +02:00
dsp::noise_reduction::NoiseBlanker nb;
2022-06-15 16:08:28 +02:00
dsp::noise_reduction::FMIF fmnr;
dsp::noise_reduction::Squelch squelch;
2021-12-07 02:13:23 +01:00
// Audio chain
dsp::stream<dsp::stereo_t> dummyAudioStream;
2022-06-15 16:08:28 +02:00
dsp::chain<dsp::stereo_t> afChain;
dsp::multirate::RationalResampler<dsp::stereo_t> resamp;
dsp::filter::Deemphasis<dsp::stereo_t> deemp;
2021-12-03 19:46:09 +01:00
SinkManager::Stream stream;
demod::Demodulator* selectedDemod = NULL;
OptionList<std::string, DeemphasisMode> deempModes;
2021-12-24 21:42:04 +01:00
OptionList<std::string, IFNRPreset> ifnrPresets;
2021-12-03 19:46:09 +01:00
double audioSampleRate = 48000.0;
2021-12-04 04:49:51 +01:00
float minBandwidth;
float maxBandwidth;
float bandwidth;
bool bandwidthLocked;
int snapInterval;
2021-12-10 22:25:22 +01:00
int selectedDemodID = 1;
bool postProcEnabled;
2021-12-04 04:49:51 +01:00
bool squelchEnabled = false;
2021-12-03 19:46:09 +01:00
float squelchLevel;
2021-12-10 22:25:22 +01:00
int deempId = 0;
2021-12-04 04:49:51 +01:00
bool deempAllowed;
2021-12-10 22:25:22 +01:00
2021-12-08 02:10:41 +01:00
bool FMIFNRAllowed;
bool FMIFNREnabled = false;
2021-12-24 21:42:04 +01:00
int fmIFPresetId;
2021-12-03 19:46:09 +01:00
bool notchEnabled = false;
float notchPos = 0;
float notchWidth = 500;
2021-12-10 22:25:22 +01:00
bool nbAllowed;
2022-06-26 03:42:22 +02:00
bool nbEnabled = false;
float nbLevel = 10.0f;
2021-12-10 22:25:22 +01:00
2022-06-26 03:42:22 +02:00
const double MIN_NB = 1.0;
const double MAX_NB = 10.0;
2021-12-03 19:46:09 +01:00
const double MIN_SQUELCH = -100.0;
const double MAX_SQUELCH = 0.0;
bool enabled = true;
};