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 sub-set of given mask, or equal to it.
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_subset(ChannelMask mask) const;
91 
92  //! Check if channel set is super-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 succeed.
96  bool is_superset(ChannelMask mask) const;
97 
98  //! Set given channel to be enabled or disabled.
99  void set_channel(size_t n, bool enabled);
100 
101  //! Set all channels in inclusive range to be enabled or disabled.
102  void set_channel_range(size_t from, size_t to, bool enabled);
103 
104  //! Set enabled channels based on given mask.
105  //! @remarks
106  //! The mask defines only first 32 channels. All channels outside of 0-31
107  //! range will be disabled.
109 
110  //! Set channel set to result of bitwise AND operation with another set.
111  //! @remarks
112  //! Similar to "&=".
113  void bitwise_and(const ChannelSet& other);
114 
115  //! Set channel set to result of bitwise OR operation with another set.
116  //! @remarks
117  //! Similar to "|=".
118  void bitwise_or(const ChannelSet& other);
119 
120  //! Set channel set to result of bitwise XOR operation with another set.
121  //! @remarks
122  //! Similar to "^=".
123  void bitwise_xor(const ChannelSet& other);
124 
125  //! Get number of bytes in bit mask.
126  size_t num_bytes() const;
127 
128  //! Get byte by index from bit mask.
129  uint8_t byte_at(size_t n) const;
130 
131 private:
132  typedef uint64_t word_t;
133 
134  enum {
135  MaxChannels = 1024,
136  WordBytes = sizeof(word_t),
137  WordBits = WordBytes * 8,
138  NumWords = MaxChannels / WordBits
139  };
140 
141  void update_();
142 
143  word_t words_[NumWords];
144 
145  uint16_t num_chans_;
146  uint16_t first_chan_;
147  uint16_t last_chan_;
148 
149  ChannelLayout layout_;
150  ChannelOrder order_;
151 };
152 
153 //! Format ChannelSet to string.
155 
156 } // namespace audio
157 } // namespace roc
158 
159 #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_channel(size_t n, bool enabled)
Set given channel to be enabled or disabled.
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.
void set_channel_range(size_t from, size_t to, bool enabled)
Set all channels in inclusive range to be enabled or disabled.
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 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 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.
void set_channel_mask(ChannelMask mask)
Set enabled channels based on given mask.
uint8_t byte_at(size_t n) const
Get byte by index from bit mask.
void clear()
Unset all fields.
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.
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:144
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.