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