1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-3.0-only
3
4#ifndef QAMBIENTSOUND_P_H
5#define QAMBIENTSOUND_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of other Qt classes. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtSpatialAudio/qambientsound.h>
19#include <QtSpatialAudio/private/qtspatialaudioglobal_p.h>
20#include <QtCore/qmutex.h>
21#include <QtCore/qurl.h>
22#include <QtCore/qfile.h>
23#include <QtCore/private/qobject_p.h>
24#include <QtMultimedia/qaudiodecoder.h>
25#include <QtMultimedia/qaudiobuffer.h>
26
27QT_BEGIN_NAMESPACE
28
29class QAudioEngine;
30
31class QAmbientSoundPrivate : public QObjectPrivate
32{
33 Q_DECLARE_PUBLIC(QAmbientSound)
34
35public:
36 explicit QAmbientSoundPrivate(int nchannels = 2) : nchannels(nchannels) { }
37
38 template <typename T>
39 static QAmbientSoundPrivate *get(T *soundSource)
40 {
41 return soundSource ? soundSource->d_func() : nullptr;
42 }
43
44 QUrl url;
45 float volume = 1.;
46 int nchannels = 2;
47 std::unique_ptr<QAudioDecoder> decoder;
48 std::unique_ptr<QFile> sourceDeviceFile;
49 QAudioEngine *engine = nullptr;
50
51 QMutex mutex;
52 int currentBuffer = 0;
53 int bufPos = 0;
54 int m_currentLoop = 0;
55 QList<QAudioBuffer> buffers;
56 int sourceId = -1; // kInvalidSourceId
57
58 QAtomicInteger<bool> m_autoPlay = true;
59 QAtomicInteger<bool> m_playing = false;
60 QAtomicInt m_loops = 1;
61 bool m_loading = false;
62
63 void play() {
64 m_playing = true;
65 }
66 void pause() {
67 m_playing = false;
68 }
69 void stop() {
70 QMutexLocker locker(&mutex);
71 m_playing = false;
72 currentBuffer = 0;
73 bufPos = 0;
74 m_currentLoop = 0;
75 }
76
77 void load();
78 void getBuffer(float *buf, int frames, int channels);
79};
80
81QT_END_NAMESPACE
82
83#endif // QAMBIENTSOUND_P_H
84

source code of qtmultimedia/src/spatialaudio/qambientsound_p.h