HephAudio v3.0.6
A cross-platform C++ library for recording, playing, and processing audio on Windows, Android, Linux, iOS, and macOS.
Loading...
Searching...
No Matches
AudioChannelLayout.h
Go to the documentation of this file.
1#pragma once
2#include "HephAudioShared.h"
3#include <cstdint>
4#include <vector>
5
8#define HEPHAUDIO_CH_MASK_MONO (HephAudio::AudioChannelMask::FrontCenter)
9#define HEPHAUDIO_CH_MASK_STEREO (HephAudio::AudioChannelMask::FrontLeft | HephAudio::AudioChannelMask::FrontRight)
10#define HEPHAUDIO_CH_MASK_2_POINT_1 (HEPHAUDIO_CH_MASK_STEREO | HephAudio::AudioChannelMask::LowFrequency)
11#define HEPHAUDIO_CH_MASK_2_1 (HEPHAUDIO_CH_MASK_STEREO | HephAudio::AudioChannelMask::BackCenter)
12#define HEPHAUDIO_CH_MASK_SURROUND (HEPHAUDIO_CH_MASK_STEREO | HephAudio::AudioChannelMask::FrontCenter)
13#define HEPHAUDIO_CH_MASK_3_POINT_1 (HEPHAUDIO_CH_MASK_SURROUND | HephAudio::AudioChannelMask::LowFrequency)
14#define HEPHAUDIO_CH_MASK_4_POINT_0 (HEPHAUDIO_CH_MASK_SURROUND | HephAudio::AudioChannelMask::BackCenter)
15#define HEPHAUDIO_CH_MASK_2_2 (HEPHAUDIO_CH_MASK_STEREO | HephAudio::AudioChannelMask::SideLeft | HephAudio::AudioChannelMask::SideRight)
16#define HEPHAUDIO_CH_MASK_QUAD (HEPHAUDIO_CH_MASK_STEREO | HephAudio::AudioChannelMask::BackLeft | HephAudio::AudioChannelMask::BackRight)
17#define HEPHAUDIO_CH_MASK_4_POINT_1 (HEPHAUDIO_CH_MASK_4_POINT_0 | HephAudio::AudioChannelMask::LowFrequency)
18#define HEPHAUDIO_CH_MASK_5_POINT_0 (HEPHAUDIO_CH_MASK_SURROUND | HephAudio::AudioChannelMask::SideLeft | HephAudio::AudioChannelMask::SideRight)
19#define HEPHAUDIO_CH_MASK_5_POINT_0_BACK (HEPHAUDIO_CH_MASK_SURROUND | HephAudio::AudioChannelMask::BackLeft | HephAudio::AudioChannelMask::BackRight)
20#define HEPHAUDIO_CH_MASK_5_POINT_1 (HEPHAUDIO_CH_MASK_5_POINT_0 | HephAudio::AudioChannelMask::LowFrequency)
21#define HEPHAUDIO_CH_MASK_5_POINT_1_BACK (HEPHAUDIO_CH_MASK_5_POINT_0_BACK | HephAudio::AudioChannelMask::LowFrequency)
22#define HEPHAUDIO_CH_MASK_6_POINT_0 (HEPHAUDIO_CH_MASK_5_POINT_0 | HephAudio::AudioChannelMask::BackCenter)
23#define HEPHAUDIO_CH_MASK_6_POINT_0_FRONT (HEPHAUDIO_CH_MASK_2_2 | HephAudio::AudioChannelMask::FrontLeftOfCenter | HephAudio::AudioChannelMask::FrontRightOfCenter)
24#define HEPHAUDIO_CH_MASK_HEXAGONAL (HEPHAUDIO_CH_MASK_5_POINT_0_BACK | HephAudio::AudioChannelMask::BackCenter)
25#define HEPHAUDIO_CH_MASK_6_POINT_1 (HEPHAUDIO_CH_MASK_5_POINT_1 | HephAudio::AudioChannelMask::BackCenter)
26#define HEPHAUDIO_CH_MASK_6_POINT_1_BACK (HEPHAUDIO_CH_MASK_5_POINT_1_BACK | HephAudio::AudioChannelMask::BackCenter)
27#define HEPHAUDIO_CH_MASK_6_POINT_1_FRONT (HEPHAUDIO_CH_MASK_6_POINT_0_FRONT | HephAudio::AudioChannelMask::LowFrequency)
28#define HEPHAUDIO_CH_MASK_7_POINT_0 (HEPHAUDIO_CH_MASK_5_POINT_0 | HephAudio::AudioChannelMask::BackLeft | HephAudio::AudioChannelMask::BackRight)
29#define HEPHAUDIO_CH_MASK_7_POINT_0_FRONT (HEPHAUDIO_CH_MASK_5_POINT_0 | HephAudio::AudioChannelMask::FrontLeftOfCenter | HephAudio::AudioChannelMask::FrontRightOfCenter)
30#define HEPHAUDIO_CH_MASK_7_POINT_1 (HEPHAUDIO_CH_MASK_5_POINT_1 | HephAudio::AudioChannelMask::BackLeft | HephAudio::AudioChannelMask::BackRight)
31#define HEPHAUDIO_CH_MASK_7_POINT_1_WIDE (HEPHAUDIO_CH_MASK_5_POINT_1 | HephAudio::AudioChannelMask::FrontLeftOfCenter | HephAudio::AudioChannelMask::FrontRightOfCenter)
32#define HEPHAUDIO_CH_MASK_7_POINT_1_WIDE_BACK (HEPHAUDIO_CH_MASK_5_POINT_1_BACK | HephAudio::AudioChannelMask::FrontLeftOfCenter | HephAudio::AudioChannelMask::FrontRightOfCenter)
33#define HEPHAUDIO_CH_MASK_OCTAGONAL (HEPHAUDIO_CH_MASK_5_POINT_0 | HephAudio::AudioChannelMask::BackLeft | HephAudio::AudioChannelMask::BackCenter | HephAudio::AudioChannelMask::BackRight)
34
35#define HEPHAUDIO_CH_LAYOUT_MONO HephAudio::AudioChannelLayout(1, HEPHAUDIO_CH_MASK_MONO)
36#define HEPHAUDIO_CH_LAYOUT_STEREO HephAudio::AudioChannelLayout(2, HEPHAUDIO_CH_MASK_STEREO)
37#define HEPHAUDIO_CH_LAYOUT_2_POINT_1 HephAudio::AudioChannelLayout(3, HEPHAUDIO_CH_MASK_2_POINT_1)
38#define HEPHAUDIO_CH_LAYOUT_2_1 HephAudio::AudioChannelLayout(3, HEPHAUDIO_CH_MASK_2_1)
39#define HEPHAUDIO_CH_LAYOUT_SURROUND HephAudio::AudioChannelLayout(3, HEPHAUDIO_CH_MASK_SURROUND)
40#define HEPHAUDIO_CH_LAYOUT_3_POINT_1 HephAudio::AudioChannelLayout(4, HEPHAUDIO_CH_MASK_3_POINT_1)
41#define HEPHAUDIO_CH_LAYOUT_4_POINT_0 HephAudio::AudioChannelLayout(4, HEPHAUDIO_CH_MASK_4_POINT_0)
42#define HEPHAUDIO_CH_LAYOUT_2_2 HephAudio::AudioChannelLayout(4, HEPHAUDIO_CH_MASK_2_2)
43#define HEPHAUDIO_CH_LAYOUT_QUAD HephAudio::AudioChannelLayout(4, HEPHAUDIO_CH_MASK_QUAD)
44#define HEPHAUDIO_CH_LAYOUT_4_POINT_1 HephAudio::AudioChannelLayout(5, HEPHAUDIO_CH_MASK_4_POINT_1)
45#define HEPHAUDIO_CH_LAYOUT_5_POINT_0 HephAudio::AudioChannelLayout(5, HEPHAUDIO_CH_MASK_5_POINT_0)
46#define HEPHAUDIO_CH_LAYOUT_5_POINT_0_BACK HephAudio::AudioChannelLayout(5, HEPHAUDIO_CH_MASK_5_POINT_0_BACK)
47#define HEPHAUDIO_CH_LAYOUT_5_POINT_1 HephAudio::AudioChannelLayout(6, HEPHAUDIO_CH_MASK_5_POINT_1)
48#define HEPHAUDIO_CH_LAYOUT_5_POINT_1_BACK HephAudio::AudioChannelLayout(6, HEPHAUDIO_CH_MASK_5_POINT_1_BACK)
49#define HEPHAUDIO_CH_LAYOUT_6_POINT_0 HephAudio::AudioChannelLayout(6, HEPHAUDIO_CH_MASK_6_POINT_0)
50#define HEPHAUDIO_CH_LAYOUT_6_POINT_0_FRONT HephAudio::AudioChannelLayout(6, HEPHAUDIO_CH_MASK_6_POINT_0_FRONT)
51#define HEPHAUDIO_CH_LAYOUT_HEXAGONAL HephAudio::AudioChannelLayout(6, HEPHAUDIO_CH_MASK_HEXAGONAL)
52#define HEPHAUDIO_CH_LAYOUT_6_POINT_1 HephAudio::AudioChannelLayout(7, HEPHAUDIO_CH_MASK_6_POINT_1)
53#define HEPHAUDIO_CH_LAYOUT_6_POINT_1_BACK HephAudio::AudioChannelLayout(7, HEPHAUDIO_CH_MASK_6_POINT_1_BACK)
54#define HEPHAUDIO_CH_LAYOUT_6_POINT_1_FRONT HephAudio::AudioChannelLayout(7, HEPHAUDIO_CH_MASK_6_POINT_1_FRONT)
55#define HEPHAUDIO_CH_LAYOUT_7_POINT_0 HephAudio::AudioChannelLayout(7, HEPHAUDIO_CH_MASK_7_POINT_0)
56#define HEPHAUDIO_CH_LAYOUT_7_POINT_0_FRONT HephAudio::AudioChannelLayout(7, HEPHAUDIO_CH_MASK_7_POINT_0_FRONT)
57#define HEPHAUDIO_CH_LAYOUT_7_POINT_1 HephAudio::AudioChannelLayout(8, HEPHAUDIO_CH_MASK_7_POINT_1)
58#define HEPHAUDIO_CH_LAYOUT_7_POINT_1_WIDE HephAudio::AudioChannelLayout(8, HEPHAUDIO_CH_MASK_7_POINT_1_WIDE)
59#define HEPHAUDIO_CH_LAYOUT_7_POINT_1_WIDE_BACK HephAudio::AudioChannelLayout(8, HEPHAUDIO_CH_MASK_7_POINT_1_WIDE_BACK)
60#define HEPHAUDIO_CH_LAYOUT_OCTAGONAL HephAudio::AudioChannelLayout(8, HEPHAUDIO_CH_MASK_OCTAGONAL)
61
62
63namespace HephAudio
64{
70 {
71 Unknown = 0,
72 FrontLeft = 0x1,
73 FrontRight = 0x2,
74 FrontCenter = 0x4,
75 LowFrequency = 0x8,
76 BackLeft = 0x10,
77 BackRight = 0x20,
78 FrontLeftOfCenter = 0x40,
79 FrontRightOfCenter = 0x80,
80 BackCenter = 0x100,
81 SideLeft = 0x200,
82 SideRight = 0x400,
83 TopCenter = 0x800,
84 TopFrontLeft = 0x1000,
85 TopFrontCenter = 0x2000,
86 TopFrontRight = 0x4000,
87 TopBackLeft = 0x8000,
88 TopBackCenter = 0x10000,
89 TopBackRight = 0x20000
90 };
91
92
93 inline constexpr AudioChannelMask operator|(const AudioChannelMask& lhs, const AudioChannelMask& rhs)
94 {
95 return (AudioChannelMask)(((int)lhs) | ((int)rhs));
96 }
97
98 inline constexpr AudioChannelMask& operator|=(AudioChannelMask& lhs, const AudioChannelMask& rhs)
99 {
100 lhs = lhs | rhs;
101 return lhs;
102 }
103
104 inline constexpr AudioChannelMask operator&(const AudioChannelMask& lhs, const AudioChannelMask& rhs)
105 {
106 return (AudioChannelMask)(((int)lhs) & ((int)rhs));
107 }
108
109 inline constexpr AudioChannelMask& operator&=(AudioChannelMask& lhs, const AudioChannelMask& rhs)
110 {
111 lhs = lhs & rhs;
112 return lhs;
113 }
114
115 inline constexpr AudioChannelMask operator^(const AudioChannelMask& lhs, const AudioChannelMask& rhs)
116 {
117 return (AudioChannelMask)(((int)lhs) ^ ((int)rhs));
118 }
119
120 inline constexpr AudioChannelMask& operator^=(AudioChannelMask& lhs, const AudioChannelMask& rhs)
121 {
122 lhs = lhs ^ rhs;
123 return lhs;
124 }
125
126 inline constexpr AudioChannelMask operator~(const AudioChannelMask& lhs)
127 {
128 return (AudioChannelMask)(~((int)lhs));
129 }
130
136 {
141 uint16_t count;
142
148
151
158 constexpr AudioChannelLayout(uint16_t count, AudioChannelMask mask) : count(count), mask(mask) {}
159
160 constexpr bool operator==(const AudioChannelLayout& rhs) const
161 {
162 return this->count == rhs.count && this->mask == rhs.mask;
163 }
164
165 constexpr bool operator!=(const AudioChannelLayout& rhs) const
166 {
167 return this->count != rhs.count || this->mask != rhs.mask;
168 }
169
174 static constexpr AudioChannelMask DefaultChannelMask(uint16_t channelCount)
175 {
176 switch (channelCount)
177 {
178 case 1:
179 return HEPHAUDIO_CH_MASK_MONO;
180 case 2:
181 return HEPHAUDIO_CH_MASK_STEREO;
182 case 3:
183 return HEPHAUDIO_CH_MASK_SURROUND;
184 case 4:
185 return HEPHAUDIO_CH_MASK_4_POINT_0;
186 case 5:
187 return HEPHAUDIO_CH_MASK_5_POINT_0;
188 case 6:
189 return HEPHAUDIO_CH_MASK_5_POINT_1;
190 case 7:
191 return HEPHAUDIO_CH_MASK_7_POINT_0;
192 case 8:
193 return HEPHAUDIO_CH_MASK_7_POINT_1;
194 default:
195 return AudioChannelMask::Unknown;
196 }
197 }
198
203 static constexpr AudioChannelLayout DefaultChannelLayout(uint16_t channelCount)
204 {
205 return AudioChannelLayout(channelCount, AudioChannelLayout::DefaultChannelMask(channelCount));
206 }
207
212 static constexpr uint16_t GetChannelCount(AudioChannelMask mask)
213 {
214 uint16_t channelCount = 0;
215 uint32_t mask32 = (uint32_t)mask;
216 for (size_t i = 0; i < 32; i++)
217 {
218 if (mask32 & 1)
219 {
220 channelCount++;
221 }
222 mask32 >>= 1;
223 }
224 return channelCount;
225 }
226
231 static constexpr uint16_t GetChannelCount(const AudioChannelLayout& layout)
232 {
233 return AudioChannelLayout::GetChannelCount(layout.mask);
234 }
235
240 static std::vector<AudioChannelMask> GetChannelMapping(AudioChannelMask mask);
241
246 static std::vector<AudioChannelMask> GetChannelMapping(const AudioChannelLayout& layout);
247 };
248}
AudioChannelMask
Definition AudioChannelLayout.h:70
#define HEPH_API
Definition HephShared.h:132
stores information about the channel layout.
Definition AudioChannelLayout.h:136
static constexpr uint16_t GetChannelCount(AudioChannelMask mask)
Definition AudioChannelLayout.h:212
static constexpr uint16_t GetChannelCount(const AudioChannelLayout &layout)
Definition AudioChannelLayout.h:231
static constexpr AudioChannelMask DefaultChannelMask(uint16_t channelCount)
Definition AudioChannelLayout.h:174
static std::vector< AudioChannelMask > GetChannelMapping(const AudioChannelLayout &layout)
static constexpr AudioChannelLayout DefaultChannelLayout(uint16_t channelCount)
Definition AudioChannelLayout.h:203
constexpr AudioChannelLayout()
Definition AudioChannelLayout.h:150
constexpr AudioChannelLayout(uint16_t count, AudioChannelMask mask)
Definition AudioChannelLayout.h:158
static std::vector< AudioChannelMask > GetChannelMapping(AudioChannelMask mask)
uint16_t count
Definition AudioChannelLayout.h:141
AudioChannelMask mask
Definition AudioChannelLayout.h:147