41f75139c4e29b19e04cfe12b4969a617c5f79e4
[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/plugin.h"
34 #include "ardour/plugin_insert.h"
35 #include "ardour/processor.h"
36 #include "ardour/readonly_control.h"
37
38 #include "osc.h"
39 #include "osc_select_observer.h"
40
41 #include <glibmm.h>
42
43 #include "pbd/i18n.h"
44
45 using namespace std;
46 using namespace PBD;
47 using namespace ARDOUR;
48 using namespace ArdourSurface;
49
50 OSCSelectObserver::OSCSelectObserver (boost::shared_ptr<Stripable> s, lo_address a, ArdourSurface::OSC::OSCSurface* su)
51         : _strip (s)
52         ,sur (su)
53         ,nsends (0)
54         ,_last_gain (0.0)
55 {
56         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
57         gainmode = sur->gainmode;
58         feedback = sur->feedback;
59         as = ARDOUR::Off;
60         send_size = 0;
61
62         if (feedback[0]) { // buttons are separate feedback
63                 _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::name_changed, this, boost::lambda::_1), OSC::instance());
64                 name_changed (ARDOUR::Properties::name);
65
66                 _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/mute"), _strip->mute_control()), OSC::instance());
67                 change_message ("/select/mute", _strip->mute_control());
68
69                 _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/solo"), _strip->solo_control()), OSC::instance());
70                 change_message ("/select/solo", _strip->solo_control());
71
72                 if (_strip->solo_isolate_control()) {
73                         _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());
74                         change_message ("/select/solo_iso", _strip->solo_isolate_control());
75                 }
76
77                 if (_strip->solo_safe_control()) {
78                         _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());
79                         change_message ("/select/solo_safe", _strip->solo_safe_control());
80                 }
81
82                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
83                 if (track) {
84                         track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::monitor_status, this, track->monitoring_control()), OSC::instance());
85                         monitor_status (track->monitoring_control());
86                 }
87
88                 boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
89                 if (rec_controllable) {
90                         rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/recenable"), _strip->rec_enable_control()), OSC::instance());
91                         change_message ("/select/recenable", _strip->rec_enable_control());
92                 }
93
94                 boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
95                 if (recsafe_controllable) {
96                         recsafe_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/record_safe"), _strip->rec_safe_control()), OSC::instance());
97                         change_message ("/select/record_safe", _strip->rec_safe_control());
98                 }
99
100                 boost::shared_ptr<AutomationControl> phase_controllable = _strip->phase_control ();
101                 if (phase_controllable) {
102                         phase_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/polarity"), _strip->phase_control()), OSC::instance());
103                         change_message ("/select/polarity", _strip->phase_control());
104                 }
105
106         }
107
108         if (feedback[1]) { // level controls
109                 _strip->gain_control()->alist()->automation_state_changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_automation, this), OSC::instance());
110                 _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_message, this), OSC::instance());
111                 gain_automation ();
112
113                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
114                 if (trim_controllable) {
115                         trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::trim_message, this, X_("/select/trimdB"), _strip->trim_control()), OSC::instance());
116                         trim_message ("/select/trimdB", _strip->trim_control());
117                 }
118
119                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
120                 if (pan_controllable) {
121                         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());
122                         change_message ("/select/pan_stereo_position", _strip->pan_azimuth_control());
123                 }
124
125                 boost::shared_ptr<Controllable> width_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_width_control());
126                 if (width_controllable) {
127                         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());
128                         change_message ("/select/pan_stereo_width", _strip->pan_width_control());
129                 }
130
131         }
132         if (feedback[13]) { // Well known controls
133                 // Rest of possible pan controls... Untested because I can't find a way to get them in the GUI :)
134                 if (_strip->pan_elevation_control ()) {
135                         _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());
136                         change_message ("/select/pan_elevation_position", _strip->pan_elevation_control());
137                 }
138                 if (_strip->pan_frontback_control ()) {
139                         _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());
140                         change_message ("/select/pan_frontback_position", _strip->pan_frontback_control());
141                 }
142                 if (_strip->pan_lfe_control ()) {
143                         _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());
144                         change_message ("/select/pan_lfe_control", _strip->pan_lfe_control());
145                 }
146
147                 // sends, plugins and eq
148                 // detecting processor changes is now in osc.cc
149
150                 // Compressor
151                 if (_strip->comp_enable_controllable ()) {
152                         _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());
153                         enable_message ("/select/comp_enable", _strip->comp_enable_controllable());
154                 }
155                 if (_strip->comp_threshold_controllable ()) {
156                         _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());
157                         change_message ("/select/comp_threshold", _strip->comp_threshold_controllable());
158                 }
159                 if (_strip->comp_speed_controllable ()) {
160                         _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());
161                         change_message ("/select/comp_speed", _strip->comp_speed_controllable());
162                 }
163                 if (_strip->comp_mode_controllable ()) {
164                         _strip->comp_mode_controllable ()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::comp_mode, this), OSC::instance());
165                         comp_mode ();
166                 }
167                 if (_strip->comp_makeup_controllable ()) {
168                         _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());
169                         change_message ("/select/comp_makeup", _strip->comp_makeup_controllable());
170                 }
171
172         }
173
174         tick();
175 }
176
177 OSCSelectObserver::~OSCSelectObserver ()
178 {
179         strip_connections.drop_connections ();
180         // all strip buttons should be off and faders 0 and etc.
181         if (feedback[0]) { // buttons are separate feedback
182                 send_float ("/select/expand", 0);
183                 text_message ("/select/name", " ");
184                 text_message ("/select/comment", " ");
185                 send_float ("/select/mute", 0);
186                 send_float ("/select/solo", 0);
187                 send_float ("/select/recenable", 0);
188                 send_float ("/select/record_safe", 0);
189                 send_float ("/select/monitor_input", 0);
190                 send_float ("/select/monitor_disk", 0);
191                 send_float ("/select/polarity", 0);
192                 send_float ("/select/n_inputs", 0);
193                 send_float ("/select/n_outputs", 0);
194         }
195         if (feedback[1]) { // level controls
196                 if (gainmode) {
197                         send_float ("/select/fader", 0);
198                 } else {
199                         send_float ("/select/gain", -193);
200                 }
201                 send_float ("/select/trimdB", 0);
202                 send_float ("/select/pan_stereo_position", 0.5);
203                 send_float ("/select/pan_stereo_width", 1);
204         }
205         if (feedback[9]) {
206                 send_float ("/select/signal", 0);
207         }
208         if (feedback[7]) {
209                 if (gainmode) {
210                         send_float ("/select/meter", 0);
211                 } else {
212                         send_float ("/select/meter", -193);
213                 }
214         }else if (feedback[8]) {
215                 send_float ("/select/meter", 0);
216         }
217         if (feedback[13]) { // Well known controls
218                 send_float ("/select/pan_elevation_position", 0);
219                 send_float ("/select/pan_frontback_position", .5);
220                 send_float ("/select/pan_lfe_control", 0);
221                 send_float ("/select/comp_enable", 0);
222                 send_float ("/select/comp_threshold", 0);
223                 send_float ("/select/comp_speed", 0);
224                 send_float ("/select/comp_mode", 0);
225                 text_message ("/select/comp_mode_name", " ");
226                 text_message ("/select/comp_speed_name", " ");
227                 send_float ("/select/comp_makeup", 0);
228         }
229         send_end();
230         plugin_end();
231         eq_end();
232
233         lo_address_free (addr);
234 }
235
236 void
237 OSCSelectObserver::renew_sends () {
238         send_end();
239         send_init();
240 }
241
242 void
243 OSCSelectObserver::renew_plugin () {
244         plugin_end();
245         plugin_init();
246 }
247
248 void
249 OSCSelectObserver::send_init()
250 {
251         // we don't know how many there are, so find out.
252         bool sends;
253         nsends  = 0;
254         do {
255                 sends = false;
256                 if (_strip->send_level_controllable (nsends)) {
257                         sends = true;
258                         nsends++;
259                 }
260         } while (sends);
261         if (!nsends) {
262                 return;
263         }
264
265         // paging should be done in osc.cc in case there is no feedback
266         send_size = nsends;
267         if (sur->send_page_size) {
268                 send_size = sur->send_page_size;
269         }
270         // check limits
271         uint32_t max_page = (uint32_t)(nsends / send_size) + 1;
272         if (sur->send_page < 1) {
273                 sur->send_page = 1;
274         } else if ((uint32_t)sur->send_page > max_page) {
275                 sur->send_page = max_page;
276         }
277         uint32_t page_start = ((sur->send_page - 1) * send_size);
278         uint32_t last_send = sur->send_page * send_size;
279         uint32_t c = 1;
280
281         for (uint32_t s = page_start; s < last_send; ++s, ++c) {
282
283                 bool send_valid = false;
284                 if (_strip->send_level_controllable (s)) {
285                         _strip->send_level_controllable(s)->Changed.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_gain, this, c, _strip->send_level_controllable(s)), OSC::instance());
286                         send_timeout.push_back (0);
287                         send_gain (c, _strip->send_level_controllable(s));
288                         send_valid = true;
289                 }
290
291                 if (_strip->send_enable_controllable (s)) {
292                         _strip->send_enable_controllable(s)->Changed.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::enable_message_with_id, this, X_("/select/send_enable"), c, _strip->send_enable_controllable(s)), OSC::instance());
293                         enable_message_with_id ("/select/send_enable", c, _strip->send_enable_controllable(s));
294                 } else if (send_valid) {
295                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (_strip);
296                         if (!r) {
297                                 // should never get here
298                                 send_float_with_id ("/select/send_enable", c, 0);
299                         }
300                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(s));
301                         if (snd) {
302                                 boost::shared_ptr<Processor> proc = boost::dynamic_pointer_cast<Processor> (snd);
303                                 proc->ActiveChanged.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_enable, this, X_("/select/send_enable"), c, proc), OSC::instance());
304                                 send_float_with_id ("/select/send_enable", c, proc->enabled());
305                         }
306                 }
307                 if (!gainmode && send_valid) {
308                         text_with_id ("/select/send_name", c, _strip->send_name(s));
309                 }
310         }
311 }
312
313 void
314 OSCSelectObserver::plugin_init()
315 {
316         if (!sur->plugin_id) {
317                 return;
318         }
319
320         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(_strip);
321         if (!r) {
322                 return;
323         }
324
325         // we have a plugin number now get the processor
326         boost::shared_ptr<Processor> proc = r->nth_plugin (sur->plugin_id - 1);
327         boost::shared_ptr<PluginInsert> pi;
328         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(proc))) {
329                 return;
330         }
331         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
332
333         bool ok = false;
334         nplug_params = sur->plug_params.size ();
335
336         // default of 0 page size means show all
337         plug_size = nplug_params;
338         if (sur->plug_page_size) {
339                 plug_size = sur->plug_page_size;
340         }
341         text_message ("/select/plugin/name", pip->name());
342         uint32_t page_end = nplug_params;
343         uint32_t max_page = 1;
344         if (plug_size) {
345                 max_page = (uint32_t)((nplug_params - 1) / plug_size) + 1;
346         }
347
348         if (sur->plug_page < 1) {
349                 sur->plug_page = 1;
350         }
351         if ((uint32_t)sur->plug_page > max_page) {
352                 sur->plug_page = max_page;
353         }
354         uint32_t page_start = ((sur->plug_page - 1) * plug_size);
355         page_end = sur->plug_page * plug_size;
356
357         int pid = 1;
358         for ( uint32_t ppi = page_start;  ppi < page_end; ++ppi, ++pid) {
359                 if (ppi >= nplug_params) {
360                         text_with_id ("/select/plugin/parameter/name", pid, " ");
361                         send_float_with_id ("/select/plugin/parameter", pid, 0);
362                         continue;
363                 }
364
365                 uint32_t controlid = pip->nth_parameter(sur->plug_params[ppi], ok);
366                 if (!ok) {
367                         continue;
368                 }
369                 ParameterDescriptor pd;
370                 pip->get_parameter_descriptor(controlid, pd);
371                 text_with_id ("/select/plugin/parameter/name", pid, pd.label);
372                 if ( pip->parameter_is_input(controlid)) {
373                         boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
374                         if (c) {
375                                 bool swtch = false;
376                                 if (pd.integer_step && pd.upper == 1) {
377                                         swtch = true;
378                                 }
379                                 c->Changed.connect (plugin_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::plugin_parameter_changed, this, pid, swtch, c), OSC::instance());
380                                 plugin_parameter_changed (pid, swtch, c);
381                         }
382                 }
383         }
384 }
385
386 void
387 OSCSelectObserver::send_end ()
388 {
389         send_connections.drop_connections ();
390         for (uint32_t i = 1; i <= send_size; i++) {
391                 if (gainmode) {
392                         send_float_with_id ("/select/send_fader", i, 0);
393                 } else {
394                         send_float_with_id ("/select/send_gain", i, -193);
395                 }
396                 // next enable
397                 send_float_with_id ("/select/send_enable", i, 0);
398                 // next name
399                 text_with_id ("/select/send_name", i, " ");
400         }
401         // need to delete or clear send_timeout
402         send_timeout.clear();
403         nsends = 0;
404 }
405
406 void
407 OSCSelectObserver::plugin_parameter_changed (int pid, bool swtch, boost::shared_ptr<PBD::Controllable> controllable)
408 {
409         if (swtch) {
410                 enable_message_with_id ("/select/plugin/parameter", pid, controllable);
411         } else {
412                 change_message_with_id ("/select/plugin/parameter", pid, controllable);
413         }
414 }
415
416 void
417 OSCSelectObserver::plugin_end ()
418 {
419         plugin_connections.drop_connections ();
420         text_message ("/select/plugin/name", " ");
421         for (uint32_t i = 1; i <= plug_size; i++) {
422                 send_float_with_id ("/select/plugin/parameter", i, 0);
423                 // next name
424                 text_with_id ("/select/plugin/parameter/name", i, " ");
425         }
426         nplug_params = 0;
427 }
428
429 void
430 OSCSelectObserver::tick ()
431 {
432         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
433                 float now_meter;
434                 if (_strip->peak_meter()) {
435                         now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
436                 } else {
437                         now_meter = -193;
438                 }
439                 if (now_meter < -144) now_meter = -193;
440                 if (_last_meter != now_meter) {
441                         if (feedback[7] || feedback[8]) {
442                                 string path = "/select/meter";
443                                 lo_message msg = lo_message_new ();
444                                 if (gainmode && feedback[7]) {
445                                         lo_message_add_float (msg, ((now_meter + 94) / 100));
446                                         lo_send_message (addr, path.c_str(), msg);
447                                 } else if ((!gainmode) && feedback[7]) {
448                                         lo_message_add_float (msg, now_meter);
449                                         lo_send_message (addr, path.c_str(), msg);
450                                 } else if (feedback[8]) {
451                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
452                                         uint16_t ledbits = ~(0xfff<<ledlvl);
453                                         lo_message_add_int32 (msg, ledbits);
454                                         lo_send_message (addr, path.c_str(), msg);
455                                 }
456                                 lo_message_free (msg);
457                         }
458                         if (feedback[9]) {
459                                 string path = "/select/signal";
460                                 lo_message msg = lo_message_new ();
461                                 float signal;
462                                 if (now_meter < -40) {
463                                         signal = 0;
464                                 } else {
465                                         signal = 1;
466                                 }
467                                 lo_message_add_float (msg, signal);
468                                 lo_send_message (addr, path.c_str(), msg);
469                                 lo_message_free (msg);
470                         }
471                 }
472                 _last_meter = now_meter;
473
474         }
475         if (feedback[1]) {
476                 if (gain_timeout) {
477                         if (gain_timeout == 1) {
478                                 text_message ("/select/name", _strip->name());
479                         }
480                         gain_timeout--;
481                 }
482
483                 if (as == ARDOUR::Play ||  as == ARDOUR::Touch) {
484                         if(_last_gain != _strip->gain_control()->get_value()) {
485                                 _last_gain = _strip->gain_control()->get_value();
486                                         gain_message ();
487                         }
488                 }
489         }
490         if (feedback[13]) {
491                 if (_strip->comp_redux_controllable()) {
492                         if (_strip->comp_redux_controllable()->get_parameter()) {
493                                 send_float ("/select/comp_redux", (float) _strip->comp_redux_controllable()->get_parameter ());
494                         }
495                 }
496                 for (uint32_t i = 0; i < send_timeout.size(); i++) {
497                         if (send_timeout[i]) {
498                                 if (send_timeout[i] == 1) {
499                                         text_with_id ("/select/send_name", i + 1, _strip->send_name(i));
500                                 }
501                                 send_timeout[i]--;
502                         }
503                 }
504         }
505
506 }
507
508 void
509 OSCSelectObserver::name_changed (const PBD::PropertyChange& what_changed)
510 {
511         if (!what_changed.contains (ARDOUR::Properties::name)) {
512                 return;
513         }
514
515         if (!_strip) {
516                 return;
517         }
518
519         text_message ("/select/name", _strip->name());
520         boost::shared_ptr<Route> route = boost::dynamic_pointer_cast<Route> (_strip);
521         if (route) {
522                 //spit out the comment at the same time
523                 text_message ("/select/comment", route->comment());
524                 // lets tell the surface how many inputs this strip has
525                 send_float ("/select/n_inputs", (float) route->n_inputs().n_total());
526                 // lets tell the surface how many outputs this strip has
527                 send_float ("/select/n_outputs", (float) route->n_outputs().n_total());
528         }
529 }
530
531 void
532 OSCSelectObserver::change_message (string path, boost::shared_ptr<Controllable> controllable)
533 {
534         lo_message msg = lo_message_new ();
535         float val = controllable->get_value();
536
537         lo_message_add_float (msg, (float) controllable->internal_to_interface (val));
538
539         lo_send_message (addr, path.c_str(), msg);
540         lo_message_free (msg);
541 }
542
543 void
544 OSCSelectObserver::enable_message (string path, boost::shared_ptr<Controllable> controllable)
545 {
546         float val = controllable->get_value();
547         if (val) {
548                 send_float (path, 1);
549         } else {
550                 send_float (path, 0);
551         }
552
553 }
554
555 void
556 OSCSelectObserver::change_message_with_id (string path, uint32_t id, boost::shared_ptr<Controllable> controllable)
557 {
558         lo_message msg = lo_message_new ();
559         float val = controllable->get_value();
560         if (feedback[2]) {
561                 path = set_path (path, id);
562         } else {
563                 lo_message_add_int32 (msg, id);
564         }
565
566         lo_message_add_float (msg, (float) controllable->internal_to_interface (val));
567
568         lo_send_message (addr, path.c_str(), msg);
569         lo_message_free (msg);
570 }
571
572 void
573 OSCSelectObserver::enable_message_with_id (string path, uint32_t id, boost::shared_ptr<Controllable> controllable)
574 {
575         float val = controllable->get_value();
576         if (val) {
577                 send_float_with_id (path, id, 1);
578         } else {
579                 send_float_with_id (path, id, 0);
580         }
581 }
582
583 void
584 OSCSelectObserver::text_message (string path, std::string text)
585 {
586         lo_message msg = lo_message_new ();
587
588         lo_message_add_string (msg, text.c_str());
589
590         lo_send_message (addr, path.c_str(), msg);
591         lo_message_free (msg);
592 }
593
594 void
595 OSCSelectObserver::monitor_status (boost::shared_ptr<Controllable> controllable)
596 {
597         int disk, input;
598         float val = controllable->get_value();
599         switch ((int) val) {
600                 case 1:
601                         disk = 0;
602                         input = 1;
603                         break;
604                 case 2:
605                         disk = 1;
606                         input = 0;
607                         break;
608                 default:
609                         disk = 0;
610                         input = 0;
611         }
612
613         send_float ("/select/monitor_input", (float) input);
614         send_float ("/select/monitor_disk", (float) disk);
615 }
616
617 void
618 OSCSelectObserver::trim_message (string path, boost::shared_ptr<Controllable> controllable)
619 {
620         lo_message msg = lo_message_new ();
621
622         lo_message_add_float (msg, (float) accurate_coefficient_to_dB (controllable->get_value()));
623
624         lo_send_message (addr, path.c_str(), msg);
625         lo_message_free (msg);
626 }
627
628 void
629 OSCSelectObserver::gain_message ()
630 {
631         float value = _strip->gain_control()->get_value();
632
633         if (gainmode) {
634                 text_message ("/select/name", string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (value)));
635                 gain_timeout = 8;
636                 send_float ("/select/fader", gain_to_slider_position (value));
637         } else {
638                 if (value < 1e-15) {
639                         send_float ("/select/gain", -200);
640                 } else {
641                         send_float ("/select/gain", accurate_coefficient_to_dB (value));
642                 }
643         }
644 }
645
646 void
647 OSCSelectObserver::gain_automation ()
648 {
649         float output;
650         as = _strip->gain_control()->alist()->automation_state();
651         string auto_name;
652         switch (as) {
653                 case ARDOUR::Off:
654                         output = 0;
655                         auto_name = "Manual";
656                         break;
657                 case ARDOUR::Play:
658                         output = 1;
659                         auto_name = "Play";
660                         break;
661                 case ARDOUR::Write:
662                         output = 2;
663                         auto_name = "Write";
664                         break;
665                 case ARDOUR::Touch:
666                         output = 3;
667                         auto_name = "Touch";
668                         break;
669                 default:
670                         break;
671         }
672
673         if (gainmode) {
674                 send_float ("/select/fader/automation", output);
675                 text_message ("/select/fader/automation_name", auto_name);
676         } else {
677                 send_float ("/select/gain/automation", output);
678                 text_message ("/select/gain/automation_name", auto_name);
679         }
680
681         gain_message ();
682 }
683
684 void
685 OSCSelectObserver::send_gain (uint32_t id, boost::shared_ptr<PBD::Controllable> controllable)
686 {
687         lo_message msg = lo_message_new ();
688         string path;
689         float value;
690         float db;
691 #ifdef MIXBUS
692                 db = controllable->get_value();
693 #else
694                 if (controllable->get_value() < 1e-15) {
695                         db = -193;
696                 } else {
697                         db = accurate_coefficient_to_dB (controllable->get_value());
698                 }
699 #endif
700
701         if (gainmode) {
702                 path = "/select/send_fader";
703 #ifdef MIXBUS
704                 value = controllable->internal_to_interface (controllable->get_value());
705 #else
706                 value = gain_to_slider_position (controllable->get_value());
707 #endif
708         text_with_id ("/select/send_name" , id, string_compose ("%1%2%3", std::fixed, std::setprecision(2), db));
709         if (send_timeout.size() > id) {
710                 send_timeout[id] = 8;
711         }
712         } else {
713                 path = "/select/send_gain";
714                 value = db;
715         }
716
717         if (feedback[2]) {
718                 path = set_path (path, id);
719         } else {
720                 lo_message_add_int32 (msg, id);
721         }
722
723         lo_message_add_float (msg, value);
724         lo_send_message (addr, path.c_str(), msg);
725         lo_message_free (msg);
726 }
727
728 void
729 OSCSelectObserver::send_enable (string path, uint32_t id, boost::shared_ptr<Processor> proc)
730 {
731         // with no delay value is wrong
732         Glib::usleep(10);
733
734         send_float_with_id ("/select/send_enable", id, proc->enabled());
735 }
736
737 void
738 OSCSelectObserver::text_with_id (string path, uint32_t id, string name)
739 {
740         lo_message msg = lo_message_new ();
741         if (feedback[2]) {
742                 path = set_path (path, id);
743         } else {
744                 lo_message_add_int32 (msg, id);
745         }
746
747         lo_message_add_string (msg, name.c_str());
748
749         lo_send_message (addr, path.c_str(), msg);
750         lo_message_free (msg);
751 }
752
753 void
754 OSCSelectObserver::comp_mode ()
755 {
756         change_message ("/select/comp_mode", _strip->comp_mode_controllable());
757         text_message ("/select/comp_mode_name", _strip->comp_mode_name(_strip->comp_mode_controllable()->get_value()));
758         text_message ("/select/comp_speed_name", _strip->comp_speed_name(_strip->comp_mode_controllable()->get_value()));
759 }
760
761 void
762 OSCSelectObserver::eq_init()
763 {
764         // HPF and enable are special case, rest are in bands
765         if (_strip->filter_enable_controllable (true)) {
766                 _strip->filter_enable_controllable (true)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_hpf/enable"), _strip->filter_enable_controllable (true)), OSC::instance());
767                 change_message ("/select/eq_hpf/enable", _strip->filter_enable_controllable(true));
768         }
769
770         if (_strip->filter_enable_controllable (false)) {
771                 _strip->filter_enable_controllable (false)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_lpf/enable"), _strip->filter_enable_controllable (false)), OSC::instance());
772                 change_message ("/select/eq_lpf/enable", _strip->filter_enable_controllable(false));
773         }
774
775         if (_strip->filter_freq_controllable (true)) {
776                 _strip->filter_freq_controllable (true)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_hpf/freq"), _strip->filter_freq_controllable (true)), OSC::instance());
777                 change_message ("/select/eq_hpf/freq", _strip->filter_freq_controllable(true));
778         }
779
780         if (_strip->filter_freq_controllable (false)) {
781                 _strip->filter_freq_controllable (false)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_lpf/freq"), _strip->filter_freq_controllable (false)), OSC::instance());
782                 change_message ("/select/eq_lpf/freq", _strip->filter_freq_controllable(false));
783         }
784
785         if (_strip->filter_slope_controllable (true)) {
786                 _strip->filter_slope_controllable (true)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_hpf/slope"), _strip->filter_slope_controllable (true)), OSC::instance());
787                 change_message ("/select/eq_hpf/slope", _strip->filter_slope_controllable(true));
788         }
789
790         if (_strip->filter_slope_controllable (false)) {
791                 _strip->filter_slope_controllable (false)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_lpf/slope"), _strip->filter_slope_controllable (false)), OSC::instance());
792                 change_message ("/select/eq_lpf/slope", _strip->filter_slope_controllable(false));
793         }
794
795         if (_strip->eq_enable_controllable ()) {
796                 _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());
797                 enable_message ("/select/eq_enable", _strip->eq_enable_controllable());
798         }
799
800         uint32_t eq_bands = _strip->eq_band_cnt ();
801         if (!eq_bands) {
802                 return;
803         }
804
805         for (uint32_t i = 0; i < eq_bands; i++) {
806                 if (_strip->eq_band_name(i).size()) {
807                         text_with_id ("/select/eq_band_name", i + 1, _strip->eq_band_name (i));
808                 }
809                 if (_strip->eq_gain_controllable (i)) {
810                         _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());
811                         change_message_with_id ("/select/eq_gain", i + 1, _strip->eq_gain_controllable(i));
812                 }
813                 if (_strip->eq_freq_controllable (i)) {
814                         _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());
815                         change_message_with_id ("/select/eq_freq", i + 1, _strip->eq_freq_controllable(i));
816                 }
817                 if (_strip->eq_q_controllable (i)) {
818                         _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());
819                         change_message_with_id ("/select/eq_q", i + 1, _strip->eq_q_controllable(i));
820                 }
821                 if (_strip->eq_shape_controllable (i)) {
822                         _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());
823                         change_message_with_id ("/select/eq_shape", i + 1, _strip->eq_shape_controllable(i));
824                 }
825         }
826 }
827
828 void
829 OSCSelectObserver::eq_end ()
830 {
831         //need to check feedback for [13]
832         eq_connections.drop_connections ();
833         if (_strip->filter_freq_controllable (true)) {
834                 send_float ("/select/eq_hpf", 0);
835         }
836         if (_strip->eq_enable_controllable ()) {
837                 send_float ("/select/eq_enable", 0);
838         }
839
840         for (uint32_t i = 1; i <= _strip->eq_band_cnt (); i++) {
841                 text_with_id ("/select/eq_band_name", i, " ");
842                 send_float_with_id ("/select/eq_gain", i, 0);
843                 send_float_with_id ("/select/eq_freq", i, 0);
844                 send_float_with_id ("/select/eq_q", i, 0);
845                 send_float_with_id ("/select/eq_shape", i, 0);
846
847
848         }
849 }
850
851 void
852 OSCSelectObserver::eq_restart(int x)
853 {
854         eq_end();
855         eq_init();
856 }
857
858 string
859 OSCSelectObserver::set_path (string path, uint32_t id)
860 {
861         if (feedback[2]) {
862                 path = string_compose ("%1/%2", path, id);
863         }
864         return path;
865 }
866
867 void
868 OSCSelectObserver::send_float (string path, float val)
869 {
870         lo_message msg = lo_message_new ();
871         lo_message_add_float (msg, val);
872
873         lo_send_message (addr, path.c_str(), msg);
874         lo_message_free (msg);
875
876 }
877
878 void
879 OSCSelectObserver::send_float_with_id (string path, uint32_t id, float val)
880 {
881         lo_message msg = lo_message_new ();
882         if (feedback[2]) {
883                 path = set_path (path, id);
884         } else {
885                 lo_message_add_int32 (msg, id);
886         }
887
888         lo_message_add_float (msg, val);
889
890         lo_send_message (addr, path.c_str(), msg);
891         lo_message_free (msg);
892
893 }
894