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 <QtQuick3DSpatialAudio/private/qquick3daudioengine_p.h>
7#include <QtSpatialAudio/qspatialsound.h>
8#include <QtMultimedia/qaudioformat.h>
9#include <QtCore/qdir.h>
10#include <QtQml/qqmlcontext.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
51{
52 delete m_sound;
53}
54
55/*!
56 \qmlproperty url SpatialSound::source
57
58 The source file for the sound to be played.
59 */
61{
62 return m_sound->source();
63}
64
66{
67 const QQmlContext *context = qmlContext(this);
68 QUrl url;
69 if (context) {
70 url = context->resolvedUrl(source);
71 } else {
72 url = QUrl::fromLocalFile(QDir::currentPath() + u"/");
73 url = url.resolved(source);
74 }
75 m_sound->setSource(url);
76}
77
78/*!
79 \qmlproperty real SpatialSound::volume
80
81 Defines an overall volume for this sound source.
82
83 Values between 0 and 1 will attenuate the sound, while values above 1
84 provide an additional gain boost.
85 */
87{
88 m_sound->setVolume(volume);
89}
90
92{
93 return m_sound->volume();
94}
95
96/*!
97 \qmlproperty enumeration SpatialSound::distanceModel
98
99 Defines how the volume of the sound scales with distance to the listener.
100 The volume starts scaling down
101 from \l size to \l distanceCutoff. The volume is constant for distances smaller
102 than size and zero for distances larger than the cutoff distance.
103
104 \table
105 \header \li Property value
106 \li Description
107 \row \li Logarithmic
108 \li Volume decreases logarithmically with distance.
109 \row \li Linear
110 \li Volume decreases linearly with distance.
111 \row \li ManualAttenuation
112 \li Attenuation is defined manually using the \l manualAttenuation property.
113 \endtable
114 */
116{
117 m_sound->setDistanceModel(QSpatialSound::DistanceModel(model));
118}
119
121{
122 return DistanceModel(m_sound->distanceModel());
123}
124
125/*!
126 \qmlproperty real SpatialSound::size
127
128 Defines the size of the sound source. If the listener is closer to the sound
129 object than the size, volume will stay constant. The size is also used to for
130 occlusion calculations, where large sources can be partially occluded by a wall.
131 */
133{
134 m_sound->setSize(min);
135}
136
138{
139 return m_sound->size();
140}
141
142/*!
143 \qmlproperty real SpatialSound::distanceCutoff
144
145 Defines a distance beyond which sound coming from the source will cutoff.
146 If the listener is further away from the sound object than the cutoff
147 distance it won't be audible anymore.
148 */
150{
151 m_sound->setDistanceCutoff(max);
152}
153
155{
156 return m_sound->distanceCutoff();
157}
158
159/*!
160 \qmlproperty real SpatialSound::manualAttenuation
161
162 Defines a manual attenuation factor if \l distanceModel is set to
163 SpatialSound.ManualAttenuation.
164 */
166{
167 m_sound->setManualAttenuation(attenuation);
168}
169
171{
172 return m_sound->manualAttenuation();
173}
174
175/*!
176 \qmlproperty real SpatialSound::occlusionIntensity
177
178 Defines how much the object is occluded. 0 implies the object is
179 not occluded at all, while a large number implies a large occlusion.
180
181 The default is 0.
182 */
184{
185 m_sound->setOcclusionIntensity(occlusion);
186}
187
189{
190 return m_sound->occlusionIntensity();
191}
192
193/*!
194 \qmlproperty real SpatialSound::directivity
195
196 Defines the directivity of the sound source. A value of 0 implies that the sound is
197 emitted equally in all directions, while a value of 1 implies that the source mainly
198 emits sound in the forward direction.
199
200 Valid values are between 0 and 1, the default is 0.
201 */
203{
204 m_sound->setDirectivity(alpha);
205}
206
208{
209 return m_sound->directivity();
210}
211
212/*!
213 \qmlproperty real SpatialSound::directivityOrder
214
215 Defines the order of the directivity of the sound source. A higher order
216 implies a sharper localization of the sound cone.
217
218 The minimum value and default for this property is 1.
219 */
221{
222 m_sound->setDirectivityOrder(alpha);
223}
224
226{
227 return m_sound->directivityOrder();
228}
229
230/*!
231 \qmlproperty real SpatialSound::nearFieldGain
232
233 Defines the near field gain for the sound source. Valid values are between 0 and 1.
234 A near field gain of 1 will raise the volume of the sound signal by approx 20 dB for
235 distances very close to the listener.
236 */
238{
239 m_sound->setNearFieldGain(gain);
240}
241
243{
244 return m_sound->nearFieldGain();
245}
246
247void QQuick3DSpatialSound::updatePosition()
248{
249 m_sound->setPosition(scenePosition());
250}
251
252void QQuick3DSpatialSound::updateRotation()
253{
254 m_sound->setRotation(sceneRotation());
255}
256
257/*!
258 \qmlproperty int SpatialSound::loops
259
260 Determines how often the sound is played before the player stops.
261 Set to SpatialSound::Infinite to loop the current sound forever.
262
263 The default value is \c 1.
264 */
265int QQuick3DSpatialSound::loops() const
266{
267 return m_sound->loops();
268}
269
271{
272 m_sound->setLoops(loops);
273}
274
275/*!
276 \qmlproperty bool SpatialSound::autoPlay
277
278 Determines whether the sound should automatically start playing when a source
279 gets specified.
280
281 The default value is \c true.
282 */
284{
285 return m_sound->autoPlay();
286}
287
289{
290 m_sound->setAutoPlay(autoPlay);
291}
292
293/*!
294 \qmlmethod void SpatialSound::play()
295
296 Starts playing back the sound. Does nothing if the sound is already playing.
297 */
299{
300 m_sound->play();
301}
302
303/*!
304 \qmlmethod void SpatialSound::pause()
305
306 Pauses sound playback at the current position. Calling play() will continue playback.
307 */
309{
310 m_sound->pause();
311}
312
313/*!
314 \qmlmethod void SpatialSound::stop()
315
316 Stops sound playback and resets the current position and loop count to 0. Calling play() will
317 begin playback at the beginning of the sound file.
318 */
320{
321 m_sound->stop();
322}
323
324QT_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.