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
qaudiobuffer.h
Go to the documentation of this file.
1// Copyright (C) 2016 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 QAUDIOBUFFER_H
5#define QAUDIOBUFFER_H
6
7#include <QtMultimedia/qaudioformat.h>
8#include <QtMultimedia/qtaudio.h>
9#include <QtMultimedia/qtmultimediaglobal.h>
10#include <QtCore/qshareddata.h>
11
12QT_BEGIN_NAMESPACE
13
14namespace QtPrivate {
18
20{
21 using value_type = unsigned char;
22 static constexpr value_type Default = 128;
23};
24
26{
27 using value_type = short;
28 static constexpr value_type Default = 0;
29};
30
32{
33 using value_type = int;
34 static constexpr value_type Default = 0;
35};
36
38{
39 using value_type = float;
40 static constexpr value_type Default = 0.;
41};
42
43}
44
45template <QAudioFormat::ChannelConfig config, QAudioFormat::SampleFormat format>
47{
48private:
49 // popcount in qalgorithms.h is unfortunately not constexpr on MSVC.
50 // Use this here as a fallback
51 static constexpr int constexprPopcount(quint32 i)
52 {
53 i = i - ((i >> 1) & 0x55555555); // add pairs of bits
54 i = (i & 0x33333333) + ((i >> 2) & 0x33333333); // quads
55 i = (i + (i >> 4)) & 0x0F0F0F0F; // groups of 8
56 return (i * 0x01010101) >> 24; // horizontal sum of bytes
57 }
58 static constexpr int nChannels = constexprPopcount(config);
59public:
61 value_type channels[nChannels];
62 static constexpr int positionToIndex(QAudioFormat::AudioChannelPosition pos)
63 {
64 if (!(config & (1u << pos)))
65 return -1;
66
67 uint maskedChannels = config & ((1u << pos) - 1);
68 return qPopulationCount(maskedChannels);
69 }
70
71
72 value_type value(QAudioFormat::AudioChannelPosition pos) const {
73 int idx = positionToIndex(pos);
74 if (idx < 0)
75 return QtPrivate::AudioSampleFormatHelper<format>::Default;
76 return channels[idx];
77 }
78 void setValue(QAudioFormat::AudioChannelPosition pos, value_type val) {
79 int idx = positionToIndex(pos);
80 if (idx < 0)
81 return;
82 channels[idx] = val;
83 }
84 value_type operator[](QAudioFormat::AudioChannelPosition pos) const {
85 return value(pos);
86 }
87 constexpr void clear() {
88 for (int i = 0; i < nChannels; ++i)
89 channels[i] = QtPrivate::AudioSampleFormatHelper<format>::Default;
90 }
91};
92
93template <QAudioFormat::SampleFormat Format>
94using QAudioFrameMono = QAudioFrame<QAudioFormat::ChannelConfigMono, Format>;
95
96template <QAudioFormat::SampleFormat Format>
97using QAudioFrameStereo = QAudioFrame<QAudioFormat::ChannelConfigStereo, Format>;
98
99template <QAudioFormat::SampleFormat Format>
100using QAudioFrame2Dot1 = QAudioFrame<QAudioFormat::ChannelConfig2Dot1, Format>;
101
102template <QAudioFormat::SampleFormat Format>
103using QAudioFrameSurround5Dot1 = QAudioFrame<QAudioFormat::ChannelConfigSurround5Dot1, Format>;
104
105template <QAudioFormat::SampleFormat Format>
106using QAudioFrameSurround7Dot1 = QAudioFrame<QAudioFormat::ChannelConfigSurround7Dot1, Format>;
107
108
109class QAudioBufferPrivate;
110QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QAudioBufferPrivate, Q_MULTIMEDIA_EXPORT)
111
112class Q_MULTIMEDIA_EXPORT QAudioBuffer
113{
114public:
115 QAudioBuffer() noexcept;
116 QAudioBuffer(const QAudioBuffer &other) noexcept;
117 QAudioBuffer(const QByteArray &data, const QAudioFormat &format, qint64 startTime = -1);
118 QAudioBuffer(int numFrames, const QAudioFormat &format, qint64 startTime = -1); // Initialized to empty
119 ~QAudioBuffer();
120
121 QAudioBuffer& operator=(const QAudioBuffer &other);
122
123 QAudioBuffer(QAudioBuffer &&other) noexcept = default;
124 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QAudioBuffer)
125 void swap(QAudioBuffer &other) noexcept
126 { d.swap(other.d); }
127
128 bool isValid() const noexcept { return d != nullptr; };
129
130 void detach();
131
132 QAudioFormat format() const noexcept;
133
134 qsizetype frameCount() const noexcept;
135 qsizetype sampleCount() const noexcept;
136 qsizetype byteCount() const noexcept;
137
138 qint64 duration() const noexcept;
139 qint64 startTime() const noexcept;
140
141 // Structures for easier access to data
142 typedef QAudioFrameMono<QAudioFormat::UInt8> U8M;
143 typedef QAudioFrameMono<QAudioFormat::Int16> S16M;
144 typedef QAudioFrameMono<QAudioFormat::Int32> S32M;
145 typedef QAudioFrameMono<QAudioFormat::Float> F32M;
146
147 typedef QAudioFrameStereo<QAudioFormat::UInt8> U8S;
148 typedef QAudioFrameStereo<QAudioFormat::Int16> S16S;
149 typedef QAudioFrameStereo<QAudioFormat::Int32> S32S;
150 typedef QAudioFrameStereo<QAudioFormat::Float> F32S;
151
152 template <typename T> const T* constData() const {
153 return static_cast<const T*>(constData());
154 }
155 template <typename T> const T* data() const {
156 return static_cast<const T*>(data());
157 }
158 template <typename T> T* data() {
159 return static_cast<T*>(data());
160 }
161private:
162 const void* constData() const noexcept;
163 const void* data() const noexcept;
164 void *data();
165
166 QExplicitlySharedDataPointer<QAudioBufferPrivate> d;
167};
168
169QT_END_NAMESPACE
170
171Q_DECLARE_METATYPE(QAudioBuffer)
172
173#endif // QAUDIOBUFFER_H
\inmodule QtMultimedia
QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QAudioBufferPrivate)
static constexpr int positionToIndex(QAudioFormat::AudioChannelPosition pos)
void setValue(QAudioFormat::AudioChannelPosition pos, value_type val)
value_type channels[nChannels]
constexpr void clear()
value_type value(QAudioFormat::AudioChannelPosition pos) const
value_type operator[](QAudioFormat::AudioChannelPosition pos) const
typename QtPrivate::AudioSampleFormatHelper< format >::value_type value_type