fix mute automation for busses & consolidate code.
[ardour.git] / libs / ardour / ardour / chan_count.h
1 /*
2     Copyright (C) 2006 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __ardour_chan_count_h__
21 #define __ardour_chan_count_h__
22
23 #include <cassert>
24 #include <ostream>
25
26 #include "pbd/xml++.h"
27 #include "ardour/data_type.h"
28
29 #ifdef INFINITE
30 #undef INFINITE
31 #endif
32
33 namespace ARDOUR {
34
35
36 /** A count of channels, possibly with many types.
37  *
38  * Operators are defined so this may safely be used as if it were a simple
39  * (single-typed) integer count of channels.
40  */
41 class LIBARDOUR_API ChanCount {
42 public:
43         ChanCount(const XMLNode& node);
44         ChanCount() { reset(); }
45
46         /** Convenience constructor for making single-typed streams (mono, stereo, midi, etc)
47          * @param type data type
48          * @param count number of channels
49          */
50         ChanCount(DataType type, uint32_t count) {
51                 reset();
52                 set(type, count);
53         }
54
55         /** zero count of all data types */
56         void reset() {
57                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
58                         _counts[*t] = 0;
59                 }
60         }
61
62         /** set channel count for given type
63          * @param type data type
64          * @param count number of channels
65          */
66         void     set(DataType t, uint32_t count) { assert(t != DataType::NIL); _counts[t] = count; }
67         /** query channel count for given type
68          * @param type data type
69          * @returns channel count for given type
70          */
71         uint32_t get(DataType t) const { assert(t != DataType::NIL); return _counts[t]; }
72
73         inline uint32_t n (DataType t) const { return _counts[t]; }
74
75         /** query number of audio channels
76          * @returns number of audio channels
77          */
78         inline uint32_t n_audio() const { return _counts[DataType::AUDIO]; }
79         /** set number of audio channels
80          * @param a number of audio channels
81          */
82         inline void set_audio(uint32_t a) { _counts[DataType::AUDIO] = a; }
83
84         /** query number of midi channels
85          * @returns number of midi channels
86          */
87         inline uint32_t n_midi()  const { return _counts[DataType::MIDI]; }
88         /** set number of audio channels
89          * @param m number of midi channels
90          */
91         inline void set_midi(uint32_t m) { _counts[DataType::MIDI] = m; }
92
93         /** query total channel count of all data types
94          * @returns total channel count (audio + midi)
95          */
96         uint32_t n_total() const {
97                 uint32_t ret = 0;
98                 for (uint32_t i=0; i < DataType::num_types; ++i)
99                         ret += _counts[i];
100
101                 return ret;
102         }
103
104         bool operator==(const ChanCount& other) const {
105                 for (uint32_t i=0; i < DataType::num_types; ++i)
106                         if (_counts[i] != other._counts[i])
107                                 return false;
108
109                 return true;
110         }
111
112         bool operator!=(const ChanCount& other) const {
113                 return ! (*this == other);
114         }
115
116         bool operator<(const ChanCount& other) const {
117                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
118                         if (_counts[*t] > other._counts[*t]) {
119                                 return false;
120                         }
121                 }
122                 return (*this != other);
123         }
124
125         bool operator<=(const ChanCount& other) const {
126                 return ( (*this < other) || (*this == other) );
127         }
128
129         bool operator>(const ChanCount& other) const {
130                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
131                         if (_counts[*t] < other._counts[*t]) {
132                                 return false;
133                         }
134                 }
135                 return (*this != other);
136         }
137
138         bool operator>=(const ChanCount& other) const {
139                 return ( (*this > other) || (*this == other) );
140         }
141
142         ChanCount operator+(const ChanCount& other) const {
143                 ChanCount ret;
144                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
145                         ret.set(*t, get(*t) + other.get(*t));
146                 }
147                 return ret;
148         }
149
150         /** underflow safe subtraction */
151         ChanCount operator-(const ChanCount& other) const {
152                 ChanCount ret;
153                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
154                         if (get(*t) < other.get(*t)) {
155                                 ret.set(*t, 0);
156                         } else {
157                                 ret.set(*t, get(*t) - other.get(*t));
158                         }
159                 }
160                 return ret;
161         }
162
163         ChanCount operator*(const unsigned int factor) const {
164                 ChanCount ret;
165                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
166                         ret.set(*t, get(*t) * factor );
167                 }
168                 return ret;
169         }
170
171         /** underflow safe subtraction */
172         ChanCount& operator-=(const ChanCount& other) {
173                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
174                         if (_counts[*t] < other._counts[*t]) {
175                                 _counts[*t] = 0;
176                         } else {
177                                 _counts[*t] -= other._counts[*t];
178                         }
179                 }
180                 return *this;
181         }
182
183         ChanCount& operator+=(const ChanCount& other) {
184                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
185                         _counts[*t] += other._counts[*t];
186                 }
187                 return *this;
188         }
189
190         static ChanCount min(const ChanCount& a, const ChanCount& b) {
191                 ChanCount ret;
192                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
193                         ret.set(*t, std::min(a.get(*t), b.get(*t)));
194                 }
195                 return ret;
196         }
197
198         static ChanCount max(const ChanCount& a, const ChanCount& b) {
199                 ChanCount ret;
200                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
201                         ret.set(*t, std::max(a.get(*t), b.get(*t)));
202                 }
203                 return ret;
204         }
205
206         XMLNode* state(const std::string& name) const;
207
208         static const ChanCount INFINITE;
209         static const ChanCount ZERO;
210
211 private:
212         uint32_t _counts[DataType::num_types];
213 };
214
215 } // namespace ARDOUR
216
217 LIBARDOUR_API std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanCount& c);
218
219 #endif // __ardour_chan_count_h__
220