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