Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsoundeffectwithplayer.cpp
Go to the documentation of this file.
1
// Copyright (C) 2025 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
#
include
"qsoundeffectwithplayer_p.h"
5
6
#
include
<
QtCore
/
qmutex
.
h
>
7
#
include
<
QtCore
/
q20map
.
h
>
8
9
#
include
<
utility
>
10
11
QT_BEGIN_NAMESPACE
12
13
namespace
QtMultimediaPrivate
{
14
15
namespace
{
16
17
QSpan<
const
float
> toFloatSpan(QSpan<
const
char
> byteArray)
18
{
19
return
QSpan{
20
reinterpret_cast
<
const
float
*>(byteArray.data()),
21
qsizetype(byteArray.size_bytes() /
sizeof
(
float
)),
22
};
23
}
24
25
}
// namespace
26
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
29
QSoundEffectVoice
::QSoundEffectVoice(VoiceId voiceId, std::shared_ptr<
const
QSample> sample,
30
float
volume,
bool
muted,
int
totalLoopCount,
31
QAudioFormat engineFormat)
32
: QRtAudioEngineVoice{ voiceId },
33
m_sample{ std::move(sample) },
34
m_engineFormat{ engineFormat },
35
m_volume
{ volume },
36
m_muted
{ muted },
37
m_loopsRemaining{ totalLoopCount }
38
{
39
}
40
41
QSoundEffectVoice
::~
QSoundEffectVoice
() =
default
;
42
43
VoicePlayResult
QSoundEffectVoice
::
play
(
QSpan
<
float
>
outputBuffer
)
noexcept
QT_MM_NONBLOCKING
44
{
45
qsizetype
playedFrames
=
playVoice
(
outputBuffer
);
46
m_currentFrame
+=
playedFrames
;
47
48
if
(
m_currentFrame
==
m_totalFrames
) {
49
const
bool
isInfiniteLoop
=
loopsRemaining
() ==
QSoundEffect
::
Infinite
;
50
bool
continuePlaying
=
isInfiniteLoop
;
51
52
if
(!
isInfiniteLoop
)
53
continuePlaying
=
m_loopsRemaining
.
fetch_sub
(1,
std
::
memory_order_relaxed
) > 1;
54
55
if
(
continuePlaying
) {
56
if
(!
isInfiniteLoop
)
57
m_currentLoopChanged
.
set
();
58
m_currentFrame
= 0;
59
QSpan
remainingOutputBuffer
=
60
drop
(
outputBuffer
,
playedFrames
*
m_engineFormat
.
channelCount
());
61
return
play
(
remainingOutputBuffer
);
62
}
63
return
VoicePlayResult
::
Finished
;
64
}
65
return
VoicePlayResult
::
Playing
;
66
}
67
68
qsizetype
QSoundEffectVoice
::
playVoice
(
QSpan
<
float
>
outputBuffer
)
noexcept
QT_MM_NONBLOCKING
69
{
70
const
QAudioFormat
&
format
=
m_sample
->
format
();
71
const
int
totalSamples
=
m_totalFrames
*
format
.
channelCount
();
72
const
int
currentSample
=
format
.
channelCount
() *
m_currentFrame
;
73
74
const
QSpan
fullSample
=
toFloatSpan
(
m_sample
->
data
());
75
const
QSpan
playbackRange
=
take
(
drop
(
fullSample
,
currentSample
),
totalSamples
);
76
77
Q_ASSERT
(!
playbackRange
.
empty
());
78
79
const
int
sampleCh
=
format
.
channelCount
();
80
const
int
engineCh
=
m_engineFormat
.
channelCount
();
81
const
qsizetype
sampleSamples
=
playbackRange
.
size
();
82
const
qsizetype
outputSamples
=
outputBuffer
.
size
();
83
const
qsizetype
maxFrames
=
std
::
min
(
sampleSamples
/
sampleCh
,
outputSamples
/
engineCh
);
84
const
qsizetype
framesToPlay
=
maxFrames
;
85
const
qsizetype
outputSamplesPlayed
=
framesToPlay
*
engineCh
;
86
87
enum
ConversionType
:
uint8_t
{
SameChannels
,
MonoToStereo
,
StereoToMono
};
88
const
ConversionType
conversion
= [&] {
89
if
(
sampleCh
==
engineCh
)
90
return
SameChannels
;
91
if
(
sampleCh
== 1 &&
engineCh
== 2)
92
return
MonoToStereo
;
93
if
(
sampleCh
== 2 &&
engineCh
== 1)
94
return
StereoToMono
;
95
Q_UNREACHABLE_RETURN
(
SameChannels
);
96
}();
97
98
if
(
m_muted
||
m_volume
== 0.f) {
99
std
::
fill_n
(
outputBuffer
.
begin
(),
outputSamplesPlayed
, 0.f);
100
return
framesToPlay
;
101
}
102
103
// later: (auto)vectorize?
104
switch
(
conversion
) {
105
case
SameChannels
:
106
for
(
qsizetype
frame
= 0;
frame
<
framesToPlay
; ++
frame
) {
107
const
qsizetype
sampleBase
=
frame
*
sampleCh
;
108
const
qsizetype
outputBase
=
frame
*
engineCh
;
109
for
(
int
ch
= 0;
ch
<
sampleCh
; ++
ch
) {
110
outputBuffer
[
outputBase
+
ch
] +=
playbackRange
[
sampleBase
+
ch
] *
m_volume
;
111
}
112
}
113
break
;
114
case
MonoToStereo
:
115
for
(
qsizetype
frame
= 0;
frame
<
framesToPlay
; ++
frame
) {
116
const
qsizetype
sampleBase
=
frame
*
sampleCh
;
117
const
qsizetype
outputBase
=
frame
*
engineCh
;
118
const
float
val
=
playbackRange
[
sampleBase
] *
m_volume
;
119
outputBuffer
[
outputBase
] +=
val
;
120
outputBuffer
[
outputBase
+ 1] +=
val
;
121
}
122
break
;
123
case
StereoToMono
:
124
float
scale
= 0.5f *
m_volume
;
125
for
(
qsizetype
frame
= 0;
frame
<
framesToPlay
; ++
frame
) {
126
const
qsizetype
sampleBase
=
frame
*
sampleCh
;
127
const
qsizetype
outputBase
=
frame
*
engineCh
;
128
const
float
val
= (
playbackRange
[
sampleBase
] +
playbackRange
[
sampleBase
+ 1]) *
scale
;
129
outputBuffer
[
outputBase
] +=
val
;
130
}
131
break
;
132
}
133
134
return
framesToPlay
;
135
}
136
137
bool
QSoundEffectVoice
::
isActive
()
noexcept
QT_MM_NONBLOCKING
138
{
139
if
(
m_currentFrame
!=
m_totalFrames
)
140
return
true
;
141
142
return
loopsRemaining
() != 0;
143
}
144
145
std
::
shared_ptr
<
QSoundEffectVoice
>
146
QSoundEffectVoice
::
clone
(
std
::
optional
<
QAudioFormat
>
newEngineFormat
)
const
147
{
148
auto
clone
=
std
::
make_shared
<
QSoundEffectVoice
>(
QRtAudioEngine
::
allocateVoiceId
(),
m_sample
,
149
m_volume
,
m_muted
,
loopsRemaining
(),
150
newEngineFormat
.
value_or
(
m_engineFormat
));
151
152
// caveat: reading frame is not atomic, so we may have a race here ... is is rare, though,
153
// not sure if we really care
154
clone
->
m_currentFrame
=
m_currentFrame
;
155
return
clone
;
156
}
157
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160
QSoundEffectPrivateWithPlayer
::
QSoundEffectPrivateWithPlayer
(
QSoundEffect
*
q
,
161
QAudioDevice
audioDevice
)
162
:
q_ptr
{
q
},
m_audioDevice
{
std
::
move
(
audioDevice
) }
163
{
164
resolveAudioDevice
();
165
166
QObject
::
connect
(&
m_mediaDevices
, &
QMediaDevices
::
audioOutputsChanged
,
this
, [
this
] {
167
QAudioDevice
defaultAudioDevice
=
QMediaDevices
::
defaultAudioOutput
();
168
if
(
defaultAudioDevice
==
m_defaultAudioDevice
)
169
return
;
170
171
m_defaultAudioDevice
=
QMediaDevices
::
defaultAudioOutput
();
172
if
(
m_audioDevice
.
isNull
())
173
setResolvedAudioDevice
(
m_defaultAudioDevice
);
174
});
175
176
m_playerReleaseTimer
.
setTimerType
(
Qt
::
VeryCoarseTimer
);
177
m_playerReleaseTimer
.
setSingleShot
(
true
);
178
}
179
180
QSoundEffectPrivateWithPlayer
::~
QSoundEffectPrivateWithPlayer
()
181
{
182
stop
();
183
if
(
m_sampleLoadFuture
)
184
m_sampleLoadFuture
->
cancelChain
();
185
}
186
187
bool
QSoundEffectPrivateWithPlayer
::
setAudioDevice
(
QAudioDevice
device
)
188
{
189
if
(
device
==
m_audioDevice
)
190
return
false
;
191
192
m_audioDevice
=
std
::
move
(
device
);
193
resolveAudioDevice
();
194
return
true
;
195
}
196
197
void
QSoundEffectPrivateWithPlayer
::
setResolvedAudioDevice
(
QAudioDevice
device
)
198
{
199
if
(
m_resolvedAudioDevice
==
device
)
200
return
;
201
202
m_resolvedAudioDevice
=
std
::
move
(
device
);
203
204
if
(
m_player
)
205
for
(
const
auto
&
voice
:
m_voices
)
206
m_player
->
stop
(
voice
);
207
208
std
::
vector
<
std
::
shared_ptr
<
QSoundEffectVoice
>>
voices
{
209
std
::
make_move_iterator
(
m_voices
.
begin
()),
std
::
make_move_iterator
(
m_voices
.
end
())
210
};
211
m_voices
.
clear
();
212
213
if
(
m_sample
) {
214
bool
hasPlayer
=
updatePlayer
(
m_sample
);
215
if
(!
hasPlayer
) {
216
setStatus
(
QSoundEffect
::
Error
);
217
return
;
218
}
219
220
for
(
const
auto
&
voice
:
voices
)
221
// we re-allocate a new voice ID and play on the new player
222
play
(
voice
->
clone
(
m_player
->
audioSink
().
format
()));
223
224
setStatus
(
QSoundEffect
::
Ready
);
225
226
for
(
const
auto
&
voice
:
voices
)
227
// we re-allocate a new voice ID and play on the new player
228
play
(
voice
->
clone
());
229
}
else
{
230
setStatus
(
m_sampleLoadFuture
?
QSoundEffect
::
Loading
:
QSoundEffect
::
Null
);
231
}
232
}
233
234
void
QSoundEffectPrivateWithPlayer
::
resolveAudioDevice
()
235
{
236
if
(
m_audioDevice
.
isNull
())
237
m_defaultAudioDevice
=
QMediaDevices
::
defaultAudioOutput
();
238
setResolvedAudioDevice
(
m_audioDevice
.
isNull
() ?
m_defaultAudioDevice
:
m_audioDevice
);
239
}
240
241
QAudioDevice
QSoundEffectPrivateWithPlayer
::
audioDevice
()
const
242
{
243
return
m_audioDevice
;
244
}
245
246
void
QSoundEffectPrivateWithPlayer
::
setSource
(
QUrl
url
,
QSampleCache
&
sampleCache
)
247
{
248
if
(
m_sampleLoadFuture
) {
249
m_sampleLoadFuture
->
cancelChain
();
250
m_sampleLoadFuture
=
std
::
nullopt
;
251
}
252
253
if
(
m_player
) {
254
QObject
::
disconnect
(
m_voiceFinishedConnection
);
255
m_playerReleaseTimer
.
callOnTimeout
(
this
, [
player
=
std
::
move
(
m_player
)] {
256
// we keep the player referenced for a little longer, so that later calls to
257
// QRtAudioEngine::getEngineFor will be able to reuse the existing instance
258
},
Qt
::
SingleShotConnection
);
259
m_playerReleaseTimer
.
start
();
260
}
261
262
m_sample
= {};
263
264
if
(
url
.
isEmpty
()) {
265
setStatus
(
QSoundEffect
::
Null
);
266
return
;
267
}
268
269
if
(!
url
.
isValid
()) {
270
setStatus
(
QSoundEffect
::
Error
);
271
return
;
272
}
273
274
setStatus
(
QSoundEffect
::
Loading
);
275
276
m_sampleLoadFuture
=
277
sampleCache
.
requestSampleFuture
(
url
).
then
(
this
, [
this
,
url
](
SharedSamplePtr
result
) {
278
if
(
result
) {
279
if
(!
formatIsSupported
(
result
->
format
())) {
280
qWarning
(
"QSoundEffect: QSoundEffect only supports mono or stereo files"
);
281
setStatus
(
QSoundEffect
::
Error
);
282
return
;
283
}
284
285
bool
hasPlayer
=
updatePlayer
(
result
);
286
m_sample
=
std
::
move
(
result
);
287
if
(!
hasPlayer
) {
288
qWarning
(
"QSoundEffect: playback of this format is not supported on the selected "
289
"audio device"
);
290
setStatus
(
QSoundEffect
::
Error
);
291
return
;
292
}
293
294
setStatus
(
QSoundEffect
::
Ready
);
295
if
(
std
::
exchange
(
m_playPending
,
false
)) {
296
play
();
297
}
298
}
else
{
299
qWarning
(
"QSoundEffect: Error decoding source %ls"
,
qUtf16Printable
(
url
.
toString
()));
300
setStatus
(
QSoundEffect
::
Error
);
301
}
302
});
303
}
304
305
void
QSoundEffectPrivateWithPlayer
::
setStatus
(
QSoundEffect
::
Status
status
)
306
{
307
if
(
status
==
m_status
)
308
return
;
309
m_status
=
status
;
310
emit
q_ptr
->
statusChanged
();
311
}
312
313
QSoundEffect
::
Status
QSoundEffectPrivateWithPlayer
::
status
()
const
314
{
315
return
m_status
;
316
}
317
318
int
QSoundEffectPrivateWithPlayer
::
loopCount
()
const
319
{
320
return
m_loopCount
;
321
}
322
323
bool
QSoundEffectPrivateWithPlayer
::
setLoopCount
(
int
loopCount
)
324
{
325
if
(
loopCount
== 0)
326
loopCount
= 1;
327
328
if
(
loopCount
==
m_loopCount
)
329
return
false
;
330
331
m_loopCount
=
loopCount
;
332
333
if
(
m_voices
.
empty
())
334
return
true
;
335
336
const
std
::
shared_ptr
<
QSoundEffectVoice
> &
voice
= *
m_voices
.
rbegin
();
337
voice
->
m_loopsRemaining
.
store
(
loopCount
,
std
::
memory_order_relaxed
);
338
339
setLoopsRemaining
(
loopCount
);
340
341
return
true
;
342
}
343
344
int
QSoundEffectPrivateWithPlayer
::
loopsRemaining
()
const
345
{
346
if
(
m_voices
.
empty
())
347
return
0;
348
349
return
m_loopsRemaining
;
350
}
351
352
float
QSoundEffectPrivateWithPlayer
::
volume
()
const
353
{
354
return
m_volume
;
355
}
356
357
bool
QSoundEffectPrivateWithPlayer
::
setVolume
(
float
volume
)
358
{
359
if
(
m_volume
==
volume
)
360
return
false
;
361
362
m_volume
=
volume
;
363
for
(
const
auto
&
voice
:
m_voices
) {
364
m_player
->
visitVoiceRt
(
voice
, [
volume
](
QSoundEffectVoice
&
voice
) {
365
voice
.
m_volume
=
volume
;
366
});
367
}
368
return
true
;
369
}
370
371
bool
QSoundEffectPrivateWithPlayer
::
muted
()
const
372
{
373
return
m_muted
;
374
}
375
376
bool
QSoundEffectPrivateWithPlayer
::
setMuted
(
bool
muted
)
377
{
378
if
(
m_muted
==
muted
)
379
return
false
;
380
381
m_muted
=
muted
;
382
for
(
const
auto
&
voice
:
m_voices
) {
383
m_player
->
visitVoiceRt
(
voice
, [
muted
](
QSoundEffectVoice
&
voice
) {
384
voice
.
m_muted
=
muted
;
385
});
386
}
387
return
true
;
388
}
389
390
void
QSoundEffectPrivateWithPlayer
::
play
()
391
{
392
if
(!
m_sample
) {
393
m_playPending
=
true
;
394
return
;
395
}
396
397
if
(
status
() !=
QSoundEffect
::
Ready
)
398
return
;
399
400
Q_ASSERT
(
m_player
);
401
402
// each `play` will start a new voice
403
auto
voice
=
std
::
make_shared
<
QSoundEffectVoice
>(
QRtAudioEngine
::
allocateVoiceId
(),
m_sample
,
404
m_volume
,
m_muted
,
m_loopCount
,
405
m_player
->
audioSink
().
format
());
406
407
play
(
std
::
move
(
voice
));
408
}
409
410
void
QSoundEffectPrivateWithPlayer
::
stop
()
411
{
412
size_t
activeVoices
=
m_voices
.
size
();
413
for
(
const
auto
&
voice
:
m_voices
)
414
m_player
->
stop
(
voice
->
voiceId
());
415
setLoopsRemaining
(0);
416
417
m_voices
.
clear
();
418
m_playPending
=
false
;
419
if
(
activeVoices
)
420
emit
q_ptr
->
playingChanged
();
421
}
422
423
bool
QSoundEffectPrivateWithPlayer
::
playing
()
const
424
{
425
return
!
m_voices
.
empty
();
426
}
427
428
void
QSoundEffectPrivateWithPlayer
::
play
(
std
::
shared_ptr
<
QSoundEffectVoice
>
voice
)
429
{
430
QObject
::
connect
(&
voice
->
m_currentLoopChanged
, &
QAutoResetEvent
::
activated
,
this
,
431
[
this
,
voiceId
=
voice
->
voiceId
()] {
432
auto
foundVoice
=
m_voices
.
find
(
voiceId
);
433
if
(
foundVoice
==
m_voices
.
end
())
434
return
;
435
436
if
(
voiceId
!=
activeVoice
())
437
return
;
438
439
setLoopsRemaining
((*
foundVoice
)->
loopsRemaining
());
440
});
441
442
m_player
->
play
(
voice
);
443
m_voices
.
insert
(
std
::
move
(
voice
));
444
setLoopsRemaining
(
m_loopCount
);
445
if
(
m_voices
.
size
() == 1)
446
emit
q_ptr
->
playingChanged
();
447
}
448
449
bool
QSoundEffectPrivateWithPlayer
::
updatePlayer
(
const
SharedSamplePtr
&
sample
)
450
{
451
Q_ASSERT
(
sample
);
452
Q_ASSERT
(
m_voices
.
empty
());
453
QObject
::
disconnect
(
m_voiceFinishedConnection
);
454
455
m_player
= {};
456
if
(
m_resolvedAudioDevice
.
isNull
())
457
return
false
;
458
459
m_player
= [&]() ->
std
::
shared_ptr
<
QRtAudioEngine
> {
460
auto
player
=
QRtAudioEngine
::
getEngineFor
(
m_resolvedAudioDevice
,
sample
->
format
());
461
if
(
player
)
462
return
player
;
463
464
QAudioFormat
alternativeFormat
=
sample
->
format
();
465
switch
(
sample
->
format
().
channelCount
()) {
466
case
1:
467
alternativeFormat
.
setChannelCount
(2);
468
break
;
469
case
2:
470
alternativeFormat
.
setChannelCount
(1);
471
break
;
472
default
:
473
Q_UNREACHABLE_RETURN
({});
474
}
475
476
return
QRtAudioEngine
::
getEngineFor
(
m_resolvedAudioDevice
,
alternativeFormat
);
477
}();
478
479
if
(!
m_player
)
480
return
false
;
481
482
m_voiceFinishedConnection
=
QObject
::
connect
(
m_player
.
get
(), &
QRtAudioEngine
::
voiceFinished
,
483
this
, [
this
](
VoiceId
voiceId
) {
484
if
(
voiceId
==
activeVoice
())
485
setLoopsRemaining
(0);
486
487
auto
found
=
m_voices
.
find
(
voiceId
);
488
if
(
found
!=
m_voices
.
end
()) {
489
m_voices
.
erase
(
found
);
490
if
(
m_voices
.
empty
())
491
emit
q_ptr
->
playingChanged
();
492
}
493
});
494
return
true
;
495
}
496
497
std
::
optional
<
VoiceId
>
QSoundEffectPrivateWithPlayer
::
activeVoice
()
const
498
{
499
if
(
m_voices
.
empty
())
500
return
std
::
nullopt
;
501
return
(*
m_voices
.
rbegin
())->
voiceId
();
502
}
503
504
bool
QSoundEffectPrivateWithPlayer
::
formatIsSupported
(
const
QAudioFormat
&
fmt
)
505
{
506
switch
(
fmt
.
channelCount
()) {
507
case
1:
508
case
2:
509
return
true
;
510
default
:
511
return
false
;
512
}
513
}
514
515
void
QSoundEffectPrivateWithPlayer
::
setLoopsRemaining
(
int
loopsRemaining
)
516
{
517
if
(
loopsRemaining
==
m_loopsRemaining
)
518
return
;
519
m_loopsRemaining
=
loopsRemaining
;
520
emit
q_ptr
->
loopsRemainingChanged
();
521
}
522
523
}
// namespace QtMultimediaPrivate
524
525
QT_END_NAMESPACE
QtMultimediaPrivate::QSoundEffectVoice::~QSoundEffectVoice
Q_MULTIMEDIA_EXPORT ~QSoundEffectVoice()
QtMultimediaPrivate::QSoundEffectVoice::m_volume
float m_volume
Definition
qsoundeffectwithplayer_p.h:59
QtMultimediaPrivate::QSoundEffectVoice::m_muted
bool m_muted
Definition
qsoundeffectwithplayer_p.h:60
QtMultimediaPrivate::QSoundEffectVoice::m_totalFrames
const int m_totalFrames
Definition
qsoundeffectwithplayer_p.h:53
QtMultimediaPrivate
Definition
qaudiosystem_p.h:51
qtmultimedia
src
multimedia
audio
qsoundeffectwithplayer.cpp
Generated on
for Qt by
1.16.1