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