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
5#ifndef QAUDIOFORMAT_H
6#define QAUDIOFORMAT_H
7
8#include <QtCore/qobject.h>
9#include <QtCore/qshareddata.h>
10
11#include <QtMultimedia/qtmultimediaglobal.h>
12
13QT_BEGIN_NAMESPACE
14
15class QAudioFormatPrivate;
16
17namespace QtPrivate {
18template <typename... Args>
19constexpr int channelConfig(Args... values) {
20 return (0 | ... | (1u << values));
21}
22}
23
24class QAudioFormat
25{
26public:
27 enum SampleFormat : quint16 {
28 Unknown,
29 UInt8,
30 Int16,
31 Int32,
32 Float,
33 NSampleFormats
34 };
35
36 // This matches the speaker positions of a 22.2 audio layout. Stereo, Surround 5.1 and Surround 7.1 are subsets of these
37 enum AudioChannelPosition {
38 UnknownPosition,
39 FrontLeft,
40 FrontRight,
41 FrontCenter,
42 LFE,
43 BackLeft,
44 BackRight,
45 FrontLeftOfCenter,
46 FrontRightOfCenter,
47 BackCenter,
48 SideLeft,
49 SideRight,
50 TopCenter,
51 TopFrontLeft,
52 TopFrontCenter,
53 TopFrontRight,
54 TopBackLeft,
55 TopBackCenter,
56 TopBackRight,
57 LFE2,
58 TopSideLeft,
59 TopSideRight,
60 BottomFrontCenter,
61 BottomFrontLeft,
62 BottomFrontRight
63 };
64 static constexpr int NChannelPositions = BottomFrontRight + 1;
65
66 enum ChannelConfig : quint32 {
67 ChannelConfigUnknown = 0,
68 ChannelConfigMono = QtPrivate::channelConfig(values: FrontCenter),
69 ChannelConfigStereo = QtPrivate::channelConfig(values: FrontLeft, values: FrontRight),
70 ChannelConfig2Dot1 = QtPrivate::channelConfig(values: FrontLeft, values: FrontRight, values: LFE),
71 ChannelConfig3Dot0 = QtPrivate::channelConfig(values: FrontLeft, values: FrontRight, values: FrontCenter),
72 ChannelConfig3Dot1 = QtPrivate::channelConfig(values: FrontLeft, values: FrontRight, values: FrontCenter, values: LFE),
73 ChannelConfigSurround5Dot0 = QtPrivate::channelConfig(values: FrontLeft, values: FrontRight, values: FrontCenter, values: BackLeft, values: BackRight),
74 ChannelConfigSurround5Dot1 = QtPrivate::channelConfig(values: FrontLeft, values: FrontRight, values: FrontCenter, values: LFE, values: BackLeft, values: BackRight),
75 ChannelConfigSurround7Dot0 = QtPrivate::channelConfig(values: FrontLeft, values: FrontRight, values: FrontCenter, values: BackLeft, values: BackRight, values: SideLeft, values: SideRight),
76 ChannelConfigSurround7Dot1 = QtPrivate::channelConfig(values: FrontLeft, values: FrontRight, values: FrontCenter, values: LFE, values: BackLeft, values: BackRight, values: SideLeft, values: SideRight),
77 };
78
79 template <typename... Args>
80 static constexpr ChannelConfig channelConfig(Args... channels)
81 {
82 return ChannelConfig(QtPrivate::channelConfig(channels...));
83 }
84
85 constexpr bool isValid() const noexcept
86 {
87 return m_sampleRate > 0 && m_channelCount > 0 && m_sampleFormat != Unknown;
88 }
89
90 constexpr void setSampleRate(int sampleRate) noexcept { m_sampleRate = sampleRate; }
91 constexpr int sampleRate() const noexcept { return m_sampleRate; }
92
93 Q_MULTIMEDIA_EXPORT void setChannelConfig(ChannelConfig config) noexcept;
94 constexpr ChannelConfig channelConfig() const noexcept { return m_channelConfig; }
95
96 constexpr void setChannelCount(int channelCount) noexcept { m_channelConfig = ChannelConfigUnknown; m_channelCount = channelCount; }
97 constexpr int channelCount() const noexcept { return m_channelCount; }
98
99 Q_MULTIMEDIA_EXPORT int channelOffset(AudioChannelPosition channel) const noexcept;
100
101 constexpr void setSampleFormat(SampleFormat f) noexcept { m_sampleFormat = f; }
102 constexpr SampleFormat sampleFormat() const noexcept { return m_sampleFormat; }
103
104 // Helper functions
105 Q_MULTIMEDIA_EXPORT qint32 bytesForDuration(qint64 microseconds) const;
106 Q_MULTIMEDIA_EXPORT qint64 durationForBytes(qint32 byteCount) const;
107
108 Q_MULTIMEDIA_EXPORT qint32 bytesForFrames(qint32 frameCount) const;
109 Q_MULTIMEDIA_EXPORT qint32 framesForBytes(qint32 byteCount) const;
110
111 Q_MULTIMEDIA_EXPORT qint32 framesForDuration(qint64 microseconds) const;
112 Q_MULTIMEDIA_EXPORT qint64 durationForFrames(qint32 frameCount) const;
113
114 constexpr int bytesPerFrame() const { return bytesPerSample()*channelCount(); }
115 constexpr int bytesPerSample() const noexcept
116 {
117 switch (m_sampleFormat) {
118 case Unknown:
119 case NSampleFormats: return 0;
120 case UInt8: return 1;
121 case Int16: return 2;
122 case Int32:
123 case Float: return 4;
124 }
125 return 0;
126 }
127
128 Q_MULTIMEDIA_EXPORT float normalizedSampleValue(const void *sample) const;
129
130 friend bool operator==(const QAudioFormat &a, const QAudioFormat &b)
131 {
132 return a.m_sampleRate == b.m_sampleRate &&
133 a.m_channelCount == b.m_channelCount &&
134 a.m_sampleFormat == b.m_sampleFormat;
135 }
136 friend bool operator!=(const QAudioFormat &a, const QAudioFormat &b)
137 {
138 return !(a == b);
139 }
140
141 static Q_MULTIMEDIA_EXPORT ChannelConfig defaultChannelConfigForChannelCount(int channelCount);
142
143private:
144 SampleFormat m_sampleFormat = SampleFormat::Unknown;
145 short m_channelCount = 0;
146 ChannelConfig m_channelConfig = ChannelConfigUnknown;
147 int m_sampleRate = 0;
148 quint64 reserved = 0;
149};
150
151#ifndef QT_NO_DEBUG_STREAM
152Q_MULTIMEDIA_EXPORT QDebug operator<<(QDebug, const QAudioFormat &);
153Q_MULTIMEDIA_EXPORT QDebug operator<<(QDebug, QAudioFormat::SampleFormat);
154#endif
155
156QT_END_NAMESPACE
157
158Q_DECLARE_METATYPE(QAudioFormat)
159
160#endif // QAUDIOFORMAT_H
161

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