21a312e4075e3154145cf141f7078426b6f6314a
[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 ui_context() MackieControlProtocol::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() __invalidator (*(MackieControlProtocol::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                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
236                                                                    _surface->number(), _index, _route->name()));
237                 
238
239                 if (has_solo()) {
240                         _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
241                 }
242                 if (has_mute()) {
243                         _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
244                 }
245                 
246                 if (has_gain()) {
247                         _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
248                 }
249                 
250                 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
251                 
252                 if (_route->pannable()) {
253                         _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
254                         _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
255                 }
256                 
257                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
258         
259                 if (trk) {
260                         trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
261                 }
262                 
263                 // TODO this works when a currently-banked route is made inactive, but not
264                 // when a route is activated which should be currently banked.
265                 
266                 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
267                 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
268         
269                 // TODO
270                 // SelectedChanged
271                 // RemoteControlIDChanged. Better handled at Session level.
272
273                 /* Update */
274
275                 notify_all ();
276         } else {
277                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
278                                                                    _surface->number(), _index));
279         }
280 }
281
282 void 
283 Strip::notify_all()
284 {
285         if  (has_solo()) {
286                 notify_solo_changed ();
287         }
288         
289         if  (has_mute()) {
290                 notify_mute_changed ();
291         }
292         
293         if  (has_gain()) {
294                 notify_gain_changed ();
295         }
296         
297         notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
298         
299         if  (has_vpot()) {
300                 notify_panner_changed ();
301         }
302         
303         if  (has_recenable()) {
304                 notify_record_enable_changed ();
305         }
306 }
307
308 void 
309 Strip::notify_solo_changed ()
310 {
311         if (_route) {
312                 Button& button = solo();
313                 _surface->write (builder.build_led (button, _route->soloed()));
314         }
315 }
316
317 void 
318 Strip::notify_mute_changed ()
319 {
320         if (_route) {
321                 Button & button = mute();
322                 _surface->write (builder.build_led (button, _route->muted()));
323         }
324 }
325
326 void 
327 Strip::notify_record_enable_changed ()
328 {
329         if (_route) {
330                 Button & button = recenable();
331                 _surface->write (builder.build_led (button, _route->record_enabled()));
332         }
333 }
334
335 void 
336 Strip::notify_active_changed ()
337 {
338         _surface->mcp().refresh_current_bank();
339 }
340
341 void 
342 Strip::notify_route_deleted ()
343 {
344         _surface->mcp().refresh_current_bank();
345 }
346
347 void 
348 Strip::notify_gain_changed (bool force_update)
349 {
350         if (_route) {
351                 Fader & fader = gain();
352
353                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("route %1 gain change, update fader %2 on port %3 in-use ? %4\n", 
354                                                                    _route->name(), 
355                                                                    fader.raw_id(),
356                                                                    _surface->port().output_port().name(),
357                                                                    fader.in_use()));
358                 if (!fader.in_use()) {
359                         float gain_value = gain_to_slider_position (_route->gain_control()->get_value());
360                         // check that something has actually changed
361                         if (force_update || gain_value != _last_gain_written) {
362                                 _surface->write (builder.build_fader (fader, gain_value));
363                                 _last_gain_written = gain_value;
364                         } else {
365                                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader not updated because gain still equals %1\n", gain_value));
366                         }
367                 }
368         }
369 }
370
371 void 
372 Strip::notify_property_changed (const PropertyChange& what_changed)
373 {
374         if (!what_changed.contains (ARDOUR::Properties::name)) {
375                 return;
376         }
377
378         if (_route) {
379                 string line1;
380                 string fullname = _route->name();
381                 
382                 if (fullname.length() <= 6) {
383                         line1 = fullname;
384                 } else {
385                         line1 = PBD::short_version (fullname, 6);
386                 }
387                 
388                 _surface->write (builder.strip_display (*_surface, *this, 0, line1));
389                 _surface->write (builder.strip_display_blank (*_surface, *this, 1));
390         }
391 }
392
393 void 
394 Strip::notify_panner_changed (bool force_update)
395 {
396         if (_route) {
397                 Pot & pot = vpot();
398                 boost::shared_ptr<Panner> panner = _route->panner();
399                 if (panner) {
400                         double pos = panner->position ();
401
402                         // cache the MidiByteArray here, because the mackie led control is much lower
403                         // resolution than the panner control. So we save lots of byte
404                         // sends in spite of more work on the comparison
405                         MidiByteArray bytes = builder.build_led_ring (pot, ControlState (on, pos), MackieMidiBuilder::midi_pot_mode_dot);
406                         // check that something has actually changed
407                         if (force_update || bytes != _last_pan_written)
408                         {
409                                 _surface->write (bytes);
410                                 _last_pan_written = bytes;
411                         }
412                 } else {
413                         _surface->write (builder.zero_control (pot));
414                 }
415         }
416 }
417
418 bool 
419 Strip::handle_button (SurfacePort & port, Control & control, ButtonState bs)
420 {
421         if (!_route) {
422                 // no route so always switch the light off
423                 // because no signals will be emitted by a non-route
424                 _surface->write (builder.build_led (control.led(), off));
425                 return false;
426         }
427
428         bool state = false;
429
430         if (bs == press) {
431                 if (control.name() == "recenable") {
432                         state = !_route->record_enabled();
433                         _route->set_record_enabled (state, this);
434                 } else if (control.name() == "mute") {
435                         state = !_route->muted();
436                         _route->set_mute (state, this);
437                 } else if (control.name() == "solo") {
438                         state = !_route->soloed();
439                         _route->set_solo (state, this);
440                 } else if (control.name() == "select") {
441                         _surface->mcp().select_track (_route);
442                 } else if (control.name() == "vselect") {
443                         // TODO could be used to select different things to apply the pot to?
444                         //state = default_button_press (dynamic_cast<Button&> (control));
445                 }
446         }
447
448         if (control.name() == "fader_touch") {
449
450                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
451
452                 state = (bs == press);
453
454                 gain().set_in_use (state);
455                 
456                 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
457
458                         /* BCF faders don't support touch, so add a timeout to reset
459                            their `in_use' state.
460                         */
461
462                         _surface->mcp().add_in_use_timeout (*_surface, gain(), &fader_touch());
463                 }
464         }
465
466         return state;
467 }
468
469 void
470 Strip::periodic ()
471 {
472         if (!_route) {
473                 return;
474         }
475
476         update_automation ();
477         update_meter ();
478 }
479
480 void 
481 Strip::update_automation ()
482 {
483         ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
484
485         if (gain_state == Touch || gain_state == Play) {
486                 notify_gain_changed (false);
487         }
488
489         if (_route->panner()) {
490                 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
491                 if (panner_state == Touch || panner_state == Play) {
492                         notify_panner_changed (false);
493                 }
494         }
495 }
496
497 void
498 Strip::update_meter ()
499 {
500         float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
501         _surface->write (meter().update_message (dB));
502 }