plugin: abstract audioprovider
This commit is contained in:
parent
c99e05026f
commit
af43c1166e
5 changed files with 131 additions and 76 deletions
|
|
@ -4,7 +4,7 @@ pkg_check_modules(AUBIO REQUIRED aubio)
|
||||||
|
|
||||||
qt_add_qml_module(caelestia
|
qt_add_qml_module(caelestia
|
||||||
URI Caelestia
|
URI Caelestia
|
||||||
VERSION 0.1
|
VERSION ${VERSION}
|
||||||
SOURCES
|
SOURCES
|
||||||
cutils.hpp cutils.cpp
|
cutils.hpp cutils.cpp
|
||||||
cachingimagemanager.hpp cachingimagemanager.cpp
|
cachingimagemanager.hpp cachingimagemanager.cpp
|
||||||
|
|
@ -13,6 +13,7 @@ qt_add_qml_module(caelestia
|
||||||
beattracker.hpp beattracker.cpp
|
beattracker.hpp beattracker.cpp
|
||||||
service.hpp service.cpp
|
service.hpp service.cpp
|
||||||
serviceref.hpp serviceref.cpp
|
serviceref.hpp serviceref.cpp
|
||||||
|
audioprovider.hpp audioprovider.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
qt_query_qml_module(caelestia
|
qt_query_qml_module(caelestia
|
||||||
|
|
|
||||||
78
plugin/src/Caelestia/audioprovider.cpp
Normal file
78
plugin/src/Caelestia/audioprovider.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
#include "audioprovider.hpp"
|
||||||
|
|
||||||
|
#include "service.hpp"
|
||||||
|
#include <QAudioSource>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QIODevice>
|
||||||
|
#include <QMediaDevices>
|
||||||
|
#include <QObject>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace caelestia {
|
||||||
|
|
||||||
|
AudioProvider::AudioProvider(int sampleRate, int hopSize, QObject* parent)
|
||||||
|
: Service(parent)
|
||||||
|
, m_hopSize(hopSize) {
|
||||||
|
QAudioFormat format;
|
||||||
|
format.setSampleRate(sampleRate);
|
||||||
|
format.setChannelCount(1);
|
||||||
|
format.setSampleFormat(QAudioFormat::Int16);
|
||||||
|
|
||||||
|
m_source = new QAudioSource(QMediaDevices::defaultAudioInput(), format, this);
|
||||||
|
connect(m_source, &QAudioSource::stateChanged, this, &AudioProvider::handleStateChanged);
|
||||||
|
};
|
||||||
|
|
||||||
|
AudioProvider::~AudioProvider() {
|
||||||
|
m_source->stop();
|
||||||
|
delete m_source;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioProvider::start() {
|
||||||
|
m_device = m_source->start();
|
||||||
|
connect(m_device, &QIODevice::readyRead, this, &AudioProvider::processData);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioProvider::stop() {
|
||||||
|
m_source->stop();
|
||||||
|
m_device = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> void AudioProvider::process(T* outBuf) {
|
||||||
|
const QByteArray data = m_device->readAll();
|
||||||
|
const int16_t* samples = reinterpret_cast<const int16_t*>(data.constData());
|
||||||
|
const size_t count = static_cast<size_t>(data.size()) / sizeof(int16_t);
|
||||||
|
const size_t hopSize = static_cast<size_t>(m_hopSize);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < count; ++i) {
|
||||||
|
outBuf[i % hopSize] = static_cast<T>(samples[i] / 32768.0);
|
||||||
|
if ((i + 1) % hopSize == 0) {
|
||||||
|
consumeData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template void AudioProvider::process(float* outBuf);
|
||||||
|
template void AudioProvider::process(double* outBuf);
|
||||||
|
|
||||||
|
void AudioProvider::handleStateChanged(QtAudio::State state) const {
|
||||||
|
if (state == QtAudio::StoppedState && m_source->error() != QtAudio::NoError) {
|
||||||
|
switch (m_source->error()) {
|
||||||
|
case QtAudio::OpenError:
|
||||||
|
qWarning() << "AudioProvider: failed to open audio device";
|
||||||
|
break;
|
||||||
|
case QtAudio::IOError:
|
||||||
|
qWarning() << "AudioProvider: an error occurred during read/write of audio device";
|
||||||
|
break;
|
||||||
|
case QtAudio::UnderrunError:
|
||||||
|
qWarning() << "AudioProvider: audio data is not being fed to audio device fast enough";
|
||||||
|
break;
|
||||||
|
case QtAudio::FatalError:
|
||||||
|
qCritical() << "AudioProvider: fatal error in audio device";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace caelestia
|
||||||
36
plugin/src/Caelestia/audioprovider.hpp
Normal file
36
plugin/src/Caelestia/audioprovider.hpp
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "service.hpp"
|
||||||
|
#include <QAudioSource>
|
||||||
|
#include <QIODevice>
|
||||||
|
#include <QObject>
|
||||||
|
#include <qqmlintegration.h>
|
||||||
|
|
||||||
|
namespace caelestia {
|
||||||
|
|
||||||
|
class AudioProvider : public Service {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit AudioProvider(int sampleRate = 44100, int hopSize = 512, QObject* parent = nullptr);
|
||||||
|
~AudioProvider();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
int m_hopSize;
|
||||||
|
|
||||||
|
template <typename T> void process(T* outBuf);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QAudioSource* m_source;
|
||||||
|
QIODevice* m_device;
|
||||||
|
|
||||||
|
void start() override;
|
||||||
|
void stop() override;
|
||||||
|
|
||||||
|
void handleStateChanged(QtAudio::State state) const;
|
||||||
|
|
||||||
|
virtual void processData() = 0;
|
||||||
|
virtual void consumeData() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace caelestia
|
||||||
|
|
@ -1,90 +1,38 @@
|
||||||
#include "beattracker.hpp"
|
#include "beattracker.hpp"
|
||||||
|
|
||||||
#include "service.hpp"
|
#include "audioprovider.hpp"
|
||||||
#include <QAudioSource>
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QIODevice>
|
|
||||||
#include <QMediaDevices>
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <aubio/aubio.h>
|
#include <aubio/aubio.h>
|
||||||
|
|
||||||
namespace caelestia {
|
namespace caelestia {
|
||||||
|
|
||||||
BeatTracker::BeatTracker(uint_t sampleRate, uint_t hopSize, QObject* parent)
|
BeatTracker::BeatTracker(uint_t sampleRate, uint_t hopSize, QObject* parent)
|
||||||
: Service(parent)
|
: AudioProvider(static_cast<int>(sampleRate), static_cast<int>(hopSize), parent)
|
||||||
, m_tempo(new_aubio_tempo("default", 1024, hopSize, sampleRate))
|
, m_tempo(new_aubio_tempo("default", 1024, hopSize, sampleRate))
|
||||||
, m_in(new_fvec(hopSize))
|
, m_in(new_fvec(hopSize))
|
||||||
, m_out(new_fvec(2))
|
, m_out(new_fvec(2))
|
||||||
, m_hopSize(hopSize)
|
, m_bpm(120) {};
|
||||||
, m_bpm(120) {
|
|
||||||
QAudioFormat format;
|
|
||||||
format.setSampleRate(static_cast<int>(sampleRate));
|
|
||||||
format.setChannelCount(1);
|
|
||||||
format.setSampleFormat(QAudioFormat::Int16);
|
|
||||||
|
|
||||||
m_source = new QAudioSource(QMediaDevices::defaultAudioInput(), format, this);
|
|
||||||
connect(m_source, &QAudioSource::stateChanged, this, &BeatTracker::handleStateChanged);
|
|
||||||
};
|
|
||||||
|
|
||||||
BeatTracker::~BeatTracker() {
|
BeatTracker::~BeatTracker() {
|
||||||
del_aubio_tempo(m_tempo);
|
del_aubio_tempo(m_tempo);
|
||||||
del_fvec(m_in);
|
del_fvec(m_in);
|
||||||
del_fvec(m_out);
|
del_fvec(m_out);
|
||||||
|
|
||||||
m_source->stop();
|
|
||||||
delete m_source;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
smpl_t BeatTracker::bpm() const {
|
smpl_t BeatTracker::bpm() const {
|
||||||
return m_bpm;
|
return m_bpm;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BeatTracker::start() {
|
void BeatTracker::processData() {
|
||||||
m_device = m_source->start();
|
process(m_in->data);
|
||||||
connect(m_device, &QIODevice::readyRead, this, &BeatTracker::process);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BeatTracker::stop() {
|
void BeatTracker::consumeData() {
|
||||||
m_source->stop();
|
aubio_tempo_do(m_tempo, m_in, m_out);
|
||||||
m_device = nullptr;
|
if (m_out->data[0] != 0.0f) {
|
||||||
}
|
m_bpm = aubio_tempo_get_bpm(m_tempo);
|
||||||
|
emit bpmChanged();
|
||||||
void BeatTracker::process() {
|
emit beat(m_bpm);
|
||||||
const QByteArray data = m_device->readAll();
|
|
||||||
const int16_t* samples = reinterpret_cast<const int16_t*>(data.constData());
|
|
||||||
const size_t count = static_cast<size_t>(data.size()) / sizeof(int16_t);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < count; ++i) {
|
|
||||||
m_in->data[i % m_hopSize] = samples[i] / 32768.0f;
|
|
||||||
if ((i + 1) % m_hopSize == 0) {
|
|
||||||
aubio_tempo_do(m_tempo, m_in, m_out);
|
|
||||||
if (m_out->data[0] != 0.0f) {
|
|
||||||
m_bpm = aubio_tempo_get_bpm(m_tempo);
|
|
||||||
emit bpmChanged();
|
|
||||||
emit beat(m_bpm);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BeatTracker::handleStateChanged(QtAudio::State state) const {
|
|
||||||
if (state == QtAudio::StoppedState && m_source->error() != QtAudio::NoError) {
|
|
||||||
switch (m_source->error()) {
|
|
||||||
case QtAudio::OpenError:
|
|
||||||
qWarning() << "BeatTracker: failed to open audio device";
|
|
||||||
break;
|
|
||||||
case QtAudio::IOError:
|
|
||||||
qWarning() << "BeatTracker: an error occurred during read/write of audio device";
|
|
||||||
break;
|
|
||||||
case QtAudio::UnderrunError:
|
|
||||||
qWarning() << "BeatTracker: audio data is not being fed to audio device fast enough";
|
|
||||||
break;
|
|
||||||
case QtAudio::FatalError:
|
|
||||||
qCritical() << "BeatTracker: fatal error in audio device";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,13 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "service.hpp"
|
#include "audioprovider.hpp"
|
||||||
#include <QAudioSource>
|
|
||||||
#include <QIODevice>
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <aubio/aubio.h>
|
#include <aubio/aubio.h>
|
||||||
#include <qqmlintegration.h>
|
#include <qqmlintegration.h>
|
||||||
|
|
||||||
namespace caelestia {
|
namespace caelestia {
|
||||||
|
|
||||||
class BeatTracker : public Service {
|
class BeatTracker : public AudioProvider {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
QML_ELEMENT
|
QML_ELEMENT
|
||||||
QML_SINGLETON
|
QML_SINGLETON
|
||||||
|
|
@ -27,20 +25,14 @@ signals:
|
||||||
void beat(smpl_t bpm);
|
void beat(smpl_t bpm);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QAudioSource* m_source;
|
|
||||||
QIODevice* m_device;
|
|
||||||
|
|
||||||
aubio_tempo_t* m_tempo;
|
aubio_tempo_t* m_tempo;
|
||||||
fvec_t* m_in;
|
fvec_t* m_in;
|
||||||
fvec_t* m_out;
|
fvec_t* m_out;
|
||||||
uint_t m_hopSize;
|
|
||||||
|
|
||||||
smpl_t m_bpm;
|
smpl_t m_bpm;
|
||||||
|
|
||||||
void start() override;
|
void processData() override;
|
||||||
void stop() override;
|
void consumeData() override;
|
||||||
void process();
|
|
||||||
void handleStateChanged(QtAudio::State state) const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace caelestia
|
} // namespace caelestia
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue