2 Copyright (C) 2009 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.
19 $Id: insert.cc 712 2006-07-28 01:08:57Z drobilla $
24 #include "ardour/chan_mapping.h"
25 #include "ardour/types_convert.h"
29 static const char* state_node_name = "Channelmap";
35 ChanMapping::ChanMapping(ChanCount identity)
37 if (identity == ChanCount::INFINITE) {
41 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
42 for (size_t i = 0; i < identity.get(*t); ++i) {
48 ChanMapping::ChanMapping (const ChanMapping& other)
50 const ChanMapping::Mappings& mp (other.mappings());
51 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
52 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
53 set (tm->first, i->first, i->second);
58 ChanMapping::ChanMapping (const XMLNode& node)
60 XMLNodeConstIterator iter = node.children().begin();
61 for ( ; iter != node.children().end(); ++iter) {
62 if ((*iter)->name() == X_(state_node_name)) {
63 DataType type(DataType::NIL);
66 (*iter)->get_property("type", type);
67 (*iter)->get_property("from", from);
68 (*iter)->get_property("to", to);
75 ChanMapping::get(DataType t, uint32_t from, bool* valid) const
77 Mappings::const_iterator tm = _mappings.find(t);
78 if (tm == _mappings.end()) {
79 if (valid) { *valid = false; }
82 TypeMapping::const_iterator m = tm->second.find(from);
83 if (m == tm->second.end()) {
84 if (valid) { *valid = false; }
87 if (valid) { *valid = true; }
92 ChanMapping::get_src(DataType t, uint32_t to, bool* valid) const
94 Mappings::const_iterator tm = _mappings.find(t);
95 if (tm == _mappings.end()) {
96 if (valid) { *valid = false; }
99 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
100 if (i->second == to) {
101 if (valid) { *valid = true; }
105 if (valid) { *valid = false; }
112 ChanMapping::set(DataType t, uint32_t from, uint32_t to)
114 assert(t != DataType::NIL);
115 Mappings::iterator tm = _mappings.find (t);
116 if (tm == _mappings.end()) {
117 tm = _mappings.insert(std::make_pair(t, TypeMapping())).first;
119 tm->second.insert(std::make_pair(from, to));
123 ChanMapping::unset(DataType t, uint32_t from)
125 assert(t != DataType::NIL);
126 Mappings::iterator tm = _mappings.find (t);
127 if (tm == _mappings.end()) {
130 tm->second.erase(from);
133 /** Offset the 'from' field of every mapping for type @a t by @a delta */
135 ChanMapping::offset_from(DataType t, int32_t delta)
137 Mappings::iterator tm = _mappings.find(t);
138 if (tm != _mappings.end ()) {
140 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
141 new_map.insert (make_pair (m->first + delta, m->second));
143 tm->second = new_map;
147 /** Offset the 'to' field of every mapping for type @a t by @a delta */
149 ChanMapping::offset_to(DataType t, int32_t delta)
151 Mappings::iterator tm = _mappings.find(t);
152 if (tm != _mappings.end()) {
153 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
160 ChanMapping::state(const std::string& name) const
162 XMLNode* node = new XMLNode (name);
163 const Mappings& mp (mappings());
164 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
165 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
166 XMLNode* n = new XMLNode(X_(state_node_name));
167 n->set_property("type", tm->first.to_string());
168 n->set_property("from", i->first);
169 n->set_property("to", i->second);
170 node->add_child_nocopy(*n);
177 ChanMapping::is_subset (const ChanMapping& superset) const
179 const Mappings& mp (mappings());
180 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
181 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
183 if (i->second != superset.get (tm->first, i->first, &valid)) {
195 ChanMapping::is_monotonic () const
197 const Mappings& mp (mappings());
198 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
199 uint32_t prev = UINT32_MAX;
200 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
201 // set keys are strictly weak ordered
202 if (i->first < i->second || i->second == prev) {
212 ChanMapping::is_identity (ChanCount offset) const
214 const Mappings& mp (mappings());
215 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
216 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
217 if (i->first + offset.get (tm->first) != i->second) {
226 ChanMapping::n_total () const
228 // fast version of count().n_total();
230 const Mappings& mp (mappings());
231 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
232 rv += tm->second.size ();
238 ChanMapping::count () const
241 const Mappings& mp (mappings());
242 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
243 rv.set (tm->first, tm->second.size ());
250 } // namespace ARDOUR
252 std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanMapping& cm)
254 const ARDOUR::ChanMapping::Mappings& mp (cm.mappings());
255 for (ARDOUR::ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
256 o << tm->first.to_string() << endl;
257 for (ARDOUR::ChanMapping::TypeMapping::const_iterator i = tm->second.begin();
258 i != tm->second.end(); ++i) {
259 o << "\t" << i->first << " => " << i->second << endl;