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