ecec38865659b157c830cef040dbc3d0d3b89489
[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/types_convert.h"
25 #include "ardour/chan_mapping.h"
26
27 #include "pbd/i18n.h"
28
29 static const char* state_node_name = "Channelmap";
30
31 using namespace std;
32
33 namespace ARDOUR {
34
35 ChanMapping::ChanMapping(ChanCount identity)
36 {
37         if (identity == ChanCount::INFINITE) {
38                 return;
39         }
40
41         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
42                 for (size_t i = 0; i < identity.get(*t); ++i) {
43                         set(*t, i, i);
44                 }
45         }
46 }
47
48 ChanMapping::ChanMapping (const ChanMapping& other)
49 {
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);
54                 }
55         }
56 }
57
58 ChanMapping::ChanMapping (const XMLNode& node)
59 {
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);
64                         uint32_t from;
65                         uint32_t to;
66                         (*iter)->get_property("type", type);
67                         (*iter)->get_property("from", from);
68                         (*iter)->get_property("to", to);
69                         set(type, from, to);
70                 }
71         }
72 }
73
74 uint32_t
75 ChanMapping::get(DataType t, uint32_t from, bool* valid) const
76 {
77         Mappings::const_iterator tm = _mappings.find(t);
78         if (tm == _mappings.end()) {
79                 if (valid) { *valid = false; }
80                 return -1;
81         }
82         TypeMapping::const_iterator m = tm->second.find(from);
83         if (m == tm->second.end()) {
84                 if (valid) { *valid = false; }
85                 return -1;
86         }
87         if (valid) { *valid = true; }
88         return m->second;
89 }
90
91 uint32_t
92 ChanMapping::get_src(DataType t, uint32_t to, bool* valid) const
93 {
94         Mappings::const_iterator tm = _mappings.find(t);
95         if (tm == _mappings.end()) {
96                 if (valid) { *valid = false; }
97                 return -1;
98         }
99         for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
100                 if (i->second == to) {
101                         if (valid) { *valid = true; }
102                         return i->first;
103                 }
104         }
105         if (valid) { *valid = false; }
106         return -1;
107 }
108
109
110
111 void
112 ChanMapping::set(DataType t, uint32_t from, uint32_t to)
113 {
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;
118         }
119         tm->second.insert(std::make_pair(from, to));
120 }
121
122 void
123 ChanMapping::unset(DataType t, uint32_t from)
124 {
125         assert(t != DataType::NIL);
126         Mappings::iterator tm = _mappings.find (t);
127         if (tm == _mappings.end()) {
128                 return;
129         }
130         tm->second.erase(from);
131 }
132
133 /** Offset the 'from' field of every mapping for type @a t by @a delta */
134 void
135 ChanMapping::offset_from(DataType t, int32_t delta)
136 {
137         Mappings::iterator tm = _mappings.find(t);
138         if (tm != _mappings.end ()) {
139                 TypeMapping new_map;
140                 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
141                         new_map.insert (make_pair (m->first + delta, m->second));
142                 }
143                 tm->second = new_map;
144         }
145 }
146
147 /** Offset the 'to' field of every mapping for type @a t by @a delta */
148 void
149 ChanMapping::offset_to(DataType t, int32_t delta)
150 {
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) {
154                         m->second += delta;
155                 }
156         }
157 }
158
159 XMLNode*
160 ChanMapping::state(const std::string& name) const
161 {
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);
171                 }
172         }
173         return node;
174 }
175
176 bool
177 ChanMapping::is_subset (const ChanMapping& superset) const
178 {
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) {
182                         bool valid;
183                         if (i->second != superset.get (tm->first, i->first, &valid)) {
184                                 return false;
185                         }
186                         if (!valid) {
187                                 return false;
188                         }
189                 }
190         }
191         return true;
192 }
193
194 bool
195 ChanMapping::is_monotonic () const
196 {
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) {
203                                 return false;
204                         }
205                         prev = i->second;
206                 }
207         }
208         return true;
209 }
210
211 bool
212 ChanMapping::is_identity (ChanCount offset) const
213 {
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) {
218                                 return false;
219                         }
220                 }
221         }
222         return true;
223 }
224
225 uint32_t
226 ChanMapping::n_total () const
227 {
228         // fast version of count().n_total();
229         uint32_t rv = 0;
230         const Mappings& mp (mappings());
231         for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
232                 rv += tm->second.size ();
233         }
234         return rv;
235 }
236
237 ChanCount
238 ChanMapping::count () const
239 {
240         ChanCount rv;
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 ());
244         }
245         return rv;
246 }
247
248
249
250 } // namespace ARDOUR
251
252 std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanMapping& cm)
253 {
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;
260                 }
261         }
262
263         return o;
264 }