summaryrefslogtreecommitdiffstats
path: root/src/imports/audioengine/qdeclarative_soundinstance_p.cpp
blob: 4fcff6c498640a5afc227d6eed2e2f9f8475e3d8 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/****************************************************************************
**
** 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 "qdeclarative_soundinstance_p.h"
#include "qdeclarative_sound_p.h"
#include "qdeclarative_audioengine_p.h"
#include "qaudioengine_p.h"
#include "qsoundinstance_p.h"
#include "qdebug.h"

#define DEBUG_AUDIOENGINE

QT_USE_NAMESPACE

/*!
    \qmltype SoundInstance
    \instantiates QDeclarativeSoundInstance
    \since 5.0
    \brief Play 3d audio content.
    \inqmlmodule QtAudioEngine
    \ingroup multimedia_audioengine
    \inherits Item
    \preliminary
    \deprecated

    There are two ways to create SoundInstance objects. You can obtain it by calling newInstance
    method of a \l Sound:

    \qml
    Rectangle {
        id:root
        color:"white"
        width: 300
        height: 500

        AudioEngine {
            id:audioengine

            AudioSample {
                name:"explosion01"
                source: "explosion-01.wav"
            }

            Sound {
                name:"explosion"
                PlayVariation {
                    sample:"explosion01"
                }
            }
        }

        property variant soundEffect: audioengine.sounds["explosion"].newInstance();

        MouseArea {
            anchors.fill: parent
            onPressed: {
                root.soundEffect.play();
            }
        }
    }
    \endqml

    Or alternatively, you can explicitly define SoundInstance outside of AudioEngine for
    easier qml bindings:

    \qml
    Rectangle {
        id:root
        color:"white"
        width: 300
        height: 500

        AudioEngine {
            id:audioengine

            AudioSample {
                name:"explosion01"
                source: "explosion-01.wav"
            }

            Sound {
                name:"explosion"
                PlayVariation {
                    sample:"explosion01"
                }
            }
        }

        Item {
            id: animator
            x: 10 + observer.percent * 100
            y: 20 + observer.percent * 80
            property real percent: 0
            SequentialAnimation on percent {
                loops: Animation.Infinite
                running: true
                NumberAnimation {
                duration: 8000
                from: 0
                to: 1
                }

            }
        }

        SoundInstance {
            id:soundEffect
            engine:audioengine
            sound:"explosion"
            position:Qt.vector3d(animator.x, animator.y, 0);
        }

        MouseArea {
            anchors.fill: parent
            onPressed: {
                soundEffect.play();
            }
        }
    }
    \endqml
*/

QDeclarativeSoundInstance::QDeclarativeSoundInstance(QObject *parent)
    : QObject(parent)
    , m_position(0, 0, 0)
    , m_direction(0, 1, 0)
    , m_velocity(0, 0, 0)
    , m_gain(1)
    , m_pitch(1)
    , m_requestState(QDeclarativeSoundInstance::StoppedState)
    , m_coneInnerAngle(360)
    , m_coneOuterAngle(360)
    , m_coneOuterGain(0)
    , m_instance(0)
    , m_engine(0)
{
#ifdef DEBUG_AUDIOENGINE
    qDebug() << "QDeclarativeSoundInstance::ctor()";
#endif
}

/*!
    \qmlproperty QtAudioEngine::AudioEngine QtAudioEngine::SoundInstance::engine

    This property holds the reference to AudioEngine, must be set only once.
*/
QDeclarativeAudioEngine* QDeclarativeSoundInstance::engine() const
{
    return m_engine;
}

void QDeclarativeSoundInstance::setEngine(QDeclarativeAudioEngine *engine)
{
#ifdef DEBUG_AUDIOENGINE
    qDebug() << "QDeclarativeSoundInstance::setEngine(" << engine << ")";
#endif
    if (!engine)
        return;

    if (m_engine) {
        qWarning("SoundInstance: you can not set different value for engine property");
        return;
    }
    m_engine = engine;
    if (!m_engine->isReady()) {
        connect(m_engine, SIGNAL(ready()), this, SLOT(engineComplete()));
    } else {
        engineComplete();
    }
}

void QDeclarativeSoundInstance::engineComplete()
{
#ifdef DEBUG_AUDIOENGINE
    qDebug() << "QDeclarativeSoundInstance::engineComplete()";
#endif
    disconnect(m_engine, SIGNAL(ready()), this, SLOT(engineComplete()));
    if (m_sound.isEmpty())
        return;

    //rebind to actual engine resource
    QString sound = m_sound;
    m_sound.clear();
    setSound(sound);
}

QDeclarativeSoundInstance::~QDeclarativeSoundInstance()
{
}

