SDRPlusPlus/modules/radio/src/path.cpp

244 wiersze
7.9 KiB
C++
Czysty Zwykły widok Historia

2020-08-11 16:33:42 +00:00
#include <path.h>
SigPath::SigPath() {
}
2020-08-16 01:39:05 +00:00
int SigPath::sampleRateChangeHandler(void* ctx, float sampleRate) {
SigPath* _this = (SigPath*)ctx;
2020-08-20 16:29:23 +00:00
_this->outputSampleRate = sampleRate;
2020-08-16 01:39:05 +00:00
_this->audioResamp.stop();
2020-08-20 16:29:23 +00:00
_this->deemp.stop();
float bw = std::min<float>(_this->bandwidth, sampleRate / 2.0f);
_this->audioResamp.setOutputSampleRate(sampleRate, bw, bw);
_this->deemp.setBlockSize(_this->audioResamp.getOutputBlockSize());
_this->deemp.setSamplerate(sampleRate);
2020-08-16 01:39:05 +00:00
_this->audioResamp.start();
2020-08-20 16:29:23 +00:00
_this->deemp.start();
2020-08-16 01:39:05 +00:00
return _this->audioResamp.getOutputBlockSize();
}
2020-08-11 16:33:42 +00:00
void SigPath::init(std::string vfoName, uint64_t sampleRate, int blockSize, dsp::stream<dsp::complex_t>* input) {
this->sampleRate = sampleRate;
this->blockSize = blockSize;
this->vfoName = vfoName;
_demod = DEMOD_FM;
2020-08-21 13:34:50 +00:00
_deemp = DEEMP_50US;
2020-08-20 16:29:23 +00:00
bandwidth = 200000;
2020-08-11 16:33:42 +00:00
// TODO: Set default VFO options
2020-09-17 22:23:03 +00:00
// TODO: ajust deemphasis for different output sample rates
// TODO: Add a mono to stereo for different modes
2020-08-11 16:33:42 +00:00
demod.init(input, 100000, 200000, 800);
amDemod.init(input, 50);
ssbDemod.init(input, 6000, 3000, 22);
2020-09-17 22:23:03 +00:00
cpx2stereo.init(input, 22);
2020-08-11 16:33:42 +00:00
audioResamp.init(&demod.output, 200000, 48000, 800);
2020-08-20 16:29:23 +00:00
deemp.init(&audioResamp.output, 800, 50e-6, 48000);
outputSampleRate = API->registerMonoStream(&deemp.output, vfoName, vfoName, sampleRateChangeHandler, this);
2020-08-16 01:39:05 +00:00
API->setBlockSize(vfoName, audioResamp.getOutputBlockSize());
2020-08-20 16:29:23 +00:00
2020-08-21 13:34:50 +00:00
setDemodulator(_demod, bandwidth);
2020-08-11 16:33:42 +00:00
}
void SigPath::setSampleRate(float sampleRate) {
this->sampleRate = sampleRate;
// Reset the demodulator and audio systems
2020-08-21 13:34:50 +00:00
setDemodulator(_demod, bandwidth);
2020-08-11 16:33:42 +00:00
}
2020-08-21 13:34:50 +00:00
void SigPath::setDemodulator(int demId, float bandWidth) {
2020-08-11 16:33:42 +00:00
if (demId < 0 || demId >= _DEMOD_COUNT) {
return;
}
audioResamp.stop();
2020-08-20 16:29:23 +00:00
deemp.stop();
2020-08-11 16:33:42 +00:00
2020-08-21 13:34:50 +00:00
bandwidth = bandWidth;
2020-08-11 16:33:42 +00:00
// Stop current demodulator
if (_demod == DEMOD_FM) {
demod.stop();
}
else if (_demod == DEMOD_NFM) {
demod.stop();
}
else if (_demod == DEMOD_AM) {
amDemod.stop();
}
else if (_demod == DEMOD_USB) {
ssbDemod.stop();
}
else if (_demod == DEMOD_LSB) {
ssbDemod.stop();
}
2020-08-21 13:34:50 +00:00
else if (_demod == DEMOD_DSB) {
ssbDemod.stop();
}
2020-09-17 22:23:03 +00:00
else if (_demod == DEMOD_RAW) {
cpx2stereo.stop();
}
2020-08-21 13:34:50 +00:00
else {
spdlog::error("UNIMPLEMENTED DEMODULATOR IN SigPath::setDemodulator (stop)");
}
2020-08-11 16:33:42 +00:00
_demod = demId;
// Set input of the audio resampler
2020-08-21 13:34:50 +00:00
// TODO: Set bandwidth from argument
2020-08-11 16:33:42 +00:00
if (demId == DEMOD_FM) {
2020-08-21 13:34:50 +00:00
API->setVFOSampleRate(vfoName, 200000, bandwidth);
2020-08-11 16:33:42 +00:00
demod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
demod.setSampleRate(200000);
2020-08-21 13:34:50 +00:00
demod.setDeviation(bandwidth / 2.0f);
2020-08-11 16:33:42 +00:00
audioResamp.setInput(&demod.output);
2020-08-21 13:34:50 +00:00
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
2020-08-20 16:29:23 +00:00
audioResamp.setInputSampleRate(200000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
2020-08-21 13:34:50 +00:00
deemp.bypass = (_deemp == DEEMP_NONE);
2020-08-11 16:33:42 +00:00
demod.start();
}
else if (demId == DEMOD_NFM) {
2020-08-21 13:34:50 +00:00
API->setVFOSampleRate(vfoName, 16000, bandwidth);
2020-08-11 16:33:42 +00:00
demod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
2020-08-20 16:29:23 +00:00
demod.setSampleRate(16000);
2020-08-21 13:34:50 +00:00
demod.setDeviation(bandwidth / 2.0f);
2020-08-11 16:33:42 +00:00
audioResamp.setInput(&demod.output);
2020-08-21 13:34:50 +00:00
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
2020-08-20 16:29:23 +00:00
audioResamp.setInputSampleRate(16000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
deemp.bypass = true;
2020-08-11 16:33:42 +00:00
demod.start();
}
else if (demId == DEMOD_AM) {
2020-08-21 13:34:50 +00:00
API->setVFOSampleRate(vfoName, 12500, bandwidth);
2020-08-11 16:33:42 +00:00
amDemod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
audioResamp.setInput(&amDemod.output);
2020-08-21 13:34:50 +00:00
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
2020-08-20 16:29:23 +00:00
audioResamp.setInputSampleRate(12500, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
deemp.bypass = true;
2020-08-11 16:33:42 +00:00
amDemod.start();
}
else if (demId == DEMOD_USB) {
2020-08-21 13:34:50 +00:00
API->setVFOSampleRate(vfoName, 6000, bandwidth);
2020-08-11 16:33:42 +00:00
ssbDemod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
ssbDemod.setMode(dsp::SSBDemod::MODE_USB);
audioResamp.setInput(&ssbDemod.output);
2020-08-21 13:34:50 +00:00
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
2020-08-20 16:29:23 +00:00
audioResamp.setInputSampleRate(6000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
deemp.bypass = true;
2020-08-11 16:33:42 +00:00
ssbDemod.start();
}
else if (demId == DEMOD_LSB) {
2020-08-21 13:34:50 +00:00
API->setVFOSampleRate(vfoName, 6000, bandwidth);
2020-08-11 16:33:42 +00:00
ssbDemod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
ssbDemod.setMode(dsp::SSBDemod::MODE_LSB);
audioResamp.setInput(&ssbDemod.output);
2020-08-21 13:34:50 +00:00
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
2020-08-20 16:29:23 +00:00
audioResamp.setInputSampleRate(6000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
deemp.bypass = true;
ssbDemod.start();
}
else if (demId == DEMOD_DSB) {
2020-08-21 13:34:50 +00:00
API->setVFOSampleRate(vfoName, 6000, bandwidth);
2020-08-20 16:29:23 +00:00
ssbDemod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
ssbDemod.setMode(dsp::SSBDemod::MODE_DSB);
audioResamp.setInput(&ssbDemod.output);
2020-08-21 13:34:50 +00:00
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
2020-08-20 16:29:23 +00:00
audioResamp.setInputSampleRate(6000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
deemp.bypass = true;
2020-08-11 16:33:42 +00:00
ssbDemod.start();
}
2020-09-17 22:23:03 +00:00
else if (demId == DEMOD_RAW) {
API->setVFOSampleRate(vfoName, 10000, bandwidth);
cpx2stereo.setBlockSize(API->getVFOOutputBlockSize(vfoName));
//audioResamp.setInput(&cpx2stereo.output);
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(10000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
cpx2stereo.start();
}
2020-08-21 13:34:50 +00:00
else {
2020-09-06 14:31:50 +00:00
spdlog::error("UNIMPLEMENTED DEMODULATOR IN SigPath::setDemodulator (start): {0}", demId);
2020-08-21 13:34:50 +00:00
}
2020-08-11 16:33:42 +00:00
2020-08-20 16:29:23 +00:00
deemp.setBlockSize(audioResamp.getOutputBlockSize());
2020-08-11 16:33:42 +00:00
audioResamp.start();
2020-08-20 16:29:23 +00:00
deemp.start();
2020-08-11 16:33:42 +00:00
}
void SigPath::updateBlockSize() {
2020-08-21 13:34:50 +00:00
setDemodulator(_demod, bandwidth);
}
void SigPath::setDeemphasis(int deemph) {
_deemp = deemph;
deemp.stop();
if (_deemp == DEEMP_NONE) {
deemp.bypass = true;
}
else if (_deemp == DEEMP_50US) {
deemp.bypass = false;
deemp.setTau(50e-6);
}
else if (_deemp == DEEMP_75US) {
deemp.bypass = false;
deemp.setTau(75e-6);
}
deemp.start();
}
void SigPath::setBandwidth(float bandWidth) {
bandwidth = bandWidth;
API->setVFOBandwidth(vfoName, bandwidth);
if (_demod == DEMOD_FM) {
demod.stop();
demod.setDeviation(bandwidth / 2.0f);
demod.start();
}
else if (_demod == DEMOD_NFM) {
demod.stop();
demod.setDeviation(bandwidth / 2.0f);
demod.start();
}
else if (_demod == DEMOD_AM) {
// Notbing to change
}
else if (_demod == DEMOD_USB) {
ssbDemod.stop();
ssbDemod.setBandwidth(bandwidth);
ssbDemod.start();
}
else if (_demod == DEMOD_LSB) {
ssbDemod.stop();
ssbDemod.setBandwidth(bandwidth);
ssbDemod.start();
}
else if (_demod == DEMOD_DSB) {
ssbDemod.stop();
ssbDemod.setBandwidth(bandwidth);
ssbDemod.start();
}
2020-09-17 22:23:03 +00:00
else if (_demod == DEMOD_RAW) {
// Notbing to change
}
2020-08-21 13:34:50 +00:00
else {
spdlog::error("UNIMPLEMENTED DEMODULATOR IN SigPath::setBandwidth");
}
float _audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
if (audioBw != _audioBw) {
audioBw = _audioBw;
audioResamp.stop();
audioResamp.setInputSampleRate(6000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
audioResamp.start();
}
2020-08-11 16:33:42 +00:00
}
void SigPath::start() {
demod.start();
audioResamp.start();
2020-08-20 16:29:23 +00:00
deemp.start();
2020-08-16 01:39:05 +00:00
API->startStream(vfoName);
2020-08-11 16:33:42 +00:00
}