OSC: Fix select fader automation play feedback as well fixes issue #7160
[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 <vector>
21 #include "boost/lambda/lambda.hpp"
22
23 #include "ardour/session.h"
24 #include "ardour/track.h"
25 #include "ardour/monitor_control.h"
26 #include "ardour/dB.h"
27 #include "ardour/meter.h"
28 #include "ardour/phase_control.h"
29 #include "ardour/solo_isolate_control.h"
30 #include "ardour/solo_safe_control.h"
31 #include "ardour/route.h"
32 #include "ardour/send.h"
33 #include "ardour/processor.h"
34
35 #include "osc.h"
36 #include "osc_select_observer.h"
37
38 #include <glibmm.h>
39
40 #include "pbd/i18n.h"
41
42 using namespace std;
43 using namespace PBD;
44 using namespace ARDOUR;
45 using namespace ArdourSurface;
46
47 OSCSelectObserver::OSCSelectObserver (boost::shared_ptr<Stripable> s, lo_address a, uint32_t gm, std::bitset<32> fb)
48         : _strip (s)
49         ,gainmode (gm)
50         ,feedback (fb)
51         ,nsends (0)
52         ,_last_gain (0.0)
53 {
54         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
55         as = ARDOUR::AutoState::Off;
56
57         if (feedback[0]) { // buttons are separate feedback
58                 _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::name_changed, this, boost::lambda::_1), OSC::instance());
59                 name_changed (ARDOUR::Properties::name);
60
61                 _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/mute"), _strip->mute_control()), OSC::instance());
62                 change_message ("/select/mute", _strip->mute_control());
63
64                 _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/solo"), _strip->solo_control()), OSC::instance());
65                 change_message ("/select/solo", _strip->solo_control());
66
67                 if (_strip->solo_isolate_control()) {
68                         _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());
69                         change_message ("/select/solo_iso", _strip->solo_isolate_control());
70                 }
71
72                 if (_strip->solo_safe_control()) {
73                         _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());
74                         change_message ("/select/solo_safe", _strip->solo_safe_control());
75                 }
76
77                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
78                 if (track) {
79                         track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::monitor_status, this, track->monitoring_control()), OSC::instance());
80                         monitor_status (track->monitoring_control());
81                 }
82
83                 boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
84                 if (rec_controllable) {
85                         rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/recenable"), _strip->rec_enable_control()), OSC::instance());
86                         change_message ("/select/recenable", _strip->rec_enable_control());
87                 }
88
89                 boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
90                 if (recsafe_controllable) {
91                         recsafe_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/record_safe"), _strip->rec_safe_control()), OSC::instance());
92                         change_message ("/select/record_safe", _strip->rec_safe_control());
93                 }
94
95                 boost::shared_ptr<AutomationControl> phase_controllable = _strip->phase_control ();
96                 if (phase_controllable) {
97                         phase_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/polarity"), _strip->phase_control()), OSC::instance());
98                         change_message ("/select/polarity", _strip->phase_control());
99                 }
100
101         }
102
103         if (feedback[1]) { // level controls
104                 boost::shared_ptr<GainControl> gain_cont = _strip->gain_control();
105                 if (gainmode) {
106                         gain_cont->alist()->automation_state_changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCSelectObserver::gain_automation, this, X_("/select/fader")), OSC::instance());
107                         gain_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_message, this, X_("/select/fader"), gain_cont), OSC::instance());
108                         gain_automation ("/select/fader");
109                 } else {
110                         gain_cont->alist()->automation_state_changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCSelectObserver::gain_automation, this, X_("/select/gain")), OSC::instance());
111                         gain_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_message, this, X_("/select/gain"), _strip->gain_control()), OSC::instance());
112                         gain_automation ("/strip/gain");
113                 }
114
115                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
116                 if (trim_controllable) {
117                         trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::trim_message, this, X_("/select/trimdB"), _strip->trim_control()), OSC::instance());
118                         trim_message ("/select/trimdB", _strip->trim_control());
119                 }
120
121                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
122                 if (pan_controllable) {
123                         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());
124                         change_message ("/select/pan_stereo_position", _strip->pan_azimuth_control());
125                 }
126
127                 boost::shared_ptr<Controllable> width_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_width_control());
128                 if (width_controllable) {
129                         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());
130                         change_message ("/select/pan_stereo_width", _strip->pan_width_control());
131                 }
132
133         }
134         if (feedback[13]) { // Well known controls
135                 // Rest of possible pan controls... Untested because I can't find a way to get them in the GUI :)
136                 if (_strip->pan_elevation_control ()) {
137                         _strip->pan_elevation_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_elevation_position"), _strip->pan_elevation_control()), OSC::instance());
138                         change_message ("/select/pan_elevation_position", _strip->pan_elevation_control());
139                 }
140                 if (_strip->pan_frontback_control ()) {
141                         _strip->pan_frontback_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_frontback_position"), _strip->pan_frontback_control()), OSC::instance());
142                         change_message ("/select/pan_frontback_position", _strip->pan_frontback_control());
143                 }
144                 if (_strip->pan_lfe_control ()) {
145                         _strip->pan_lfe_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/pan_lfe_control"), _strip->pan_lfe_control()), OSC::instance());
146                         change_message ("/select/pan_lfe_control", _strip->pan_lfe_control());
147                 }
148
149                 // sends and eq
150                 // detecting processor changes requires cast to route
151                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(_strip);
152                 if (r) {
153                         r->processors_changed.connect  (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_restart, this, -1), OSC::instance());
154                         send_init();
155                         eq_init();
156                 }
157
158                 // Compressor
159                 if (_strip->comp_enable_controllable ()) {
160                         _strip->comp_enable_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::enable_message, this, X_("/select/comp_enable"), _strip->comp_enable_controllable()), OSC::instance());
161                         enable_message ("/select/comp_enable", _strip->comp_enable_controllable());
162                 }
163                 if (_strip->comp_threshold_controllable ()) {
164                         _strip->comp_threshold_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/comp_threshold"), _strip->comp_threshold_controllable()), OSC::instance());
165                         change_message ("/select/comp_threshold", _strip->comp_threshold_controllable());
166                 }
167                 if (_strip->comp_speed_controllable ()) {
168                         _strip->comp_speed_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/comp_speed"), _strip->comp_speed_controllable()), OSC::instance());
169                         change_message ("/select/comp_speed", _strip->comp_speed_controllable());
170                 }
171                 if (_strip->comp_mode_controllable ()) {
172                         _strip->comp_mode_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::comp_mode, this), OSC::instance());
173                         comp_mode ();
174                 }
175                 if (_strip->comp_makeup_controllable ()) {
176                         _strip->comp_makeup_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/comp_makeup"), _strip->comp_makeup_controllable()), OSC::instance());
177                         change_message ("/select/comp_makeup", _strip->comp_makeup_controllable());
178                 }
179
180         }
181
182         tick();
183 }
184
185 OSCSelectObserver::~OSCSelectObserver ()
186 {
187         strip_connections.drop_connections ();
188         // all strip buttons should be off and faders 0 and etc.
189         if (feedback[0]) { // buttons are separate feedback
190                 clear_strip ("/select/expand", 0);
191                 text_message ("/select/name", " ");
192                 text_message ("/select/comment", " ");
193                 clear_strip ("/select/mute", 0);
194                 clear_strip ("/select/solo", 0);
195                 clear_strip ("/select/recenable", 0);
196                 clear_strip ("/select/record_safe", 0);
197                 clear_strip ("/select/monitor_input", 0);
198                 clear_strip ("/select/monitor_disk", 0);
199                 clear_strip ("/select/polarity", 0);
200                 clear_strip ("/select/n_inputs", 0);
201                 clear_strip ("/select/n_outputs", 0);
202         }
203         if (feedback[1]) { // level controls
204                 if (gainmode) {
205                         clear_strip ("/select/fader", 0);
206                 } else {
207                         clear_strip ("/select/gain", -193);
208                 }
209                 clear_strip ("/select/trimdB", 0);
210                 clear_strip ("/select/pan_stereo_position", 0.5);
211                 clear_strip ("/select/pan_stereo_width", 1);
212         }
213         if (feedback[9]) {
214                 clear_strip ("/select/signal", 0);
215         }
216         if (feedback[7]) {
217                 if (gainmode) {
218                         clear_strip ("/select/meter", 0);
219                 } else {
220                         clear_strip ("/select/meter", -193);
221                 }
222         }else if (feedback[8]) {
223                 clear_strip ("/select/meter", 0);
224         }
225         if (feedback[13]) { // Well known controls
226                 clear_strip ("/select/pan_elevation_position", 0);
227                 clear_strip ("/select/pan_frontback_position", .5);
228                 clear_strip ("/select/pan_lfe_control", 0);
229                 clear_strip ("/select/comp_enable", 0);
230                 clear_strip ("/select/comp_threshold", 0);
231                 clear_strip ("/select/comp_speed", 0);
232                 clear_strip ("/select/comp_mode", 0);
233                 text_message ("/select/comp_mode_name", " ");
234                 text_message ("/select/comp_speed_name", " ");
235                 clear_strip ("/select/comp_makeup", 0);
236         }
237         send_end();
238         eq_end();
239
240         lo_address_free (addr);
241 }
242
243 void
244 OSCSelectObserver::send_init()
245 {
246         // we don't know how many there are, so find out.
247         bool sends;
248         do {
249                 sends = false;
250                 if (_strip->send_level_controllable (nsends)) {
251                         _strip->send_level_controllable(nsends)->Changed.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_gain, this, nsends, _strip->send_level_controllable(nsends)), OSC::instance());
252                         send_timeout.push_back (0);
253                         send_gain (nsends, _strip->send_level_controllable(nsends));
254                         sends = true;
255                 }
256
257                 if (_strip->send_enable_controllable (nsends)) {
258                         _strip->send_enable_controllable(nsends)->Changed.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::enable_message_with_id, this, X_("/select/send_enable"), nsends + 1, _strip->send_enable_controllable(nsends)), OSC::instance());
259                         enable_message_with_id ("/select/send_enable", nsends + 1, _strip->send_enable_controllable(nsends));
260                         sends = true;
261                 } else if (sends) {
262                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (_strip);
263                         if (!r) {
264                                 // should never get here
265                                 clear_strip_with_id ("/select/send_enable", nsends + 1, 0);
266                         }
267                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(nsends));
268                         if (snd) {
269                                 boost::shared_ptr<Processor> proc = boost::dynamic_pointer_cast<Processor> (snd);
270                                 proc->ActiveChanged.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_enable, this, X_("/select/send_enable"), nsends + 1, proc), OSC::instance());
271                                 clear_strip_with_id ("/select/send_enable", nsends + 1, proc->enabled());
272                         }
273                 }
274                 // this should get signalled by the route the send goes to, (TODO)
275                 if (!gainmode && sends) { // if the gain control is there, this is too
276                         text_with_id ("/select/send_name", nsends + 1, _strip->send_name(nsends));
277                 }
278                 // Send numbers are 0 based, OSC is 1 based so this gets incremented at the end
279                 if (sends) {
280                         nsends++;
281                 }
282         } while (sends);
283 }
284
285 void
286 OSCSelectObserver::send_end ()
287 {
288         send_connections.drop_connections ();
289         for (uint32_t i = 1; i <= nsends; i++) {
290                 if (gainmode) {
291                         clear_strip_with_id ("/select/send_fader", i, 0);
292                 } else {
293                         clear_strip_with_id ("/select/send_gain", i, -193);
294                 }
295                 // next enable
296                 clear_strip_with_id ("/select/send_enable", i, 0);
297                 // next name
298                 text_with_id ("/select/send_name", i, " ");
299         }
300         nsends = 0;
301 }
302
303 void
304 OSCSelectObserver::send_restart(int x)
305 {
306         send_end();
307         send_init();
308 }
309
310 void
311 OSCSelectObserver::tick ()
312 {
313         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
314                 float now_meter;
315                 if (_strip->peak_meter()) {
316                         now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
317                 } else {
318                         now_meter = -193;
319                 }
320                 if (now_meter < -144) now_meter = -193;
321                 if (_last_meter != now_meter) {
322                         if (feedback[7] || feedback[8]) {
323                                 string path = "/select/meter";
324                                 lo_message msg = lo_message_new ();
325                                 if (gainmode && feedback[7]) {
326                                         lo_message_add_float (msg, ((now_meter + 94) / 100));
327                                         lo_send_message (addr, path.c_str(), msg);
328                                 } else if ((!gainmode) && feedback[7]) {
329                                         lo_message_add_float (msg, now_meter);
330                                         lo_send_message (addr, path.c_str(), msg);
331                                 } else if (feedback[8]) {
332                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
333                                         uint16_t ledbits = ~(0xfff<<ledlvl);
334                                         lo_message_add_int32 (msg, ledbits);
335                                         lo_send_message (addr, path.c_str(), msg);
336                                 }
337                                 lo_message_free (msg);
338                         }
339                         if (feedback[9]) {
340                                 string path = "/select/signal";
341                                 lo_message msg = lo_message_new ();
342                                 float signal;
343                                 if (now_meter < -40) {
344                                         signal = 0;
345                                 } else {
346                                         signal = 1;
347                                 }
348                                 lo_message_add_float (msg, signal);
349                                 lo_send_message (addr, path.c_str(), msg);
350                                 lo_message_free (msg);
351                         }
352                 }
353                 _last_meter = now_meter;
354
355         }
356         if (feedback[1]) {
357                 if (gain_timeout) {
358                         if (gain_timeout == 1) {
359                                 text_message ("/select/name", _strip->name());
360                         }
361                         gain_timeout--;
362                 }
363         }
364         if (feedback[13]) {
365                 for (uint32_t i = 0; i < send_timeout.size(); i++) {
366                         if (send_timeout[i]) {
367                                 if (send_timeout[i] == 1) {
368                                         text_with_id ("/select/send_name", i + 1, _strip->send_name(i));
369                                 }
370                                 send_timeout[i]--;
371                         }
372                 }
373         }
374         if (feedback[1]) {
375                 if (as != ARDOUR::AutoState::Off) {
376                         if(_last_gain != _strip->gain_control()->get_value()) {
377                                 _last_gain = _strip->gain_control()->get_value();
378                                 if (gainmode) {
379                                         gain_message ("/select/fader", _strip->gain_control());
380                                 } else {
381                                         gain_message ("/select/fader", _strip->gain_control());
382                                 }
383                         }
384                 }
385         }
386
387 }
388
389 void
390 OSCSelectObserver::name_changed (const PBD::PropertyChange& what_changed)
391 {
392         if (!what_changed.contains (ARDOUR::Properties::name)) {
393                 return;
394         }
395
396         if (!_strip) {
397                 return;
398         }
399
400         text_message ("/select/name", _strip->name());
401         boost::shared_ptr<Route> route = boost::dynamic_pointer_cast<Route> (_strip);
402         if (route) {
403                 //spit out the comment at the same time
404                 text_message ("/select/comment", route->comment());
405                 // lets tell the surface how many inputs this strip has
406                 clear_strip ("/select/n_inputs", (float) route->n_inputs().n_total());
407                 // lets tell the surface how many outputs this strip has
408                 clear_strip ("/select/n_outputs", (float) route->n_outputs().n_total());
409         }
410 }
411
412 void
413 OSCSelectObserver::change_message (string path, boost::shared_ptr<Controllable> controllable)
414 {
415         lo_message msg = lo_message_new ();
416         float val = controllable->get_value();
417
418         lo_message_add_float (msg, (float) controllable->internal_to_interface (val));
419
420         lo_send_message (addr, path.c_str(), msg);
421         lo_message_free (msg);
422 }
423
424 void
425 OSCSelectObserver::enable_message (string path, boost::shared_ptr<Controllable> controllable)
426 {
427         float val = controllable->get_value();
428         if (val) {
429                 clear_strip (path, 1);
430         } else {
431                 clear_strip (path, 0);
432         }
433
434 }
435
436 void
437 OSCSelectObserver::change_message_with_id (string path, uint32_t id, boost::shared_ptr<Controllable> controllable)
438 {
439         lo_message msg = lo_message_new ();
440         float val = controllable->get_value();
441         if (feedback[2]) {
442                 path = set_path (path, id);
443         } else {
444                 lo_message_add_int32 (msg, id);
445         }
446
447         lo_message_add_float (msg, (float) controllable->internal_to_interface (val));
448
449         lo_send_message (addr, path.c_str(), msg);
450         lo_message_free (msg);
451 }
452
453 void
454 OSCSelectObserver::enable_message_with_id (string path, uint32_t id, boost::shared_ptr<Controllable> controllable)
455 {
456         float val = controllable->get_value();
457         if (val) {
458                 clear_strip_with_id (path, id, 1);
459         } else {
460                 clear_strip_with_id (path, id, 0);
461         }
462 }
463
464 void
465 OSCSelectObserver::text_message (string path, std::string text)
466 {
467         lo_message msg = lo_message_new ();
468
469         lo_message_add_string (msg, text.c_str());
470
471         lo_send_message (addr, path.c_str(), msg);
472         lo_message_free (msg);
473 }
474
475 void
476 OSCSelectObserver::monitor_status (boost::shared_ptr<Controllable> controllable)
477 {
478         int disk, input;
479         float val = controllable->get_value();
480         switch ((int) val) {
481                 case 1:
482                         disk = 0;
483                         input = 1;
484                         break;
485                 case 2:
486                         disk = 1;
487                         input = 0;
488                         break;
489                 default:
490                         disk = 0;
491                         input = 0;
492         }
493
494         clear_strip ("/select/monitor_input", (float) input);
495         clear_strip ("/select/monitor_disk", (float) disk);
496 }
497
498 void
499 OSCSelectObserver::trim_message (string path, boost::shared_ptr<Controllable> controllable)
500 {
501         lo_message msg = lo_message_new ();
502
503         lo_message_add_float (msg, (float) accurate_coefficient_to_dB (controllable->get_value()));
504
505         lo_send_message (addr, path.c_str(), msg);
506         lo_message_free (msg);
507 }
508
509 void
510 OSCSelectObserver::gain_message (string path, boost::shared_ptr<Controllable> controllable)
511 {
512         lo_message msg = lo_message_new ();
513
514         if (gainmode) {
515                 lo_message_add_float (msg, gain_to_slider_position (controllable->get_value()));
516                 text_message ("/select/name", string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (controllable->get_value())));
517                 gain_timeout = 8;
518         } else {
519                 if (controllable->get_value() < 1e-15) {
520                         lo_message_add_float (msg, -200);
521                 } else {
522                         lo_message_add_float (msg, accurate_coefficient_to_dB (controllable->get_value()));
523                 }
524         }
525
526         lo_send_message (addr, path.c_str(), msg);
527         lo_message_free (msg);
528 }
529
530 void
531 OSCSelectObserver::gain_automation (string path)
532 {
533         lo_message msg = lo_message_new ();
534         string apath = string_compose ("%1/automation", path);
535
536         boost::shared_ptr<GainControl> control = _strip->gain_control();
537         as = control->alist()->automation_state();
538         lo_message_add_float (msg, as);
539         gain_message (path, control);
540         lo_send_message (addr, apath.c_str(), msg);
541         lo_message_free (msg);
542 }
543
544 void
545 OSCSelectObserver::send_gain (uint32_t id, boost::shared_ptr<PBD::Controllable> controllable)
546 {
547         lo_message msg = lo_message_new ();
548         string path;
549         float value;
550         float db;
551 #ifdef MIXBUS
552                 db = controllable->get_value();
553 #else
554                 if (controllable->get_value() < 1e-15) {
555                         db = -193;
556                 } else {
557                         db = accurate_coefficient_to_dB (controllable->get_value());
558                 }
559 #endif
560
561         if (gainmode) {
562                 path = "/select/send_fader";
563 #ifdef MIXBUS
564                 value = controllable->internal_to_interface (controllable->get_value());
565 #else
566                 value = gain_to_slider_position (controllable->get_value());
567 #endif
568         text_with_id ("/select/send_name" , id + 1, string_compose ("%1%2%3", std::fixed, std::setprecision(2), db));
569         if (send_timeout.size() > id) {
570                 send_timeout[id] = 8;
571         }
572         } else {
573                 path = "/select/send_gain";
574                 value = db;
575         }
576
577         if (feedback[2]) {
578                 path = set_path (path, id + 1);
579         } else {
580                 lo_message_add_int32 (msg, id + 1);
581         }
582
583         lo_message_add_float (msg, value);
584         lo_send_message (addr, path.c_str(), msg);
585         lo_message_free (msg);
586 }
587
588 void
589 OSCSelectObserver::send_enable (string path, uint32_t id, boost::shared_ptr<Processor> proc)
590 {
591         // with no delay value is wrong
592         Glib::usleep(10);
593
594         clear_strip_with_id ("/select/send_enable", id, proc->enabled());
595 }
596
597 void
598 OSCSelectObserver::text_with_id (string path, uint32_t id, string name)
599 {
600         lo_message msg = lo_message_new ();
601         if (feedback[2]) {
602                 path = set_path (path, id);
603         } else {
604                 lo_message_add_int32 (msg, id);
605         }
606
607         lo_message_add_string (msg, name.c_str());
608
609         lo_send_message (addr, path.c_str(), msg);
610         lo_message_free (msg);
611 }
612
613 void
614 OSCSelectObserver::comp_mode ()
615 {
616         change_message ("/select/comp_mode", _strip->comp_mode_controllable());
617         text_message ("/select/comp_mode_name", _strip->comp_mode_name(_strip->comp_mode_controllable()->get_value()));
618         text_message ("/select/comp_speed_name", _strip->comp_speed_name(_strip->comp_mode_controllable()->get_value()));
619 }
620
621 void
622 OSCSelectObserver::eq_init()
623 {
624         // HPF and enable are special case, rest are in bands
625         if (_strip->eq_hpf_controllable ()) {
626                 _strip->eq_hpf_controllable ()->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_hpf"), _strip->eq_hpf_controllable()), OSC::instance());
627                 change_message ("/select/eq_hpf", _strip->eq_hpf_controllable());
628         }
629         if (_strip->eq_enable_controllable ()) {
630                 _strip->eq_enable_controllable ()->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::enable_message, this, X_("/select/eq_enable"), _strip->eq_enable_controllable()), OSC::instance());
631                 enable_message ("/select/eq_enable", _strip->eq_enable_controllable());
632         }
633
634         uint32_t eq_bands = _strip->eq_band_cnt ();
635         if (!eq_bands) {
636                 return;
637         }
638
639         for (uint32_t i = 0; i < eq_bands; i++) {
640                 if (_strip->eq_band_name(i).size()) {
641                         text_with_id ("/select/eq_band_name", i + 1, _strip->eq_band_name (i));
642                 }
643                 if (_strip->eq_gain_controllable (i)) {
644                         _strip->eq_gain_controllable(i)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message_with_id, this, X_("/select/eq_gain"), i + 1, _strip->eq_gain_controllable(i)), OSC::instance());
645                         change_message_with_id ("/select/eq_gain", i + 1, _strip->eq_gain_controllable(i));
646                 }
647                 if (_strip->eq_freq_controllable (i)) {
648                         _strip->eq_freq_controllable(i)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message_with_id, this, X_("/select/eq_freq"), i + 1, _strip->eq_freq_controllable(i)), OSC::instance());
649                         change_message_with_id ("/select/eq_freq", i + 1, _strip->eq_freq_controllable(i));
650                 }
651                 if (_strip->eq_q_controllable (i)) {
652                         _strip->eq_q_controllable(i)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message_with_id, this, X_("/select/eq_q"), i + 1, _strip->eq_q_controllable(i)), OSC::instance());
653                         change_message_with_id ("/select/eq_q", i + 1, _strip->eq_q_controllable(i));
654                 }
655                 if (_strip->eq_shape_controllable (i)) {
656                         _strip->eq_shape_controllable(i)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message_with_id, this, X_("/select/eq_shape"), i + 1, _strip->eq_shape_controllable(i)), OSC::instance());
657                         change_message_with_id ("/select/eq_shape", i + 1, _strip->eq_shape_controllable(i));
658                 }
659         }
660 }
661
662 void
663 OSCSelectObserver::eq_end ()
664 {
665         //need to check feedback for [13]
666         eq_connections.drop_connections ();
667         if (_strip->eq_hpf_controllable ()) {
668                 clear_strip ("/select/eq_hpf", 0);
669         }
670         if (_strip->eq_enable_controllable ()) {
671                 clear_strip ("/select/eq_enable", 0);
672         }
673
674         for (uint32_t i = 1; i <= _strip->eq_band_cnt (); i++) {
675                 text_with_id ("/select/eq_band_name", i, " ");
676                 clear_strip_with_id ("/select/eq_gain", i, 0);
677                 clear_strip_with_id ("/select/eq_freq", i, 0);
678                 clear_strip_with_id ("/select/eq_q", i, 0);
679                 clear_strip_with_id ("/select/eq_shape", i, 0);
680
681
682         }
683 }
684
685 void
686 OSCSelectObserver::eq_restart(int x)
687 {
688         eq_end();
689         eq_init();
690 }
691
692 string
693 OSCSelectObserver::set_path (string path, uint32_t id)
694 {
695         if (feedback[2]) {
696                 path = string_compose ("%1/%2", path, id);
697         }
698         return path;
699 }
700
701 void
702 OSCSelectObserver::clear_strip (string path, float val)
703 {
704         lo_message msg = lo_message_new ();
705         lo_message_add_float (msg, val);
706
707         lo_send_message (addr, path.c_str(), msg);
708         lo_message_free (msg);
709
710 }
711
712 void
713 OSCSelectObserver::clear_strip_with_id (string path, uint32_t id, float val)
714 {
715         lo_message msg = lo_message_new ();
716         if (feedback[2]) {
717                 path = set_path (path, id);
718         } else {
719                 lo_message_add_int32 (msg, id);
720         }
721
722         lo_message_add_float (msg, val);
723
724         lo_send_message (addr, path.c_str(), msg);
725         lo_message_free (msg);
726
727 }
728