6ce80ab789e53ab6cf293e98f990999ebdd8334a
[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 #include "ardour/solo_isolate_control.h"
30
31 #include "osc.h"
32 #include "osc_route_observer.h"
33
34 #include "pbd/i18n.h"
35
36 using namespace std;
37 using namespace PBD;
38 using namespace ARDOUR;
39 using namespace ArdourSurface;
40
41 OSCRouteObserver::OSCRouteObserver (OSC& o, uint32_t ss, ArdourSurface::OSC::OSCSurface* su)
42         : _osc (o)
43         ,ssid (ss)
44         ,sur (su)
45         ,_last_gain (-1.0)
46         ,_last_trim (-1.0)
47         ,_init (true)
48 {
49         addr = lo_address_new_from_url  (sur->remote_url.c_str());
50         refresh_strip (true);
51 }
52
53 OSCRouteObserver::~OSCRouteObserver ()
54 {
55         _init = true;
56
57         strip_connections.drop_connections ();
58
59         lo_address_free (addr);
60 }
61
62 void
63 OSCRouteObserver::no_strip ()
64 {
65         // This gets called on drop references
66         _init = true;
67
68         strip_connections.drop_connections ();
69         /*
70          * The strip will sit idle at this point doing nothing until
71          * the surface has recalculated it's strip list and then calls
72          * refresh_strip. Otherwise refresh strip will get a strip address
73          * that does not exist... Crash
74          */
75  }
76         
77 void
78 OSCRouteObserver::refresh_strip (bool force)
79 {
80         _init = true;
81         uint32_t sid = sur->bank + ssid - 2;
82         if (sid >= sur->strips.size ()) {
83                 // this _should_ only occure if the number of strips is less than banksize
84                 clear_strip ();
85                 return;
86         }
87         // future
88         /*if (sur->linkset) {
89                 //to be added with linkset stuff
90                 if (_osc.linksets[sur->linkset]->not_ready)
91                         clear_strip ();
92                         switch (ssid) {
93                                 case 1:
94                                         _osc.text_message_with_id ("/strip/name", ssid, "Device", addr);
95                                         break;
96                                 case 2:
97                                         _osc.text_message_with_id ("/strip/name", ssid, string_compose ("%1", not_ready), addr);
98                                         break;
99                                 case 3:
100                                         _osc.text_message_with_id ("/strip/name", ssid, "Missing", addr);
101                                         break;
102                                 case 4:
103                                         _osc.text_message_with_id ("/strip/name", ssid, "from", addr);
104                                         break;
105                                 case 5:
106                                         _osc.text_message_with_id ("/strip/name", ssid, "Linkset", addr);
107                                         break;
108                                 default:
109                                         break;
110                         }
111                         return;
112                 }
113         }*/
114
115         boost::shared_ptr<ARDOUR::Stripable> new_strip = sur->strips[sur->bank + ssid - 2];
116         if (_strip && (new_strip == _strip) && !force) {
117                 _init = false;
118                 return;
119         }
120
121         _strip = new_strip;
122         _strip->DropReferences.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::no_strip, this), OSC::instance());
123         as = ARDOUR::Off;
124
125         if (sur->feedback[0]) { // buttons are separate feedback
126                 _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::name_changed, this, boost::lambda::_1), OSC::instance());
127                 name_changed (ARDOUR::Properties::name);
128
129                 _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/mute"), _strip->mute_control()), OSC::instance());
130                 send_change_message ("/strip/mute", _strip->mute_control());
131
132                 _strip->solo_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/solo"), _strip->solo_control()), OSC::instance());
133                 send_change_message ("/strip/solo", _strip->solo_control());
134
135                 if (_strip->solo_isolate_control()) {
136                         _strip->solo_isolate_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/solo_iso"), _strip->solo_isolate_control()), OSC::instance());
137                         send_change_message ("/strip/solo_iso", _strip->solo_isolate_control());
138                 }
139
140                 if (_strip->solo_safe_control()) {
141                         _strip->solo_safe_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCRouteObserver::send_change_message, this, X_("/strip/solo_safe"), _strip->solo_safe_control()), OSC::instance());
142                         send_change_message ("/strip/solo_safe", _strip->solo_safe_control());
143                 }
144
145                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (_strip);
146                 if (track) {
147                         track->monitoring_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_monitor_status, this, track->monitoring_control()), OSC::instance());
148                         send_monitor_status (track->monitoring_control());
149                 }
150
151                 boost::shared_ptr<AutomationControl> rec_controllable = _strip->rec_enable_control ();
152                 if (rec_controllable) {
153                         rec_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_change_message, this, X_("/strip/recenable"), _strip->rec_enable_control()), OSC::instance());
154                         send_change_message ("/strip/recenable", _strip->rec_enable_control());
155                 }
156                 boost::shared_ptr<AutomationControl> recsafe_controllable = _strip->rec_safe_control ();
157                 if (rec_controllable) {
158                         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());
159                         send_change_message ("/strip/record_safe", _strip->rec_safe_control());
160                 }
161                 _strip->presentation_info().PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_select_status, this, _1), OSC::instance());
162                 send_select_status (ARDOUR::Properties::selected);
163         }
164
165         if (sur->feedback[1]) { // level controls
166                 boost::shared_ptr<GainControl> gain_cont = _strip->gain_control();
167                 gain_cont->alist()->automation_state_changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::gain_automation, this), OSC::instance());
168                 gain_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_gain_message, this), OSC::instance());
169                 gain_automation ();
170
171                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
172                 if (trim_controllable) {
173                         trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCRouteObserver::send_trim_message, this), OSC::instance());
174                         send_trim_message ();
175                 }
176
177                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->pan_azimuth_control());
178                 if (pan_controllable) {
179                         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());
180                         send_change_message ("/strip/pan_stereo_position", _strip->pan_azimuth_control());
181                 }
182         }
183         _init = false;
184         tick();
185
186 }
187
188 void
189 OSCRouteObserver::clear_strip ()
190 {
191         _init = true;
192
193         strip_connections.drop_connections ();
194
195         // all strip buttons should be off and faders 0 and etc.
196         _osc.float_message_with_id ("/strip/expand", ssid, 0, addr);
197         if (sur->feedback[0]) { // buttons are separate feedback
198                 _osc.text_message_with_id ("/strip/name", ssid, " ", addr);
199                 _osc.float_message_with_id ("/strip/mute", ssid, 0, addr);
200                 _osc.float_message_with_id ("/strip/solo", ssid, 0, addr);
201                 _osc.float_message_with_id ("/strip/recenable", ssid, 0, addr);
202                 _osc.float_message_with_id ("/strip/record_safe", ssid, 0, addr);
203                 _osc.float_message_with_id ("/strip/monitor_input", ssid, 0, addr);
204                 _osc.float_message_with_id ("/strip/monitor_disk", ssid, 0, addr);
205                 _osc.float_message_with_id ("/strip/gui_select", ssid, 0, addr);
206                 _osc.float_message_with_id ("/strip/select", ssid, 0, addr);
207         }
208         if (sur->feedback[1]) { // level controls
209                 if (sur->gainmode) {
210                         _osc.float_message_with_id ("/strip/fader", ssid, 0, addr);
211                 } else {
212                         _osc.float_message_with_id ("/strip/gain", ssid, -193, addr);
213                 }
214                 _osc.float_message_with_id ("/strip/trimdB", ssid, 0, addr);
215                 _osc.float_message_with_id ("/strip/pan_stereo_position", ssid, 0.5, addr);
216         }
217         if (sur->feedback[9]) {
218                 _osc.float_message_with_id ("/strip/signal", ssid, 0, addr);
219         }
220         if (sur->feedback[7]) {
221                 if (sur->gainmode) {
222                         _osc.float_message_with_id ("/strip/meter", ssid, 0, addr);
223                 } else {
224                         _osc.float_message_with_id ("/strip/meter", ssid, -193, addr);
225                 }
226         }else if (sur->feedback[8]) {
227                 _osc.float_message_with_id ("/strip/meter", ssid, 0, addr);
228         }
229 }
230
231
232 void
233 OSCRouteObserver::tick ()
234 {
235         if (_init) {
236                 return;
237         }
238         if (sur->feedback[7] || sur->feedback[8] || sur->feedback[9]) { // meters enabled
239                 // the only meter here is master
240                 float now_meter;
241                 if (_strip->peak_meter()) {
242                         now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
243                 } else {
244                         now_meter = -193;
245                 }
246                 if (now_meter < -120) now_meter = -193;
247                 if (_last_meter != now_meter) {
248                         if (sur->feedback[7] || sur->feedback[8]) {
249                                 if (sur->gainmode && sur->feedback[7]) {
250                                         _osc.float_message_with_id ("/strip/meter", ssid, ((now_meter + 94) / 100), addr);
251                                 } else if ((!sur->gainmode) && sur->feedback[7]) {
252                                         _osc.float_message_with_id ("/strip/meter", ssid, now_meter, addr);
253                                 } else if (sur->feedback[8]) {
254                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
255                                         uint16_t ledbits = ~(0xfff<<ledlvl);
256                                         _osc.int_message_with_id ("/strip/meter", ssid, ledbits, addr);
257                                 }
258                         }
259                         if (sur->feedback[9]) {
260                                 float signal;
261                                 if (now_meter < -40) {
262                                         signal = 0;
263                                 } else {
264                                         signal = 1;
265                                 }
266                                 _osc.float_message_with_id ("/strip/signal", ssid, signal, addr);
267                         }
268                 }
269                 _last_meter = now_meter;
270
271         }
272         if (sur->feedback[1]) {
273                 if (gain_timeout) {
274                         if (gain_timeout == 1) {
275                                 _osc.text_message_with_id ("/strip/name", ssid, _strip->name(), addr);
276                         }
277                         gain_timeout--;
278                 }
279                 if (trim_timeout) {
280                         if (trim_timeout == 1) {
281                                 _osc.text_message_with_id ("/strip/name", ssid, _strip->name(), addr);
282                         }
283                         trim_timeout--;
284                 }
285                 if (as == ARDOUR::Play ||  as == ARDOUR::Touch) {
286                         if(_last_gain != _strip->gain_control()->get_value()) {
287                                 _last_gain = _strip->gain_control()->get_value();
288                                 send_gain_message ();
289                         }
290                 }
291         }
292
293 }
294
295 void
296 OSCRouteObserver::name_changed (const PBD::PropertyChange& what_changed)
297 {
298         if (!what_changed.contains (ARDOUR::Properties::name)) {
299             return;
300         }
301
302         if (_strip) {
303                 _osc.text_message_with_id ("/strip/name", ssid, _strip->name(), addr);
304         }
305 }
306
307 void
308 OSCRouteObserver::send_change_message (string path, boost::shared_ptr<Controllable> controllable)
309 {
310         float val = controllable->get_value();
311         _osc.float_message_with_id (path, ssid, (float) controllable->internal_to_interface (val), addr);
312 }
313
314 void
315 OSCRouteObserver::send_monitor_status (boost::shared_ptr<Controllable> controllable)
316 {
317         int disk, input;
318         float val = controllable->get_value();
319         switch ((int) val) {
320                 case 1:
321                         disk = 0;
322                         input = 1;
323                         break;
324                 case 2:
325                         disk = 1;
326                         input = 0;
327                         break;
328                 case 3:
329                         disk = 1;
330                         input = 1;
331                         break;
332                 default:
333                         disk = 0;
334                         input = 0;
335         }
336         _osc.int_message_with_id ("/strip/monitor_input", ssid, input, addr);
337         _osc.int_message_with_id ("/strip/monitor_disk", ssid, disk, addr);
338
339 }
340
341 void
342 OSCRouteObserver::send_trim_message ()
343 {
344         if (_last_trim != _strip->trim_control()->get_value()) {
345                 _last_trim = _strip->trim_control()->get_value();
346         } else {
347                 return;
348         }
349         if (sur->gainmode) {
350                 _osc.text_message_with_id ("/strip/name", ssid, string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (_last_trim)), addr);
351                 trim_timeout = 8;
352         }
353
354         _osc.float_message_with_id ("/strip/trimdB", ssid, (float) accurate_coefficient_to_dB (_last_trim), addr);
355 }
356
357 void
358 OSCRouteObserver::send_gain_message ()
359 {
360         boost::shared_ptr<Controllable> controllable = _strip->gain_control();
361         if (_last_gain != controllable->get_value()) {
362                 _last_gain = controllable->get_value();
363         } else {
364                 return;
365         }
366
367         if (sur->gainmode) {
368                 _osc.float_message_with_id ("/strip/fader", ssid, controllable->internal_to_interface (_last_gain), addr);
369                 _osc.text_message_with_id ("/strip/name", ssid, string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (controllable->get_value())), addr);
370                 gain_timeout = 8;
371         } else {
372                 if (controllable->get_value() < 1e-15) {
373                         _osc.float_message_with_id ("/strip/gain", ssid, -200, addr);
374                 } else {
375                         _osc.float_message_with_id ("/strip/gain", ssid, accurate_coefficient_to_dB (_last_gain), addr);
376                 }
377         }
378 }
379
380 void
381 OSCRouteObserver::gain_automation ()
382 {
383         string path = "/strip/gain";
384         if (sur->gainmode) {
385                 path = "/strip/fader";
386         }
387         send_gain_message ();
388         as = _strip->gain_control()->alist()->automation_state();
389         string auto_name;
390         float output = 0;
391         switch (as) {
392                 case ARDOUR::Off:
393                         output = 0;
394                         auto_name = "Manual";
395                         break;
396                 case ARDOUR::Play:
397                         output = 1;
398                         auto_name = "Play";
399                         break;
400                 case ARDOUR::Write:
401                         output = 2;
402                         auto_name = "Write";
403                         break;
404                 case ARDOUR::Touch:
405                         output = 3;
406                         auto_name = "Touch";
407                         break;
408                 case ARDOUR::Latch:
409                         output = 4;
410                         auto_name = "Latch";
411                         break;
412                 default:
413                         break;
414         }
415         _osc.float_message_with_id (string_compose ("%1/automation", path), ssid, output, addr);
416         _osc.text_message_with_id (string_compose ("%1/automation_name", path), ssid, auto_name, addr);
417 }
418
419 void
420 OSCRouteObserver::send_select_status (const PropertyChange& what)
421 {
422         if (what == PropertyChange(ARDOUR::Properties::selected)) {
423                 if (_strip) {
424                         _osc.float_message_with_id ("/strip/select", ssid, _strip->is_selected(), addr);
425                 }
426         }
427 }