/*!
    \qmlproperty string QtAudioEngine::SoundInstance::sound

    This property specifies which Sound this SoundInstance will use. Unlike some properties in
    other types, this property can be changed dynamically.
*/
QString QDeclarativeSoundInstance::sound() const
{
    return m_sound;
}

void QDeclarativeSoundInstance::setSound(const QString& sound)
{
#ifdef DEBUG_AUDIOENGINE
    qDebug() << "QDeclarativeSoundInstance::setSound(" << sound << ")";
#endif
    if (m_sound == sound)
        return;

    if (!m_engine || !m_engine->isReady()) {
        m_sound = sound;
        emit soundChanged();
        return;
    }

#ifdef DEBUG_AUDIOENGINE
    qDebug() << "SoundInstance switch sound from [" << m_sound << "] to [" << sound << "]";
#endif

    stop();
    dropInstance();

    m_sound = sound;
    if (!m_sound.isEmpty()) {
        m_instance = m_engine->newSoundInstance(m_sound);
        connect(m_instance, SIGNAL(stateChanged(QSoundInstance::State)), this, SLOT(handleStateChanged()));
        m_instance->setPosition(m_position);
        m_instance->setDirection(m_direction);
        m_instance->setVelocity(m_velocity);
        m_instance->setGain(m_gain);
        m_instance->setPitch(m_pitch);
        m_instance->setCone(m_coneInnerAngle, m_coneOuterAngle, m_coneOuterGain);
        if (m_requestState == QDeclarativeSoundInstance::PlayingState) {
            m_instance->play();
        } else if (m_requestState == QDeclarativeSoundInstance::PausedState) {
            m_instance->pause();
        }
    }
    emit soundChanged();
}

void QDeclarativeSoundInstance::dropInstance()
{
    if (m_instance) {
        disconnect(m_instance, SIGNAL(stateChanged(QSoundInstance::State)), this, SLOT(handleStateChanged()));
        m_engine->releaseSoundInstance(m_instance);
        m_instance = 0;
    }
}

/*!
    \qmlproperty enumeration QtAudioEngine::SoundInstance::state

    This property holds the current playback state. It can be one of:

    \table
    \header \li Value \li Description
    \row \li StopppedState
        \li The SoundInstance is not playing, and when playback begins next it
        will play from position zero.
    \row \li PlayingState
        \li The SoundInstance is playing the media.
    \row \li PausedState
        \li The SoundInstance is not playing, and when playback begins next it
        will play from the position that it was paused at.
    \endtable
*/
QDeclarativeSoundInstance::State QDeclarativeSoundInstance::state() const
{
    if (m_instance)
        return State(m_instance->state());
    return m_requestState;
}

/*!
    \qmlmethod QtAudioEngine::SoundInstance::play()

    Starts playback.
*/
void QDeclarativeSoundInstance::play()
{
#ifdef DEBUG_AUDIOENGINE
    qDebug() << "QDeclarativeSoundInstance::play()";
#endif
    if (!m_instance) {
        m_requestState = QDeclarativeSoundInstance::PlayingState;
        return;
    }
    m_instance->play();
}

/*!
    \qmlmethod QtAudioEngine::SoundInstance::stop()

    Stops current playback.
*/
void QDeclarativeSoundInstance::stop()
{
#ifdef DEBUG_AUDIOENGINE
    qDebug() << "QDeclarativeSoundInstance::stop()";
#endif
    m_requestState = QDeclarativeSoundInstance::StoppedState;
    if (!m_instance)
        return;
    m_instance->stop();
}

/*!
    \qmlmethod QtAudioEngine::SoundInstance::pause()

    Pauses current playback.
*/
void QDeclarativeSoundInstance::pause()
{
#ifdef DEBUG_AUDIOENGINE
    qDebug() << "QDeclarativeSoundInstance::pause()";
#endif
    if (!m_instance) {
        m_requestState = QDeclarativeSoundInstance::PausedState;
        return;
    }
    m_instance->pause();
}

void QDeclarativeSoundInstance::updatePosition(qreal deltaTime)
{
    if (!m_instance || deltaTime == 0 || m_velocity.lengthSquared() == 0)
        return;
    setPosition(m_position + m_velocity * deltaTime);
}

/*!
    \qmlproperty vector3d QtAudioEngine::SoundInstance::position

    This property holds the current 3d position.
*/
QVector3D QDeclarativeSoundInstance::position() const
{
    return m_position;
}

void QDeclarativeSoundInstance::setPosition(const QVector3D& position)
{
    if (m_position == position)
        return;
    m_position = position;
    emit positionChanged();
    if (!m_instance) {
        return;
    }
    m_instance->setPosition(m_position);
}

