a63520b572b204477671ce7c0f4d7b7a0abd219e
[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 "pbd/control_math.h"
23
24 #include "ardour/amp.h"
25 #include "ardour/session.h"
26 #include "ardour/dB.h"
27 #include "ardour/meter.h"
28 #include "ardour/monitor_processor.h"
29
30 #include "osc.h"
31 #include "osc_global_observer.h"
32
33 #include "pbd/i18n.h"
34
35 using namespace std;
36 using namespace PBD;
37 using namespace ARDOUR;
38 using namespace ArdourSurface;
39
40 OSCGlobalObserver::OSCGlobalObserver (OSC& o, Session& s, ArdourSurface::OSC::OSCSurface* su)
41         : _osc (o)
42         ,sur (su)
43         ,_init (true)
44         ,_last_master_gain (0.0)
45         ,_last_master_trim (0.0)
46         ,_last_monitor_gain (0.0)
47         ,last_punchin (4)
48         ,last_punchout (4)
49         ,last_click (4)
50 {
51         addr = lo_address_new_from_url  (sur->remote_url.c_str());
52         session = &s;
53         gainmode = sur->gainmode;
54         feedback = sur->feedback;
55         _last_sample = -1;
56         if (feedback[4]) {
57
58                 // connect to all the things we want to send feed back from
59
60                 /*
61                 *       Master (todo)
62                 *               Pan width
63                 */
64
65                 // Master channel first
66                 _osc.text_message (X_("/master/name"), "Master", addr);
67                 boost::shared_ptr<Stripable> strip = session->master_out();
68
69                 boost::shared_ptr<Controllable> mute_controllable = boost::dynamic_pointer_cast<Controllable>(strip->mute_control());
70                 mute_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_change_message, this, X_("/master/mute"), strip->mute_control()), OSC::instance());
71                 send_change_message ("/master/mute", strip->mute_control());
72
73                 boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(strip->trim_control());
74                 trim_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_trim_message, this, X_("/master/trimdB"), strip->trim_control()), OSC::instance());
75                 send_trim_message ("/master/trimdB", strip->trim_control());
76
77                 boost::shared_ptr<Controllable> pan_controllable = boost::dynamic_pointer_cast<Controllable>(strip->pan_azimuth_control());
78                 if (pan_controllable) {
79                         pan_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_change_message, this, X_("/master/pan_stereo_position"), strip->pan_azimuth_control()), OSC::instance());
80                         send_change_message ("/master/pan_stereo_position", strip->pan_azimuth_control());
81                 }
82
83                 boost::shared_ptr<Controllable> gain_controllable = boost::dynamic_pointer_cast<Controllable>(strip->gain_control());
84                 gain_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_gain_message, this, X_("/master/"), strip->gain_control()), OSC::instance());
85                 send_gain_message ("/master/", strip->gain_control());
86
87                 // monitor stuff next
88                 strip = session->monitor_out();
89                 if (strip) {
90                         _osc.text_message (X_("/monitor/name"), "Monitor", addr);
91
92                         boost::shared_ptr<Controllable> mon_mute_cont = strip->monitor_control()->cut_control();
93                         mon_mute_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_change_message, this, X_("/monitor/mute"), mon_mute_cont), OSC::instance());
94                         send_change_message ("/monitor/mute", mon_mute_cont);
95
96                         boost::shared_ptr<Controllable> mon_dim_cont = strip->monitor_control()->dim_control();
97                         mon_dim_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_change_message, this, X_("/monitor/dim"), mon_dim_cont), OSC::instance());
98                         send_change_message ("/monitor/dim", mon_dim_cont);
99
100                         boost::shared_ptr<Controllable> mon_mono_cont = strip->monitor_control()->mono_control();
101                         mon_mono_cont->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_change_message, this, X_("/monitor/mono"), mon_mono_cont), OSC::instance());
102                         send_change_message ("/monitor/mono", mon_mono_cont);
103
104                         gain_controllable = boost::dynamic_pointer_cast<Controllable>(strip->gain_control());
105                                 gain_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_gain_message, this, X_("/monitor/"), strip->gain_control()), OSC::instance());
106                                 send_gain_message ("/monitor/", strip->gain_control());
107                 }
108
109                 //Transport feedback
110                 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_transport_state_changed, this), OSC::instance());
111                 send_transport_state_changed ();
112                 session->TransportLooped.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_transport_state_changed, this), OSC::instance());
113                 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_record_state_changed, this), OSC::instance());
114                 send_record_state_changed ();
115                 session->locations_modified.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::marks_changed, this), OSC::instance());
116                 marks_changed ();
117
118                 // session feedback
119                 session->StateSaved.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::session_name, this, X_("/session_name"), _1), OSC::instance());
120                 session_name (X_("/session_name"), session->snap_name());
121                 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::solo_active, this, _1), OSC::instance());
122                 solo_active (session->soloing() || session->listening());
123
124                 boost::shared_ptr<Controllable> click_controllable = boost::dynamic_pointer_cast<Controllable>(session->click_gain()->gain_control());
125                 click_controllable->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCGlobalObserver::send_change_message, this, X_("/click/level"), click_controllable), OSC::instance());
126                 send_change_message ("/click/level", click_controllable);
127
128                 extra_check ();
129
130                 /*
131                 *       Maybe (many) more
132                 */
133         }
134         _init = false;
135 }
136
137 OSCGlobalObserver::~OSCGlobalObserver ()
138 {
139         _init = true;
140
141         // need to add general zero everything messages
142         strip_connections.drop_connections ();
143         session_connections.drop_connections ();
144
145         lo_address_free (addr);
146 }
147
148 void
149 OSCGlobalObserver::clear_observer ()
150 {
151         strip_connections.drop_connections ();
152         session_connections.drop_connections ();
153         _osc.text_message (X_("/master/name"), " ", addr);
154         _osc.text_message (X_("/monitor/name"), " ", addr);
155         _osc.text_message (X_("/session_name"), " ", addr);
156         _osc.text_message (X_("/marker"), " ", addr);
157         if (feedback[6]) { // timecode enabled
158                 _osc.text_message (X_("/position/smpte"), " ", addr);
159         }
160         if (feedback[5]) { // Bar beat enabled
161                 _osc.text_message (X_("/position/bbt"), " ", addr);
162         }
163         if (feedback[11]) { // minutes/seconds enabled
164                 _osc.text_message (X_("/position/time"), " ", addr);
165         }
166         if (feedback[10]) { // samples
167                 _osc.text_message (X_("/position/samples"), " ", addr);
168         }
169         if (feedback[3]) { //heart beat enabled
170                 _osc.float_message (X_("/heartbeat"), 0.0, addr);
171         }
172         if (feedback[7] || feedback[8]) { // meters enabled
173                 float meter = 0;
174                 if (feedback[7] && !gainmode) {
175                         meter = -193;
176                 }
177                 _osc.float_message (X_("/master/meter"), meter, addr);
178         }
179         if (feedback[9]) {
180                 _osc.float_message (X_("/master/signal"), 0, addr);
181         }
182         _osc.float_message ("/master/fader", 0, addr);
183         _osc.float_message ("/monitor/fader", 0, addr);
184         _osc.float_message ("/master/gain", -193, addr);
185         _osc.float_message ("/monitor/gain", -193, addr);
186         _osc.float_message (X_("/master/trimdB"), 0, addr);
187         _osc.float_message ("/master/mute", 0, addr);
188         _osc.float_message ("/master/pan_stereo_position", 0.5, addr);
189         _osc.float_message ("/monitor/mute", 0, addr);
190         _osc.float_message ("/monitor/dim", 0, addr);
191         _osc.float_message ("/monitor/mono", 0, addr);
192         _osc.float_message (X_("/loop_toggle"), 0, addr);
193         _osc.float_message (X_("/transport_play"), 0, addr);
194         _osc.float_message (X_("/transport_stop"), 0, addr);
195         _osc.float_message (X_("/toggle_roll"), 0, addr);
196         _osc.float_message (X_("/rewind"), 0, addr);
197         _osc.float_message (X_("/ffwd"), 0, addr);
198         _osc.float_message (X_("/record_tally"), 0, addr);
199         _osc.float_message (X_("/rec_enable_toggle"), 0, addr);
200         _osc.float_message (X_("/cancel_all_solos"), 0, addr);
201         _osc.float_message (X_("/toggle_punch_out"), 0, addr);
202         _osc.float_message (X_("/toggle_punch_in"), 0, addr);
203         _osc.float_message (X_("/toggle_click"), 0, addr);
204         _osc.float_message (X_("/click/level"), 0, addr);
205
206
207 }
208
209 void
210 OSCGlobalObserver::tick ()
211 {
212         if (_init) {
213                 return;
214         }
215         samplepos_t now_sample = session->transport_sample();
216         if (now_sample != _last_sample) {
217                 if (feedback[6]) { // timecode enabled
218                         Timecode::Time timecode;
219                         session->timecode_time (now_sample, timecode);
220
221                         // Timecode mode: Hours/Minutes/Seconds/Samples
222                         ostringstream os;
223                         os << setw(2) << setfill('0') << timecode.hours;
224                         os << ':';
225                         os << setw(2) << setfill('0') << timecode.minutes;
226                         os << ':';
227                         os << setw(2) << setfill('0') << timecode.seconds;
228                         os << ':';
229                         os << setw(2) << setfill('0') << timecode.frames;
230
231                         _osc.text_message ("/position/smpte", os.str(), addr);
232                 }
233                 if (feedback[5]) { // Bar beat enabled
234                         Timecode::BBT_Time bbt_time;
235
236                         session->bbt_time (now_sample, bbt_time);
237
238                         // semantics:  BBB/bb/tttt
239                         ostringstream os;
240
241                         os << setw(3) << setfill('0') << bbt_time.bars;
242                         os << '|';
243                         os << setw(2) << setfill('0') << bbt_time.beats;
244                         os << '|';
245                         os << setw(4) << setfill('0') << bbt_time.ticks;
246
247                         _osc.text_message ("/position/bbt", os.str(), addr);
248                 }
249                 if (feedback[11]) { // minutes/seconds enabled
250                         samplepos_t left = now_sample;
251                         int hrs = (int) floor (left / (session->sample_rate() * 60.0f * 60.0f));
252                         left -= (samplecnt_t) floor (hrs * session->sample_rate() * 60.0f * 60.0f);
253                         int mins = (int) floor (left / (session->sample_rate() * 60.0f));
254                         left -= (samplecnt_t) floor (mins * session->sample_rate() * 60.0f);
255                         int secs = (int) floor (left / (float) session->sample_rate());
256                         left -= (samplecnt_t) floor ((double)(secs * session->sample_rate()));
257                         int millisecs = floor (left * 1000.0 / (float) session->sample_rate());
258
259                         // Min/sec mode: Hours/Minutes/Seconds/msec
260                         ostringstream os;
261                         os << setw(2) << setfill('0') << hrs;
262                         os << ':';
263                         os << setw(2) << setfill('0') << mins;
264                         os << ':';
265                         os << setw(2) << setfill('0') << secs;
266                         os << '.';
267                         os << setw(3) << setfill('0') << millisecs;
268
269                         _osc.text_message ("/position/time", os.str(), addr);
270                 }
271                 if (feedback[10]) { // samples
272                         ostringstream os;
273                         os << now_sample;
274                         _osc.text_message ("/position/samples", os.str(), addr);
275                 }
276                 _last_sample = now_sample;
277                 mark_update ();
278         }
279         if (feedback[3]) { //heart beat enabled
280                 if (_heartbeat == 10) {
281                         _osc.float_message (X_("/heartbeat"), 1.0, addr);
282                 }
283                 if (!_heartbeat) {
284                         _osc.float_message (X_("/heartbeat"), 0.0, addr);
285                 }
286                 _heartbeat++;
287                 if (_heartbeat > 20) _heartbeat = 0;
288         }
289         if (feedback[7] || feedback[8] || feedback[9]) { // meters enabled
290                 // the only meter here is master
291                 float now_meter = session->master_out()->peak_meter()->meter_level(0, MeterMCP);
292                 if (now_meter < -94) now_meter = -193;
293                 if (_last_meter != now_meter) {
294                         if (feedback[7] || feedback[8]) {
295                                 if (gainmode && feedback[7]) {
296                                         // change from db to 0-1
297                                         _osc.float_message (X_("/master/meter"), ((now_meter + 94) / 100), addr);
298                                 } else if ((!gainmode) && feedback[7]) {
299                                         _osc.float_message (X_("/master/meter"), now_meter, addr);
300                                 } else if (feedback[8]) {
301                                         uint32_t ledlvl = (uint32_t)(((now_meter + 54) / 3.75)-1);
302                                         uint32_t ledbits = ~(0xfff<<ledlvl);
303                                         _osc.float_message (X_("/master/meter"), ledbits, addr);
304                                 }
305                         }
306                         if (feedback[9]) {
307                                 float signal;
308                                 if (now_meter < -40) {
309                                         signal = 0;
310                                 } else {
311                                         signal = 1;
312                                 }
313                                 _osc.float_message (X_("/master/signal"), signal, addr);
314                         }
315                 }
316                 _last_meter = now_meter;
317
318         }
319         if (feedback[4]) {
320                 if (master_timeout) {
321                         if (master_timeout == 1) {
322                                 _osc.text_message (X_("/master/name"), "Master", addr);
323                         }
324                         master_timeout--;
325                 }
326                 if (monitor_timeout) {
327                         if (monitor_timeout == 1) {
328                                 _osc.text_message (X_("/monitor/name"), "Monitor", addr);
329                         }
330                         monitor_timeout--;
331                 }
332                 extra_check ();
333         }
334 }
335
336 void
337 OSCGlobalObserver::send_change_message (string path, boost::shared_ptr<Controllable> controllable)
338 {
339         float val = controllable->get_value();
340         _osc.float_message (path, (float) controllable->internal_to_interface (val), addr);
341 }
342
343 void
344 OSCGlobalObserver::session_name (string path, string name)
345 {
346         _osc.text_message (path, name, addr);
347 }
348
349 void
350 OSCGlobalObserver::send_gain_message (string path, boost::shared_ptr<Controllable> controllable)
351 {
352         bool ismaster = false;
353         if (path.find("master") != std::string::npos) {
354                 ismaster = true;
355                 if (_last_master_gain != controllable->get_value()) {
356                         _last_master_gain = controllable->get_value();
357                 } else {
358                         return;
359                 }
360         } else {
361                 if (_last_monitor_gain != controllable->get_value()) {
362                         _last_monitor_gain = controllable->get_value();
363                 } else {
364                         return;
365                 }
366         }
367         if (gainmode) {
368                 _osc.float_message (string_compose ("%1fader", path), controllable->internal_to_interface (controllable->get_value()), addr);
369                 if (gainmode == 1) {
370                         _osc.text_message (string_compose ("%1name", path), string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (controllable->get_value())), addr);
371                         if (ismaster) {
372                                 master_timeout = 8;
373                         } else {
374                                 monitor_timeout = 8;
375                         }
376                 }
377         }
378         if (!gainmode || gainmode == 2) {
379                 if (controllable->get_value() < 1e-15) {
380                         _osc.float_message (string_compose ("%1gain",path), -200, addr);
381                 } else {
382                         _osc.float_message (string_compose ("%1gain",path), accurate_coefficient_to_dB (controllable->get_value()), addr);
383                 }
384         }
385 }
386
387 void
388 OSCGlobalObserver::send_trim_message (string path, boost::shared_ptr<Controllable> controllable)
389 {
390         if (_last_master_trim != controllable->get_value()) {
391                 _last_master_trim = controllable->get_value();
392         } else {
393                 return;
394         }
395         _osc.float_message (X_("/master/trimdB"), (float) accurate_coefficient_to_dB (controllable->get_value()), addr);
396 }
397
398
399 void
400 OSCGlobalObserver::send_transport_state_changed()
401 {
402         _osc.float_message (X_("/loop_toggle"), session->get_play_loop(), addr);
403         _osc.float_message (X_("/transport_play"), session->transport_speed() == 1.0, addr);
404         _osc.float_message (X_("/toggle_roll"), session->transport_speed() == 1.0, addr);
405         _osc.float_message (X_("/transport_stop"), session->transport_stopped(), addr);
406         _osc.float_message (X_("/rewind"), session->transport_speed() < 0.0, addr);
407         _osc.float_message (X_("/ffwd"), (session->transport_speed() != 1.0 && session->transport_speed() > 0.0), addr);
408 }
409
410 void
411 OSCGlobalObserver::marks_changed ()
412 {
413         const Locations::LocationList& ll (session->locations ()->list ());
414         // get Locations that are marks
415         for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
416                 if ((*l)->is_session_range ()) {
417                         lm.push_back (LocationMarker(_("start"), (*l)->start ()));
418                         lm.push_back (LocationMarker(_("end"), (*l)->end ()));
419                         continue;
420                 }
421                 if ((*l)->is_mark ()) {
422                         lm.push_back (LocationMarker((*l)->name(), (*l)->start ()));
423                 }
424         }
425         // sort them by position
426         LocationMarkerSort location_marker_sort;
427         std::sort (lm.begin(), lm.end(), location_marker_sort);
428         mark_update ();
429
430 }
431
432 void
433 OSCGlobalObserver::mark_update ()
434 {
435         if (!lm.size()) {
436                 _osc.text_message (X_("/marker"), "No Marks", addr);
437                 return;
438         }
439         uint32_t prev = 0;
440         uint32_t next = lm.size() - 1;
441         for (uint32_t i = 0; i < lm.size (); i++) {
442                 if ((lm[i].when <= _last_sample) && (i > prev)) {
443                         prev = i;
444                 }
445                 if ((lm[i].when >= _last_sample) && (i < next)) {
446                         next = i;
447                         break;
448                 }
449         }
450         if ((prev_mark != lm[prev].when) || (next_mark != lm[next].when)) {
451                 string send_str = lm[prev].label;
452                 prev_mark = lm[prev].when;
453                 next_mark = lm[next].when;
454                 if (prev != next) {
455                         send_str = string_compose ("%1 <-> %2", lm[prev].label, lm[next].label);
456                 }
457                 if (_last_sample > lm[lm.size() - 1].when) {
458                         send_str = string_compose ("%1 <-", lm[lm.size() - 1].label);
459                 }
460                 if (_last_sample < lm[0].when) {
461                         send_str = string_compose ("-> %1", lm[0].label);
462                 }
463                 _osc.text_message (X_("/marker"), send_str, addr);
464         }
465
466 }
467
468 void
469 OSCGlobalObserver::send_record_state_changed ()
470 {
471         _osc.float_message (X_("/rec_enable_toggle"), (int)session->get_record_enabled (), addr);
472
473         if (session->have_rec_enabled_track ()) {
474                 _osc.float_message (X_("/record_tally"), 1, addr);
475         } else {
476                 _osc.float_message (X_("/record_tally"), 0, addr);
477         }
478 }
479
480 void
481 OSCGlobalObserver::solo_active (bool active)
482 {
483         _osc.float_message (X_("/cancel_all_solos"), (float) active, addr);
484 }
485
486 void
487 OSCGlobalObserver::extra_check ()
488 {
489         if (last_punchin != session->config.get_punch_in()) {
490                 last_punchin = session->config.get_punch_in();
491                 _osc.float_message (X_("/toggle_punch_in"), last_punchin, addr);
492         }
493         if (last_punchout != session->config.get_punch_out()) {
494                 last_punchout = session->config.get_punch_out();
495                 _osc.float_message (X_("/toggle_punch_out"), last_punchout, addr);
496         }
497         if (last_click != Config->get_clicking()) {
498                 last_click = Config->get_clicking();
499                 _osc.float_message (X_("/toggle_click"), last_click, addr);
500         }
501 }
502