Fixed issues with new module system

This commit is contained in:
Ryzerth
2020-09-20 00:19:39 +02:00
parent d6b9e1d86a
commit 9de585190f
38 changed files with 460 additions and 358 deletions

View File

@ -0,0 +1,309 @@
#include <signal_path/audio.h>
namespace audio {
std::map<std::string, AudioStream_t*> streams;
float registerMonoStream(dsp::stream<float>* stream, std::string name, std::string vfoName, int (*sampleRateChangeHandler)(void* ctx, float sampleRate), void* ctx) {
AudioStream_t* astr = new AudioStream_t;
astr->type = STREAM_TYPE_MONO;
astr->ctx = ctx;
astr->audio = new io::AudioSink;
astr->audio->init(1);
astr->deviceId = astr->audio->getDeviceId();
float sampleRate = astr->audio->devices[astr->deviceId].sampleRates[0];
int blockSize = sampleRate / 200; // default block size
astr->monoAudioStream = new dsp::stream<float>(blockSize * 2);
astr->audio->setBlockSize(blockSize);
astr->audio->setStreamType(io::AudioSink::MONO);
astr->audio->setMonoInput(astr->monoAudioStream);
astr->audio->setSampleRate(sampleRate);
astr->blockSize = blockSize;
astr->sampleRate = sampleRate;
astr->monoStream = stream;
astr->sampleRateChangeHandler = sampleRateChangeHandler;
astr->monoDynSplit = new dsp::DynamicSplitter<float>(stream, blockSize);
astr->monoDynSplit->bind(astr->monoAudioStream);
astr->running = false;
astr->volume = 1.0f;
astr->sampleRateId = 0;
astr->vfoName = vfoName;
streams[name] = astr;
return sampleRate;
}
float registerStereoStream(dsp::stream<dsp::StereoFloat_t>* stream, std::string name, std::string vfoName, int (*sampleRateChangeHandler)(void* ctx, float sampleRate), void* ctx) {
AudioStream_t* astr = new AudioStream_t;
astr->type = STREAM_TYPE_STEREO;
astr->ctx = ctx;
astr->audio = new io::AudioSink;
astr->audio->init(1);
float sampleRate = astr->audio->devices[astr->audio->getDeviceId()].sampleRates[0];
int blockSize = sampleRate / 200; // default block size
astr->stereoAudioStream = new dsp::stream<dsp::StereoFloat_t>(blockSize * 2);
astr->audio->setBlockSize(blockSize);
astr->audio->setStreamType(io::AudioSink::STEREO);
astr->audio->setStereoInput(astr->stereoAudioStream);
astr->audio->setSampleRate(sampleRate);
astr->blockSize = blockSize;
astr->sampleRate = sampleRate;
astr->stereoStream = stream;
astr->sampleRateChangeHandler = sampleRateChangeHandler;
astr->stereoDynSplit = new dsp::DynamicSplitter<dsp::StereoFloat_t>(stream, blockSize);
astr->stereoDynSplit->bind(astr->stereoAudioStream);
astr->running = false;
streams[name] = astr;
astr->vfoName = vfoName;
return sampleRate;
}
void startStream(std::string name) {
AudioStream_t* astr = streams[name];
if (astr->running) {
return;
}
if (astr->type == STREAM_TYPE_MONO) {
astr->monoDynSplit->start();
}
else {
astr->stereoDynSplit->start();
}
astr->audio->start();
astr->running = true;
}
void stopStream(std::string name) {
AudioStream_t* astr = streams[name];
if (!astr->running) {
return;
}
if (astr->type == STREAM_TYPE_MONO) {
astr->monoDynSplit->stop();
}
else {
astr->stereoDynSplit->stop();
}
astr->audio->stop();
astr->running = false;
}
void removeStream(std::string name) {
AudioStream_t* astr = streams[name];
stopStream(name);
for (int i = 0; i < astr->boundStreams.size(); i++) {
astr->boundStreams[i].streamRemovedHandler(astr->ctx);
}
delete astr->monoDynSplit;
}
dsp::stream<float>* bindToStreamMono(std::string name, void (*streamRemovedHandler)(void* ctx), void (*sampleRateChangeHandler)(void* ctx, float sampleRate, int blockSize), void* ctx) {
AudioStream_t* astr = streams[name];
BoundStream_t bstr;
bstr.type = STREAM_TYPE_MONO;
bstr.ctx = ctx;
bstr.streamRemovedHandler = streamRemovedHandler;
bstr.sampleRateChangeHandler = sampleRateChangeHandler;
if (astr->type == STREAM_TYPE_MONO) {
bstr.monoStream = new dsp::stream<float>(astr->blockSize * 2);
astr->monoDynSplit->stop();
astr->monoDynSplit->bind(bstr.monoStream);
if (astr->running) {
astr->monoDynSplit->start();
}
astr->boundStreams.push_back(bstr);
return bstr.monoStream;
}
bstr.stereoStream = new dsp::stream<dsp::StereoFloat_t>(astr->blockSize * 2);
bstr.s2m = new dsp::StereoToMono(bstr.stereoStream, astr->blockSize * 2);
bstr.monoStream = &bstr.s2m->output;
astr->stereoDynSplit->stop();
astr->stereoDynSplit->bind(bstr.stereoStream);
if (astr->running) {
astr->stereoDynSplit->start();
}
bstr.s2m->start();
astr->boundStreams.push_back(bstr);
return bstr.monoStream;
}
dsp::stream<dsp::StereoFloat_t>* bindToStreamStereo(std::string name, void (*streamRemovedHandler)(void* ctx), void (*sampleRateChangeHandler)(void* ctx, float sampleRate, int blockSize), void* ctx) {
AudioStream_t* astr = streams[name];
BoundStream_t bstr;
bstr.type = STREAM_TYPE_STEREO;
bstr.ctx = ctx;
bstr.streamRemovedHandler = streamRemovedHandler;
bstr.sampleRateChangeHandler = sampleRateChangeHandler;
if (astr->type == STREAM_TYPE_STEREO) {
bstr.stereoStream = new dsp::stream<dsp::StereoFloat_t>(astr->blockSize * 2);
astr->stereoDynSplit->stop();
astr->stereoDynSplit->bind(bstr.stereoStream);
if (astr->running) {
astr->stereoDynSplit->start();
}
astr->boundStreams.push_back(bstr);
return bstr.stereoStream;
}
bstr.monoStream = new dsp::stream<float>(astr->blockSize * 2);
bstr.m2s = new dsp::MonoToStereo(bstr.monoStream, astr->blockSize * 2);
bstr.stereoStream = &bstr.m2s->output;
astr->monoDynSplit->stop();
astr->monoDynSplit->bind(bstr.monoStream);
if (astr->running) {
astr->monoDynSplit->start();
}
bstr.m2s->start();
astr->boundStreams.push_back(bstr);
return bstr.stereoStream;
}
void setBlockSize(std::string name, int blockSize) {
AudioStream_t* astr = streams[name];
if (astr->running) {
return;
}
if (astr->type == STREAM_TYPE_MONO) {
astr->monoDynSplit->setBlockSize(blockSize);
for (int i = 0; i < astr->boundStreams.size(); i++) {
BoundStream_t bstr = astr->boundStreams[i];
bstr.monoStream->setMaxLatency(blockSize * 2);
if (bstr.type == STREAM_TYPE_STEREO) {
bstr.m2s->stop();
bstr.m2s->setBlockSize(blockSize);
bstr.m2s->start();
}
}
astr->blockSize = blockSize;
return;
}
astr->monoDynSplit->setBlockSize(blockSize);
for (int i = 0; i < astr->boundStreams.size(); i++) {
BoundStream_t bstr = astr->boundStreams[i];
bstr.stereoStream->setMaxLatency(blockSize * 2);
if (bstr.type == STREAM_TYPE_MONO) {
bstr.s2m->stop();
bstr.s2m->setBlockSize(blockSize);
bstr.s2m->start();
}
}
astr->blockSize = blockSize;
}
void unbindFromStreamMono(std::string name, dsp::stream<float>* stream) {
AudioStream_t* astr = streams[name];
for (int i = 0; i < astr->boundStreams.size(); i++) {
BoundStream_t bstr = astr->boundStreams[i];
if (bstr.monoStream != stream) {
continue;
}
if (astr->type == STREAM_TYPE_STEREO) {
astr->stereoDynSplit->stop();
astr->stereoDynSplit->unbind(bstr.stereoStream);
if (astr->running) {
astr->stereoDynSplit->start();
}
bstr.s2m->stop();
delete bstr.s2m;
return;
}
astr->monoDynSplit->stop();
astr->monoDynSplit->unbind(bstr.monoStream);
if (astr->running) {
astr->monoDynSplit->start();
}
delete stream;
return;
}
}
void unbindFromStreamStereo(std::string name, dsp::stream<dsp::StereoFloat_t>* stream) {
AudioStream_t* astr = streams[name];
for (int i = 0; i < astr->boundStreams.size(); i++) {
BoundStream_t bstr = astr->boundStreams[i];
if (bstr.stereoStream != stream) {
continue;
}
if (astr->type == STREAM_TYPE_MONO) {
astr->monoDynSplit->stop();
astr->monoDynSplit->unbind(bstr.monoStream);
if (astr->running) {
astr->monoDynSplit->start();
}
bstr.m2s->stop();
delete bstr.m2s;
return;
}
astr->stereoDynSplit->stop();
astr->stereoDynSplit->unbind(bstr.stereoStream);
if (astr->running) {
astr->stereoDynSplit->start();
}
delete stream;
return;
}
}
std::string getNameFromVFO(std::string vfoName) {
for (auto const& [name, stream] : streams) {
if (stream->vfoName == vfoName) {
return name;
}
}
return "";
}
void setSampleRate(std::string name, float sampleRate) {
AudioStream_t* astr = streams[name];
if (astr->running) {
return;
}
int blockSize = astr->sampleRateChangeHandler(astr->ctx, sampleRate);
astr->audio->setSampleRate(sampleRate);
astr->audio->setBlockSize(blockSize);
if (astr->type == STREAM_TYPE_MONO) {
astr->monoDynSplit->setBlockSize(blockSize);
for (int i = 0; i < astr->boundStreams.size(); i++) {
BoundStream_t bstr = astr->boundStreams[i];
if (bstr.type == STREAM_TYPE_STEREO) {
bstr.m2s->stop();
bstr.m2s->setBlockSize(blockSize);
bstr.sampleRateChangeHandler(bstr.ctx, sampleRate, blockSize);
bstr.m2s->start();
continue;
}
bstr.sampleRateChangeHandler(bstr.ctx, sampleRate, blockSize);
}
}
else {
astr->stereoDynSplit->setBlockSize(blockSize);
for (int i = 0; i < astr->boundStreams.size(); i++) {
BoundStream_t bstr = astr->boundStreams[i];
if (bstr.type == STREAM_TYPE_MONO) {
bstr.s2m->stop();
bstr.s2m->setBlockSize(blockSize);
bstr.sampleRateChangeHandler(bstr.ctx, sampleRate, blockSize);
bstr.s2m->start();
continue;
}
bstr.sampleRateChangeHandler(bstr.ctx, sampleRate, blockSize);
}
}
}
void setAudioDevice(std::string name, int deviceId, float sampleRate) {
AudioStream_t* astr = streams[name];
if (astr->running) {
return;
}
astr->deviceId = deviceId;
astr->audio->setDevice(deviceId);
setSampleRate(name, sampleRate);
}
std::vector<std::string> getStreamNameList() {
std::vector<std::string> list;
for (auto [name, stream] : streams) {
list.push_back(name);
}
return list;
}
};

