Qt 6.x
The Qt SDK
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#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
12
31{
32 setEngine(engine);
33}
34
36{
37 setEngine(nullptr);
38 delete d;
39}
40
49void QAmbientSound::setVolume(float volume)
50{
51 if (d->volume == volume)
52 return;
53 d->volume = volume;
54 auto *ep = QAudioEnginePrivate::get(d->engine);
55 if (ep)
56 ep->resonanceAudio->api->SetSourceVolume(d->sourceId, d->volume);
58}
59
61{
62 return d->volume;
63}
64
66{
67 if (d->url == url)
68 return;
69 d->url = url;
70
71 d->load();
73}
74
81{
82 return d->url;
83}
102{
103 return d->m_loops.loadRelaxed();
104}
105
107{
108 int oldLoops = d->m_loops.fetchAndStoreRelaxed(loops);
109 if (oldLoops != loops)
111}
112
122{
123 return d->m_autoPlay.loadRelaxed();
124}
125
126void QAmbientSound::setAutoPlay(bool autoPlay)
127{
129 if (old != autoPlay)
131}
132
137{
138 d->play();
139}
140
145{
146 d->pause();
147}
148
154{
155 d->stop();
156}
157
161void QAmbientSound::setEngine(QAudioEngine *engine)
162{
163 if (d->engine == engine)
164 return;
166
167 if (ep)
168 ep->removeStereoSound(this);
169 d->engine = engine;
170
172 if (ep) {
173 ep->addStereoSound(this);
174 ep->resonanceAudio->api->SetSourceVolume(d->sourceId, d->volume);
175 }
176}
177
182{
183 return d->engine;
184}
185
187
188#include "moc_qambientsound.cpp"
QAudioEngine * engine
QAtomicInteger< bool > m_autoPlay
void setAutoPlay(bool autoPlay)
void volumeChanged()
QAmbientSound(QAudioEngine *engine)
Creates a stereo sound source for engine.
void pause()
Pauses sound playback.
void setLoops(int loops)
void play()
Starts playing back the sound.
QAudioEngine * engine() const
Returns the engine associated with this sound.
void loopsChanged()
int loops
Determines how many times the sound is played before the player stops.
float volume
Defines the volume of the sound.
bool autoPlay
Determines whether the sound should automatically start playing when a source gets specified.
void sourceChanged()
void autoPlayChanged()
void setSource(const QUrl &url)
QUrl source
The source file for the sound to be played.
void stop()
Stops sound playback and resets the current position and current loop count to 0.
void setVolume(float volume)
static QAudioEnginePrivate * get(QAudioEngine *engine)
\inmodule QtSpatialAudio
T fetchAndStoreRelaxed(T newValue) noexcept
T loadRelaxed() const noexcept
\inmodule QtCore
Definition qurl.h:94
#define this
Definition dialogs.cpp:9
Combined button and popup list for selecting options.
#define emit
QUrl url("example.com")
[constructor-url-reference]
QJSEngine engine
[0]