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/qaudiosink.h>
19#include <QtMultimedia/qmediadevices.h>
20#include <QtMultimedia/private/qaudiosystem_p.h>
21#include <QtMultimedia/private/qautoresetevent_p.h>
22#include <QtMultimedia/private/qrtaudioengine_p.h>
23#include <QtMultimedia/private/qsoundeffect_p.h>
24
25QT_BEGIN_NAMESPACE
26
27namespace QtMultimediaPrivate {
28
30
32{
33public:
34 QSoundEffectVoice(VoiceId voiceId, std::shared_ptr<const QSample> sample, float volume,
35 bool muted, int totalLoopCount);
36
38 bool isActive() noexcept QT_MM_NONBLOCKING override;
39 const QAudioFormat &format() noexcept override { return m_sample->format(); }
40
41 int loopsRemaining() const { return m_loopsRemaining.load(std::memory_order_relaxed); }
43
48
49 float m_volume{};
50 bool m_muted{};
51
54
56};
57
58// Design notes
59// * each play() will start a new voice
60// * stop() will stop all voices
61// * the most recently created voice can be obtained via `activeVoice()`
62// * mute/volume will affect all voices
63// * loopCount/loopRemaining will be obtained from most recently created voice
64class QSoundEffectPrivateWithPlayer final : public QObject, public QSoundEffectPrivate
65{
66public:
67 QSoundEffectPrivateWithPlayer(QSoundEffect *q, QAudioDevice audioDevice);
68 Q_DISABLE_COPY_MOVE(QSoundEffectPrivateWithPlayer)
69 ~QSoundEffectPrivateWithPlayer() override;
70
71 // QSoundEffectPrivate interface
72 bool setAudioDevice(QAudioDevice device) override;
73 QAudioDevice audioDevice() const override;
74 bool setSource(const QUrl &, QSampleCache &) override;
75 QUrl url() const override;
76 QSoundEffect::Status status() const override;
77 int loopCount() const override;
78 bool setLoopCount(int) override;
79 int loopsRemaining() const override;
80 float volume() const override;
81 bool setVolume(float) override;
82 bool muted() const override;
83 bool setMuted(bool) override;
84 void play() override;
85 void stop() override;
86 bool playing() const override;
87
88private:
89 void play(std::shared_ptr<QSoundEffectVoice>);
90 void setStatus(QSoundEffect::Status status);
91 [[nodiscard]] bool updatePlayer();
92 std::optional<VoiceId> activeVoice() const;
93 static bool formatIsSupported(const QAudioFormat &);
94 void setResolvedAudioDevice(QAudioDevice device);
95 void resolveAudioDevice();
96
97 QSoundEffect *q_ptr{};
98 QAudioDevice m_audioDevice;
99 QAudioDevice m_resolvedAudioDevice;
100 std::shared_ptr<QRtAudioEngine> m_player;
101 QMetaObject::Connection m_voiceFinishedConnection;
102 std::set<std::shared_ptr<QSoundEffectVoice>, QRtAudioEngineVoiceCompare> m_voices;
103
104 void setLoopsRemaining(int);
105 int m_loopsRemaining{ 0 };
106
107 QFuture<void> m_sampleLoadFuture;
108 QUrl m_url;
109 SharedSamplePtr m_sample;
110 float m_volume = 1.f;
111 bool m_muted = false;
112 int m_loopCount = 1;
113 bool m_playPending = false;
114
115 QSoundEffect::Status m_status{};
116
117 QMediaDevices m_mediaDevices;
118 QAudioDevice m_defaultAudioDevice;
119};
120
121} // namespace QtMultimediaPrivate
122
123QT_END_NAMESPACE
124
125#endif // QSOUNDEFFECTWITHPLAYER_P_H
bool setSource(const QUrl &, QSampleCache &) override
QSoundEffectPrivateWithPlayer(QSoundEffect *q, QAudioDevice audioDevice)
const std::shared_ptr< const QSample > m_sample
bool isActive() noexcept QT_MM_NONBLOCKING override
const QAudioFormat & format() noexcept override
QSoundEffectVoice(VoiceId voiceId, std::shared_ptr< const QSample > sample, float volume, bool muted, int totalLoopCount)
VoicePlayResult play(QSpan< float >) noexcept QT_MM_NONBLOCKING override
std::shared_ptr< QSoundEffectVoice > clone() const