2 Copyright (C) 2006 Paul Davis
3 Author: David Robillard
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.
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.
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.
20 #ifndef __ardour_chan_count_h__
21 #define __ardour_chan_count_h__
26 #include "pbd/xml++.h"
27 #include "ardour/data_type.h"
32 /** A count of channels, possibly with many types.
34 * Operators are defined so this may safely be used as if it were a simple
35 * (single-typed) integer count of channels.
37 class LIBARDOUR_API ChanCount {
39 ChanCount(const XMLNode& node);
40 ChanCount() { reset(); }
42 /** Convenience constructor for making single-typed streams (mono, stereo, midi, etc)
43 * @param type data type
44 * @param count number of channels
46 ChanCount(DataType type, uint32_t count) {
51 /** zero count of all data types */
53 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
58 /** set channel count for given type
59 * @param type data type
60 * @param count number of channels
62 void set(DataType t, uint32_t count) { assert(t != DataType::NIL); _counts[t] = count; }
63 /** query channel count for given type
64 * @param type data type
65 * @returns channel count for given type
67 uint32_t get(DataType t) const { assert(t != DataType::NIL); return _counts[t]; }
69 inline uint32_t n (DataType t) const { return _counts[t]; }
71 /** query number of audio channels
72 * @returns number of audio channels
74 inline uint32_t n_audio() const { return _counts[DataType::AUDIO]; }
75 /** set number of audio channels
76 * @param a number of audio channels
78 inline void set_audio(uint32_t a) { _counts[DataType::AUDIO] = a; }
80 /** query number of midi channels
81 * @returns number of midi channels
83 inline uint32_t n_midi() const { return _counts[DataType::MIDI]; }
84 /** set number of audio channels
85 * @param m number of midi channels
87 inline void set_midi(uint32_t m) { _counts[DataType::MIDI] = m; }
89 /** query total channel count of all data types
90 * @returns total channel count (audio + midi)
92 uint32_t n_total() const {
94 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t)
100 bool operator==(const ChanCount& other) const {
101 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t)
102 if (_counts[*t] != other._counts[*t])
108 bool operator!=(const ChanCount& other) const {
109 return ! (*this == other);
112 bool operator<(const ChanCount& other) const {
113 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
114 if (_counts[*t] > other._counts[*t]) {
118 return (*this != other);
121 bool operator<=(const ChanCount& other) const {
122 return ( (*this < other) || (*this == other) );
125 bool operator>(const ChanCount& other) const {
126 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
127 if (_counts[*t] < other._counts[*t]) {
131 return (*this != other);
134 bool operator>=(const ChanCount& other) const {
135 return ( (*this > other) || (*this == other) );
138 ChanCount operator+(const ChanCount& other) const {
140 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
141 ret.set(*t, get(*t) + other.get(*t));
146 /** underflow safe subtraction */
147 ChanCount operator-(const ChanCount& other) const {
149 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
150 if (get(*t) < other.get(*t)) {
153 ret.set(*t, get(*t) - other.get(*t));
159 ChanCount operator*(const unsigned int factor) const {
161 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
162 ret.set(*t, get(*t) * factor );
167 /** underflow safe subtraction */
168 ChanCount& operator-=(const ChanCount& other) {
169 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
170 if (_counts[*t] < other._counts[*t]) {
173 _counts[*t] -= other._counts[*t];
179 ChanCount& operator+=(const ChanCount& other) {
180 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
181 _counts[*t] += other._counts[*t];
186 static ChanCount min(const ChanCount& a, const ChanCount& b) {
188 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
189 ret.set(*t, std::min(a.get(*t), b.get(*t)));
194 static ChanCount max(const ChanCount& a, const ChanCount& b) {
196 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
197 ret.set(*t, std::max(a.get(*t), b.get(*t)));
202 XMLNode* state(const std::string& name) const;
204 static const ChanCount ZERO;
207 uint32_t _counts[DataType::num_types];
210 } // namespace ARDOUR
212 LIBARDOUR_API std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanCount& c);
214 #endif // __ardour_chan_count_h__