summaryrefslogtreecommitdiffstats
path: root/src/multimedia/audio/qaudiobuffer.cpp
blob: 69adcc5b719706f47aeb6e4b8d22fc99abfa48b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qaudiobuffer.h"

#include <QObject>
#include <QDebug>

QT_BEGIN_NAMESPACE

class QAudioBufferPrivate : public QSharedData
{
public:
    QAudioBufferPrivate(const QAudioFormat &f, const QByteArray &d, qint64 start)
        : format(f), data(d), startTime(start)
    {
    }

    QAudioFormat format;
    QByteArray data;
    qint64 startTime;
};

QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QAudioBufferPrivate);

/*!
    \class QAbstractAudioBuffer
    \internal
*/

/*!
    \class QAudioBuffer
    \inmodule QtMultimedia
    \ingroup multimedia
    \ingroup multimedia_audio
    \brief The QAudioBuffer class represents a collection of audio samples with a specific format
   and sample rate.

    QAudioBuffer is used by the QAudioDecoder class to hand decoded audio data over to the
   application. An audio buffer contains data in a certain QAudioFormat that can be queried using
   format(). It is also tagged with timing and duration information.

    To access the data stored inside the buffer, use the data() or constData() methods.

    Audio buffers are explicitly shared, in most cases, you should call detach() before
    modifying the data.
*/

/*!
    Create a new, empty, invalid buffer.
 */
QAudioBuffer::QAudioBuffer() noexcept = default;

/*!
    Creates a new audio buffer from \a other. Audio buffers are explicitly shared,
    you should call detach() on the buffer to make a copy that can then be modified.
 */
QAudioBuffer::QAudioBuffer(const QAudioBuffer &other) noexcept = default;

/*!
    Creates a new audio buffer from the supplied \a data, in the
    given \a format.  The format will determine how the number
    and sizes of the samples are interpreted from the \a data.

    If the supplied \a data is not an integer multiple of the
    calculated frame size, the excess data will not be used.

    This audio buffer will copy the contents of \a data.

    \a startTime (in microseconds) indicates when this buffer
    starts in the stream.
    If this buffer is not part of a stream, set it to -1.
 */
QAudioBuffer::QAudioBuffer(const QByteArray &data, const QAudioFormat &format, qint64 startTime)
{
    if (!format.isValid() || !data.size())
        return;
    d = new QAudioBufferPrivate(format, data, startTime);
}

/*!
    Creates a new audio buffer with space for \a numFrames frames of
    the given \a format.  The individual samples will be initialized
    to the default for the format.

    \a startTime (in microseconds) indicates when this buffer
    starts in the stream.
    If this buffer is not part of a stream, set it to -1.
 */
QAudioBuffer::QAudioBuffer(int numFrames, const QAudioFormat &format, qint64 startTime)
{
    if (!format.isValid() || !numFrames)
        return;

    QByteArray data(format.bytesForFrames(numFrames), '\0');
    d = new QAudioBufferPrivate(format, data, startTime);
}

/*!
    \fn QAudioBuffer::QAudioBuffer(QAudioBuffer &&other)

    Constructs a QAudioBuffer by moving from \a other.
*/

/*!
   \fn void QAudioBuffer::swap(QAudioBuffer &other) noexcept

   Swaps the audio buffer with \a other.
*/

/*!
    \fn QAudioBuffer &QAudioBuffer::operator=(QAudioBuffer &&other)

    Moves \a other into this QAudioBuffer.
*/

/*!
    Assigns the \a other buffer to this.
 */
QAudioBuffer &QAudioBuffer::operator=(const QAudioBuffer &other) = default;

/*!
    Destroys this audio buffer.
 */
QAudioBuffer::~QAudioBuffer() = default;

/*! \fn bool QAudioBuffer::isValid() const noexcept

    Returns true if this is a valid buffer.  A valid buffer
    has more than zero frames in it and a valid format.
 */

/*!
    Detaches this audio buffers from other copies that might share data with it.
*/
void QAudioBuffer::detach()
{
    if (!d)
        return;
    d = new QAudioBufferPrivate(*d);
}

/*!
    Returns the \l {QAudioFormat}{format} of this buffer.

    Several properties of this format influence how
    the \l duration() or \l byteCount() are calculated
    from the \l frameCount().
 */