View File

@ -0,0 +1,64 @@
#pragma once
#include <dsp/stream.h>
#include <dsp/routing.h>
#include <io/audio.h>
#include <map>
#include <watcher.h>
namespace audio {
enum {
STREAM_TYPE_MONO,
STREAM_TYPE_STEREO,
_STREAM_TYPE_COUNT
};
struct BoundStream_t {
dsp::stream<float>* monoStream;
dsp::stream<dsp::StereoFloat_t>* stereoStream;
dsp::StereoToMono* s2m;
dsp::MonoToStereo* m2s;
void (*streamRemovedHandler)(void* ctx);
void (*sampleRateChangeHandler)(void* ctx, float sampleRate, int blockSize);
void* ctx;
int type;
};
struct AudioStream_t {
io::AudioSink* audio;
dsp::stream<float>* monoAudioStream;
dsp::stream<dsp::StereoFloat_t>* stereoAudioStream;
std::vector<BoundStream_t> boundStreams;
dsp::stream<float>* monoStream;
dsp::DynamicSplitter<float>* monoDynSplit;
dsp::stream<dsp::StereoFloat_t>* stereoStream;
dsp::DynamicSplitter<dsp::StereoFloat_t>* stereoDynSplit;
int (*sampleRateChangeHandler)(void* ctx, float sampleRate);
float sampleRate;
int blockSize;
int type;
bool running = false;
float volume;
int sampleRateId;
int deviceId;
void* ctx;
std::string vfoName;
};
extern std::map<std::string, AudioStream_t*> streams;
float registerMonoStream(dsp::stream<float>* stream, std::string name, std::string vfoName, int (*sampleRateChangeHandler)(void* ctx, float sampleRate), void* ctx);
float registerStereoStream(dsp::stream<dsp::StereoFloat_t>* stream, std::string name, std::string vfoName, int (*sampleRateChangeHandler)(void* ctx, float sampleRate), void* ctx);
void startStream(std::string name);
void stopStream(std::string name);
void removeStream(std::string name);
dsp::stream<float>* bindToStreamMono(std::string name, void (*streamRemovedHandler)(void* ctx), void (*sampleRateChangeHandler)(void* ctx, float sampleRate, int blockSize), void* ctx);
dsp::stream<dsp::StereoFloat_t>* bindToStreamStereo(std::string name, void (*streamRemovedHandler)(void* ctx), void (*sampleRateChangeHandler)(void* ctx, float sampleRate, int blockSize), void* ctx);
void setBlockSize(std::string name, int blockSize);
void unbindFromStreamMono(std::string name, dsp::stream<float>* stream);
void unbindFromStreamStereo(std::string name, dsp::stream<dsp::StereoFloat_t>* stream);
std::string getNameFromVFO(std::string vfoName);
void setSampleRate(std::string name, float sampleRate);
void setAudioDevice(std::string name, int deviceId, float sampleRate);
std::vector<std::string> getStreamNameList();
};

