Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qdrawavaudiodecoder_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-2.0-only OR GPL-3.0-only
3
4
#
ifndef
QDRAWAVAUDIODECODER_P_H
5
#
define
QDRAWAVAUDIODECODER_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
/
qplatformaudiodecoder_p
.
h
>
19
#
include
<
QtCore
/
qbytearray
.
h
>
20
#
include
<
QtCore
/
qspan
.
h
>
21
#
include
<
QtCore
/
qurl
.
h
>
22
#
include
<
QtCore
/
private
/
qexpected_p
.
h
>
23
24
#
include
<
algorithm
>
25
#
include
<
atomic
>
26
#
include
<
cstdint
>
27
#
include
<
memory
>
28
#
include
<
new
>
29
#
include
<
optional
>
30
#
include
<
type_traits
>
31
32
QT_BEGIN_NAMESPACE
33
34
class
QAudioDecoder;
35
class
QIODevice;
36
37
namespace
QtMultimediaPrivate
{
38
39
using
QDrWavDecodeResult
=
q23
::
expected
<
QAudioBuffer
,
std
::
pair
<
QAudioDecoder
::
Error
,
QString
>>;
40
QDrWavDecodeResult
loadWaveAndDecodeData
(
QSpan
<
const
std
::
byte
>,
41
const
QAudioFormat &requestedFormat);
42
43
#
ifdef
__cpp_concepts
44
template
<
typename
Reader
>
45
concept
FrameReader
=
std
::
is_invocable_v
<
Reader
,
uint64_t
,
char
*>
46
&&
std
::
is_convertible_v
<
std
::
invoke_result_t
<
Reader
,
uint64_t
,
char
*>,
uint64_t
>;
47
#
endif
48
49
#
ifdef
__cpp_concepts
50
template
<
FrameReader
Reader
>
51
#
else
52
template
<
typename
Reader>
53
#
endif
54
std
::
optional
<
QByteArray
>
readFramesInChunks
(uint64_t declaredFrameCount, size_t bytesPerFrame,
55
Reader &&readFrames)
56
{
57
constexpr
size_t chunkBytes = 10 * 1024 * 1024;
// 10 MB
58
if
(bytesPerFrame == 0)
59
return
std
::nullopt;
60
61
const
uint64_t chunkFrames =
std
::max<uint64_t>(1, chunkBytes / bytesPerFrame);
62
63
QT_TRY
64
{
65
QByteArray result;
66
uint64_t totalFramesRead = 0;
67
while
(totalFramesRead < declaredFrameCount) {
68
const
uint64_t framesToRead =
69
std::min(chunkFrames, declaredFrameCount - totalFramesRead);
70
const
uint64_t bytesToRead = framesToRead * bytesPerFrame;
71
const
qsizetype oldSize = result.size();
72
if
(bytesToRead > uint64_t(QByteArray::maxSize() - oldSize))
73
return
std::nullopt;
74
75
result.resizeForOverwrite(oldSize + qsizetype(bytesToRead));
76
const
uint64_t framesRead = readFrames(framesToRead, result.data() + oldSize);
77
Q_ASSERT(framesRead <= framesToRead);
78
result.resize(oldSize + qsizetype(framesRead * bytesPerFrame));
79
80
totalFramesRead += framesRead;
81
if
(framesRead < framesToRead)
82
break
;
83
}
84
if
(totalFramesRead != declaredFrameCount)
85
return
std::nullopt;
86
return
result;
87
}
88
QT_CATCH(
const
std::bad_alloc &)
89
{
90
return
std::nullopt;
91
}
92
}
93
94
}
// namespace QtMultimediaPrivate
95
96
class
QDrWavAudioDecoder
final
:
public
QPlatformAudioDecoder
97
{
98
public
:
99
explicit
QDrWavAudioDecoder
(QAudioDecoder *parent);
100
~
QDrWavAudioDecoder
()
override
;
101
102
// Source management
103
QUrl
source
()
const
override
{
return
m_source; }
104
void
setSource
(
const
QUrl &)
override
;
105
106
QIODevice *
sourceDevice
()
const
override
{
return
m_sourceDevice; }
107
void
setSourceDevice
(QIODevice *)
override
;
108
109
// Lifecycle
110
void
start
()
override
;
111
void
stop
()
override
;
112
113
// Format
114
QAudioFormat
audioFormat
()
const
override
{
return
m_audioFormat; }
115
void
setAudioFormat
(
const
QAudioFormat &)
override
;
116
117
// Buffer pull model
118
QAudioBuffer
read
()
override
;
119
120
// Metadata
121
bool
canReadQrc
()
const
override
{
return
true
; }
122
123
private
:
124
using
QDrWavDecodeResult = QtMultimediaPrivate::QDrWavDecodeResult;
125
126
QDrWavDecodeResult loadAndDecodeFile(
const
QUrl &, QIODevice *,
const
QAudioFormat &,
127
std
::shared_ptr<
std
::atomic_bool> decodingStopped);
128
129
// Handle decode completion
130
void
onDecodeFinished(QDrWavDecodeResult,
const
std
::atomic_bool &decodingStopped);
131
132
QUrl m_source;
133
QIODevice *m_sourceDevice =
nullptr
;
134
QAudioFormat m_audioFormat;
135
136
// Decoded result
137
QAudioBuffer m_buffer;
138
139
std
::shared_ptr<
std
::atomic_bool> m_decodingStopped;
140
};
141
142
QT_END_NAMESPACE
143
144
#
endif
// QDRAWAVAUDIODECODER_P_H
QDrWavAudioDecoder::canReadQrc
bool canReadQrc() const override
Definition
qdrawavaudiodecoder_p.h:121
QDrWavAudioDecoder::audioFormat
QAudioFormat audioFormat() const override
Definition
qdrawavaudiodecoder_p.h:114
QDrWavAudioDecoder::setSource
void setSource(const QUrl &) override
Definition
qdrawavaudiodecoder.cpp:182
QDrWavAudioDecoder::setAudioFormat
void setAudioFormat(const QAudioFormat &) override
Definition
qdrawavaudiodecoder.cpp:198
QDrWavAudioDecoder::read
QAudioBuffer read() override
Definition
qdrawavaudiodecoder.cpp:251
QDrWavAudioDecoder::~QDrWavAudioDecoder
~QDrWavAudioDecoder() override
Definition
qdrawavaudiodecoder.cpp:177
QDrWavAudioDecoder::sourceDevice
QIODevice * sourceDevice() const override
Definition
qdrawavaudiodecoder_p.h:106
QDrWavAudioDecoder::stop
void stop() override
Definition
qdrawavaudiodecoder.cpp:234
QDrWavAudioDecoder::source
QUrl source() const override
Definition
qdrawavaudiodecoder_p.h:103
QDrWavAudioDecoder::setSourceDevice
void setSourceDevice(QIODevice *) override
Definition
qdrawavaudiodecoder.cpp:190
QDrWavAudioDecoder::QDrWavAudioDecoder
QDrWavAudioDecoder(QAudioDecoder *parent)
Definition
qdrawavaudiodecoder.cpp:172
QDrWavAudioDecoder::start
void start() override
Definition
qdrawavaudiodecoder.cpp:206
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
QtMultimediaPrivate::impl
Definition
qaudiosystem_p.h:257
QtMultimediaPrivate::makeResourceError
static QDrWavDecodeResult makeResourceError(const QString &message)
Definition
qdrawavaudiodecoder.cpp:34
QtMultimediaPrivate::readFramesInChunks
std::optional< QByteArray > readFramesInChunks(uint64_t declaredFrameCount, size_t bytesPerFrame, Reader &&readFrames)
Definition
qdrawavaudiodecoder_p.h:54
QtMultimediaPrivate::makeFormatError
static QDrWavDecodeResult makeFormatError(const QString &message)
Definition
qdrawavaudiodecoder.cpp:29
QtMultimediaPrivate::loadWaveAndDecodeData
QDrWavDecodeResult loadWaveAndDecodeData(QSpan< const std::byte > data, const QAudioFormat &requestedFormat)
Definition
qdrawavaudiodecoder.cpp:39
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtmultimedia
src
multimedia
audio
qdrawavaudiodecoder_p.h
Generated on
for Qt by
1.18.0