-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudiocapture.cpp
More file actions
156 lines (127 loc) · 3.76 KB
/
audiocapture.cpp
File metadata and controls
156 lines (127 loc) · 3.76 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "audiocapture.h"
#include <QByteArray>
AudioCapture::AudioCapture(QObject *parent)
: QObject(parent) {}
bool AudioCapture::prepare(const QString &deviceId) {
stop();
prepared = false;
audioSpec = AudioSpec{};
if (!selectDevice(deviceId))
return false;
if (!selectFormat())
return false;
prepared = true;
return true;
}
bool AudioCapture::start() {
if (!prepared)
return false;
if (audioIODevice != nullptr) {
return true;
}
// Ensure previous input is fully released
releaseInput();
audioSource = new QAudioSource(audioDevice, audioFormat, this);
audioIODevice = audioSource->start();
if (!audioIODevice) {
emit errorOccurred(tr("Failed to start audio input."));
releaseInput();
return false;
}
connect(audioIODevice, &QIODevice::readyRead, this, &AudioCapture::handleReadyRead, Qt::UniqueConnection);
return true;
}
void AudioCapture::stop() {
releaseInput();
}
AudioSpec AudioCapture::spec() const {
return audioSpec;
}
QAudioFormat AudioCapture::format() const {
return audioFormat;
}
bool AudioCapture::isPrepared() const {
return prepared;
}
void AudioCapture::handleReadyRead() {
if (!audioIODevice)
return;
const QByteArray data = audioIODevice->readAll();
if (!data.isEmpty()) {
emit audioDataReady(data);
}
}
bool AudioCapture::selectDevice(const QString &deviceId) {
audioDevice = QAudioDevice();
if (!deviceId.isEmpty()) {
const QByteArray id = QByteArray::fromHex(deviceId.toLatin1());
const auto devices = QMediaDevices::audioInputs();
for (const auto &device : devices) {
if (device.id() == id) {
audioDevice = device;
break;
}
}
}
if (audioDevice.isNull())
audioDevice = QMediaDevices::defaultAudioInput();
if (audioDevice.isNull()) {
emit errorOccurred(tr("No audio input device available."));
return false;
}
return true;
}
bool AudioCapture::selectFormat() {
QAudioFormat target;
target.setSampleRate(16000);
target.setChannelCount(1);
target.setSampleFormat(QAudioFormat::Int16);
if (audioDevice.isFormatSupported(target)) {
audioFormat = target;
} else {
audioFormat = audioDevice.preferredFormat();
}
bool ok = false;
const QString formatName = sonioxFormatFor(audioFormat, &ok);
if (!ok) {
emit errorOccurred(tr("Unsupported audio format for streaming."));
return false;
}
audioSpec.sampleRate = audioFormat.sampleRate();
audioSpec.channels = audioFormat.channelCount();
audioSpec.format = formatName;
return audioSpec.sampleRate > 0 && audioSpec.channels > 0;
}
QString AudioCapture::sonioxFormatFor(const QAudioFormat &format, bool *ok) const {
if (ok)
*ok = false;
switch (format.sampleFormat()) {
case QAudioFormat::UInt8:
if (ok) *ok = true;
return QStringLiteral("pcm_u8");
case QAudioFormat::Int16:
if (ok) *ok = true;
return QStringLiteral("pcm_s16le");
case QAudioFormat::Int32:
if (ok) *ok = true;
return QStringLiteral("pcm_s32le");
case QAudioFormat::Float:
if (ok) *ok = true;
return QStringLiteral("pcm_f32le");
default:
break;
}
return QString();
}
void AudioCapture::releaseInput() {
// Disconnect any pending signals from old audioIODevice
if (audioIODevice) {
disconnect(audioIODevice, nullptr, this, nullptr);
audioIODevice = nullptr;
}
if (audioSource) {
audioSource->stop();
audioSource->deleteLater();
audioSource = nullptr;
}
}