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
qambientsound.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 <QtSpatialAudio/private/qspatialaudiosound_p.h>
8#include <QtCore/qdebug.h>
9#include <QtCore/qurl.h>
10
11#include <resonance_audio.h>
12
13#include <memory>
14
15QT_BEGIN_NAMESPACE
16
17namespace {
18
19int addStereoSource(QAudioEngine *engine)
20{
21 auto *ep = QAudioEnginePrivate::get(engine);
22 if (!ep)
23 return -1;
24 return ep->resonanceAudio->api->CreateStereoSource(2);
25}
26
27} // namespace
28
30{
31 Q_DECLARE_PUBLIC(QAmbientSound)
32
33public:
35};
36
37QAmbientSoundPrivate::QAmbientSoundPrivate(QAudioEngine *engine)
38 : QSpatialAudioSoundPrivate{
39 engine,
40 2,
41 addStereoSource(engine),
42 }
43{
44 QSpatialAudioSoundPrivate::applyVolume();
45}
46
47/*!
48 \class QAmbientSound
49 \inmodule QtSpatialAudio
50 \ingroup spatialaudio
51 \ingroup multimedia_audio
52
53 \brief A stereo overlay sound.
54
55 QAmbientSound represents a position and orientation independent sound.
56 It's commonly used for background sounds (e.g. music) that is supposed to be independent
57 of the listeners position and orientation.
58
59 \note QAmbientSound is only active when the QAudioEngine is using OutputMode::Stereo or
60 OutputMode::Headphone.
61 */
62
63/*!
64 Creates a stereo sound source for \a engine.
65
66 \note Must be called with a valid QAudioEngine
67 */
68QAmbientSound::QAmbientSound(QAudioEngine *engine) : QObject(*new QAmbientSoundPrivate(engine))
69{
70 if (!engine)
71 qWarning() << "Cannot create QAmbientSound without a valid QAudioEngine";
72}
73
74QAmbientSound::~QAmbientSound()
75{
76 Q_D(QAmbientSound);
77 if (d->state() != QSpatialAudioSoundPrivate::State::Stopped)
78 d->stop();
79}
80
81/*!
82 \property QAmbientSound::volume
83
84 Defines the volume of the sound.
85
86 Values between 0 and 1 will attenuate the sound, while values above 1
87 provide an additional gain boost.
88 */
89void QAmbientSound::setVolume(float volume)
90{
91 Q_D(QAmbientSound);
92 if (volume != d->volume()) {
93 d->setVolume(volume);
94 emit volumeChanged();
95 }
96}
97
98float QAmbientSound::volume() const
99{
100 Q_D(const QAmbientSound);
101 return d->volume();
102}
103
104void QAmbientSound::setSource(const QUrl &url)
105{
106 Q_D(QAmbientSound);
107 if (d->url() == url)
108 return;
109 d->loadUrl(url);
110
111 emit sourceChanged();
112}
113
114/*!
115 \property QAmbientSound::source
116
117 The source file for the sound to be played.
118 */
119QUrl QAmbientSound::source() const
120{
121 Q_D(const QAmbientSound);
122 return d->url();
123}
124/*!
125 \enum QAmbientSound::Loops
126
127 Lets you control the playback loop using the following values:
128
129 \value Infinite Loops infinitely
130 \value Once Stops playback after running once
131*/
132/*!
133 \property QAmbientSound::loops
134
135 Determines how many times the sound is played before the player stops.
136 Set to QAmbientSound::Infinite to play the current sound in
137 a loop forever.
138
139 The default value is \c 1.
140 */
141int QAmbientSound::loops() const
142{
143 Q_D(const QAmbientSound);
144 return d->loops();
145}
146
147void QAmbientSound::setLoops(int loops)
148{
149 Q_D(QAmbientSound);
150 if (loops != d->loops()) {
151 d->setLoops(loops);
152 emit loopsChanged();
153 }
154}
155
156/*!
157 \property QAmbientSound::autoPlay
158
159 Determines whether the sound should automatically start playing when a source
160 gets specified.
161
162 The default value is \c true.
163 */
164bool QAmbientSound::autoPlay() const
165{
166 Q_D(const QAmbientSound);
167 return d->autoPlay();
168}
169
170void QAmbientSound::setAutoPlay(bool autoPlay)
171{
172 Q_D(QAmbientSound);
173 if (autoPlay != d->autoPlay()) {
174 d->setAutoPlay(autoPlay);
175 emit autoPlayChanged();
176 }
177}
178
179/*!
180 Starts playing back the sound. Does nothing if the sound is already playing.
181 */
182void QAmbientSound::play()
183{
184 Q_D(QAmbientSound);
185 d->play();
186}
187
188/*!
189 Pauses sound playback. Calling play() will continue playback.
190 */
191void QAmbientSound::pause()
192{
193 Q_D(QAmbientSound);
194 d->pause();
195}
196
197/*!
198 Stops sound playback and resets the current position and current loop count to 0.
199 Calling play() will start playback at the beginning of the sound file.
200 */
201void QAmbientSound::stop()
202{
203 Q_D(QAmbientSound);
204 d->stop();
205}
206
207/*!
208 Returns the engine associated with this sound.
209 */
210QAudioEngine *QAmbientSound::engine() const
211{
212 Q_D(const QAmbientSound);
213
214 return d->engine;
215}
216
217QT_END_NAMESPACE
218
219#include "moc_qambientsound.cpp"
QAmbientSoundPrivate(QAudioEngine *engine)
int addStereoSource(QAudioEngine *engine)