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"
36 /** A count of channels, possibly with many types.
38 * Operators are defined so this may safely be used as if it were a simple
39 * (single-typed) integer count of channels.
41 class LIBARDOUR_API ChanCount {
43 ChanCount(const XMLNode& node);
44 ChanCount() { reset(); }
46 /** Convenience constructor for making single-typed streams (mono, stereo, midi, etc)
47 * @param type data type
48 * @param count number of channels
50 ChanCount(DataType type, uint32_t count) {
55 /** zero count of all data types */
57 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
62 /** set channel count for given type
63 * @param type data type
64 * @param count number of channels
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
71 uint32_t get(DataType t) const { assert(t != DataType::NIL); return _counts[t]; }
73 inline uint32_t n (DataType t) const { return _counts[t]; }
75 /** query number of audio channels
76 * @returns number of audio channels
78 inline uint32_t n_audio() const { return _counts[DataType::AUDIO]; }
79 /** set number of audio channels
80 * @param a number of audio channels
82 inline void set_audio(uint32_t a) { _counts[DataType::AUDIO] = a; }
84 /** query number of midi channels
85 * @returns number of midi channels
87 inline uint32_t n_midi() const { return _counts[DataType::MIDI]; }
88 /** set number of audio channels
89 * @param m number of midi channels
91 inline void set_midi(uint32_t m) { _counts[DataType::MIDI] = m; }
93 /** query total channel count of all data types
94 * @returns total channel count (audio + midi)
96 uint32_t n_total() const {
98 for (uint32_t i=0; i < DataType::num_types; ++i)
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])
112 bool operator!=(const ChanCount& other) const {
113 return ! (*this == other);
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]) {
122 return (*this != other);
125 bool operator<=(const ChanCount& other) const {
126 return ( (*this < other) || (*this == other) );
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]) {
135 return (*this != other);
138 bool operator>=(const ChanCount& other) const {
139 return ( (*this > other) || (*this == other) );
142 ChanCount operator+(const ChanCount& other) const {
144 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
145 ret.set(*t, get(*t) + other.get(*t));
150 /** underflow safe subtraction */
151 ChanCount operator-(const ChanCount& other) const {
153 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
154 if (get(*t) < other.get(*t)) {
157 ret.set(*t, get(*t) - other.get(*t));
163 ChanCount operator*(const unsigned int factor) const {
165 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
166 ret.set(*t, get(*t) * factor );
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]) {
177 _counts[*t] -= other._counts[*t];
183 ChanCount& operator+=(const ChanCount& other) {
184 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
185 _counts[*t] += other._counts[*t];
190 static ChanCount min(const ChanCount& a, const ChanCount& b) {
192 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
193 ret.set(*t, std::min(a.get(*t), b.get(*t)));
198 static ChanCount max(const ChanCount& a, const ChanCount& b) {
200 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
201 ret.set(*t, std::max(a.get(*t), b.get(*t)));
206 XMLNode* state(const std::string& name) const;
208 static const ChanCount INFINITE;
209 static const ChanCount ZERO;
212 uint32_t _counts[DataType::num_types];
215 } // namespace ARDOUR
217 LIBARDOUR_API std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanCount& c);
219 #endif // __ardour_chan_count_h__