1 /****************************************************************************************
2  * Copyright (c) 2004 Max Howell <max.howell@methylblue.com>                            *
3  * Copyright (c) 2009 Martin Sandsmark <martin.sandsmark@kde.org>                       *
4  * Copyright (c) 2013 Mark Kretschmann <kretschmann@kde.org>                            *
5  *                                                                                      *
6  * This program is free software; you can redistribute it and/or modify it under        *
7  * the terms of the GNU General Public License as published by the Free Software        *
8  * Foundation; either version 2 of the License, or (at your option) any later           *
9  * version.                                                                             *
10  *                                                                                      *
11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
13  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
14  *                                                                                      *
15  * You should have received a copy of the GNU General Public License along with         *
16  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
17  ****************************************************************************************/
18 
19 #ifndef ANALYZERBASE_H
20 #define ANALYZERBASE_H
21 
22 #ifdef __FreeBSD__
23 #include <sys/types.h>
24 #endif
25 
26 #include <phonon/audiodataoutput.h>
27 
28 #include <QMap>
29 #include <QQuickFramebufferObject>
30 #include <QThread>
31 #include <QVector>
32 
33 #include <KConfigGroup>
34 
35 
36 namespace Analyzer
37 {
38     class Worker;
39 
40     class Base : public QQuickFramebufferObject
41 {
42     Q_OBJECT
43     Q_PROPERTY(qreal minFrequency READ minFreq WRITE setMinFreq NOTIFY minFreqChanged)
44     Q_PROPERTY(qreal maxFrequency READ maxFreq WRITE setMaxFreq NOTIFY maxFreqChanged)
45     Q_PROPERTY(WindowFunction windowFunction READ windowFunction WRITE setWindowFunction NOTIFY windowFunctionChanged)
46     Q_PROPERTY(qreal minimumFrequency READ minFreq WRITE setMinFreq NOTIFY minFreqChanged)
47     Q_PROPERTY(qreal maximumFrequency READ maxFreq WRITE setMaxFreq NOTIFY maxFreqChanged)
48     Q_PROPERTY(int sampleSize READ sampleSize WRITE setSampleSize NOTIFY sampleSizeChanged)
49 
50 public:
51     enum WindowFunction
52     {
53         Rectangular,
54         Hann,
55         Nuttall,
56         Lanczos,
57         Sine
58     };
59     Q_ENUM(WindowFunction)
60 
61     static const int DEMO_INTERVAL = 20; // ~50 fps
62 
63     ~Base() override;
64 
maxFreq()65     qreal maxFreq() const { return m_maxFreq; }
66     void setMaxFreq( qreal maxFreq );
minFreq()67     qreal minFreq() const { return m_minFreq; }
68     void setMinFreq( qreal minFreq );
69     WindowFunction windowFunction() const;
70     void setWindowFunction( WindowFunction windowFunction );
71     int sampleSize() const;
72     void setSampleSize( uint sampleSize );
73 
74     /**
75      * Returns the worker object associated with this analyzer.
76      */
77     const Worker* worker() const;
78 
79 Q_SIGNALS:
80     void minFreqChanged();
81     void maxFreqChanged();
82     void scopeSizeChanged( uint );
83     void windowFunctionChanged( WindowFunction );
84     void sampleSizeChanged( uint );
85     void calculateExpFactorNeeded( qreal, qreal, uint );
86 
87 protected:
88     Base( QQuickItem* );
89 
90     /**
91      * Creates a new worker instance.
92      * Subclasses must implement this function.
93      * All compute heavy tasks should be offloaded to the created worker.
94      * If you make any connections with your worker, remember to make them queued connections.
95      * Do not set a parent for the worker. Base will take ownership of it.
96      */
97     virtual Worker* createWorker() const = 0;
98 
99     /**
100      * Returns the standard KConfigGroup for all analyzers.
101      * You can reimplement this function, if you want your subclass to have a different config.
102      */
103     virtual KConfigGroup config() const;
104 
105     /**
106      * Use this function to set the size for the scope computed by the worker.
107      */
108     void setScopeSize( int size );
109 
110 private:
111     void connectSignals();
112     void disconnectSignals();
113     void currentDesktopChanged();
114     void refreshSampleRate();
115 
116     double m_minFreq, m_maxFreq;
117     int m_sampleRate;
118     int m_scopeSize;
119 
120     Worker *m_worker;
121     QThread m_workerThread;
122 };
123 
124 
125 } //END namespace Analyzer
126 
127 #endif
128