View File

@ -0,0 +1,99 @@
#include <signal_path/dsp.h>
SignalPath::SignalPath() {
}
void SignalPath::init(uint64_t sampleRate, int fftRate, int fftSize, dsp::stream<dsp::complex_t>* input, dsp::complex_t* fftBuffer, void fftHandler(dsp::complex_t*)) {
this->sampleRate = sampleRate;
this->fftRate = fftRate;
this->fftSize = fftSize;
inputBlockSize = sampleRate / 200.0f;
dcBiasRemover.init(input, 32000);
dcBiasRemover.bypass = true;
split.init(&dcBiasRemover.output, 32000);
fftBlockDec.init(&split.output_a, (sampleRate / fftRate) - fftSize, fftSize);
fftHandlerSink.init(&fftBlockDec.output, fftBuffer, fftSize, fftHandler);
dynSplit.init(&split.output_b, 32000);
}
void SignalPath::setSampleRate(float sampleRate) {
this->sampleRate = sampleRate;
inputBlockSize = sampleRate / 200.0f;
dcBiasRemover.stop();
split.stop();
fftBlockDec.stop();
fftHandlerSink.stop();
dynSplit.stop();
for (auto const& [name, vfo] : vfos) {
vfo.vfo->stop();
}
dcBiasRemover.setBlockSize(inputBlockSize);
split.setBlockSize(inputBlockSize);
int skip = (sampleRate / fftRate) - fftSize;
fftBlockDec.setSkip(skip);
dynSplit.setBlockSize(inputBlockSize);
mod::broadcastEvent(mod::EVENT_STREAM_PARAM_CHANGED);
for (auto const& [name, vfo] : vfos) {
vfo.vfo->setInputSampleRate(sampleRate, inputBlockSize);
vfo.vfo->start();
}
fftHandlerSink.start();
fftBlockDec.start();
split.start();
dcBiasRemover.start();
dynSplit.start();
}
void SignalPath::start() {
dcBiasRemover.start();
split.start();
fftBlockDec.start();
fftHandlerSink.start();
dynSplit.start();
}
void SignalPath::setDCBiasCorrection(bool enabled) {
dcBiasRemover.bypass = !enabled;
}
dsp::VFO* SignalPath::addVFO(std::string name, float outSampleRate, float bandwidth, float offset) {
if (vfos.find(name) != vfos.end()) {
return NULL;
}
dynSplit.stop();
VFO_t vfo;
vfo.inputStream = new dsp::stream<dsp::complex_t>(inputBlockSize * 2);
dynSplit.bind(vfo.inputStream);
vfo.vfo = new dsp::VFO();
vfo.vfo->init(vfo.inputStream, sampleRate, outSampleRate, bandwidth, offset, inputBlockSize);
vfo.vfo->start();
vfos[name] = vfo;
dynSplit.start();
return vfo.vfo;
}
void SignalPath::removeVFO(std::string name) {
if (vfos.find(name) == vfos.end()) {
return;
}
dynSplit.stop();
VFO_t vfo = vfos[name];
vfo.vfo->stop();
dynSplit.unbind(vfo.inputStream);
delete vfo.vfo;
delete vfo.inputStream;
dynSplit.start();
vfos.erase(name);
}

