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