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"
28 static const char* state_node_name = "Channelmap";
34 ChanMapping::ChanMapping(ChanCount identity)
36 if (identity == ChanCount::INFINITE) {
40 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
41 for (size_t i = 0; i < identity.get(*t); ++i) {
47 ChanMapping::ChanMapping (const ChanMapping& other)
49 const ChanMapping::Mappings& mp (other.mappings());
50 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
51 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
52 set (tm->first, i->first, i->second);
57 ChanMapping::ChanMapping (const XMLNode& node)
59 XMLNodeConstIterator iter = node.children().begin();
60 for ( ; iter != node.children().end(); ++iter) {
61 if ((*iter)->name() == X_(state_node_name)) {
62 const string& type_str = (*iter)->property("type")->value();
63 const string& from_str = (*iter)->property("from")->value();
64 const string& to_str = (*iter)->property("to")->value();
65 set(DataType(type_str), atol (from_str.c_str()), atol (to_str.c_str()));
71 ChanMapping::get(DataType t, uint32_t from, bool* valid) const
73 Mappings::const_iterator tm = _mappings.find(t);
74 if (tm == _mappings.end()) {
75 if (valid) { *valid = false; }
78 TypeMapping::const_iterator m = tm->second.find(from);
79 if (m == tm->second.end()) {
80 if (valid) { *valid = false; }
83 if (valid) { *valid = true; }
88 ChanMapping::get_src(DataType t, uint32_t to, bool* valid) const
90 Mappings::const_iterator tm = _mappings.find(t);
91 if (tm == _mappings.end()) {
92 if (valid) { *valid = false; }
95 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
96 if (i->second == to) {
97 if (valid) { *valid = true; }
101 if (valid) { *valid = false; }
108 ChanMapping::set(DataType t, uint32_t from, uint32_t to)
110 assert(t != DataType::NIL);
111 Mappings::iterator tm = _mappings.find (t);
112 if (tm == _mappings.end()) {
113 tm = _mappings.insert(std::make_pair(t, TypeMapping())).first;
115 tm->second.insert(std::make_pair(from, to));
119 ChanMapping::unset(DataType t, uint32_t from)
121 assert(t != DataType::NIL);
122 Mappings::iterator tm = _mappings.find (t);
123 if (tm == _mappings.end()) {
126 tm->second.erase(from);
129 /** Offset the 'from' field of every mapping for type @a t by @a delta */
131 ChanMapping::offset_from(DataType t, int32_t delta)
133 Mappings::iterator tm = _mappings.find(t);
134 if (tm != _mappings.end ()) {
136 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
137 new_map.insert (make_pair (m->first + delta, m->second));
139 tm->second = new_map;
143 /** Offset the 'to' field of every mapping for type @a t by @a delta */
145 ChanMapping::offset_to(DataType t, int32_t delta)
147 Mappings::iterator tm = _mappings.find(t);
148 if (tm != _mappings.end()) {
149 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
156 ChanMapping::state(const std::string& name) const
158 XMLNode* node = new XMLNode (name);
159 const Mappings& mp (mappings());
160 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
161 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
162 XMLNode* n = new XMLNode(X_(state_node_name));
163 n->add_property("type", tm->first.to_string());
164 n->add_property("from", i->first);
165 n->add_property("to", i->second);
166 node->add_child_nocopy(*n);
173 ChanMapping::is_subset (const ChanMapping& superset) const
175 const Mappings& mp (mappings());
176 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
177 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
179 if (i->second != superset.get (tm->first, i->first, &valid)) {
191 ChanMapping::is_monotonic () const
193 const Mappings& mp (mappings());
194 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
195 uint32_t prev = UINT32_MAX;
196 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
197 // set keys are strictly weak ordered
198 if (i->first < i->second || i->second == prev) {
208 ChanMapping::is_identity (ChanCount offset) const
210 const Mappings& mp (mappings());
211 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
212 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
213 if (i->first + offset.get (tm->first) != i->second) {
222 ChanMapping::n_total () const
224 // fast version of count().n_total();
226 const Mappings& mp (mappings());
227 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
228 rv += tm->second.size ();
234 ChanMapping::count () const
237 const Mappings& mp (mappings());
238 for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
239 rv.set (tm->first, tm->second.size ());
246 } // namespace ARDOUR
248 std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanMapping& cm)
250 const ARDOUR::ChanMapping::Mappings& mp (cm.mappings());
251 for (ARDOUR::ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
252 o << tm->first.to_string() << endl;
253 for (ARDOUR::ChanMapping::TypeMapping::const_iterator i = tm->second.begin();
254 i != tm->second.end(); ++i) {
255 o << "\t" << i->first << " => " << i->second << endl;