summaryrefslogtreecommitdiffstats
path: root/src/imports/audioengine/qsoundsource_openal_p.cpp
blob: 5fce57491d59af194afa6d1f17db943a084a600a (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qaudioengine_openal_p.h"
#include "qdebug.h"

#define DEBUG_AUDIOENGINE

QT_USE_NAMESPACE

QSoundSourcePrivate::QSoundSourcePrivate(QObject *parent)
    : QSoundSource(parent)
    , m_alSource(0)
    , m_bindBuffer(0)
    , m_isReady(false)
    , m_state(QSoundSource::StoppedState)
    , m_gain(0)
    , m_pitch(0)
    , m_coneInnerAngle(0)
    , m_coneOuterAngle(0)
    , m_coneOuterGain(1)
{
#ifdef DEBUG_AUDIOENGINE
    qDebug() << "creating new QSoundSourcePrivate";
#endif
    alGenSources(1, &m_alSource);
    QAudioEnginePrivate::checkNoError("create source");
    setGain(1);
    setPitch(1);
    setCone(360, 360, 0);
}

QSoundSourcePrivate::~QSoundSourcePrivate()
{
#ifdef DEBUG_AUDIOENGINE
    qDebug() << "QSoundSourcePrivate::dtor";
#endif
    release();
}

void QSoundSourcePrivate::release()
{
    if (m_alSource) {
#ifdef DEBUG_AUDIOENGINE
    qDebug() << "QSoundSourcePrivate::release";
#endif
        stop();
        unbindBuffer();
        alDeleteSources(1, &m_alSource);
        QAudioEnginePrivate::checkNoError("delete source");
        m_alSource = 0;
    }
}

void QSoundSourcePrivate::bindBuffer(QSoundBuffer* soundBuffer)
{
    unbindBuffer();
    Q_ASSERT(soundBuffer->state() == QSoundBuffer::Ready);
    m_bindBuffer = qobject_cast<QSoundBufferPrivateAL*>(soundBuffer);
    m_bindBuffer->bindToSource(m_alSource);
    m_isReady = true;
}

void QSoundSourcePrivate::unbindBuffer()
{
    if (m_bindBuffer) {
        m_bindBuffer->unbindFromSource(m_alSource);
        m_bindBuffer = 0;
    }
    m_isReady = false;
    if (m_state != QSoundSource::StoppedState) {
        m_state = QSoundSource::StoppedState;
        emit stateChanged(m_state);
    }
}

void QSoundSourcePrivate::play()
{
    if (!m_alSource || !m_isReady)
        return;
    alSourcePlay(m_alSource);
#ifdef DEBUG_AUDIOENGINE
    QAudioEnginePrivate::checkNoError("play");
#endif
    emit activate(this);
}

bool QSoundSourcePrivate::isLooping() const
{
    if (!m_alSource)
        return false;
    ALint looping = 0;
    alGetSourcei(m_alSource, AL_LOOPING, &looping);
    return looping == AL_TRUE ? true : false;
}

void QSoundSourcePrivate::pause()
{
    if (!m_alSource || !m_isReady)
        return;
    alSourcePause(m_alSource);
#ifdef DEBUG_AUDIOENGINE
    QAudioEnginePrivate::checkNoError("pause");
#endif
}

void QSoundSourcePrivate::stop()
{
    if (!m_alSource)
        return;
    alSourceStop(m_alSource);
#ifdef DEBUG_AUDIOENGINE
    QAudioEnginePrivate::checkNoError("stop");
#endif
}

QSoundSource::State QSoundSourcePrivate::state() const
{
    return m_state;
}

void QSoundSourcePrivate::checkState()
{
    QSoundSource::State st;
    st = QSoundSource::StoppedState;
    if (m_alSource && m_isReady) {
        ALint s;
        alGetSourcei(m_alSource, AL_SOURCE_STATE, &s);
        switch (s) {
        case AL_PLAYING:
            st = QSoundSource::PlayingState;
            break;
        case AL_PAUSED:
            st = QSoundSource::PausedState;
            break;
        }
    }
    if (st == m_state)
        return;
    m_state = st;
    emit stateChanged(m_state);
}

void QSoundSourcePrivate::setLooping(bool looping)
{
    if (!m_alSource)
        return;
    alSourcei(m_alSource, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
}

void QSoundSourcePrivate::setPosition(const QVector3D& position)
{
    if (!m_alSource)
        return;
    alSource3f(m_alSource, AL_POSITION, position.x(), position.y(), position.z());
#ifdef DEBUG_AUDIOENGINE
    QAudioEnginePrivate::checkNoError("source set position");
#endif
}

void QSoundSourcePrivate::setDirection(const QVector3D& direction)
{
    if (!m_alSource)
        return;
    alSource3f(m_alSource, AL_DIRECTION, direction.x(), direction.y(), direction.z());
#ifdef DEBUG_AUDIOENGINE
    QAudioEnginePrivate::checkNoError("source set direction");
#endif
}

void QSoundSourcePrivate::setVelocity(const QVector3D& velocity)
{
    if (!m_alSource)
        return;
    alSource3f(m_alSource, AL_VELOCITY, velocity.x(), velocity.y(), velocity.z());
#ifdef DEBUG_AUDIOENGINE
    QAudioEnginePrivate::checkNoError("source set velocity");
#endif
}

QVector3D QSoundSourcePrivate::velocity() const
{
    if (!m_alSource)
        return QVector3D(0, 0, 0);
    ALfloat x, y, z;
    alGetSource3f(m_alSource, AL_VELOCITY, &x, &y, &z);
    return QVector3D(x, y, z);
}

QVector3D QSoundSourcePrivate::position() const
{
    if (!m_alSource)
        return QVector3D(0, 0, 0);
    ALfloat x, y, z;
    alGetSource3f(m_alSource, AL_POSITION, &x, &y, &z);
    return QVector3D(x, y, z);
}

QVector3D QSoundSourcePrivate::direction() const
{
    if (!m_alSource)
        return QVector3D(0, 1, 0);
    ALfloat x, y, z;
    alGetSource3f(m_alSource, AL_DIRECTION, &x, &y, &z);
    return QVector3D(x, y, z);
}

void QSoundSourcePrivate::setGain(qreal gain)
{
    if (!m_alSource || gain == m_gain)
        return;
    alSourcef(m_alSource, AL_GAIN, gain);
#ifdef DEBUG_AUDIOENGINE
    QAudioEnginePrivate::checkNoError("source set gain");
#endif
    m_gain = gain;
}

void QSoundSourcePrivate::setPitch(qreal pitch)
{
    if (!m_alSource || m_pitch == pitch)
        return;
    alSourcef(m_alSource, AL_PITCH, pitch);
#ifdef DEBUG_AUDIOENGINE
    QAudioEnginePrivate::checkNoError("source set pitch");
#endif
    m_pitch = pitch;
}

void QSoundSourcePrivate::setCone(qreal innerAngle, qreal outerAngle, qreal outerGain)
{
    if (innerAngle > outerAngle)
        outerAngle = innerAngle;
    Q_ASSERT(outerAngle <= 360 && innerAngle >= 0);

    //make sure the setting order will always keep outerAngle >= innerAngle in openAL
    if (outerAngle >= m_coneInnerAngle) {
        if (m_coneOuterAngle != outerAngle) {
            alSourcef(m_alSource, AL_CONE_OUTER_ANGLE, outerAngle);
#ifdef DEBUG_AUDIOENGINE
            QAudioEnginePrivate::checkNoError("source set cone outerAngle");
#endif
            m_coneOuterAngle = outerAngle;
        }
        if (m_coneInnerAngle != innerAngle) {
            alSourcef(m_alSource, AL_CONE_INNER_ANGLE, innerAngle);
#ifdef DEBUG_AUDIOENGINE
            QAudioEnginePrivate::checkNoError("source set cone innerAngle");
#endif
            m_coneInnerAngle = innerAngle;
        }
    } else {
        if (m_coneInnerAngle != innerAngle) {
            alSourcef(m_alSource, AL_CONE_INNER_ANGLE, innerAngle);
#ifdef DEBUG_AUDIOENGINE
            QAudioEnginePrivate::checkNoError("source set cone innerAngle");
#endif
            m_coneInnerAngle = innerAngle;
        }
        if (m_coneOuterAngle != outerAngle) {
            alSourcef(m_alSource, AL_CONE_OUTER_ANGLE, outerAngle);
#ifdef DEBUG_AUDIOENGINE
            QAudioEnginePrivate::checkNoError("source set cone outerAngle");
#endif
            m_coneOuterAngle = outerAngle;
        }
    }

    if (outerGain != m_coneOuterGain) {
        alSourcef(m_alSource, AL_CONE_OUTER_GAIN, outerGain);
#ifdef DEBUG_AUDIOENGINE
        QAudioEnginePrivate::checkNoError("source set cone outerGain");
#endif
        m_coneOuterGain = outerGain;
    }
}