2 Copyright (C) 2006,2007 John Anderson
3 Copyright (C) 2012 Paul Davis
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.
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.
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.
24 #include "midi++/port.h"
26 #include "pbd/compose.h"
27 #include "pbd/convert.h"
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"
39 #include "mackie_control_protocol.h"
40 #include "surface_port.h"
49 using namespace Mackie;
51 using namespace ARDOUR;
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__))
57 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
58 #define invalidator() __invalidator (*(MackieControlProtocol::instance()), __FILE__, __LINE__)
60 Strip::Strip (Surface& s, const std::string& name, int index, StripControlDefinition* ctls)
73 /* build the controls for this track, which will automatically add them
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);
88 TODO could optimise this to use enum, but it's only
89 called during the protocol class instantiation.
91 void Strip::add (Control & control)
100 if ((fader = dynamic_cast<Fader*>(&control)) != 0) {
104 } else if ((pot = dynamic_cast<Pot*>(&control)) != 0) {
108 } else if ((button = dynamic_cast<Button*>(&control)) != 0) {
110 if (control.id() >= Button::recenable_base_id &&
111 control.id() < Button::recenable_base_id + 8) {
115 } else if (control.id() >= Button::mute_base_id &&
116 control.id() < Button::mute_base_id + 8) {
120 } else if (control.id() >= Button::solo_base_id &&
121 control.id() < Button::solo_base_id + 8) {
125 } else if (control.id() >= Button::select_base_id &&
126 control.id() < Button::select_base_id + 8) {
130 } else if (control.id() >= Button::vselect_base_id &&
131 control.id() < Button::vselect_base_id + 8) {
135 } else if (control.id() >= Button::fader_touch_base_id &&
136 control.id() < Button::fader_touch_base_id + 8) {
138 _fader_touch = button;
141 } else if ((meter = dynamic_cast<Meter*>(&control)) != 0) {
150 throw MackieControlException ("gain is null");
159 throw MackieControlException ("vpot is null");
167 if (_recenable == 0) {
168 throw MackieControlException ("recenable is null");
177 throw MackieControlException ("solo is null");
185 throw MackieControlException ("mute is null");
194 throw MackieControlException ("select is null");
203 throw MackieControlException ("vselect is null");
211 if (_fader_touch == 0) {
212 throw MackieControlException ("fader_touch is null");
214 return *_fader_touch;
221 throw MackieControlException ("meter is null");
226 std::ostream & Mackie::operator << (std::ostream & os, const Strip & strip)
228 os << typeid (strip).name();
230 os << "has_solo: " << boolalpha << strip.has_solo();
232 os << "has_recenable: " << boolalpha << strip.has_recenable();
234 os << "has_mute: " << boolalpha << strip.has_mute();
236 os << "has_select: " << boolalpha << strip.has_select();
238 os << "has_vselect: " << boolalpha << strip.has_vselect();
240 os << "has_fader_touch: " << boolalpha << strip.has_fader_touch();
242 os << "has_vpot: " << boolalpha << strip.has_vpot();
244 os << "has_gain: " << boolalpha << strip.has_gain();
251 Strip::set_route (boost::shared_ptr<Route> r)
253 route_connections.drop_connections ();
259 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
260 _surface->number(), _index, _route->name()));
264 _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
267 _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
271 _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
274 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
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());
281 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
284 trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
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.
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());
295 // RemoteControlIDChanged. Better handled at Session level.
301 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
302 _surface->number(), _index));
310 notify_solo_changed ();
314 notify_mute_changed ();
318 notify_gain_changed ();
321 notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
324 notify_panner_changed ();
327 if (has_recenable()) {
328 notify_record_enable_changed ();
333 Strip::notify_solo_changed ()
335 if (_route && _solo) {
336 _surface->write (_solo->led().set_state (_route->soloed() ? on : off));
341 Strip::notify_mute_changed ()
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 _surface->write (_mute->led().set_state (_route->muted() ? on : off));
351 Strip::notify_record_enable_changed ()
353 if (_route && _recenable) {
354 _surface->write (_recenable->led().set_state (_route->record_enabled() ? on : off));
359 Strip::notify_active_changed ()
361 _surface->mcp().refresh_current_bank();
365 Strip::notify_route_deleted ()
367 _surface->mcp().refresh_current_bank();
371 Strip::notify_gain_changed (bool force_update)
374 Fader & fader = gain();
376 if (!fader.in_use()) {
377 float position = gain_to_slider_position (_route->gain_control()->get_value());
378 // check that something has actually changed
379 if (force_update || position != _last_gain_written) {
380 _surface->write (fader.set_position (position));
381 _last_gain_written = position;
388 Strip::notify_property_changed (const PropertyChange& what_changed)
390 if (!what_changed.contains (ARDOUR::Properties::name)) {
396 string fullname = _route->name();
398 if (fullname.length() <= 6) {
401 line1 = PBD::short_version (fullname, 6);
404 _surface->write (display (0, line1));
405 _surface->write (blank_display (1));
410 Strip::notify_panner_changed (bool force_update)
414 boost::shared_ptr<Panner> panner = _route->panner();
417 double pos = panner->position ();
419 // cache the MidiByteArray here, because the mackie led control is much lower
420 // resolution than the panner control. So we save lots of byte
421 // sends in spite of more work on the comparison
423 MidiByteArray bytes = pot.set_all (pos, true, Pot::dot);
425 // check that something has actually changed
426 if (force_update || bytes != _last_pan_written)
428 _surface->write (bytes);
429 _last_pan_written = bytes;
432 _surface->write (pot.zero());
438 Strip::handle_button (Button& button, ButtonState bs)
440 button.set_in_use (bs == press);
443 // no route so always switch the light off
444 // because no signals will be emitted by a non-route
445 _surface->write (button.led().set_state (off));
450 if (button.id() >= Button::recenable_base_id &&
451 button.id() < Button::recenable_base_id + 8) {
453 _route->set_record_enabled (!_route->record_enabled(), this);
455 } else if (button.id() >= Button::mute_base_id &&
456 button.id() < Button::mute_base_id + 8) {
458 _route->set_mute (!_route->muted(), this);
460 } else if (button.id() >= Button::solo_base_id &&
461 button.id() < Button::solo_base_id + 8) {
463 _route->set_solo (!_route->soloed(), this);
465 } else if (button.id() >= Button::select_base_id &&
466 button.id() < Button::select_base_id + 8) {
468 _surface->mcp().select_track (_route);
470 } else if (button.id() >= Button::vselect_base_id &&
471 button.id() < Button::vselect_base_id + 8) {
476 if (button.id() >= Button::fader_touch_base_id &&
477 button.id() < Button::fader_touch_base_id + 8) {
479 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
481 bool state = (bs == press);
483 _gain->set_in_use (state);
485 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
487 /* BCF faders don't support touch, so add a timeout to reset
488 their `in_use' state.
491 _surface->mcp().add_in_use_timeout (*_surface, gain(), &fader_touch());
497 Strip::handle_fader (Fader& fader, float position)
499 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", position));
502 _route->gain_control()->set_value (slider_position_to_gain (position));
505 if (ARDOUR::Config->get_mackie_emulation() == "bcf") {
506 /* reset the timeout while we're still moving the fader */
507 _surface->mcp().add_in_use_timeout (*_surface, fader, fader.in_use_touch_control);
510 // must echo bytes back to slider now, because
511 // the notifier only works if the fader is not being
512 // touched. Which it is if we're getting input.
514 _surface->write (fader.set_position (position));
518 Strip::handle_pot (Pot& pot, ControlState& state)
521 _surface->write (pot.set_onoff (false));
525 boost::shared_ptr<Pannable> pannable = _route->pannable();
528 boost::shared_ptr<AutomationControl> ac;
530 if (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL) {
531 ac = pannable->pan_width_control;
533 ac = pannable->pan_azimuth_control;
536 double p = ac->get_value();
538 // calculate new value, and adjust
539 p += state.delta * state.sign;
554 update_automation ();
559 Strip::update_automation ()
561 ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
563 if (gain_state == Touch || gain_state == Play) {
564 notify_gain_changed (false);
567 if (_route->panner()) {
568 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
569 if (panner_state == Touch || panner_state == Play) {
570 notify_panner_changed (false);
576 Strip::update_meter ()
578 float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
579 _surface->write (meter().update_message (dB));
585 MidiByteArray retval;
587 for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
588 retval << (*it)->zero ();
591 retval << blank_display (0);
592 retval << blank_display (1);
598 Strip::blank_display (uint32_t line_number)
600 return display (line_number, string());
604 Strip::display (uint32_t line_number, const std::string& line)
606 assert (line_number <= 1);
608 MidiByteArray retval;
610 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
613 retval << _surface->sysex_hdr();
617 // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
618 retval << (_index * 7 + (line_number * 0x38));
620 // ascii data to display
622 // pad with " " out to 6 chars
623 for (int i = line.length(); i < 6; ++i) {
627 // column spacer, unless it's the right-hand column
635 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));