Skip to content

Commit

Permalink
QThread::create Workaround for old Qt versions
Browse files Browse the repository at this point in the history
  • Loading branch information
he29-net committed Sep 22, 2019
1 parent 5668d42 commit 19b1528
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
8 changes: 3 additions & 5 deletions plugins/SpectrumAnalyzer/Analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@
#include "embed.h"
#include "lmms_basics.h"
#include "plugin_export.h"
#include <QThread>

#ifdef SA_DEBUG
#include <chrono>
#include <iostream>
#endif
#include <iostream>

extern "C" {
Plugin::Descriptor PLUGIN_EXPORT analyzer_plugin_descriptor =
Expand All @@ -58,20 +56,20 @@ Analyzer::Analyzer(Model *parent, const Plugin::Descriptor::SubPluginFeatures::K
Effect(&analyzer_plugin_descriptor, parent, key),
m_processor(&m_controls),
m_controls(this),
m_processorThread(m_processor, m_inputBuffer, m_notifier),
// Buffer is sized to cover 4* the current maximum LMMS audio buffer size,
// so that it has some reserve space in case data processor is busy.
m_inputBuffer(4 * m_maxBufferSize)
{
m_processorThread = QThread::create([=]{m_processor.analyse(m_inputBuffer, m_notifier);});
m_processorThread->start();
m_processorThread.start();
}


Analyzer::~Analyzer()
{
m_processor.terminate();
m_notifier.wakeAll();
m_processorThread->wait();
m_processorThread.wait();
}

// Take audio data and pass them to the spectrum processor.
Expand Down
9 changes: 7 additions & 2 deletions plugins/SpectrumAnalyzer/Analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
#ifndef ANALYZER_H
#define ANALYZER_H

#include "DataprocLauncher.h"
#include "Effect.h"
#include "SaControls.h"
#include "SaProcessor.h"
#include <QThread>
#include <QWaitCondition>
#include "../../src/3rdparty/ringbuffer/include/ringbuffer/ringbuffer.h"

Expand All @@ -53,7 +53,12 @@ class Analyzer : public Effect
// Maximum LMMS buffer size (hard coded, the actual constant is hard to get)
const unsigned int m_maxBufferSize = 4096;

QThread* m_processorThread;
// QThread::create() workaround
// Replace DataprocLauncher by QThread and replace initializer in constructor
// with the following commented line when LMMS CI starts using Qt > 5.9
//m_processorThread = QThread::create([=]{m_processor.analyse(m_inputBuffer, m_notifier);});
DataprocLauncher m_processorThread;

ringbuffer_t<sampleFrame> m_inputBuffer;
QWaitCondition m_notifier;

Expand Down
2 changes: 1 addition & 1 deletion plugins/SpectrumAnalyzer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ INCLUDE_DIRECTORIES(${FFTW3F_INCLUDE_DIRS})
LINK_LIBRARIES(${FFTW3F_LIBRARIES} ringbuffer)

BUILD_PLUGIN(analyzer Analyzer.cpp SaProcessor.cpp SaControls.cpp SaControlsDialog.cpp SaSpectrumView.cpp SaWaterfallView.cpp
MOCFILES SaProcessor.h SaControls.h SaControlsDialog.h SaSpectrumView.h SaWaterfallView.h EMBEDDED_RESOURCES *.svg logo.png)
MOCFILES SaProcessor.h SaControls.h SaControlsDialog.h SaSpectrumView.h SaWaterfallView.h DataprocLauncher.h EMBEDDED_RESOURCES *.svg logo.png)
55 changes: 55 additions & 0 deletions plugins/SpectrumAnalyzer/DataprocLauncher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* DataprocLauncher.h - QThread::create workaround for older Qt version
*
* Copyright (c) 2019 Martin Pavelek <he29/dot/HS/at/gmail/dot/com>
*
* This file is part of LMMS - https://lmms.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/

#ifndef DATAPROCLAUNCHER_H
#define DATAPROCLAUNCHER_H

#include <QThread>
#include <QWaitCondition>

#include "SaProcessor.h"
#include "../../src/3rdparty/ringbuffer/include/ringbuffer/ringbuffer.h"

class DataprocLauncher : public QThread
{
public:
explicit DataprocLauncher(SaProcessor &proc, ringbuffer_t<sampleFrame> &buffer, QWaitCondition &notifier)
: m_processor(&proc),
m_inputBuffer(&buffer),
m_notifier(&notifier)
{
}

private:
void run() override
{
m_processor->analyse(*m_inputBuffer, *m_notifier);
}

SaProcessor *m_processor;
ringbuffer_t<sampleFrame> *m_inputBuffer;
QWaitCondition *m_notifier;
};

#endif // DATAPROCLAUNCHER_H

0 comments on commit 19b1528

Please sign in to comment.