View File

@ -0,0 +1,47 @@
#pragma once
#include <dsp/filter.h>
#include <dsp/resampling.h>
#include <dsp/source.h>
#include <dsp/math.h>
#include <dsp/demodulator.h>
#include <dsp/routing.h>
#include <dsp/sink.h>
#include <dsp/correction.h>
#include <dsp/vfo.h>
#include <io/audio.h>
#include <map>
#include <module.h>
class SignalPath {
public:
SignalPath();
void init(uint64_t sampleRate, int fftRate, int fftSize, dsp::stream<dsp::complex_t>* input, dsp::complex_t* fftBuffer, void fftHandler(dsp::complex_t*));
void start();
void setSampleRate(float sampleRate);
void setDCBiasCorrection(bool enabled);
void setFFTRate(float rate);
dsp::VFO* addVFO(std::string name, float outSampleRate, float bandwidth, float offset);
void removeVFO(std::string name);
private:
struct VFO_t {
dsp::stream<dsp::complex_t>* inputStream;
dsp::VFO* vfo;
};
dsp::DCBiasRemover dcBiasRemover;
dsp::Splitter split;
// FFT
dsp::BlockDecimator fftBlockDec;
dsp::HandlerSink fftHandlerSink;
// VFO
dsp::DynamicSplitter<dsp::complex_t> dynSplit;
std::map<std::string, VFO_t> vfos;
float sampleRate;
float fftRate;
int fftSize;
int inputBlockSize;
};

