1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QAUDIOBUFFER_H
41#define QAUDIOBUFFER_H
42
43#include <QtCore/qshareddata.h>
44
45#include <QtMultimedia/qtmultimediaglobal.h>
46#include <QtMultimedia/qmultimedia.h>
47
48#include <QtMultimedia/qaudio.h>
49#include <QtMultimedia/qaudioformat.h>
50
51QT_BEGIN_NAMESPACE
52
53class QAbstractAudioBuffer;
54class QAudioBufferPrivate;
55class Q_MULTIMEDIA_EXPORT QAudioBuffer
56{
57public:
58 QAudioBuffer();
59 QAudioBuffer(QAbstractAudioBuffer *provider);
60 QAudioBuffer(const QAudioBuffer &other);
61 QAudioBuffer(const QByteArray &data, const QAudioFormat &format, qint64 startTime = -1);
62 QAudioBuffer(int numFrames, const QAudioFormat &format, qint64 startTime = -1); // Initialized to empty
63
64 QAudioBuffer& operator=(const QAudioBuffer &other);
65
66 ~QAudioBuffer();
67
68 bool isValid() const;
69
70 QAudioFormat format() const;
71
72 int frameCount() const;
73 int sampleCount() const;
74 int byteCount() const;
75
76 qint64 duration() const;
77 qint64 startTime() const;
78
79 // Data modification
80 // void clear();
81 // Other ideas
82 // operator *=
83 // operator += (need to be careful about different formats)
84
85 // Data access
86 const void* constData() const; // Does not detach, preferred
87 const void* data() const; // Does not detach
88 void *data(); // detaches
89
90 // Structures for easier access to stereo data
91 template <typename T> struct StereoFrameDefault { enum { Default = 0 }; };
92
93 template <typename T> struct StereoFrame {
94
95 StereoFrame()
96 : left(T(StereoFrameDefault<T>::Default))
97 , right(T(StereoFrameDefault<T>::Default))
98 {
99 }
100
101 StereoFrame(T leftSample, T rightSample)
102 : left(leftSample)
103 , right(rightSample)
104 {
105 }
106
107 StereoFrame& operator=(const StereoFrame &other)
108 {
109 // Two straight assigns is probably
110 // cheaper than a conditional check on
111 // self assignment
112 left = other.left;
113 right = other.right;
114 return *this;
115 }
116
117 T left;
118 T right;
119
120 T average() const {return (left + right) / 2;}
121 void clear() {left = right = T(StereoFrameDefault<T>::Default);}
122 };
123
124 typedef StereoFrame<unsigned char> S8U;
125 typedef StereoFrame<signed char> S8S;
126 typedef StereoFrame<unsigned short> S16U;
127 typedef StereoFrame<signed short> S16S;
128 typedef StereoFrame<float> S32F;
129
130 template <typename T> const T* constData() const {
131 return static_cast<const T*>(constData());
132 }
133 template <typename T> const T* data() const {
134 return static_cast<const T*>(data());
135 }
136 template <typename T> T* data() {
137 return static_cast<T*>(data());
138 }
139private:
140 QAudioBufferPrivate *d;
141};
142
143template <> struct QAudioBuffer::StereoFrameDefault<unsigned char> { enum { Default = 128 }; };
144template <> struct QAudioBuffer::StereoFrameDefault<unsigned short> { enum { Default = 32768 }; };
145
146
147QT_END_NAMESPACE
148
149Q_DECLARE_METATYPE(QAudioBuffer)
150
151#endif // QAUDIOBUFFER_H
152

source code of qtmultimedia/src/multimedia/audio/qaudiobuffer.h