SDRPlusPlus/radio/src/main.cpp

225 lines
6.8 KiB
C++
Raw Normal View History

2020-08-11 18:33:42 +02:00
#include <imgui.h>
2020-08-12 16:43:44 +02:00
#include <watcher.h>
2020-09-24 19:36:57 +02:00
#include <config.h>
2020-10-07 14:44:39 +02:00
#include <core.h>
2020-12-08 04:36:37 +01:00
#include <gui/style.h>
2020-12-06 16:13:47 +01:00
#include <signal_path/signal_path.h>
#include <radio_demod.h>
2020-12-22 20:00:51 +01:00
#include <module.h>
2020-12-06 16:13:47 +01:00
#include <wfm_demod.h>
#include <fm_demod.h>
#include <am_demod.h>
#include <usb_demod.h>
#include <lsb_demod.h>
#include <dsb_demod.h>
#include <raw_demod.h>
#include <cw_demod.h>
2020-12-22 14:50:26 +01:00
#include <options.h>
2020-08-11 18:33:42 +02:00
2020-08-21 15:34:50 +02:00
#define CONCAT(a, b) ((std::string(a) + b).c_str())
2020-08-11 18:33:42 +02:00
2020-12-08 04:36:37 +01:00
SDRPP_MOD_INFO {
/* Name: */ "radio",
/* Description: */ "Radio module for SDR++",
/* Author: */ "Ryzerth",
/* Version: */ 0, 3, 0,
/* Max instances */ -1
2020-10-01 01:21:15 +02:00
};
static ConfigManager config;
2020-12-08 04:36:37 +01:00
class RadioModule : public ModuleManager::Instance {
2020-10-01 01:21:15 +02:00
public:
RadioModule(std::string name) {
this->name = name;
2020-10-07 14:44:39 +02:00
2020-12-06 16:13:47 +01:00
vfo = sigpath::vfoManager.createVFO(name, ImGui::WaterfallVFO::REF_CENTER, 0, 200000, 200000, 1);
2020-12-08 04:36:37 +01:00
ns.init(vfo->output);
config.aquire();
if (!config.conf.contains(name)) {
config.conf[name]["selectedDemodId"] = 1;
}
demodId = config.conf[name]["selectedDemodId"];
config.release(true);
wfmDemod.init(name, vfo, audioSampRate, 200000, &config);
fmDemod.init(name, vfo, audioSampRate, 12500, &config);
amDemod.init(name, vfo, audioSampRate, 12500, &config);
usbDemod.init(name, vfo, audioSampRate, 3000, &config);
lsbDemod.init(name, vfo, audioSampRate, 3000, &config);
dsbDemod.init(name, vfo, audioSampRate, 6000, &config);
rawDemod.init(name, vfo, audioSampRate, audioSampRate, &config);
cwDemod.init(name, vfo, audioSampRate, 200, &config);
2020-12-06 16:13:47 +01:00
srChangeHandler.ctx = this;
srChangeHandler.handler = sampleRateChangeHandler;
stream.init(wfmDemod.getOutput(), srChangeHandler, audioSampRate);
sigpath::sinkManager.registerStream(name, &stream);
selectDemodById(demodId);
2020-12-06 16:13:47 +01:00
stream.start();
2020-12-08 04:36:37 +01:00
gui::menu.registerEntry(name, menuHandler, this, this);
2020-10-01 01:21:15 +02:00
}
~RadioModule() {
2020-12-06 16:13:47 +01:00
2020-10-01 01:21:15 +02:00
}
2020-08-11 18:33:42 +02:00
2020-12-08 04:36:37 +01:00
void enable() {
vfo = sigpath::vfoManager.createVFO(name, ImGui::WaterfallVFO::REF_CENTER, 0, 200000, 200000, 1);
2021-02-16 23:56:53 +01:00
wfmDemod.setVFO(vfo);
fmDemod.setVFO(vfo);
amDemod.setVFO(vfo);
usbDemod.setVFO(vfo);
lsbDemod.setVFO(vfo);
dsbDemod.setVFO(vfo);
rawDemod.setVFO(vfo);
cwDemod.setVFO(vfo);
2020-12-08 04:36:37 +01:00
currentDemod->select();
currentDemod->start();
enabled = true;
}
void disable() {
currentDemod->stop();
sigpath::vfoManager.deleteVFO(vfo);
//ns.setInput(vfo->output);
//ns.start();
enabled = false;
}
bool isEnabled() {
return enabled;
}
2020-10-01 01:21:15 +02:00
private:
static void menuHandler(void* ctx) {
RadioModule* _this = (RadioModule*)ctx;
2020-12-08 04:36:37 +01:00
if (!_this->enabled) { style::beginDisabled(); }
2020-12-06 16:13:47 +01:00
float menuWidth = ImGui::GetContentRegionAvailWidth();
2020-10-01 01:21:15 +02:00
ImGui::BeginGroup();
// TODO: Change VFO ref in signal path
ImGui::Columns(4, CONCAT("RadioModeColumns##_", _this->name), false);
2020-12-06 16:13:47 +01:00
if (ImGui::RadioButton(CONCAT("NFM##_", _this->name), _this->demodId == 0) && _this->demodId != 0) {
_this->selectDemodById(0);
2020-10-01 01:21:15 +02:00
}
2020-12-06 16:13:47 +01:00
if (ImGui::RadioButton(CONCAT("WFM##_", _this->name), _this->demodId == 1) && _this->demodId != 1) {
_this->selectDemodById(1);
2020-10-01 01:21:15 +02:00
}
ImGui::NextColumn();
2020-12-06 16:13:47 +01:00
if (ImGui::RadioButton(CONCAT("AM##_", _this->name), _this->demodId == 2) && _this->demodId != 2) {
_this->selectDemodById(2);
2020-10-01 01:21:15 +02:00
}
2020-12-06 16:13:47 +01:00
if (ImGui::RadioButton(CONCAT("DSB##_", _this->name), _this->demodId == 3) && _this->demodId != 3) {
_this->selectDemodById(3);
2020-10-01 01:21:15 +02:00
}
ImGui::NextColumn();
2020-12-06 16:13:47 +01:00
if (ImGui::RadioButton(CONCAT("USB##_", _this->name), _this->demodId == 4) && _this->demodId != 4) {
_this->selectDemodById(4);
2020-10-01 01:21:15 +02:00
}
2020-12-06 16:13:47 +01:00
if (ImGui::RadioButton(CONCAT("CW##_", _this->name), _this->demodId == 5) && _this->demodId != 5) {
_this->selectDemodById(5);
2020-12-06 16:13:47 +01:00
};
2020-10-01 01:21:15 +02:00
ImGui::NextColumn();
2020-12-06 16:13:47 +01:00
if (ImGui::RadioButton(CONCAT("LSB##_", _this->name), _this->demodId == 6) && _this->demodId != 6) {
_this->selectDemodById(6);
2020-10-01 01:21:15 +02:00
}
2020-12-06 16:13:47 +01:00
if (ImGui::RadioButton(CONCAT("RAW##_", _this->name), _this->demodId == 7) && _this->demodId != 7) {
_this->selectDemodById(7);
2020-10-01 01:21:15 +02:00
};
ImGui::Columns(1, CONCAT("EndRadioModeColumns##_", _this->name), false);
ImGui::EndGroup();
2020-12-06 16:13:47 +01:00
_this->currentDemod->showMenu();
2020-12-08 04:36:37 +01:00
if (!_this->enabled) { style::endDisabled(); }
2020-10-07 14:44:39 +02:00
}
2020-12-06 16:13:47 +01:00
static void sampleRateChangeHandler(float sampleRate, void* ctx) {
RadioModule* _this = (RadioModule*)ctx;
// TODO: If too slow, change all demods here and not when setting
_this->audioSampRate = sampleRate;
if (_this->currentDemod != NULL) {
_this->currentDemod->setAudioSampleRate(_this->audioSampRate);
}
}
2020-12-06 16:13:47 +01:00
void selectDemod(Demodulator* demod) {
if (currentDemod != NULL) { currentDemod->stop(); }
currentDemod = demod;
currentDemod->setAudioSampleRate(audioSampRate);
stream.setInput(currentDemod->getOutput());
currentDemod->select();
2020-12-06 16:46:50 +01:00
vfo->output->flush();
2020-12-06 16:13:47 +01:00
currentDemod->start();
}
void selectDemodById(int id) {
demodId = id;
if (id == 0) { selectDemod(&fmDemod); }
if (id == 1) { selectDemod(&wfmDemod); }
if (id == 2) { selectDemod(&amDemod); }
if (id == 3) { selectDemod(&dsbDemod); }
if (id == 4) { selectDemod(&usbDemod); }
if (id == 5) { selectDemod(&cwDemod); }
if (id == 6) { selectDemod(&lsbDemod); }
if (id == 7) { selectDemod(&rawDemod); }
config.aquire();
config.conf[name]["selectedDemodId"] = demodId;
config.release(true);
}
2020-08-11 18:33:42 +02:00
std::string name;
2020-12-08 04:36:37 +01:00
bool enabled = true;
2020-12-06 16:13:47 +01:00
int demodId = 0;
float audioSampRate = 48000;
Demodulator* currentDemod = NULL;
VFOManager::VFO* vfo;
WFMDemodulator wfmDemod;
FMDemodulator fmDemod;
AMDemodulator amDemod;
USBDemodulator usbDemod;
LSBDemodulator lsbDemod;
DSBDemodulator dsbDemod;
RAWDemodulator rawDemod;
CWDemodulator cwDemod;
2020-12-08 04:36:37 +01:00
dsp::NullSink<dsp::complex_t> ns;
2020-12-06 16:13:47 +01:00
Event<float>::EventHandler srChangeHandler;
SinkManager::Stream stream;
2020-08-11 18:33:42 +02:00
2020-10-01 01:21:15 +02:00
};
2020-08-11 18:33:42 +02:00
2020-10-01 01:21:15 +02:00
MOD_EXPORT void _INIT_() {
json def = json({});
2020-12-22 14:50:26 +01:00
config.setPath(options::opts.root + "/radio_config.json");
config.load(def);
config.enableAutoSave();
2020-08-12 16:43:44 +02:00
}
2020-12-08 04:36:37 +01:00
MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) {
2020-10-01 01:21:15 +02:00
return new RadioModule(name);
2020-08-11 18:33:42 +02:00
}
2020-10-01 01:21:15 +02:00
MOD_EXPORT void _DELETE_INSTANCE_(void* instance) {
delete (RadioModule*)instance;
2020-08-11 18:33:42 +02:00
}
2020-12-08 04:36:37 +01:00
MOD_EXPORT void _END_() {
config.disableAutoSave();
config.save();
2020-08-11 18:33:42 +02:00
}