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