Debian Codegraph @

src/context/applets/analyzer/plugin/AnalyzerWorker.h

raw
1 /*
2 * Copyright 2018 Malte Veerman <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License or (at your option) version 3 or any later version
8 * accepted by the membership of KDE e.V. (or its successor approved
9 * by the membership of KDE e.V.), which shall act as a proxy
10 * defined in Section 14 of version 3 of the license.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef ANALYZERWORKER_H
22 #define ANALYZERWORKER_H
23
24 #include "AnalyzerBase.h"
25
26 #include <QElapsedTimer>
27 #include <QMutex>
28 #include <QObject>
29 #include <QTime>
30
31 #include <complex>
32 #include <fftw3.h>
33
34
35 class QTimer;
36
37 namespace Analyzer
38 {
39
40 /**
41 * Base worker class for all analyzers
42 * All compute heavy tasks should be offloaded to this.
43 */
44 class Worker : public QObject
45 {
46 friend class Base;
47
48 Q_OBJECT
49
50 public:
51 const static int PROCESSING_INTERVAL = 5; // Interval between new data lookups
52 const static int DATA_BUFFER_SIZE = 8; // Higher values increase latency, lower values increase risk of missing frames
53
54 Worker();
55 ~Worker() override;
56
57 void playbackStateChanged();
58 void stopTimers();
59 void setHibernate( bool state );
60
61 protected:
62 /**
63 * @return The current scope data.
64 */
65 const QVector<double>& scope() const { return m_currentScope; }
66
67 /**
68 * This function is being called after new scope data is ready.
69 * Get the scope to be analyzed by calling scope().
70 * Subclasses must implement this function.
71 */
72 virtual void analyze() = 0;
73
74 private:
75 struct BandInfo
76 {
77 double lowerFreq;
78 double midFreq;
79 double upperFreq;
80 double lowerK;
81 double midK;
82 double upperK;
83 int scopeIndex;
84 };
85
86 /**
87 * This function is thread-safe.
88 */
89 void receiveData( const QMap<int, QVector<qint16> > &newData );
90
91 // None of the following functions are thread-safe. Only connect with queued connections to them.
92 void processData();
93 void applyWindowFunction();
94 void makeScope();
95 void setSampleSize( uint size );
96 void setWindowFunction( Base::WindowFunction windowFunction );
97 void setScopeSize( int size );
98 void calculateExpFactor( qreal minFreq, qreal maxFreq, int sampleRate );
99 void resetDemo() { m_demoT = 201; }
100
101 /**
102 * Override this function for your custom idle animation.
103 */
104 virtual void demo();
105
106 fftw_plan m_plan;
107 mutable QMutex m_rawInMutex;
108 QList<double> m_rawIn;
109 double *m_in;
110 std::complex<double> *m_out;
111 QVector<double> m_currentScope;
112 QVector<BandInfo> m_interpolatedScopeBands;
113 QVector<BandInfo> m_notInterpolatedScopeBands;
114 uint m_size;
115 double m_expFactor;
116 Base::WindowFunction m_windowFunction;
117 int m_expectedDataTime;
118 int m_demoT;
119 QElapsedTimer m_lastUpdate;
120 QTimer *m_demoTimer;
121 QTimer *m_processTimer;
122 bool m_hibernate;
123 };
124
125 }
126
127 #endif // ANALYZERWORKER_H