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