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
qspatialaudiosound_p.h
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-3.0-only
3
4#ifndef QSPATIALAUDIOSOUND_P_H
5#define QSPATIALAUDIOSOUND_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 <QtMultimedia/qaudiobuffer.h>
19#include <QtMultimedia/qaudioformat.h>
20#include <QtMultimedia/private/qmultimedia_source_resolver_p.h>
21#include <QtMultimedia/private/qsamplecache_p.h>
22#include <QtSpatialAudio/private/qtspatialaudioglobal_p.h>
23#include <QtCore/qurl.h>
24#include <QtCore/qfuture.h>
25#include <QtCore/private/qobject_p.h>
26
27#include <atomic>
28#include <memory>
29
30namespace vraudio {
31class ResonanceAudioApi;
32}
33
34QT_BEGIN_NAMESPACE
35
36class QAudioEngine;
37class QQuick3DSpatialSound;
39
40namespace QSpatialAudioPrivate {
41
43{
44public:
45 explicit QSpatialAudioPlaybackState(QAudioBuffer buffer, bool playing, int loops);
46
47 void getBuffer(QSpan<float> output);
48
49 void pause();
50 void resume();
51 void setLoops(int);
52
53 QAudioFormat format() const;
54
55private:
56 // controls
57 std::atomic_bool m_playing = false;
58 std::atomic_int m_loops = 1;
59
60 // state
61 int m_currentSample = 0;
62 int m_currentLoop = 0;
63
64 const QAudioBuffer m_buffer;
65};
66
67} // namespace QSpatialAudioPrivate
68
70{
71public:
72 QSpatialAudioSoundPrivate(QAudioEngine *engine, int nchannels, int sourceId);
74
75 QUrl url() const { return m_url; }
76 void loadUrl(const QUrl &url);
77
78 void setVolume(float volume);
79 float volume() const { return m_volume; }
80
81 int loops() const { return m_loops; }
82 void setLoops(int);
83 bool autoPlay() const { return m_autoPlay; }
84 void setAutoPlay(bool);
85
86 virtual void updateRoomEffects() { }
87
88 void play();
89 void pause();
90 void stop();
91
92 const int nchannels = 2;
93 QAudioEngine *const engine;
94 const int sourceId;
95
96 enum class State : uint8_t {
100 };
101 State state() const { return m_state; }
102
103protected:
104 template <typename Functor>
105 auto withResonanceApi(Functor &&f)
106 {
107 auto *api = getAPI();
108 if (api)
109 f(api);
110 else {
111 using result = std::invoke_result_t<Functor, vraudio::ResonanceAudioApi *>;
112 if constexpr (std::is_void_v<result>)
113 return;
114 else
115 return result{ };
116 }
117 }
118
119 virtual void applyVolume();
120
121private:
122 State m_state = State::Stopped;
123 bool m_autoPlay = true;
124
125 float m_volume = 1.f;
126 int m_loops = 1;
127
128 SharedSamplePtr m_sample;
129 std::optional<QAudioBuffer> m_buffer;
130 QFuture<void> m_loadFuture;
131
132 QUrl m_url; // unresolved URL
135
136 friend class QQuick3DSpatialSound;
138 std::unique_ptr<const AbstractSourceResolver> m_sourceResolver =
140
141 vraudio::ResonanceAudioApi *getAPI();
142
143 // playback state
144 using QSpatialAudioPlaybackState = QSpatialAudioPrivate::QSpatialAudioPlaybackState;
145 using SharedPlaybackState = std::shared_ptr<QSpatialAudioPrivate::QSpatialAudioPlaybackState>;
146 SharedPlaybackState m_playbackState;
147
148 void setState(SharedPlaybackState);
149};
150
151QT_END_NAMESPACE
152
153#endif // QSPATIALAUDIOSOUND_P_H
QSpatialAudioPlaybackState(QAudioBuffer buffer, bool playing, int loops)
void loadUrl(const QUrl &url)
QSpatialAudioSoundPrivate(QAudioEngine *engine, int nchannels, int sourceId)