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 | #include "spectrumanalyser.h" |
52 | #include "utils.h" |
53 | #include "fftreal_wrapper.h" |
54 | |
55 | #include <qmath.h> |
56 | #include <qmetatype.h> |
57 | #include <QAudioFormat> |
58 | #include <QThread> |
59 | |
60 | SpectrumAnalyserThread::SpectrumAnalyserThread(QObject *parent) |
61 | : QObject(parent) |
62 | #ifndef DISABLE_FFT |
63 | , m_fft(new FFTRealWrapper) |
64 | #endif |
65 | , m_numSamples(SpectrumLengthSamples) |
66 | , m_windowFunction(DefaultWindowFunction) |
67 | , m_window(SpectrumLengthSamples, 0.0) |
68 | , m_input(SpectrumLengthSamples, 0.0) |
69 | , m_output(SpectrumLengthSamples, 0.0) |
70 | , m_spectrum(SpectrumLengthSamples) |
71 | #ifdef SPECTRUM_ANALYSER_SEPARATE_THREAD |
72 | , m_thread(new QThread(this)) |
73 | #endif |
74 | { |
75 | #ifdef SPECTRUM_ANALYSER_SEPARATE_THREAD |
76 | // moveToThread() cannot be called on a QObject with a parent |
77 | setParent(0); |
78 | moveToThread(thread: m_thread); |
79 | m_thread->start(); |
80 | #endif |
81 | calculateWindow(); |
82 | } |
83 | |
84 | SpectrumAnalyserThread::~SpectrumAnalyserThread() |
85 | { |
86 | #ifndef DISABLE_FFT |
87 | delete m_fft; |
88 | #endif |
89 | } |
90 | |
91 | void SpectrumAnalyserThread::setWindowFunction(WindowFunction type) |
92 | { |
93 | m_windowFunction = type; |
94 | calculateWindow(); |
95 | } |
96 | |
97 | void SpectrumAnalyserThread::calculateWindow() |
98 | { |
99 | for (int i=0; i<m_numSamples; ++i) { |
100 | DataType x = 0.0; |
101 | |
102 | switch (m_windowFunction) { |
103 | case NoWindow: |
104 | x = 1.0; |
105 | break; |
106 | case HannWindow: |
107 | x = 0.5 * (1 - qCos(v: (2 * M_PI * i) / (m_numSamples - 1))); |
108 | break; |
109 | default: |
110 | Q_ASSERT(false); |
111 | } |
112 | |
113 | m_window[i] = x; |
114 | } |
115 | } |
116 | |
117 | void SpectrumAnalyserThread::calculateSpectrum(const QByteArray &buffer, |
118 | int inputFrequency, |
119 | int bytesPerSample) |
120 | { |
121 | #ifndef DISABLE_FFT |
122 | Q_ASSERT(buffer.size() == m_numSamples * bytesPerSample); |
123 | |
124 | // Initialize data array |
125 | const char *ptr = buffer.constData(); |
126 | for (int i=0; i<m_numSamples; ++i) { |
127 | const qint16 pcmSample = *reinterpret_cast<const qint16*>(ptr); |
128 | // Scale down to range [-1.0, 1.0] |
129 | const DataType realSample = pcmToReal(pcm: pcmSample); |
130 | const DataType windowedSample = realSample * m_window[i]; |
131 | m_input[i] = windowedSample; |
132 | ptr += bytesPerSample; |
133 | } |
134 | |
135 | // Calculate the FFT |
136 | m_fft->calculateFFT(in: m_output.data(), out: m_input.data()); |
137 | |
138 | // Analyze output to obtain amplitude and phase for each frequency |
139 | for (int i=2; i<=m_numSamples/2; ++i) { |
140 | // Calculate frequency of this complex sample |
141 | m_spectrum[i].frequency = qreal(i * inputFrequency) / (m_numSamples); |
142 | |
143 | const qreal real = m_output[i]; |
144 | qreal imag = 0.0; |
145 | if (i>0 && i<m_numSamples/2) |
146 | imag = m_output[m_numSamples/2 + i]; |
147 | |
148 | const qreal magnitude = qSqrt(v: real*real + imag*imag); |
149 | qreal amplitude = SpectrumAnalyserMultiplier * qLn(v: magnitude); |
150 | |
151 | // Bound amplitude to [0.0, 1.0] |
152 | m_spectrum[i].clipped = (amplitude > 1.0); |
153 | amplitude = qMax(a: qreal(0.0), b: amplitude); |
154 | amplitude = qMin(a: qreal(1.0), b: amplitude); |
155 | m_spectrum[i].amplitude = amplitude; |
156 | } |
157 | #endif |
158 | |
159 | emit calculationComplete(spectrum: m_spectrum); |
160 | } |
161 | |
162 | |
163 | //============================================================================= |
164 | // SpectrumAnalyser |
165 | //============================================================================= |
166 | |
167 | SpectrumAnalyser::SpectrumAnalyser(QObject *parent) |
168 | : QObject(parent) |
169 | , m_thread(new SpectrumAnalyserThread(this)) |
170 | , m_state(Idle) |
171 | #ifdef DUMP_SPECTRUMANALYSER |
172 | , m_count(0) |
173 | #endif |
174 | { |
175 | connect(sender: m_thread, signal: &SpectrumAnalyserThread::calculationComplete, |
176 | receiver: this, slot: &SpectrumAnalyser::calculationComplete); |
177 | } |
178 | |
179 | SpectrumAnalyser::~SpectrumAnalyser() |
180 | { |
181 | |
182 | } |
183 | |
184 | #ifdef DUMP_SPECTRUMANALYSER |
185 | void SpectrumAnalyser::setOutputPath(const QString &outputDir) |
186 | { |
187 | m_outputDir.setPath(outputDir); |
188 | m_textFile.setFileName(m_outputDir.filePath("spectrum.txt" )); |
189 | m_textFile.open(QIODevice::WriteOnly | QIODevice::Text); |
190 | m_textStream.setDevice(&m_textFile); |
191 | } |
192 | #endif |
193 | |
194 | //----------------------------------------------------------------------------- |
195 | // Public functions |
196 | //----------------------------------------------------------------------------- |
197 | |
198 | void SpectrumAnalyser::setWindowFunction(WindowFunction type) |
199 | { |
200 | const bool b = QMetaObject::invokeMethod(obj: m_thread, member: "setWindowFunction" , |
201 | type: Qt::AutoConnection, |
202 | Q_ARG(WindowFunction, type)); |
203 | Q_ASSERT(b); |
204 | Q_UNUSED(b) // suppress warnings in release builds |
205 | } |
206 | |
207 | void SpectrumAnalyser::calculate(const QByteArray &buffer, |
208 | const QAudioFormat &format) |
209 | { |
210 | // QThread::currentThread is marked 'for internal use only', but |
211 | // we're only using it for debug output here, so it's probably OK :) |
212 | SPECTRUMANALYSER_DEBUG << "SpectrumAnalyser::calculate" |
213 | << QThread::currentThread() |
214 | << "state" << m_state; |
215 | |
216 | if (isReady()) { |
217 | Q_ASSERT(isPCMS16LE(format)); |
218 | |
219 | const int bytesPerSample = format.sampleSize() * format.channelCount() / 8; |
220 | |
221 | #ifdef DUMP_SPECTRUMANALYSER |
222 | m_count++; |
223 | const QString pcmFileName = m_outputDir.filePath(QString("spectrum_%1.pcm" ).arg(m_count, 4, 10, QChar('0'))); |
224 | QFile pcmFile(pcmFileName); |
225 | pcmFile.open(QIODevice::WriteOnly); |
226 | const int bufferLength = m_numSamples * bytesPerSample; |
227 | pcmFile.write(buffer, bufferLength); |
228 | |
229 | m_textStream << "TimeDomain " << m_count << "\n" ; |
230 | const qint16* input = reinterpret_cast<const qint16*>(buffer); |
231 | for (int i=0; i<m_numSamples; ++i) { |
232 | m_textStream << i << "\t" << *input << "\n" ; |
233 | input += format.channels(); |
234 | } |
235 | #endif |
236 | |
237 | m_state = Busy; |
238 | |
239 | // Invoke SpectrumAnalyserThread::calculateSpectrum using QMetaObject. If |
240 | // m_thread is in a different thread from the current thread, the |
241 | // calculation will be done in the child thread. |
242 | // Once the calculation is finished, a calculationChanged signal will be |
243 | // emitted by m_thread. |
244 | const bool b = QMetaObject::invokeMethod(obj: m_thread, member: "calculateSpectrum" , |
245 | type: Qt::AutoConnection, |
246 | Q_ARG(QByteArray, buffer), |
247 | Q_ARG(int, format.sampleRate()), |
248 | Q_ARG(int, bytesPerSample)); |
249 | Q_ASSERT(b); |
250 | Q_UNUSED(b) // suppress warnings in release builds |
251 | |
252 | #ifdef DUMP_SPECTRUMANALYSER |
253 | m_textStream << "FrequencySpectrum " << m_count << "\n" ; |
254 | FrequencySpectrum::const_iterator x = m_spectrum.begin(); |
255 | for (int i=0; i<m_numSamples; ++i, ++x) |
256 | m_textStream << i << "\t" |
257 | << x->frequency << "\t" |
258 | << x->amplitude<< "\t" |
259 | << x->phase << "\n" ; |
260 | #endif |
261 | } |
262 | } |
263 | |
264 | bool SpectrumAnalyser::isReady() const |
265 | { |
266 | return (Idle == m_state); |
267 | } |
268 | |
269 | void SpectrumAnalyser::cancelCalculation() |
270 | { |
271 | if (Busy == m_state) |
272 | m_state = Cancelled; |
273 | } |
274 | |
275 | |
276 | //----------------------------------------------------------------------------- |
277 | // Private slots |
278 | //----------------------------------------------------------------------------- |
279 | |
280 | void SpectrumAnalyser::calculationComplete(const FrequencySpectrum &spectrum) |
281 | { |
282 | Q_ASSERT(Idle != m_state); |
283 | if (Busy == m_state) |
284 | emit spectrumChanged(spectrum); |
285 | m_state = Idle; |
286 | } |
287 | |