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