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