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
qquick3dspatialsound.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-3.0-only
3
5
6#include <QtMultimediaQuick/private/qqmlcontext_source_resolver_p.h>
7#include <QtQuick3DSpatialAudio/private/qquick3daudioengine_p.h>
8#include <QtSpatialAudio/qspatialsound.h>
9#include <QtSpatialAudio/private/qspatialaudiosound_p.h>
10
12
13/*!
14 \qmltype SpatialSound
15 \inqmlmodule QtQuick3D.SpatialAudio
16 \ingroup quick3d_spatialaudio
17 \ingroup multimedia_audio_qml
18
19 \brief A sound object in 3D space.
20
21 A SpatialSound represents an audible object in 3D space. You can define
22 it's position and orientation in space, set the sound it is playing and define a
23 volume for the object.
24
25 The object can have different attenuation behavior, emit sound mainly in one direction
26 or spherically, and behave as if occluded by some other object.
27 */
28
29QQuick3DSpatialSound::QQuick3DSpatialSound()
30{
31 m_sound = new QSpatialSound(QQuick3DAudioEngine::getEngine());
32
33 connect(this, &QQuick3DNode::scenePositionChanged, this, &QQuick3DSpatialSound::updatePosition);
34 connect(this, &QQuick3DNode::sceneRotationChanged, this, &QQuick3DSpatialSound::updateRotation);
35 connect(m_sound, &QSpatialSound::sourceChanged, this, &QQuick3DSpatialSound::sourceChanged);
36 connect(m_sound, &QSpatialSound::volumeChanged, this, &QQuick3DSpatialSound::volumeChanged);
37 connect(m_sound, &QSpatialSound::distanceModelChanged, this, &QQuick3DSpatialSound::distanceModelChanged);
38 connect(m_sound, &QSpatialSound::sizeChanged, this, &QQuick3DSpatialSound::sizeChanged);
39 connect(m_sound, &QSpatialSound::distanceCutoffChanged, this, &QQuick3DSpatialSound::distanceCutoffChanged);
40 connect(m_sound, &QSpatialSound::manualAttenuationChanged, this, &QQuick3DSpatialSound::manualAttenuationChanged);
41 connect(m_sound, &QSpatialSound::occlusionIntensityChanged, this, &QQuick3DSpatialSound::occlusionIntensityChanged);
42 connect(m_sound, &QSpatialSound::directivityChanged, this, &QQuick3DSpatialSound::directivityChanged);
43 connect(m_sound, &QSpatialSound::directivityOrderChanged, this, &QQuick3DSpatialSound::directivityOrderChanged);
44 connect(m_sound, &QSpatialSound::nearFieldGainChanged, this, &QQuick3DSpatialSound::nearFieldGainChanged);
45 connect(m_sound, &QSpatialSound::loopsChanged, this, &QQuick3DSpatialSound::loopsChanged);
46 connect(m_sound, &QSpatialSound::autoPlayChanged, this, &QQuick3DSpatialSound::autoPlayChanged);
47
48 auto *soundPrivate = static_cast<QSpatialAudioSoundPrivate *>(QObjectPrivate::get(m_sound));
49 soundPrivate->m_sourceResolver =
50 std::make_unique<QMultimediaPrivate::QQmlContextSourceResolver>(this);
51}
52
53QQuick3DSpatialSound::~QQuick3DSpatialSound()
54{
55 delete m_sound;
56}
57
58/*!
59 \qmlproperty url SpatialSound::source
60
61 The source file for the sound to be played.
62 */
63QUrl QQuick3DSpatialSound::source() const
64{
65 return m_sound->source();
66}
67
68void QQuick3DSpatialSound::setSource(const QUrl& source)
69{
70 m_sound->setSource(source);
71}
72
73/*!
74 \qmlproperty real SpatialSound::volume
75
76 Defines an overall volume for this sound source.
77
78 Values between 0 and 1 will attenuate the sound, while values above 1
79 provide an additional gain boost.
80 */
81void QQuick3DSpatialSound::setVolume(float volume)
82{
83 m_sound->setVolume(volume);
84}
85
86float QQuick3DSpatialSound::volume() const
87{
88 return m_sound->volume();
89}
90
91/*!
92 \qmlproperty enumeration SpatialSound::distanceModel
93
94 Defines how the volume of the sound scales with distance to the listener.
95 The volume starts scaling down
96 from \l size to \l distanceCutoff. The volume is constant for distances smaller
97 than size and zero for distances larger than the cutoff distance.
98
99 \table
100 \header \li Property value
101 \li Description
102 \row \li Logarithmic
103 \li Volume decreases logarithmically with distance.
104 \row \li Linear
105 \li Volume decreases linearly with distance.
106 \row \li ManualAttenuation
107 \li Attenuation is defined manually using the \l manualAttenuation property.
108 \endtable
109 */
110void QQuick3DSpatialSound::setDistanceModel(DistanceModel model)
111{
112 m_sound->setDistanceModel(QSpatialSound::DistanceModel(model));
113}
114
115QQuick3DSpatialSound::DistanceModel QQuick3DSpatialSound::distanceModel() const
116{
117 return DistanceModel(m_sound->distanceModel());
118}
119
120/*!
121 \qmlproperty real SpatialSound::size
122
123 Defines the size of the sound source. If the listener is closer to the sound
124 object than the size, volume will stay constant. The size is also used to for
125 occlusion calculations, where large sources can be partially occluded by a wall.
126 */
127void QQuick3DSpatialSound::setSize(float min)
128{
129 m_sound->setSize(min);
130}
131
132float QQuick3DSpatialSound::size() const
133{
134 return m_sound->size();
135}
136
137/*!
138 \qmlproperty real SpatialSound::distanceCutoff
139
140 Defines a distance beyond which sound coming from the source will cutoff.
141 If the listener is further away from the sound object than the cutoff
142 distance it won't be audible anymore.
143 */
144void QQuick3DSpatialSound::setDistanceCutoff(float max)
145{
146 m_sound->setDistanceCutoff(max);
147}
148
149float QQuick3DSpatialSound::distanceCutoff() const
150{
151 return m_sound->distanceCutoff();
152}
153
154/*!
155 \qmlproperty real SpatialSound::manualAttenuation
156
157 Defines a manual attenuation factor if \l distanceModel is set to
158 SpatialSound.ManualAttenuation.
159 */
160void QQuick3DSpatialSound::setManualAttenuation(float attenuation)
161{
162 m_sound->setManualAttenuation(attenuation);
163}
164
165float QQuick3DSpatialSound::manualAttenuation() const
166{
167 return m_sound->manualAttenuation();
168}
169
170/*!
171 \qmlproperty real SpatialSound::occlusionIntensity
172
173 Defines how much the object is occluded. 0 implies the object is
174 not occluded at all, while a large number implies a large occlusion.
175
176 The default is 0.
177 */
178void QQuick3DSpatialSound::setOcclusionIntensity(float occlusion)
179{
180 m_sound->setOcclusionIntensity(occlusion);
181}
182
183float QQuick3DSpatialSound::occlusionIntensity() const
184{
185 return m_sound->occlusionIntensity();
186}
187
188/*!
189 \qmlproperty real SpatialSound::directivity
190
191 Defines the directivity of the sound source. A value of 0 implies that the sound is
192 emitted equally in all directions, while a value of 1 implies that the source mainly
193 emits sound in the forward direction.
194
195 Valid values are between 0 and 1, the default is 0.
196 */
197void QQuick3DSpatialSound::setDirectivity(float alpha)
198{
199 m_sound->setDirectivity(alpha);
200}
201
202float QQuick3DSpatialSound::directivity() const
203{
204 return m_sound->directivity();
205}
206
207/*!
208 \qmlproperty real SpatialSound::directivityOrder
209
210 Defines the order of the directivity of the sound source. A higher order
211 implies a sharper localization of the sound cone.
212
213 The minimum value and default for this property is 1.
214 */
215void QQuick3DSpatialSound::setDirectivityOrder(float alpha)
216{
217 m_sound->setDirectivityOrder(alpha);
218}
219
220float QQuick3DSpatialSound::directivityOrder() const
221{
222 return m_sound->directivityOrder();
223}
224
225/*!
226 \qmlproperty real SpatialSound::nearFieldGain
227
228 Defines the near field gain for the sound source. Valid values are between 0 and 1.
229 A near field gain of 1 will raise the volume of the sound signal by approx 20 dB for
230 distances very close to the listener.
231 */
232void QQuick3DSpatialSound::setNearFieldGain(float gain)
233{
234 m_sound->setNearFieldGain(gain);
235}
236
237float QQuick3DSpatialSound::nearFieldGain() const
238{
239 return m_sound->nearFieldGain();
240}
241
242void QQuick3DSpatialSound::updatePosition()
243{
244 m_sound->setPosition(scenePosition());
245}
246
247void QQuick3DSpatialSound::updateRotation()
248{
249 m_sound->setRotation(sceneRotation());
250}
251
252/*!
253 \qmlproperty int SpatialSound::loops
254
255 Determines how often the sound is played before the player stops.
256 Set to SpatialSound::Infinite to loop the current sound forever.
257
258 The default value is \c 1.
259 */
260int QQuick3DSpatialSound::loops() const
261{
262 return m_sound->loops();
263}
264
265void QQuick3DSpatialSound::setLoops(int loops)
266{
267 m_sound->setLoops(loops);
268}
269
270/*!
271 \qmlproperty bool SpatialSound::autoPlay
272
273 Determines whether the sound should automatically start playing when a source
274 gets specified.
275
276 The default value is \c true.
277 */
278bool QQuick3DSpatialSound::autoPlay() const
279{
280 return m_sound->autoPlay();
281}
282
283void QQuick3DSpatialSound::setAutoPlay(bool autoPlay)
284{
285 m_sound->setAutoPlay(autoPlay);
286}
287
288/*!
289 \qmlmethod void SpatialSound::play()
290
291 Starts playing back the sound. Does nothing if the sound is already playing.
292 */
293void QQuick3DSpatialSound::play()
294{
295 m_sound->play();
296}
297
298/*!
299 \qmlmethod void SpatialSound::pause()
300
301 Pauses sound playback at the current position. Calling play() will continue playback.
302 */
303void QQuick3DSpatialSound::pause()
304{
305 m_sound->pause();
306}
307
308/*!
309 \qmlmethod void SpatialSound::stop()
310
311 Stops sound playback and resets the current position and loop count to 0. Calling play() will
312 begin playback at the beginning of the sound file.
313 */
314void QQuick3DSpatialSound::stop()
315{
316 m_sound->stop();
317}
318
319QT_END_NAMESPACE
Combined button and popup list for selecting options.