View File

@ -0,0 +1,6 @@
#include <signal_path/signal_path.h>
namespace sigpath {
SignalPath signalPath;
VFOManager vfoManager;
};

View File

@ -0,0 +1,9 @@
#pragma once
#include <signal_path/dsp.h>
#include <signal_path/vfo_manager.h>
#include <module.h>
namespace sigpath {
SDRPP_EXPORT SignalPath signalPath;
SDRPP_EXPORT VFOManager vfoManager;
};

View File

@ -0,0 +1,128 @@
#include <signal_path/vfo_manager.h>
#include <signal_path/signal_path.h>
VFOManager::VFO::VFO(std::string name, int reference, float offset, float bandwidth, float sampleRate, int blockSize) {
this->name = name;
dspVFO = sigpath::signalPath.addVFO(name, sampleRate, bandwidth, offset);
wtfVFO = new ImGui::WaterfallVFO;
wtfVFO->setReference(reference);
wtfVFO->setBandwidth(bandwidth);
wtfVFO->setOffset(offset);
output = dspVFO->output;
printf("Created VFO: %p", wtfVFO);
gui::waterfall.vfos[name] = wtfVFO;
}
VFOManager::VFO::~VFO() {
gui::waterfall.vfos.erase(name);
sigpath::signalPath.removeVFO(name);
delete wtfVFO;
}
void VFOManager::VFO::setOffset(float offset) {
wtfVFO->setOffset(offset);
dspVFO->setOffset(wtfVFO->centerOffset);
}
void VFOManager::VFO::setCenterOffset(float offset) {
wtfVFO->setCenterOffset(offset);
dspVFO->setOffset(offset);
}
void VFOManager::VFO::setBandwidth(float bandwidth) {
wtfVFO->setBandwidth(bandwidth);
dspVFO->setBandwidth(bandwidth);
}
void VFOManager::VFO::setSampleRate(float sampleRate, float bandwidth) {
dspVFO->setOutputSampleRate(sampleRate, bandwidth);
wtfVFO->setBandwidth(bandwidth);
}
void VFOManager::VFO::setReference(int ref) {
wtfVFO->setReference(ref);
}
int VFOManager::VFO::getOutputBlockSize() {
return dspVFO->getOutputBlockSize();
}
VFOManager::VFOManager() {
}
VFOManager::VFO* VFOManager::createVFO(std::string name, int reference, float offset, float bandwidth, float sampleRate, int blockSize) {
if (vfos.find(name) != vfos.end() || name == "") {
return NULL;
}
VFOManager::VFO* vfo = new VFO(name, reference, offset, bandwidth, sampleRate, blockSize);
vfos[name] = vfo;
return vfo;
}
void VFOManager::deleteVFO(VFOManager::VFO* vfo) {
std::string name = "";
for (auto const& [_name, _vfo] : vfos) {
if (_vfo == vfo) {
name == _name;
break;
}
}
if (name == "") {
return;
}
vfos.erase(name);
}
void VFOManager::setOffset(std::string name, float offset) {
if (vfos.find(name) == vfos.end()) {
return;
}
vfos[name]->setOffset(offset);
}
void VFOManager::setCenterOffset(std::string name, float offset) {
if (vfos.find(name) == vfos.end()) {
return;
}
vfos[name]->setCenterOffset(offset);
}
void VFOManager::setBandwidth(std::string name, float bandwidth) {
if (vfos.find(name) == vfos.end()) {
return;
}
vfos[name]->setBandwidth(bandwidth);
}
void VFOManager::setSampleRate(std::string name, float sampleRate, float bandwidth) {
if (vfos.find(name) == vfos.end()) {
return;
}
vfos[name]->setSampleRate(sampleRate, bandwidth);
}
void VFOManager::setReference(std::string name, int ref) {
if (vfos.find(name) == vfos.end()) {
return;
}
vfos[name]->setReference(ref);
}
int VFOManager::getOutputBlockSize(std::string name) {
if (vfos.find(name) == vfos.end()) {
return -1;
}
return vfos[name]->getOutputBlockSize();
}
void VFOManager::updateFromWaterfall(ImGui::WaterFall* wtf) {
for (auto const& [name, vfo] : vfos) {
if (vfo->wtfVFO->centerOffsetChanged) {
spdlog::info("UH OH: Change!");
vfo->wtfVFO->centerOffsetChanged = false;
vfo->dspVFO->setOffset(vfo->wtfVFO->centerOffset);
}
}
}

