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