/*!
    \qmlproperty vector3d QtAudioEngine::SoundInstance::direction

    This property holds the current 3d direction.
*/
QVector3D QDeclarativeSoundInstance::direction() const
{
    return m_direction;
}

void QDeclarativeSoundInstance::setDirection(const QVector3D& direction)
{
    if (m_direction == direction)
        return;
    m_direction = direction;
    emit directionChanged();
    if (!m_instance) {
        return;
    }
    m_instance->setDirection(m_direction);
}

/*!
    \qmlproperty vector3d QtAudioEngine::SoundInstance::velocity

    This property holds the current 3d velocity.
*/
QVector3D QDeclarativeSoundInstance::velocity() const
{
    return m_velocity;
}

void QDeclarativeSoundInstance::setVelocity(const QVector3D& velocity)
{
    if (m_velocity == velocity)
        return;
    m_velocity = velocity;
    emit velocityChanged();
    if (!m_instance)
        return;
    m_instance->setVelocity(m_velocity);
}

/*!
    \qmlproperty vector3d QtAudioEngine::SoundInstance::gain

    This property holds the gain adjustment which will be used to modulate the audio output level
    from this SoundInstance.
*/
qreal QDeclarativeSoundInstance::gain() const
{
    return m_gain;
}

void QDeclarativeSoundInstance::setGain(qreal gain)
{
    if (gain == m_gain)
        return;
    if (gain < 0) {
        qWarning("gain must be a positive value!");
        return;
    }
    m_gain = gain;
    emit gainChanged();
    if (!m_instance)
        return;
    m_instance->setGain(m_gain);
}

/*!
    \qmlproperty vector3d QtAudioEngine::SoundInstance::pitch

    This property holds the pitch adjustment which will be used to modulate the audio pitch
    from this SoundInstance.
*/
qreal QDeclarativeSoundInstance::pitch() const
{
    return m_pitch;
}

void QDeclarativeSoundInstance::setPitch(qreal pitch)
{
    if (pitch == m_pitch)
        return;
    if (pitch < 0) {
        qWarning("pitch must be a positive value!");
        return;
    }
    m_pitch = pitch;
    emit pitchChanged();
    if (!m_instance)
        return;
    m_instance->setPitch(m_pitch);
}

void QDeclarativeSoundInstance::setConeInnerAngle(qreal innerAngle)
{
    if (m_coneInnerAngle == innerAngle)
        return;
    m_coneInnerAngle = innerAngle;
    if (!m_instance)
        return;
    m_instance->setCone(m_coneInnerAngle, m_coneOuterAngle, m_coneOuterGain);
}

void QDeclarativeSoundInstance::setConeOuterAngle(qreal outerAngle)
{
    if (m_coneOuterAngle == outerAngle)
        return;
    m_coneOuterAngle = outerAngle;
    if (!m_instance)
        return;
    m_instance->setCone(m_coneInnerAngle, m_coneOuterAngle, m_coneOuterGain);
}

void QDeclarativeSoundInstance::setConeOuterGain(qreal outerGain)
{
    if (m_coneOuterGain == outerGain)
        return;
    m_coneOuterGain = outerGain;
    if (!m_instance)
        return;
    m_instance->setCone(m_coneInnerAngle, m_coneOuterAngle, m_coneOuterGain);
}

void QDeclarativeSoundInstance::handleStateChanged()
{
    emit stateChanged();
}

/*!
    \qmlsignal QtAudioEngine::SoundInstance::stateChanged(state)

    This signal is emitted when \a state is changed.

    The corresponding handler is \c onStateChanged.
*/

/*!
    \qmlsignal QtAudioEngine::SoundInstance::positionChanged()

    This signal is emitted when \l position is changed.

    The corresponding handler is \c onPositionChanged.
*/

/*!
    \qmlsignal QtAudioEngine::SoundInstance::directionChanged()

    This signal is emitted when \l direction is changed.

    The corresponding handler is \c onDirectionChanged.
*/

/*!
    \qmlsignal QtAudioEngine::SoundInstance::velocityChanged()

    This signal is emitted when \l velocity is changed.

    The corresponding handler is \c onVelocityChanged.
*/

/*!
    \qmlsignal QtAudioEngine::SoundInstance::gainChanged()

    This signal is emitted when \l gain is changed.

    The corresponding handler is \c onGainChanged.
*/

/*!
    \qmlsignal QtAudioEngine::SoundInstance::pitchChanged()

    This signal is emitted when \l pitch is changed.

    The corresponding handler is \c onPitchChanged.
*/

/*!
    \qmlsignal QtAudioEngine::SoundInstance::soundChanged()

    This signal is emitted when \l sound is changed.

    The corresponding handler is \c onSoundChanged.
*/