View File

@ -0,0 +1,47 @@
#pragma once
#include <dsp/vfo.h>
#include <gui/waterfall.h>
#include <gui/gui.h>
class VFOManager {
public:
VFOManager();
class VFO {
public:
VFO(std::string name, int reference, float offset, float bandwidth, float sampleRate, int blockSize);
~VFO();
void setOffset(float offset);
void setCenterOffset(float offset);
void setBandwidth(float bandwidth);
void setSampleRate(float sampleRate, float bandwidth);
void setReference(int ref);
int getOutputBlockSize();
dsp::stream<dsp::complex_t>* output;
friend class VFOManager;
private:
std::string name;
dsp::VFO* dspVFO;
ImGui::WaterfallVFO* wtfVFO;
};
VFOManager::VFO* createVFO(std::string name, int reference, float offset, float bandwidth, float sampleRate, int blockSize);
void deleteVFO(VFOManager::VFO* vfo);
void setOffset(std::string name, float offset);
void setCenterOffset(std::string name, float offset);
void setBandwidth(std::string name, float bandwidth);
void setSampleRate(std::string name, float sampleRate, float bandwidth);
void setReference(std::string name, int ref);
int getOutputBlockSize(std::string name);
void updateFromWaterfall(ImGui::WaterFall* wtf);
private:
std::map<std::string, VFO*> vfos;
};