fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / chan_mapping.cc
1 /*
2     Copyright (C) 2009 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     $Id: insert.cc 712 2006-07-28 01:08:57Z drobilla $
20 */
21
22 #include <stdint.h>
23 #include <iostream>
24 #include "ardour/chan_mapping.h"
25
26 #include "pbd/i18n.h"
27
28 static const char* state_node_name = "Channelmap";
29
30 using namespace std;
31
32 namespace ARDOUR {
33
34 ChanMapping::ChanMapping(ChanCount identity)
35 {
36         if (identity == ChanCount::INFINITE) {
37                 return;
38         }
39
40         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
41                 for (size_t i = 0; i < identity.get(*t); ++i) {
42                         set(*t, i, i);
43                 }
44         }
45 }
46
47 ChanMapping::ChanMapping (const ChanMapping& other)
48 {
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);
53                 }
54         }
55 }
56
57 ChanMapping::ChanMapping (const XMLNode& node)
58 {
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()));
66                 }
67         }
68 }
69
70 uint32_t
71 ChanMapping::get(DataType t, uint32_t from, bool* valid) const
72 {
73         Mappings::const_iterator tm = _mappings.find(t);
74         if (tm == _mappings.end()) {
75                 if (valid) { *valid = false; }
76                 return -1;
77         }
78         TypeMapping::const_iterator m = tm->second.find(from);
79         if (m == tm->second.end()) {
80                 if (valid) { *valid = false; }
81                 return -1;
82         }
83         if (valid) { *valid = true; }
84         return m->second;
85 }
86
87 uint32_t
88 ChanMapping::get_src(DataType t, uint32_t to, bool* valid) const
89 {
90         Mappings::const_iterator tm = _mappings.find(t);
91         if (tm == _mappings.end()) {
92                 if (valid) { *valid = false; }
93                 return -1;
94         }
95         for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
96                 if (i->second == to) {
97                         if (valid) { *valid = true; }
98                         return i->first;
99                 }
100         }
101         if (valid) { *valid = false; }
102         return -1;
103 }
104
105
106
107 void
108 ChanMapping::set(DataType t, uint32_t from, uint32_t to)
109 {
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;
114         }
115         tm->second.insert(std::make_pair(from, to));
116 }
117
118 void
119 ChanMapping::unset(DataType t, uint32_t from)
120 {
121         assert(t != DataType::NIL);
122         Mappings::iterator tm = _mappings.find (t);
123         if (tm == _mappings.end()) {
124                 return;
125         }
126         tm->second.erase(from);
127 }
128
129 /** Offset the 'from' field of every mapping for type @a t by @a delta */
130 void
131 ChanMapping::offset_from(DataType t, int32_t delta)
132 {
133         Mappings::iterator tm = _mappings.find(t);
134         if (tm != _mappings.end ()) {
135                 TypeMapping new_map;
136                 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
137                         new_map.insert (make_pair (m->first + delta, m->second));
138                 }
139                 tm->second = new_map;
140         }
141 }
142
143 /** Offset the 'to' field of every mapping for type @a t by @a delta */
144 void
145 ChanMapping::offset_to(DataType t, int32_t delta)
146 {
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) {
150                         m->second += delta;
151                 }
152         }
153 }
154
155 XMLNode*
156 ChanMapping::state(const std::string& name) const
157 {
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);
167                 }
168         }
169         return node;
170 }
171
172 bool
173 ChanMapping::is_subset (const ChanMapping& superset) const
174 {
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) {
178                         bool valid;
179                         if (i->second != superset.get (tm->first, i->first, &valid)) {
180                                 return false;
181                         }
182                         if (!valid) {
183                                 return false;
184                         }
185                 }
186         }
187         return true;
188 }
189
190 bool
191 ChanMapping::is_monotonic () const
192 {
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) {
199                                 return false;
200                         }
201                         prev = i->second;
202                 }
203         }
204         return true;
205 }
206
207 bool
208 ChanMapping::is_identity (ChanCount offset) const
209 {
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) {
214                                 return false;
215                         }
216                 }
217         }
218         return true;
219 }
220
221 uint32_t
222 ChanMapping::n_total () const
223 {
224         // fast version of count().n_total();
225         uint32_t rv = 0;
226         const Mappings& mp (mappings());
227         for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
228                 rv += tm->second.size ();
229         }
230         return rv;
231 }
232
233 ChanCount
234 ChanMapping::count () const
235 {
236         ChanCount rv;
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 ());
240         }
241         return rv;
242 }
243
244
245
246 } // namespace ARDOUR
247
248 std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanMapping& cm)
249 {
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;
256                 }
257         }
258
259         return o;
260 }