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