0ca79af505d1f69ccf58b1008e8f77c84ea0be24
[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 using namespace std;
27
28 namespace ARDOUR {
29
30 ChanMapping::ChanMapping(ChanCount identity)
31 {
32         if (identity == ChanCount::INFINITE) {
33                 return;
34         }
35
36         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
37                 for (size_t i = 0; i < identity.get(*t); ++i) {
38                         set(*t, i, i);
39                 }
40         }
41 }
42
43 ChanMapping::ChanMapping (const ChanMapping& other )
44 {
45         const ChanMapping::Mappings& mp (other.mappings());
46         for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
47                 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
48                         set (tm->first, i->first, i->second);
49                 }
50         }
51 }
52
53 uint32_t
54 ChanMapping::get(DataType t, uint32_t from, bool* valid) const
55 {
56         Mappings::const_iterator tm = _mappings.find(t);
57         if (tm == _mappings.end()) {
58                 if (valid) { *valid = false; }
59                 return -1;
60         }
61         TypeMapping::const_iterator m = tm->second.find(from);
62         if (m == tm->second.end()) {
63                 if (valid) { *valid = false; }
64                 return -1;
65         }
66         if (valid) { *valid = true; }
67         return m->second;
68 }
69
70 void
71 ChanMapping::set(DataType t, uint32_t from, uint32_t to)
72 {
73         assert(t != DataType::NIL);
74         Mappings::iterator tm = _mappings.find (t);
75         if (tm == _mappings.end()) {
76                 tm = _mappings.insert(std::make_pair(t, TypeMapping())).first;
77         }
78         tm->second.insert(std::make_pair(from, to));
79 }
80
81 void
82 ChanMapping::unset(DataType t, uint32_t from)
83 {
84         assert(t != DataType::NIL);
85         Mappings::iterator tm = _mappings.find (t);
86         if (tm == _mappings.end()) {
87                 return;
88         }
89         tm->second.erase(from);
90 }
91
92 /** Offset the 'from' field of every mapping for type @a t by @a delta */
93 void
94 ChanMapping::offset_from(DataType t, int32_t delta)
95 {
96         Mappings::iterator tm = _mappings.find(t);
97         if (tm != _mappings.end ()) {
98                 TypeMapping new_map;
99                 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
100                         new_map.insert (make_pair (m->first + delta, m->second));
101                 }
102                 tm->second = new_map;
103         }
104 }
105
106 /** Offset the 'to' field of every mapping for type @a t by @a delta */
107 void
108 ChanMapping::offset_to(DataType t, int32_t delta)
109 {
110         Mappings::iterator tm = _mappings.find(t);
111         if (tm != _mappings.end()) {
112                 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
113                         m->second += delta;
114                 }
115         }
116 }
117
118 bool
119 ChanMapping::is_subset (const ChanMapping& superset) const
120 {
121         const Mappings& mp (mappings());
122         for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
123                 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
124                         bool valid;
125                         if (i->second != superset.get (tm->first, i->first, &valid)) {
126                                 return false;
127                         }
128                         if (!valid) {
129                                 return false;
130                         }
131                 }
132         }
133         return true;
134 }
135
136 bool
137 ChanMapping::is_monotonic () const
138 {
139         const Mappings& mp (mappings());
140         for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
141                 uint32_t prev = UINT32_MAX;
142                 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
143                         // set keys are strictly weak ordered
144                         if (i->first < i->second || i->second == prev) {
145                                 return false;
146                         }
147                         prev = i->second;
148                 }
149         }
150         return true;
151 }
152
153 bool
154 ChanMapping::is_identity (ChanCount offset) const
155 {
156         const Mappings& mp (mappings());
157         for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
158                 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
159                         if (i->first + offset.get (tm->first) != i->second) {
160                                 return false;
161                         }
162                 }
163         }
164         return true;
165 }
166
167 } // namespace ARDOUR
168
169 std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanMapping& cm)
170 {
171         const ARDOUR::ChanMapping::Mappings& mp (cm.mappings());
172         for (ARDOUR::ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
173                 o << tm->first.to_string() << endl;
174                 for (ARDOUR::ChanMapping::TypeMapping::const_iterator i = tm->second.begin();
175                                 i != tm->second.end(); ++i) {
176                         o << "\t" << i->first << " => " << i->second << endl;
177                 }
178         }
179
180         return o;
181 }