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