OSC: Add strip type selection.
[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                 lo_message_add_string (msg, " ");
137                 lo_send_message (addr, "/select/name", msg);
138                 lo_message_free (msg);
139                 msg = lo_message_new ();
140                 lo_message_add_string (msg, " ");
141                 lo_send_message (addr, "/select/comment", msg);
142                 lo_message_free (msg);
143                 clear_strip ("/select/mute", 0);
144                 clear_strip ("/select/solo", 0);
145                 clear_strip ("/select/recenable", 0);
146                 clear_strip ("/select/record_safe", 0);
147                 clear_strip ("/select/monitor_input", 0);
148                 clear_strip ("/select/monitor_disk", 0);
149                 clear_strip ("/select/polarity", 0);
150                 clear_strip ("/select/n_inputs", 0);
151                 clear_strip ("/select/n_outputs", 0);
152         }
153         if (feedback[1]) { // level controls
154                 if (gainmode) {
155                         clear_strip ("/select/fader", 0);
156                 } else {
157                         clear_strip ("/select/gain", -193);
158                 }
159                 clear_strip ("/select/trimdB", 0);
160                 clear_strip ("/select/pan_stereo_position", 0.5);
161                 clear_strip ("/select/pan_stereo_width", 1);
162         }
163         if (feedback[9]) {
164                 clear_strip ("/select/signal", 0);
165         }
166         if (feedback[7]) {
167                 if (gainmode) {
168                         clear_strip ("/select/meter", 0);
169                 } else {
170                         clear_strip ("/select/meter", -193);
171                 }
172         }else if (feedback[8]) {
173                 clear_strip ("/select/meter", 0);
174         }
175         send_end();
176
177         lo_address_free (addr);
178 }
179
180 void
181 OSCSelectObserver::send_init()
182 {
183         // we don't know how many there are, so find out.
184         bool sends;
185         do {
186                 sends = false;
187                 if (_strip->send_level_controllable (nsends)) {
188                         _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());
189                         send_gain (nsends, _strip->send_level_controllable(nsends));
190                         sends = true;
191                 }
192
193                 if (_strip->send_enable_controllable (nsends)) {
194                         _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());
195                         send_enable ("/select/send_enable", nsends, _strip->send_enable_controllable(nsends));
196                         sends = true;
197                 } else if (sends) {
198                         // not used by Ardour, just mixbus so in Ardour always true
199                         lo_message msg = lo_message_new ();
200                         path = "/select/send_enable";
201                         if (feedback[2]) {
202                                 path = set_path (path, nsends + 1);
203                         } else {
204                                 lo_message_add_int32 (msg, nsends + 1);
205                         }
206                         lo_message_add_int32 (msg, 1);
207                         lo_send_message (addr, path.c_str(), msg);
208                         lo_message_free (msg);
209                 }
210                 // this should get signalled by the route the send goes to, (TODO)
211                 if (sends) { // if the gain control is there, this is too
212                         send_name ("/select/send_name", nsends, _strip->send_name(nsends));
213                 }
214                 // Send numbers are 0 based, OSC is 1 based so this gets incremented at the end
215                 if (sends) {
216                         nsends++;
217                 }
218         } while (sends);
219 }
220
221 void
222 OSCSelectObserver::send_end ()
223 {
224         send_connections.drop_connections ();
225         for (uint32_t i = 1; i <= nsends; i++) {
226                 lo_message msg = lo_message_new ();
227                 string path = "/select/send_gain";
228                 if (feedback[2]) {
229                         path = set_path (path, i);
230                 } else {
231                         lo_message_add_int32 (msg, i);
232                 }
233
234                 if (gainmode) {
235                         lo_message_add_int32 (msg, 0);
236                 } else {
237                         lo_message_add_float (msg, -193);
238                 }
239                 lo_send_message (addr, path.c_str(), msg);
240                 lo_message_free (msg);
241                 // next enable
242                 msg = lo_message_new ();
243                 path = "/select/send_enable";
244                 if (feedback[2]) {
245                         path = set_path (path, i);
246                 } else {
247                         lo_message_add_int32 (msg, i);
248                 }
249                 lo_message_add_int32 (msg, 0);
250                 lo_send_message (addr, path.c_str(), msg);
251                 lo_message_free (msg);
252                 // next name
253                 msg = lo_message_new ();
254                 path = "/select/send_name";
255                 if (feedback[2]) {
256                         path = set_path (path, i);
257                 } else {
258                         lo_message_add_int32 (msg, i);
259                 }
260                 lo_message_add_string (msg, " ");
261                 lo_send_message (addr, path.c_str(), msg);
262                 lo_message_free (msg);
263         }
264         nsends = 0;
265 }
266
267 void
268 OSCSelectObserver::send_restart(int x)
269 {
270         send_end();
271         send_init();
272 }
273
274 void
275 OSCSelectObserver::tick ()
276 {
277         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
278                 float now_meter;
279                 if (_strip->peak_meter()) {
280                         now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
281                 } else {
282                         now_meter = -193;
283                 }
284                 if (now_meter < -193) now_meter = -193;
285                 if (_last_meter != now_meter) {
286                         if (feedback[7] || feedback[8]) {
287                                 string path = "/select/meter";
288                                 lo_message msg = lo_message_new ();
289                                 if (gainmode && feedback[7]) {
290                                         uint32_t lev1023 = (uint32_t)((now_meter + 54) * 17.05);
291                                         lo_message_add_int32 (msg, lev1023);
292                                         lo_send_message (addr, path.c_str(), msg);
293                                 } else if ((!gainmode) && feedback[7]) {
294                                         lo_message_add_float (msg, now_meter);
295                                         lo_send_message (addr, path.c_str(), msg);
296                                 } else if (feedback[8]) {
297                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
298                                         uint16_t ledbits = ~(0xfff<<ledlvl);
299                                         lo_message_add_int32 (msg, ledbits);
300                                         lo_send_message (addr, path.c_str(), msg);
301                                 }
302                                 lo_message_free (msg);
303                         }
304                         if (feedback[9]) {
305                                 string path = "/select/signal";
306                                 lo_message msg = lo_message_new ();
307                                 float signal;
308                                 if (now_meter < -40) {
309                                         signal = 0;
310                                 } else {
311                                         signal = 1;
312                                 }
313                                 lo_message_add_float (msg, signal);
314                                 lo_send_message (addr, path.c_str(), msg);
315                                 lo_message_free (msg);
316                         }
317                 }
318                 _last_meter = now_meter;
319
320         }
321
322 }
323
324 void
325 OSCSelectObserver::name_changed (const PBD::PropertyChange& what_changed)
326 {
327         if (!what_changed.contains (ARDOUR::Properties::name)) {
328             return;
329         }
330
331         if (!_strip) {
332                 return;
333         }
334
335         lo_message msg = lo_message_new ();
336
337         string path = "/select/name";
338         lo_message_add_string (msg, _strip->name().c_str());
339
340         lo_send_message (addr, path.c_str(), msg);
341         lo_message_free (msg);
342
343         //spit out the comment at the same time
344         msg = lo_message_new ();
345         path = "/select/comment";
346         boost::shared_ptr<Route> route = boost::dynamic_pointer_cast<Route> (_strip);
347         lo_message_add_string (msg, route->comment().c_str());
348         lo_send_message (addr, path.c_str(), msg);
349         lo_message_free (msg);
350
351         // lets tell the surface how many inputs this strip has
352         msg = lo_message_new ();
353         path = "/select/n_inputs";
354         lo_message_add_int32 (msg, route->n_inputs().n_total());
355         lo_send_message (addr, path.c_str(), msg);
356         lo_message_free (msg);
357         // lets tell the surface how many outputs this strip has
358         msg = lo_message_new ();
359         path = "/select/n_outputs";
360         lo_message_add_int32 (msg, route->n_outputs().n_total());
361         lo_send_message (addr, path.c_str(), msg);
362         lo_message_free (msg);
363
364 }
365
366 void
367 OSCSelectObserver::change_message (string path, boost::shared_ptr<Controllable> controllable)
368 {
369         lo_message msg = lo_message_new ();
370
371         lo_message_add_float (msg, (float) controllable->get_value());
372
373         lo_send_message (addr, path.c_str(), msg);
374         lo_message_free (msg);
375 }
376
377 void
378 OSCSelectObserver::monitor_status (boost::shared_ptr<Controllable> controllable)
379 {
380         int disk, input;
381         float val = controllable->get_value();
382         switch ((int) val) {
383                 case 1:
384                         disk = 0;
385                         input = 1;
386                         break;
387                 case 2:
388                         disk = 1;
389                         input = 0;
390                         break;
391                 default:
392                         disk = 0;
393                         input = 0;
394         }
395
396         lo_message msg = lo_message_new ();
397         string path = "/select/monitor_input";
398         lo_message_add_int32 (msg, (float) input);
399         lo_send_message (addr, path.c_str(), msg);
400         lo_message_free (msg);
401
402         msg = lo_message_new ();
403         path = "/select/monitor_disk";
404         lo_message_add_int32 (msg, (float) disk);
405         lo_send_message (addr, path.c_str(), msg);
406         lo_message_free (msg);
407
408 }
409
410 void
411 OSCSelectObserver::trim_message (string path, boost::shared_ptr<Controllable> controllable)
412 {
413         lo_message msg = lo_message_new ();
414
415         lo_message_add_float (msg, (float) accurate_coefficient_to_dB (controllable->get_value()));
416
417         lo_send_message (addr, path.c_str(), msg);
418         lo_message_free (msg);
419 }
420
421 void
422 OSCSelectObserver::gain_message (string path, boost::shared_ptr<Controllable> controllable)
423 {
424         lo_message msg = lo_message_new ();
425
426         if (gainmode) {
427                 if (controllable->get_value() == 1) {
428                         lo_message_add_int32 (msg, 800);
429                 } else {
430                         lo_message_add_int32 (msg, gain_to_slider_position (controllable->get_value()) * 1023);
431                 }
432         } else {
433                 if (controllable->get_value() < 1e-15) {
434                         lo_message_add_float (msg, -200);
435                 } else {
436                         lo_message_add_float (msg, accurate_coefficient_to_dB (controllable->get_value()));
437                 }
438         }
439
440         lo_send_message (addr, path.c_str(), msg);
441         lo_message_free (msg);
442 }
443
444 void
445 OSCSelectObserver::send_gain (uint32_t id, boost::shared_ptr<PBD::Controllable> controllable)
446 {
447         lo_message msg = lo_message_new ();
448         string path;
449         float value;
450
451         if (gainmode) {
452                 path = "/select/send_fader";
453                 if (controllable->get_value() == 1) {
454                         value = 800;
455                 } else {
456                         value = gain_to_slider_position (controllable->get_value());
457                 }
458         } else {
459                 path = "/select/send_gain";
460                 if (controllable->get_value() < 1e-15) {
461                         value = -193;
462                 } else {
463                         value = accurate_coefficient_to_dB (controllable->get_value());
464                 }
465         }
466
467         if (feedback[2]) {
468                 path = set_path (path, id + 1);
469         } else {
470                 lo_message_add_int32 (msg, id + 1);
471         }
472
473         lo_message_add_float (msg, value);
474         lo_send_message (addr, path.c_str(), msg);
475         lo_message_free (msg);
476 }
477
478 void
479 OSCSelectObserver::send_enable (string path, uint32_t id, boost::shared_ptr<Controllable> controllable)
480 {
481         lo_message msg = lo_message_new ();
482         if (feedback[2]) {
483                 path = set_path (path, id + 1);
484         } else {
485                 lo_message_add_int32 (msg, id + 1);
486         }
487
488         lo_message_add_float (msg, (float) controllable->get_value());
489
490         lo_send_message (addr, path.c_str(), msg);
491         lo_message_free (msg);
492 }
493
494 void
495 OSCSelectObserver::send_name (string path, uint32_t id, string name)
496 {
497         lo_message msg = lo_message_new ();
498         if (feedback[2]) {
499                 path = set_path (path, id + 1);
500         } else {
501                 lo_message_add_int32 (msg, id + 1);
502         }
503
504         lo_message_add_string (msg, name.c_str());
505
506         lo_send_message (addr, path.c_str(), msg);
507         lo_message_free (msg);
508 }
509
510 string
511 OSCSelectObserver::set_path (string path, uint32_t id)
512 {
513         if (feedback[2]) {
514   ostringstream os;
515   os << path << "/" << id;
516   path = os.str();
517         }
518         return path;
519 }
520
521 void
522 OSCSelectObserver::clear_strip (string path, float val)
523 {
524         lo_message msg = lo_message_new ();
525         lo_message_add_float (msg, val);
526
527         lo_send_message (addr, path.c_str(), msg);
528         lo_message_free (msg);
529
530 }
531