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