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