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