MCP: fix build
[ardour.git] / libs / surfaces / mackie / strip.cc
1 /*
2         Copyright (C) 2006,2007 John Anderson
3         Copyright (C) 2012 Paul Davis
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the Free Software
17         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <sstream>
21 #include <stdint.h>
22 #include "strip.h"
23
24 #include "midi++/port.h"
25
26 #include "pbd/compose.h"
27 #include "pbd/convert.h"
28
29 #include "ardour/debug.h"
30 #include "ardour/midi_ui.h"
31 #include "ardour/route.h"
32 #include "ardour/track.h"
33 #include "ardour/pannable.h"
34 #include "ardour/panner.h"
35 #include "ardour/rc_configuration.h"
36 #include "ardour/meter.h"
37
38 #include "mackie_control_protocol.h"
39 #include "surface_port.h"
40 #include "surface.h"
41 #include "button.h"
42 #include "led.h"
43 #include "ledring.h"
44 #include "pot.h"
45 #include "fader.h"
46 #include "jog.h"
47 #include "meter.h"
48
49 using namespace Mackie;
50 using namespace std;
51 using namespace ARDOUR;
52 using namespace PBD;
53
54 #define midi_ui_context() ARDOUR::MidiControlUI::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
55 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
56
57 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
58 #define invalidator(x) __invalidator (*(MidiControlUI::instance()), __FILE__, __LINE__)
59
60 Strip::Strip (Surface& s, const std::string& name, int index, StripControlDefinition* ctls)
61         : Group (name)
62         , _solo (0)
63         , _recenable (0)
64         , _mute (0)
65         , _select (0)
66         , _vselect (0)
67         , _fader_touch (0)
68         , _vpot (0)
69         , _gain (0)
70         , _index (index)
71         , _surface (&s)
72 {
73         /* build the controls for this track, which will automatically add them
74            to the Group 
75         */
76
77         for (uint32_t i = 0; ctls[i].name[0]; ++i) {
78                 ctls[i].factory (*_surface, ctls[i].base_id + index, ctls[i].name, *this);
79         }
80 }       
81
82 Strip::~Strip ()
83 {
84         
85 }
86
87 /**
88         TODO could optimise this to use enum, but it's only
89         called during the protocol class instantiation.
90 */
91 void Strip::add (Control & control)
92 {
93         Group::add (control);
94
95         if  (control.name() == "gain") {
96                 _gain = reinterpret_cast<Fader*>(&control);
97         } else if  (control.name() == "vpot") {
98                 _vpot = reinterpret_cast<Pot*>(&control);
99         } else if  (control.name() == "recenable") {
100                 _recenable = reinterpret_cast<Button*>(&control);
101         } else if  (control.name() == "solo") {
102                 _solo = reinterpret_cast<Button*>(&control);
103         } else if  (control.name() == "mute") {
104                 _mute = reinterpret_cast<Button*>(&control);
105         } else if  (control.name() == "select") {
106                 _select = reinterpret_cast<Button*>(&control);
107         } else if  (control.name() == "vselect") {
108                 _vselect = reinterpret_cast<Button*>(&control);
109         } else if  (control.name() == "fader_touch") {
110                 _fader_touch = reinterpret_cast<Button*>(&control);
111         } else if  (control.name() == "meter") {
112                 _meter = reinterpret_cast<Meter*>(&control);
113         } else if  (control.type() == Control::type_led || control.type() == Control::type_led_ring) {
114                 // relax
115         } else {
116                 ostringstream os;
117                 os << "Strip::add: unknown control type " << control;
118                 throw MackieControlException (os.str());
119         }
120 }
121
122 Fader& 
123 Strip::gain()
124 {
125         if  (_gain == 0) {
126                 throw MackieControlException ("gain is null");
127         }
128         return *_gain;
129 }
130
131 Pot& 
132 Strip::vpot()
133 {
134         if  (_vpot == 0) {
135                 throw MackieControlException ("vpot is null");
136         }
137         return *_vpot;
138 }
139
140 Button& 
141 Strip::recenable()
142 {
143         if  (_recenable == 0) {
144                 throw MackieControlException ("recenable is null");
145         }
146         return *_recenable;
147 }
148
149 Button& 
150 Strip::solo()
151 {
152         if  (_solo == 0) {
153                 throw MackieControlException ("solo is null");
154         }
155         return *_solo;
156 }
157 Button& 
158 Strip::mute()
159 {
160         if  (_mute == 0) {
161                 throw MackieControlException ("mute is null");
162         }
163         return *_mute;
164 }
165
166 Button& 
167 Strip::select()
168 {
169         if  (_select == 0) {
170                 throw MackieControlException ("select is null");
171         }
172         return *_select;
173 }
174
175 Button& 
176 Strip::vselect()
177 {
178         if  (_vselect == 0) {
179                 throw MackieControlException ("vselect is null");
180         }
181         return *_vselect;
182 }
183
184 Button& 
185 Strip::fader_touch()
186 {
187         if  (_fader_touch == 0) {
188                 throw MackieControlException ("fader_touch is null");
189         }
190         return *_fader_touch;
191 }
192
193 Meter& 
194 Strip::meter()
195 {
196         if  (_meter == 0) {
197                 throw MackieControlException ("meter is null");
198         }
199         return *_meter;
200 }
201
202 std::ostream & Mackie::operator <<  (std::ostream & os, const Strip & strip)
203 {
204         os << typeid (strip).name();
205         os << " { ";
206         os << "has_solo: " << boolalpha << strip.has_solo();
207         os << ", ";
208         os << "has_recenable: " << boolalpha << strip.has_recenable();
209         os << ", ";
210         os << "has_mute: " << boolalpha << strip.has_mute();
211         os << ", ";
212         os << "has_select: " << boolalpha << strip.has_select();
213         os << ", ";
214         os << "has_vselect: " << boolalpha << strip.has_vselect();
215         os << ", ";
216         os << "has_fader_touch: " << boolalpha << strip.has_fader_touch();
217         os << ", ";
218         os << "has_vpot: " << boolalpha << strip.has_vpot();
219         os << ", ";
220         os << "has_gain: " << boolalpha << strip.has_gain();
221         os << " }";
222         
223         return os;
224 }
225
226 void
227 Strip::set_route (boost::shared_ptr<Route> r)
228 {
229         route_connections.drop_connections ();
230
231         _route = r;
232
233         if (r) {
234
235                 if (has_solo()) {
236                         _route->solo_control()->Changed.connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_solo_changed, this), midi_ui_context());
237                 }
238                 if (has_mute()) {
239                         _route->mute_control()->Changed.connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_mute_changed, this), midi_ui_context());
240                 }
241                 
242                 if (has_gain()) {
243                         _route->gain_control()->Changed.connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_gain_changed, this, false), midi_ui_context());
244                 }
245                 
246                 _route->PropertyChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_property_changed, this, _1), midi_ui_context());
247                 
248                 if (_route->pannable()) {
249                         _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_panner_changed, this, false), midi_ui_context());
250                         _route->pannable()->pan_width_control->Changed.connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_panner_changed, this, false), midi_ui_context());
251                 }
252                 
253                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
254         
255                 if (trk) {
256                         trk->rec_enable_control()->Changed .connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_record_enable_changed, this), midi_ui_context());
257                 }
258                 
259                 // TODO this works when a currently-banked route is made inactive, but not
260                 // when a route is activated which should be currently banked.
261                 
262                 _route->active_changed.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_active_changed, this), midi_ui_context());
263                 _route->DropReferences.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_route_deleted, this), midi_ui_context());
264         
265                 // TODO
266                 // SelectedChanged
267                 // RemoteControlIDChanged. Better handled at Session level.
268
269                 /* Update */
270
271                 notify_all ();
272         }
273 }
274
275 void 
276 Strip::notify_all()
277 {
278         if  (has_solo()) {
279                 notify_solo_changed ();
280         }
281         
282         if  (has_mute()) {
283                 notify_mute_changed ();
284         }
285         
286         if  (has_gain()) {
287                 notify_gain_changed ();
288         }
289         
290         notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
291         
292         if  (has_vpot()) {
293                 notify_panner_changed ();
294         }
295         
296         if  (has_recenable()) {
297                 notify_record_enable_changed ();
298         }
299 }
300
301 void 
302 Strip::notify_solo_changed ()
303 {
304         if (_route) {
305                 Button& button = solo();
306                 _surface->write (builder.build_led (button, _route->soloed()));
307         }
308 }
309
310 void 
311 Strip::notify_mute_changed ()
312 {
313         if (_route) {
314                 Button & button = mute();
315                 _surface->write (builder.build_led (button, _route->muted()));
316         }
317 }
318
319 void 
320 Strip::notify_record_enable_changed ()
321 {
322         if (_route) {
323                 Button & button = recenable();
324                 _surface->write (builder.build_led (button, _route->record_enabled()));
325         }
326 }
327
328 void 
329 Strip::notify_active_changed ()
330 {
331         _surface->mcp().refresh_current_bank();
332 }
333
334 void 
335 Strip::notify_route_deleted ()
336 {
337         _surface->mcp().refresh_current_bank();
338 }
339
340 void 
341 Strip::notify_gain_changed (bool force_update)
342 {
343         if (_route) {
344                 Fader & fader = gain();
345                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("route %1 gain change, update fader %2 on port %3\n", 
346                                                                    _route->name(), 
347                                                                    fader.raw_id(),
348                                                                    _surface->port().output_port().name()));
349                 if (!fader.in_use()) {
350                         float gain_value = gain_to_slider_position (_route->gain_control()->get_value());
351                         // check that something has actually changed
352                         if (force_update || gain_value != _last_gain_written) {
353                                 _surface->write (builder.build_fader (fader, gain_value));
354                                 _last_gain_written = gain_value;
355                         }
356                 }
357         }
358 }
359
360 void 
361 Strip::notify_property_changed (const PropertyChange& what_changed)
362 {
363         if (!what_changed.contains (ARDOUR::Properties::name)) {
364                 return;
365         }
366
367         if (_route) {
368                 string line1;
369                 string fullname = _route->name();
370                 
371                 if (fullname.length() <= 6) {
372                         line1 = fullname;
373                 } else {
374                         line1 = PBD::short_version (fullname, 6);
375                 }
376                 
377                 _surface->write (builder.strip_display (*_surface, *this, 0, line1));
378                 _surface->write (builder.strip_display_blank (*_surface, *this, 1));
379         }
380 }
381
382 void 
383 Strip::notify_panner_changed (bool force_update)
384 {
385         if (_route) {
386                 Pot & pot = vpot();
387                 boost::shared_ptr<Panner> panner = _route->panner();
388                 if (panner) {
389                         double pos = panner->position ();
390
391                         // cache the MidiByteArray here, because the mackie led control is much lower
392                         // resolution than the panner control. So we save lots of byte
393                         // sends in spite of more work on the comparison
394                         MidiByteArray bytes = builder.build_led_ring (pot, ControlState (on, pos), MackieMidiBuilder::midi_pot_mode_dot);
395                         // check that something has actually changed
396                         if (force_update || bytes != _last_pan_written)
397                         {
398                                 _surface->write (bytes);
399                                 _last_pan_written = bytes;
400                         }
401                 } else {
402                         _surface->write (builder.zero_control (pot));
403                 }
404         }
405 }
406
407 bool 
408 Strip::handle_button (SurfacePort & port, Control & control, ButtonState bs)
409 {
410         if (!_route) {
411                 // no route so always switch the light off
412                 // because no signals will be emitted by a non-route
413                 _surface->write (builder.build_led (control.led(), off));
414                 return false;
415         }
416
417         bool state = false;
418
419         if (bs == press) {
420                 if (control.name() == "recenable") {
421                         state = !_route->record_enabled();
422                         _route->set_record_enabled (state, this);
423                 } else if (control.name() == "mute") {
424                         state = !_route->muted();
425                         _route->set_mute (state, this);
426                 } else if (control.name() == "solo") {
427                         state = !_route->soloed();
428                         _route->set_solo (state, this);
429                 } else if (control.name() == "select") {
430                         _surface->mcp().select_track (_route);
431                 } else if (control.name() == "vselect") {
432                         // TODO could be used to select different things to apply the pot to?
433                         //state = default_button_press (dynamic_cast<Button&> (control));
434                 }
435         }
436
437         if (control.name() == "fader_touch") {
438
439                 state = (bs == press);
440                 
441                 gain().set_in_use (state);
442                 
443                 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
444
445                         /* BCF faders don't support touch, so add a timeout to reset
446                            their `in_use' state.
447                         */
448
449                         _surface->mcp().add_in_use_timeout (*_surface, gain(), &fader_touch());
450                 }
451         }
452
453         return state;
454 }
455
456 void
457 Strip::periodic ()
458 {
459         if (!_route) {
460                 return;
461         }
462
463         update_automation ();
464         update_meter ();
465 }
466
467 void 
468 Strip::update_automation ()
469 {
470         ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
471
472         if (gain_state == Touch || gain_state == Play) {
473                 notify_gain_changed (false);
474         }
475
476         if (_route->panner()) {
477                 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
478                 if (panner_state == Touch || panner_state == Play) {
479                         notify_panner_changed (false);
480                 }
481         }
482 }
483
484 void
485 Strip::update_meter ()
486 {
487         float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
488         _surface->write (meter().update_message (dB));
489 }