File indexing completed on 2025-01-05 04:25:40

0001 /****************************************************************************************
0002  * Copyright (c) 2004 Max Howell <max.howell@methylblue.com>                            *
0003  * Copyright (c) 2009 Martin Sandsmark <martin.sandsmark@kde.org>                       *
0004  * Copyright (c) 2013 Mark Kretschmann <kretschmann@kde.org>                            *
0005  *                                                                                      *
0006  * This program is free software; you can redistribute it and/or modify it under        *
0007  * the terms of the GNU General Public License as published by the Free Software        *
0008  * Foundation; either version 2 of the License, or (at your option) any later           *
0009  * version.                                                                             *
0010  *                                                                                      *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0014  *                                                                                      *
0015  * You should have received a copy of the GNU General Public License along with         *
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0017  ****************************************************************************************/
0018 
0019 #ifndef ANALYZERBASE_H
0020 #define ANALYZERBASE_H
0021 
0022 #ifdef __FreeBSD__
0023 #include <sys/types.h>
0024 #endif
0025 
0026 #include <phonon/audiodataoutput.h>
0027 
0028 #include <QMap>
0029 #include <QQuickFramebufferObject>
0030 #include <QThread>
0031 #include <QVector>
0032 
0033 #include <KConfigGroup>
0034 
0035 
0036 namespace Analyzer
0037 {
0038     class Worker;
0039 
0040     class Base : public QQuickFramebufferObject
0041 {
0042     Q_OBJECT
0043     Q_PROPERTY(qreal minFrequency READ minFreq WRITE setMinFreq NOTIFY minFreqChanged)
0044     Q_PROPERTY(qreal maxFrequency READ maxFreq WRITE setMaxFreq NOTIFY maxFreqChanged)
0045     Q_PROPERTY(WindowFunction windowFunction READ windowFunction WRITE setWindowFunction NOTIFY windowFunctionChanged)
0046     Q_PROPERTY(qreal minimumFrequency READ minFreq WRITE setMinFreq NOTIFY minFreqChanged)
0047     Q_PROPERTY(qreal maximumFrequency READ maxFreq WRITE setMaxFreq NOTIFY maxFreqChanged)
0048     Q_PROPERTY(int sampleSize READ sampleSize WRITE setSampleSize NOTIFY sampleSizeChanged)
0049 
0050 public:
0051     enum WindowFunction
0052     {
0053         Rectangular,
0054         Hann,
0055         Nuttall,
0056         Lanczos,
0057         Sine
0058     };
0059     Q_ENUM(WindowFunction)
0060 
0061     static const int DEMO_INTERVAL = 20; // ~50 fps
0062 
0063     ~Base() override;
0064 
0065     qreal maxFreq() const { return m_maxFreq; }
0066     void setMaxFreq( qreal maxFreq );
0067     qreal minFreq() const { return m_minFreq; }
0068     void setMinFreq( qreal minFreq );
0069     WindowFunction windowFunction() const;
0070     void setWindowFunction( WindowFunction windowFunction );
0071     int sampleSize() const;
0072     void setSampleSize( uint sampleSize );
0073 
0074     /**
0075      * Returns the worker object associated with this analyzer.
0076      */
0077     const Worker* worker() const;
0078 
0079 Q_SIGNALS:
0080     void minFreqChanged();
0081     void maxFreqChanged();
0082     void scopeSizeChanged( uint );
0083     void windowFunctionChanged( WindowFunction );
0084     void sampleSizeChanged( uint );
0085     void calculateExpFactorNeeded( qreal, qreal, uint );
0086 
0087 protected:
0088     Base( QQuickItem* );
0089 
0090     /**
0091      * Creates a new worker instance.
0092      * Subclasses must implement this function.
0093      * All compute heavy tasks should be offloaded to the created worker.
0094      * If you make any connections with your worker, remember to make them queued connections.
0095      * Do not set a parent for the worker. Base will take ownership of it.
0096      */
0097     virtual Worker* createWorker() const = 0;
0098 
0099     /**
0100      * Returns the standard KConfigGroup for all analyzers.
0101      * You can reimplement this function, if you want your subclass to have a different config.
0102      */
0103     virtual KConfigGroup config() const;
0104 
0105     /**
0106      * Use this function to set the size for the scope computed by the worker.
0107      */
0108     void setScopeSize( int size );
0109 
0110 private:
0111     void connectSignals();
0112     void disconnectSignals();
0113     void currentDesktopChanged();
0114     void refreshSampleRate();
0115 
0116     double m_minFreq, m_maxFreq;
0117     int m_sampleRate;
0118     int m_scopeSize;
0119 
0120     Worker *m_worker;
0121     QThread m_workerThread;
0122 };
0123 
0124 
0125 } //END namespace Analyzer
0126 
0127 #endif