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 "qquick3dambientsound_p.h"
4#include "qquick3daudioengine_p.h"
5#include "qambientsound.h"
6#include <QAudioFormat>
7#include <qdir.h>
8#include <QQmlContext>
9#include <QQmlFile>
10
11QT_BEGIN_NAMESPACE
12
13/*!
14 \qmltype AmbientSound
15 \inqmlmodule QtQuick3D.SpatialAudio
16 \ingroup quick3d_spatialaudio
17 \ingroup multimedia_audio_qml
18
19 \brief A stereo overlay sound.
20
21 A AmbientSound 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
26QQuick3DAmbientSound::QQuick3DAmbientSound()
27{
28 m_sound = new QAmbientSound(QQuick3DAudioEngine::getEngine());
29
30 connect(sender: m_sound, signal: &QAmbientSound::sourceChanged, context: this, slot: &QQuick3DAmbientSound::sourceChanged);
31 connect(sender: m_sound, signal: &QAmbientSound::volumeChanged, context: this, slot: &QQuick3DAmbientSound::volumeChanged);
32 connect(sender: m_sound, signal: &QAmbientSound::loopsChanged, context: this, slot: &QQuick3DAmbientSound::loopsChanged);
33 connect(sender: m_sound, signal: &QAmbientSound::autoPlayChanged, context: this, slot: &QQuick3DAmbientSound::autoPlayChanged);
34}
35
36QQuick3DAmbientSound::~QQuick3DAmbientSound()
37{
38 delete m_sound;
39}
40
41/*!
42 \qmlproperty url AmbientSound::source
43
44 The source file for the sound to be played.
45 */
46QUrl QQuick3DAmbientSound::source() const
47{
48 return m_sound->source();
49}
50
51void QQuick3DAmbientSound::setSource(QUrl source)
52{
53 const QQmlContext *context = qmlContext(this);
54 QUrl url;
55 if (context) {
56 url = context->resolvedUrl(source);
57 } else {
58 url = QUrl::fromLocalFile(localfile: QDir::currentPath() + u"/");
59 url = url.resolved(relative: source);
60 }
61 m_sound->setSource(url);
62}
63
64/*!
65 \qmlproperty float AmbientSound::volume
66
67 Defines an overall volume for this sound source.
68 */
69void QQuick3DAmbientSound::setVolume(float volume)
70{
71 m_sound->setVolume(volume);
72}
73
74float QQuick3DAmbientSound::volume() const
75{
76 return m_sound->volume();
77}
78
79/*!
80 \qmlproperty int AmbientSound::loops
81
82 Determines how often the sound is played before the player stops.
83 Set to QAmbienSound::Infinite to loop the current sound forever.
84
85 The default value is \c 1.
86 */
87int QQuick3DAmbientSound::loops() const
88{
89 return m_sound->loops();
90}
91
92void QQuick3DAmbientSound::setLoops(int loops)
93{
94 m_sound->setLoops(loops);
95}
96
97/*!
98 \qmlproperty bool AmbientSound::autoPlay
99
100 Determines whether the sound should automatically start playing when a source
101 gets specified.
102
103 The default value is \c true.
104 */
105bool QQuick3DAmbientSound::autoPlay() const
106{
107 return m_sound->autoPlay();
108}
109
110void QQuick3DAmbientSound::setAutoPlay(bool autoPlay)
111{
112 m_sound->setAutoPlay(autoPlay);
113}
114
115/*!
116 \qmlmethod AmbientSound::play()
117
118 Starts playing back the sound. Does nothing if the sound is already playing.
119 */
120void QQuick3DAmbientSound::play()
121{
122 m_sound->play();
123}
124
125/*!
126 \qmlmethod AmbientSound::pause()
127
128 Pauses sound playback at the current position. Calling play() will continue playback.
129 */
130void QQuick3DAmbientSound::pause()
131{
132 m_sound->pause();
133}
134
135/*!
136 \qmlmethod AmbientSound::stop()
137
138 Stops sound playback and resets the current position and loop count to 0. Calling play() will
139 begin playback at the beginning of the sound file.
140 */
141void QQuick3DAmbientSound::stop()
142{
143 m_sound->stop();
144}
145
146QT_END_NAMESPACE
147

source code of qtmultimedia/src/spatialaudioquick3d/qquick3dambientsound.cpp