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
qquick3daudioengine.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#include <qquick3daudioengine_p.h>
4#include <qaudiodevice.h>
5
7
8static QAudioEngine *globalEngine = nullptr;
9
10/*!
11 \qmltype AudioEngine
12 \inqmlmodule QtQuick3D.SpatialAudio
13 \ingroup quick3d_spatialaudio
14 \ingroup multimedia_audio_qml
15
16 \brief AudioEngine manages sound objects inside a 3D scene.
17
18 AudioEngine manages sound objects inside a 3D scene. You can add
19 SpatialSound objects to the scene to define sounds that happen
20 at a specified location in 3D space. AmbientSound allows you to add
21 a stereo overlay (for example voice over or a sound track).
22
23 You can use AudioListener to define the position of the person listening
24 to the sound field relative to the sound sources. Sound sources will be less audible
25 if the listener is further away from source. They will also get mapped to the corresponding
26 loudspeakers depending on the direction between listener and source. In many cases, the
27 AudioListener object can simply be instantiated as a child object of the QtQuick3D.Camera
28 object.
29
30 Create AudioRoom objcects to simulate the sound (reflections and reverb) of a room with
31 certain dimensions and different types of walls.
32
33 AudioEngine does offer a mode where Qt is using simulating the effects of the ear
34 using head related impulse reponse functions (see also https://en.wikipedia.org/wiki/Sound_localization)
35 to localize the sound in 3D space when using headphones and create a spatial audio effect through
36 headphones.
37
38 As the rest of Qt Quick 3D, the audio engine uses a coordinate system that is in centimeters by default.
39 The axes are defined so that positive x points to the right, positive y points up and positive z points
40 backwards.
41*/
42
43
44QQuick3DAudioEngine::QQuick3DAudioEngine()
45{
46 auto *e = getEngine();
47 connect(e, &QAudioEngine::outputModeChanged, this, &QQuick3DAudioEngine::outputModeChanged);
48 connect(e, &QAudioEngine::outputDeviceChanged, this, &QQuick3DAudioEngine::outputDeviceChanged);
49 connect(e, &QAudioEngine::masterVolumeChanged, this, &QQuick3DAudioEngine::masterVolumeChanged);
50 connect(e, &QAudioEngine::distanceScaleChanged, this, &QQuick3DAudioEngine::distanceScaleChanged);
51}
52
56
57/*!
58 \qmlproperty enumeration AudioEngine::outputMode
59
60 Sets or retrieves the current output mode of the engine.
61
62 \table
63 \header \li Property value
64 \li Description
65 \row \li Surround
66 \li Map the sounds to the loudspeaker configuration of the output device.
67 This is normally a stereo or surround speaker setup.
68 \row \li Stereo
69 \li Map the sounds to the stereo loudspeaker configuration of the output device.
70 This will ignore any additional speakers and only use the left and right channels
71 to create a stero rendering of the sound field.
72 \row \li Headphone
73 \li Use Headphone spatialization to create a 3D audio effect when listening
74 to the sound field through headphones.
75 \endtable
76 */
77
78void QQuick3DAudioEngine::setOutputMode(OutputMode mode)
79{
80 globalEngine->setOutputMode(QAudioEngine::OutputMode(mode));
81}
82
84{
85 return OutputMode(globalEngine->outputMode());
86}
87
88/*!
89 \qmlproperty QtMultimedia.AudioDevice AudioEngine::outputDevice
90
91 Sets or returns the device that is being used for outputting the sound field.
92 */
93void QQuick3DAudioEngine::setOutputDevice(const QAudioDevice &device)
94{
95 globalEngine->setOutputDevice(device);
96}
97
98QAudioDevice QQuick3DAudioEngine::outputDevice() const
99{
100 return globalEngine->outputDevice();
101}
102
103/*!
104 \qmlproperty real AudioEngine::masterVolume
105
106 Sets or returns overall volume being used to render the sound field.
107 */
109{
110 globalEngine->setMasterVolume(volume);
111}
112
114{
115 return globalEngine->masterVolume();
116}
117
118/*!
119 \qmlproperty real QAudioEngine::distanceScale
120 \since 6.11
121
122 Defines the scale of the coordinate system being used by the spatial audio engine.
123 By default, all units are in centimeters, in line with the default units being
124 used by Qt Quick 3D.
125
126 Set the distance scale to 100 to get units in meters.
127*/
129{
130 globalEngine->setDistanceScale(scale);
131}
132
134{
135 return globalEngine->distanceScale();
136}
137
138QAudioEngine *QQuick3DAudioEngine::getEngine()
139{
140 if (!globalEngine) {
141 globalEngine = new QAudioEngine;
142 globalEngine->start();
143 }
144 return globalEngine;
145}
146
147QT_END_NAMESPACE
148
149#include "moc_qquick3daudioengine_p.cpp"
void setOutputDevice(const QAudioDevice &device)
\qmlproperty QtMultimedia.AudioDevice AudioEngine::outputDevice
void setMasterVolume(float volume)
\qmlproperty real AudioEngine::masterVolume
QAudioDevice outputDevice() const
void setOutputMode(OutputMode mode)
\qmlproperty enumeration AudioEngine::outputMode
OutputMode outputMode() const
void setDistanceScale(float scale)
\qmlproperty real QAudioEngine::distanceScale
static QT_BEGIN_NAMESPACE QAudioEngine * globalEngine