diff --git a/core/src/utils/new_event.h b/core/src/utils/new_event.h index 128eff99..940765a7 100644 --- a/core/src/utils/new_event.h +++ b/core/src/utils/new_event.h @@ -8,8 +8,9 @@ typedef int HandlerID; template class NewEvent { - using Handler = std::function; public: + using Handler = std::function; + HandlerID bind(const Handler& handler) { std::lock_guard lck(mtx); HandlerID id = genID(); diff --git a/source_modules/spectran_http_source/src/main.cpp b/source_modules/spectran_http_source/src/main.cpp index b221281c..8ec62131 100644 --- a/source_modules/spectran_http_source/src/main.cpp +++ b/source_modules/spectran_http_source/src/main.cpp @@ -145,6 +145,7 @@ private: } else if (connected && SmGui::Button("Disconnect##spectran_http_source")) { _this->client->onCenterFrequencyChanged.unbind(_this->onFreqChangedId); + _this->client->onCenterFrequencyChanged.unbind(_this->onSamplerateChangedId); _this->client->close(); } if (_this->running) { style::endDisabled(); } @@ -164,6 +165,7 @@ private: gotReport = false; client = std::make_shared(hostname, port, &stream); onFreqChangedId = client->onCenterFrequencyChanged.bind(&SpectranHTTPSourceModule::onFreqChanged, this); + onSamplerateChangedId = client->onSamplerateChanged.bind(&SpectranHTTPSourceModule::onSamplerateChanged, this); client->startWorker(); } catch (std::runtime_error e) { @@ -178,6 +180,10 @@ private: gotReport = true; } + void onSamplerateChanged(double newSr) { + core::setInputSampleRate(newSr); + } + std::string name; bool enabled = true; double sampleRate; @@ -186,6 +192,7 @@ private: std::shared_ptr client; HandlerID onFreqChangedId; + HandlerID onSamplerateChangedId; double freq; diff --git a/source_modules/spectran_http_source/src/spectran_http_client.cpp b/source_modules/spectran_http_source/src/spectran_http_client.cpp index 958b6798..8b886b75 100644 --- a/source_modules/spectran_http_source/src/spectran_http_client.cpp +++ b/source_modules/spectran_http_source/src/spectran_http_client.cpp @@ -50,7 +50,7 @@ void SpectranHTTPClient::setCenterFrequency(uint64_t freq) { // Make request net::http::RequestHeader rqhdr(net::http::METHOD_PUT, "/control", host); char buf[1024]; - sprintf(buf, "{\"frequencyCenter\":%d,\"frequencySpan\":%d,\"type\":\"capture\"}", freq, _span); + sprintf(buf, "{\"frequencyCenter\":%d,\"frequencySpan\":%d,\"type\":\"capture\"}", freq, _samplerate); std::string data = buf; char lenBuf[16]; sprintf(lenBuf, "%d", data.size()); @@ -93,14 +93,28 @@ void SpectranHTTPClient::worker() { std::string endFreqStr = jsonData.substr(endFreqBegin + 15, endFreqEnd - endFreqBegin - 15); int64_t endFreq = std::stoll(endFreqStr); + auto sampleFreqBegin = jsonData.find("\"sampleFrequency\":"); + bool sampleFreqReceived = (sampleFreqBegin != -1); + int64_t sampleFreq; + if (sampleFreqReceived) { + auto sampleFreqEnd = jsonData.find(',', sampleFreqBegin); + std::string sampleFreqStr = jsonData.substr(sampleFreqBegin + 18, sampleFreqEnd - sampleFreqBegin - 18); + sampleFreq = std::stoll(sampleFreqStr); + } + // Calculate and update center freq - _span = endFreq - startFreq; + int64_t samplerate = sampleFreqReceived ? sampleFreq : (endFreq - startFreq); int64_t centerFreq = round(((double)endFreq + (double)startFreq) / 2.0); if (centerFreq != _centerFreq) { - flog::debug("{}, {}, {}", _span, centerFreq); + flog::debug("New center freq: {}", centerFreq); _centerFreq = centerFreq; onCenterFrequencyChanged(centerFreq); } + if (samplerate != _samplerate) { + flog::debug("New samplerate: {}", samplerate); + _samplerate = samplerate; + onSamplerateChanged(samplerate); + } // Read (and check for) record separator uint8_t rs; diff --git a/source_modules/spectran_http_source/src/spectran_http_client.h b/source_modules/spectran_http_source/src/spectran_http_client.h index 214af854..c2fb83d6 100644 --- a/source_modules/spectran_http_source/src/spectran_http_client.h +++ b/source_modules/spectran_http_source/src/spectran_http_client.h @@ -19,6 +19,7 @@ public: void setCenterFrequency(uint64_t freq); NewEvent onCenterFrequencyChanged; + NewEvent onSamplerateChanged; private: void worker(); @@ -33,5 +34,5 @@ private: bool streamingEnabled = false; int64_t _centerFreq = 0; - uint64_t _span = 5000000; + uint64_t _samplerate = 0; }; \ No newline at end of file