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