summaryrefslogtreecommitdiffstats
path: root/src/spatialaudioquick3d/qquick3dambientsound.cpp
blob: 3b8ca49187148b3a5d0a1087576b9821c26f3952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-3.0-only
#include "qquick3dambientsound_p.h"
#include "qquick3daudioengine_p.h"
#include "qambientsound.h"
#include <QAudioFormat>
#include <qdir.h>
#include <QQmlContext>
#include <QQmlFile>

QT_BEGIN_NAMESPACE

/*!
    \qmltype AmbientSound
    \inqmlmodule QtQuick3D.SpatialAudio
    \ingroup quick3d_spatialaudio
    \ingroup multimedia_audio_qml

    \brief A stereo overlay sound.

    A AmbientSound represents a position and orientation independent sound.
    It's commonly used for background sounds (e.g. music) that is supposed to be independent
    of the listeners position and orientation.
  */

QQuick3DAmbientSound::QQuick3DAmbientSound()
{
    m_sound = new QAmbientSound(QQuick3DAudioEngine::getEngine());

    connect(m_sound, &QAmbientSound::sourceChanged, this, &QQuick3DAmbientSound::sourceChanged);
    connect(m_sound, &QAmbientSound::volumeChanged, this, &QQuick3DAmbientSound::volumeChanged);
    connect(m_sound, &QAmbientSound::loopsChanged, this, &QQuick3DAmbientSound::loopsChanged);
    connect(m_sound, &QAmbientSound::autoPlayChanged, this, &QQuick3DAmbientSound::autoPlayChanged);
}

QQuick3DAmbientSound::~QQuick3DAmbientSound()
{
    delete m_sound;
}

/*!
    \qmlproperty url AmbientSound::source

    The source file for the sound to be played.
 */
QUrl QQuick3DAmbientSound::source() const
{
    return m_sound->source();
}

void QQuick3DAmbientSound::setSource(QUrl source)
{
    const QQmlContext *context = qmlContext(this);
    QUrl url;
    if (context) {
        url = context->resolvedUrl(source);
    } else {
        url = QUrl::fromLocalFile(QDir::currentPath() + u"/");
        url = url.resolved(source);
    }
    m_sound->setSource(url);
}

/*!
    \qmlproperty float AmbientSound::volume

    Defines an overall volume for this sound source.
 */
void QQuick3DAmbientSound::setVolume(float volume)
{
    m_sound->setVolume(volume);
}

float QQuick3DAmbientSound::volume() const
{
    return m_sound->volume();
}

/*!
   \qmlproperty int AmbientSound::loops

    Determines how often the sound is played before the player stops.
    Set to QAmbienSound::Infinite to loop the current sound forever.

    The default value is \c 1.
 */
int QQuick3DAmbientSound::loops() const
{
    return m_sound->loops();
}

void QQuick3DAmbientSound::setLoops(int loops)
{
    m_sound->setLoops(loops);
}

/*!
   \qmlproperty bool AmbientSound::autoPlay

    Determines whether the sound should automatically start playing when a source
    gets specified.

    The default value is \c true.
 */
bool QQuick3DAmbientSound::autoPlay() const
{
    return m_sound->autoPlay();
}

void QQuick3DAmbientSound::setAutoPlay(bool autoPlay)
{
    m_sound->setAutoPlay(autoPlay);
}

/*!
    \qmlmethod AmbientSound::play()

    Starts playing back the sound. Does nothing if the sound is already playing.
 */
void QQuick3DAmbientSound::play()
{
    m_sound->play();
}

/*!
    \qmlmethod AmbientSound::pause()

    Pauses sound playback at the current position. Calling play() will continue playback.
 */
void QQuick3DAmbientSound::pause()
{
    m_sound->pause();
}

/*!
    \qmlmethod AmbientSound::stop()

    Stops sound playback and resets the current position and loop count to 0. Calling play() will
    begin playback at the beginning of the sound file.
 */
void QQuick3DAmbientSound::stop()
{
    m_sound->stop();
}

QT_END_NAMESPACE