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