From fb32b4d55a20a8cd13285357a004f743911cc59e Mon Sep 17 00:00:00 2001 From: Ryzerth Date: Wed, 21 Jul 2021 04:08:28 +0200 Subject: [PATCH] Made the file source set the frequency of the waterfall --- core/src/gui/tuner.cpp | 10 ++++++++ core/src/gui/tuner.h | 3 +++ core/src/gui/widgets/frequency_select.cpp | 11 +++++++- core/src/gui/widgets/frequency_select.h | 4 +++ core/src/gui/widgets/waterfall.cpp | 12 ++++++--- core/src/gui/widgets/waterfall.h | 2 ++ file_source/src/main.cpp | 31 ++++++++++++++++++++++- 7 files changed, 67 insertions(+), 6 deletions(-) diff --git a/core/src/gui/tuner.cpp b/core/src/gui/tuner.cpp index 99896e50..e1a98b18 100644 --- a/core/src/gui/tuner.cpp +++ b/core/src/gui/tuner.cpp @@ -4,6 +4,7 @@ #include namespace tuner { + void centerTuning(std::string vfoName, double freq) { if (vfoName != "") { if (gui::waterfall.vfos.find(vfoName) == gui::waterfall.vfos.end()) { return; } @@ -110,6 +111,12 @@ namespace tuner { } } + void iqTuning(double freq) { + gui::waterfall.setCenterFrequency(freq); + gui::waterfall.centerFreqMoved = true; + sigpath::sourceManager.tune(freq); + } + void tune(int mode, std::string vfoName, double freq) { switch (mode) { case TUNER_MODE_CENTER: @@ -124,6 +131,9 @@ namespace tuner { case TUNER_MODE_UPPER_HALF: normalTuning(vfoName, freq); break; + case TUNER_MODE_IQ_ONLY: + iqTuning(freq); + break; } } } \ No newline at end of file diff --git a/core/src/gui/tuner.h b/core/src/gui/tuner.h index 568a0a4e..e8b9c0d5 100644 --- a/core/src/gui/tuner.h +++ b/core/src/gui/tuner.h @@ -1,15 +1,18 @@ #pragma once #include +#include namespace tuner { void centerTuning(std::string vfoName, double freq); void normalTuning(std::string vfoName, double freq); + void iqTuning(double freq); enum { TUNER_MODE_CENTER, TUNER_MODE_NORMAL, TUNER_MODE_LOWER_HALF, TUNER_MODE_UPPER_HALF, + TUNER_MODE_IQ_ONLY, _TUNER_MODE_COUNT }; diff --git a/core/src/gui/widgets/frequency_select.cpp b/core/src/gui/widgets/frequency_select.cpp index 069e806c..5113976e 100644 --- a/core/src/gui/widgets/frequency_select.cpp +++ b/core/src/gui/widgets/frequency_select.cpp @@ -173,6 +173,7 @@ void FrequencySelect::draw() { for (int j = i; j < 12; j++) { digits[j] = 0; } + frequencyChanged = true; } if (ImGui::IsKeyPressed(GLFW_KEY_UP)) { @@ -214,7 +215,15 @@ void FrequencySelect::draw() { for (int i = 0; i < 12; i++) { freq += digits[i] * pow(10, 11 - i); } - frequency = freq; + + uint64_t orig = freq; + freq = std::clamp(freq, minFreq, maxFreq); + if (freq != orig && limitFreq) { + setFrequency(freq); + } + else { + frequency = orig; + } ImGui::PopFont(); diff --git a/core/src/gui/widgets/frequency_select.h b/core/src/gui/widgets/frequency_select.h index 89ad3663..362ea102 100644 --- a/core/src/gui/widgets/frequency_select.h +++ b/core/src/gui/widgets/frequency_select.h @@ -14,6 +14,10 @@ public: bool frequencyChanged = false; bool digitHovered = false; + bool limitFreq; + uint64_t minFreq; + uint64_t maxFreq; + private: void onPosChange(); void onResize(); diff --git a/core/src/gui/widgets/waterfall.cpp b/core/src/gui/widgets/waterfall.cpp index b198fe1a..f33c12db 100644 --- a/core/src/gui/widgets/waterfall.cpp +++ b/core/src/gui/widgets/waterfall.cpp @@ -336,14 +336,18 @@ namespace ImGui { if (viewOffset + (viewBandwidth / 2.0) > wholeBandwidth / 2.0) { double freqOffset = (viewOffset + (viewBandwidth / 2.0)) - (wholeBandwidth / 2.0); viewOffset = (wholeBandwidth / 2.0) - (viewBandwidth / 2.0); - centerFreq += freqOffset; - centerFreqMoved = true; + if (!centerFrequencyLocked) { + centerFreq += freqOffset; + centerFreqMoved = true; + } } if (viewOffset - (viewBandwidth / 2.0) < -(wholeBandwidth / 2.0)) { double freqOffset = (viewOffset - (viewBandwidth / 2.0)) + (wholeBandwidth / 2.0); viewOffset = (viewBandwidth / 2.0) - (wholeBandwidth / 2.0); - centerFreq += freqOffset; - centerFreqMoved = true; + if (!centerFrequencyLocked) { + centerFreq += freqOffset; + centerFreqMoved = true; + } } lowerFreq = (centerFreq + viewOffset) - (viewBandwidth / 2.0); diff --git a/core/src/gui/widgets/waterfall.h b/core/src/gui/widgets/waterfall.h index e36b5727..43622811 100644 --- a/core/src/gui/widgets/waterfall.h +++ b/core/src/gui/widgets/waterfall.h @@ -141,6 +141,8 @@ namespace ImGui { float selectedVFOSNR = NAN; + bool centerFrequencyLocked = false; + std::map vfos; std::string selectedVFO = ""; bool selectedVFOChanged = false; diff --git a/file_source/src/main.cpp b/file_source/src/main.cpp index 63bfee5a..bae61592 100644 --- a/file_source/src/main.cpp +++ b/file_source/src/main.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include #define CONCAT(a, b) ((std::string(a) + b).c_str()) @@ -63,13 +66,19 @@ private: static void menuSelected(void* ctx) { FileSourceModule* _this = (FileSourceModule*)ctx; core::setInputSampleRate(_this->sampleRate); + tuner::tune(tuner::TUNER_MODE_IQ_ONLY, "", _this->centerFreq); sigpath::signalPath.setBuffering(false); + gui::waterfall.centerFrequencyLocked = true; + gui::freqSelect.minFreq = _this->centerFreq - (_this->sampleRate/2); + gui::freqSelect.maxFreq = _this->centerFreq + (_this->sampleRate/2); + gui::freqSelect.limitFreq = true; spdlog::info("FileSourceModule '{0}': Menu Select!", _this->name); } static void menuDeselected(void* ctx) { FileSourceModule* _this = (FileSourceModule*)ctx; sigpath::signalPath.setBuffering(true); + gui::waterfall.centerFrequencyLocked = false; spdlog::info("FileSourceModule '{0}': Menu Deselect!", _this->name); } @@ -112,8 +121,16 @@ private: _this->reader = new WavReader(_this->fileSelect.path); _this->sampleRate = _this->reader->getSampleRate(); core::setInputSampleRate(_this->sampleRate); + std::string filename = std::filesystem::path(_this->fileSelect.path).filename().string(); + _this->centerFreq = _this->getFrequency(filename); + tuner::tune(tuner::TUNER_MODE_IQ_ONLY, "", _this->centerFreq); + gui::freqSelect.minFreq = _this->centerFreq - (_this->sampleRate/2); + gui::freqSelect.maxFreq = _this->centerFreq + (_this->sampleRate/2); + gui::freqSelect.limitFreq = true; + } + catch (std::exception e) { + spdlog::error("Error: {0}", e.what()); } - catch (std::exception e) {} config.acquire(); config.conf["path"] = _this->fileSelect.path; config.release(true); @@ -152,6 +169,16 @@ private: delete[] inBuf; } + double getFrequency(std::string filename) { + std::regex expr("[0-9]+Hz"); + std::smatch matches; + std::regex_search(filename, matches, expr); + spdlog::warn("{0} {1}", filename, matches.size()); + if (matches.empty()) { return 0; } + std::string freqStr = matches[0].str(); + return std::atof(freqStr.substr(0, freqStr.size() - 2).c_str()); + } + FileSelect fileSelect; std::string name; dsp::stream stream; @@ -162,6 +189,8 @@ private: float sampleRate = 48000; std::thread workerThread; + double centerFreq = 0; + bool float32Mode = false; };