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