Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qsoundeffectwithplayer_p.h
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
#
ifndef
QSOUNDEFFECTWITHPLAYER_P_H
5
#
define
QSOUNDEFFECTWITHPLAYER_P_H
6
7
//
8
// W A R N I N G
9
// -------------
10
//
11
// This file is not part of the Qt API. It exists purely as an
12
// implementation detail. This header file may change from version to
13
// version without notice, or even be removed.
14
//
15
// We mean it.
16
//
17
18
#
include
<
QtMultimedia
/
private
/
qaudiosystem_p
.
h
>
19
#
include
<
QtMultimedia
/
private
/
qautoresetevent_p
.
h
>
20
#
include
<
QtMultimedia
/
private
/
qrtaudioengine_p
.
h
>
21
#
include
<
QtMultimedia
/
private
/
qsoundeffect_p
.
h
>
22
#
include
<
QtMultimedia
/
qaudiosink
.
h
>
23
#
include
<
QtMultimedia
/
qmediadevices
.
h
>
24
#
include
<
QtCore
/
qchronotimer
.
h
>
25
26
QT_BEGIN_NAMESPACE
27
28
namespace
QtMultimediaPrivate
{
29
30
using
QtPrivate
::
QAutoResetEvent
;
31
32
class
QSoundEffectVoice
final
:
public
QRtAudioEngineVoice
33
{
34
public
:
35
Q_MULTIMEDIA_EXPORT
QSoundEffectVoice
(
VoiceId
voiceId
,
std
::
shared_ptr
<
const
QSample
>
sample
,
36
float
volume
,
bool
muted
,
int
totalLoopCount
,
37
QAudioFormat
engineFormat
);
38
39
Q_MULTIMEDIA_EXPORT
~
QSoundEffectVoice
();
40
41
Q_MULTIMEDIA_EXPORT VoicePlayResult
play
(
QSpan
<
float
>)
noexcept
Q_DECL_NONBLOCKING_FUNCTION
override
;
42
43
bool
isActive
()
noexcept
Q_DECL_NONBLOCKING_FUNCTION
override
;
44
const
QAudioFormat
&
format
()
noexcept
override
{
return
m_sample->format(); }
45
46
int
loopsRemaining
()
const
{
return
m_loopsRemaining.load(std::memory_order_relaxed); }
47
std
::
shared_ptr
<
QSoundEffectVoice
>
48
clone
(std::optional<QAudioFormat> newEngineFormat = {})
const
;
49
50
Q_MULTIMEDIA_EXPORT qsizetype
playVoice
(
QSpan
<
float
>)
noexcept
Q_DECL_NONBLOCKING_FUNCTION
;
51
52
const
std
::
shared_ptr
<
const
QSample
>
m_sample
;
53
const
int
m_totalFrames
{
54
m_sample
->
format
().
framesForBytes
(
m_sample
->
data
().
size
()),
55
};
56
57
const
QAudioFormat
m_engineFormat
;
58
59
float
m_volume
{};
60
bool
m_muted
{};
61
62
std
::
atomic_int
m_loopsRemaining
;
63
int
m_currentFrame
{};
64
65
QAutoResetEvent
m_currentLoopChanged
;
66
};
67
68
// Design notes
69
// * each play() will start a new voice
70
// * stop() will stop all voices
71
// * the most recently created voice can be obtained via `activeVoice()`
72
// * mute/volume will affect all voices
73
// * loopCount/loopRemaining will be obtained from most recently created voice
74
class
Q_MULTIMEDIA_EXPORT QSoundEffectPrivateWithPlayer
final
:
public
QObject
,
75
public
QSoundEffectPrivate
76
{
77
public
:
78
static
std
::
shared_ptr
<
QRtAudioEngine
>
getEngineFor
(
const
QAudioDevice
&,
const
QAudioFormat
&);
79
80
QSoundEffectPrivateWithPlayer
(
QSoundEffect
*
q
,
QAudioDevice
audioDevice
);
81
Q_DISABLE_COPY_MOVE
(
QSoundEffectPrivateWithPlayer
)
82
~
QSoundEffectPrivateWithPlayer
()
override
;
83
84
// QSoundEffectPrivate interface
85
bool
setAudioDevice
(
QAudioDevice
device
)
override
;
86
QAudioDevice
audioDevice
()
const
override
;
87
void
setSource
(
QUrl
,
QSampleCache
&)
override
;
88
QSoundEffect
::
Status
status
()
const
override
;
89
int
loopCount
()
const
override
;
90
bool
setLoopCount
(
int
)
override
;
91
int
loopsRemaining
()
const
override
;
92
float
volume
()
const
override
;
93
bool
setVolume
(
float
)
override
;
94
bool
muted
()
const
override
;
95
bool
setMuted
(
bool
)
override
;
96
void
play
()
override
;
97
void
stop
()
override
;
98
bool
playing
()
const
override
;
99
100
// testing
101
const
std
::
shared_ptr
<
QRtAudioEngine
> &
player
()
const
{
return
m_player
; }
102
const
auto
&
voices
()
const
{
return
m_voices
; }
103
104
private
:
105
void
play
(
std
::
shared_ptr
<
QSoundEffectVoice
>);
106
void
setStatus
(
QSoundEffect
::
Status
status
);
107
[[
nodiscard
]]
bool
updatePlayer
(
const
SharedSamplePtr
&
sample
);
108
std
::
optional
<
VoiceId
>
activeVoice
()
const
;
109
static
bool
formatIsSupported
(
const
QAudioFormat
&);
110
void
setResolvedAudioDevice
(
QAudioDevice
device
);
111
void
resolveAudioDevice
();
112
113
QSoundEffect
*
q_ptr
{};
114
QAudioDevice
m_audioDevice
;
115
QAudioDevice
m_resolvedAudioDevice
;
116
std
::
shared_ptr
<
QRtAudioEngine
>
m_player
;
117
QMetaObject
::
Connection
m_voiceFinishedConnection
;
118
QMetaObject
::
Connection
m_playerStateConnection
;
119
std
::
set
<
std
::
shared_ptr
<
QSoundEffectVoice
>,
QRtAudioEngineVoiceCompare
>
m_voices
;
120
121
void
setLoopsRemaining
(
int
);
122
int
m_loopsRemaining
{ 0 };
123
124
std
::
optional
<
QFuture
<
void
>>
m_sampleLoadFuture
;
125
SharedSamplePtr
m_sample
;
126
float
m_volume
= 1.f;
127
bool
m_muted
=
false
;
128
int
m_loopCount
= 1;
129
bool
m_playPending
=
false
;
130
131
QSoundEffect
::
Status
m_status
{};
132
133
QMediaDevices
m_mediaDevices
;
134
QAudioDevice
m_defaultAudioDevice
;
135
QChronoTimer
m_playerReleaseTimer
{
136
std
::
chrono
::
seconds
(2),
137
};
138
};
139
140
}
// namespace QtMultimediaPrivate
141
142
QT_END_NAMESPACE
143
144
#
endif
// QSOUNDEFFECTWITHPLAYER_P_H
QtMultimediaPrivate::QSoundEffectVoice::m_loopsRemaining
std::atomic_int m_loopsRemaining
Definition
qsoundeffectwithplayer_p.h:62
QtMultimediaPrivate::QSoundEffectVoice::~QSoundEffectVoice
Q_MULTIMEDIA_EXPORT ~QSoundEffectVoice()
QtMultimediaPrivate::QSoundEffectVoice::m_currentFrame
int m_currentFrame
Definition
qsoundeffectwithplayer_p.h:63
QtMultimediaPrivate::QSoundEffectVoice::m_engineFormat
const QAudioFormat m_engineFormat
Definition
qsoundeffectwithplayer_p.h:57
QtMultimediaPrivate::QSoundEffectVoice::m_sample
const std::shared_ptr< const QSample > m_sample
Definition
qsoundeffectwithplayer_p.h:52
QtMultimediaPrivate::QSoundEffectVoice::loopsRemaining
int loopsRemaining() const
Definition
qsoundeffectwithplayer_p.h:46
QtMultimediaPrivate::QSoundEffectVoice::m_currentLoopChanged
QAutoResetEvent m_currentLoopChanged
Definition
qsoundeffectwithplayer_p.h:65
QtMultimediaPrivate::QSoundEffectVoice::clone
std::shared_ptr< QSoundEffectVoice > clone(std::optional< QAudioFormat > newEngineFormat={}) const
Definition
qsoundeffectwithplayer.cpp:214
QtMultimediaPrivate::QSoundEffectVoice::m_volume
float m_volume
Definition
qsoundeffectwithplayer_p.h:59
QtMultimediaPrivate::QSoundEffectVoice::format
const QAudioFormat & format() noexcept override
Definition
qsoundeffectwithplayer_p.h:44
QtMultimediaPrivate::QSoundEffectVoice::m_muted
bool m_muted
Definition
qsoundeffectwithplayer_p.h:60
QtMultimediaPrivate::QSoundEffectVoice::isActive
bool isActive() noexcept Q_DECL_NONBLOCKING_FUNCTION override
Definition
qsoundeffectwithplayer.cpp:205
QtMultimediaPrivate::QSoundEffectVoice::m_totalFrames
const int m_totalFrames
Definition
qsoundeffectwithplayer_p.h:53
QtMultimediaPrivate::impl
Definition
qaudiosystem_p.h:257
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtmultimedia
src
multimedia
audio
qsoundeffectwithplayer_p.h
Generated on
for Qt by
1.18.0