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