New stuff lmao

This commit is contained in:
Ryzerth
2020-08-20 18:29:23 +02:00
parent 649be86cb6
commit 709627a738
15 changed files with 750 additions and 108 deletions

View File

@ -11,8 +11,6 @@ struct RadioContext_t {
std::string name;
int demod = 1;
SigPath sigPath;
// watcher<float> volume;
// watcher<int> audioDevice;
};
MOD_EXPORT void* _INIT_(mod::API_t* _API, ImGuiContext* imctx, std::string _name) {
@ -21,24 +19,12 @@ MOD_EXPORT void* _INIT_(mod::API_t* _API, ImGuiContext* imctx, std::string _name
ctx->name = _name;
ctx->sigPath.init(_name, 200000, 1000, API->registerVFO(_name, mod::API_t::REF_CENTER, 0, 200000, 200000, 1000));
ctx->sigPath.start();
// ctx->volume.val = 1.0f;
// ctx->volume.markAsChanged();
// API->bindVolumeVariable(&ctx->volume.val);
// ctx->audioDevice.val = ctx->sigPath.audio.getDeviceId();
// ctx->audioDevice.changed(); // clear change
ImGui::SetCurrentContext(imctx);
return ctx;
}
MOD_EXPORT void _NEW_FRAME_(RadioContext_t* ctx) {
// if (ctx->volume.changed()) {
// ctx->sigPath.setVolume(ctx->volume.val);
// }
// if (ctx->audioDevice.changed()) {
// ctx->sigPath.audio.stop();
// ctx->sigPath.audio.setDevice(ctx->audioDevice.val);
// ctx->sigPath.audio.start();
// }
}
MOD_EXPORT void _DRAW_MENU_(RadioContext_t* ctx) {
@ -48,28 +34,28 @@ MOD_EXPORT void _DRAW_MENU_(RadioContext_t* ctx) {
if (ImGui::RadioButton(CONCAT("NFM##_", ctx->name), ctx->demod == 0) && ctx->demod != 0) {
ctx->sigPath.setDemodulator(SigPath::DEMOD_NFM);
ctx->demod = 0;
API->setVFOBandwidth(ctx->name, 12500);
API->setVFOReference(ctx->name, mod::API_t::REF_CENTER);
}
if (ImGui::RadioButton(CONCAT("WFM##_", ctx->name), ctx->demod == 1) && ctx->demod != 1) {
ctx->sigPath.setDemodulator(SigPath::DEMOD_FM);
ctx->demod = 1;
API->setVFOBandwidth(ctx->name, 200000);
API->setVFOReference(ctx->name, mod::API_t::REF_CENTER);
}
ImGui::NextColumn();
if (ImGui::RadioButton(CONCAT("AM##_", ctx->name), ctx->demod == 2) && ctx->demod != 2) {
ctx->sigPath.setDemodulator(SigPath::DEMOD_AM);
ctx->demod = 2;
API->setVFOBandwidth(ctx->name, 12500);
API->setVFOReference(ctx->name, mod::API_t::REF_CENTER);
}
if (ImGui::RadioButton(CONCAT("DSB##_", ctx->name), ctx->demod == 3) && ctx->demod != 3) { ctx->demod = 3; };
if (ImGui::RadioButton(CONCAT("DSB##_", ctx->name), ctx->demod == 3) && ctx->demod != 3) {
ctx->sigPath.setDemodulator(SigPath::DEMOD_DSB);
ctx->demod = 3;
API->setVFOReference(ctx->name, mod::API_t::REF_CENTER);
}
ImGui::NextColumn();
if (ImGui::RadioButton(CONCAT("USB##_", ctx->name), ctx->demod == 4) && ctx->demod != 4) {
ctx->sigPath.setDemodulator(SigPath::DEMOD_USB);
ctx->demod = 4;
API->setVFOBandwidth(ctx->name, 3000);
API->setVFOReference(ctx->name, mod::API_t::REF_LOWER);
}
if (ImGui::RadioButton(CONCAT("CW##_", ctx->name), ctx->demod == 5) && ctx->demod != 5) { ctx->demod = 5; };
@ -77,7 +63,6 @@ MOD_EXPORT void _DRAW_MENU_(RadioContext_t* ctx) {
if (ImGui::RadioButton(CONCAT("LSB##_", ctx->name), ctx->demod == 6) && ctx->demod != 6) {
ctx->sigPath.setDemodulator(SigPath::DEMOD_LSB);
ctx->demod = 6;
API->setVFOBandwidth(ctx->name, 3000);
API->setVFOReference(ctx->name, mod::API_t::REF_UPPER);
}
if (ImGui::RadioButton(CONCAT("RAW##_", ctx->name), ctx->demod == 7) && ctx->demod != 7) { ctx->demod = 7; };
@ -85,21 +70,11 @@ MOD_EXPORT void _DRAW_MENU_(RadioContext_t* ctx) {
ImGui::EndGroup();
// ImGui::PushItemWidth(ImGui::GetWindowSize().x);
// ImGui::Combo(CONCAT("##_audio_dev_", ctx->name), &ctx->audioDevice.val, ctx->sigPath.audio.devTxtList.c_str());
// ImGui::PopItemWidth();
ImGui::Checkbox(CONCAT("Deemphasis##_", ctx->name), &ctx->sigPath.deemp.bypass);
}
MOD_EXPORT void _HANDLE_EVENT_(RadioContext_t* ctx, int eventId) {
// INSTEAD OF EVENTS, REGISTER HANDLER WHEN CREATING VFO
if (eventId == mod::EVENT_STREAM_PARAM_CHANGED) {
ctx->sigPath.updateBlockSize();
}
else if (eventId == mod::EVENT_SELECTED_VFO_CHANGED) {
// if (API->getSelectedVFOName() == ctx->name) {
// API->bindVolumeVariable(&ctx->volume.val);
// }
}
}
MOD_EXPORT void _STOP_(RadioContext_t* ctx) {

View File

@ -6,9 +6,16 @@ SigPath::SigPath() {
int SigPath::sampleRateChangeHandler(void* ctx, float sampleRate) {
SigPath* _this = (SigPath*)ctx;
_this->outputSampleRate = sampleRate;
_this->audioResamp.stop();
_this->audioResamp.setOutputSampleRate(sampleRate, sampleRate / 2.0f, sampleRate / 2.0f);
_this->deemp.stop();
float bw = std::min<float>(_this->bandwidth, sampleRate / 2.0f);
spdlog::warn("New bandwidth: {0}", bw);
_this->audioResamp.setOutputSampleRate(sampleRate, bw, bw);
_this->deemp.setBlockSize(_this->audioResamp.getOutputBlockSize());
_this->deemp.setSamplerate(sampleRate);
_this->audioResamp.start();
_this->deemp.start();
return _this->audioResamp.getOutputBlockSize();
}
@ -18,6 +25,7 @@ void SigPath::init(std::string vfoName, uint64_t sampleRate, int blockSize, dsp:
this->vfoName = vfoName;
_demod = DEMOD_FM;
bandwidth = 200000;
// TODO: Set default VFO options
@ -26,8 +34,11 @@ void SigPath::init(std::string vfoName, uint64_t sampleRate, int blockSize, dsp:
ssbDemod.init(input, 6000, 3000, 22);
audioResamp.init(&demod.output, 200000, 48000, 800);
API->registerMonoStream(&audioResamp.output, vfoName, vfoName, sampleRateChangeHandler, this);
deemp.init(&audioResamp.output, 800, 50e-6, 48000);
outputSampleRate = API->registerMonoStream(&deemp.output, vfoName, vfoName, sampleRateChangeHandler, this);
API->setBlockSize(vfoName, audioResamp.getOutputBlockSize());
setDemodulator(_demod);
}
void SigPath::setSampleRate(float sampleRate) {
@ -43,6 +54,7 @@ void SigPath::setDemodulator(int demId) {
}
audioResamp.stop();
deemp.stop();
// Stop current demodulator
if (_demod == DEMOD_FM) {
@ -65,47 +77,76 @@ void SigPath::setDemodulator(int demId) {
// Set input of the audio resampler
if (demId == DEMOD_FM) {
API->setVFOSampleRate(vfoName, 200000, 200000);
bandwidth = 15000;
demod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
demod.setSampleRate(200000);
demod.setDeviation(100000);
audioResamp.setInput(&demod.output);
audioResamp.setInputSampleRate(200000, API->getVFOOutputBlockSize(vfoName), 15000, 15000);
float audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(200000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
deemp.bypass = false;
demod.start();
}
if (demId == DEMOD_NFM) {
API->setVFOSampleRate(vfoName, 12500, 12500);
API->setVFOSampleRate(vfoName, 16000, 16000);
bandwidth = 8000;
demod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
demod.setSampleRate(12500);
demod.setDeviation(6250);
demod.setSampleRate(16000);
demod.setDeviation(8000);
audioResamp.setInput(&demod.output);
audioResamp.setInputSampleRate(12500, API->getVFOOutputBlockSize(vfoName));
float audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(16000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
deemp.bypass = true;
demod.start();
}
else if (demId == DEMOD_AM) {
API->setVFOSampleRate(vfoName, 12500, 12500);
bandwidth = 6250;
amDemod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
audioResamp.setInput(&amDemod.output);
audioResamp.setInputSampleRate(12500, API->getVFOOutputBlockSize(vfoName));
float audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(12500, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
deemp.bypass = true;
amDemod.start();
}
else if (demId == DEMOD_USB) {
API->setVFOSampleRate(vfoName, 6000, 3000);
bandwidth = 3000;
ssbDemod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
ssbDemod.setMode(dsp::SSBDemod::MODE_USB);
audioResamp.setInput(&ssbDemod.output);
audioResamp.setInputSampleRate(6000, API->getVFOOutputBlockSize(vfoName));
float audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(6000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
deemp.bypass = true;
ssbDemod.start();
}
else if (demId == DEMOD_LSB) {
API->setVFOSampleRate(vfoName, 6000, 3000);
bandwidth = 3000;
ssbDemod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
ssbDemod.setMode(dsp::SSBDemod::MODE_LSB);
audioResamp.setInput(&ssbDemod.output);
audioResamp.setInputSampleRate(6000, API->getVFOOutputBlockSize(vfoName));
float audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(6000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
deemp.bypass = true;
ssbDemod.start();
}
else if (demId == DEMOD_DSB) {
API->setVFOSampleRate(vfoName, 6000, 6000);
bandwidth = 3000;
ssbDemod.setBlockSize(API->getVFOOutputBlockSize(vfoName));
ssbDemod.setMode(dsp::SSBDemod::MODE_DSB);
audioResamp.setInput(&ssbDemod.output);
float audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(6000, API->getVFOOutputBlockSize(vfoName), audioBw, audioBw);
deemp.bypass = true;
ssbDemod.start();
}
deemp.setBlockSize(audioResamp.getOutputBlockSize());
audioResamp.start();
deemp.start();
}
void SigPath::updateBlockSize() {
@ -115,5 +156,6 @@ void SigPath::updateBlockSize() {
void SigPath::start() {
demod.start();
audioResamp.start();
deemp.start();
API->startStream(vfoName);
}

View File

@ -18,7 +18,7 @@ public:
void start();
void setSampleRate(float sampleRate);
void setVFOFrequency(long frequency);
void setVFOFrequency(uint64_t frequency);
void updateBlockSize();
@ -30,9 +30,12 @@ public:
DEMOD_AM,
DEMOD_USB,
DEMOD_LSB,
DEMOD_DSB,
_DEMOD_COUNT
};
dsp::FMDeemphasis deemp;
private:
static int sampleRateChangeHandler(void* ctx, float sampleRate);
@ -49,6 +52,8 @@ private:
std::string vfoName;
float sampleRate;
float bandwidth;
float outputSampleRate;
int blockSize;
int _demod;
};

View File

@ -21,6 +21,8 @@ struct RecorderContext_t {
std::string lastNameList;
std::string selectedStreamName;
int selectedStreamId;
uint64_t samplesWritten;
float sampleRate;
};
void _writeWorker(RecorderContext_t* ctx) {
@ -34,6 +36,7 @@ void _writeWorker(RecorderContext_t* ctx) {
sampleBuf[(i * 2) + 0] = floatBuf[i].l * 0x7FFF;
sampleBuf[(i * 2) + 1] = floatBuf[i].r * 0x7FFF;
}
ctx->samplesWritten += 1024;
ctx->writer->writeSamples(sampleBuf, 2048 * sizeof(int16_t));
}
delete[] floatBuf;
@ -60,6 +63,9 @@ MOD_EXPORT void* _INIT_(mod::API_t* _API, ImGuiContext* imctx, std::string _name
API = _API;
RecorderContext_t* ctx = new RecorderContext_t;
ctx->recording = false;
ctx->selectedStreamName = "";
ctx->selectedStreamId = 0;
ctx->lastNameList = "";
ImGui::SetCurrentContext(imctx);
return ctx;
}
@ -78,14 +84,30 @@ MOD_EXPORT void _DRAW_MENU_(RecorderContext_t* ctx) {
nameList += '\0';
}
if (nameList == "") {
ImGui::Text("No audio stream available");
return;
}
if (ctx->lastNameList != nameList) {
ctx->lastNameList = nameList;
auto _nameIt = std::find(streamNames.begin(), streamNames.end(), ctx->selectedStreamName);
if (_nameIt == streamNames.end()) {
// TODO: verify if there even is a stream
ctx->selectedStreamId = 0;
ctx->selectedStreamName = streamNames[ctx->selectedStreamId];
}
else {
ctx->selectedStreamId = std::distance(streamNames.begin(), _nameIt);
ctx->selectedStreamName = streamNames[ctx->selectedStreamId];
}
}
ImGui::PushItemWidth(menuColumnWidth);
if (!ctx->recording) {
ImGui::Combo(CONCAT("##_strea_select_", ctx->name), &ctx->selectedStreamId, nameList.c_str());
if (ImGui::Combo(CONCAT("##_strea_select_", ctx->name), &ctx->selectedStreamId, nameList.c_str())) {
ctx->selectedStreamName = nameList[ctx->selectedStreamId];
}
}
else {
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
@ -99,8 +121,10 @@ MOD_EXPORT void _DRAW_MENU_(RecorderContext_t* ctx) {
if (!ctx->recording) {
if (ImGui::Button("Record", ImVec2(menuColumnWidth, 0))) {
ctx->samplesWritten = 0;
ctx->sampleRate = 48000;
ctx->writer = new WavWriter("recordings/" + genFileName("audio_"), 16, 2, 48000);
ctx->stream = API->bindToStreamStereo("Radio", streamRemovedHandler, sampleRateChanged, ctx);
ctx->stream = API->bindToStreamStereo(ctx->selectedStreamName, streamRemovedHandler, sampleRateChanged, ctx);
ctx->workerThread = std::thread(_writeWorker, ctx);
ctx->recording = true;
ctx->startTime = time(0);
@ -112,12 +136,13 @@ MOD_EXPORT void _DRAW_MENU_(RecorderContext_t* ctx) {
ctx->stream->stopReader();
ctx->workerThread.join();
ctx->stream->clearReadStop();
API->unbindFromStreamStereo("Radio", ctx->stream);
API->unbindFromStreamStereo(ctx->selectedStreamName, ctx->stream);
ctx->writer->close();
delete ctx->writer;
ctx->recording = false;
}
time_t diff = time(0) - ctx->startTime;
uint64_t seconds = ctx->samplesWritten / (uint64_t)ctx->sampleRate;
time_t diff = seconds;
tm *dtm = gmtime(&diff);
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Recording %02d:%02d:%02d", dtm->tm_hour, dtm->tm_min, dtm->tm_sec);
}