demos/spectrum/app/spectrumanalyser.h
changeset 25 e24348a560a6
child 29 b72c6db6890b
equal deleted inserted replaced
23:89e065397ea6 25:e24348a560a6
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are met:
       
    14 ** - Redistributions of source code must retain the above copyright notice,
       
    15 **   this list of conditions and the following disclaimer.
       
    16 ** - Redistributions in binary form must reproduce the above copyright notice,
       
    17 **   this list of conditions and the following disclaimer in the documentation
       
    18 **   and/or other materials provided with the distribution.
       
    19 ** - Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the
       
    20 **   names of its contributors may be used to endorse or promote products
       
    21 **   derived from this software without specific prior written permission.
       
    22 **
       
    23 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    24 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    25 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    26 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
       
    27 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    28 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    29 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    30 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    31 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    32 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
       
    33 ** POSSIBILITY OF SUCH DAMAGE.
       
    34 ** $QT_END_LICENSE$
       
    35 **
       
    36 *****************************************************************************/
       
    37 
       
    38 #ifndef SPECTRUMANALYSER_H
       
    39 #define SPECTRUMANALYSER_H
       
    40 
       
    41 #include <QByteArray>
       
    42 #include <QObject>
       
    43 #include <QVector>
       
    44 
       
    45 #ifdef DUMP_SPECTRUMANALYSER
       
    46 #include <QDir>
       
    47 #include <QFile>
       
    48 #include <QTextStream>
       
    49 #endif
       
    50 
       
    51 #include "frequencyspectrum.h"
       
    52 #include "spectrum.h"
       
    53 
       
    54 #ifndef DISABLE_FFT
       
    55 #include "FFTRealFixLenParam.h"
       
    56 #endif
       
    57 
       
    58 class QAudioFormat;
       
    59 class QThread;
       
    60 class FFTRealWrapper;
       
    61 
       
    62 class SpectrumAnalyserThreadPrivate;
       
    63 
       
    64 /**
       
    65  * Implementation of the spectrum analysis which can be run in a
       
    66  * separate thread.
       
    67  */
       
    68 class SpectrumAnalyserThread : public QObject
       
    69 {
       
    70     Q_OBJECT
       
    71 public:
       
    72     SpectrumAnalyserThread(QObject *parent);
       
    73     ~SpectrumAnalyserThread();
       
    74 
       
    75 public slots:
       
    76     void setWindowFunction(WindowFunction type);
       
    77     void calculateSpectrum(const QByteArray &buffer,
       
    78                            int inputFrequency,
       
    79                            int bytesPerSample);
       
    80 
       
    81 signals:
       
    82     void calculationComplete(const FrequencySpectrum &spectrum);
       
    83 
       
    84 private:
       
    85     void calculateWindow();
       
    86 
       
    87 private:
       
    88 #ifndef DISABLE_FFT
       
    89     FFTRealWrapper*                             m_fft;
       
    90 #endif
       
    91 
       
    92     const int                                   m_numSamples;
       
    93 
       
    94     WindowFunction                              m_windowFunction;
       
    95 
       
    96 #ifdef DISABLE_FFT
       
    97     typedef qreal                               DataType;
       
    98 #else
       
    99     typedef FFTRealFixLenParam::DataType        DataType;
       
   100 #endif
       
   101     QVector<DataType>                           m_window;
       
   102 
       
   103     QVector<DataType>                           m_input;
       
   104     QVector<DataType>                           m_output;
       
   105 
       
   106     FrequencySpectrum                           m_spectrum;
       
   107 
       
   108 #ifdef SPECTRUM_ANALYSER_SEPARATE_THREAD
       
   109     QThread*                                    m_thread;
       
   110 #endif
       
   111 };
       
   112 
       
   113 /**
       
   114  * Class which performs frequency spectrum analysis on a window of
       
   115  * audio samples, provided to it by the Engine.
       
   116  */
       
   117 class SpectrumAnalyser : public QObject
       
   118 {
       
   119     Q_OBJECT
       
   120 public:
       
   121     SpectrumAnalyser(QObject *parent = 0);
       
   122     ~SpectrumAnalyser();
       
   123 
       
   124 #ifdef DUMP_SPECTRUMANALYSER
       
   125     void setOutputPath(const QString &outputPath);
       
   126 #endif
       
   127 
       
   128 public:
       
   129     /*
       
   130      * Set the windowing function which is applied before calculating the FFT
       
   131      */
       
   132     void setWindowFunction(WindowFunction type);
       
   133 
       
   134     /*
       
   135      * Calculate a frequency spectrum
       
   136      *
       
   137      * \param buffer       Audio data
       
   138      * \param format       Format of audio data
       
   139      *
       
   140      * Frequency spectrum is calculated asynchronously.  The result is returned
       
   141      * via the spectrumChanged signal.
       
   142      *
       
   143      * An ongoing calculation can be cancelled by calling cancelCalculation().
       
   144      *
       
   145      */
       
   146     void calculate(const QByteArray &buffer, const QAudioFormat &format);
       
   147 
       
   148     /*
       
   149      * Check whether the object is ready to perform another calculation
       
   150      */
       
   151     bool isReady() const;
       
   152 
       
   153     /*
       
   154      * Cancel an ongoing calculation
       
   155      *
       
   156      * Note that cancelling is asynchronous.
       
   157      */
       
   158     void cancelCalculation();
       
   159 
       
   160 signals:
       
   161     void spectrumChanged(const FrequencySpectrum &spectrum);
       
   162 
       
   163 private slots:
       
   164     void calculationComplete(const FrequencySpectrum &spectrum);
       
   165 
       
   166 private:
       
   167     void calculateWindow();
       
   168 
       
   169 private:
       
   170 
       
   171     SpectrumAnalyserThread*    m_thread;
       
   172 
       
   173     enum State {
       
   174         Idle,
       
   175         Busy,
       
   176         Cancelled
       
   177     };
       
   178 
       
   179     State              m_state;
       
   180 
       
   181 #ifdef DUMP_SPECTRUMANALYSER
       
   182     QDir                m_outputDir;
       
   183     int                 m_count;
       
   184     QFile               m_textFile;
       
   185     QTextStream         m_textStream;
       
   186 #endif
       
   187 };
       
   188 
       
   189 #endif // SPECTRUMANALYSER_H
       
   190