Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
channel_set.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_audio/channel_set.h
10 //! @brief Channel set.
11 
12 #ifndef ROC_AUDIO_CHANNEL_SET_H_
13 #define ROC_AUDIO_CHANNEL_SET_H_
14 
15 #include "roc_audio/channel_defs.h"
16 #include "roc_core/stddefs.h"
18 
19 namespace roc {
20 namespace audio {
21 
22 //! Channel set.
23 //! Multi-word bitmask with bits corresponding to enabled channels.
24 //! Meaning of each channel is defined by ChannelLayout.
25 //! Order of serialized channels is defined by ChannelOrder.
26 class ChannelSet {
27 public:
28  //! Construct empty channel set.
30 
31  //! Construct with given layout and 32-bit channel mask.
32  //! @remarks
33  //! The mask defines only first 32 channels. All channels outside of 0-31
34  //! range will be disabled. If you need more channels, construct empty
35  //! channel set and enable channels or channel ranges using setters.
37 
38  //! Check two channel sets for equality.
39  bool operator==(const ChannelSet& other) const;
40 
41  //! Check two channel sets for equality.
42  bool operator!=(const ChannelSet& other) const;
43 
44  //! Check if channel set has valid layout and order, and non-zero channels.
45  bool is_valid() const;
46 
47  //! Unset all fields.
48  void clear();
49 
50  //! Get channel layout.
51  //! @remarks
52  //! Defines meaning of channel numbers (e.g. that channel 0 is front-left).
54 
55  //! Set layout of the channel set.
57 
58  //! Get channel order.
59  //! @remarks
60  //! Defines order of serialized channels
61  //! (e.g. that front-left goes before front-right).
63 
64  //! Set order of the channel set.
66 
67  //! Get maximum possible number of channels.
68  static size_t max_channels();
69 
70  //! Get number of enabled channels.
71  size_t num_channels() const;
72 
73  //! Check if specific channel is enabled.
74  bool has_channel(size_t n) const;
75 
76  //! Get index of first enabled channel.
77  //! @remarks
78  //! Panics if there are no enabled channels.
79  size_t first_channel() const;
80 
81  //! Get index of last enabled channel.
82  //! @remarks
83  //! Panics if there are no enabled channels.
84  size_t last_channel() const;
85 
86  //! Check if channel set is equal to given mask.
87  //! @remarks
88  //! The mask defines only first 32 channels. If any channels outside of 0-31
89  //! range are enabled in channel set, the method will fail.
90  bool is_equal(ChannelMask mask) const;
91 
92  //! Check if channel set is sub-set of given mask, or equal to it.
93  //! @remarks
94  //! The mask defines only first 32 channels. If any channels outside of 0-31
95  //! range are enabled in channel set, the method will fail.
96  bool is_subset(ChannelMask mask) const;
97 
98  //! Check if channel set is super-set of given mask, or equal to it.
99  //! @remarks
100  //! The mask defines only first 32 channels. If any channels outside of 0-31
101  //! range are enabled in channel set, the method will succeed.
102  bool is_superset(ChannelMask mask) const;
103 
104  //! Set channel mask to given bitmask.
105  //! @remarks
106  //! The mask defines only first 32 channels.
107  //! All channels outside of the 0-31 range are disabled.
108  void set_mask(ChannelMask mask);
109 
110  //! Set channel mask to all channels from inclusive range.
111  //! @remarks
112  //! All channels within range and enabled.
113  //! All other channels are disabled.
114  void set_range(size_t from, size_t to);
115 
116  //! Set channel mask based on channel count.
117  //! @remarks
118  //! Tries to find a mask that looks most appropriate for given channel count.
119  //! Falls back to just enabling first N channels and disabling others.
120  void set_count(size_t count);
121 
122  //! Enable/disable given channel.
123  void toggle_channel(size_t n, bool enabled);
124 
125  //! Enable/disable all channels in inclusive range.
126  void toggle_channel_range(size_t from, size_t to, bool enabled);
127 
128  //! Set channel set to result of bitwise AND operation with another set.
129  //! @remarks
130  //! Similar to "&=".
131  void bitwise_and(const ChannelSet& other);
132 
133  //! Set channel set to result of bitwise OR operation with another set.
134  //! @remarks
135  //! Similar to "|=".
136  void bitwise_or(const ChannelSet& other);
137 
138  //! Set channel set to result of bitwise XOR operation with another set.
139  //! @remarks
140  //! Similar to "^=".
141  void bitwise_xor(const ChannelSet& other);
142 
143  //! Get number of bytes in bit mask.
144  size_t num_bytes() const;
145 
146  //! Get byte by index from bit mask.
147  uint8_t byte_at(size_t n) const;
148 
149 private:
150  typedef uint64_t word_t;
151 
152  enum {
153  MaxChannels = 1024,
154  WordBytes = sizeof(word_t),
155  WordBits = WordBytes * 8,
156  NumWords = MaxChannels / WordBits
157  };
158 
159  void clear_chans_();
160  void index_chans_();
161 
162  word_t words_[NumWords];
163 
164  uint16_t num_chans_;
165  uint16_t first_chan_;
166  uint16_t last_chan_;
167 
168  ChannelLayout layout_;
169  ChannelOrder order_;
170 };
171 
172 //! Format ChannelSet to string.
174 
175 } // namespace audio
176 } // namespace roc
177 
178 #endif // ROC_AUDIO_CHANNEL_SET_H_
Channel layout, order, and positions.
Channel set. Multi-word bitmask with bits corresponding to enabled channels. Meaning of each channel ...
Definition: channel_set.h:26
void set_layout(ChannelLayout layout)
Set layout of the channel set.
size_t last_channel() const
Get index of last enabled channel.
bool operator==(const ChannelSet &other) const
Check two channel sets for equality.
ChannelSet(ChannelLayout layout, ChannelOrder order, ChannelMask mask)
Construct with given layout and 32-bit channel mask.
static size_t max_channels()
Get maximum possible number of channels.
void toggle_channel(size_t n, bool enabled)
Enable/disable given channel.
void bitwise_or(const ChannelSet &other)
Set channel set to result of bitwise OR operation with another set.
bool has_channel(size_t n) const
Check if specific channel is enabled.
size_t num_bytes() const
Get number of bytes in bit mask.
void toggle_channel_range(size_t from, size_t to, bool enabled)
Enable/disable all channels in inclusive range.
void bitwise_and(const ChannelSet &other)
Set channel set to result of bitwise AND operation with another set.
void set_order(ChannelOrder order)
Set order of the channel set.
uint8_t byte_at(size_t n) const
Get byte by index from bit mask.
void clear()
Unset all fields.
void set_mask(ChannelMask mask)
Set channel mask to given bitmask.
void set_range(size_t from, size_t to)
Set channel mask to all channels from inclusive range.
void bitwise_xor(const ChannelSet &other)
Set channel set to result of bitwise XOR operation with another set.
ChannelLayout layout() const
Get channel layout.
size_t first_channel() const
Get index of first enabled channel.
size_t num_channels() const
Get number of enabled channels.
bool is_superset(ChannelMask mask) const
Check if channel set is super-set of given mask, or equal to it.
bool is_subset(ChannelMask mask) const
Check if channel set is sub-set of given mask, or equal to it.
ChannelSet()
Construct empty channel set.
bool operator!=(const ChannelSet &other) const
Check two channel sets for equality.
bool is_valid() const
Check if channel set has valid layout and order, and non-zero channels.
bool is_equal(ChannelMask mask) const
Check if channel set is equal to given mask.
void set_count(size_t count)
Set channel mask based on channel count.
ChannelOrder order() const
Get channel order.
ChannelLayout
Channel layout. Defines meaning of channels in ChannelSet. ChannelMapper uses channel layout to decid...
Definition: channel_defs.h:23
uint32_t ChannelMask
Channel mask.
Definition: channel_defs.h:148
void format_channel_set(const ChannelSet &ch_set, core::StringBuilder &bld)
Format ChannelSet to string.
ChannelOrder
Surround channel order.
Definition: channel_defs.h:49
Root namespace.
Commonly used types and functions.
String builder.