move all code to construct MIDI messages into relevant Control/Strip/Surface object...
[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/rc_configuration.h"
36 #include "ardour/meter.h"
37
38 #include "mackie_control_protocol.h"
39 #include "surface_port.h"
40 #include "surface.h"
41 #include "button.h"
42 #include "led.h"
43 #include "pot.h"
44 #include "fader.h"
45 #include "jog.h"
46 #include "meter.h"
47
48 using namespace Mackie;
49 using namespace std;
50 using namespace ARDOUR;
51 using namespace PBD;
52
53 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
54 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
55
56 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
57 #define invalidator() __invalidator (*(MackieControlProtocol::instance()), __FILE__, __LINE__)
58
59 Strip::Strip (Surface& s, const std::string& name, int index, StripControlDefinition* ctls)
60         : Group (name)
61         , _solo (0)
62         , _recenable (0)
63         , _mute (0)
64         , _select (0)
65         , _vselect (0)
66         , _fader_touch (0)
67         , _vpot (0)
68         , _gain (0)
69         , _index (index)
70         , _surface (&s)
71 {
72         /* build the controls for this track, which will automatically add them
73            to the Group 
74         */
75
76         for (uint32_t i = 0; ctls[i].name[0]; ++i) {
77                 ctls[i].factory (*_surface, ctls[i].base_id + index, ctls[i].name, *this);
78         }
79 }       
80
81 Strip::~Strip ()
82 {
83         
84 }
85
86 /**
87         TODO could optimise this to use enum, but it's only
88         called during the protocol class instantiation.
89 */
90 void Strip::add (Control & control)
91 {
92         Group::add (control);
93
94         if  (control.name() == "gain") {
95                 _gain = reinterpret_cast<Fader*>(&control);
96         } else if  (control.name() == "vpot") {
97                 _vpot = reinterpret_cast<Pot*>(&control);
98         } else if  (control.name() == "recenable") {
99                 _recenable = reinterpret_cast<Button*>(&control);
100         } else if  (control.name() == "solo") {
101                 _solo = reinterpret_cast<Button*>(&control);
102         } else if  (control.name() == "mute") {
103                 _mute = reinterpret_cast<Button*>(&control);
104         } else if  (control.name() == "select") {
105                 _select = reinterpret_cast<Button*>(&control);
106         } else if  (control.name() == "vselect") {
107                 _vselect = reinterpret_cast<Button*>(&control);
108         } else if  (control.name() == "fader_touch") {
109                 _fader_touch = reinterpret_cast<Button*>(&control);
110         } else if  (control.name() == "meter") {
111                 _meter = reinterpret_cast<Meter*>(&control);
112         } else {
113                 // relax 
114         }
115 }
116
117 Fader& 
118 Strip::gain()
119 {
120         if  (_gain == 0) {
121                 throw MackieControlException ("gain is null");
122         }
123         return *_gain;
124 }
125
126 Pot& 
127 Strip::vpot()
128 {
129         if  (_vpot == 0) {
130                 throw MackieControlException ("vpot is null");
131         }
132         return *_vpot;
133 }
134
135 Button& 
136 Strip::recenable()
137 {
138         if  (_recenable == 0) {
139                 throw MackieControlException ("recenable is null");
140         }
141         return *_recenable;
142 }
143
144 Button& 
145 Strip::solo()
146 {
147         if  (_solo == 0) {
148                 throw MackieControlException ("solo is null");
149         }
150         return *_solo;
151 }
152 Button& 
153 Strip::mute()
154 {
155         if  (_mute == 0) {
156                 throw MackieControlException ("mute is null");
157         }
158         return *_mute;
159 }
160
161 Button& 
162 Strip::select()
163 {
164         if  (_select == 0) {
165                 throw MackieControlException ("select is null");
166         }
167         return *_select;
168 }
169
170 Button& 
171 Strip::vselect()
172 {
173         if  (_vselect == 0) {
174                 throw MackieControlException ("vselect is null");
175         }
176         return *_vselect;
177 }
178
179 Button& 
180 Strip::fader_touch()
181 {
182         if  (_fader_touch == 0) {
183                 throw MackieControlException ("fader_touch is null");
184         }
185         return *_fader_touch;
186 }
187
188 Meter& 
189 Strip::meter()
190 {
191         if  (_meter == 0) {
192                 throw MackieControlException ("meter is null");
193         }
194         return *_meter;
195 }
196
197 std::ostream & Mackie::operator <<  (std::ostream & os, const Strip & strip)
198 {
199         os << typeid (strip).name();
200         os << " { ";
201         os << "has_solo: " << boolalpha << strip.has_solo();
202         os << ", ";
203         os << "has_recenable: " << boolalpha << strip.has_recenable();
204         os << ", ";
205         os << "has_mute: " << boolalpha << strip.has_mute();
206         os << ", ";
207         os << "has_select: " << boolalpha << strip.has_select();
208         os << ", ";
209         os << "has_vselect: " << boolalpha << strip.has_vselect();
210         os << ", ";
211         os << "has_fader_touch: " << boolalpha << strip.has_fader_touch();
212         os << ", ";
213         os << "has_vpot: " << boolalpha << strip.has_vpot();
214         os << ", ";
215         os << "has_gain: " << boolalpha << strip.has_gain();
216         os << " }";
217         
218         return os;
219 }
220
221 void
222 Strip::set_route (boost::shared_ptr<Route> r)
223 {
224         route_connections.drop_connections ();
225
226         _route = r;
227
228         if (r) {
229                 
230                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
231                                                                    _surface->number(), _index, _route->name()));
232                 
233
234                 if (has_solo()) {
235                         _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
236                 }
237                 if (has_mute()) {
238                         _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
239                 }
240                 
241                 if (has_gain()) {
242                         _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
243                 }
244                 
245                 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
246                 
247                 if (_route->pannable()) {
248                         _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
249                         _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
250                 }
251                 
252                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
253         
254                 if (trk) {
255                         trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
256                 }
257                 
258                 // TODO this works when a currently-banked route is made inactive, but not
259                 // when a route is activated which should be currently banked.
260                 
261                 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
262                 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
263         
264                 // TODO
265                 // SelectedChanged
266                 // RemoteControlIDChanged. Better handled at Session level.
267
268                 /* Update */
269
270                 notify_all ();
271         } else {
272                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
273                                                                    _surface->number(), _index));
274         }
275 }
276
277 void 
278 Strip::notify_all()
279 {
280         if  (has_solo()) {
281                 notify_solo_changed ();
282         }
283         
284         if  (has_mute()) {
285                 notify_mute_changed ();
286         }
287         
288         if  (has_gain()) {
289                 notify_gain_changed ();
290         }
291         
292         notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
293         
294         if  (has_vpot()) {
295                 notify_panner_changed ();
296         }
297         
298         if  (has_recenable()) {
299                 notify_record_enable_changed ();
300         }
301 }
302
303 void 
304 Strip::notify_solo_changed ()
305 {
306         if (_route && _solo) {
307                 _surface->write (_solo->led().set_state (_route->soloed() ? on : off));
308         }
309 }
310
311 void 
312 Strip::notify_mute_changed ()
313 {
314         if (_route && _mute) {
315                 _surface->write (_mute->led().set_state (_route->muted() ? on : off));
316         }
317 }
318
319 void 
320 Strip::notify_record_enable_changed ()
321 {
322         if (_route && _recenable)  {
323                 _surface->write (_recenable->led().set_state (_route->record_enabled() ? on : off));
324         }
325 }
326
327 void 
328 Strip::notify_active_changed ()
329 {
330         _surface->mcp().refresh_current_bank();
331 }
332
333 void 
334 Strip::notify_route_deleted ()
335 {
336         _surface->mcp().refresh_current_bank();
337 }
338
339 void 
340 Strip::notify_gain_changed (bool force_update)
341 {
342         if (_route) {
343                 Fader & fader = gain();
344
345                 if (!fader.in_use()) {
346                         float position = gain_to_slider_position (_route->gain_control()->get_value());
347                         // check that something has actually changed
348                         if (force_update || position != _last_gain_written) {
349                                 _surface->write (fader.set_position (position));
350                                 _last_gain_written = position;
351                         }
352                 }
353         }
354 }
355
356 void 
357 Strip::notify_property_changed (const PropertyChange& what_changed)
358 {
359         if (!what_changed.contains (ARDOUR::Properties::name)) {
360                 return;
361         }
362
363         if (_route) {
364                 string line1;
365                 string fullname = _route->name();
366                 
367                 if (fullname.length() <= 6) {
368                         line1 = fullname;
369                 } else {
370                         line1 = PBD::short_version (fullname, 6);
371                 }
372                 
373                 _surface->write (display (0, line1));
374                 _surface->write (blank_display (1));
375         }
376 }
377
378 void 
379 Strip::notify_panner_changed (bool force_update)
380 {
381         if (_route) {
382                 Pot & pot = vpot();
383                 boost::shared_ptr<Panner> panner = _route->panner();
384
385                 if (panner) {
386                         double pos = panner->position ();
387
388                         // cache the MidiByteArray here, because the mackie led control is much lower
389                         // resolution than the panner control. So we save lots of byte
390                         // sends in spite of more work on the comparison
391
392                         MidiByteArray bytes = pot.set_all (pos, true, Pot::dot);
393
394                         // check that something has actually changed
395                         if (force_update || bytes != _last_pan_written)
396                         {
397                                 _surface->write (bytes);
398                                 _last_pan_written = bytes;
399                         }
400                 } else {
401                         _surface->write (pot.zero());
402                 }
403         }
404 }
405
406 bool 
407 Strip::handle_button (SurfacePort & port, Control & control, ButtonState bs)
408 {
409         Button* button = dynamic_cast<Button*>(&control);
410         
411         if (!button) {
412                 return false;
413         }
414
415         if (!_route) {
416                 // no route so always switch the light off
417                 // because no signals will be emitted by a non-route
418                 _surface->write (button->led().set_state  (off));
419                 return false;
420         }
421
422         bool state = false;
423
424         if (bs == press) {
425                 if (control.name() == "recenable") {
426                         state = !_route->record_enabled();
427                         _route->set_record_enabled (state, this);
428                 } else if (control.name() == "mute") {
429                         state = !_route->muted();
430                         _route->set_mute (state, this);
431                 } else if (control.name() == "solo") {
432                         state = !_route->soloed();
433                         _route->set_solo (state, this);
434                 } else if (control.name() == "select") {
435                         _surface->mcp().select_track (_route);
436                 } else if (control.name() == "vselect") {
437                         // TODO could be used to select different things to apply the pot to?
438                         //state = default_button_press (dynamic_cast<Button&> (control));
439                 }
440         }
441
442         if (control.name() == "fader_touch") {
443
444                 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
445
446                 state = (bs == press);
447
448                 gain().set_in_use (state);
449                 
450                 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
451
452                         /* BCF faders don't support touch, so add a timeout to reset
453                            their `in_use' state.
454                         */
455
456                         _surface->mcp().add_in_use_timeout (*_surface, gain(), &fader_touch());
457                 }
458         }
459
460         return state;
461 }
462
463 void
464 Strip::periodic ()
465 {
466         if (!_route) {
467                 return;
468         }
469
470         update_automation ();
471         update_meter ();
472 }
473
474 void 
475 Strip::update_automation ()
476 {
477         ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
478
479         if (gain_state == Touch || gain_state == Play) {
480                 notify_gain_changed (false);
481         }
482
483         if (_route->panner()) {
484                 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
485                 if (panner_state == Touch || panner_state == Play) {
486                         notify_panner_changed (false);
487                 }
488         }
489 }
490
491 void
492 Strip::update_meter ()
493 {
494         float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
495         _surface->write (meter().update_message (dB));
496 }
497
498 MidiByteArray
499 Strip::zero ()
500 {
501         MidiByteArray retval;
502
503         for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
504                 retval << (*it)->zero ();
505         }
506
507         retval << blank_display (0);
508         retval << blank_display (1);
509         
510         return retval;
511 }
512
513 MidiByteArray
514 Strip::blank_display (uint32_t line_number)
515 {
516         return display (line_number, string());
517 }
518
519 MidiByteArray
520 Strip::display (uint32_t line_number, const std::string& line)
521 {
522         assert (line_number <= 1);
523
524         MidiByteArray retval;
525
526         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
527
528         // sysex header
529         retval << _surface->sysex_hdr();
530         
531         // code for display
532         retval << 0x12;
533         // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
534         retval << (_index * 7 + (line_number * 0x38));
535         
536         // ascii data to display
537         retval << line;
538         // pad with " " out to 6 chars
539         for (int i = line.length(); i < 6; ++i) {
540                 retval << ' ';
541         }
542         
543         // column spacer, unless it's the right-hand column
544         if (_index < 7) {
545                 retval << ' ';
546         }
547
548         // sysex trailer
549         retval << MIDI::eox;
550         
551         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));
552
553         return retval;
554 }