-
Notifications
You must be signed in to change notification settings - Fork 5
/
basetonegenerator.cpp
81 lines (62 loc) · 2.26 KB
/
basetonegenerator.cpp
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
80
81
#include "basetonegenerator.h"
// Qt includes
#include <QAudioOutput>
//! private helper to allow QAudioOutput to read from a io device
class BaseToneGeneratorHelper : public QIODevice
{
public:
BaseToneGeneratorHelper(BaseToneGenerator &generator, QObject *parent = nullptr);
qint64 readData(char *data, qint64 maxlen) override;
qint64 writeData(const char *data, qint64 len) override;
private:
BaseToneGenerator &m_generator;
};
class BaseToneGenerator::BaseToneGeneratorPrivate
{
public:
BaseToneGeneratorPrivate(BaseToneGenerator &generator, const QAudioDeviceInfo &audioDeviceInfo, const QAudioFormat &format);
~BaseToneGeneratorPrivate() { output.stop(); }
BaseToneGeneratorHelper helper;
QAudioOutput output;
};
BaseToneGenerator::BaseToneGenerator() = default;
BaseToneGenerator::~BaseToneGenerator() = default;
void BaseToneGenerator::start()
{
Q_ASSERT(!running());
QAudioFormat format;
format.setSampleRate(m_samplerate);
format.setChannelCount(2);
format.setSampleSize(sizeof(SamplePair::Type) * 8);
format.setSampleType(QAudioFormat::SignedInt);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
m_private = std::make_unique<BaseToneGeneratorPrivate>(*this, m_device, format);
m_private->output.start(&m_private->helper);
}
void BaseToneGenerator::stop()
{
Q_ASSERT(running());
m_private = nullptr;
}
BaseToneGeneratorHelper::BaseToneGeneratorHelper(BaseToneGenerator &generator, QObject *parent) :
QIODevice{parent}, m_generator{generator}
{
setOpenMode(QIODevice::ReadOnly);
}
qint64 BaseToneGeneratorHelper::readData(char *data, qint64 maxlen)
{
Q_ASSERT(maxlen % sizeof(SamplePair) == 0);
return m_generator.fill(reinterpret_cast<SamplePair*>(data),
reinterpret_cast<SamplePair*>(data) + (maxlen/sizeof(SamplePair))) * sizeof(SamplePair);
}
qint64 BaseToneGeneratorHelper::writeData(const char *data, qint64 len)
{
Q_UNUSED(data)
Q_UNUSED(len)
qFatal("writing is not allowed!");
}
BaseToneGenerator::BaseToneGeneratorPrivate::BaseToneGeneratorPrivate(BaseToneGenerator &generator, const QAudioDeviceInfo &audioDeviceInfo, const QAudioFormat &format) :
helper{generator}, output{audioDeviceInfo, format}
{
}