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
qaudiolistener.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 <QtSpatialAudio/private/qaudioengine_p.h>
7#include <QtMultimedia/qaudiosink.h>
8#include <QtCore/private/qobject_p.h>
9
10
12
14{
15public:
16 QAudioEngine *engine = nullptr;
19};
20
21/*!
22 \class QAudioListener
23 \inmodule QtSpatialAudio
24 \ingroup spatialaudio
25 \ingroup multimedia_audio
26
27 \brief Defines the position and orientation of the person listening to a sound field
28 defined by QAudioEngine.
29
30 A QAudioEngine can have exactly one listener that defines the position and orientation
31 of the person listening to the sound field.
32 */
33
34/*!
35 Creates a listener for the spatial audio engine for \a engine.
36 */
37QAudioListener::QAudioListener(QAudioEngine *engine) : QObject(*new QAudioListenerPrivate)
38{
39 setEngine(engine);
40}
41
42/*!
43 Destroys the listener.
44 */
45QAudioListener::~QAudioListener()
46{
47 // Unregister this listener from the engine
48 setEngine(nullptr);
49}
50
51/*!
52 Sets the listener's position in 3D space to \a pos. Units are in centimeters
53 by default.
54
55 \sa QAudioEngine::distanceScale
56 */
57void QAudioListener::setPosition(QVector3D pos)
58{
59 Q_D(QAudioListener);
60
61 d->pos = pos;
62 auto *ep = QAudioEnginePrivate::get(d->engine);
63 if (!ep)
64 return;
65 ep->setListenerPosition(pos);
66}
67
68/*!
69 Returns the current position of the listener.
70 */
71QVector3D QAudioListener::position() const
72{
73 Q_D(const QAudioListener);
74 return d->pos;
75}
76
77/*!
78 Sets the listener's orientation in 3D space to \a q.
79 */
80void QAudioListener::setRotation(const QQuaternion &q)
81{
82 Q_D(QAudioListener);
83 d->rotation = q;
84 auto *ep = QAudioEnginePrivate::get(d->engine);
85 if (ep)
86 ep->setListenerRotation(q);
87}
88
89/*!
90 Returns the listener's orientation in 3D space.
91 */
92QQuaternion QAudioListener::rotation() const
93{
94 Q_D(const QAudioListener);
95 return d->rotation;
96}
97
98/*!
99 \internal
100 */
101void QAudioListener::setEngine(QAudioEngine *engine)
102{
103 Q_D(QAudioListener);
104 if (d->engine) {
105 auto *ed = QAudioEnginePrivate::get(d->engine);
106 ed->setListenerPosition(std::nullopt);
107 }
108 d->engine = engine;
109 if (d->engine) {
110 auto *ed = QAudioEnginePrivate::get(d->engine);
111 bool hasListener = ed->listenerPosition().has_value();
112 if (!hasListener) {
113 qWarning() << "Ignoring attempt to add a second listener to the spatial audio engine.";
114 d->engine = nullptr;
115 return;
116 }
117 ed->setListenerPosition(d->pos);
118 }
119}
120
121/*!
122 Returns the engine associated with this listener.
123 */
124QAudioEngine *QAudioListener::engine() const
125{
126 Q_D(const QAudioListener);
127 return d->engine;
128}
129
130QT_END_NAMESPACE
Combined button and popup list for selecting options.