Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qambientsound_p.h
Go to the documentation of this file.
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/qfuture.h>
23#include <QtCore/private/qobject_p.h>
24#include <QtCore/private/qexpected_p.h>
25#include <QtMultimedia/qaudiobuffer.h>
26#include <QtMultimedia/qaudiodecoder.h>
27#include <QtMultimedia/qaudioformat.h>
28#include <QtMultimedia/private/qmultimedia_source_resolver_p.h>
29
30#include <atomic>
31#include <memory>
32
33QT_BEGIN_NAMESPACE
34
35class QAudioEngine;
38
40{
41 Q_DECLARE_PUBLIC(QAmbientSound)
42
43public:
46
47 template <typename T>
48 static QAmbientSoundPrivate *get(T *soundSource)
49 {
50 return soundSource ? soundSource->d_func() : nullptr;
51 }
52
53 QUrl url() const { return m_url; }
54 void loadUrl(const QUrl &url);
55
56 void setVolume(float volume);
57 float volume() const { return m_volume; }
58
59protected:
60 virtual void applyVolume();
61
62public:
63 const int nchannels = 2;
64 QAudioEngine *const engine;
65
66 int sourceId = -1; // kInvalidSourceId
67
68 std::atomic_bool m_autoPlay = true;
69 std::atomic_bool m_playing = false;
70 std::atomic_int m_loops = 1;
71
72 void play() { m_playing = true; }
73 void pause() { m_playing = false; }
74 void stop()
75 {
76 QMutexLocker locker(&mutex);
77 m_playing = false;
78 currentBuffer = 0;
79 bufPos = 0;
80 m_currentLoop = 0;
81 }
82 void getBuffer(QSpan<float> buf, int frames, int channels);
83
84private:
85 float m_volume = 1.f;
86
87 std::unique_ptr<QAudioDecoder> m_decoder;
88
89 QMutex mutex;
90 int currentBuffer = 0;
91 int bufPos = 0;
92 int m_currentLoop = 0;
93
94 QList<QAudioBuffer> buffers;
95 QFuture<void> m_loadFuture;
96
98 QFuture<LoadResult> load(QUrl resolvedUrl, QAudioFormat format);
99
100 QUrl m_url; // unresolved URL
103
106 std::unique_ptr<const AbstractSourceResolver> m_sourceResolver =
108};
109
110QT_END_NAMESPACE
111
112#endif // QAMBIENTSOUND_P_H
void setVolume(float volume)
void loadUrl(const QUrl &url)
std::atomic_int m_loops
std::atomic_bool m_playing
static QAmbientSoundPrivate * get(T *soundSource)
virtual void applyVolume()
void getBuffer(QSpan< float > buf, int frames, int channels)
QAudioEngine *const engine
std::atomic_bool m_autoPlay