QAudioFormat QAudioBuffer::format() const noexcept
{
    if (!d)
        return QAudioFormat();
    return d->format;
}

/*!
    Returns the number of complete audio frames in this buffer.

    An audio frame is an interleaved set of one sample per channel
    for the same instant in time.
*/
qsizetype QAudioBuffer::frameCount() const noexcept
{
    if (!d)
        return 0;
    return d->format.framesForBytes(d->data.size());
}

/*!
    Returns the number of samples in this buffer.

    If the format of this buffer has multiple channels,
    then this count includes all channels.  This means
    that a stereo buffer with 1000 samples in total will
    have 500 left samples and 500 right samples (interleaved),
    and this function will return 1000.

    \sa frameCount()
*/
qsizetype QAudioBuffer::sampleCount() const noexcept
{
    return frameCount() * format().channelCount();
}

/*!
    Returns the size of this buffer, in bytes.
 */
qsizetype QAudioBuffer::byteCount() const noexcept
{
    return d ? d->data.size() : 0;
}

/*!
    Returns the duration of audio in this buffer, in microseconds.

    This depends on the \l format(), and the \l frameCount().
*/
qint64 QAudioBuffer::duration() const noexcept
{
    return format().durationForFrames(frameCount());
}

/*!
    Returns the time in a stream that this buffer starts at (in microseconds).

    If this buffer is not part of a stream, this will return -1.
 */
qint64 QAudioBuffer::startTime() const noexcept
{
    if (!d)
        return -1;
    return d->startTime;
}

/*!
    \fn template <typename T> const T* QAudioBuffer::constData() const

    Returns a pointer to this buffer's data.  You can only read it.

    This method is preferred over the const version of \l data() to
    prevent unnecessary copying.

    Note that there is no checking done on the format of the audio
    buffer - this is simply a convenience function.

    \code
    // With a 16bit sample buffer:
    const quint16 *data = buffer->constData<quint16>();
    \endcode

*/

const void *QAudioBuffer::constData() const noexcept
{
    if (!d)
        return nullptr;
    return d->data.constData();
}

/*!
    \fn template <typename T> const T* QAudioBuffer::data() const

    Returns a pointer to this buffer's data.  You can only read it.

    You should use the \l constData() function rather than this
    to prevent accidental deep copying.

    Note that there is no checking done on the format of
    the audio buffer - this is simply a convenience function.

    \code
    // With a 16bit sample const buffer:
    const quint16 *data = buffer->data<quint16>();
    \endcode
*/

const void *QAudioBuffer::data() const noexcept
{
    if (!d)
        return nullptr;
    return d->data.constData();
}

/*!
    \fn template <typename T> T* QAudioBuffer::data()

    Returns a pointer to this buffer's data.  You can modify the
    data through the returned pointer.

    Since QAudioBuffer objects are explicitly shared, you should usually
    call detach() before modifying the data through this function.

    Note that there is no checking done on the format of the audio
    buffer - this is simply a convenience function.

    \code
    // With a 16bit sample buffer:
    quint16 *data = buffer->data<quint16>(); // May cause deep copy
    \endcode
*/

void *QAudioBuffer::data()
{
    if (!d)
        return nullptr;
    return d->data.data();
}

/*!
    \typedef QAudioBuffer::S16S

    This is a predefined specialization for a signed stereo 16 bit sample.  Each
    channel is a \e {signed short}.
*/

/*!
    \typedef QAudioBuffer::U8M

    This is a predefined specialization for an unsigned 8 bit mono sample.
*/
/*!
    \typedef QAudioBuffer::S16M
    This is a predefined specialization for a signed 16 bit mono sample.
i*/
/*!
    \typedef QAudioBuffer::S32M
    This is a predefined specialization for a signed 32 bit mono sample.
*/
/*!
    \typedef QAudioBuffer::F32M
    This is a predefined specialization for a 32 bit float mono sample.
*/
/*!
    \typedef QAudioBuffer::U8S
    This is a predifined specialization for an unsiged 8 bit stereo sample.
*/
/*!
    \typedef QAudioBuffer::S32S
    This is a predifined specialization for a siged 32 bit stereo sample.
*/
/*!
    \typedef QAudioBuffer::F32S
    This is a predifined specialization for a 32 bit float stereo sample.
*/
QT_END_NAMESPACE