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 Q_D(QAudioListener);
40
41 d->engine = engine;
42 if (d->engine) {
43 auto *ed = QAudioEnginePrivate::get(d->engine);
44 bool hasListener = ed->listenerPosition().has_value();
45 if (hasListener) {
46 qWarning() << "Ignoring attempt to add a second listener to the spatial audio engine.";
47 d->engine = nullptr;
48 } else {
49 ed->setListenerPosition(d->pos);
50 }
51 }
52}
53
54/*!
55 Destroys the listener.
56 */
57QAudioListener::~QAudioListener()
58{
59 Q_D(QAudioListener);
60
61 if (d->engine) {
62 auto *ed = QAudioEnginePrivate::get(d->engine);
63 ed->setListenerPosition(std::nullopt);
64 }
65 d->engine = nullptr;
66}
67
68/*!
69 Sets the listener's position in 3D space to \a pos. Units are in centimeters
70 by default.
71
72 \sa QAudioEngine::distanceScale
73 */
74void QAudioListener::setPosition(QVector3D pos)
75{
76 Q_D(QAudioListener);
77
78 d->pos = pos;
79 auto *ep = QAudioEnginePrivate::get(d->engine);
80 if (!ep)
81 return;
82 ep->setListenerPosition(pos);
83}
84
85/*!
86 Returns the current position of the listener.
87 */
88QVector3D QAudioListener::position() const
89{
90 Q_D(const QAudioListener);
91 return d->pos;
92}
93
94/*!
95 Sets the listener's orientation in 3D space to \a q.
96 */
97void QAudioListener::setRotation(const QQuaternion &q)
98{
99 Q_D(QAudioListener);
100 d->rotation = q;
101 auto *ep = QAudioEnginePrivate::get(d->engine);
102 if (ep)
103 ep->setListenerRotation(q);
104}
105
106/*!
107 Returns the listener's orientation in 3D space.
108 */
109QQuaternion QAudioListener::rotation() const
110{
111 Q_D(const QAudioListener);
112 return d->rotation;
113}
114
115/*!
116 Returns the engine associated with this listener.
117 */
118QAudioEngine *QAudioListener::engine() const
119{
120 Q_D(const QAudioListener);
121 return d->engine;
122}
123
124QT_END_NAMESPACE
Combined button and popup list for selecting options.