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