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
qsoundeffectwithplayer_p.h
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSOUNDEFFECTWITHPLAYER_P_H
5#define QSOUNDEFFECTWITHPLAYER_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 purely as an
12// implementation detail. 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/private/qaudiosystem_p.h>
19#include <QtMultimedia/private/qautoresetevent_p.h>
20#include <QtMultimedia/private/qrtaudioengine_p.h>
21#include <QtMultimedia/private/qsoundeffect_p.h>
22#include <QtMultimedia/qaudiosink.h>
23#include <QtMultimedia/qmediadevices.h>
24#include <QtCore/qchronotimer.h>
25
26QT_BEGIN_NAMESPACE
27
28namespace QtMultimediaPrivate {
29
31
33{
34public:
36 float volume, bool muted, int totalLoopCount,
38
40
41 Q_MULTIMEDIA_EXPORT VoicePlayResult play(QSpan<float>) noexcept QT_MM_NONBLOCKING override;
42
43 bool isActive() noexcept QT_MM_NONBLOCKING override;
44 const QAudioFormat &format() noexcept override { return m_sample->format(); }
45
46 int loopsRemaining() const { return m_loopsRemaining.load(std::memory_order_relaxed); }
48 clone(std::optional<QAudioFormat> newEngineFormat = {}) const;
49
50 Q_MULTIMEDIA_EXPORT qsizetype playVoice(QSpan<float>) noexcept QT_MM_NONBLOCKING;
51
56
58
59 float m_volume{};
60 bool m_muted{};
61
64
66};
67
68// Design notes
69// * each play() will start a new voice
70// * stop() will stop all voices
71// * the most recently created voice can be obtained via `activeVoice()`
72// * mute/volume will affect all voices
73// * loopCount/loopRemaining will be obtained from most recently created voice
74class QSoundEffectPrivateWithPlayer final : public QObject, public QSoundEffectPrivate
75{
76public:
77 Q_MULTIMEDIA_EXPORT
78 static std::shared_ptr<QRtAudioEngine> getEngineFor(const QAudioDevice &, const QAudioFormat &);
79
80 QSoundEffectPrivateWithPlayer(QSoundEffect *q, QAudioDevice audioDevice);
81 Q_DISABLE_COPY_MOVE(QSoundEffectPrivateWithPlayer)
82 ~QSoundEffectPrivateWithPlayer() override;
83
84 // QSoundEffectPrivate interface
85 bool setAudioDevice(QAudioDevice device) override;
86 QAudioDevice audioDevice() const override;
87 void setSource(QUrl, QSampleCache &) override;
88 QSoundEffect::Status status() const override;
89 int loopCount() const override;
90 bool setLoopCount(int) override;
91 int loopsRemaining() const override;
92 float volume() const override;
93 bool setVolume(float) override;
94 bool muted() const override;
95 bool setMuted(bool) override;
96 void play() override;
97 void stop() override;
98 bool playing() const override;
99
100private:
101 void play(std::shared_ptr<QSoundEffectVoice>);
102 void setStatus(QSoundEffect::Status status);
103 [[nodiscard]] bool updatePlayer(const SharedSamplePtr &sample);
104 std::optional<VoiceId> activeVoice() const;
105 static bool formatIsSupported(const QAudioFormat &);
106 void setResolvedAudioDevice(QAudioDevice device);
107 void resolveAudioDevice();
108
109 QSoundEffect *q_ptr{};
110 QAudioDevice m_audioDevice;
111 QAudioDevice m_resolvedAudioDevice;
112 std::shared_ptr<QRtAudioEngine> m_player;
113 QMetaObject::Connection m_voiceFinishedConnection;
114 std::set<std::shared_ptr<QSoundEffectVoice>, QRtAudioEngineVoiceCompare> m_voices;
115
116 void setLoopsRemaining(int);
117 int m_loopsRemaining{ 0 };
118
119 std::optional<QFuture<void>> m_sampleLoadFuture;
120 SharedSamplePtr m_sample;
121 float m_volume = 1.f;
122 bool m_muted = false;
123 int m_loopCount = 1;
124 bool m_playPending = false;
125
126 QSoundEffect::Status m_status{};
127
128 QMediaDevices m_mediaDevices;
129 QAudioDevice m_defaultAudioDevice;
130 QChronoTimer m_playerReleaseTimer{
131 std::chrono::seconds(2),
132 };
133};
134
135} // namespace QtMultimediaPrivate
136
137QT_END_NAMESPACE
138
139#endif // QSOUNDEFFECTWITHPLAYER_P_H
QSoundEffectPrivateWithPlayer(QSoundEffect *q, QAudioDevice audioDevice)
Q_MULTIMEDIA_EXPORT ~QSoundEffectVoice()
const std::shared_ptr< const QSample > m_sample
std::shared_ptr< QSoundEffectVoice > clone(std::optional< QAudioFormat > newEngineFormat={}) const
bool isActive() noexcept QT_MM_NONBLOCKING override
const QAudioFormat & format() noexcept override