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