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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
#include "soundout.h"
#include <QDateTime>
#include <QAudioDeviceInfo>
#include <QAudioOutput>
#include <QSysInfo>
#include <qmath.h>
#include <QDebug>
#include "Logger.hpp"
#include "Audio/AudioDevice.hpp"
#include "moc_soundout.cpp"
namespace
{
TxEvidence::TxBackendState txBackendState (QAudio::State state) noexcept
{
switch (state)
{
case QAudio::ActiveState: return TxEvidence::TxBackendState::Active;
case QAudio::IdleState: return TxEvidence::TxBackendState::Idle;
case QAudio::SuspendedState: return TxEvidence::TxBackendState::Suspended;
case QAudio::StoppedState: return TxEvidence::TxBackendState::Stopped;
#if QT_VERSION >= QT_VERSION_CHECK (5, 10, 0)
case QAudio::InterruptedState: return TxEvidence::TxBackendState::Interrupted;
#endif
}
return TxEvidence::TxBackendState::Unknown;
}
TxEvidence::TxBackendError txBackendError (QAudio::Error error) noexcept
{
switch (error)
{
case QAudio::NoError: return TxEvidence::TxBackendError::None;
case QAudio::OpenError: return TxEvidence::TxBackendError::Open;
case QAudio::IOError: return TxEvidence::TxBackendError::Io;
case QAudio::UnderrunError: return TxEvidence::TxBackendError::Underrun;
case QAudio::FatalError: return TxEvidence::TxBackendError::Fatal;
}
return TxEvidence::TxBackendError::Unknown;
}
}
bool SoundOutput::checkStream () const
{
bool result {false};
Q_ASSERT_X (m_stream, "SoundOutput", "programming error");
if (m_stream) {
switch (m_stream->error ())
{
case QAudio::OpenError:
Q_EMIT error (tr ("An error opening the audio output device has occurred."));
break;
case QAudio::IOError:
Q_EMIT error (tr ("An error occurred during write to the audio output device."));
break;
case QAudio::UnderrunError:
Q_EMIT error (tr ("Audio data not being fed to the audio output device fast enough."));
break;
case QAudio::FatalError:
Q_EMIT error (tr ("Non-recoverable error, audio output device not usable at this time."));
break;
case QAudio::NoError:
result = true;
break;
}
}
return result;
}
void SoundOutput::setFormat (QAudioDeviceInfo const& device, unsigned channels, int frames_buffered)
{
Q_ASSERT (0 < channels && channels < 3);
m_device = device;
m_channels = channels;
m_framesBuffered = frames_buffered;
}
void SoundOutput::restart (QIODevice * source)
{
++m_backendStartSequence;
bool failed {false};
if (!m_device.isNull ())
{
QAudioFormat format (m_device.preferredFormat ());
// qDebug () << "Preferred audio output format:" << format;
format.setChannelCount (m_channels);
format.setCodec ("audio/pcm");
format.setSampleRate (48000);
format.setSampleType (QAudioFormat::SignedInt);
format.setSampleSize (16);
format.setByteOrder (QAudioFormat::Endian (QSysInfo::ByteOrder));
if (!format.isValid ())
{
Q_EMIT error (tr ("Requested output audio format is not valid."));
failed = true;
}
else if (!m_device.isFormatSupported (format))
{
Q_EMIT error (tr ("Requested output audio format is not supported on device."));
failed = true;
}
else
{
// qDebug () << "Selected audio output format:" << format;
m_stream.reset (new QAudioOutput (m_device, format));
checkStream ();
m_stream->setVolume (m_volume);
m_stream->setNotifyInterval(1000);
error_ = false;
connect (m_stream.data(), &QAudioOutput::stateChanged, this, &SoundOutput::handleStateChanged);
connect (m_stream.data(), &QAudioOutput::notify, [this] () {
checkStream ();
publishRawTxPlayoutSnapshot ();
});
// qDebug() << "A" << m_volume << m_stream->notifyInterval();
}
}
else
{
failed = true;
}
if (failed)
{
publishUnavailableTxPlayoutSnapshot (true, tr ("Audio output restart attempt failed."));
}
if (!m_stream)
{
if (!error_)
{
error_ = true; // only signal error once
Q_EMIT error (tr ("No audio output device configured."));
}
if (!failed)
{
publishUnavailableTxPlayoutSnapshot (true, tr ("No audio output device configured."));
}
return;
}
else
{
error_ = false;
}
// we have to set this before every start on the stream because the
// Windows implementation seems to forget the buffer size after a
// stop.
//qDebug () << "SoundOut default buffer size (bytes):" << m_stream->bufferSize () << "period size:" << m_stream->periodSize ();
if (m_framesBuffered > 0)
{
m_stream->setBufferSize (m_stream->format().bytesForFrames (m_framesBuffered));
}
m_stream->setCategory ("production");
m_stream->start (source);
publishRawTxPlayoutSnapshot (true);
// LOG_DEBUG ("Selected buffer size (bytes): " << m_stream->bufferSize () << " period size: " << m_stream->periodSize ());
}
void SoundOutput::suspend ()
{
if (m_stream && QAudio::ActiveState == m_stream->state ())
{
m_stream->suspend ();
checkStream ();
publishRawTxPlayoutSnapshot ();
}
}
void SoundOutput::resume ()
{
if (m_stream && QAudio::SuspendedState == m_stream->state ())
{
m_stream->resume ();
checkStream ();
publishRawTxPlayoutSnapshot ();
}
}
void SoundOutput::reset ()
{
if (m_stream)
{
auto const beforeReset = makeRawTxPlayoutSnapshot ();
m_stream->reset ();
checkStream ();
Q_EMIT rawTxPlayoutSnapshot (beforeReset);
publishRawTxPlayoutSnapshot ();
}
else
{
publishRawTxPlayoutSnapshot ();
}
}
void SoundOutput::stop ()
{
if (m_stream)
{
auto const beforeStop = makeRawTxPlayoutSnapshot ();
m_stream->reset ();
m_stream->stop ();
Q_EMIT rawTxPlayoutSnapshot (beforeStop);
publishRawTxPlayoutSnapshot ();
}
else
{
publishRawTxPlayoutSnapshot ();
}
#ifdef __APPLE__
// this code is here to help certain rigs not drop audio, however on Sequoia this causes audio to drop, so we don't do it!
if( QOperatingSystemVersion::current() < QOperatingSystemVersion(QOperatingSystemVersion::MacOS, 15) )
m_stream.reset ();
#endif
}
qreal SoundOutput::attenuation () const
{
return -(20. * qLn (m_volume) / qLn (10.));
}
int SoundOutput::bufferSize () const
{
return m_stream ? m_stream->bufferSize () : 0;
}
TxEvidence::TxRawPlayoutSnapshot SoundOutput::makeRawTxPlayoutSnapshot (bool startEvent) const
{
TxEvidence::TxRawPlayoutSnapshot snapshot;
if (!m_stream) return snapshot;
QAudioFormat const format {m_stream->format ()};
qint64 const capacity {m_stream->bufferSize ()};
qint64 const available {m_stream->bytesFree ()};
snapshot.tier = TxEvidence::TxPlayoutTier::DeviceClock;
snapshot.backend_start_sequence = m_backendStartSequence;
snapshot.start_event = startEvent;
snapshot.available = true;
snapshot.state = txBackendState (m_stream->state ());
snapshot.error = txBackendError (m_stream->error ());
snapshot.available_bytes = available;
snapshot.buffered_bytes = capacity >= 0 && available >= 0 ? qMax<qint64> (0, capacity - available) : -1;
snapshot.capacity_bytes = capacity;
snapshot.processed_usecs = m_stream->processedUSecs ();
snapshot.elapsed_usecs = m_stream->elapsedUSecs ();
snapshot.channel_count = format.channelCount ();
snapshot.bytes_per_frame = format.bytesPerFrame ();
snapshot.sample_rate_hz = format.sampleRate ();
snapshot.report_interval_ms = m_stream->notifyInterval ();
return snapshot;
}
void SoundOutput::publishRawTxPlayoutSnapshot (bool startEvent) const
{
if (!m_stream)
{
publishUnavailableTxPlayoutSnapshot (startEvent, tr ("No audio output stream is active."));
return;
}
auto const snapshot = makeRawTxPlayoutSnapshot (startEvent);
Q_EMIT rawTxPlayoutSnapshot (snapshot);
}
void SoundOutput::publishUnavailableTxPlayoutSnapshot (bool startEvent, QString const& diagnostic) const
{
TxEvidence::TxRawPlayoutSnapshot snapshot;
snapshot.backend_start_sequence = m_backendStartSequence;
snapshot.start_event = startEvent;
snapshot.state = TxEvidence::TxBackendState::Unavailable;
snapshot.error = TxEvidence::TxBackendError::Open;
snapshot.diagnostic = diagnostic;
Q_EMIT rawTxPlayoutSnapshot (snapshot);
}
void SoundOutput::setAttenuation (qreal a)
{
Q_ASSERT (0. <= a && a <= 999.);
m_volume = qPow(10.0, -a/20.0);
// qDebug () << "SoundOut: attn = " << a << ", vol = " << m_volume;
if (m_stream)
{
m_stream->setVolume (m_volume);
}
}
void SoundOutput::resetAttenuation ()
{
m_volume = 1.;
if (m_stream)
{
m_stream->setVolume (m_volume);
}
}
void SoundOutput::handleStateChanged (QAudio::State newState)
{
switch (newState)
{
case QAudio::IdleState:
Q_EMIT status (tr ("Idle"));
Q_EMIT audioOutputIdle ();
break;
case QAudio::ActiveState:
Q_EMIT status (tr ("Sending"));
Q_EMIT audioOutputActive ();
break;
case QAudio::SuspendedState:
Q_EMIT status (tr ("Suspended"));
break;
#if QT_VERSION >= QT_VERSION_CHECK (5, 10, 0)
case QAudio::InterruptedState:
Q_EMIT status (tr ("Interrupted"));
break;
#endif
case QAudio::StoppedState:
if (!checkStream ())
{
Q_EMIT status (tr ("Error"));
}
else
{
Q_EMIT status (tr ("Stopped"));
}
break;
}
publishRawTxPlayoutSnapshot ();
}
|