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