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.cpp
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#include "qaudiobuffer.h"
5
6#include <QObject>
7#include <QDebug>
8
9QT_BEGIN_NAMESPACE
10
11class QAudioBufferPrivate : public QSharedData
12{
13public:
14 QAudioBufferPrivate(const QAudioFormat &f, const QByteArray &d, qint64 start)
15 : format(f), data(d), startTime(start)
16 {
17 }
18
19 QAudioFormat format;
20 QByteArray data;
21 qint64 startTime;
22};
23
25
26/*!
27 \class QAbstractAudioBuffer
28 \internal
29*/
30
31/*!
32 \class QAudioBuffer
33 \inmodule QtMultimedia
34 \ingroup multimedia
35 \ingroup multimedia_audio
36 \brief The QAudioBuffer class represents a collection of audio samples with a specific format
37 and sample rate.
38
39 QAudioBuffer is used by the QAudioDecoder class to hand decoded audio data over to the
40 application. An audio buffer contains data in a certain QAudioFormat that can be queried using
41 format(). It is also tagged with timing and duration information.
42
43 To access the data stored inside the buffer, use the data() or constData() methods.
44
45 Audio buffers are explicitly shared, in most cases, you should call detach() before
46 modifying the data.
47*/
48
49/*!
50 Create a new, empty, invalid buffer.
51 */
52QAudioBuffer::QAudioBuffer() noexcept = default;
53
54/*!
55 Creates a new audio buffer from \a other. Audio buffers are explicitly shared,
56 you should call detach() on the buffer to make a copy that can then be modified.
57 */
58QAudioBuffer::QAudioBuffer(const QAudioBuffer &other) noexcept = default;
59
60/*!
61 Creates a new audio buffer from the supplied \a data, in the
62 given \a format. The format will determine how the number
63 and sizes of the samples are interpreted from the \a data.
64
65 If the supplied \a data is not an integer multiple of the
66 calculated frame size, the excess data will not be used.
67
68 This audio buffer will copy the contents of \a data.
69
70 \a startTime (in microseconds) indicates when this buffer
71 starts in the stream.
72 If this buffer is not part of a stream, set it to -1.
73 */
74QAudioBuffer::QAudioBuffer(const QByteArray &data, const QAudioFormat &format, qint64 startTime)
75{
76 if (!format.isValid() || !data.size())
77 return;
78 d = new QAudioBufferPrivate(format, data, startTime);
79}
80
81/*!
82 Creates a new audio buffer with space for \a numFrames frames of
83 the given \a format. The individual samples will be initialized
84 to the default for the format.
85
86 \a startTime (in microseconds) indicates when this buffer
87 starts in the stream.
88 If this buffer is not part of a stream, set it to -1.
89 */
90QAudioBuffer::QAudioBuffer(int numFrames, const QAudioFormat &format, qint64 startTime)
91{
92 if (!format.isValid() || !numFrames)
93 return;
94
95 QByteArray data(format.bytesForFrames(numFrames), '\0');
96 d = new QAudioBufferPrivate(format, data, startTime);
97}
98
99/*!
100 \fn QAudioBuffer::QAudioBuffer(QAudioBuffer &&other)
101
102 Constructs a QAudioBuffer by moving from \a other.
103*/
104
105/*!
106 \fn void QAudioBuffer::swap(QAudioBuffer &other) noexcept
107
108 Swaps the audio buffer with \a other.
109*/
110
111/*!
112 \fn QAudioBuffer &QAudioBuffer::operator=(QAudioBuffer &&other)
113
114 Moves \a other into this QAudioBuffer.
115*/
116
117/*!
118 Assigns the \a other buffer to this.
119 */
120QAudioBuffer &QAudioBuffer::operator=(const QAudioBuffer &other) = default;
121
122/*!
123 Destroys this audio buffer.
124 */
125QAudioBuffer::~QAudioBuffer() = default;
126
127/*! \fn bool QAudioBuffer::isValid() const noexcept
128
129 Returns true if this is a valid buffer. A valid buffer
130 has more than zero frames in it and a valid format.
131 */
132
133/*!
134 Detaches this audio buffers from other copies that might share data with it.
135*/
136void QAudioBuffer::detach()
137{
138 if (!d)
139 return;
140 d = new QAudioBufferPrivate(*d);
141}
142
143/*!
144 Returns the \l {QAudioFormat}{format} of this buffer.
145
146 Several properties of this format influence how
147 the \l duration() or \l byteCount() are calculated
148 from the \l frameCount().
149 */
150QAudioFormat QAudioBuffer::format() const noexcept
151{
152 if (!d)
153 return QAudioFormat();
154 return d->format;
155}
156
157/*!
158 Returns the number of complete audio frames in this buffer.
159
160 An audio frame is an interleaved set of one sample per channel
161 for the same instant in time.
162*/
163qsizetype QAudioBuffer::frameCount() const noexcept
164{
165 if (!d)
166 return 0;
167 return d->format.framesForBytes(d->data.size());
168}
169
170/*!
171 Returns the number of samples in this buffer.
172
173 If the format of this buffer has multiple channels,
174 then this count includes all channels. This means
175 that a stereo buffer with 1000 samples in total will
176 have 500 left samples and 500 right samples (interleaved),
177 and this function will return 1000.
178
179 \sa frameCount()
180*/
181qsizetype QAudioBuffer::sampleCount() const noexcept
182{
183 return frameCount() * format().channelCount();
184}
185
186/*!
187 Returns the size of this buffer, in bytes.
188 */
189qsizetype QAudioBuffer::byteCount() const noexcept
190{
191 return d ? d->data.size() : 0;
192}
193
194/*!
195 Returns the duration of audio in this buffer, in microseconds.
196
197 This depends on the \l format(), and the \l frameCount().
198*/
199qint64 QAudioBuffer::duration() const noexcept
200{
201 return format().durationForFrames(frameCount());
202}
203
204/*!
205 Returns the time in a stream that this buffer starts at (in microseconds).
206
207 If this buffer is not part of a stream, this will return -1.
208 */
209qint64 QAudioBuffer::startTime() const noexcept
210{
211 if (!d)
212 return -1;
213 return d->startTime;
214}
215
216/*!
217 \fn template <typename T> const T* QAudioBuffer::constData() const
218
219 Returns a pointer to this buffer's data. You can only read it.
220
221 This method is preferred over the const version of \l data() to
222 prevent unnecessary copying.
223
224 Note that there is no checking done on the format of the audio
225 buffer - this is simply a convenience function.
226
227 \code
228 // With a 16bit sample buffer:
229 const quint16 *data = buffer->constData<quint16>();
230 \endcode
231
232*/
233
234const void *QAudioBuffer::constData() const noexcept
235{
236 if (!d)
237 return nullptr;
238 return d->data.constData();
239}
240
241/*!
242 \fn template <typename T> const T* QAudioBuffer::data() const
243
244 Returns a pointer to this buffer's data. You can only read it.
245
246 You should use the \l constData() function rather than this
247 to prevent accidental deep copying.
248
249 Note that there is no checking done on the format of
250 the audio buffer - this is simply a convenience function.
251
252 \code
253 // With a 16bit sample const buffer:
254 const quint16 *data = buffer->data<quint16>();
255 \endcode
256*/
257
258const void *QAudioBuffer::data() const noexcept
259{
260 if (!d)
261 return nullptr;
262 return d->data.constData();
263}
264
265/*!
266 \fn template <typename T> T* QAudioBuffer::data()
267
268 Returns a pointer to this buffer's data. You can modify the
269 data through the returned pointer.
270
271 Since QAudioBuffer objects are explicitly shared, you should usually
272 call detach() before modifying the data through this function.
273
274 Note that there is no checking done on the format of the audio
275 buffer - this is simply a convenience function.
276
277 \code
278 // With a 16bit sample buffer:
279 quint16 *data = buffer->data<quint16>(); // May cause deep copy
280 \endcode
281*/
282
283void *QAudioBuffer::data()
284{
285 if (!d)
286 return nullptr;
287 return d->data.data();
288}
289
290/*!
291 \typedef QAudioBuffer::S16S
292
293 This is a predefined specialization for a signed stereo 16 bit sample. Each
294 channel is a \e {signed short}.
295*/
296
297/*!
298 \typedef QAudioBuffer::U8M
299
300 This is a predefined specialization for an unsigned 8 bit mono sample.
301*/
302/*!
303 \typedef QAudioBuffer::S16M
304 This is a predefined specialization for a signed 16 bit mono sample.
305i*/
306/*!
307 \typedef QAudioBuffer::S32M
308 This is a predefined specialization for a signed 32 bit mono sample.
309*/
310/*!
311 \typedef QAudioBuffer::F32M
312 This is a predefined specialization for a 32 bit float mono sample.
313*/
314/*!
315 \typedef QAudioBuffer::U8S
316 This is a predifined specialization for an unsiged 8 bit stereo sample.
317*/
318/*!
319 \typedef QAudioBuffer::S32S
320 This is a predifined specialization for a siged 32 bit stereo sample.
321*/
322/*!
323 \typedef QAudioBuffer::F32S
324 This is a predifined specialization for a 32 bit float stereo sample.
325*/
326QT_END_NAMESPACE
QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QAudioBufferPrivate)