MCP: probably fix issues with vpots not working
[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 <sys/time.h>
25
26 #include "midi++/port.h"
27
28 #include "pbd/compose.h"
29 #include "pbd/convert.h"
30
31 #include "ardour/debug.h"
32 #include "ardour/midi_ui.h"
33 #include "ardour/route.h"
34 #include "ardour/track.h"
35 #include "ardour/pannable.h"
36 #include "ardour/panner.h"
37 #include "ardour/panner_shell.h"
38 #include "ardour/rc_configuration.h"
39 #include "ardour/meter.h"
40
41 #include "mackie_control_protocol.h"
42 #include "surface_port.h"
43 #include "surface.h"
44 #include "button.h"
45 #include "led.h"
46 #include "pot.h"
47 #include "fader.h"
48 #include "jog.h"
49 #include "meter.h"
50
51 using namespace Mackie;
52 using namespace std;
53 using namespace ARDOUR;
54 using namespace PBD;
55
56 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
57 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
58
59 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
60 #define invalidator() __invalidator (*(MackieControlProtocol::instance()), __FILE__, __LINE__)
61
62 Strip::Strip (Surface& s, const std::string& name, int index, const map<Button::ID,StripButtonInfo>& strip_buttons)
63         : Group (name)
64         , _solo (0)
65         , _recenable (0)
66         , _mute (0)
67         , _select (0)
68         , _vselect (0)
69         , _fader_touch (0)
70         , _vpot (0)
71         , _fader (0)
72         , _index (index)
73         , _surface (&s)
74         , _controls_locked (false)
75         , _reset_display_at (0)
76         , _last_gain_position_written (-1.0)
77         , _last_pan_position_written (-1.0)
78 {
79         _fader = dynamic_cast<Fader*> (Fader::factory (*_surface, index, "fader", *this));
80         _vpot = dynamic_cast<Pot*> (Pot::factory (*_surface, Pot::ID + index, "vpot", *this));
81         _meter = dynamic_cast<Meter*> (Meter::factory (*_surface, index, "meter", *this));
82
83         for (map<Button::ID,StripButtonInfo>::const_iterator b = strip_buttons.begin(); b != strip_buttons.end(); ++b) {
84                 (void) Button::factory (*_surface, b->first, b->second.base_id + index, b->second.name, *this);
85         }
86 }       
87
88 Strip::~Strip ()
89 {
90         /* surface is responsible for deleting all controls */
91 }
92
93 void 
94 Strip::add (Control & control)
95 {
96         Button* button;
97
98         Group::add (control);
99
100         /* fader, vpot, meter were all set explicitly */
101
102         if ((button = dynamic_cast<Button*>(&control)) != 0) {
103                 switch (button->bid()) {
104                 case Button::RecEnable:
105                         _recenable = button;
106                         break;
107                 case Button::Mute:
108                         _mute = button;
109                         break;
110                 case Button::Solo:
111                         _solo = button;
112                         break;
113                 case Button::Select:
114                         _select = button;
115                         break;
116                 case Button::VSelect:
117                         _vselect = button;
118                         break;
119                 case Button::FaderTouch:
120                         _fader_touch = button;
121                 default:
122                         break;
123                 }
124         }
125 }
126
127 void
128 Strip::set_route (boost::shared_ptr<Route> r)
129 {
130         if (_controls_locked) {
131                 return;
132         }
133
134         route_connections.drop_connections ();
135
136         _route = r;
137
138         if (r) {
139
140                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
141                                                                    _surface->number(), _index, _route->name()));
142                 
143
144                 if (_solo) {
145                         _solo->set_normal_control (_route->solo_control());
146                         _solo->set_modified_control (boost::shared_ptr<AutomationControl>());
147                         _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
148                 }
149
150                 if (_mute) {
151                         _mute->set_normal_control (_route->mute_control());
152                         _mute->set_modified_control (boost::shared_ptr<AutomationControl>());
153                         _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
154                 }
155                 
156                 _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
157                 
158                 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
159                 
160                 if (_route->pannable()) {
161                         _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
162                         _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
163                 }
164
165                 /* bind fader & pan pot, as appropriate for current flip mode */
166
167                 flip_mode_changed (false);
168                 
169                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
170         
171                 if (trk) {
172                         _recenable->set_normal_control (trk->rec_enable_control());
173                         _recenable->set_modified_control (boost::shared_ptr<AutomationControl>());
174                         trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
175                 }
176                 
177                 // TODO this works when a currently-banked route is made inactive, but not
178                 // when a route is activated which should be currently banked.
179                 
180                 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
181                 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
182         
183                 // TODO
184                 // SelectedChanged
185                 // RemoteControlIDChanged. Better handled at Session level.
186
187                 /* Update */
188
189                 notify_all ();
190         } else {
191                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
192                                                                    _surface->number(), _index));
193         }
194 }
195
196 void 
197 Strip::notify_all()
198 {
199         notify_solo_changed ();
200         notify_mute_changed ();
201         notify_gain_changed ();
202         notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
203         notify_panner_changed ();
204         notify_record_enable_changed ();
205 }
206
207 void 
208 Strip::notify_solo_changed ()
209 {
210         if (_route && _solo) {
211                 _surface->write (_solo->set_state (_route->soloed() ? on : off));
212         }
213 }
214
215 void 
216 Strip::notify_mute_changed ()
217 {
218         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Strip %1 mute changed\n", _index));
219         if (_route && _mute) {
220                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("\troute muted ? %1\n", _route->muted()));
221                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("mute message: %1\n", _mute->set_state (_route->muted() ? on : off)));
222
223                 _surface->write (_mute->set_state (_route->muted() ? on : off));
224         }
225 }
226
227 void 
228 Strip::notify_record_enable_changed ()
229 {
230         if (_route && _recenable)  {
231                 _surface->write (_recenable->set_state (_route->record_enabled() ? on : off));
232         }
233 }
234
235 void 
236 Strip::notify_active_changed ()
237 {
238         _surface->mcp().refresh_current_bank();
239 }
240
241 void 
242 Strip::notify_route_deleted ()
243 {
244         _surface->mcp().refresh_current_bank();
245 }
246
247 void 
248 Strip::notify_gain_changed (bool force_update)
249 {
250         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("gain changed for strip %1, flip mode %2\n", _index, _surface->mcp().flip_mode()));
251
252         if (_route) {
253                 
254                 Control* control;
255
256                 if (_surface->mcp().flip_mode()) {
257                         control = _vpot;
258                 } else {
259                         control = _fader;
260                 }
261
262                 if (!control->in_use()) {
263
264                         float pos = _route->gain_control()->internal_to_interface (_route->gain_control()->get_value());
265
266                         if (force_update || pos != _last_gain_position_written) {
267
268                                 if (_surface->mcp().flip_mode()) {
269                                         _surface->write (_vpot->set_all (pos, true, Pot::wrap));
270                                         do_parameter_display (GainAutomation, pos);
271                                 } else {
272                                         _surface->write (_fader->set_position (pos));
273                                         do_parameter_display (GainAutomation, pos);
274                                 }
275
276                                 queue_display_reset (2000);
277                                 _last_gain_position_written = pos;
278                                 
279                         } else {
280                                 DEBUG_TRACE (DEBUG::MackieControl, "value is stale, no message sent\n");
281                         }
282                 } else {
283                         DEBUG_TRACE (DEBUG::MackieControl, "fader in use, no message sent\n");
284                 }
285         } else {
286                 DEBUG_TRACE (DEBUG::MackieControl, "no route or no fader\n");
287         }
288 }
289
290 void 
291 Strip::notify_property_changed (const PropertyChange& what_changed)
292 {
293         if (!what_changed.contains (ARDOUR::Properties::name)) {
294                 return;
295         }
296
297         if (_route) {
298                 string line1;
299                 string fullname = _route->name();
300                 
301                 if (fullname.length() <= 6) {
302                         line1 = fullname;
303                 } else {
304                         line1 = PBD::short_version (fullname, 6);
305                 }
306                 
307                 _surface->write (display (0, line1));
308         }
309 }
310
311 void 
312 Strip::notify_panner_changed (bool force_update)
313 {
314         if (_route) {
315
316                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("pan change for strip %1\n", _index));
317
318                 boost::shared_ptr<Pannable> pannable = _route->pannable();
319
320                 if (!pannable) {
321                         _surface->write (_vpot->zero());
322                         return;
323                 }
324
325                 Control* control;
326
327                 if (_surface->mcp().flip_mode()) {
328                         control = _fader;
329                 } else {
330                         control = _vpot;
331                 }
332
333                 if (!control->in_use()) {
334                         
335                         double pos = pannable->pan_azimuth_control->internal_to_interface (pannable->pan_azimuth_control->get_value());
336                         
337                         if (force_update || pos != _last_pan_position_written) {
338                                 
339                                 if (_surface->mcp().flip_mode()) {
340                                         
341                                         _surface->write (_fader->set_position (pos));
342                                         do_parameter_display (PanAzimuthAutomation, pos);
343                                 } else {
344                                         _surface->write (_vpot->set_all (pos, true, Pot::dot));
345                                         do_parameter_display (PanAzimuthAutomation, pos);
346                                 }
347
348                                 queue_display_reset (2000);
349                                 _last_pan_position_written = pos;
350                         }
351                 }
352         }
353 }
354
355 void
356 Strip::handle_button (Button& button, ButtonState bs)
357 {
358         if (bs == press) {
359                 button.set_in_use (true);
360         } else {
361                 button.set_in_use (false);
362         }
363
364         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip %1 handling button %2 press ? %3\n", _index, button.bid(), (bs == press)));
365
366         int lock_mod = (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT);
367         int ms = _surface->mcp().modifier_state();
368         bool modified = (ms & MackieControlProtocol::MODIFIER_CONTROL);
369
370         if (button.bid() == Button::Select) {
371
372                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("select touch, lock ? %1\n", ((ms & lock_mod) == lock_mod) ? 1 : 0));
373
374                 if (bs == press) {
375
376                         if ((ms & lock_mod) == lock_mod) {
377                                 _controls_locked = !_controls_locked;
378                                 return;
379                         }
380                         
381                         DEBUG_TRACE (DEBUG::MackieControl, "add select button on press\n");
382                         _surface->mcp().add_down_select_button (_surface->number(), _index);                    
383
384                         _surface->mcp().select_range ();
385
386                 } else {
387                         DEBUG_TRACE (DEBUG::MackieControl, "remove select button on release\n");
388                         _surface->mcp().remove_down_select_button (_surface->number(), _index);                 
389                 }
390
391                 return;
392         }
393         
394         if (button.bid() == Button::FaderTouch) {
395
396                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
397
398                 /* never use the modified control for fader stuff */
399
400                 if (bs == press) {
401
402                         _fader->set_in_use (true);
403                         _fader->start_touch (_surface->mcp().transport_frame(), false);
404                         boost::shared_ptr<AutomationControl> ac = _fader->control (false);
405                         if (ac) {
406                                 do_parameter_display ((AutomationType) ac->parameter().type(), ac->internal_to_interface (ac->get_value()));
407                                 queue_display_reset (2000);
408                         }
409
410                 } else {
411
412                         _fader->set_in_use (false);
413                         _fader->stop_touch (_surface->mcp().transport_frame(), true, false);
414
415                 }
416                 
417                 return;
418         }
419
420         boost::shared_ptr<AutomationControl> control = button.control (modified);
421                 
422         if (control) {
423
424                 if (bs == press) {
425                         DEBUG_TRACE (DEBUG::MackieControl, "add button on release\n");
426                         _surface->mcp().add_down_button ((AutomationType) control->parameter().type(), _surface->number(), _index);
427                         
428                         float new_value;
429
430                         if (ms & MackieControlProtocol::MODIFIER_OPTION) {
431                                 /* reset to default/normal value */
432                                 new_value = control->normal();
433                         } else {
434                                 new_value = control->get_value() ? 0.0 : 1.0;
435                         }
436
437                         /* get all controls that either have their
438                          * button down or are within a range of
439                          * several down buttons
440                          */
441                         
442                         MackieControlProtocol::ControlList controls = _surface->mcp().down_controls ((AutomationType) control->parameter().type());
443                         
444                         
445                         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("there are %1 buttons down for control type %2, new value = %3\n",
446                                                                             controls.size(), control->parameter().type(), new_value));
447
448                         /* apply change */
449
450                         for (MackieControlProtocol::ControlList::iterator c = controls.begin(); c != controls.end(); ++c) {
451                                 (*c)->set_value (new_value);
452                         }
453
454                 } else {
455                         DEBUG_TRACE (DEBUG::MackieControl, "remove button on release\n");
456                         _surface->mcp().remove_down_button ((AutomationType) control->parameter().type(), _surface->number(), _index);
457                 }
458                         
459         } else {
460                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("button has no control at present (modified ? %1)\n", modified));
461         }
462 }
463
464 void
465 Strip::do_parameter_display (AutomationType type, float val)
466 {
467         float dB;
468
469         switch (type) {
470         case GainAutomation:
471                 dB = fast_coefficient_to_dB (val);
472                 if (val == 0.0) {
473                         _surface->write (display (1, " -inf "));
474                 } else {
475                         char buf[16];
476                         
477                         snprintf (buf, sizeof (buf), "%6.1f", dB);
478                         _surface->write (display (1, buf));
479                 }               
480                 break;
481
482         case PanAzimuthAutomation:
483                 if (_route) {
484                         boost::shared_ptr<Pannable> p = _route->pannable();
485                         if (p && p->panner()) {
486                                 string str = p->panner()->value_as_string (p->pan_azimuth_control);
487                                 _surface->write (display (1, str));
488                         }
489                 }
490         default:
491                 break;
492         }
493 }
494
495 void
496 Strip::handle_fader (Fader& fader, float position)
497 {
498         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", position));
499
500         bool modified = (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL);
501
502         fader.set_value (position, modified);
503         fader.start_touch (_surface->mcp().transport_frame(), modified);
504         queue_display_reset (2000);
505
506         // must echo bytes back to slider now, because
507         // the notifier only works if the fader is not being
508         // touched. Which it is if we're getting input.
509
510         _surface->write (fader.set_position (position));
511 }
512
513 void
514 Strip::handle_pot (Pot& pot, float delta)
515 {
516         /* Pots only emit events when they move, not when they
517            stop moving. So to get a stop event, we need to use a timeout.
518         */
519         
520         bool modified = (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL);
521         pot.start_touch (_surface->mcp().transport_frame(), modified);
522         _surface->mcp().add_in_use_timeout (*_surface, pot, pot.control (modified));
523
524         double p = pot.get_value (modified);
525         p += delta;
526         p = min (1.0, p);
527         p = max (0.0, p);
528         pot.set_value (p, modified);
529 }
530
531 void
532 Strip::periodic (uint64_t usecs)
533 {
534         if (!_route) {
535                 return;
536         }
537
538         update_automation ();
539         update_meter ();
540
541         if (_reset_display_at && _reset_display_at < usecs) {
542                 reset_display ();
543         }
544 }
545
546 void 
547 Strip::update_automation ()
548 {
549         ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
550
551         if (gain_state == Touch || gain_state == Play) {
552                 notify_gain_changed (false);
553         }
554
555         if (_route->panner()) {
556                 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
557                 if (panner_state == Touch || panner_state == Play) {
558                         notify_panner_changed (false);
559                 }
560         }
561 }
562
563 void
564 Strip::update_meter ()
565 {
566         if (_meter) {
567                 float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
568                 _surface->write (_meter->update_message (dB));
569         }
570 }
571
572 MidiByteArray
573 Strip::zero ()
574 {
575         MidiByteArray retval;
576
577         for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
578                 retval << (*it)->zero ();
579         }
580
581         retval << blank_display (0);
582         retval << blank_display (1);
583         
584         return retval;
585 }
586
587 MidiByteArray
588 Strip::blank_display (uint32_t line_number)
589 {
590         return display (line_number, string());
591 }
592
593 MidiByteArray
594 Strip::display (uint32_t line_number, const std::string& line)
595 {
596         assert (line_number <= 1);
597
598         MidiByteArray retval;
599
600         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
601
602         // sysex header
603         retval << _surface->sysex_hdr();
604         
605         // code for display
606         retval << 0x12;
607         // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
608         retval << (_index * 7 + (line_number * 0x38));
609         
610         // ascii data to display
611         retval << line;
612         // pad with " " out to 6 chars
613         for (int i = line.length(); i < 6; ++i) {
614                 retval << ' ';
615         }
616         
617         // column spacer, unless it's the right-hand column
618         if (_index < 7) {
619                 retval << ' ';
620         }
621
622         // sysex trailer
623         retval << MIDI::eox;
624         
625         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));
626
627         return retval;
628 }
629
630 void
631 Strip::lock_controls ()
632 {
633         _controls_locked = true;
634 }
635
636 void
637 Strip::unlock_controls ()
638 {
639         _controls_locked = false;
640 }
641
642 MidiByteArray
643 Strip::gui_selection_changed (ARDOUR::RouteNotificationListPtr rl)
644 {
645         for (ARDOUR::RouteNotificationList::iterator i = rl->begin(); i != rl->end(); ++i) {
646                 if ((*i) == _route) {
647                         return _select->set_state (on);
648                 }
649         }
650
651         return _select->set_state (off);
652 }
653
654 string
655 Strip::static_display_string () const
656 {
657         if (_surface->mcp().flip_mode()) {
658                 return "Fader";
659         } else {
660                 return "Pan";
661         }
662 }
663
664 void
665 Strip::flip_mode_changed (bool notify)
666 {
667         if (!_route) {
668                 return;
669         }
670
671         boost::shared_ptr<Pannable> pannable = _route->pannable();
672
673         if (_surface->mcp().flip_mode()) {
674
675                 if (pannable) {
676                         _fader->set_normal_control (pannable->pan_azimuth_control);
677                         _fader->set_modified_control (pannable->pan_width_control);
678                 }
679                 _vpot->set_normal_control (_route->gain_control());
680                 _vpot->set_modified_control (boost::shared_ptr<AutomationControl>());
681
682                 _surface->write (display (1, static_display_string ()));
683
684         } else {
685
686                 if (pannable) {
687                         _vpot->set_normal_control (pannable->pan_azimuth_control);
688                         _vpot->set_modified_control (pannable->pan_width_control);
689                 }
690                 _fader->set_normal_control (_route->gain_control());
691                 _fader->set_modified_control (boost::shared_ptr<AutomationControl>());
692
693                 _surface->write (display (1, static_display_string()));
694         }
695
696         if (notify) {
697                 notify_all ();
698         }
699 }
700
701 void
702 Strip::queue_display_reset (uint32_t msecs)
703 {
704         struct timeval now;
705         struct timeval delta;
706         struct timeval when;
707         gettimeofday (&now, 0);
708         
709         delta.tv_sec = msecs/1000;
710         delta.tv_usec = (msecs - ((msecs/1000) * 1000)) * 1000;
711         
712         timeradd (&now, &delta, &when);
713
714         _reset_display_at = (when.tv_sec * 1000000) + when.tv_usec;
715 }
716
717 void
718 Strip::clear_display_reset ()
719 {
720         _reset_display_at = 0;
721 }
722
723 void
724 Strip::reset_display ()
725 {
726         _surface->write (display (1, static_display_string()));
727         clear_display_reset ();
728 }
729