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