OSC: add feedback for /monitor/mute,dim,mono
[ardour.git] / libs / surfaces / osc / osc_global_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/session.h"
23 #include "ardour/dB.h"
24 #include "ardour/meter.h"
25 #include "ardour/monitor_processor.h"
26
27 #include "osc.h"
28 #include "osc_global_observer.h"
29
30 #include "pbd/i18n.h"
31
32 using namespace std;
33 using namespace PBD;
34 using namespace ARDOUR;
35 using namespace ArdourSurface;
36
37 OSCGlobalObserver::OSCGlobalObserver (Session& s, lo_address a, uint32_t gm, std::bitset<32> fb)
38         : gainmode (gm)
39         ,feedback (fb)
40 {
41         addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
42         session = &s;
43         _last_frame = -1;
44         if (feedback[4]) {
45
46                 // connect to all the things we want to send feed back from
47
48                 /*
49                 *       Master (todo)
50                 *               Pan width
51                 */
52
53                 // Master channel first
54                 text_message (X_("/master/name"), "Master");
55                 boost::shared_ptr<Stripable> strip = session->master_out();
56
57                 boost::shared_ptr<Controllable> mute_controllable = boost::dynamic_pointer_cast<Controllable>(strip->mute_control());
58                 mute_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCGlobalObserver::send_change_message, this, X_("/master/mute"), strip->mute_control()), OSC::instance());
59                 send_change_message ("/master/mute", strip->mute_control());
60
61                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(strip->trim_control());
62                 trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCGlobalObserver::send_trim_message, this, X_("/master/trimdB"), strip->trim_control()), OSC::instance());
63                 send_trim_message ("/master/trimdB", strip->trim_control());
64
65                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(strip->pan_azimuth_control());
66                 if (pan_controllable) {
67                         pan_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCGlobalObserver::send_change_message, this, X_("/master/pan_stereo_position"), strip->pan_azimuth_control()), OSC::instance());
68                         send_change_message ("/master/pan_stereo_position", strip->pan_azimuth_control());
69                 }
70
71                 boost::shared_ptr<Controllable> gain_controllable = boost::dynamic_pointer_cast<Controllable>(strip->gain_control());
72                 gain_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCGlobalObserver::send_gain_message, this, X_("/master/"), strip->gain_control()), OSC::instance());
73                 send_gain_message ("/master/", strip->gain_control());
74
75                 // monitor stuff next
76                 strip = session->monitor_out();
77                 if (strip) {
78                         text_message (X_("/monitor/name"), "Monitor");
79
80                         boost::shared_ptr<Controllable> mon_mute_cont = strip->monitor_control()->cut_control();
81                         mon_mute_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCGlobalObserver::send_change_message, this, X_("/monitor/mute"), mon_mute_cont), OSC::instance());
82                         send_change_message ("/monitor/mute", mon_mute_cont);
83
84                         boost::shared_ptr<Controllable> mon_dim_cont = strip->monitor_control()->dim_control();
85                         mon_dim_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCGlobalObserver::send_change_message, this, X_("/monitor/dim"), mon_dim_cont), OSC::instance());
86                         send_change_message ("/monitor/dim", mon_dim_cont);
87
88                         boost::shared_ptr<Controllable> mon_mono_cont = strip->monitor_control()->mono_control();
89                         mon_mono_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCGlobalObserver::send_change_message, this, X_("/monitor/mono"), mon_mono_cont), OSC::instance());
90                         send_change_message ("/monitor/mono", mon_mono_cont);
91
92                         gain_controllable = boost::dynamic_pointer_cast<Controllable>(strip->gain_control());
93                                 gain_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, bind (&OSCGlobalObserver::send_gain_message, this, X_("/monitor/"), strip->gain_control()), OSC::instance());
94                                 send_gain_message ("/monitor/", strip->gain_control());
95                 }
96
97                 /*
98                 *       Transport (todo)
99                 *               punchin/out
100                 */
101                 //Transport feedback
102                 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_transport_state_changed, this), OSC::instance());
103                 send_transport_state_changed ();
104                 session->TransportLooped.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_transport_state_changed, this), OSC::instance());
105                 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_record_state_changed, this), OSC::instance());
106                 send_record_state_changed ();
107
108                 // session feedback
109                 session->StateSaved.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::text_message, this, X_("/session_name"), _1), OSC::instance());
110                 text_message (X_("/session_name"), session->snap_name());
111                 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::solo_active, this, _1), OSC::instance());
112                 solo_active (session->soloing() || session->listening());
113
114                 /*
115                 *       Maybe (many) more
116                 */
117         }
118 }
119
120 OSCGlobalObserver::~OSCGlobalObserver ()
121 {
122
123         // need to add general zero everything messages
124         strip_connections.drop_connections ();
125         session_connections.drop_connections ();
126
127         lo_address_free (addr);
128 }
129
130 void
131 OSCGlobalObserver::tick ()
132 {
133         framepos_t now_frame = session->transport_frame();
134         if (now_frame != _last_frame) {
135                 if (feedback[6]) { // timecode enabled
136                         Timecode::Time timecode;
137                         session->timecode_time (now_frame, timecode);
138
139                         // Timecode mode: Hours/Minutes/Seconds/Frames
140                         ostringstream os;
141                         os << setw(2) << setfill('0') << timecode.hours;
142                         os << ':';
143                         os << setw(2) << setfill('0') << timecode.minutes;
144                         os << ':';
145                         os << setw(2) << setfill('0') << timecode.seconds;
146                         os << ':';
147                         os << setw(2) << setfill('0') << timecode.frames;
148
149                         text_message ("/position/smpte", os.str());
150                 }
151                 if (feedback[5]) { // Bar beat enabled
152                         Timecode::BBT_Time bbt_time;
153
154                         session->bbt_time (now_frame, bbt_time);
155
156                         // semantics:  BBB/bb/tttt
157                         ostringstream os;
158
159                         os << setw(3) << setfill('0') << bbt_time.bars;
160                         os << '|';
161                         os << setw(2) << setfill('0') << bbt_time.beats;
162                         os << '|';
163                         os << setw(4) << setfill('0') << bbt_time.ticks;
164
165                         text_message ("/position/bbt", os.str());
166                 }
167                 if (feedback[11]) { // minutes/seconds enabled
168                         framepos_t left = now_frame;
169                         int hrs = (int) floor (left / (session->frame_rate() * 60.0f * 60.0f));
170                         left -= (framecnt_t) floor (hrs * session->frame_rate() * 60.0f * 60.0f);
171                         int mins = (int) floor (left / (session->frame_rate() * 60.0f));
172                         left -= (framecnt_t) floor (mins * session->frame_rate() * 60.0f);
173                         int secs = (int) floor (left / (float) session->frame_rate());
174                         left -= (framecnt_t) floor ((double)(secs * session->frame_rate()));
175                         int millisecs = floor (left * 1000.0 / (float) session->frame_rate());
176
177                         // Min/sec mode: Hours/Minutes/Seconds/msec
178                         ostringstream os;
179                         os << setw(2) << setfill('0') << hrs;
180                         os << ':';
181                         os << setw(2) << setfill('0') << mins;
182                         os << ':';
183                         os << setw(2) << setfill('0') << secs;
184                         os << '.';
185                         os << setw(3) << setfill('0') << millisecs;
186
187                         text_message ("/position/time", os.str());
188                 }
189                 if (feedback[10]) { // samples
190                         ostringstream os;
191                         os << now_frame;
192                         text_message ("/position/samples", os.str());
193                 }
194                 _last_frame = now_frame;
195         }
196         if (feedback[3]) { //heart beat enabled
197                 if (_heartbeat == 10) {
198                         float_message (X_("/heartbeat"), 1.0);
199                 }
200                 if (!_heartbeat) {
201                         float_message (X_("/heartbeat"), 0.0);
202                 }
203                 _heartbeat++;
204                 if (_heartbeat > 20) _heartbeat = 0;
205         }
206         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
207                 // the only meter here is master
208                 float now_meter = session->master_out()->peak_meter()->meter_level(0, MeterMCP);
209                 if (now_meter < -94) now_meter = -193;
210                 if (_last_meter != now_meter) {
211                         if (feedback[7] || feedback[8]) {
212                                 if (gainmode && feedback[7]) {
213                                         // change from db to 0-1
214                                         float_message (X_("/master/meter"), ((now_meter + 94) / 100));
215                                 } else if ((!gainmode) && feedback[7]) {
216                                         float_message (X_("/master/meter"), now_meter);
217                                 } else if (feedback[8]) {
218                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
219                                         uint32_t ledbits = ~(0xfff<<ledlvl);
220                                         int_message (X_("/master/meter"), ledbits);
221                                 }
222                         }
223                         if (feedback[9]) {
224                                 float signal;
225                                 if (now_meter < -40) {
226                                         signal = 0;
227                                 } else {
228                                         signal = 1;
229                                 }
230                                 float_message (X_("/master/signal"), signal);
231                         }
232                 }
233                 _last_meter = now_meter;
234
235         }
236         if (feedback[4]) {
237                 if (master_timeout) {
238                         if (master_timeout == 1) {
239                                 text_message (X_("/master/name"), "Master");
240                         }
241                         master_timeout--;
242                 }
243                 if (monitor_timeout) {
244                         if (monitor_timeout == 1) {
245                                 text_message (X_("/monitor/name"), "Monitor");
246                         }
247                         monitor_timeout--;
248                 }
249         }
250 }
251
252 void
253 OSCGlobalObserver::send_change_message (string path, boost::shared_ptr<Controllable> controllable)
254 {
255         float_message (path, (float) controllable->get_value());
256 }
257
258 void
259 OSCGlobalObserver::send_gain_message (string path, boost::shared_ptr<Controllable> controllable)
260 {
261         if (gainmode) {
262                 float_message (string_compose ("%1fader", path), gain_to_slider_position (controllable->get_value()));
263                 text_message (string_compose ("%1name", path), string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (controllable->get_value())));
264                 if (path.find("master") != std::string::npos) {
265                         master_timeout = 8;
266                 } else {
267                         monitor_timeout = 8;
268                 }
269
270         } else {
271                 if (controllable->get_value() < 1e-15) {
272                         float_message (string_compose ("%1gain",path), -200);
273                 } else {
274                         float_message (string_compose ("%1gain",path), accurate_coefficient_to_dB (controllable->get_value()));
275                 }
276         }
277 }
278
279 void
280 OSCGlobalObserver::send_trim_message (string path, boost::shared_ptr<Controllable> controllable)
281 {
282         float_message (X_("/master/trimdB"), (float) accurate_coefficient_to_dB (controllable->get_value()));
283 }
284
285
286 void
287 OSCGlobalObserver::send_transport_state_changed()
288 {
289         int_message (X_("/loop_toggle"), session->get_play_loop());
290         int_message (X_("/transport_play"), session->transport_speed() == 1.0);
291         int_message (X_("/transport_stop"), session->transport_stopped());
292         int_message (X_("/rewind"), session->transport_speed() < 0.0);
293         int_message (X_("/ffwd"), (session->transport_speed() != 1.0 && session->transport_speed() > 0.0));
294 }
295
296 void
297 OSCGlobalObserver::send_record_state_changed ()
298 {
299         int_message (X_("/rec_enable_toggle"), (int)session->get_record_enabled ());
300
301         if (session->have_rec_enabled_track ()) {
302                 int_message (X_("/record_tally"), 1);
303         } else {
304                 int_message (X_("/record_tally"), 0);
305         }
306 }
307
308 void
309 OSCGlobalObserver::solo_active (bool active)
310 {
311         float_message (X_("/cancel_all_solos"), (float) active);
312 }
313
314 void
315 OSCGlobalObserver::text_message (string path, std::string text)
316 {
317         lo_message msg = lo_message_new ();
318
319         lo_message_add_string (msg, text.c_str());
320
321         lo_send_message (addr, path.c_str(), msg);
322         lo_message_free (msg);
323 }
324
325 void
326 OSCGlobalObserver::float_message (string path, float value)
327 {
328         lo_message msg = lo_message_new ();
329
330         lo_message_add_float (msg, value);
331
332         lo_send_message (addr, path.c_str(), msg);
333         lo_message_free (msg);
334 }
335
336 void
337 OSCGlobalObserver::int_message (string path, uint32_t value)
338 {
339         lo_message msg = lo_message_new ();
340
341         lo_message_add_int32 (msg, value);
342
343         lo_send_message (addr, path.c_str(), msg);
344         lo_message_free (msg);
345 }