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

source code of qtmultimedia/src/spatialaudio/qambientsound.cpp