diff --git a/sdrbase/device/deviceset.cpp b/sdrbase/device/deviceset.cpp index a9d0925ee..c35b952b6 100644 --- a/sdrbase/device/deviceset.cpp +++ b/sdrbase/device/deviceset.cpp @@ -470,6 +470,18 @@ int DeviceSet::webapiSpectrumDataGet(int bins, qint64 startFrequency, qint64 sto return m_spectrumVis->webapiSpectrumDataGet(bins, startFrequency, stopFrequency, reduce, response, errorMessage); } +int DeviceSet::webapiSpectrumHistoryGet(double seconds, int bins, qint64 startFrequency, qint64 stopFrequency, double thresholdDb, + SWGSDRangel::SWGGLSpectrumHistory& response, QString& errorMessage) const +{ + return m_spectrumVis->webapiSpectrumHistoryGet(seconds, bins, startFrequency, stopFrequency, thresholdDb, response, errorMessage); +} + +int DeviceSet::webapiSpectrumHistoryImageGet(double seconds, int bins, qint64 startFrequency, qint64 stopFrequency, int maxRows, + QByteArray& png, QJsonObject& description, QString& errorMessage) const +{ + return m_spectrumVis->webapiSpectrumHistoryImageGet(seconds, bins, startFrequency, stopFrequency, maxRows, png, description, errorMessage); +} + int DeviceSet::webapiSpectrumActionsPost(const QStringList& spectrumActionsKeys, SWGSDRangel::SWGSpectrumActions& query, QString& errorMessage) { return m_spectrumVis->webapiActionsPost(spectrumActionsKeys, query, errorMessage); diff --git a/sdrbase/device/deviceset.h b/sdrbase/device/deviceset.h index 869238087..09596a252 100644 --- a/sdrbase/device/deviceset.h +++ b/sdrbase/device/deviceset.h @@ -22,6 +22,7 @@ #define SDRSRV_DEVICE_DEVICESET_H_ #include +#include #include "export.h" @@ -39,6 +40,7 @@ namespace SWGSDRangel { class SWGGLSpectrumReport; class SWGSpectrumActions; class SWGGLSpectrumData; + class SWGGLSpectrumHistory; class SWGSpectrumServer; class SWGSuccessResponse; }; @@ -82,6 +84,10 @@ public: int webapiSpectrumReportGet(SWGSDRangel::SWGGLSpectrumReport& response, QString& errorMessage) const; int webapiSpectrumDataGet(int bins, qint64 startFrequency, qint64 stopFrequency, const QString& reduce, SWGSDRangel::SWGGLSpectrumData& response, QString& errorMessage) const; + int webapiSpectrumHistoryGet(double seconds, int bins, qint64 startFrequency, qint64 stopFrequency, double thresholdDb, + SWGSDRangel::SWGGLSpectrumHistory& response, QString& errorMessage) const; + int webapiSpectrumHistoryImageGet(double seconds, int bins, qint64 startFrequency, qint64 stopFrequency, int maxRows, + QByteArray& png, QJsonObject& description, QString& errorMessage) const; int webapiSpectrumActionsPost(const QStringList& spectrumActionsKeys, SWGSDRangel::SWGSpectrumActions& query, QString& errorMessage); int webapiSpectrumSettingsPutPatch( bool force, diff --git a/sdrbase/dsp/glspectruminterface.h b/sdrbase/dsp/glspectruminterface.h index efea51af6..e3569d32e 100644 --- a/sdrbase/dsp/glspectruminterface.h +++ b/sdrbase/dsp/glspectruminterface.h @@ -21,8 +21,12 @@ #define SDRBASE_DSP_GLSPECTRUMINTERFACE_H_ #include +#include +#include #include "dsp/dsptypes.h" +class SpectrumVis; + class GLSpectrumInterface { public: @@ -34,6 +38,23 @@ public: (void) fftSize; } + // Called by the SpectrumVis this is attached to: set to null before being destroyed + virtual void setSpectrumVis(SpectrumVis *spectrumVis) { (void) spectrumVis; } + + //!< One row of the spectrum history + typedef std::function HistoryRowCallback; + + //!< Get spectrum history from scroll buffe, that are no older than since + //!< folded together by maximum so that at most maxRows are delivered over + //!< the whole period. Returns false when there is no history at all + virtual bool getSpectrumHistory(const QDateTime& since, int maxRows, const HistoryRowCallback& row) + { + (void) since; + (void) maxRows; + (void) row; + return false; + } + // Actions on what is displayed. The default does nothing, so a spectrum with no GUI simply // ignores them rather than needing every caller to know whether one is attached. virtual void spectrumAutoscale() {} diff --git a/sdrbase/dsp/spectrumvis.cpp b/sdrbase/dsp/spectrumvis.cpp index 4c68a3bc3..b25057f2a 100644 --- a/sdrbase/dsp/spectrumvis.cpp +++ b/sdrbase/dsp/spectrumvis.cpp @@ -26,6 +26,13 @@ #include "SWGGLSpectrumReport.h" #include "SWGSpectrumActions.h" #include "SWGGLSpectrumData.h" +#include "SWGGLSpectrumHistory.h" +#include "SWGSpectrumHistorySignal.h" +#include +#include +#include +#include +#include #include "SWGSpectrumPeak.h" #include "SWGSpectrumServer.h" #include "SWGSuccessResponse.h" @@ -72,10 +79,29 @@ SpectrumVis::SpectrumVis(Real scalef) : SpectrumVis::~SpectrumVis() { + if (m_glSpectrum) { + m_glSpectrum->setSpectrumVis(nullptr); + } + FFTFactory *fftFactory = DSPEngine::instance()->getFFTFactory(); fftFactory->releaseEngine(m_settings.m_fftSize, false, m_fftEngineSequence); } +void SpectrumVis::setGLSpectrum(GLSpectrumInterface* glSpectrum) +{ + QMutexLocker mutexLocker(&m_mutex); + + if (m_glSpectrum && (m_glSpectrum != glSpectrum)) { + m_glSpectrum->setSpectrumVis(nullptr); + } + + m_glSpectrum = glSpectrum; + + if (m_glSpectrum) { + m_glSpectrum->setSpectrumVis(this); + } +} + void SpectrumVis::setScalef(Real scalef) { MsgConfigureScalingFactor* cmd = new MsgConfigureScalingFactor(scalef); @@ -875,7 +901,348 @@ void SpectrumVis::handleConfigureWSSpectrum(const QString& address, uint16_t por } } +// The history reduced to a matrix of rows by bins, in dB, over one frequency range and one +// device setting: rows taken while the device was tuned or sampling differently are left out, +// since their bins mean other frequencies +struct SpectrumVis::ReducedHistory +{ + int m_bins = 0; + int m_rows = 0; + qint64 m_startFrequency = 0; //!< Centre of the first bin + double m_binBandwidth = 0.0; + qint64 m_centerFrequency = 0; + int m_bandwidth = 0; + int m_skippedRows = 0; + std::vector m_values; //!< rows * bins, oldest row first + QList m_times; +}; + +// Collects the rows of the display's scroll buffer into a reduced matrix. Returns an HTTP status +int SpectrumVis::reduceHistory(double seconds, int bins, qint64 startFrequency, qint64 stopFrequency, int maxRows, + ReducedHistory& out, QString& errorMessage) const +{ + QMutexLocker locker(&m_mutex); + const int fftSize = m_settings.m_fftSize; + const qint64 centerFrequency = m_centerFrequency; + const int bandwidth = m_sampleRate; + const bool linear = m_settings.m_linear; + const bool scrolling = m_settings.m_scrollBar; + GLSpectrumInterface *display = m_glSpectrum; + + if (!display) + { + errorMessage = "There is no spectrum display for this device set, so there is no history"; + return 404; + } + + if (!scrolling) + { + errorMessage = "The spectrum history is kept only while scrolling is enabled: " + "set scrollBar to 1 (and scrollLength to the rows to keep) in the spectrum settings"; + return 404; + } + + if ((fftSize <= 0) || (bandwidth <= 0)) + { + errorMessage = "The spectrum has no sample rate yet"; + return 500; + } + + bins = std::max(1, std::min(bins, m_maxDataBins)); + const double hzPerBin = bandwidth / (double) fftSize; + const qint64 spectrumStart = centerFrequency - bandwidth / 2; + const qint64 wantedStart = (startFrequency == 0) ? spectrumStart : startFrequency; + const qint64 wantedStop = (stopFrequency == 0) ? spectrumStart + bandwidth : stopFrequency; + + if (wantedStop <= wantedStart) + { + errorMessage = "stopFrequency must be above startFrequency"; + return 400; + } + + int firstBin = (int) std::floor((wantedStart - spectrumStart) / hzPerBin); + int lastBin = (int) std::ceil((wantedStop - spectrumStart) / hzPerBin) - 1; + firstBin = std::max(0, std::min(firstBin, fftSize - 1)); + lastBin = std::max(firstBin, std::min(lastBin, fftSize - 1)); + const int available = lastBin - firstBin + 1; + const int returned = std::min(bins, available); + const double binsPerGroup = available / (double) returned; + + out.m_bins = returned; + out.m_centerFrequency = centerFrequency; + out.m_bandwidth = bandwidth; + out.m_binBandwidth = hzPerBin * binsPerGroup; + out.m_startFrequency = spectrumStart + (qint64) (firstBin * hzPerBin + out.m_binBandwidth / 2); + out.m_rows = 0; + out.m_skippedRows = 0; + out.m_values.clear(); + out.m_times.clear(); + + const QDateTime since = QDateTime::currentDateTimeUtc().addMSecs(-(qint64) (seconds * 1000.0)); + + const bool have = display->getSpectrumHistory(since, maxRows, + [&](const Real *spectrum, int rowFftSize, quint32 rowSampleRate, qint64 rowCentre, const QDateTime& dateTime) + { + if ((rowFftSize != fftSize) || ((int) rowSampleRate != bandwidth) || (rowCentre != centerFrequency)) + { + out.m_skippedRows++; + return; + } + + const size_t base = out.m_values.size(); + out.m_values.resize(base + returned); + + for (int i = 0; i < returned; i++) + { + const int from = firstBin + (int) std::floor(i * binsPerGroup); + int to = firstBin + (int) std::floor((i + 1) * binsPerGroup) - 1; + to = std::max(from, std::min(to, lastBin)); + float value = spectrum[from]; + + for (int bin = from + 1; bin <= to; bin++) { + value = std::max(value, spectrum[bin]); + } + + if (linear) { + value = (float) (10.0 * std::log10(std::max((double) value, 1e-20))); + } + + out.m_values[base + i] = value; + } + + out.m_times.append(dateTime); + out.m_rows++; + }); + + if (!have) + { + errorMessage = "The spectrum history is kept only while scrolling is enabled in the spectrum display"; + return 404; + } + + if (out.m_rows == 0) + { + errorMessage = out.m_skippedRows > 0 + ? QString("The %1 rows of history in that time were taken with the device tuned or sampling differently, so they do not describe the current band").arg(out.m_skippedRows) + : "There is no spectrum history yet for that time: the spectrum is only computed while it is displayed, and rows arrive at the display's refresh rate"; + return 404; + } + + return 200; +} + +int SpectrumVis::webapiSpectrumHistoryGet(double seconds, int bins, qint64 startFrequency, qint64 stopFrequency, double thresholdDb, + SWGSDRangel::SWGGLSpectrumHistory& response, QString& errorMessage) const +{ + ReducedHistory history; + const int status = reduceHistory(seconds, bins, startFrequency, stopFrequency, m_maxHistoryRows, history, errorMessage); + + if (status != 200) { + return status; + } + + const int rows = history.m_rows; + const int n = history.m_bins; + std::vector maxDb(n, -1e9f); + std::vector meanDb(n, 0.0); + + for (int r = 0; r < rows; r++) + { + const float *row = &history.m_values[(size_t) r * n]; + + for (int i = 0; i < n; i++) + { + maxDb[i] = std::max(maxDb[i], row[i]); + meanDb[i] += row[i]; + } + } + + for (int i = 0; i < n; i++) { + meanDb[i] /= rows; + } + + // The floor is the median of the per bin means: most bins of most bands hold no signal, + // and a mean survives the odd burst that a minimum would not + std::vector sorted(meanDb); + std::sort(sorted.begin(), sorted.end()); + const double floorDb = sorted[n / 2]; + const double threshold = floorDb + thresholdDb; + std::vector above(n, 0); + + for (int r = 0; r < rows; r++) + { + const float *row = &history.m_values[(size_t) r * n]; + + for (int i = 0; i < n; i++) + { + if (row[i] > threshold) { + above[i]++; + } + } + } + + response.init(); + response.setCenterFrequency(history.m_centerFrequency); + response.setBandwidth(history.m_bandwidth); + response.setStartFrequency(history.m_startFrequency); + response.setStopFrequency(history.m_startFrequency + (qint64) ((n - 1) * history.m_binBandwidth)); + response.setBinBandwidth((int) history.m_binBandwidth); + response.setBins(n); + response.setRows(rows); + response.setSkippedRows(history.m_skippedRows); + response.setFirstTime(new QString(history.m_times.first().toString(Qt::ISODateWithMs))); + response.setLastTime(new QString(history.m_times.last().toString(Qt::ISODateWithMs))); + response.setSeconds((float) (history.m_times.first().msecsTo(history.m_times.last()) / 1000.0)); + response.setFloorDb((float) floorDb); + response.setThresholdDb((float) threshold); + + for (int i = 0; i < n; i++) + { + response.getMaxDb()->append(maxDb[i]); + response.getMeanDb()->append((float) meanDb[i]); + response.getOccupancy()->append((float) (above[i] / (double) rows)); + } + + // Signals: runs of bins whose maximum rose above the threshold. Each is judged over the + // rows as a whole, so a signal hopping within its run still counts as one transmission + // per row, and its duty cycle is the share of rows in which any of its bins was up + struct Found { int m_first; int m_last; }; + std::vector runs; + + for (int i = 0; i < n; i++) + { + if (maxDb[i] <= threshold) { + continue; + } + + if (!runs.empty() && (runs.back().m_last == i - 1)) { + runs.back().m_last = i; + } else { + runs.push_back({i, i}); + } + } + + // Over thousands of rows, noise alone rises past a threshold a few dB above the floor now + // and then, so a run that was up in only a row or two of many is noise, not a signal, + // unless it stood well clear when it did + const int minActive = std::max(2, rows / 200); + + for (const Found& run : runs) + { + int peakBin = run.m_first; + int active = 0; + int firstRow = -1; + int lastRow = -1; + + for (int i = run.m_first + 1; i <= run.m_last; i++) + { + if (maxDb[i] > maxDb[peakBin]) { + peakBin = i; + } + } + + for (int r = 0; r < rows; r++) + { + const float *row = &history.m_values[(size_t) r * n]; + bool up = false; + + for (int i = run.m_first; (i <= run.m_last) && !up; i++) { + up = row[i] > threshold; + } + + if (up) + { + active++; + lastRow = r; + + if (firstRow < 0) { + firstRow = r; + } + } + } + + if ((active < minActive) && (maxDb[peakBin] < threshold + 6.0)) { + continue; + } + + SWGSDRangel::SWGSpectrumHistorySignal *found = new SWGSDRangel::SWGSpectrumHistorySignal(); + found->init(); + found->setFrequency(history.m_startFrequency + (qint64) (peakBin * history.m_binBandwidth)); + found->setStartFrequency(history.m_startFrequency + (qint64) (run.m_first * history.m_binBandwidth - history.m_binBandwidth / 2)); + found->setStopFrequency(history.m_startFrequency + (qint64) (run.m_last * history.m_binBandwidth + history.m_binBandwidth / 2)); + found->setBandwidth((int) ((run.m_last - run.m_first + 1) * history.m_binBandwidth)); + found->setPeakDb(maxDb[peakBin]); + found->setDutyCycle((float) (active / (double) rows)); + found->setFirstSeen(new QString(history.m_times[firstRow].toString(Qt::ISODateWithMs))); + found->setLastSeen(new QString(history.m_times[lastRow].toString(Qt::ISODateWithMs))); + response.getSignals()->append(found); + } + + response.setSignalCount(response.getSignals()->size()); + return 200; +} + +int SpectrumVis::webapiSpectrumHistoryImageGet(double seconds, int bins, qint64 startFrequency, qint64 stopFrequency, int maxRows, + QByteArray& png, QJsonObject& description, QString& errorMessage) const +{ + ReducedHistory history; + maxRows = std::max(1, std::min(maxRows, m_maxHistoryRows)); + const int status = reduceHistory(seconds, bins, startFrequency, stopFrequency, maxRows, history, errorMessage); + + if (status != 200) { + return status; + } + + const int n = history.m_bins; + const int rows = history.m_rows; + + // Greyscale from the floor to the peak: the floor as the median of everything, black a + // little below it so noise is not solid black, white at the hottest bin + std::vector sorted(history.m_values); + std::sort(sorted.begin(), sorted.end()); + const float floorDb = sorted[sorted.size() / 2]; + const float peakDb = sorted.back(); + const float low = floorDb - 5.0f; + const float high = std::max(peakDb, low + 10.0f); + + QImage image(n, rows, QImage::Format_Grayscale8); + + for (int r = 0; r < rows; r++) + { + uchar *line = image.scanLine(r); + const float *row = &history.m_values[(size_t) r * n]; + + for (int i = 0; i < n; i++) + { + float v = (row[i] - low) / (high - low); + v = std::max(0.0f, std::min(1.0f, v)); + line[i] = (uchar) (v * 255.0f); + } + } + + QBuffer buffer(&png); + buffer.open(QIODevice::WriteOnly); + + if (!image.save(&buffer, "PNG")) + { + errorMessage = "The image could not be encoded"; + return 500; + } + description["width"] = n; + description["height"] = rows; + description["startFrequency"] = (double) history.m_startFrequency; + description["stopFrequency"] = (double) (history.m_startFrequency + (qint64) ((n - 1) * history.m_binBandwidth)); + description["binBandwidth"] = history.m_binBandwidth; + description["centerFrequency"] = (double) history.m_centerFrequency; + description["firstTime"] = history.m_times.first().toString(Qt::ISODateWithMs); + description["lastTime"] = history.m_times.last().toString(Qt::ISODateWithMs); + description["seconds"] = history.m_times.first().msecsTo(history.m_times.last()) / 1000.0; + description["blackDb"] = low; + description["whiteDb"] = high; + description["skippedRows"] = history.m_skippedRows; + return 200; +} int SpectrumVis::webapiActionsPost(const QStringList& spectrumActionsKeys, SWGSDRangel::SWGSpectrumActions& query, QString& errorMessage) { diff --git a/sdrbase/dsp/spectrumvis.h b/sdrbase/dsp/spectrumvis.h index 347f15213..e11ef2bb0 100644 --- a/sdrbase/dsp/spectrumvis.h +++ b/sdrbase/dsp/spectrumvis.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "dsp/basebandsamplesink.h" @@ -47,6 +48,7 @@ namespace SWGSDRangel { class SWGGLSpectrumReport; class SWGSpectrumActions; class SWGGLSpectrumData; + class SWGGLSpectrumHistory; class SWGSpectrumServer; class SWGSuccessResponse; }; @@ -175,7 +177,7 @@ public: SpectrumVis(Real scalef); virtual ~SpectrumVis(); - void setGLSpectrum(GLSpectrumInterface* glSpectrum) { m_glSpectrum = glSpectrum; } + void setGLSpectrum(GLSpectrumInterface* glSpectrum); void setWorkspaceIndex(int index) { m_workspaceIndex = index; } int getWorkspaceIndex() const { return m_workspaceIndex; } @@ -206,6 +208,22 @@ public: SWGSDRangel::SWGGLSpectrumData& response, QString& errorMessage) const; static const int m_maxDataBins = 4096; //!< A reduced spectrum any larger is not a summary + + //!< Statistics over the spectrum history the display keeps for scrolling: per bin max, mean + //!< and occupancy, and the signals found. thresholdDb is above the measured floor + int webapiSpectrumHistoryGet(double seconds, int bins, qint64 startFrequency, qint64 stopFrequency, double thresholdDb, + SWGSDRangel::SWGGLSpectrumHistory& response, QString& errorMessage) const; + //!< The same history as a greyscale PNG, newest row at the bottom, bins wide and one row per pixel up to maxRows + int webapiSpectrumHistoryImageGet(double seconds, int bins, qint64 startFrequency, qint64 stopFrequency, int maxRows, + QByteArray& png, QJsonObject& description, QString& errorMessage) const; + static const int m_maxHistoryRows = 4000; + +private: + struct ReducedHistory; + int reduceHistory(double seconds, int bins, qint64 startFrequency, qint64 stopFrequency, int maxRows, + ReducedHistory& out, QString& errorMessage) const; + +public: int webapiActionsPost(const QStringList& spectrumActionsKeys, SWGSDRangel::SWGSpectrumActions& query, QString& errorMessage); //!< Called by whatever measures the spectrum, once per set of results diff --git a/sdrbase/webapi/webapiadapter.cpp b/sdrbase/webapi/webapiadapter.cpp index dc0ce1a2f..ae2c5373e 100644 --- a/sdrbase/webapi/webapiadapter.cpp +++ b/sdrbase/webapi/webapiadapter.cpp @@ -74,6 +74,7 @@ #include "SWGWorkspaceInfo.h" #include "SWGWorkspaceActions.h" #include "SWGWindowList.h" +#include "SWGGLSpectrumHistory.h" #include "SWGWindowInfo.h" #include "SWGChannelsDetail.h" #include "SWGChannelSettings.h" @@ -1939,6 +1940,51 @@ int WebAPIAdapter::devicesetSpectrumActionsPost( } } +int WebAPIAdapter::devicesetSpectrumHistoryGet( + int deviceSetIndex, + double seconds, + int bins, + qint64 startFrequency, + qint64 stopFrequency, + double thresholdDb, + SWGSDRangel::SWGGLSpectrumHistory& response, + SWGSDRangel::SWGErrorResponse& error) +{ + error.init(); + + if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainCore->m_deviceSets.size())) + { + const DeviceSet *deviceSet = m_mainCore->m_deviceSets[deviceSetIndex]; + return deviceSet->webapiSpectrumHistoryGet(seconds, bins, startFrequency, stopFrequency, thresholdDb, response, *error.getMessage()); + } + + *error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex); + return 404; +} + +int WebAPIAdapter::devicesetSpectrumHistoryImageGet( + int deviceSetIndex, + double seconds, + int bins, + qint64 startFrequency, + qint64 stopFrequency, + int maxRows, + QByteArray& png, + QJsonObject& description, + SWGSDRangel::SWGErrorResponse& error) +{ + error.init(); + + if ((deviceSetIndex >= 0) && (deviceSetIndex < (int) m_mainCore->m_deviceSets.size())) + { + const DeviceSet *deviceSet = m_mainCore->m_deviceSets[deviceSetIndex]; + return deviceSet->webapiSpectrumHistoryImageGet(seconds, bins, startFrequency, stopFrequency, maxRows, png, description, *error.getMessage()); + } + + *error.getMessage() = QString("There is no device set with index %1").arg(deviceSetIndex); + return 404; +} + int WebAPIAdapter::devicesetSpectrumDataGet( int deviceSetIndex, int bins, diff --git a/sdrbase/webapi/webapiadapter.h b/sdrbase/webapi/webapiadapter.h index 8ff0ed7fe..aed11b8d1 100644 --- a/sdrbase/webapi/webapiadapter.h +++ b/sdrbase/webapi/webapiadapter.h @@ -260,6 +260,27 @@ public: SWGSDRangel::SWGGLSpectrumData& response, SWGSDRangel::SWGErrorResponse& error); + virtual int devicesetSpectrumHistoryGet( + int deviceSetIndex, + double seconds, + int bins, + qint64 startFrequency, + qint64 stopFrequency, + double thresholdDb, + SWGSDRangel::SWGGLSpectrumHistory& response, + SWGSDRangel::SWGErrorResponse& error); + + virtual int devicesetSpectrumHistoryImageGet( + int deviceSetIndex, + double seconds, + int bins, + qint64 startFrequency, + qint64 stopFrequency, + int maxRows, + QByteArray& png, + QJsonObject& description, + SWGSDRangel::SWGErrorResponse& error); + virtual int devicesetSpectrumReportGet( int deviceSetIndex, SWGSDRangel::SWGGLSpectrumReport& response, diff --git a/sdrbase/webapi/webapiadapterinterface.cpp b/sdrbase/webapi/webapiadapterinterface.cpp index be818e481..ac2cb756a 100644 --- a/sdrbase/webapi/webapiadapterinterface.cpp +++ b/sdrbase/webapi/webapiadapterinterface.cpp @@ -56,6 +56,8 @@ std::regex WebAPIAdapterInterface::devicesetSpectrumSettingsURLRe("^/sdrangel/de std::regex WebAPIAdapterInterface::devicesetSpectrumReportURLRe("^/sdrangel/deviceset/([0-9]{1,2})/spectrum/report$"); std::regex WebAPIAdapterInterface::devicesetSpectrumActionsURLRe("^/sdrangel/deviceset/([0-9]{1,2})/spectrum/actions$"); std::regex WebAPIAdapterInterface::devicesetSpectrumDataURLRe("^/sdrangel/deviceset/([0-9]{1,2})/spectrum/data$"); +std::regex WebAPIAdapterInterface::devicesetSpectrumHistoryURLRe("^/sdrangel/deviceset/([0-9]{1,2})/spectrum/history$"); +std::regex WebAPIAdapterInterface::devicesetSpectrumHistoryImageURLRe("^/sdrangel/deviceset/([0-9]{1,2})/spectrum/history/image$"); std::regex WebAPIAdapterInterface::devicesetSpectrumServerURLRe("^/sdrangel/deviceset/([0-9]{1,2})/spectrum/server$"); std::regex WebAPIAdapterInterface::devicesetSpectrumWorkspaceURLRe("^/sdrangel/deviceset/([0-9]{1,2})/spectrum/workspace$"); std::regex WebAPIAdapterInterface::devicesetDeviceURLRe("^/sdrangel/deviceset/([0-9]{1,2})/device$"); diff --git a/sdrbase/webapi/webapiadapterinterface.h b/sdrbase/webapi/webapiadapterinterface.h index d90d30b0c..8ac809c7b 100644 --- a/sdrbase/webapi/webapiadapterinterface.h +++ b/sdrbase/webapi/webapiadapterinterface.h @@ -23,6 +23,8 @@ #define SDRBASE_WEBAPI_WEBAPIADAPTERINTERFACE_H_ #include +#include +#include #include #include @@ -86,6 +88,7 @@ namespace SWGSDRangel class SWGGLSpectrumReport; class SWGSpectrumActions; class SWGGLSpectrumData; + class SWGGLSpectrumHistory; class SWGSpectrumServer; } @@ -1002,6 +1005,56 @@ public: return 501; } + /** + * Statistics over the spectrum history kept for scrolling + */ + virtual int devicesetSpectrumHistoryGet( + int deviceSetIndex, + double seconds, + int bins, + qint64 startFrequency, + qint64 stopFrequency, + double thresholdDb, + SWGSDRangel::SWGGLSpectrumHistory& response, + SWGSDRangel::SWGErrorResponse& error) + { + (void) deviceSetIndex; + (void) seconds; + (void) bins; + (void) startFrequency; + (void) stopFrequency; + (void) thresholdDb; + (void) response; + *error.getMessage() = "Function not implemented"; + return 501; + } + + /** + * The spectrum history as a PNG waterfall; description receives its extents + */ + virtual int devicesetSpectrumHistoryImageGet( + int deviceSetIndex, + double seconds, + int bins, + qint64 startFrequency, + qint64 stopFrequency, + int maxRows, + QByteArray& png, + QJsonObject& description, + SWGSDRangel::SWGErrorResponse& error) + { + (void) deviceSetIndex; + (void) seconds; + (void) bins; + (void) startFrequency; + (void) stopFrequency; + (void) maxRows; + (void) png; + (void) description; + *error.getMessage() = "Function not implemented"; + return 501; + } + /** * A reduced copy of the current power spectrum */ @@ -1807,6 +1860,8 @@ public: static std::regex devicesetSpectrumReportURLRe; static std::regex devicesetSpectrumActionsURLRe; static std::regex devicesetSpectrumDataURLRe; + static std::regex devicesetSpectrumHistoryURLRe; + static std::regex devicesetSpectrumHistoryImageURLRe; static std::regex devicesetSpectrumServerURLRe; static std::regex devicesetSpectrumWorkspaceURLRe; static std::regex devicesetDeviceURLRe; diff --git a/sdrbase/webapi/webapirequestmapper.cpp b/sdrbase/webapi/webapirequestmapper.cpp index c45b5a6b5..20a877ae4 100644 --- a/sdrbase/webapi/webapirequestmapper.cpp +++ b/sdrbase/webapi/webapirequestmapper.cpp @@ -55,6 +55,7 @@ #include "SWGWorkspaceInfo.h" #include "SWGWorkspaceActions.h" #include "SWGWindowList.h" +#include "SWGGLSpectrumHistory.h" #include "SWGChannelsDetail.h" #include "SWGChannelSettings.h" #include "SWGChannelReport.h" @@ -196,6 +197,10 @@ void WebAPIRequestMapper::service(qtwebapp::HttpRequest& request, qtwebapp::Http devicesetSpectrumActionsService(std::string(desc_match[1]), request, response); } else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetSpectrumDataURLRe)) { devicesetSpectrumDataService(std::string(desc_match[1]), request, response); + } else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetSpectrumHistoryURLRe)) { + devicesetSpectrumHistoryService(std::string(desc_match[1]), request, response, false); + } else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetSpectrumHistoryImageURLRe)) { + devicesetSpectrumHistoryService(std::string(desc_match[1]), request, response, true); } else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::devicesetSpectrumServerURLRe)) { devicesetSpectrumServerService(std::string(desc_match[1]), request, response); } else if (std::regex_match(pathStr, desc_match, WebAPIAdapterInterface::workspaceActionsURLRe)) { @@ -1801,6 +1806,120 @@ void WebAPIRequestMapper::devicesetService(const std::string& indexStr, qtwebapp +void WebAPIRequestMapper::devicesetSpectrumHistoryService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response, bool image) +{ + SWGSDRangel::SWGErrorResponse errorResponse; + response.setHeader("Access-Control-Allow-Origin", "*"); + + auto fail = [&](int status, const QString& message) + { + response.setHeader("Content-Type", "application/json"); + response.setStatus(status, message.toLatin1().constData()); + errorResponse.init(); + *errorResponse.getMessage() = message; + response.write(errorResponse.asJson().toUtf8()); + }; + + try + { + int deviceSetIndex = boost::lexical_cast(indexStr); + + if (request.getMethod() != "GET") + { + fail(405, "Invalid HTTP method"); + return; + } + + // Numbers from the query string, each checked rather than defaulted on a parse failure + double seconds = 60.0; + int bins = image ? 512 : 128; + int rows = 512; + double threshold = 6.0; + qint64 startFrequency = 0; + qint64 stopFrequency = 0; + + for (const char *name : {"seconds", "bins", "rows", "threshold", "startFrequency", "stopFrequency"}) + { + QByteArray parameter = request.getParameter(name); + + if (parameter.isEmpty()) { + continue; + } + + bool parsed = false; + const double value = QString(parameter).toDouble(&parsed); + + if (!parsed) + { + fail(400, QString("%1 must be a number").arg(name)); + return; + } + + const QString key(name); + + if (key == "seconds") { + seconds = value; + } else if (key == "bins") { + bins = (int) value; + } else if (key == "rows") { + rows = (int) value; + } else if (key == "threshold") { + threshold = value; + } else if (key == "startFrequency") { + startFrequency = (qint64) value; + } else { + stopFrequency = (qint64) value; + } + } + + if ((seconds <= 0.0) || (bins < 1) || (rows < 1)) + { + fail(400, "seconds, bins and rows must be positive"); + return; + } + + if (image) + { + QByteArray png; + QJsonObject description; + int status = m_adapter->devicesetSpectrumHistoryImageGet(deviceSetIndex, seconds, bins, startFrequency, stopFrequency, rows, png, description, errorResponse); + + if (status / 100 != 2) + { + fail(status, errorResponse.getMessage() ? *errorResponse.getMessage() : QString("Error")); + return; + } + + response.setHeader("Content-Type", "image/png"); + + for (const QString& key : description.keys()) { + response.setHeader(("X-Spectrum-History-" + key).toLatin1(), description[key].toVariant().toString().toLatin1()); + } + + response.setStatus(200); + response.write(png); + } + else + { + SWGSDRangel::SWGGLSpectrumHistory normalResponse; + normalResponse.init(); + int status = m_adapter->devicesetSpectrumHistoryGet(deviceSetIndex, seconds, bins, startFrequency, stopFrequency, threshold, normalResponse, errorResponse); + response.setHeader("Content-Type", "application/json"); + response.setStatus(status); + + if (status/100 == 2) { + response.write(normalResponse.asJson().toUtf8()); + } else { + response.write(errorResponse.asJson().toUtf8()); + } + } + } + catch (const boost::bad_lexical_cast &e) + { + fail(400, QString("Wrong integer conversion on index %1").arg(e.what())); + } +} + void WebAPIRequestMapper::devicesetSpectrumDataService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) { SWGSDRangel::SWGErrorResponse errorResponse; diff --git a/sdrbase/webapi/webapirequestmapper.h b/sdrbase/webapi/webapirequestmapper.h index b47ce4444..2751e250c 100644 --- a/sdrbase/webapi/webapirequestmapper.h +++ b/sdrbase/webapi/webapirequestmapper.h @@ -89,6 +89,7 @@ private: void devicesetSpectrumReportService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); void devicesetSpectrumActionsService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); void devicesetSpectrumDataService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); + void devicesetSpectrumHistoryService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response, bool image); void devicesetSpectrumServerService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); void devicesetSpectrumWorkspaceService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); void devicesetDeviceService(const std::string& indexStr, qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response); diff --git a/sdrgui/gui/glspectrum.cpp b/sdrgui/gui/glspectrum.cpp index 3532ecff8..db55771c0 100644 --- a/sdrgui/gui/glspectrum.cpp +++ b/sdrgui/gui/glspectrum.cpp @@ -26,6 +26,7 @@ #include #include +#include "dsp/spectrumvis.h" #include "gui/glspectrum.h" #include "gui/glspectrumview.h" #include "gui/spectrummeasurements.h" @@ -33,6 +34,7 @@ GLSpectrum::GLSpectrum(QWidget *parent) : QWidget(parent) { + m_spectrumVis = nullptr; m_spectrumContainer = new QWidget(); QHBoxLayout *hLayout = new QHBoxLayout(m_spectrumContainer); m_spectrum = new GLSpectrumView(); @@ -58,6 +60,19 @@ GLSpectrum::GLSpectrum(QWidget *parent) : setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); } +GLSpectrum::~GLSpectrum() +{ + if (m_spectrumVis) { + m_spectrumVis->setGLSpectrum(nullptr); + } +} + +void GLSpectrum::setSpectrumVis(SpectrumVis* spectrumVis) +{ + m_spectrumVis = spectrumVis; + m_spectrum->setSpectrumVis(spectrumVis); +} + void GLSpectrum::spectrumAutoscale() { diff --git a/sdrgui/gui/glspectrum.h b/sdrgui/gui/glspectrum.h index e8f9811c1..cd425b15e 100644 --- a/sdrgui/gui/glspectrum.h +++ b/sdrgui/gui/glspectrum.h @@ -37,6 +37,7 @@ class SDRGUI_API GLSpectrum : public QWidget, public GLSpectrumInterface { public: GLSpectrum(QWidget *parent = nullptr); + ~GLSpectrum() override; GLSpectrumView *getSpectrumView() const { return m_spectrum; } // Asked for through the web API, which arrives on an HTTP thread, so each is queued onto the // GUI thread rather than run where it was asked for @@ -97,12 +98,13 @@ public: void removeChannelMarker(ChannelMarker* channelMarker) { m_spectrum->removeChannelMarker(channelMarker); } void setMessageQueueToGUI(MessageQueue* messageQueue) { m_spectrum->setMessageQueueToGUI(messageQueue); } void newSpectrum(const Real* spectrum, int fftSize) { m_spectrum->newSpectrum(spectrum, fftSize); } + bool getSpectrumHistory(const QDateTime& since, int maxRows, const HistoryRowCallback& row) override { return m_spectrum->getSpectrumHistory(since, maxRows, row); } void clearSpectrumHistogram() { m_spectrum->clearSpectrumHistogram(); } Real getWaterfallShare() const { return m_spectrum->getWaterfallShare(); } void setWaterfallShare(Real waterfallShare) { m_spectrum->setWaterfallShare(waterfallShare); } void setFPSPeriodMs(int fpsPeriodMs) { m_spectrum->setFPSPeriodMs(fpsPeriodMs); } void setDisplayedStream(bool sourceOrSink, int streamIndex) { m_spectrum->setDisplayedStream(sourceOrSink, streamIndex); } - void setSpectrumVis(SpectrumVis *spectrumVis) { m_spectrum->setSpectrumVis(spectrumVis); } + void setSpectrumVis(SpectrumVis* spectrumVis); SpectrumVis *getSpectrumVis() { return m_spectrum->getSpectrumVis(); } const QList& getHistogramMarkers() const { return m_spectrum->getHistogramMarkers(); } QList& getHistogramMarkers() { return m_spectrum->getHistogramMarkers(); } @@ -142,6 +144,7 @@ private: QSplitter *m_splitter; GLSpectrumView *m_spectrum; SpectrumMeasurements *m_measurements; + SpectrumVis *m_spectrumVis; //!< The SpectrumVis feeding thisd SpectrumSettings::MeasurementsPosition m_position; QWidget *m_spectrumContainer; QScrollBar *m_scrollBar; diff --git a/sdrgui/gui/glspectrumview.cpp b/sdrgui/gui/glspectrumview.cpp index bc52d7369..395e04257 100644 --- a/sdrgui/gui/glspectrumview.cpp +++ b/sdrgui/gui/glspectrumview.cpp @@ -1182,6 +1182,67 @@ void GLSpectrumView::updateSpectrumBuffer(const Real *spectrum, int fftSize, qui m_spectrumBuffer.append(spectrumData); } +// We use the scroll buffer for the Web API's spectrum history +bool GLSpectrumView::getSpectrumHistory(const QDateTime& since, int maxRows, const HistoryRowCallback& row) +{ + QMutexLocker mutexLocker(&m_mutex); + + if (!m_scrollBarEnabled) { + return false; + } + + const int size = (int) m_spectrumBuffer.size(); + int first = size; + + // Back from the newest to the oldest row still wanted + while ((first > 0) && (m_spectrumBuffer[first - 1].m_dateTime >= since)) { + first--; + } + + // Every FFT is a row, hundreds a second, so a period is usually far more rows than asked + // for. Consecutive rows are folded together by maximum rather than skipped, so a burst + // shorter than the fold still shows, and a fold never mixes rows of different tunings + const int candidates = size - first; + const int fold = std::max(1, (candidates + std::max(1, maxRows) - 1) / std::max(1, maxRows)); + std::vector combined; + + for (int i = first; i < size; ) + { + const Spectrum& lead = m_spectrumBuffer[i]; + int end = i + 1; + + while ((end < size) && (end - i < fold) + && (m_spectrumBuffer[end].m_sampleRate == lead.m_sampleRate) + && (m_spectrumBuffer[end].m_centerFrequency == lead.m_centerFrequency)) { + end++; + } + + if (end - i == 1) + { + row(lead.m_spectrum, m_spectrumBufferFFTSize, lead.m_sampleRate, lead.m_centerFrequency, lead.m_dateTime); + } + else + { + combined.assign(lead.m_spectrum, lead.m_spectrum + m_spectrumBufferFFTSize); + + for (int j = i + 1; j < end; j++) + { + const Real *other = m_spectrumBuffer[j].m_spectrum; + + for (int k = 0; k < m_spectrumBufferFFTSize; k++) { + combined[k] = std::max(combined[k], other[k]); + } + } + + row(combined.data(), m_spectrumBufferFFTSize, lead.m_sampleRate, lead.m_centerFrequency, m_spectrumBuffer[end - 1].m_dateTime); + } + + i = end; + } + + return true; +} + void GLSpectrumView::clearWaterfallRow(int nbBins) { if (m_waterfallBufferPos < m_waterfallBuffer->height()) diff --git a/sdrgui/gui/glspectrumview.h b/sdrgui/gui/glspectrumview.h index b978479e0..8779de975 100644 --- a/sdrgui/gui/glspectrumview.h +++ b/sdrgui/gui/glspectrumview.h @@ -229,6 +229,7 @@ public: void setMessageQueueToGUI(MessageQueue* messageQueue) { m_messageQueueToGUI = messageQueue; } virtual void newSpectrum(const Real* spectrum, int fftSize); + virtual bool getSpectrumHistory(const QDateTime& since, int maxRows, const HistoryRowCallback& row) override; void clearSpectrumHistogram(); Real getWaterfallShare() const { return m_waterfallShare; }