move ControllableDescriptor from libpbd to libardour; add support for describing...
[ardour.git] / libs / ardour / controllable_descriptor.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "pbd/strsplit.h"
20 #include "pbd/convert.h"
21
22 #include "ardour/controllable_descriptor.h"
23
24 using namespace std;
25 using namespace PBD;
26 using namespace ARDOUR;
27
28 int
29 ControllableDescriptor::set (const std::string& str)
30 {
31         string::size_type first_space = str.find_first_of (" ");
32
33         if (first_space == string::npos) {
34                 return -1;
35         }
36
37         string front = str.substr (0, first_space);
38         string back = str.substr (first_space);
39
40         vector<string> path;
41         split (front, path, '/');
42
43         if (path.size() < 2) {
44                 return -1;
45         }
46
47         vector<string> rest;
48         split (back, rest, ' ');
49
50         if (rest.size() < 1) {
51                 return -1;
52         }
53
54         if (path[0] == "route" || path[0] == "rid") {
55
56                 _top_level_type = PresentationOrderRoute;
57
58                 if (rest[0][0] == 'B') {
59                         _banked = true;
60                         _presentation_order = atoi (rest[0].substr (1));
61                 } else if (rest[0][0] == 'S') {
62                         _top_level_type = SelectionCount;
63                         _banked = true;
64                         _selection_id = atoi (rest[0].substr (1));
65                 } else if (isdigit (rest[0][0])) {
66                         _banked = false;
67                         _presentation_order = atoi (rest[0]);
68                 } else {
69                         return -1;
70                 }
71
72         } else if (path[0] == "vca") {
73
74                 _top_level_type = PresentationOrderVCA;
75
76                 if (rest[0][0] == 'B') {
77                         _banked = true;
78                         _presentation_order = atoi (rest[0].substr (1));
79                 } else if (rest[0][0] == 'S') {
80                         _top_level_type = SelectionCount;
81                         _banked = true;
82                         _selection_id = atoi (rest[0].substr (1));
83                 } else if (isdigit (rest[0][0])) {
84                         _banked = false;
85                         _presentation_order = atoi (rest[0]);
86                 } else {
87                         return -1;
88                 }
89
90         } else if (path[0] == "bus" || path[0] == "track") {
91
92                 _top_level_type = NamedRoute;
93                 _top_level_name = rest[0];
94         }
95
96         if (path[1] == "gain") {
97                 _subtype = Gain;
98
99         } else if (path[1] == "trim") {
100                 _subtype = Trim;
101
102         } else if (path[1] == "solo") {
103                 _subtype = Solo;
104
105         } else if (path[1] == "mute") {
106                 _subtype = Mute;
107
108         } else if (path[1] == "recenable") {
109                 _subtype = Recenable;
110
111         } else if (path[1] == "balance") {
112                 _subtype = Balance;
113
114         } else if (path[1] == "panwidth") {
115                 _subtype = PanWidth;
116
117         } else if (path[1] == "pandirection") {
118                 _subtype = PanDirection;
119
120         } else if (path[1] == "plugin") {
121                 if (path.size() == 3 && rest.size() == 3) {
122                         if (path[2] == "parameter") {
123                                 _subtype = PluginParameter;
124                                 _target.push_back (atoi (rest[1]));
125                                 _target.push_back (atoi (rest[2]));
126                         } else {
127                                 return -1;
128                         }
129                 } else {
130                         return -1;
131                 }
132         } else if (path[1] == "send") {
133
134                 if (path.size() == 3 && rest.size() == 2) {
135                         if (path[2] == "gain") {
136                                 _subtype = SendGain;
137                                 _target.push_back (atoi (rest[1]));
138                         } else {
139                                 return -1;
140                         }
141                 } else {
142                         return -1;
143                 }
144         }
145
146         return 0;
147 }
148
149 uint32_t
150 ControllableDescriptor::presentation_order () const
151 {
152         if (banked()) {
153                 return _presentation_order + _bank_offset;
154         }
155
156         return _presentation_order;
157 }
158
159 uint32_t
160 ControllableDescriptor::selection_id () const
161 {
162         if (banked()) {
163                 return _selection_id + _bank_offset;
164         }
165
166         return _selection_id;
167 }
168
169 uint32_t
170 ControllableDescriptor::target (uint32_t n) const
171 {
172         if (n < _target.size()) {
173                 return _target[n];
174         }
175
176         return 0;
177 }