diff options
author | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-03-11 09:11:37 +0200 |
---|---|---|
committer | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-03-11 09:11:37 +0200 |
commit | 0bb4f64c8fc3d0654454be4b9289abf5a5b9e0a2 (patch) | |
tree | 1fc002a63bce6ca204f178668a9ad8ea219a0286 /examples | |
parent | 7997c3aca1d6e03dd31e145d70a7a40df17e5330 (diff) |
Spectrum example now loops through sound files endlessly
Diffstat (limited to 'examples')
12 files changed, 46 insertions, 104 deletions
diff --git a/examples/datavis3d/spectrum/spectrum.pri b/examples/datavis3d/spectrum/spectrum.pri index c0829955..31a3767c 100644 --- a/examples/datavis3d/spectrum/spectrum.pri +++ b/examples/datavis3d/spectrum/spectrum.pri @@ -7,8 +7,8 @@ # Debug output from engine #DEFINES += LOG_ENGINE -# Disables rendering of the waveform -DEFINES += DISABLE_WAVEFORM +# Disable level calculation +DEFINES += DISABLE_LEVEL # Perform spectrum analysis calculation in a separate thread DEFINES += SPECTRUM_ANALYSER_SEPARATE_THREAD diff --git a/examples/datavis3d/spectrum/spectrumapp/engine.cpp b/examples/datavis3d/spectrum/spectrumapp/engine.cpp index 8f35331e..e655cbcb 100644 --- a/examples/datavis3d/spectrum/spectrumapp/engine.cpp +++ b/examples/datavis3d/spectrum/spectrumapp/engine.cpp @@ -334,10 +334,8 @@ void Engine::audioStateChanged(QAudio::State state) } if (QAudio::NoError != error) { reset(); -// emit playbackEnded(error); return; } -// emit playbackEnded(error); } setState(state); } diff --git a/examples/datavis3d/spectrum/spectrumapp/engine.h b/examples/datavis3d/spectrum/spectrumapp/engine.h index f9625677..8ff69165 100644 --- a/examples/datavis3d/spectrum/spectrumapp/engine.h +++ b/examples/datavis3d/spectrum/spectrumapp/engine.h @@ -53,14 +53,6 @@ #include <QObject> #include <QVector> -#ifdef DUMP_CAPTURED_AUDIO -#define DUMP_DATA -#endif - -#ifdef DUMP_SPECTRUM -#define DUMP_DATA -#endif - class FrequencySpectrum; QT_BEGIN_NAMESPACE class QAudioInput; @@ -272,10 +264,6 @@ private: int m_count; -#ifdef DUMP_DATA - QDir m_outputDir; -#endif - }; #endif // ENGINE_H diff --git a/examples/datavis3d/spectrum/spectrumapp/main.cpp b/examples/datavis3d/spectrum/spectrumapp/main.cpp index 59fd9ab5..a78de547 100644 --- a/examples/datavis3d/spectrum/spectrumapp/main.cpp +++ b/examples/datavis3d/spectrum/spectrumapp/main.cpp @@ -44,6 +44,7 @@ #include <QGuiApplication> #include <QAudio> +#include <QTimer> using namespace QtDataVis3D; @@ -53,20 +54,23 @@ public: MainApp(Q3DBars *window); ~MainApp(); - void start(); + void start(QString fileName); public slots: void spectrumChanged(qint64 position, qint64 length, const FrequencySpectrum &spectrum); void stateChanged(QAudio::Mode mode, QAudio::State state); +private slots: + void restart(); + private: int barIndex(qreal frequency) const; private: Q3DBars *m_chart; Engine *m_engine; - int m_infoMessageTimerId; + QTimer *m_restartTimer; // Lower bound of first band in the spectrum in Hz qreal m_lowFreq; // Upper band of last band in the spectrum in Hz @@ -76,24 +80,28 @@ private: MainApp::MainApp(Q3DBars *window) : m_chart(window) , m_engine(new Engine(this)) - , m_lowFreq(0.0) - , m_highFreq(1000.0) + , m_restartTimer(new QTimer(this)) + , m_lowFreq(SpectrumLowFreq) + , m_highFreq(SpectrumHighFreq) { m_chart->setupSampleSpace(QPoint(SpectrumNumBands, SpectrumNumBands*2)); - m_chart->setBarSpecs(QPointF(1.0f, 1.0f), QPointF(0.2f, 0.1f)); + m_chart->setBarSpecs(QPointF(1.0f, 0.75f), QPointF(0.2f, 0.1f)); m_chart->setBarType(Q3DBars::Bars, false); + QObject::connect(m_engine, &Engine::changedSpectrum, this, &MainApp::spectrumChanged); + QObject::connect(m_engine, &Engine::stateChanged, this, &MainApp::stateChanged); + m_restartTimer->setSingleShot(true); + QObject::connect(m_restartTimer, &QTimer::timeout, this, &MainApp::restart); } MainApp::~MainApp() { + delete m_engine; + delete m_restartTimer; } -void MainApp::start() +void MainApp::start(QString fileName) { - // create all necessary objects & connect signals - QObject::connect(m_engine, &Engine::changedSpectrum, this, &MainApp::spectrumChanged); - QObject::connect(m_engine, &Engine::stateChanged, this, &MainApp::stateChanged); - m_engine->loadFile(QStringLiteral(":/file1")); + m_engine->loadFile(fileName); m_engine->startPlayback(); } @@ -129,15 +137,30 @@ void MainApp::stateChanged(QAudio::Mode mode, QAudio::State state) qDebug() << "mode:" << mode << " state: " << state; // Restart once playback is finished if (QAudio::AudioOutput == mode && QAudio::StoppedState == state) { - // TODO: Doesn't work yet -> DEBUG - m_engine->reset(); - // TODO: Change song each time? - m_engine->loadFile(QStringLiteral(":/file1")); - m_engine->startPlayback(); + m_restartTimer->start(500); } } //----------------------------------------------------------------------------- +// Private slots +//----------------------------------------------------------------------------- + +void MainApp::restart() +{ + // Change file each time + QString fileToLoad = QStringLiteral(":/file"); + static int fileNo = 1; + QString nrStr; + nrStr.setNum(fileNo); + fileToLoad.append(nrStr); + qDebug() << fileToLoad; + start(fileToLoad); + fileNo++; + if (fileNo > 3) + fileNo = 1; +} + +//----------------------------------------------------------------------------- // Private functions //----------------------------------------------------------------------------- @@ -165,7 +188,7 @@ int main(int argc, char *argv[]) window.show(); MainApp *mainApp = new MainApp(&window); - mainApp->start(); + mainApp->start(QStringLiteral(":/file3")); return app.exec(); } diff --git a/examples/datavis3d/spectrum/spectrumapp/onclassical_demo_fiati-di-parma_thuille_terzo-tempo_sestetto_small-version.wav b/examples/datavis3d/spectrum/spectrumapp/onclassical_demo_fiati-di-parma_thuille_terzo-tempo_sestetto_small-version.wav Binary files differdeleted file mode 100644 index 1b6bbe57..00000000 --- a/examples/datavis3d/spectrum/spectrumapp/onclassical_demo_fiati-di-parma_thuille_terzo-tempo_sestetto_small-version.wav +++ /dev/null diff --git a/examples/datavis3d/spectrum/spectrumapp/soundFiles/Rockhop.wav b/examples/datavis3d/spectrum/spectrumapp/soundFiles/Rockhop.wav Binary files differnew file mode 100644 index 00000000..e56e1c0f --- /dev/null +++ b/examples/datavis3d/spectrum/spectrumapp/soundFiles/Rockhop.wav diff --git a/examples/datavis3d/spectrum/spectrumapp/soundFiles/futurebells_beat.wav b/examples/datavis3d/spectrum/spectrumapp/soundFiles/futurebells_beat.wav Binary files differnew file mode 100644 index 00000000..c45cbc71 --- /dev/null +++ b/examples/datavis3d/spectrum/spectrumapp/soundFiles/futurebells_beat.wav diff --git a/examples/datavis3d/spectrum/spectrumapp/soundFiles/onclassical_demo_fiati-di-parma_thuille_terzo-tempo_sestetto_small-version.wav b/examples/datavis3d/spectrum/spectrumapp/soundFiles/onclassical_demo_fiati-di-parma_thuille_terzo-tempo_sestetto_small-version.wav Binary files differnew file mode 100644 index 00000000..78b8dbda --- /dev/null +++ b/examples/datavis3d/spectrum/spectrumapp/soundFiles/onclassical_demo_fiati-di-parma_thuille_terzo-tempo_sestetto_small-version.wav diff --git a/examples/datavis3d/spectrum/spectrumapp/spectrum.h b/examples/datavis3d/spectrum/spectrumapp/spectrum.h index ae7d7e70..776a1289 100644 --- a/examples/datavis3d/spectrum/spectrumapp/spectrum.h +++ b/examples/datavis3d/spectrum/spectrumapp/spectrum.h @@ -53,7 +53,7 @@ const int SpectrumLengthSamples = PowerOfTwo<FFTLengthPowerOfTwo>::Result; // Number of bands in the frequency spectrum -const int SpectrumNumBands = 25; +const int SpectrumNumBands = 15; // Lower bound of first band in the spectrum const qreal SpectrumLowFreq = 0.0; // Hz diff --git a/examples/datavis3d/spectrum/spectrumapp/spectrum.qrc b/examples/datavis3d/spectrum/spectrumapp/spectrum.qrc index 321d08b8..9368abc7 100644 --- a/examples/datavis3d/spectrum/spectrumapp/spectrum.qrc +++ b/examples/datavis3d/spectrum/spectrumapp/spectrum.qrc @@ -1,5 +1,7 @@ <RCC> <qresource prefix="/"> - <file alias="file1">onclassical_demo_fiati-di-parma_thuille_terzo-tempo_sestetto_small-version.wav</file> + <file alias="file1">soundFiles/onclassical_demo_fiati-di-parma_thuille_terzo-tempo_sestetto_small-version.wav</file> + <file alias="file2">soundFiles/Rockhop.wav</file> + <file alias="file3">soundFiles/futurebells_beat.wav</file> </qresource> </RCC> diff --git a/examples/datavis3d/spectrum/spectrumapp/spectrumanalyser.cpp b/examples/datavis3d/spectrum/spectrumapp/spectrumanalyser.cpp index d3fe5114..e1dbb25b 100644 --- a/examples/datavis3d/spectrum/spectrumapp/spectrumanalyser.cpp +++ b/examples/datavis3d/spectrum/spectrumapp/spectrumanalyser.cpp @@ -49,9 +49,7 @@ SpectrumAnalyserThread::SpectrumAnalyserThread(QObject *parent) : QObject(parent) -#ifndef DISABLE_FFT , m_fft(new FFTRealWrapper) -#endif , m_numSamples(SpectrumLengthSamples) , m_windowFunction(DefaultWindowFunction) , m_window(SpectrumLengthSamples, 0.0) @@ -73,9 +71,7 @@ SpectrumAnalyserThread::SpectrumAnalyserThread(QObject *parent) SpectrumAnalyserThread::~SpectrumAnalyserThread() { -#ifndef DISABLE_FFT delete m_fft; -#endif } void SpectrumAnalyserThread::setWindowFunction(WindowFunction type) @@ -108,7 +104,6 @@ void SpectrumAnalyserThread::calculateSpectrum(const QByteArray &buffer, int inputFrequency, int bytesPerSample) { -#ifndef DISABLE_FFT Q_ASSERT(buffer.size() == m_numSamples * bytesPerSample); // Initialize data array @@ -144,7 +139,6 @@ void SpectrumAnalyserThread::calculateSpectrum(const QByteArray &buffer, amplitude = qMin(qreal(1.0), amplitude); m_spectrum[i].amplitude = amplitude; } -#endif emit calculationComplete(m_spectrum); } @@ -158,9 +152,6 @@ SpectrumAnalyser::SpectrumAnalyser(QObject *parent) : QObject(parent) , m_thread(new SpectrumAnalyserThread(this)) , m_state(Idle) -#ifdef DUMP_SPECTRUMANALYSER - , m_count(0) -#endif { CHECKED_CONNECT(m_thread, SIGNAL(calculationComplete(FrequencySpectrum)), this, SLOT(calculationComplete(FrequencySpectrum))); @@ -171,16 +162,6 @@ SpectrumAnalyser::~SpectrumAnalyser() } -#ifdef DUMP_SPECTRUMANALYSER -void SpectrumAnalyser::setOutputPath(const QString &outputDir) -{ - m_outputDir.setPath(outputDir); - m_textFile.setFileName(m_outputDir.filePath("spectrum.txt")); - m_textFile.open(QIODevice::WriteOnly | QIODevice::Text); - m_textStream.setDevice(&m_textFile); -} -#endif - //----------------------------------------------------------------------------- // Public functions //----------------------------------------------------------------------------- @@ -208,22 +189,6 @@ void SpectrumAnalyser::calculate(const QByteArray &buffer, const int bytesPerSample = format.sampleSize() * format.channelCount() / 8; -#ifdef DUMP_SPECTRUMANALYSER - m_count++; - const QString pcmFileName = m_outputDir.filePath(QString("spectrum_%1.pcm").arg(m_count, 4, 10, QChar('0'))); - QFile pcmFile(pcmFileName); - pcmFile.open(QIODevice::WriteOnly); - const int bufferLength = m_numSamples * bytesPerSample; - pcmFile.write(buffer, bufferLength); - - m_textStream << "TimeDomain " << m_count << "\n"; - const qint16* input = reinterpret_cast<const qint16*>(buffer); - for (int i=0; i<m_numSamples; ++i) { - m_textStream << i << "\t" << *input << "\n"; - input += format.channels(); - } -#endif - m_state = Busy; // Invoke SpectrumAnalyserThread::calculateSpectrum using QMetaObject. If @@ -238,16 +203,6 @@ void SpectrumAnalyser::calculate(const QByteArray &buffer, Q_ARG(int, bytesPerSample)); Q_ASSERT(b); Q_UNUSED(b) // suppress warnings in release builds - -#ifdef DUMP_SPECTRUMANALYSER - m_textStream << "FrequencySpectrum " << m_count << "\n"; - FrequencySpectrum::const_iterator x = m_spectrum.begin(); - for (int i=0; i<m_numSamples; ++i, ++x) - m_textStream << i << "\t" - << x->frequency << "\t" - << x->amplitude<< "\t" - << x->phase << "\n"; -#endif } } diff --git a/examples/datavis3d/spectrum/spectrumapp/spectrumanalyser.h b/examples/datavis3d/spectrum/spectrumapp/spectrumanalyser.h index 582d032d..a5761951 100644 --- a/examples/datavis3d/spectrum/spectrumapp/spectrumanalyser.h +++ b/examples/datavis3d/spectrum/spectrumapp/spectrumanalyser.h @@ -45,18 +45,10 @@ #include <QObject> #include <QVector> -#ifdef DUMP_SPECTRUMANALYSER -#include <QDir> -#include <QFile> -#include <QTextStream> -#endif - #include "frequencyspectrum.h" #include "spectrum.h" -#ifndef DISABLE_FFT #include "FFTRealFixLenParam.h" -#endif QT_FORWARD_DECLARE_CLASS(QAudioFormat) QT_FORWARD_DECLARE_CLASS(QThread) @@ -90,19 +82,14 @@ private: void calculateWindow(); private: -#ifndef DISABLE_FFT FFTRealWrapper* m_fft; -#endif const int m_numSamples; WindowFunction m_windowFunction; -#ifdef DISABLE_FFT - typedef qreal DataType; -#else typedef FFTRealFixLenParam::DataType DataType; -#endif + QVector<DataType> m_window; QVector<DataType> m_input; @@ -127,10 +114,6 @@ public: SpectrumAnalyser(QObject *parent = 0); ~SpectrumAnalyser(); -#ifdef DUMP_SPECTRUMANALYSER - void setOutputPath(const QString &outputPath); -#endif - public: /* * Set the windowing function which is applied before calculating the FFT @@ -183,13 +166,6 @@ private: }; State m_state; - -#ifdef DUMP_SPECTRUMANALYSER - QDir m_outputDir; - int m_count; - QFile m_textFile; - QTextStream m_textStream; -#endif }; #endif // SPECTRUMANALYSER_H |