OSC: add delta controls to master, monitor and select as well
[ardour.git] / libs / surfaces / osc / osc_cue_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/track.h"
25 #include "ardour/dB.h"
26 #include "ardour/meter.h"
27
28 #include "osc.h"
29 #include "osc_cue_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 OSCCueObserver::OSCCueObserver (boost::shared_ptr<Stripable> s, std::vector<boost::shared_ptr<ARDOUR::Stripable> >& snds, lo_address a)
39         : sends (snds)
40         , _strip (s)
41         , tick_enable (false)
42 {
43         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
44
45         _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCCueObserver::name_changed, this, boost::lambda::_1, 0), OSC::instance());
46         name_changed (ARDOUR::Properties::name, 0);
47
48         _strip->mute_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCCueObserver::send_change_message, this, X_("/cue/mute"), 0, _strip->mute_control()), OSC::instance());
49         send_change_message ("/cue/mute", 0, _strip->mute_control());
50
51         gain_timeout.push_back (0);
52         _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCCueObserver::send_gain_message, this, 0, _strip->gain_control()), OSC::instance());
53         send_gain_message (0, _strip->gain_control());
54
55         send_init ();
56
57         tick_enable = true;
58         tick ();
59 }
60
61 OSCCueObserver::~OSCCueObserver ()
62 {
63
64         strip_connections.drop_connections ();
65         send_end ();
66         // all strip buttons should be off and faders 0 and etc.
67         text_with_id ("/cue/name", 0, " ");
68         clear_strip ("/cue/mute", 0);
69         clear_strip ("/cue/fader", 0);
70         clear_strip ("/cue/signal", 0);
71
72         lo_address_free (addr);
73 }
74
75 void
76 OSCCueObserver::tick ()
77 {
78         if (!tick_enable) {
79                 return;
80         }
81         float now_meter;
82         if (_strip->peak_meter()) {
83                 now_meter = _strip->peak_meter()->meter_level(0, MeterMCP);
84         } else {
85                 now_meter = -193;
86         }
87         if (now_meter < -120) now_meter = -193;
88         if (_last_meter != now_meter) {
89                 string path = "/cue/signal";
90                 lo_message msg = lo_message_new ();
91                 float signal;
92                 if (now_meter < -40) {
93                         signal = 0;
94                 } else {
95                         signal = 1;
96                 }
97                 lo_message_add_float (msg, signal);
98                 lo_send_message (addr, path.c_str(), msg);
99                 lo_message_free (msg);
100         }
101         _last_meter = now_meter;
102
103         for (uint32_t i = 0; i < gain_timeout.size(); i++) {
104                 if (gain_timeout[i]) {
105                         if (gain_timeout[i] == 1) {
106                                 name_changed (ARDOUR::Properties::name, i);
107                         }
108                         gain_timeout[i]--;
109                 }
110         }
111
112 }
113
114 void
115 OSCCueObserver::send_init()
116 {
117         for (uint32_t i = 0; i < sends.size(); i++) {
118                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (sends[i]);
119                 boost::shared_ptr<Send> send = r->internal_send_for (boost::dynamic_pointer_cast<Route> (_strip));
120                 if (r) {
121                         r->processors_changed.connect  (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCCueObserver::send_restart, this), OSC::instance());
122                 }
123
124                 if (send) {
125                         // send name
126                         if (r) {
127                                 sends[i]->PropertyChanged.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCCueObserver::name_changed, this, boost::lambda::_1, i + 1), OSC::instance());
128                                 name_changed (ARDOUR::Properties::name, i + 1);
129                         }
130                                 
131
132                         if (send->gain_control()) {
133                                 gain_timeout.push_back (0);
134                                 send->gain_control()->Changed.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCCueObserver::send_gain_message, this, i + 1, send->gain_control()), OSC::instance());
135                                 send_gain_message (i + 1, send->gain_control());
136                         }
137                         
138                         boost::shared_ptr<Processor> proc = boost::dynamic_pointer_cast<Processor> (send);
139                                 proc->ActiveChanged.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCCueObserver::send_enabled_message, this, X_("/cue/send_enable"), i + 1, proc->enabled()), OSC::instance());
140                                 send_enabled_message (X_("/cue/send/enable"), i + 1, proc->enabled());
141                 }
142         }
143
144 }
145
146 void
147 OSCCueObserver::send_end ()
148 {
149         send_connections.drop_connections ();
150         for (uint32_t i = 1; i <= sends.size(); i++) {
151                 clear_strip (string_compose ("/cue/send/fader/%1", i), 0);
152                 clear_strip (string_compose ("/cue/send/enable/%1", i), 0);
153                 text_with_id ("/cue/send/name", i, " ");
154         }
155 }
156
157 void
158 OSCCueObserver::send_restart ()
159 {
160         tick_enable = false;
161         send_end();
162         send_init();
163         tick_enable = true;
164 }
165
166 void
167 OSCCueObserver::name_changed (const PBD::PropertyChange& what_changed, uint32_t id)
168 {
169         if (!what_changed.contains (ARDOUR::Properties::name)) {
170             return;
171         }
172
173         if (!_strip) {
174                 return;
175         }
176         if (id) {
177                 text_with_id ("/cue/send/name", id, sends[id - 1]->name());
178         } else {
179                 text_with_id ("/cue/name", 0, _strip->name());
180         }
181 }
182
183 void
184 OSCCueObserver::send_change_message (string path, uint32_t id, boost::shared_ptr<Controllable> controllable)
185 {
186         lo_message msg = lo_message_new ();
187
188         if (id) {
189                 path = string_compose("%1/%2", path, id);
190         }
191         float val = controllable->get_value();
192         lo_message_add_float (msg, (float) controllable->internal_to_interface (val));
193
194         lo_send_message (addr, path.c_str(), msg);
195         lo_message_free (msg);
196 }
197
198 void
199 OSCCueObserver::text_with_id (string path, uint32_t id, string val)
200 {
201         lo_message msg = lo_message_new ();
202         if (id) {
203                 path = string_compose("%1/%2", path, id);
204         }
205
206         lo_message_add_string (msg, val.c_str());
207
208         lo_send_message (addr, path.c_str(), msg);
209         lo_message_free (msg);
210 }
211
212 void
213 OSCCueObserver::send_gain_message (uint32_t id,  boost::shared_ptr<Controllable> controllable)
214 {
215         string path = "/cue";
216         if (id) {
217                 path = "/cue/send";
218         }
219
220         text_with_id (string_compose ("%1/name", path), id, string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (controllable->get_value())));
221         path = string_compose ("%1/fader", path);
222         if (id) {
223                 path = string_compose ("%1/%2", path, id);
224         }
225         lo_message msg = lo_message_new ();
226         lo_message_add_float (msg, controllable->internal_to_interface (controllable->get_value()));
227         gain_timeout[id] = 8;
228
229         lo_send_message (addr, path.c_str(), msg);
230         lo_message_free (msg);
231 }
232
233 void
234 OSCCueObserver::send_enabled_message (std::string path, uint32_t id, bool enabled)
235 {
236         lo_message msg = lo_message_new ();
237
238         if (id) {
239                 path = string_compose("%1/%2", path, id);
240         }
241         lo_message_add_float (msg, (float) enabled);
242
243         lo_send_message (addr, path.c_str(), msg);
244         lo_message_free (msg);
245         
246 }
247
248 void
249 OSCCueObserver::clear_strip (string path, float val)
250 {
251         lo_message msg = lo_message_new ();
252         lo_message_add_float (msg, val);
253
254         lo_send_message (addr, path.c_str(), msg);
255         lo_message_free (msg);
256
257 }
258