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