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