OSC: Catch new strips, gone strips and redo banks and observers. Add more select...
[ardour.git] / libs / surfaces / osc / osc_select_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 #include "ardour/phase_control.h"
28 #include "ardour/solo_isolate_control.h"
29 #include "ardour/solo_safe_control.h"
30 #include "ardour/route.h"
31
32 #include "osc.h"
33 #include "osc_select_observer.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace PBD;
39 using namespace ARDOUR;
40 using namespace ArdourSurface;
41
42 OSCSelectObserver::OSCSelectObserver (boost::shared_ptr<Stripable> s, lo_address a, uint32_t ss, uint32_t gm, std::bitset<32> fb)
43         : _strip (s)
44         ,ssid (ss)
45         ,gainmode (gm)
46         ,feedback (fb)
47         ,nsends (0)
48 {
49         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
50
51         if (feedback[0]) { // buttons are separate feedback
52                 _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::name_changed, this, boost::lambda::_1), OSC::instance());
53                 name_changed (ARDOUR::Properties::name);
54
55                 _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/mute"), _strip->mute_control()), OSC::instance());
56                 change_message ("/select/mute", _strip->mute_control());
57
58                 _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/solo"), _strip->solo_control()), OSC::instance());
59                 change_message ("/select/solo", _strip->solo_control());
60
61                 _strip->solo_isolate_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/solo_iso"), _strip->solo_isolate_control()), OSC::instance());
62                 change_message ("/select/solo_iso", _strip->solo_isolate_control());
63
64                 _strip->solo_safe_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/solo_safe"), _strip->solo_safe_control()), OSC::instance());
65                 change_message ("/select/solo_safe", _strip->solo_safe_control());
66
67                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
68                 if (track) {
69                 track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::monitor_status, this, track->monitoring_control()), OSC::instance());
70                 monitor_status (track->monitoring_control());
71                 }
72
73                 boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
74                 if (rec_controllable) {
75                         rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/recenable"), _strip->rec_enable_control()), OSC::instance());
76                         change_message ("/select/recenable", _strip->rec_enable_control());
77                 }
78
79                 boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
80                 if (recsafe_controllable) {
81                         recsafe_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/record_safe"), _strip->rec_safe_control()), OSC::instance());
82                         change_message ("/select/record_safe", _strip->rec_safe_control());
83                 }
84
85                 boost::shared_ptr<AutomationControl> phase_controllable = _strip->phase_control ();
86                 if (phase_controllable) {
87                         phase_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/polarity"), _strip->phase_control()), OSC::instance());
88                         change_message ("/select/polarity", _strip->phase_control());
89                 }
90
91         }
92
93         if (feedback[1]) { // level controls
94                 if (gainmode) {
95                         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_message, this, X_("/select/fader"), _strip->gain_control()), OSC::instance());
96                         gain_message ("/select/fader", _strip->gain_control());
97                 } else {
98                         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_message, this, X_("/select/gain"), _strip->gain_control()), OSC::instance());
99                         gain_message ("/select/gain", _strip->gain_control());
100                 }
101
102                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
103                 if (trim_controllable) {
104                         trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::trim_message, this, X_("/select/trimdB"), _strip->trim_control()), OSC::instance());
105                         trim_message ("/select/trimdB", _strip->trim_control());
106                 }
107
108                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
109                 if (pan_controllable) {
110                         pan_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_stereo_position"), _strip->pan_azimuth_control()), OSC::instance());
111                         change_message ("/select/pan_stereo_position", _strip->pan_azimuth_control());
112                 }
113
114                 boost::shared_ptr<Controllable> width_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_width_control());
115                 if (width_controllable) {
116                         width_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_stereo_width"), _strip->pan_width_control()), OSC::instance());
117                         change_message ("/select/pan_stereo_width", _strip->pan_width_control());
118                 }
119
120                 // detecting processor changes requires cast to route
121                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(_strip);
122                 r->processors_changed.connect  (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_restart, this, -1), OSC::instance());
123                 send_init();
124         }
125         tick();
126 }
127
128 OSCSelectObserver::~OSCSelectObserver ()
129 {
130
131         strip_connections.drop_connections ();
132         // all strip buttons should be off and faders 0 and etc.
133         if (feedback[0]) { // buttons are separate feedback
134                 lo_message msg = lo_message_new ();
135                 // name is a string do it first
136                 string path = "/select/name";
137                 lo_message_add_string (msg, " ");
138                 lo_send_message (addr, path.c_str(), msg);
139                 lo_message_free (msg);
140                 clear_strip ("/select/mute", 0);
141                 clear_strip ("/select/solo", 0);
142                 clear_strip ("/select/recenable", 0);
143                 clear_strip ("/select/record_safe", 0);
144                 clear_strip ("/select/monitor_input", 0);
145                 clear_strip ("/select/monitor_disk", 0);
146                 clear_strip ("/select/polarity", 0);
147         }
148         if (feedback[1]) { // level controls
149                 if (gainmode) {
150                         clear_strip ("/select/fader", 0);
151                 } else {
152                         clear_strip ("/select/gain", -193);
153                 }
154                 clear_strip ("/select/trimdB", 0);
155                 clear_strip ("/select/pan_stereo_position", 0.5);
156                 clear_strip ("/select/pan_stereo_width", 1);
157         }
158         if (feedback[9]) {
159                 clear_strip ("/select/signal", 0);
160         }
161         if (feedback[7]) {
162                 if (gainmode) {
163                         clear_strip ("/select/meter", 0);
164                 } else {
165                         clear_strip ("/select/meter", -193);
166                 }
167         }else if (feedback[8]) {
168                 clear_strip ("/select/meter", 0);
169         }
170         send_end();
171
172         lo_address_free (addr);
173 }
174
175 void
176 OSCSelectObserver::send_init()
177 {
178         // we don't know how many there are, so find out.
179         bool sends;
180         do {
181                 sends = false;
182                 if (_strip->send_level_controllable (nsends)) {
183                         _strip->send_level_controllable(nsends)->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_gain, this, nsends, _strip->send_level_controllable(nsends)), OSC::instance());
184                         send_gain (nsends, _strip->send_level_controllable(nsends));
185                         sends = true;
186                 }
187
188                 if (_strip->send_enable_controllable (nsends)) {
189                         _strip->send_enable_controllable(nsends)->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_enable, this, X_("/select/send_enable"), nsends, _strip->send_enable_controllable(nsends)), OSC::instance());
190                         send_enable ("/select/send_enable", nsends, _strip->send_enable_controllable(nsends));
191                         sends = true;
192                 } else if (sends) {
193                         // not used by Ardour, just mixbus so in Ardour always true
194                         lo_message msg = lo_message_new ();
195                         path = "/select/send_enable";
196                         if (feedback[2]) {
197                                 path = set_path (path, nsends + 1);
198                         } else {
199                                 lo_message_add_int32 (msg, nsends + 1);
200                         }
201                         lo_message_add_int32 (msg, 1);
202                         lo_send_message (addr, path.c_str(), msg);
203                         lo_message_free (msg);
204                 }
205                 // this should get signalled by the route the send goes to, (TODO)
206                 if (sends) { // if the gain control is there, this is too
207                         send_name ("/select/send_name", nsends, _strip->send_name(nsends));
208                 }
209                 // Send numbers are 0 based, OSC is 1 based so this gets incremented at the end
210                 if (sends) {
211                         nsends++;
212                 }
213         } while (sends);
214 }
215
216 void
217 OSCSelectObserver::send_end ()
218 {
219         send_connections.drop_connections ();
220         for (uint32_t i = 1; i <= nsends; i++) {
221                 lo_message msg = lo_message_new ();
222                 string path = "/select/send_gain";
223                 if (feedback[2]) {
224                         path = set_path (path, i);
225                 } else {
226                         lo_message_add_int32 (msg, i);
227                 }
228
229                 if (gainmode) {
230                         lo_message_add_int32 (msg, 0);
231                 } else {
232                         lo_message_add_float (msg, -193);
233                 }
234                 lo_send_message (addr, path.c_str(), msg);
235                 lo_message_free (msg);
236                 // next enable
237                 msg = lo_message_new ();
238                 path = "/select/send_enable";
239                 if (feedback[2]) {
240                         path = set_path (path, i);
241                 } else {
242                         lo_message_add_int32 (msg, i);
243                 }
244                 lo_message_add_int32 (msg, 0);
245                 lo_send_message (addr, path.c_str(), msg);
246                 lo_message_free (msg);
247                 // next name
248                 msg = lo_message_new ();
249                 path = "/select/send_name";
250                 if (feedback[2]) {
251                         path = set_path (path, i);
252                 } else {
253                         lo_message_add_int32 (msg, i);
254                 }
255                 lo_message_add_string (msg, " ");
256                 lo_send_message (addr, path.c_str(), msg);
257                 lo_message_free (msg);
258         }
259         nsends = 0;
260 }
261
262 void
263 OSCSelectObserver::send_restart(int x)
264 {
265         send_end();
266         send_init();
267 }
268
269 void
270 OSCSelectObserver::tick ()
271 {
272         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
273
274                 float now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
275                 if (now_meter < -193) now_meter = -193;
276                 if (_last_meter != now_meter) {
277                         if (feedback[7] || feedback[8]) {
278                                 string path = "/select/meter";
279                                 lo_message msg = lo_message_new ();
280                                 if (gainmode && feedback[7]) {
281                                         uint32_t lev1023 = (uint32_t)((now_meter + 54) * 17.05);
282                                         lo_message_add_int32 (msg, lev1023);
283                                         lo_send_message (addr, path.c_str(), msg);
284                                 } else if ((!gainmode) && feedback[7]) {
285                                         lo_message_add_float (msg, now_meter);
286                                         lo_send_message (addr, path.c_str(), msg);
287                                 } else if (feedback[8]) {
288                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
289                                         uint16_t ledbits = ~(0xfff<<ledlvl);
290                                         lo_message_add_int32 (msg, ledbits);
291                                         lo_send_message (addr, path.c_str(), msg);
292                                 }
293                                 lo_message_free (msg);
294                         }
295                         if (feedback[9]) {
296                                 string path = "/select/signal";
297                                 lo_message msg = lo_message_new ();
298                                 float signal;
299                                 if (now_meter < -40) {
300                                         signal = 0;
301                                 } else {
302                                         signal = 1;
303                                 }
304                                 lo_message_add_float (msg, signal);
305                                 lo_send_message (addr, path.c_str(), msg);
306                                 lo_message_free (msg);
307                         }
308                 }
309                 _last_meter = now_meter;
310
311         }
312
313 }
314
315 void
316 OSCSelectObserver::name_changed (const PBD::PropertyChange& what_changed)
317 {
318         if (!what_changed.contains (ARDOUR::Properties::name)) {
319             return;
320         }
321
322         if (!_strip) {
323                 return;
324         }
325
326         lo_message msg = lo_message_new ();
327
328         string path = "/select/name";
329         lo_message_add_string (msg, _strip->name().c_str());
330
331         lo_send_message (addr, path.c_str(), msg);
332         lo_message_free (msg);
333
334         //spit out the comment at the same time
335         msg = lo_message_new ();
336         path = "/select/comment";
337         boost::shared_ptr<Route> route = boost::dynamic_pointer_cast<Route> (_strip);
338         lo_message_add_string (msg, route->comment().c_str());
339         lo_send_message (addr, path.c_str(), msg);
340         lo_message_free (msg);
341
342         // lets tell the surface how many inputs this strip has
343         msg = lo_message_new ();
344         path = "/select/n_inputs";
345         lo_message_add_int32 (msg, route->n_inputs().n_total());
346         lo_send_message (addr, path.c_str(), msg);
347         lo_message_free (msg);
348         // lets tell the surface how many outputs this strip has
349         msg = lo_message_new ();
350         path = "/select/n_outputs";
351         lo_message_add_int32 (msg, route->n_outputs().n_total());
352         lo_send_message (addr, path.c_str(), msg);
353         lo_message_free (msg);
354
355 }
356
357 void
358 OSCSelectObserver::change_message (string path, boost::shared_ptr<Controllable> controllable)
359 {
360         lo_message msg = lo_message_new ();
361
362         lo_message_add_float (msg, (float) controllable->get_value());
363
364         lo_send_message (addr, path.c_str(), msg);
365         lo_message_free (msg);
366 }
367
368 void
369 OSCSelectObserver::monitor_status (boost::shared_ptr<Controllable> controllable)
370 {
371         int disk, input;
372         float val = controllable->get_value();
373         switch ((int) val) {
374                 case 1:
375                         disk = 0;
376                         input = 1;
377                         break;
378                 case 2:
379                         disk = 1;
380                         input = 0;
381                         break;
382                 default:
383                         disk = 0;
384                         input = 0;
385         }
386
387         lo_message msg = lo_message_new ();
388         string path = "/select/monitor_input";
389         lo_message_add_int32 (msg, (float) input);
390         lo_send_message (addr, path.c_str(), msg);
391         lo_message_free (msg);
392
393         msg = lo_message_new ();
394         path = "/select/monitor_disk";
395         lo_message_add_int32 (msg, (float) disk);
396         lo_send_message (addr, path.c_str(), msg);
397         lo_message_free (msg);
398
399 }
400
401 void
402 OSCSelectObserver::trim_message (string path, boost::shared_ptr<Controllable> controllable)
403 {
404         lo_message msg = lo_message_new ();
405
406         lo_message_add_float (msg, (float) accurate_coefficient_to_dB (controllable->get_value()));
407
408         lo_send_message (addr, path.c_str(), msg);
409         lo_message_free (msg);
410 }
411
412 void
413 OSCSelectObserver::gain_message (string path, boost::shared_ptr<Controllable> controllable)
414 {
415         lo_message msg = lo_message_new ();
416
417         if (gainmode) {
418                 if (controllable->get_value() == 1) {
419                         lo_message_add_int32 (msg, 800);
420                 } else {
421                         lo_message_add_int32 (msg, gain_to_slider_position (controllable->get_value()) * 1023);
422                 }
423         } else {
424                 if (controllable->get_value() < 1e-15) {
425                         lo_message_add_float (msg, -200);
426                 } else {
427                         lo_message_add_float (msg, accurate_coefficient_to_dB (controllable->get_value()));
428                 }
429         }
430
431         lo_send_message (addr, path.c_str(), msg);
432         lo_message_free (msg);
433 }
434
435 void
436 OSCSelectObserver::send_gain (uint32_t id, boost::shared_ptr<PBD::Controllable> controllable)
437 {
438         lo_message msg = lo_message_new ();
439         string path;
440         float value;
441
442         if (gainmode) {
443                 path = "/select/send_fader";
444                 if (controllable->get_value() == 1) {
445                         value = 800;
446                 } else {
447                         value = gain_to_slider_position (controllable->get_value());
448                 }
449         } else {
450                 path = "/select/send_gain";
451                 if (controllable->get_value() < 1e-15) {
452                         value = -193;
453                 } else {
454                         value = accurate_coefficient_to_dB (controllable->get_value());
455                 }
456         }
457
458         if (feedback[2]) {
459                 path = set_path (path, id + 1);
460         } else {
461                 lo_message_add_int32 (msg, id + 1);
462         }
463
464         lo_message_add_float (msg, value);
465         lo_send_message (addr, path.c_str(), msg);
466         lo_message_free (msg);
467 }
468
469 void
470 OSCSelectObserver::send_enable (string path, uint32_t id, boost::shared_ptr<Controllable> controllable)
471 {
472         lo_message msg = lo_message_new ();
473         if (feedback[2]) {
474                 path = set_path (path, id + 1);
475         } else {
476                 lo_message_add_int32 (msg, id + 1);
477         }
478
479         lo_message_add_float (msg, (float) controllable->get_value());
480
481         lo_send_message (addr, path.c_str(), msg);
482         lo_message_free (msg);
483 }
484
485 void
486 OSCSelectObserver::send_name (string path, uint32_t id, string name)
487 {
488         lo_message msg = lo_message_new ();
489         if (feedback[2]) {
490                 path = set_path (path, id + 1);
491         } else {
492                 lo_message_add_int32 (msg, id + 1);
493         }
494
495         lo_message_add_string (msg, name.c_str());
496
497         lo_send_message (addr, path.c_str(), msg);
498         lo_message_free (msg);
499 }
500
501 string
502 OSCSelectObserver::set_path (string path, uint32_t id)
503 {
504         if (feedback[2]) {
505   ostringstream os;
506   os << path << "/" << id;
507   path = os.str();
508         }
509         return path;
510 }
511
512 void
513 OSCSelectObserver::clear_strip (string path, float val)
514 {
515         lo_message msg = lo_message_new ();
516         lo_message_add_float (msg, val);
517
518         lo_send_message (addr, path.c_str(), msg);
519         lo_message_free (msg);
520
521 }
522