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