namespace required
[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 "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_select_observer.h"
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace PBD;
35 using namespace ARDOUR;
36 using namespace ArdourSurface;
37
38 OSCSelectObserver::OSCSelectObserver (boost::shared_ptr<Stripable> s, lo_address a, uint32_t ss, uint32_t gm, std::bitset<32> fb)
39         : _strip (s)
40         ,ssid (ss)
41         ,gainmode (gm)
42         ,feedback (fb)
43         ,nsends (0)
44 {
45         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
46
47         if (feedback[0]) { // buttons are separate feedback
48                 _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::name_changed, this, boost::lambda::_1), OSC::instance());
49                 name_changed (ARDOUR::Properties::name);
50
51                 _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_change_message, this, X_("/select/mute"), _strip->mute_control()), OSC::instance());
52                 send_change_message ("/select/mute", _strip->mute_control());
53
54                 _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_change_message, this, X_("/select/solo"), _strip->solo_control()), OSC::instance());
55                 send_change_message ("/select/solo", _strip->solo_control());
56
57                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
58                 if (track) {
59                 track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_monitor_status, this, track->monitoring_control()), OSC::instance());
60                 send_monitor_status (track->monitoring_control());
61                 }
62
63                 boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
64                 if (rec_controllable) {
65                         rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_change_message, this, X_("/select/recenable"), _strip->rec_enable_control()), OSC::instance());
66                         send_change_message ("/select/recenable", _strip->rec_enable_control());
67                 }
68                 boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
69                 if (rec_controllable) {
70                         recsafe_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_change_message, this, X_("/select/record_safe"), _strip->rec_safe_control()), OSC::instance());
71                         send_change_message ("/select/record_safe", _strip->rec_safe_control());
72                 }
73         }
74
75         if (feedback[1]) { // level controls
76                 if (gainmode) {
77                         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_gain_message, this, X_("/select/fader"), _strip->gain_control()), OSC::instance());
78                         send_gain_message ("/select/fader", _strip->gain_control());
79                 } else {
80                         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_gain_message, this, X_("/select/gain"), _strip->gain_control()), OSC::instance());
81                         send_gain_message ("/select/gain", _strip->gain_control());
82                 }
83
84                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
85                 if (trim_controllable) {
86                         trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_trim_message, this, X_("/select/trimdB"), _strip->trim_control()), OSC::instance());
87                         send_trim_message ("/select/trimdB", _strip->trim_control());
88                 }
89
90                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
91                 if (pan_controllable) {
92                         pan_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_change_message, this, X_("/select/pan_stereo_position"), _strip->pan_azimuth_control()), OSC::instance());
93                         send_change_message ("/select/pan_stereo_position", _strip->pan_azimuth_control());
94                 }
95                 // detecting processor changes requires cast to route
96                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(_strip);
97                 r->processors_changed.connect  (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_restart, this, -1), OSC::instance());
98                 send_init();
99         }
100         tick();
101 }
102
103 OSCSelectObserver::~OSCSelectObserver ()
104 {
105
106         strip_connections.drop_connections ();
107         // all strip buttons should be off and faders 0 and etc.
108         if (feedback[0]) { // buttons are separate feedback
109                 lo_message msg = lo_message_new ();
110                 // name is a string do it first
111                 string path = "/select/name";
112                 lo_message_add_string (msg, " ");
113                 lo_send_message (addr, path.c_str(), msg);
114                 lo_message_free (msg);
115                 clear_strip ("/select/mute", 0);
116                 clear_strip ("/select/solo", 0);
117                 clear_strip ("/select/recenable", 0);
118                 clear_strip ("/select/record_safe", 0);
119                 clear_strip ("/select/monitor_input", 0);
120                 clear_strip ("/select/monitor_disk", 0);
121         }
122         if (feedback[1]) { // level controls
123                 if (gainmode) {
124                         clear_strip ("/select/fader", 0);
125                 } else {
126                         clear_strip ("/select/gain", -193);
127                 }
128                 clear_strip ("/select/trimdB", 0);
129                 clear_strip ("/select/pan_stereo_position", 0.5);
130         }
131         if (feedback[9]) {
132                 clear_strip ("/select/signal", 0);
133         }
134         if (feedback[7]) {
135                 if (gainmode) {
136                         clear_strip ("/select/meter", 0);
137                 } else {
138                         clear_strip ("/select/meter", -193);
139                 }
140         }else if (feedback[8]) {
141                 clear_strip ("/select/meter", 0);
142         }
143         send_end();
144
145         lo_address_free (addr);
146 }
147
148 void
149 OSCSelectObserver::send_init()
150 {
151         // we don't know how many there are, so find out.
152         bool sends;
153         do {
154                 sends = false;
155                 if (_strip->send_level_controllable (nsends)) {
156                         _strip->send_level_controllable(nsends)->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_gain, this, X_("/select/send_gain"), nsends, _strip->send_level_controllable(nsends)), OSC::instance());
157                         send_gain ("/select/send_gain", nsends, _strip->send_level_controllable(nsends));
158                         sends = true;
159                 }
160
161                 if (_strip->send_enable_controllable (nsends)) {
162                         _strip->send_enable_controllable(nsends)->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_enable, this, X_("/select/send_enable"), nsends, _strip->send_enable_controllable(nsends)), OSC::instance());
163                         send_enable ("/select/send_enable", nsends, _strip->send_enable_controllable(nsends));
164                         sends = true;
165                 } else if (sends) {
166                         // not used by Ardour, just mixbus so in Ardour always true
167                         lo_message msg = lo_message_new ();
168                         path = "/select/send_enable";
169                         if (feedback[2]) {
170                                 path = set_path (path, nsends + 1);
171                         } else {
172                                 lo_message_add_int32 (msg, nsends + 1);
173                         }
174                         lo_message_add_int32 (msg, 1);
175                         lo_send_message (addr, path.c_str(), msg);
176                         lo_message_free (msg);
177                         }
178
179                 if (sends) { // if the gain control is there, this is too
180                         send_rename ("/select/send_name", nsends, _strip->send_name(nsends));
181                 }
182                 // Send numbers are 0 based, OSC is 1 based so this gets incremented at the end
183                 if (sends) {
184                         nsends++;
185                 }
186         } while (sends);
187 }
188
189 void
190 OSCSelectObserver::send_end ()
191 {
192         send_connections.drop_connections ();
193         for (uint32_t i = 1; i <= nsends; i++) {
194                 lo_message msg = lo_message_new ();
195                 string path = "/select/send_gain";
196                 if (feedback[2]) {
197                         path = set_path (path, i);
198                 } else {
199                         lo_message_add_int32 (msg, i);
200                 }
201
202                 if (gainmode) {
203                         lo_message_add_int32 (msg, 0);
204                 } else {
205                         lo_message_add_float (msg, -193);
206                 }
207                 lo_send_message (addr, path.c_str(), msg);
208                 lo_message_free (msg);
209                 // next enable
210                 msg = lo_message_new ();
211                 path = "/select/send_enable";
212                 if (feedback[2]) {
213                         path = set_path (path, i);
214                 } else {
215                         lo_message_add_int32 (msg, i);
216                 }
217                 lo_message_add_int32 (msg, 0);
218                 lo_send_message (addr, path.c_str(), msg);
219                 lo_message_free (msg);
220                 // next name
221                 msg = lo_message_new ();
222                 path = "/select/send_name";
223                 if (feedback[2]) {
224                         path = set_path (path, i);
225                 } else {
226                         lo_message_add_int32 (msg, i);
227                 }
228                 lo_message_add_string (msg, " ");
229                 lo_send_message (addr, path.c_str(), msg);
230                 lo_message_free (msg);
231         }
232         nsends = 0;
233 }
234
235 void
236 OSCSelectObserver::send_restart(int x)
237 {
238         send_end();
239         send_init();
240 }
241
242 void
243 OSCSelectObserver::tick ()
244 {
245         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
246
247                 float now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
248                 if (now_meter < -193) now_meter = -193;
249                 if (_last_meter != now_meter) {
250                         if (feedback[7] || feedback[8]) {
251                                 string path = "/select/meter";
252                                 lo_message msg = lo_message_new ();
253                                 if (gainmode && feedback[7]) {
254                                         uint32_t lev1023 = (uint32_t)((now_meter + 54) * 17.05);
255                                         lo_message_add_int32 (msg, lev1023);
256                                         lo_send_message (addr, path.c_str(), msg);
257                                 } else if ((!gainmode) && feedback[7]) {
258                                         lo_message_add_float (msg, now_meter);
259                                         lo_send_message (addr, path.c_str(), msg);
260                                 } else if (feedback[8]) {
261                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
262                                         uint16_t ledbits = ~(0xfff<<ledlvl);
263                                         lo_message_add_int32 (msg, ledbits);
264                                         lo_send_message (addr, path.c_str(), msg);
265                                 }
266                                 lo_message_free (msg);
267                         }
268                         if (feedback[9]) {
269                                 string path = "/select/signal";
270                                 lo_message msg = lo_message_new ();
271                                 float signal;
272                                 if (now_meter < -40) {
273                                         signal = 0;
274                                 } else {
275                                         signal = 1;
276                                 }
277                                 lo_message_add_float (msg, signal);
278                                 lo_send_message (addr, path.c_str(), msg);
279                                 lo_message_free (msg);
280                         }
281                 }
282                 _last_meter = now_meter;
283
284         }
285
286 }
287
288 void
289 OSCSelectObserver::name_changed (const PBD::PropertyChange& what_changed)
290 {
291         if (!what_changed.contains (ARDOUR::Properties::name)) {
292             return;
293         }
294
295         if (!_strip) {
296                 return;
297         }
298
299         lo_message msg = lo_message_new ();
300
301         // ssid is the strip on the surface this observer refers to
302         // not part of the internal ordering.
303         string path = "/select/name";
304         /*if (feedback[2]) {
305                 path = set_path (path);
306         } else {
307                 lo_message_add_int32 (msg, ssid);
308         }*/
309         lo_message_add_string (msg, _strip->name().c_str());
310
311         lo_send_message (addr, path.c_str(), msg);
312         lo_message_free (msg);
313 }
314
315 void
316 OSCSelectObserver::send_change_message (string path, boost::shared_ptr<Controllable> controllable)
317 {
318         lo_message msg = lo_message_new ();
319
320         lo_message_add_float (msg, (float) controllable->get_value());
321
322         lo_send_message (addr, path.c_str(), msg);
323         lo_message_free (msg);
324 }
325
326 void
327 OSCSelectObserver::send_monitor_status (boost::shared_ptr<Controllable> controllable)
328 {
329         int disk, input;
330         float val = controllable->get_value();
331         switch ((int) val) {
332                 case 1:
333                         disk = 0;
334                         input = 1;
335                         break;
336                 case 2:
337                         disk = 1;
338                         input = 0;
339                         break;
340                 default:
341                         disk = 0;
342                         input = 0;
343         }
344
345         lo_message msg = lo_message_new ();
346         string path = "/select/monitor_input";
347         lo_message_add_int32 (msg, (float) input);
348         lo_send_message (addr, path.c_str(), msg);
349         lo_message_free (msg);
350
351         msg = lo_message_new ();
352         path = "/select/monitor_disk";
353         lo_message_add_int32 (msg, (float) disk);
354         lo_send_message (addr, path.c_str(), msg);
355         lo_message_free (msg);
356
357 }
358
359 void
360 OSCSelectObserver::send_trim_message (string path, boost::shared_ptr<Controllable> controllable)
361 {
362         lo_message msg = lo_message_new ();
363
364         lo_message_add_float (msg, (float) accurate_coefficient_to_dB (controllable->get_value()));
365
366         lo_send_message (addr, path.c_str(), msg);
367         lo_message_free (msg);
368 }
369
370 void
371 OSCSelectObserver::send_gain_message (string path, boost::shared_ptr<Controllable> controllable)
372 {
373         lo_message msg = lo_message_new ();
374
375         if (gainmode) {
376                 if (controllable->get_value() == 1) {
377                         lo_message_add_int32 (msg, 800);
378                 } else {
379                         lo_message_add_int32 (msg, gain_to_slider_position (controllable->get_value()) * 1023);
380                 }
381         } else {
382                 if (controllable->get_value() < 1e-15) {
383                         lo_message_add_float (msg, -200);
384                 } else {
385                         lo_message_add_float (msg, accurate_coefficient_to_dB (controllable->get_value()));
386                 }
387         }
388
389         lo_send_message (addr, path.c_str(), msg);
390         lo_message_free (msg);
391 }
392
393 void
394 OSCSelectObserver::send_gain (std::string path, uint32_t id, boost::shared_ptr<PBD::Controllable> controllable)
395 {
396         lo_message msg = lo_message_new ();
397
398         if (feedback[2]) {
399                 path = set_path (path, id + 1);
400         } else {
401                 lo_message_add_int32 (msg, id + 1);
402         }
403
404         if (gainmode) {
405                 if (controllable->get_value() == 1) {
406                         lo_message_add_int32 (msg, 800);
407                 } else {
408                         lo_message_add_int32 (msg, gain_to_slider_position (controllable->get_value()) * 1023);
409                 }
410         } else {
411                 if (controllable->get_value() < 1e-15) {
412                         lo_message_add_float (msg, -200);
413                 } else {
414                         lo_message_add_float (msg, accurate_coefficient_to_dB (controllable->get_value()));
415                 }
416         }
417
418         lo_send_message (addr, path.c_str(), msg);
419         lo_message_free (msg);
420 }
421
422 void
423 OSCSelectObserver::send_enable (string path, uint32_t id, boost::shared_ptr<Controllable> controllable)
424 {
425         lo_message msg = lo_message_new ();
426         if (feedback[2]) {
427                 path = set_path (path, id + 1);
428         } else {
429                 lo_message_add_int32 (msg, id + 1);
430         }
431
432         lo_message_add_float (msg, (float) controllable->get_value());
433
434         lo_send_message (addr, path.c_str(), msg);
435         lo_message_free (msg);
436 }
437
438 void
439 OSCSelectObserver::send_rename (string path, uint32_t id, string name)
440 {
441         lo_message msg = lo_message_new ();
442         if (feedback[2]) {
443                 path = set_path (path, id + 1);
444         } else {
445                 lo_message_add_int32 (msg, id + 1);
446         }
447
448         lo_message_add_string (msg, name.c_str());
449
450         lo_send_message (addr, path.c_str(), msg);
451         lo_message_free (msg);
452 }
453
454 string
455 OSCSelectObserver::set_path (string path, uint32_t id)
456 {
457         if (feedback[2]) {
458   ostringstream os;
459   os << path << "/" << id;
460   path = os.str();
461         }
462         return path;
463 }
464
465 void
466 OSCSelectObserver::clear_strip (string path, float val)
467 {
468         lo_message msg = lo_message_new ();
469         lo_message_add_float (msg, val);
470
471         lo_send_message (addr, path.c_str(), msg);
472         lo_message_free (msg);
473
474 }
475