More robust plugin I/O mapping.
[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 uint32_t
44 ChanMapping::get(DataType t, uint32_t from, bool* valid)
45 {
46         Mappings::iterator tm = _mappings.find(t);
47         if (tm == _mappings.end()) {
48                 *valid = false;
49                 return -1;
50         }
51         TypeMapping::iterator m = tm->second.find(from);
52         if (m == tm->second.end()) {
53                 *valid = false;
54                 return -1;
55         }
56         *valid = true;
57         return m->second;
58 }
59
60 void
61 ChanMapping::set(DataType t, uint32_t from, uint32_t to)
62 {
63         assert(t != DataType::NIL);
64         Mappings::iterator tm = _mappings.find(t);
65         if (tm == _mappings.end()) {
66                 tm = _mappings.insert(std::make_pair(t, TypeMapping())).first;
67         }
68         tm->second.insert(std::make_pair(from, to));
69 }
70
71 /** Offset the 'from' field of every mapping for type @a t by @a delta */
72 void
73 ChanMapping::offset_from(DataType t, int32_t delta)
74 {
75         Mappings::iterator tm = _mappings.find(t);
76         if (tm != _mappings.end()) {
77                 TypeMapping new_map;
78                 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
79                         new_map.insert(make_pair(m->first + delta, m->second));
80                 }
81                 tm->second = new_map;
82         }
83 }
84
85 /** Offset the 'to' field of every mapping for type @a t by @a delta */
86 void
87 ChanMapping::offset_to(DataType t, int32_t delta)
88 {
89         Mappings::iterator tm = _mappings.find(t);
90         if (tm != _mappings.end()) {
91                 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
92                         m->second += delta;
93                 }
94         }
95 }
96
97 } // namespace ARDOUR
98
99 std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanMapping& cm)
100 {
101         for (ARDOUR::ChanMapping::Mappings::const_iterator tm = cm.mappings().begin();
102                         tm != cm.mappings().end(); ++tm) {
103                 o << tm->first.to_string() << endl;
104                 for (ARDOUR::ChanMapping::TypeMapping::const_iterator i = tm->second.begin();
105                                 i != tm->second.end(); ++i) {
106                         o << "\t" << i->first << " => " << i->second << endl;
107                 }
108         }
109
110         return o;
111 }
112