-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAudioOutputQt6.cpp
62 lines (50 loc) · 1.12 KB
/
AudioOutputQt6.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
#include "AudioOutputQt6.h"
#include "Audio.h"
#include <QAudioOutput>
#include <QAudioSink>
#include <QDebug>
#include <QMediaDevices>
#include <memory>
// AudioOutput
struct AudioOutput::Private {
QAudioDevice device;
std::shared_ptr<QAudioOutput> output;
std::shared_ptr<QAudioSink> sink;
};
AudioOutput::AudioOutput()
: m(new Private)
{
}
AudioOutput::~AudioOutput()
{
delete m;
}
QString AudioOutput::description()
{
return m->device.description();
}
void AudioOutput::start(AudioDevice const &dev, const QAudioFormat &format, QIODevice *out)
{
stop();
RECOMMENDED_BUFFER_SIZE = format.bytesForFrames(1) * format.sampleRate() / 20;
m->device = dev.device_;
m->output = std::make_shared<QAudioOutput>(m->device);
m->sink = std::make_shared<QAudioSink>(m->device, format);
m->sink->start(out);
}
void AudioOutput::stop()
{
m->device = {};
m->output.reset();
m->sink.reset();
}
int AudioOutput::bytesFree(OutputBuffer *out) const
{
int n = (int)out->queue_.size();
if (n < RECOMMENDED_BUFFER_SIZE) {
n = RECOMMENDED_BUFFER_SIZE - n;
n = std::min(n, (int)m->sink->bytesFree());
return n;
}
return 0;
}