| 1 | /* |
| 2 | * Copyright (C) 2015, Mike Walters <[email protected]> |
| 3 | * |
| 4 | * This file is part of inspectrum. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include <QCache> |
| 23 | #include <QString> |
| 24 | #include <QWidget> |
| 25 | #include "fft.h" |
| 26 | #include "inputsource.h" |
| 27 | #include "plot.h" |
| 28 | #include "tuner.h" |
| 29 | #include "tunertransform.h" |
| 30 | |
| 31 | #include <memory> |
| 32 | #include <array> |
| 33 | #include <math.h> |
| 34 | #include <vector> |
| 35 | |
| 36 | class AnnotationLocation; |
| 37 | |
| 38 | |
| 39 | class TileCacheKey |
| 40 | { |
| 41 | |
| 42 | public: |
| 43 | TileCacheKey(int fftSize, int zoomLevel, size_t sample) { |
| 44 | this->fftSize = fftSize; |
| 45 | this->zoomLevel = zoomLevel; |
| 46 | this->sample = sample; |
| 47 | } |
| 48 | |
| 49 | bool operator==(const TileCacheKey &k2) const { |
| 50 | return (this->fftSize == k2.fftSize) && |
| 51 | (this->zoomLevel == k2.zoomLevel) && |
| 52 | (this->sample == k2.sample); |
| 53 | } |
| 54 | |
| 55 | int fftSize; |
| 56 | int zoomLevel; |
| 57 | size_t sample; |
| 58 | }; |
| 59 | |
| 60 | class SpectrogramPlot : public Plot |
| 61 | { |
| 62 | Q_OBJECT |
| 63 | |
| 64 | public: |
| 65 | SpectrogramPlot(std::shared_ptr<SampleSource<std::complex<float>>> src); |
| 66 | void invalidateEvent() override; |
| 67 | std::shared_ptr<AbstractSampleSource> output() override; |
| 68 | void paintFront(QPainter &painter, QRect &rect, range_t<size_t> sampleRange) override; |
| 69 | void paintMid(QPainter &painter, QRect &rect, range_t<size_t> sampleRange) override; |
| 70 | bool mouseEvent(QEvent::Type type, QMouseEvent *event) override; |
| 71 | void leaveEvent(); |
| 72 | std::shared_ptr<SampleSource<std::complex<float>>> input() { return inputSource; }; |
| 73 | void setSampleRate(double sampleRate); |
| 74 | bool tunerEnabled(); |
| 75 | void enableScales(bool enabled); |
| 76 | void enableAnnotations(bool enabled); |
| 77 | bool isAnnotationsEnabled(); |
| 78 | QString *mouseAnnotationComment(const QMouseEvent *event); |
| 79 | |
| 80 | public slots: |
| 81 | void setFFTSize(int size); |
| 82 | void setPowerMax(int power); |
| 83 | void setPowerMin(int power); |
| 84 | void setZoomLevel(int zoom); |
| 85 | void tunerMoved(); |
| 86 | |
| 87 | private: |
| 88 | const int linesPerGraduation = 50; |
| 89 | static const int tileSize = 65536; // This must be a multiple of the maximum FFT size |
| 90 | |
| 91 | std::shared_ptr<SampleSource<std::complex<float>>> inputSource; |
| 92 | std::vector<AnnotationLocation> visibleAnnotationLocations; |
| 93 | std::unique_ptr<FFT> fft; |
| 94 | std::unique_ptr<float[]> window; |
| 95 | QCache<TileCacheKey, QPixmap> pixmapCache; |
| 96 | QCache<TileCacheKey, std::array<float, tileSize>> fftCache; |
| 97 | uint colormap[256]; |
| 98 | |
| 99 | int fftSize; |
| 100 | int zoomLevel; |
| 101 | float powerMax; |
| 102 | float powerMin; |
| 103 | double sampleRate; |
| 104 | bool frequencyScaleEnabled; |
| 105 | bool sigmfAnnotationsEnabled; |
| 106 | |
| 107 | Tuner tuner; |
| 108 | std::shared_ptr<TunerTransform> tunerTransform; |
| 109 | |
| 110 | QPixmap* getPixmapTile(size_t tile); |
| 111 | float* getFFTTile(size_t tile); |
| 112 | void getLine(float *dest, size_t sample); |
| 113 | int getStride(); |
| 114 | float getTunerPhaseInc(); |
| 115 | std::vector<float> getTunerTaps(); |
| 116 | int linesPerTile(); |
| 117 | void paintFrequencyScale(QPainter &painter, QRect &rect); |
| 118 | void paintAnnotations(QPainter &painter, QRect &rect, range_t<size_t> sampleRange); |
| 119 | }; |
| 120 | |
| 121 | class AnnotationLocation |
| 122 | { |
| 123 | public: |
| 124 | Annotation annotation; |
| 125 | |
| 126 | AnnotationLocation(Annotation annotation, int x, int y, int width, int height) |
| 127 | : annotation(annotation), x(x), y(y), width(width), height(height) {} |
| 128 | |
| 129 | bool isInside(int pos_x, int pos_y) { |
| 130 | return (x <= pos_x) && (pos_x <= x + width) |
| 131 | && (y <= pos_y) && (pos_y <= y + height); |
| 132 | } |
| 133 | |
| 134 | private: |
| 135 | int x; |
| 136 | int y; |
| 137 | int width; |
| 138 | int height; |
| 139 | }; |