convert rec-enable control for a Track from PBD::COntrollable to ARDOUR::AutomatioNCo...
[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/panner_shell.h"
36 #include "ardour/rc_configuration.h"
37 #include "ardour/meter.h"
38
39 #include "mackie_control_protocol.h"
40 #include "surface_port.h"
41 #include "surface.h"
42 #include "button.h"
43 #include "led.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         , _fader (0)
70         , _index (index)
71         , _surface (&s)
72         , _controls_locked (false)
73         , _last_gain_position_written (-1.0)
74         , _last_pan_position_written (-1.0)
75 {
76         /* build the controls for this track, which will automatically add them
77            to the Group 
78         */
79
80         for (uint32_t i = 0; ctls[i].name[0]; ++i) {
81                 ctls[i].factory (*_surface, ctls[i].base_id + index, ctls[i].name, *this);
82         }
83 }       
84
85 Strip::~Strip ()
86 {
87 }
88
89 /**
90         TODO could optimise this to use enum, but it's only
91         called during the protocol class instantiation.
92 */
93 void Strip::add (Control & control)
94 {
95         Group::add (control);
96
97         Fader* fader;
98         Pot* pot;
99         Button* button;
100         Meter* meter;
101
102         if ((fader = dynamic_cast<Fader*>(&control)) != 0) {
103
104                 _fader = fader;
105
106         } else if ((pot = dynamic_cast<Pot*>(&control)) != 0) {
107
108                 _vpot = pot;
109
110         } else if ((button = dynamic_cast<Button*>(&control)) != 0) {
111
112                 if (control.id() >= Button::recenable_base_id &&
113                     control.id() < Button::recenable_base_id + 8) {
114                         
115                         _recenable = button;
116
117                 } else if (control.id() >= Button::mute_base_id &&
118                            control.id() < Button::mute_base_id + 8) {
119
120                         _mute = button;
121
122                 } else if (control.id() >= Button::solo_base_id &&
123                            control.id() < Button::solo_base_id + 8) {
124
125                         _solo = button;
126
127                 } else if (control.id() >= Button::select_base_id &&
128                            control.id() < Button::select_base_id + 8) {
129
130                         _select = button;
131
132                 } else if (control.id() >= Button::vselect_base_id &&
133                            control.id() < Button::vselect_base_id + 8) {
134
135                         _vselect = button;
136
137                 } else if (control.id() >= Button::fader_touch_base_id &&
138                            control.id() < Button::fader_touch_base_id + 8) {
139
140                         _fader_touch = button;
141                 }
142
143         } else if ((meter = dynamic_cast<Meter*>(&control)) != 0) {
144                 _meter = meter;
145         }
146 }
147
148 void
149 Strip::set_route (boost::shared_ptr<Route> r)
150 {
151         if (_controls_locked) {
152                 return;
153         }
154
155         route_connections.drop_connections ();
156
157         _route = r;
158
159         if (r) {
160
161                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
162                                                                    _surface->number(), _index, _route->name()));
163                 
164
165                 if (_solo) {
166                         _solo->set_normal_control (_route->solo_control());
167                         _solo->set_modified_control (boost::shared_ptr<AutomationControl>());
168                         _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
169                 }
170
171                 if (_mute) {
172                         _mute->set_normal_control (_route->mute_control());
173                         _mute->set_modified_control (boost::shared_ptr<AutomationControl>());
174                         _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
175                 }
176                 
177                 _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
178                 
179                 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
180                 
181                 if (_route->pannable()) {
182                         _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
183                         _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
184                 }
185
186                 /* bind fader & pan pot, as appropriate for current flip mode */
187
188                 flip_mode_changed (false);
189                 
190                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
191         
192                 if (trk) {
193                         _recenable->set_normal_control (trk->rec_enable_control());
194                         _recenable->set_modified_control (boost::shared_ptr<AutomationControl>());
195                         trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
196                 }
197                 
198                 // TODO this works when a currently-banked route is made inactive, but not
199                 // when a route is activated which should be currently banked.
200                 
201                 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
202                 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
203         
204                 // TODO
205                 // SelectedChanged
206                 // RemoteControlIDChanged. Better handled at Session level.
207
208                 /* Update */
209
210                 notify_all ();
211         } else {
212                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
213                                                                    _surface->number(), _index));
214         }
215 }
216
217 void 
218 Strip::notify_all()
219 {
220         notify_solo_changed ();
221         notify_mute_changed ();
222         notify_gain_changed ();
223         notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
224         notify_panner_changed ();
225         notify_record_enable_changed ();
226 }
227
228 void 
229 Strip::notify_solo_changed ()
230 {
231         if (_route && _solo) {
232                 _surface->write (_solo->set_state (_route->soloed() ? on : off));
233         }
234 }
235
236 void 
237 Strip::notify_mute_changed ()
238 {
239         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Strip %1 mute changed\n", _index));
240         if (_route && _mute) {
241                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("\troute muted ? %1\n", _route->muted()));
242                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("mute message: %1\n", _mute->set_state (_route->muted() ? on : off)));
243
244                 _surface->write (_mute->set_state (_route->muted() ? on : off));
245         }
246 }
247
248 void 
249 Strip::notify_record_enable_changed ()
250 {
251         if (_route && _recenable)  {
252                 _surface->write (_recenable->set_state (_route->record_enabled() ? on : off));
253         }
254 }
255
256 void 
257 Strip::notify_active_changed ()
258 {
259         _surface->mcp().refresh_current_bank();
260 }
261
262 void 
263 Strip::notify_route_deleted ()
264 {
265         _surface->mcp().refresh_current_bank();
266 }
267
268 void 
269 Strip::notify_gain_changed (bool force_update)
270 {
271         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("gain changed for strip %1, flip mode %2\n", _index, _surface->mcp().flip_mode()));
272
273         if (_route) {
274                 
275                 Control* control;
276
277                 if (_surface->mcp().flip_mode()) {
278                         control = _vpot;
279                 } else {
280                         control = _fader;
281                 }
282
283                 if (!control->in_use()) {
284
285                         float pos = _route->gain_control()->internal_to_interface (_route->gain_control()->get_value());
286
287                         if (force_update || pos != _last_gain_position_written) {
288
289                                 if (_surface->mcp().flip_mode()) {
290                                         _surface->write (_vpot->set_all (pos, true, Pot::wrap));
291                                 } else {
292                                         _surface->write (_fader->set_position (pos));
293                                 }
294                                 _last_gain_position_written = pos;
295
296                         } else {
297                                 DEBUG_TRACE (DEBUG::MackieControl, "value is stale, no message sent\n");
298                         }
299                 } else {
300                         DEBUG_TRACE (DEBUG::MackieControl, "fader in use, no message sent\n");
301                 }
302         } else {
303                 DEBUG_TRACE (DEBUG::MackieControl, "no route or no fader\n");
304         }
305 }
306
307 void 
308 Strip::notify_property_changed (const PropertyChange& what_changed)
309 {
310         if (!what_changed.contains (ARDOUR::Properties::name)) {
311                 return;
312         }
313
314         if (_route) {
315                 string line1;
316                 string fullname = _route->name();
317                 
318                 if (fullname.length() <= 6) {
319                         line1 = fullname;
320                 } else {
321                         line1 = PBD::short_version (fullname, 6);
322                 }
323                 
324                 _surface->write (display (0, line1));
325         }
326 }
327
328 void 
329 Strip::notify_panner_changed (bool force_update)
330 {
331         if (_route) {
332
333                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("pan change for strip %1\n", _index));
334
335                 boost::shared_ptr<Pannable> pannable = _route->pannable();
336
337                 if (!pannable) {
338                         _surface->write (_vpot->zero());
339                         return;
340                 }
341
342                 Control* control;
343
344                 if (_surface->mcp().flip_mode()) {
345                         control = _fader;
346                 } else {
347                         control = _vpot;
348                 }
349
350                 if (!control->in_use()) {
351                         
352                         double pos = pannable->pan_azimuth_control->internal_to_interface (pannable->pan_azimuth_control->get_value());
353                         
354                         if (force_update || pos != _last_pan_position_written) {
355                                 if (_surface->mcp().flip_mode()) {
356                                         _surface->write (_fader->set_position (pos));
357                                 } else {
358                                         _surface->write (_vpot->set_all (pos, true, Pot::dot));
359                                 }
360                                 _last_pan_position_written = pos;
361                         }
362                 }
363         }
364 }
365
366 void
367 Strip::handle_button (Button& button, ButtonState bs)
368 {
369         button.set_in_use (bs == press);
370
371         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip %1 handling button %2\n", _index, button.id()));
372
373         if (bs != press) {
374                 return;
375         }
376
377         int lock_mod = (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT);
378         int ms = _surface->mcp().modifier_state();
379         bool modified = (ms & MackieControlProtocol::MODIFIER_CONTROL);
380
381         if (button.id() >= Button::select_base_id &&
382             button.id() < Button::select_base_id + 8) {
383
384                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("select touch, lock ? %1\n", ((ms & lock_mod) == lock_mod) ? 1 : 0));
385
386                 if ((ms & lock_mod) == lock_mod) {
387                         _controls_locked = !_controls_locked;
388                         return;
389                 }
390                 
391                 if (_route) {
392                         _surface->mcp().select_track (_route);
393                 }
394
395                 return;
396         }
397
398         if (button.id() >= Button::fader_touch_base_id &&
399             button.id() < Button::fader_touch_base_id + 8) {
400
401                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
402
403                 bool state = (bs == press);
404                 
405                 _fader->set_in_use (state);
406                 _fader->start_touch (_surface->mcp().transport_frame(), modified);
407
408                 if (!_surface->mcp().device_info().has_touch_sense_faders()) {
409                         _surface->mcp().add_in_use_timeout (*_surface, *_fader, _fader->control (modified));
410                 }
411
412                 return;
413         }
414
415         boost::shared_ptr<AutomationControl> control = button.control (modified);
416                 
417         if (control) {
418                 if (ms & MackieControlProtocol::MODIFIER_OPTION) {
419                         /* reset to default/normal value */
420                         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("reset %1 to default of %2\n", control->name(), control->normal()));
421                         control->set_value (control->normal());
422                 } else {
423                         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("toggle %1 to default of %2\n", control->name(), control->get_value() ? 0.0 : 1.0));
424                         control->set_value (control->get_value() ? 0.0 : 1.0);
425                 }
426         } else {
427                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("button has no control at present (modified ? %1)\n", modified));
428         }
429 }
430
431 void
432 Strip::handle_fader (Fader& fader, float position)
433 {
434         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", position));
435
436         bool modified = (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL);
437
438         fader.set_value (position, modified);
439         fader.start_touch (_surface->mcp().transport_frame(), modified);
440
441         if (!_surface->mcp().device_info().has_touch_sense_faders()) {
442                 _surface->mcp().add_in_use_timeout (*_surface, fader, fader.control (modified));
443         }
444         
445         // must echo bytes back to slider now, because
446         // the notifier only works if the fader is not being
447         // touched. Which it is if we're getting input.
448
449         _surface->write (fader.set_position (position));
450 }
451
452 void
453 Strip::handle_pot (Pot& pot, float delta)
454 {
455         /* Pots only emit events when they move, not when they
456            stop moving. So to get a stop event, we need to use a timeout.
457         */
458         
459         bool modified = (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL);
460         pot.start_touch (_surface->mcp().transport_frame(), modified);
461         _surface->mcp().add_in_use_timeout (*_surface, pot, pot.control (modified));
462
463         double p = pot.get_value (modified);
464         p += delta;
465         p = min (1.0, p);
466         p = max (0.0, p);
467         pot.set_value (p, modified);
468 }
469
470 void
471 Strip::periodic ()
472 {
473         if (!_route) {
474                 return;
475         }
476
477         update_automation ();
478         update_meter ();
479 }
480
481 void 
482 Strip::update_automation ()
483 {
484         ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
485
486         if (gain_state == Touch || gain_state == Play) {
487                 notify_gain_changed (false);
488         }
489
490         if (_route->panner()) {
491                 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
492                 if (panner_state == Touch || panner_state == Play) {
493                         notify_panner_changed (false);
494                 }
495         }
496 }
497
498 void
499 Strip::update_meter ()
500 {
501         if (_meter) {
502                 float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
503                 _surface->write (_meter->update_message (dB));
504         }
505 }
506
507 MidiByteArray
508 Strip::zero ()
509 {
510         MidiByteArray retval;
511
512         for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
513                 retval << (*it)->zero ();
514         }
515
516         retval << blank_display (0);
517         retval << blank_display (1);
518         
519         return retval;
520 }
521
522 MidiByteArray
523 Strip::blank_display (uint32_t line_number)
524 {
525         return display (line_number, string());
526 }
527
528 MidiByteArray
529 Strip::display (uint32_t line_number, const std::string& line)
530 {
531         assert (line_number <= 1);
532
533         MidiByteArray retval;
534
535         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
536
537         // sysex header
538         retval << _surface->sysex_hdr();
539         
540         // code for display
541         retval << 0x12;
542         // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
543         retval << (_index * 7 + (line_number * 0x38));
544         
545         // ascii data to display
546         retval << line;
547         // pad with " " out to 6 chars
548         for (int i = line.length(); i < 6; ++i) {
549                 retval << ' ';
550         }
551         
552         // column spacer, unless it's the right-hand column
553         if (_index < 7) {
554                 retval << ' ';
555         }
556
557         // sysex trailer
558         retval << MIDI::eox;
559         
560         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));
561
562         return retval;
563 }
564
565 void
566 Strip::lock_controls ()
567 {
568         _controls_locked = true;
569 }
570
571 void
572 Strip::unlock_controls ()
573 {
574         _controls_locked = false;
575 }
576
577 MidiByteArray
578 Strip::gui_selection_changed (ARDOUR::RouteNotificationListPtr rl)
579 {
580         for (ARDOUR::RouteNotificationList::iterator i = rl->begin(); i != rl->end(); ++i) {
581                 if ((*i) == _route) {
582                         return _select->set_state (on);
583                 }
584         }
585
586         return _select->set_state (off);
587 }
588
589 void
590 Strip::flip_mode_changed (bool notify)
591 {
592         if (!_route) {
593                 return;
594         }
595
596         boost::shared_ptr<Pannable> pannable = _route->pannable();
597
598         if (_surface->mcp().flip_mode()) {
599
600                 if (pannable) {
601                         _fader->set_normal_control (pannable->pan_azimuth_control);
602                         _fader->set_modified_control (pannable->pan_width_control);
603                 }
604                 _vpot->set_normal_control (_route->gain_control());
605                 _vpot->set_modified_control (boost::shared_ptr<AutomationControl>());
606
607                 _surface->write (display (1, "Fader"));
608
609         } else {
610
611                 if (pannable) {
612                         _vpot->set_normal_control (pannable->pan_azimuth_control);
613                         _vpot->set_modified_control (pannable->pan_width_control);
614                 }
615                 _fader->set_normal_control (_route->gain_control());
616                 _fader->set_modified_control (boost::shared_ptr<AutomationControl>());
617
618                 _surface->write (display (1, "Pan"));
619         }
620
621         if (notify) {
622                 notify_all ();
623         }
624 }