9c688628726e3bba05d2fc43e0d452b90fb008ae
[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                 _surface->write (blank_display (1));
408         }
409 }
410
411 void 
412 Strip::notify_panner_changed (bool force_update)
413 {
414         if (_route) {
415                 Pot & pot = vpot();
416                 boost::shared_ptr<Panner> panner = _route->panner();
417
418                 if (panner) {
419                         double pos = panner->position ();
420
421                         // cache the MidiByteArray here, because the mackie led control is much lower
422                         // resolution than the panner control. So we save lots of byte
423                         // sends in spite of more work on the comparison
424
425                         MidiByteArray bytes = pot.set_all (pos, true, Pot::dot);
426
427                         // check that something has actually changed
428                         if (force_update || bytes != _last_pan_written)
429                         {
430                                 _surface->write (bytes);
431                                 _last_pan_written = bytes;
432                         }
433                 } else {
434                         _surface->write (pot.zero());
435                 }
436         }
437 }
438
439 void
440 Strip::handle_button (Button& button, ButtonState bs)
441 {
442         button.set_in_use (bs == press);
443
444         if (!_route) {
445                 // no route so always switch the light off
446                 // because no signals will be emitted by a non-route
447                 _surface->write (button.led().set_state  (off));
448                 return;
449         }
450
451         if (bs == press) {
452                 if (button.id() >= Button::recenable_base_id &&
453                     button.id() < Button::recenable_base_id + 8) {
454
455                         _route->set_record_enabled (!_route->record_enabled(), this);
456
457                 } else if (button.id() >= Button::mute_base_id &&
458                            button.id() < Button::mute_base_id + 8) {
459
460                         _route->set_mute (!_route->muted(), this);
461
462                 } else if (button.id() >= Button::solo_base_id &&
463                            button.id() < Button::solo_base_id + 8) {
464
465                         _route->set_solo (!_route->soloed(), this);
466
467                 } else if (button.id() >= Button::select_base_id &&
468                            button.id() < Button::select_base_id + 8) {
469
470                         _surface->mcp().select_track (_route);
471
472                 } else if (button.id() >= Button::vselect_base_id &&
473                            button.id() < Button::vselect_base_id + 8) {
474
475                 }
476         }
477
478         if (button.id() >= Button::fader_touch_base_id &&
479             button.id() < Button::fader_touch_base_id + 8) {
480
481                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
482
483                 bool state = (bs == press);
484
485                 _gain->set_in_use (state);
486                 
487                 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
488
489                         /* BCF faders don't support touch, so add a timeout to reset
490                            their `in_use' state.
491                         */
492
493                         _surface->mcp().add_in_use_timeout (*_surface, gain(), &fader_touch());
494                 }
495         }
496 }
497
498 void
499 Strip::handle_fader (Fader& fader, float position)
500 {
501         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", position));
502
503         switch (_surface->mcp().flip_mode()) {
504         case MackieControlProtocol::Normal:
505                 break;
506         case MackieControlProtocol::Zero:
507                 break;
508         case MackieControlProtocol::Mirror:
509                 break;
510         case MackieControlProtocol::Swap:
511                 if (_vpot) {
512                         handle_pot (*_vpot, 1.0);
513                 }
514                 return;
515         }
516
517         if (_route) {
518                 _route->gain_control()->set_value (slider_position_to_gain (position));
519         }
520         
521         if (ARDOUR::Config->get_mackie_emulation() == "bcf") {
522                 /* reset the timeout while we're still moving the fader */
523                 _surface->mcp().add_in_use_timeout (*_surface, fader, fader.in_use_touch_control);
524         }
525         
526         // must echo bytes back to slider now, because
527         // the notifier only works if the fader is not being
528         // touched. Which it is if we're getting input.
529
530         _surface->write (fader.set_position (position));
531 }
532
533 void
534 Strip::handle_pot (Pot& pot, float delta)
535 {
536         if (!_route) {
537                 _surface->write (pot.set_onoff (false));
538                 return;
539         }
540
541         boost::shared_ptr<Pannable> pannable = _route->pannable();
542
543         if (pannable) {
544                 boost::shared_ptr<AutomationControl> ac;
545                 
546                 if (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL) {
547                         ac = pannable->pan_width_control;
548                 } else {
549                         ac = pannable->pan_azimuth_control;
550                 }
551                 
552                 double p = ac->get_value();
553                 
554                 // calculate new value, and adjust
555                 p += delta;
556                 p = min (1.0, p);
557                 p = max (0.0, p);
558
559                 ac->set_value (p);
560         }
561 }
562
563 void
564 Strip::periodic ()
565 {
566         if (!_route) {
567                 return;
568         }
569
570         update_automation ();
571         update_meter ();
572 }
573
574 void 
575 Strip::update_automation ()
576 {
577         ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
578
579         if (gain_state == Touch || gain_state == Play) {
580                 notify_gain_changed (false);
581         }
582
583         if (_route->panner()) {
584                 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
585                 if (panner_state == Touch || panner_state == Play) {
586                         notify_panner_changed (false);
587                 }
588         }
589 }
590
591 void
592 Strip::update_meter ()
593 {
594         float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
595         _surface->write (meter().update_message (dB));
596 }
597
598 MidiByteArray
599 Strip::zero ()
600 {
601         MidiByteArray retval;
602
603         for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
604                 retval << (*it)->zero ();
605         }
606
607         retval << blank_display (0);
608         retval << blank_display (1);
609         
610         return retval;
611 }
612
613 MidiByteArray
614 Strip::blank_display (uint32_t line_number)
615 {
616         return display (line_number, string());
617 }
618
619 MidiByteArray
620 Strip::display (uint32_t line_number, const std::string& line)
621 {
622         assert (line_number <= 1);
623
624         MidiByteArray retval;
625
626         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
627
628         // sysex header
629         retval << _surface->sysex_hdr();
630         
631         // code for display
632         retval << 0x12;
633         // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
634         retval << (_index * 7 + (line_number * 0x38));
635         
636         // ascii data to display
637         retval << line;
638         // pad with " " out to 6 chars
639         for (int i = line.length(); i < 6; ++i) {
640                 retval << ' ';
641         }
642         
643         // column spacer, unless it's the right-hand column
644         if (_index < 7) {
645                 retval << ' ';
646         }
647
648         // sysex trailer
649         retval << MIDI::eox;
650         
651         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));
652
653         return retval;
654 }