diff options
Diffstat (limited to 'plugin/src/Caelestia')
| -rw-r--r-- | plugin/src/Caelestia/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | plugin/src/Caelestia/beattracker.cpp | 25 | ||||
| -rw-r--r-- | plugin/src/Caelestia/beattracker.hpp | 13 | ||||
| -rw-r--r-- | plugin/src/Caelestia/service.cpp | 39 | ||||
| -rw-r--r-- | plugin/src/Caelestia/service.hpp | 30 | ||||
| -rw-r--r-- | plugin/src/Caelestia/serviceref.cpp | 43 | ||||
| -rw-r--r-- | plugin/src/Caelestia/serviceref.hpp | 29 |
7 files changed, 150 insertions, 31 deletions
diff --git a/plugin/src/Caelestia/CMakeLists.txt b/plugin/src/Caelestia/CMakeLists.txt index cc8e567..464ed98 100644 --- a/plugin/src/Caelestia/CMakeLists.txt +++ b/plugin/src/Caelestia/CMakeLists.txt @@ -11,6 +11,8 @@ qt_add_qml_module(caelestia filesystemmodel.hpp filesystemmodel.cpp qalculator.hpp qalculator.cpp beattracker.hpp beattracker.cpp + service.hpp service.cpp + serviceref.hpp serviceref.cpp ) qt_query_qml_module(caelestia diff --git a/plugin/src/Caelestia/beattracker.cpp b/plugin/src/Caelestia/beattracker.cpp index 433ffab..aeca64b 100644 --- a/plugin/src/Caelestia/beattracker.cpp +++ b/plugin/src/Caelestia/beattracker.cpp @@ -1,5 +1,6 @@ #include "beattracker.hpp" +#include "service.hpp" #include <QAudioSource> #include <QDebug> #include <QIODevice> @@ -10,13 +11,12 @@ namespace caelestia { BeatTracker::BeatTracker(uint_t sampleRate, uint_t hopSize, QObject* parent) - : QObject(parent) + : Service(parent) , m_tempo(new_aubio_tempo("default", 1024, hopSize, sampleRate)) , m_in(new_fvec(hopSize)) , m_out(new_fvec(2)) , m_hopSize(hopSize) - , m_bpm(120) - , m_refCount(0) { + , m_bpm(120) { QAudioFormat format; format.setSampleRate(static_cast<int>(sampleRate)); format.setChannelCount(1); @@ -39,25 +39,6 @@ smpl_t BeatTracker::bpm() const { return m_bpm; } -int BeatTracker::refCount() const { - return m_refCount; -} - -void BeatTracker::setRefCount(int refCount) { - if (m_refCount == refCount) { - return; - } - - m_refCount = refCount; - emit refCountChanged(); - - if (m_refCount == 0) { - stop(); - } else if (!m_device) { - start(); - } -} - void BeatTracker::start() { m_device = m_source->start(); connect(m_device, &QIODevice::readyRead, this, &BeatTracker::process); diff --git a/plugin/src/Caelestia/beattracker.hpp b/plugin/src/Caelestia/beattracker.hpp index fda0ddc..7c7fada 100644 --- a/plugin/src/Caelestia/beattracker.hpp +++ b/plugin/src/Caelestia/beattracker.hpp @@ -1,5 +1,6 @@ #pragma once +#include "service.hpp" #include <QAudioSource> #include <QIODevice> #include <QObject> @@ -8,13 +9,12 @@ namespace caelestia { -class BeatTracker : public QObject { +class BeatTracker : public Service { Q_OBJECT QML_ELEMENT QML_SINGLETON Q_PROPERTY(smpl_t bpm READ bpm NOTIFY bpmChanged) - Q_PROPERTY(int refCount READ refCount WRITE setRefCount NOTIFY refCountChanged) public: explicit BeatTracker(uint_t sampleRate = 44100, uint_t hopSize = 512, QObject* parent = nullptr); @@ -22,12 +22,8 @@ public: [[nodiscard]] smpl_t bpm() const; - [[nodiscard]] int refCount() const; - void setRefCount(int refCount); - signals: void bpmChanged(); - void refCountChanged(); void beat(smpl_t bpm); private: @@ -40,10 +36,9 @@ private: uint_t m_hopSize; smpl_t m_bpm; - int m_refCount; - void start(); - void stop(); + void start() override; + void stop() override; void process(); void handleStateChanged(QtAudio::State state) const; }; diff --git a/plugin/src/Caelestia/service.cpp b/plugin/src/Caelestia/service.cpp new file mode 100644 index 0000000..9c13421 --- /dev/null +++ b/plugin/src/Caelestia/service.cpp @@ -0,0 +1,39 @@ +#include "service.hpp" + +#include <QDebug> +#include <QObject> + +namespace caelestia { + +Service::Service(QObject* parent) + : QObject(parent) + , m_refCount(0) {} + +int Service::refCount() const { + return m_refCount; +} + +void Service::ref() { + if (m_refCount == 0) { + start(); + } + + m_refCount++; + emit refCountChanged(); +} + +void Service::unref() { + if (m_refCount == 0) { + qWarning() << "ServiceRef::unref: attempted to unref service with no active refs"; + return; + } + + m_refCount--; + emit refCountChanged(); + + if (m_refCount == 0) { + stop(); + } +} + +} // namespace caelestia diff --git a/plugin/src/Caelestia/service.hpp b/plugin/src/Caelestia/service.hpp new file mode 100644 index 0000000..861d715 --- /dev/null +++ b/plugin/src/Caelestia/service.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include <QObject> + +namespace caelestia { + +class Service : public QObject { + Q_OBJECT + + Q_PROPERTY(int refCount READ refCount NOTIFY refCountChanged) + +public: + explicit Service(QObject* parent = nullptr); + + [[nodiscard]] int refCount() const; + + void ref(); + void unref(); + +signals: + void refCountChanged(); + +private: + int m_refCount; + + virtual void start() = 0; + virtual void stop() = 0; +}; + +} // namespace caelestia diff --git a/plugin/src/Caelestia/serviceref.cpp b/plugin/src/Caelestia/serviceref.cpp new file mode 100644 index 0000000..ebda663 --- /dev/null +++ b/plugin/src/Caelestia/serviceref.cpp @@ -0,0 +1,43 @@ +#include "serviceref.hpp" +#include "service.hpp" + +#include <QObject> + +namespace caelestia { + +ServiceRef::ServiceRef(Service* service, QObject* parent) + : QObject(parent) + , m_service(service) { + if (m_service) { + m_service->ref(); + } +} + +ServiceRef::~ServiceRef() { + if (m_service) { + m_service->unref(); + } +} + +Service* ServiceRef::service() const { + return m_service; +} + +void ServiceRef::setService(Service* service) { + if (m_service == service) { + return; + } + + if (m_service) { + m_service->unref(); + } + + m_service = service; + emit serviceChanged(); + + if (m_service) { + m_service->ref(); + } +} + +} // namespace caelestia diff --git a/plugin/src/Caelestia/serviceref.hpp b/plugin/src/Caelestia/serviceref.hpp new file mode 100644 index 0000000..fb48a47 --- /dev/null +++ b/plugin/src/Caelestia/serviceref.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "service.hpp" +#include <QObject> +#include <qqmlintegration.h> + +namespace caelestia { + +class ServiceRef : public QObject { + Q_OBJECT + QML_ELEMENT + + Q_PROPERTY(Service* service READ service WRITE setService NOTIFY serviceChanged) + +public: + explicit ServiceRef(Service* service = nullptr, QObject* parent = nullptr); + ~ServiceRef(); + + [[nodiscard]] Service* service() const; + void setService(Service* service); + +signals: + void serviceChanged(); + +private: + Service* m_service; +}; + +} // namespace caelestia |