9075737b374f09b456caf942424874fab125e569
[ardour.git] / libs / surfaces / osc / osc_route_observer.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "boost/lambda/lambda.hpp"
21
22 #include "pbd/control_math.h"
23
24 #include "ardour/session.h"
25 #include "ardour/track.h"
26 #include "ardour/monitor_control.h"
27 #include "ardour/dB.h"
28 #include "ardour/meter.h"
29
30 #include "osc.h"
31 #include "osc_route_observer.h"
32
33 #include "pbd/i18n.h"
34
35 using namespace std;
36 using namespace PBD;
37 using namespace ARDOUR;
38 using namespace ArdourSurface;
39
40 OSCRouteObserver::OSCRouteObserver (boost::shared_ptr<Stripable> s, lo_address a, uint32_t ss, ArdourSurface::OSC::OSCSurface* su)
41         : _strip (s)
42         ,ssid (ss)
43         ,sur (su)
44         ,_last_gain (0.0)
45 {
46         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
47         gainmode = sur->gainmode;
48         feedback = sur->feedback;
49         as = ARDOUR::Off;
50
51         if (feedback[0]) { // buttons are separate feedback
52                 _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::name_changed, this, boost::lambda::_1), OSC::instance());
53                 name_changed (ARDOUR::Properties::name);
54
55                 _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/mute"), _strip->mute_control()), OSC::instance());
56                 send_change_message ("/strip/mute", _strip->mute_control());
57
58                 _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/solo"), _strip->solo_control()), OSC::instance());
59                 send_change_message ("/strip/solo", _strip->solo_control());
60
61                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
62                 if (track) {
63                         track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_monitor_status, this, track->monitoring_control()), OSC::instance());
64                         send_monitor_status (track->monitoring_control());
65                 }
66
67                 boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
68                 if (rec_controllable) {
69                         rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/recenable"), _strip->rec_enable_control()), OSC::instance());
70                         send_change_message ("/strip/recenable", _strip->rec_enable_control());
71                 }
72                 boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
73                 if (rec_controllable) {
74                         recsafe_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/record_safe"), _strip->rec_safe_control()), OSC::instance());
75                         send_change_message ("/strip/record_safe", _strip->rec_safe_control());
76                 }
77                 _strip->presentation_info().PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_select_status, this, _1), OSC::instance());
78                 send_select_status (ARDOUR::Properties::selected);
79         }
80
81         if (feedback[1]) { // level controls
82                 boost::shared_ptr<GainControl> gain_cont = _strip->gain_control();
83                 if (gainmode) {
84                         gain_cont->alist()->automation_state_changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::gain_automation, this, X_("/strip/fader")), OSC::instance());
85                         gain_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_gain_message, this, X_("/strip/fader"), gain_cont), OSC::instance());
86                         gain_automation ("/strip/fader");
87                 } else {
88                         gain_cont->alist()->automation_state_changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::gain_automation, this, X_("/strip/gain")), OSC::instance());
89                         gain_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_gain_message, this, X_("/strip/gain"), gain_cont), OSC::instance());
90                         gain_automation ("/strip/gain");
91                 }
92
93                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
94                 if (trim_controllable) {
95                         trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_trim_message, this, X_("/strip/trimdB"), _strip->trim_control()), OSC::instance());
96                         send_trim_message ("/strip/trimdB", _strip->trim_control());
97                 }
98
99                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
100                 if (pan_controllable) {
101                         pan_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/pan_stereo_position"), _strip->pan_azimuth_control()), OSC::instance());
102                         send_change_message ("/strip/pan_stereo_position", _strip->pan_azimuth_control());
103                 }
104         }
105         tick();
106 }
107
108 OSCRouteObserver::~OSCRouteObserver ()
109 {
110
111         strip_connections.drop_connections ();
112         if (sur->no_clear) {
113                 // some surfaces destroy their own strips and don't need the extra noise
114                 lo_address_free (addr);
115                 return;
116         }
117
118         // all strip buttons should be off and faders 0 and etc.
119         clear_strip ("/strip/expand", 0);
120         if (feedback[0]) { // buttons are separate feedback
121                 text_with_id ("/strip/name", ssid, " ");
122                 clear_strip ("/strip/mute", 0);
123                 clear_strip ("/strip/solo", 0);
124                 clear_strip ("/strip/recenable", 0);
125                 clear_strip ("/strip/record_safe", 0);
126                 clear_strip ("/strip/monitor_input", 0);
127                 clear_strip ("/strip/monitor_disk", 0);
128                 clear_strip ("/strip/gui_select", 0);
129                 clear_strip ("/strip/select", 0);
130         }
131         if (feedback[1]) { // level controls
132                 if (gainmode) {
133                         clear_strip ("/strip/fader", 0);
134                 } else {
135                         clear_strip ("/strip/gain", -193);
136                 }
137                 clear_strip ("/strip/trimdB", 0);
138                 clear_strip ("/strip/pan_stereo_position", 0.5);
139         }
140         if (feedback[9]) {
141                 clear_strip ("/strip/signal", 0);
142         }
143         if (feedback[7]) {
144                 if (gainmode) {
145                         clear_strip ("/strip/meter", 0);
146                 } else {
147                         clear_strip ("/strip/meter", -193);
148                 }
149         }else if (feedback[8]) {
150                 clear_strip ("/strip/meter", 0);
151         }
152
153         lo_address_free (addr);
154 }
155
156 void
157 OSCRouteObserver::tick ()
158 {
159         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
160                 // the only meter here is master
161                 float now_meter;
162                 if (_strip->peak_meter()) {
163                         now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
164                 } else {
165                         now_meter = -193;
166                 }
167                 if (now_meter < -120) now_meter = -193;
168                 if (_last_meter != now_meter) {
169                         if (feedback[7] || feedback[8]) {
170                                 string path = "/strip/meter";
171                                 lo_message msg = lo_message_new ();
172                                 if (feedback[2]) {
173                                         path = set_path (path);
174                                 } else {
175                                         lo_message_add_int32 (msg, ssid);
176                                 }
177                                 if (gainmode && feedback[7]) {
178                                         lo_message_add_float (msg, ((now_meter + 94) / 100));
179                                         lo_send_message (addr, path.c_str(), msg);
180                                 } else if ((!gainmode) && feedback[7]) {
181                                         lo_message_add_float (msg, now_meter);
182                                         lo_send_message (addr, path.c_str(), msg);
183                                 } else if (feedback[8]) {
184                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
185                                         uint16_t ledbits = ~(0xfff<<ledlvl);
186                                         lo_message_add_int32 (msg, ledbits);
187                                         lo_send_message (addr, path.c_str(), msg);
188                                 }
189                                 lo_message_free (msg);
190                         }
191                         if (feedback[9]) {
192                                 string path = "/strip/signal";
193                                 lo_message msg = lo_message_new ();
194                                 if (feedback[2]) {
195                                         path = set_path (path);
196                                 } else {
197                                         lo_message_add_int32 (msg, ssid);
198                                 }
199                                 float signal;
200                                 if (now_meter < -40) {
201                                         signal = 0;
202                                 } else {
203                                         signal = 1;
204                                 }
205                                 lo_message_add_float (msg, signal);
206                                 lo_send_message (addr, path.c_str(), msg);
207                                 lo_message_free (msg);
208                         }
209                 }
210                 _last_meter = now_meter;
211
212         }
213         if (feedback[1]) {
214                 if (gain_timeout) {
215                         if (gain_timeout == 1) {
216                                 text_with_id ("/strip/name", ssid, _strip->name());
217                         }
218                         gain_timeout--;
219                 }
220                 if (trim_timeout) {
221                         if (trim_timeout == 1) {
222                                 text_with_id ("/strip/name", ssid, _strip->name());
223                         }
224                         trim_timeout--;
225                 }
226                 if (as == ARDOUR::Play ||  as == ARDOUR::Touch) {
227                         if(_last_gain != _strip->gain_control()->get_value()) {
228                                 _last_gain = _strip->gain_control()->get_value();
229                                 if (gainmode) {
230                                         send_gain_message ("/strip/fader", _strip->gain_control());
231                                 } else {
232                                         send_gain_message ("/strip/gain", _strip->gain_control());
233                                 }
234                         }
235                 }
236         }
237
238 }
239
240 void
241 OSCRouteObserver::name_changed (const PBD::PropertyChange& what_changed)
242 {
243         if (!what_changed.contains (ARDOUR::Properties::name)) {
244             return;
245         }
246
247         if (!_strip) {
248                 return;
249         }
250         text_with_id ("/strip/name", ssid, _strip->name());
251 }
252
253 void
254 OSCRouteObserver::send_change_message (string path, boost::shared_ptr<Controllable> controllable)
255 {
256         lo_message msg = lo_message_new ();
257
258         if (feedback[2]) {
259                 path = set_path (path);
260         } else {
261                 lo_message_add_int32 (msg, ssid);
262         }
263         float val = controllable->get_value();
264         lo_message_add_float (msg, (float) controllable->internal_to_interface (val));
265
266         lo_send_message (addr, path.c_str(), msg);
267         lo_message_free (msg);
268 }
269
270 void
271 OSCRouteObserver::text_with_id (string path, uint32_t id, string name)
272 {
273         lo_message msg = lo_message_new ();
274         if (feedback[2]) {
275                 path = set_path (path);
276         } else {
277                 lo_message_add_int32 (msg, id);
278         }
279
280         lo_message_add_string (msg, name.c_str());
281
282         lo_send_message (addr, path.c_str(), msg);
283         lo_message_free (msg);
284 }
285
286 void
287 OSCRouteObserver::send_monitor_status (boost::shared_ptr<Controllable> controllable)
288 {
289         int disk, input;
290         float val = controllable->get_value();
291         switch ((int) val) {
292                 case 1:
293                         disk = 0;
294                         input = 1;
295                         break;
296                 case 2:
297                         disk = 1;
298                         input = 0;
299                         break;
300                 default:
301                         disk = 0;
302                         input = 0;
303         }
304
305         lo_message msg = lo_message_new ();
306         string path = "/strip/monitor_input";
307         if (feedback[2]) {
308                 path = set_path (path);
309         } else {
310                 lo_message_add_int32 (msg, ssid);
311         }
312         lo_message_add_int32 (msg, (float) input);
313         lo_send_message (addr, path.c_str(), msg);
314         lo_message_free (msg);
315
316         msg = lo_message_new ();
317         path = "/strip/monitor_disk";
318         if (feedback[2]) {
319                 path = set_path (path);
320         } else {
321                 lo_message_add_int32 (msg, ssid);
322         }
323         lo_message_add_int32 (msg, (float) disk);
324         lo_send_message (addr, path.c_str(), msg);
325         lo_message_free (msg);
326
327 }
328
329 void
330 OSCRouteObserver::send_trim_message (string path, boost::shared_ptr<Controllable> controllable)
331 {
332         if (gainmode) {
333                 text_with_id ("/strip/name", ssid, string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (controllable->get_value())));
334                 trim_timeout = 8;
335         }
336
337         lo_message msg = lo_message_new ();
338
339         if (feedback[2]) {
340                 path = set_path (path);
341         } else {
342                 lo_message_add_int32 (msg, ssid);
343         }
344
345         lo_message_add_float (msg, (float) accurate_coefficient_to_dB (controllable->get_value()));
346
347         lo_send_message (addr, path.c_str(), msg);
348         lo_message_free (msg);
349 }
350
351 void
352 OSCRouteObserver::send_gain_message (string path, boost::shared_ptr<Controllable> controllable)
353 {
354         lo_message msg = lo_message_new ();
355
356         if (feedback[2]) {
357                 path = set_path (path);
358         } else {
359                 lo_message_add_int32 (msg, ssid);
360         }
361
362         if (gainmode) {
363                 lo_message_add_float (msg, gain_to_position (controllable->get_value())); // XXX use internal_to_interface
364                 text_with_id ("/strip/name", ssid, string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (controllable->get_value())));
365                 gain_timeout = 8;
366         } else {
367                 if (controllable->get_value() < 1e-15) {
368                         lo_message_add_float (msg, -200);
369                 } else {
370                         lo_message_add_float (msg, accurate_coefficient_to_dB (controllable->get_value()));
371                 }
372         }
373
374         lo_send_message (addr, path.c_str(), msg);
375         lo_message_free (msg);
376 }
377
378 void
379 OSCRouteObserver::gain_automation (string path)
380 {
381         lo_message msg = lo_message_new ();
382         string apath = string_compose ("%1/automation", path);
383         string npath = string_compose ("%1/automation_name", path);
384
385         if (feedback[2]) {
386                 apath = set_path (apath);
387         } else {
388                 lo_message_add_int32 (msg, ssid);
389         }
390
391         boost::shared_ptr<GainControl> control = _strip->gain_control();
392         as = control->alist()->automation_state();
393         string auto_name;
394         float output;
395         switch (as) {
396                 case ARDOUR::Off:
397                         output = 0;
398                         auto_name = "Manual";
399                         break;
400                 case ARDOUR::Play:
401                         output = 1;
402                         auto_name = "Play";
403                         break;
404                 case ARDOUR::Write:
405                         output = 2;
406                         auto_name = "Write";
407                         break;
408                 case ARDOUR::Touch:
409                         output = 3;
410                         auto_name = "Touch";
411                         break;
412                 default:
413                         break;
414         }
415
416         send_gain_message (path, control);
417         lo_message_add_float (msg, output);
418         lo_send_message (addr, apath.c_str(), msg);
419         lo_message_free (msg);
420         text_with_id (npath, ssid, auto_name);
421 }
422
423 string
424 OSCRouteObserver::set_path (string path)
425 {
426         if (feedback[2]) {
427                 path = string_compose ("%1/%2", path, ssid);
428         }
429         return path;
430 }
431
432 void
433 OSCRouteObserver::clear_strip (string path, float val)
434 {
435         lo_message msg = lo_message_new ();
436         if (feedback[2]) {
437                 path = set_path (path);
438         } else {
439                 lo_message_add_int32 (msg, ssid);
440         }
441         lo_message_add_float (msg, val);
442
443         lo_send_message (addr, path.c_str(), msg);
444         lo_message_free (msg);
445
446 }
447
448 void
449 OSCRouteObserver::send_select_status (const PropertyChange& what)
450 {
451         if (what == PropertyChange(ARDOUR::Properties::selected)) {
452                 if (_strip) {
453                         string path = "/strip/select";
454
455                         lo_message msg = lo_message_new ();
456                         if (feedback[2]) {
457                                 path = set_path (path);
458                         } else {
459                                 lo_message_add_int32 (msg, ssid);
460                         }
461                         lo_message_add_float (msg, _strip->is_selected());
462                         lo_send_message (addr, path.c_str(), msg);
463                         lo_message_free (msg);
464                 }
465         }
466 }