From 0b276bed1d410b6fe8108d2b2632e114048e6ad0 Mon Sep 17 00:00:00 2001 From: Ryzerth Date: Sun, 9 May 2021 03:06:57 +0200 Subject: [PATCH] Fixed weird audio glitch on some AM station --- audio_sink/src/main.cpp | 7 +++ core/src/dsp/demodulator.h | 6 +- core/src/dsp/processing.h | 88 --------------------------- core/src/gui/menus/module_manager.cpp | 19 ++++-- 4 files changed, 24 insertions(+), 96 deletions(-) diff --git a/audio_sink/src/main.cpp b/audio_sink/src/main.cpp index 93a2624d..4b48d8f6 100644 --- a/audio_sink/src/main.cpp +++ b/audio_sink/src/main.cpp @@ -199,6 +199,13 @@ private: int count = _this->stereoPacker.out.read(); if (count < 0) { return 0; } if (nBufferFrames != count) { spdlog::warn("Buffer size missmatch, wanted {0}, was asked for {1}", count, nBufferFrames); } + + for (int i = 0; i < count; i++) { + if (_this->stereoPacker.out.readBuf[i].l == NAN || _this->stereoPacker.out.readBuf[i].r == NAN) { spdlog::error("NAN in audio data"); } + if (_this->stereoPacker.out.readBuf[i].l == INFINITY || _this->stereoPacker.out.readBuf[i].r == INFINITY) { spdlog::error("INFINITY in audio data"); } + if (_this->stereoPacker.out.readBuf[i].l == -INFINITY || _this->stereoPacker.out.readBuf[i].r == -INFINITY) { spdlog::error("-INFINITY in audio data"); } + } + memcpy(outputBuffer, _this->stereoPacker.out.readBuf, nBufferFrames * sizeof(dsp::stereo_t)); _this->stereoPacker.out.flush(); return 0; diff --git a/core/src/dsp/demodulator.h b/core/src/dsp/demodulator.h index 26dd0404..3c5ea4c1 100644 --- a/core/src/dsp/demodulator.h +++ b/core/src/dsp/demodulator.h @@ -213,12 +213,9 @@ namespace dsp { _in->flush(); - float avg; - volk_32f_accumulator_s32f(&avg, out.writeBuf, count); - avg /= (float)count; - for (int i = 0; i < count; i++) { out.writeBuf[i] -= avg; + avg += out.writeBuf[i] * 10e-4; } if (!out.swap(count)) { return -1; } @@ -229,6 +226,7 @@ namespace dsp { private: stream* _in; + float avg = 0; }; diff --git a/core/src/dsp/processing.h b/core/src/dsp/processing.h index fce713ac..87e95734 100644 --- a/core/src/dsp/processing.h +++ b/core/src/dsp/processing.h @@ -147,94 +147,6 @@ namespace dsp { }; - template - class FeedForwardAGC : public generic_block> { - public: - FeedForwardAGC() {} - - FeedForwardAGC(stream* in) { init(in); } - - ~FeedForwardAGC() { - generic_block>::stop(); - delete[] buffer; - } - - void init(stream* in) { - _in = in; - buffer = new T[STREAM_BUFFER_SIZE]; - generic_block>::registerInput(_in); - generic_block>::registerOutput(&out); - } - - void setInput(stream* in) { - std::lock_guard lck(generic_block>::ctrlMtx); - generic_block>::tempStop(); - generic_block>::unregisterInput(_in); - _in = in; - generic_block>::registerInput(_in); - generic_block>::tempStart(); - } - - int run() { - int count = _in->read(); - if (count < 0) { return -1; } - - float level; - float val; - - // Process buffer - memcpy(&buffer[inBuffer], _in->readBuf, count * sizeof(T)); - inBuffer += count; - - // If there aren't enough samples, wait for more - if (inBuffer < sampleCount) { - _in->flush(); - return count; - } - - int toProcess = (inBuffer - sampleCount) + 1; - - if constexpr (std::is_same_v) { - for (int i = 0; i < toProcess; i++) { - level = 1e-4; - for (int j = 0; j < sampleCount; j++) { - val = fabsf(buffer[i + j]); - if (val > level) { level = val; } - } - out.writeBuf[i] = buffer[i] / level; - } - } - if constexpr (std::is_same_v) { - for (int i = 0; i < toProcess; i++) { - level = 1e-4; - for (int j = 0; j < sampleCount; j++) { - val = buffer[i + j].fastAmplitude(); - if (val > level) { level = val; } - } - out.writeBuf[i] = buffer[i] / level; - } - } - - _in->flush(); - - // Move rest of buffer - memmove(buffer, &buffer[toProcess], (sampleCount - 1) * sizeof(T)); - inBuffer -= toProcess; - - if (!out.swap(count)) { return -1; } - return toProcess; - } - - stream out; - - private: - T* buffer; - int inBuffer = 0; - int sampleCount = 1024; - stream* _in; - - }; - class ComplexAGC : public generic_block { public: ComplexAGC() {} diff --git a/core/src/gui/menus/module_manager.cpp b/core/src/gui/menus/module_manager.cpp index a15630ab..c64c99e1 100644 --- a/core/src/gui/menus/module_manager.cpp +++ b/core/src/gui/menus/module_manager.cpp @@ -32,8 +32,11 @@ namespace module_manager_menu { ImGui::BeginTable("Module Manager Table", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg); ImGui::TableSetupColumn("Name"); ImGui::TableSetupColumn("Type"); - ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 16); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 10); ImGui::TableHeadersRow(); + + float height = ImGui::CalcTextSize("-").y; + for (auto& [name, inst] : core::moduleManager.instances) { // TEMPORARY EXCLUSION FOR SOURCES AND SINKS std::string type = inst.module.info->name; @@ -51,12 +54,21 @@ namespace module_manager_menu { ImGui::Text(inst.module.info->name); ImGui::TableSetColumnIndex(2); - if (ImGui::Button(("-##module_mgr_"+name).c_str(), ImVec2(16,0))) { + ImVec2 origPos = ImGui::GetCursorPos(); + ImGui::SetCursorPos(ImVec2(origPos.x - 3, origPos.y)); + if (ImGui::Button(("##module_mgr_"+name).c_str(), ImVec2(height,height))) { core::moduleManager.deleteInstance(name); } + ImGui::SetCursorPos(ImVec2(origPos.x + 2, origPos.y - 5)); + ImGui::Text("_"); } + ImGui::EndTable(); - // Add module row + // Add module row with slightly different settings + ImGui::BeginTable("Module Manager Add Table", 3); + ImGui::TableSetupColumn("Name"); + ImGui::TableSetupColumn("Type"); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 16); ImGui::TableNextRow(); ImGui::TableSetColumnIndex(0); @@ -73,7 +85,6 @@ namespace module_manager_menu { core::moduleManager.createInstance(modName, modTypes[modTypeId]); } if (strlen(modName) == 0) { style::endDisabled(); } - ImGui::EndTable(); } } \ No newline at end of file