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