OSC: Added feedback for /select
[ardour.git] / libs / surfaces / osc / osc_route_observer.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
20 #include "boost/lambda/lambda.hpp"
21
22 #include "ardour/session.h"
23 #include "ardour/track.h"
24 #include "ardour/monitor_control.h"
25 #include "ardour/dB.h"
26 #include "ardour/meter.h"
27
28 #include "osc.h"
29 #include "osc_route_observer.h"
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace PBD;
35 using namespace ARDOUR;
36 using namespace ArdourSurface;
37
38 OSCRouteObserver::OSCRouteObserver (boost::shared_ptr<Stripable> s, lo_address a, uint32_t ss, uint32_t gm, std::bitset<32> fb)
39         : _strip (s)
40         ,ssid (ss)
41         ,gainmode (gm)
42         ,feedback (fb)
43 {
44         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
45
46         if (feedback[0]) { // buttons are separate feedback
47                 _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::name_changed, this, boost::lambda::_1), OSC::instance());
48                 name_changed (ARDOUR::Properties::name);
49
50                 _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/mute"), _strip->mute_control()), OSC::instance());
51                 send_change_message ("/strip/mute", _strip->mute_control());
52
53                 _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/solo"), _strip->solo_control()), OSC::instance());
54                 send_change_message ("/strip/solo", _strip->solo_control());
55
56                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
57                 if (track) {
58                 track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_monitor_status, this, track->monitoring_control()), OSC::instance());
59                 send_monitor_status (track->monitoring_control());
60                 }
61
62                 boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
63                 if (rec_controllable) {
64                         rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/recenable"), _strip->rec_enable_control()), OSC::instance());
65                         send_change_message ("/strip/recenable", _strip->rec_enable_control());
66                 }
67                 boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
68                 if (rec_controllable) {
69                         recsafe_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/record_safe"), _strip->rec_safe_control()), OSC::instance());
70                         send_change_message ("/strip/record_safe", _strip->rec_safe_control());
71                 }
72                 send_select_status ();
73         }
74
75         if (feedback[1]) { // level controls
76                 if (gainmode) {
77                         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_gain_message, this, X_("/strip/fader"), _strip->gain_control()), OSC::instance());
78                         send_gain_message ("/strip/fader", _strip->gain_control());
79                 } else {
80                         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_gain_message, this, X_("/strip/gain"), _strip->gain_control()), OSC::instance());
81                         send_gain_message ("/strip/gain", _strip->gain_control());
82                 }
83
84                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
85                 if (trim_controllable) {
86                         trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_trim_message, this, X_("/strip/trimdB"), _strip->trim_control()), OSC::instance());
87                         send_trim_message ("/strip/trimdB", _strip->trim_control());
88                 }
89
90                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
91                 if (pan_controllable) {
92                         pan_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/pan_stereo_position"), _strip->pan_azimuth_control()), OSC::instance());
93                         send_change_message ("/strip/pan_stereo_position", _strip->pan_azimuth_control());
94                 }
95         }
96         tick();
97 }
98
99 OSCRouteObserver::~OSCRouteObserver ()
100 {
101
102         strip_connections.drop_connections ();
103         // all strip buttons should be off and faders 0 and etc.
104         if (feedback[0]) { // buttons are separate feedback
105                 lo_message msg = lo_message_new ();
106                 // name is a string do it first
107                 string path = "/strip/name";
108                 if (feedback[2]) {
109                         path = set_path (path);
110                 } else {
111                         lo_message_add_int32 (msg, ssid);
112                 }
113                 lo_message_add_string (msg, " ");
114
115                 lo_send_message (addr, path.c_str(), msg);
116                 lo_message_free (msg);
117                 clear_strip ("/strip/mute", 0);
118                 clear_strip ("/strip/solo", 0);
119                 clear_strip ("/strip/recenable", 0);
120                 clear_strip ("/strip/record_safe", 0);
121                 clear_strip ("/strip/monitor_input", 0);
122                 clear_strip ("/strip/monitor_disk", 0);
123                 clear_strip ("/strip/gui_select", 0);
124                 clear_strip ("/strip/select", 0);
125         }
126         if (feedback[1]) { // level controls
127                 if (gainmode) {
128                         clear_strip ("/strip/fader", 0);
129                 } else {
130                         clear_strip ("/strip/gain", -193);
131                 }
132                 clear_strip ("/strip/trimdB", 0);
133                 clear_strip ("/strip/pan_stereo_position", 0.5);
134         }
135         if (feedback[9]) {
136                 clear_strip ("/strip/signal", 0);
137         }
138         if (feedback[7]) {
139                 if (gainmode) {
140                         clear_strip ("/strip/meter", 0);
141                 } else {
142                         clear_strip ("/strip/meter", -193);
143                 }
144         }else if (feedback[8]) {
145                 clear_strip ("/strip/meter", 0);
146         }
147
148         lo_address_free (addr);
149 }
150
151 void
152 OSCRouteObserver::tick ()
153 {
154         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
155                 // the only meter here is master
156                 float now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
157                 if (now_meter < -193) now_meter = -193;
158                 if (_last_meter != now_meter) {
159                         if (feedback[7] || feedback[8]) {
160                                 string path = "/strip/meter";
161                                 lo_message msg = lo_message_new ();
162                                 if (feedback[2]) {
163                                         path = set_path (path);
164                                 } else {
165                                         lo_message_add_int32 (msg, ssid);
166                                 }
167                                 if (gainmode && feedback[7]) {
168                                         uint32_t lev1023 = (uint32_t)((now_meter + 54) * 17.05);
169                                         lo_message_add_int32 (msg, lev1023);
170                                         lo_send_message (addr, path.c_str(), msg);
171                                 } else if ((!gainmode) && feedback[7]) {
172                                         lo_message_add_float (msg, now_meter);
173                                         lo_send_message (addr, path.c_str(), msg);
174                                 } else if (feedback[8]) {
175                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
176                                         uint16_t ledbits = ~(0xfff<<ledlvl);
177                                         lo_message_add_int32 (msg, ledbits);
178                                         lo_send_message (addr, path.c_str(), msg);
179                                 }
180                                 lo_message_free (msg);
181                         }
182                         if (feedback[9]) {
183                                 string path = "/strip/signal";
184                                 lo_message msg = lo_message_new ();
185                                 if (feedback[2]) {
186                                         path = set_path (path);
187                                 } else {
188                                         lo_message_add_int32 (msg, ssid);
189                                 }
190                                 float signal;
191                                 if (now_meter < -40) {
192                                         signal = 0;
193                                 } else {
194                                         signal = 1;
195                                 }
196                                 lo_message_add_float (msg, signal);
197                                 lo_send_message (addr, path.c_str(), msg);
198                                 lo_message_free (msg);
199                         }
200                 }
201                 _last_meter = now_meter;
202
203         }
204
205 }
206
207 void
208 OSCRouteObserver::name_changed (const PBD::PropertyChange& what_changed)
209 {
210         if (!what_changed.contains (ARDOUR::Properties::name)) {
211             return;
212         }
213
214         if (!_strip) {
215                 return;
216         }
217
218         lo_message msg = lo_message_new ();
219
220         // ssid is the strip on the surface this observer refers to
221         // not part of the internal ordering.
222         string path = "/strip/name";
223         if (feedback[2]) {
224                 path = set_path (path);
225         } else {
226                 lo_message_add_int32 (msg, ssid);
227         }
228         lo_message_add_string (msg, _strip->name().c_str());
229
230         lo_send_message (addr, path.c_str(), msg);
231         lo_message_free (msg);
232 }
233
234 void
235 OSCRouteObserver::send_change_message (string path, boost::shared_ptr<Controllable> controllable)
236 {
237         lo_message msg = lo_message_new ();
238
239         if (feedback[2]) {
240                 path = set_path (path);
241         } else {
242                 lo_message_add_int32 (msg, ssid);
243         }
244         lo_message_add_float (msg, (float) controllable->get_value());
245
246         lo_send_message (addr, path.c_str(), msg);
247         lo_message_free (msg);
248 }
249
250 void
251 OSCRouteObserver::send_monitor_status (boost::shared_ptr<Controllable> controllable)
252 {
253         int disk, input;
254         float val = controllable->get_value();
255         switch ((int) val) {
256                 case 1:
257                         disk = 0;
258                         input = 1;
259                         break;
260                 case 2:
261                         disk = 1;
262                         input = 0;
263                         break;
264                 default:
265                         disk = 0;
266                         input = 0;
267         }
268
269         lo_message msg = lo_message_new ();
270         string path = "/strip/monitor_input";
271         if (feedback[2]) {
272                 path = set_path (path);
273         } else {
274                 lo_message_add_int32 (msg, ssid);
275         }
276         lo_message_add_int32 (msg, (float) input);
277         lo_send_message (addr, path.c_str(), msg);
278         lo_message_free (msg);
279
280         msg = lo_message_new ();
281         path = "/strip/monitor_disk";
282         if (feedback[2]) {
283                 path = set_path (path);
284         } else {
285                 lo_message_add_int32 (msg, ssid);
286         }
287         lo_message_add_int32 (msg, (float) disk);
288         lo_send_message (addr, path.c_str(), msg);
289         lo_message_free (msg);
290
291 }
292
293 void
294 OSCRouteObserver::send_trim_message (string path, boost::shared_ptr<Controllable> controllable)
295 {
296         lo_message msg = lo_message_new ();
297
298         if (feedback[2]) {
299                 path = set_path (path);
300         } else {
301                 lo_message_add_int32 (msg, ssid);
302         }
303
304         lo_message_add_float (msg, (float) accurate_coefficient_to_dB (controllable->get_value()));
305
306         lo_send_message (addr, path.c_str(), msg);
307         lo_message_free (msg);
308 }
309
310 void
311 OSCRouteObserver::send_gain_message (string path, boost::shared_ptr<Controllable> controllable)
312 {
313         lo_message msg = lo_message_new ();
314
315         if (feedback[2]) {
316                 path = set_path (path);
317         } else {
318                 lo_message_add_int32 (msg, ssid);
319         }
320
321         if (gainmode) {
322                 if (controllable->get_value() == 1) {
323                         lo_message_add_int32 (msg, 800);
324                 } else {
325                         lo_message_add_int32 (msg, gain_to_slider_position (controllable->get_value()) * 1023);
326                 }
327         } else {
328                 if (controllable->get_value() < 1e-15) {
329                         lo_message_add_float (msg, -200);
330                 } else {
331                         lo_message_add_float (msg, accurate_coefficient_to_dB (controllable->get_value()));
332                 }
333         }
334
335         lo_send_message (addr, path.c_str(), msg);
336         lo_message_free (msg);
337 }
338
339 string
340 OSCRouteObserver::set_path (string path)
341 {
342         if (feedback[2]) {
343   ostringstream os;
344   os << path << "/" << ssid;
345   path = os.str();
346         }
347         return path;
348 }
349
350 void
351 OSCRouteObserver::clear_strip (string path, float val)
352 {
353         lo_message msg = lo_message_new ();
354         if (feedback[2]) {
355                 path = set_path (path);
356         } else {
357                 lo_message_add_int32 (msg, ssid);
358         }
359         lo_message_add_float (msg, val);
360
361         lo_send_message (addr, path.c_str(), msg);
362         lo_message_free (msg);
363
364 }
365
366 void
367 OSCRouteObserver::send_select_status ()
368 {
369         // waiting for _strip->is_selected to start working
370         if (_strip) {
371                 string path = "/strip/gui_select";
372
373                 lo_message msg = lo_message_new ();
374                 if (feedback[2]) {
375                         path = set_path (path);
376                 } else {
377                         lo_message_add_int32 (msg, ssid);
378                 }
379                 //std::cout << "strip: " << ssid << " strip name: " << _strip->name() << " select: " << _strip->is_selected() << "\n";
380                 lo_message_add_float (msg, _strip->is_selected());
381                 lo_send_message (addr, path.c_str(), msg);
382                 lo_message_free (msg);
383         }
384 }