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 <QtMultimedia/qaudiobuffer.h>
19#include <QtMultimedia/qaudiodecoder.h>
20#include <QtMultimedia/qaudioformat.h>
21#include <QtMultimedia/private/qmultimedia_source_resolver_p.h>
22#include <QtSpatialAudio/qambientsound.h>
23#include <QtSpatialAudio/private/qtspatialaudioglobal_p.h>
24#include <QtCore/qurl.h>
25#include <QtCore/qfuture.h>
26#include <QtCore/private/qobject_p.h>
27#include <QtCore/private/qexpected_p.h>
28
29#include <atomic>
30#include <memory>
31
32namespace vraudio {
33class ResonanceAudioApi;
34}
35
36QT_BEGIN_NAMESPACE
37
38class QAudioEngine;
39class QQuick3DSpatialSound;
41
42namespace QSpatialAudioPrivate {
43
45{
46public:
47 explicit QSpatialAudioPlaybackState(QAudioBuffer buffer, bool playing, int loops);
48
49 void getBuffer(QSpan<float> output);
50
51 void pause();
52 void resume();
53 void setLoops(int);
54
55 QAudioFormat format() const;
56
57private:
58 // controls
59 std::atomic_bool m_playing = false;
60 std::atomic_int m_loops = 1;
61
62 // state
63 int m_currentSample = 0;
64 int m_currentLoop = 0;
65
66 const QAudioBuffer m_buffer;
67};
68
69} // namespace QSpatialAudioPrivate
70
72{
73 Q_DECLARE_PUBLIC(QAmbientSound)
74
75public:
77 QAmbientSoundPrivate(QAudioEngine *engine, int nchannels, int sourceId);
79
80 template <typename T>
81 static QAmbientSoundPrivate *get(T *soundSource)
82 {
83 return soundSource ? soundSource->d_func() : nullptr;
84 }
85
86 QUrl url() const { return m_url; }
87 void loadUrl(const QUrl &url);
88
89 void setVolume(float volume);
90 float volume() const { return m_volume; }
91
92 int loops() const { return m_loops; }
93 void setLoops(int);
94 bool autoPlay() const { return m_autoPlay; }
95 void setAutoPlay(bool);
96
97 virtual void updateRoomEffects() { }
98
99 void play();
100 void pause();
101 void stop();
102
103 const int nchannels = 2;
104 QAudioEngine *const engine;
105 const int sourceId;
106
112 State state() const { return m_state; }
113
114protected:
115 template <typename Functor>
116 auto withResonanceApi(Functor &&f)
117 {
118 auto *api = getAPI();
119 if (api)
120 f(api);
121 else {
122 using result = std::invoke_result_t<Functor, vraudio::ResonanceAudioApi *>;
123 if constexpr (std::is_void_v<result>)
124 return;
125 else
126 return result{ };
127 }
128 }
129
130 virtual void applyVolume();
131
132private:
133 State m_state = State::Stopped;
134 bool m_autoPlay = true;
135
136 float m_volume = 1.f;
137 int m_loops = 1;
138
139 std::unique_ptr<QAudioDecoder> m_decoder;
140
141 std::optional<QAudioBuffer> m_buffer;
142 QFuture<void> m_loadFuture;
143
145 QFuture<LoadResult> load(QUrl resolvedUrl, QAudioFormat format);
146
147 QUrl m_url; // unresolved URL
150
151 friend class QQuick3DSpatialSound;
153 std::unique_ptr<const AbstractSourceResolver> m_sourceResolver =
155
156 vraudio::ResonanceAudioApi *getAPI();
157
158 // playback state
159 using QSpatialAudioPlaybackState = QSpatialAudioPrivate::QSpatialAudioPlaybackState;
160 using SharedPlaybackState = std::shared_ptr<QSpatialAudioPrivate::QSpatialAudioPlaybackState>;
161 SharedPlaybackState m_playbackState;
162
163 void setState(SharedPlaybackState);
164};
165
166QT_END_NAMESPACE
167
168#endif // QAMBIENTSOUND_P_H
void setVolume(float volume)
void loadUrl(const QUrl &url)
static QAmbientSoundPrivate * get(T *soundSource)
virtual void applyVolume()
auto withResonanceApi(Functor &&f)
QAudioEngine *const engine
virtual void updateRoomEffects()
QAmbientSoundPrivate(QAudioEngine *engine, int nchannels, int sourceId)
\inmodule QtSpatialAudio
QSpatialAudioPlaybackState(QAudioBuffer buffer, bool playing, int loops)