1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include "audioprovider.hpp"
#include "audiocollector.hpp"
#include "service.hpp"
#include <qdebug.h>
#include <qthread.h>
namespace caelestia {
AudioProcessor::AudioProcessor(QObject* parent)
: QObject(parent)
, m_sampleRate(AudioCollector::instance()->sampleRate())
, m_chunkSize(AudioCollector::instance()->chunkSize()) {}
AudioProcessor::~AudioProcessor() {
stop();
}
void AudioProcessor::init() {
m_timer = new QTimer(this);
m_timer->setInterval(static_cast<int>(m_chunkSize * 1000.0 / m_sampleRate));
connect(m_timer, &QTimer::timeout, this, &AudioProcessor::process);
}
void AudioProcessor::start() {
AudioCollector::instance()->ref();
if (m_timer) {
m_timer->start();
}
}
void AudioProcessor::stop() {
if (m_timer) {
m_timer->stop();
}
AudioCollector::instance()->unref();
}
AudioProvider::AudioProvider(QObject* parent)
: Service(parent)
, m_processor(nullptr)
, m_thread(nullptr) {}
AudioProvider::~AudioProvider() {
if (m_thread) {
m_thread->quit();
m_thread->wait();
}
}
void AudioProvider::init() {
if (!m_processor) {
qWarning() << "AudioProvider::init: attempted to init with no processor set";
return;
}
m_thread = new QThread(this);
m_processor->moveToThread(m_thread);
connect(m_thread, &QThread::started, m_processor, &AudioProcessor::init);
connect(m_thread, &QThread::finished, m_processor, &AudioProcessor::deleteLater);
connect(m_thread, &QThread::finished, m_thread, &QThread::deleteLater);
m_thread->start();
}
void AudioProvider::start() {
if (m_processor) {
QMetaObject::invokeMethod(m_processor, "start", Qt::QueuedConnection);
}
}
void AudioProvider::stop() {
if (m_processor) {
QMetaObject::invokeMethod(m_processor, "stop", Qt::QueuedConnection);
}
}
} // namespace caelestia
|