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#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 QSoundEffectPrivateWithPlayer(QSoundEffect *q, QAudioDevice audioDevice);
78 Q_DISABLE_COPY_MOVE(QSoundEffectPrivateWithPlayer)
79 ~QSoundEffectPrivateWithPlayer() override;
80
81 // QSoundEffectPrivate interface
82 bool setAudioDevice(QAudioDevice device) override;
83 QAudioDevice audioDevice() const override;
84 bool setSource(const QUrl &, QSampleCache &) override;
85 QUrl url() const override;
86 QSoundEffect::Status status() const override;
87 int loopCount() const override;
88 bool setLoopCount(int) override;
89 int loopsRemaining() const override;
90 float volume() const override;
91 bool setVolume(float) override;
92 bool muted() const override;
93 bool setMuted(bool) override;
94 void play() override;
95 void stop() override;
96 bool playing() const override;
97
98private:
99 void play(std::shared_ptr<QSoundEffectVoice>);
100 void setStatus(QSoundEffect::Status status);
101 [[nodiscard]] bool updatePlayer(const SharedSamplePtr &sample);
102 std::optional<VoiceId> activeVoice() const;
103 static bool formatIsSupported(const QAudioFormat &);
104 void setResolvedAudioDevice(QAudioDevice device);
105 void resolveAudioDevice();
106
107 QSoundEffect *q_ptr{};
108 QAudioDevice m_audioDevice;
109 QAudioDevice m_resolvedAudioDevice;
110 std::shared_ptr<QRtAudioEngine> m_player;
111 QMetaObject::Connection m_voiceFinishedConnection;
112 std::set<std::shared_ptr<QSoundEffectVoice>, QRtAudioEngineVoiceCompare> m_voices;
113
114 void setLoopsRemaining(int);
115 int m_loopsRemaining{ 0 };
116
117 std::optional<QFuture<void>> m_sampleLoadFuture;
118 QUrl m_url;
119 SharedSamplePtr m_sample;
120 float m_volume = 1.f;
121 bool m_muted = false;
122 int m_loopCount = 1;
123 bool m_playPending = false;
124
125 QSoundEffect::Status m_status{};
126
127 QMediaDevices m_mediaDevices;
128 QAudioDevice m_defaultAudioDevice;
129 QChronoTimer m_playerReleaseTimer{
130 std::chrono::seconds(2),
131 };
132};
133
134} // namespace QtMultimediaPrivate
135
136QT_END_NAMESPACE
137
138#endif // QSOUNDEFFECTWITHPLAYER_P_H
bool setSource(const QUrl &, QSampleCache &) override
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