2 Copyright (C) 2006,2007 John Anderson
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <boost/shared_array.hpp>
35 #include "midi++/types.h"
36 #include "midi++/port.h"
37 #include "midi++/manager.h"
38 #include "pbd/pthread_utils.h"
39 #include "pbd/error.h"
40 #include "pbd/memento_command.h"
41 #include "pbd/convert.h"
43 #include "ardour/dB.h"
44 #include "ardour/debug.h"
45 #include "ardour/location.h"
46 #include "ardour/midi_ui.h"
47 #include "ardour/panner.h"
48 #include "ardour/panner_shell.h"
49 #include "ardour/route.h"
50 #include "ardour/session.h"
51 #include "ardour/tempo.h"
52 #include "ardour/types.h"
53 #include "ardour/audioengine.h"
55 #include "mackie_control_protocol.h"
57 #include "midi_byte_array.h"
58 #include "mackie_control_exception.h"
59 #include "route_signal.h"
60 #include "mackie_midi_builder.h"
61 #include "surface_port.h"
63 #include "bcf_surface.h"
64 #include "mackie_surface.h"
66 using namespace ARDOUR;
68 using namespace Mackie;
73 #include "pbd/abstract_ui.cc" // instantiate template
75 #define NUCLEUS_DEBUG 1
77 MackieMidiBuilder builder;
79 #define midi_ui_context() MidiControlUI::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
80 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
82 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
83 #define invalidator(x) __invalidator ((x), __FILE__, __LINE__)
85 MackieControlProtocol::MackieControlProtocol (Session& session)
86 : ControlProtocol (session, X_("Mackie"), MidiControlUI::instance())
87 , AbstractUI<MackieControlUIRequest> ("mackie")
88 , _current_initial_bank (0)
91 , _timecode_type (ARDOUR::AnyTime::BBT)
92 , _input_bundle (new ARDOUR::Bundle (_("Mackie Control In"), true))
93 , _output_bundle (new ARDOUR::Bundle (_("Mackie Control Out"), false))
96 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
98 AudioEngine::instance()->PortConnectedOrDisconnected.connect (
99 audio_engine_connections, invalidator (*this), ui_bind (&MackieControlProtocol::port_connected_or_disconnected, this, _2, _4, _5),
104 MackieControlProtocol::~MackieControlProtocol()
106 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol\n");
111 catch (exception & e) {
112 cout << "~MackieControlProtocol caught " << e.what() << endl;
115 cout << "~MackieControlProtocol caught unknown" << endl;
118 DEBUG_TRACE (DEBUG::MackieControl, "finished ~MackieControlProtocol::MackieControlProtocol\n");
122 MackieControlProtocol::surface()
125 throw MackieControlException ("_surface is 0 in MackieControlProtocol::surface");
130 const Mackie::SurfacePort&
131 MackieControlProtocol::mcu_port() const
133 if (_ports.size() < 1) {
136 return dynamic_cast<const MackiePort &> (*_ports[0]);
141 MackieControlProtocol::mcu_port()
143 if (_ports.size() < 1) {
146 return dynamic_cast<MackiePort &> (*_ports[0]);
150 // go to the previous track.
151 // Assume that get_sorted_routes().size() > route_table.size()
153 MackieControlProtocol::prev_track()
155 if (_current_initial_bank >= 1) {
156 session->set_dirty();
157 switch_banks (_current_initial_bank - 1);
161 // go to the next track.
162 // Assume that get_sorted_routes().size() > route_table.size()
164 MackieControlProtocol::next_track()
166 Sorted sorted = get_sorted_routes();
167 if (_current_initial_bank + route_table.size() < sorted.size()) {
168 session->set_dirty();
169 switch_banks (_current_initial_bank + 1);
174 MackieControlProtocol::clear_route_signals()
176 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
179 route_signals.clear();
182 // return the port for a given id - 0 based
183 // throws an exception if no port found
185 MackieControlProtocol::port_for_id (uint32_t index)
187 uint32_t current_max = 0;
188 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
189 current_max += (*it)->strips();
190 if (index < current_max) return **it;
193 // oops - no matching port
195 os << "No port for index " << index;
196 throw MackieControlException (os.str());
199 // predicate for sort call in get_sorted_routes
200 struct RouteByRemoteId
202 bool operator () (const boost::shared_ptr<Route> & a, const boost::shared_ptr<Route> & b) const
204 return a->remote_control_id() < b->remote_control_id();
207 bool operator () (const Route & a, const Route & b) const
209 return a.remote_control_id() < b.remote_control_id();
212 bool operator () (const Route * a, const Route * b) const
214 return a->remote_control_id() < b->remote_control_id();
218 MackieControlProtocol::Sorted
219 MackieControlProtocol::get_sorted_routes()
224 boost::shared_ptr<RouteList> routes = session->get_routes();
225 set<uint32_t> remote_ids;
227 // routes with remote_id 0 should never be added
228 // TODO verify this with ardour devs
229 // remote_ids.insert (0);
231 // sort in remote_id order, and exclude master, control and hidden routes
232 // and any routes that are already set.
233 for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it) {
234 Route & route = **it;
237 && !route.is_master()
238 && !route.is_hidden()
239 && !route.is_monitor()
240 && remote_ids.find (route.remote_control_id()) == remote_ids.end()
242 sorted.push_back (*it);
243 remote_ids.insert (route.remote_control_id());
246 sort (sorted.begin(), sorted.end(), RouteByRemoteId());
251 MackieControlProtocol::refresh_current_bank()
253 switch_banks (_current_initial_bank);
257 MackieControlProtocol::switch_banks (int initial)
259 // DON'T prevent bank switch if initial == _current_initial_bank
260 // because then this method can't be used as a refresh
263 Sorted sorted = get_sorted_routes();
264 int delta = sorted.size() - route_table.size();
265 if (initial < 0 || (delta > 0 && initial > delta)) {
266 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("not switching to %1\n", initial));
269 _current_initial_bank = initial;
271 // first clear the signals from old routes
272 // taken care of by the RouteSignal destructors
273 clear_route_signals();
275 // now set the signals for new routes
276 if (_current_initial_bank <= sorted.size()) {
277 // fetch the bank start and end to switch to
278 uint32_t end_pos = min (route_table.size(), sorted.size());
279 Sorted::iterator it = sorted.begin() + _current_initial_bank;
280 Sorted::iterator end = sorted.begin() + _current_initial_bank + end_pos;
282 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch to %1, %2\n", _current_initial_bank, end_pos));
284 // clear out routes from our table in case any have been deleted
285 for (vector<boost::shared_ptr<Route> >::iterator i = route_table.begin(); i != route_table.end(); ++i) {
289 // link routes to strips
291 for (; it != end && it != sorted.end(); ++it, ++i) {
292 boost::shared_ptr<Route> route = *it;
294 assert (surface().strips[i]);
295 Strip & strip = *surface().strips[i];
297 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("remote id %1 connecting %2 to %3 with port %4\n",
298 route->remote_control_id(), route->name(), strip.name(), port_for_id(i)));
299 route_table[i] = route;
300 RouteSignal * rs = new RouteSignal (route, *this, strip, port_for_id(i));
301 route_signals.push_back (rs);
302 // update strip from route
306 // create dead strips if there aren't enough routes to
308 for (; i < route_table.size(); ++i) {
309 Strip & strip = *surface().strips[i];
310 // send zero for this strip
311 MackiePort & port = port_for_id(i);
312 port.write (builder.zero_strip (port, strip));
316 // display the current start bank.
317 surface().display_bank_start (mcu_port(), builder, _current_initial_bank);
321 MackieControlProtocol::zero_all()
323 // TODO turn off Timecode displays
326 for (Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it) {
327 MackiePort & port = port_for_id ((*it)->index());
328 port.write (builder.zero_strip (port, **it));
331 // and the master strip
332 mcu_port().write (builder.zero_strip (dynamic_cast<MackiePort&> (mcu_port()), master_strip()));
334 // turn off global buttons and leds
335 // global buttons are only ever on mcu_port, so we don't have
336 // to figure out which port.
337 for (Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it) {
338 Control & control = **it;
339 if (!control.group().is_strip() && control.accepts_feedback()) {
340 mcu_port().write (builder.zero_control (control));
344 // any hardware-specific stuff
345 surface().zero_all (mcu_port(), builder);
349 MackieControlProtocol::set_active (bool yn)
357 // the reason for the locking and unlocking is that
358 // glibmm can't do a condition wait on a RecMutex
360 // TODO what happens if this fails half way?
362 // create MackiePorts
364 Glib::Mutex::Lock lock (update_mutex);
368 // now initialise MackiePorts - ie exchange sysex messages
369 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
373 // wait until all ports are active
374 // TODO a more sophisticated approach would
375 // allow things to start up with only an MCU, even if
376 // extenders were specified but not responding.
377 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
378 (*it)->wait_for_init();
381 // create surface object. This depends on the ports being
382 // correctly initialised
383 initialize_surface();
384 connect_session_signals();
389 // send current control positions to surface
390 // must come after _active = true otherwise it won't run
398 catch (exception & e) {
399 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("set_active to false because exception caught: %1\n", e.what()));
408 MackieControlProtocol::handle_strip_button (SurfacePort & port, Control & control, ButtonState bs, boost::shared_ptr<Route> route)
413 if (control.name() == "recenable") {
414 state = !route->record_enabled();
415 route->set_record_enabled (state, this);
416 } else if (control.name() == "mute") {
417 state = !route->muted();
418 route->set_mute (state, this);
419 } else if (control.name() == "solo") {
420 state = !route->soloed();
421 route->set_solo (state, this);
422 } else if (control.name() == "select") {
423 // TODO make the track selected. Whatever that means.
424 //state = default_button_press (dynamic_cast<Button&> (control));
425 } else if (control.name() == "vselect") {
426 // TODO could be used to select different things to apply the pot to?
427 //state = default_button_press (dynamic_cast<Button&> (control));
431 if (control.name() == "fader_touch") {
433 control.strip().gain().set_in_use (state);
435 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
436 /* BCF faders don't support touch, so add a timeout to reset
437 their `in_use' state.
439 port.add_in_use_timeout (control.strip().gain(), &control.strip().fader_touch());
447 MackieControlProtocol::update_led (Mackie::Button & button, Mackie::LedState ls)
450 SurfacePort * port = 0;
451 if (button.group().is_strip()) {
452 if (button.group().is_master()) {
455 port = &port_for_id (dynamic_cast<const Strip&> (button.group()).index());
460 port->write (builder.build_led (button, ls));
465 MackieControlProtocol::update_timecode_beats_led()
467 switch (_timecode_type) {
468 case ARDOUR::AnyTime::BBT:
469 update_global_led ("beats", on);
470 update_global_led ("timecode", off);
472 case ARDOUR::AnyTime::Timecode:
473 update_global_led ("timecode", on);
474 update_global_led ("beats", off);
478 os << "Unknown Anytime::Type " << _timecode_type;
479 throw runtime_error (os.str());
484 MackieControlProtocol::update_global_button (const string & name, LedState ls)
486 if (surface().controls_by_name.find (name) != surface().controls_by_name.end()) {
487 Button * button = dynamic_cast<Button*> (surface().controls_by_name[name]);
488 mcu_port().write (builder.build_led (button->led(), ls));
490 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Button %1 not found\n", name));
495 MackieControlProtocol::update_global_led (const string & name, LedState ls)
497 if (surface().controls_by_name.find (name) != surface().controls_by_name.end()) {
498 Led * led = dynamic_cast<Led*> (surface().controls_by_name[name]);
499 mcu_port().write (builder.build_led (*led, ls));
501 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Led %1 not found\n", name));
505 // send messages to surface to set controls to correct values
507 MackieControlProtocol::update_surface()
513 // do the initial bank switch to connect signals
514 // _current_initial_bank is initialised by set_state
515 switch_banks (_current_initial_bank);
517 /* Create a RouteSignal for the master route, if we don't already have one */
518 if (!master_route_signal) {
519 boost::shared_ptr<Route> mr = master_route ();
521 master_route_signal = boost::shared_ptr<RouteSignal> (new RouteSignal (mr, *this, master_strip(), mcu_port()));
522 // update strip from route
523 master_route_signal->notify_all();
527 // sometimes the jog wheel is a pot
528 surface().blank_jog_ring (mcu_port(), builder);
530 // update global buttons and displays
531 notify_record_state_changed();
532 notify_transport_state_changed();
533 update_timecode_beats_led();
537 MackieControlProtocol::connect_session_signals()
539 // receive routes added
540 session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_route_added, this, _1), midi_ui_context());
541 // receive record state toggled
542 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_record_state_changed, this), midi_ui_context());
543 // receive transport state changed
544 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_transport_state_changed, this), midi_ui_context());
545 // receive punch-in and punch-out
546 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
547 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
548 // receive rude solo changed
549 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_solo_active_changed, this, _1), midi_ui_context());
551 // make sure remote id changed signals reach here
552 // see also notify_route_added
553 Sorted sorted = get_sorted_routes();
555 for (Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it) {
556 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind(&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
561 MackieControlProtocol::add_port (MIDI::Port & midi_input_port, MIDI::Port & midi_output_port, int number)
563 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("add port %1 %2\n", midi_input_port.name(), midi_output_port.name()));
565 MackiePort * sport = new MackiePort (*this, midi_input_port, midi_output_port, number);
566 _ports.push_back (sport);
568 sport->init_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_init, this, sport));
569 sport->active_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_active, this, sport));
570 sport->inactive_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_inactive, this, sport));
572 _input_bundle->add_channel (
573 midi_input_port.name(),
574 ARDOUR::DataType::MIDI,
575 session->engine().make_port_name_non_relative (midi_input_port.name())
578 _output_bundle->add_channel (
579 midi_output_port.name(),
580 ARDOUR::DataType::MIDI,
581 session->engine().make_port_name_non_relative (midi_output_port.name())
586 MackieControlProtocol::create_ports()
588 MIDI::Manager * mm = MIDI::Manager::instance();
589 MIDI::Port * midi_input_port = mm->add_port (
590 new MIDI::Port (string_compose (_("%1 in"), default_port_name), MIDI::Port::IsInput, session->engine().jack())
592 MIDI::Port * midi_output_port = mm->add_port (
593 new MIDI::Port (string_compose (_("%1 out"), default_port_name), MIDI::Port::IsOutput, session->engine().jack())
596 /* Create main port */
598 if (!midi_input_port->ok() || !midi_output_port->ok()) {
600 os << _("Mackie control MIDI ports could not be created; Mackie control disabled");
601 error << os.str() << endmsg;
602 throw MackieControlException (os.str());
605 add_port (*midi_input_port, *midi_output_port, 0);
607 /* Create extender ports */
609 for (uint32_t index = 1; index <= Config->get_mackie_extenders(); ++index) {
610 MIDI::Port * midi_input_port = mm->add_port (
611 new MIDI::Port (string_compose (_("mcu_xt_%1 in"), index), MIDI::Port::IsInput, session->engine().jack())
613 MIDI::Port * midi_output_port = mm->add_port (
614 new MIDI::Port (string_compose (_("mcu_xt_%1 out"), index), MIDI::Port::IsOutput, session->engine().jack())
616 if (midi_input_port->ok() && midi_output_port->ok()) {
617 add_port (*midi_input_port, *midi_output_port, index);
622 boost::shared_ptr<Route>
623 MackieControlProtocol::master_route()
625 return session->master_out ();
629 MackieControlProtocol::master_strip()
631 return dynamic_cast<Strip&> (*surface().groups["master"]);
635 MackieControlProtocol::initialize_surface()
637 // set up the route table
639 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
640 strips += (*it)->strips();
643 set_route_table_size (strips);
645 // TODO same as code in mackie_port.cc
646 string emulation = ARDOUR::Config->get_mackie_emulation();
647 if (emulation == "bcf") {
648 _surface = new BcfSurface (strips);
649 } else if (emulation == "mcu") {
650 _surface = new MackieSurface (strips);
653 os << "no Surface class found for emulation: " << emulation;
654 throw MackieControlException (os.str());
659 // Connect events. Must be after route table otherwise there will be trouble
661 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
662 (*it)->control_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_control_event, this, _1, _2, _3));
667 MackieControlProtocol::close()
670 // must be before other shutdown otherwise polling loop
671 // calls methods on objects that are deleted
673 port_connections.drop_connections ();
674 session_connections.drop_connections ();
675 route_connections.drop_connections ();
678 // These will fail if the port has gone away.
679 // So catch the exception and do the rest of the
681 // because the bcf doesn't respond to the next 3 sysex messages
686 catch (exception & e) {
687 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
690 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
692 MackiePort & port = **it;
694 port.write_sysex (0x61);
696 port.write_sysex (0x62);
697 // Reset (reboot into offline mode)
698 port.write_sysex (0x63);
700 catch (exception & e) {
701 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
705 // disconnect routes from strips
706 clear_route_signals();
711 // shut down MackiePorts
712 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
720 MackieControlProtocol::get_state()
722 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state\n");
724 // add name of protocol
725 XMLNode* node = new XMLNode (X_("Protocol"));
726 node->add_property (X_("name"), ARDOUR::ControlProtocol::_name);
730 os << _current_initial_bank;
731 node->add_property (X_("bank"), os.str());
737 MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
739 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::set_state: active %1\n", _active));
743 // fetch current bank
745 if (node.property (X_("bank")) != 0) {
746 string bank = node.property (X_("bank"))->value();
749 uint32_t new_bank = atoi (bank.c_str());
750 if (_current_initial_bank != new_bank) {
751 switch_banks (new_bank);
754 catch (exception & e) {
755 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("exception in MackieControlProtocol::set_state: %1\n", e.what()));
764 MackieControlProtocol::handle_control_event (SurfacePort & port, Control & control, const ControlState & state)
766 // find the route for the control, if there is one
767 boost::shared_ptr<Route> route;
768 if (control.group().is_strip()) {
769 if (control.group().is_master()) {
770 route = master_route();
772 uint32_t index = control.ordinal() - 1 + (port.number() * port.strips());
773 if (index < route_table.size())
774 route = route_table[index];
776 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
780 // This handles control element events from the surface
781 // the state of the controls on the surface is usually updated
783 switch (control.type()) {
784 case Control::type_fader:
785 // find the route in the route table for the id
786 // if the route isn't available, skip it
787 // at which point the fader should just reset itself
790 route->gain_control()->set_value (slider_position_to_gain (state.pos));
792 if (ARDOUR::Config->get_mackie_emulation() == "bcf") {
793 /* reset the timeout while we're still moving the fader */
794 port.add_in_use_timeout (control, control.in_use_touch_control);
797 // must echo bytes back to slider now, because
798 // the notifier only works if the fader is not being
799 // touched. Which it is if we're getting input.
800 port.write (builder.build_fader ((Fader&)control, state.pos));
804 case Control::type_button:
805 if (control.group().is_strip()) {
808 handle_strip_button (port, control, state.button_state, route);
810 // no route so always switch the light off
811 // because no signals will be emitted by a non-route
812 port.write (builder.build_led (control.led(), off));
814 } else if (control.group().is_master()) {
815 // master fader touch
817 handle_strip_button (port, control, state.button_state, route);
820 // handle all non-strip buttons
821 surface().handle_button (*this, state.button_state, dynamic_cast<Button&> (control));
825 // pot (jog wheel, external control)
826 case Control::type_pot:
827 if (control.group().is_strip()) {
829 boost::shared_ptr<Panner> panner = route->panner_shell()->panner();
830 // pan for mono input routes, or stereo linked panners
832 double p = panner->position ();
834 // calculate new value, and adjust
835 p += state.delta * state.sign;
838 panner->set_position (p);
841 // it's a pot for an umnapped route, so turn all the lights off
842 port.write (builder.build_led_ring (dynamic_cast<Pot &> (control), off));
847 if (control.is_jog()) {
848 _jog_wheel.jog_event (port, control, state);
850 cout << "external controller" << state.ticks * state.sign << endl;
856 cout << "Control::type not handled: " << control.type() << endl;
860 /////////////////////////////////////////////////
861 // handlers for Route signals
862 // TODO should these be part of RouteSignal?
863 // They started off as signal/slot handlers for signals
864 // from Route, but they're also used in polling for automation
865 /////////////////////////////////////////////////
868 MackieControlProtocol::notify_solo_changed (RouteSignal * route_signal)
871 Button & button = route_signal->strip().solo();
872 route_signal->port().write (builder.build_led (button, route_signal->route()->soloed()));
874 catch (exception & e) {
875 cout << e.what() << endl;
880 MackieControlProtocol::notify_mute_changed (RouteSignal * route_signal)
883 Button & button = route_signal->strip().mute();
884 route_signal->port().write (builder.build_led (button, route_signal->route()->muted()));
886 catch (exception & e) {
887 cout << e.what() << endl;
892 MackieControlProtocol::notify_record_enable_changed (RouteSignal * route_signal)
895 Button & button = route_signal->strip().recenable();
896 route_signal->port().write (builder.build_led (button, route_signal->route()->record_enabled()));
898 catch (exception & e) {
899 cout << e.what() << endl;
903 void MackieControlProtocol::notify_active_changed (RouteSignal *)
906 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::notify_active_changed\n");
907 refresh_current_bank();
909 catch (exception & e) {
910 cout << e.what() << endl;
915 MackieControlProtocol::notify_gain_changed (RouteSignal * route_signal, bool force_update)
918 Fader & fader = route_signal->strip().gain();
919 if (!fader.in_use()) {
920 float gain_value = gain_to_slider_position (route_signal->route()->gain_control()->get_value());
921 // check that something has actually changed
922 if (force_update || gain_value != route_signal->last_gain_written()) {
923 route_signal->port().write (builder.build_fader (fader, gain_value));
924 route_signal->last_gain_written (gain_value);
928 catch (exception & e) {
929 cout << e.what() << endl;
934 MackieControlProtocol::notify_property_changed (const PropertyChange& what_changed, RouteSignal * route_signal)
936 if (!what_changed.contains (Properties::name)) {
941 Strip & strip = route_signal->strip();
943 /* XXX: not sure about this check to only display stuff for strips of index < 8 */
944 if (!strip.is_master() && strip.index() < 8) {
946 string fullname = route_signal->route()->name();
948 if (fullname.length() <= 6) {
951 line1 = PBD::short_version (fullname, 6);
955 cerr << "show strip name from " << fullname << " as " << line1 << endl;
958 SurfacePort & port = route_signal->port();
959 port.write (builder.strip_display (port, strip, 0, line1));
960 port.write (builder.strip_display_blank (port, strip, 1));
963 catch (exception & e) {
964 cout << e.what() << endl;
969 MackieControlProtocol::notify_panner_changed (RouteSignal * route_signal, bool force_update)
972 Pot & pot = route_signal->strip().vpot();
973 boost::shared_ptr<Panner> panner = route_signal->route()->panner();
975 double pos = panner->position ();
977 // cache the MidiByteArray here, because the mackie led control is much lower
978 // resolution than the panner control. So we save lots of byte
979 // sends in spite of more work on the comparison
980 MidiByteArray bytes = builder.build_led_ring (pot, ControlState (on, pos), MackieMidiBuilder::midi_pot_mode_dot);
981 // check that something has actually changed
982 if (force_update || bytes != route_signal->last_pan_written())
984 route_signal->port().write (bytes);
985 route_signal->last_pan_written (bytes);
988 route_signal->port().write (builder.zero_control (pot));
991 catch (exception & e) {
992 cout << e.what() << endl;
996 // TODO handle plugin automation polling
998 MackieControlProtocol::update_automation (RouteSignal & rs)
1000 ARDOUR::AutoState gain_state = rs.route()->gain_control()->automation_state();
1002 if (gain_state == Touch || gain_state == Play) {
1003 notify_gain_changed (&rs, false);
1006 if (rs.route()->panner()) {
1007 ARDOUR::AutoState panner_state = rs.route()->panner()->automation_state();
1008 if (panner_state == Touch || panner_state == Play) {
1009 notify_panner_changed (&rs, false);
1015 MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
1017 Timecode::BBT_Time bbt_time;
1018 session->bbt_time (now_frame, bbt_time);
1020 // According to the Logic docs
1021 // digits: 888/88/88/888
1022 // BBT mode: Bars/Beats/Subdivisions/Ticks
1024 os << setw(3) << setfill('0') << bbt_time.bars;
1025 os << setw(2) << setfill('0') << bbt_time.beats;
1027 // figure out subdivisions per beat
1028 const Meter & meter = session->tempo_map().meter_at (now_frame);
1030 if (meter.note_divisor() == 8 && (meter.divisions_per_bar() == 12.0 || meter.divisions_per_bar() == 9.0 || meter.divisions_per_bar() == 6.0)) {
1034 uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
1035 uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
1037 os << setw(2) << setfill('0') << subdivisions + 1;
1038 os << setw(3) << setfill('0') << ticks;
1044 MackieControlProtocol::format_timecode_timecode (framepos_t now_frame)
1046 Timecode::Time timecode;
1047 session->timecode_time (now_frame, timecode);
1049 // According to the Logic docs
1050 // digits: 888/88/88/888
1051 // Timecode mode: Hours/Minutes/Seconds/Frames
1053 os << setw(3) << setfill('0') << timecode.hours;
1054 os << setw(2) << setfill('0') << timecode.minutes;
1055 os << setw(2) << setfill('0') << timecode.seconds;
1056 os << setw(3) << setfill('0') << timecode.frames;
1062 MackieControlProtocol::update_timecode_display()
1064 if (surface().has_timecode_display()) {
1065 // do assignment here so current_frame is fixed
1066 framepos_t current_frame = session->transport_frame();
1069 switch (_timecode_type) {
1070 case ARDOUR::AnyTime::BBT:
1071 timecode = format_bbt_timecode (current_frame);
1073 case ARDOUR::AnyTime::Timecode:
1074 timecode = format_timecode_timecode (current_frame);
1078 os << "Unknown timecode: " << _timecode_type;
1079 throw runtime_error (os.str());
1082 // only write the timecode string to the MCU if it's changed
1083 // since last time. This is to reduce midi bandwidth used.
1084 if (timecode != _timecode_last) {
1085 surface().display_timecode (mcu_port(), builder, timecode, _timecode_last);
1086 _timecode_last = timecode;
1092 MackieControlProtocol::poll_session_data()
1094 // XXX need to attach this to a timer in the MIDI UI event loop (20msec)
1097 // do all currently mapped routes
1098 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
1099 update_automation (**it);
1102 // and the master strip
1103 if (master_route_signal != 0) {
1104 update_automation (*master_route_signal);
1107 update_timecode_display();
1111 /////////////////////////////////////
1112 // Transport Buttons
1113 /////////////////////////////////////
1116 MackieControlProtocol::frm_left_press (Button &)
1118 // can use first_mark_before/after as well
1119 unsigned long elapsed = _frm_left_last.restart();
1121 Location * loc = session->locations()->first_location_before (
1122 session->transport_frame()
1125 // allow a quick double to go past a previous mark
1126 if (session->transport_rolling() && elapsed < 500 && loc != 0) {
1127 Location * loc_two_back = session->locations()->first_location_before (loc->start());
1128 if (loc_two_back != 0)
1134 // move to the location, if it's valid
1136 session->request_locate (loc->start(), session->transport_rolling());
1143 MackieControlProtocol::frm_left_release (Button &)
1149 MackieControlProtocol::frm_right_press (Button &)
1151 // can use first_mark_before/after as well
1152 Location * loc = session->locations()->first_location_after (session->transport_frame());
1155 session->request_locate (loc->start(), session->transport_rolling());
1162 MackieControlProtocol::frm_right_release (Button &)
1168 MackieControlProtocol::stop_press (Button &)
1170 session->request_stop();
1175 MackieControlProtocol::stop_release (Button &)
1177 return session->transport_stopped();
1181 MackieControlProtocol::play_press (Button &)
1183 session->request_transport_speed (1.0);
1188 MackieControlProtocol::play_release (Button &)
1190 return session->transport_rolling();
1194 MackieControlProtocol::record_press (Button &)
1196 if (session->get_record_enabled()) {
1197 session->disable_record (false);
1199 session->maybe_enable_record();
1205 MackieControlProtocol::record_release (Button &)
1207 if (session->get_record_enabled()) {
1208 if (session->transport_rolling()) {
1219 MackieControlProtocol::rewind_press (Button &)
1221 _jog_wheel.push (JogWheel::speed);
1222 _jog_wheel.transport_direction (-1);
1223 session->request_transport_speed (-_jog_wheel.transport_speed());
1228 MackieControlProtocol::rewind_release (Button &)
1231 _jog_wheel.transport_direction (0);
1232 if (_transport_previously_rolling) {
1233 session->request_transport_speed (1.0);
1235 session->request_stop();
1241 MackieControlProtocol::ffwd_press (Button &)
1243 _jog_wheel.push (JogWheel::speed);
1244 _jog_wheel.transport_direction (1);
1245 session->request_transport_speed (_jog_wheel.transport_speed());
1250 MackieControlProtocol::ffwd_release (Button &)
1253 _jog_wheel.transport_direction (0);
1254 if (_transport_previously_rolling) {
1255 session->request_transport_speed (1.0);
1257 session->request_stop();
1263 MackieControlProtocol::loop_press (Button &)
1265 session->request_play_loop (!session->get_play_loop());
1270 MackieControlProtocol::loop_release (Button &)
1272 return session->get_play_loop();
1276 MackieControlProtocol::punch_in_press (Button &)
1278 bool const state = !session->config.get_punch_in();
1279 session->config.set_punch_in (state);
1284 MackieControlProtocol::punch_in_release (Button &)
1286 return session->config.get_punch_in();
1290 MackieControlProtocol::punch_out_press (Button &)
1292 bool const state = !session->config.get_punch_out();
1293 session->config.set_punch_out (state);
1298 MackieControlProtocol::punch_out_release (Button &)
1300 return session->config.get_punch_out();
1304 MackieControlProtocol::home_press (Button &)
1306 session->goto_start();
1311 MackieControlProtocol::home_release (Button &)
1317 MackieControlProtocol::end_press (Button &)
1319 session->goto_end();
1324 MackieControlProtocol::end_release (Button &)
1330 MackieControlProtocol::clicking_press (Button &)
1332 bool state = !Config->get_clicking();
1333 Config->set_clicking (state);
1338 MackieControlProtocol::clicking_release (Button &)
1340 return Config->get_clicking();
1343 LedState MackieControlProtocol::global_solo_press (Button &)
1345 bool state = !session->soloing();
1346 session->set_solo (session->get_routes(), state);
1350 LedState MackieControlProtocol::global_solo_release (Button &)
1352 return session->soloing();
1355 ///////////////////////////////////////////
1357 ///////////////////////////////////////////
1359 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
1361 if (p == "punch-in") {
1362 update_global_button ("punch_in", session->config.get_punch_in());
1363 } else if (p == "punch-out") {
1364 update_global_button ("punch_out", session->config.get_punch_out());
1365 } else if (p == "clicking") {
1366 update_global_button ("clicking", Config->get_clicking());
1368 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("parameter changed: %1\n", p));
1372 // RouteList is the set of routes that have just been added
1374 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
1376 // currently assigned banks are less than the full set of
1377 // strips, so activate the new strip now.
1378 if (route_signals.size() < route_table.size()) {
1379 refresh_current_bank();
1381 // otherwise route added, but current bank needs no updating
1383 // make sure remote id changes in the new route are handled
1384 typedef ARDOUR::RouteList ARS;
1386 for (ARS::iterator it = rl.begin(); it != rl.end(); ++it) {
1387 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
1392 MackieControlProtocol::notify_solo_active_changed (bool active)
1394 Button * rude_solo = reinterpret_cast<Button*> (surface().controls_by_name["solo"]);
1395 mcu_port().write (builder.build_led (*rude_solo, active ? flashing : off));
1399 MackieControlProtocol::notify_remote_id_changed()
1401 Sorted sorted = get_sorted_routes();
1403 // if a remote id has been moved off the end, we need to shift
1404 // the current bank backwards.
1405 if (sorted.size() - _current_initial_bank < route_signals.size()) {
1406 // but don't shift backwards past the zeroth channel
1407 switch_banks (max((Sorted::size_type) 0, sorted.size() - route_signals.size()));
1409 // Otherwise just refresh the current bank
1410 refresh_current_bank();
1414 ///////////////////////////////////////////
1415 // Transport signals
1416 ///////////////////////////////////////////
1419 MackieControlProtocol::notify_record_state_changed()
1421 // switch rec button on / off / flashing
1422 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1423 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1427 MackieControlProtocol::notify_transport_state_changed()
1429 // switch various play and stop buttons on / off
1430 update_global_button ("play", session->transport_rolling());
1431 update_global_button ("stop", !session->transport_rolling());
1432 update_global_button ("loop", session->get_play_loop());
1434 _transport_previously_rolling = session->transport_rolling();
1436 // rec is special because it's tristate
1437 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1438 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1441 /////////////////////////////////////
1443 /////////////////////////////////////
1445 MackieControlProtocol::left_press (Button &)
1447 Sorted sorted = get_sorted_routes();
1448 if (sorted.size() > route_table.size()) {
1449 int new_initial = _current_initial_bank - route_table.size();
1450 if (new_initial < 0) {
1454 if (new_initial != int (_current_initial_bank)) {
1455 session->set_dirty();
1456 switch_banks (new_initial);
1466 MackieControlProtocol::left_release (Button &)
1472 MackieControlProtocol::right_press (Button &)
1474 Sorted sorted = get_sorted_routes();
1475 if (sorted.size() > route_table.size()) {
1476 uint32_t delta = sorted.size() - (route_table.size() + _current_initial_bank);
1478 if (delta > route_table.size()) {
1479 delta = route_table.size();
1483 session->set_dirty();
1484 switch_banks (_current_initial_bank + delta);
1494 MackieControlProtocol::right_release (Button &)
1500 MackieControlProtocol::channel_left_press (Button &)
1502 Sorted sorted = get_sorted_routes();
1503 if (sorted.size() > route_table.size()) {
1512 MackieControlProtocol::channel_left_release (Button &)
1518 MackieControlProtocol::channel_right_press (Button &)
1520 Sorted sorted = get_sorted_routes();
1521 if (sorted.size() > route_table.size()) {
1530 MackieControlProtocol::channel_right_release (Button &)
1535 /////////////////////////////////////
1537 /////////////////////////////////////
1539 MackieControlProtocol::marker_press (Button &)
1541 // cut'n'paste from LocationUI::add_new_location()
1543 framepos_t where = session->audible_frame();
1544 session->locations()->next_available_name(markername,"mcu");
1545 Location *location = new Location (*session, where, where, markername, Location::IsMark);
1546 session->begin_reversible_command (_("add marker"));
1547 XMLNode &before = session->locations()->get_state();
1548 session->locations()->add (location, true);
1549 XMLNode &after = session->locations()->get_state();
1550 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1551 session->commit_reversible_command ();
1556 MackieControlProtocol::marker_release (Button &)
1562 jog_wheel_state_display (JogWheel::State state, SurfacePort & port)
1565 case JogWheel::zoom:
1566 port.write (builder.two_char_display ("Zm"));
1568 case JogWheel::scroll:
1569 port.write (builder.two_char_display ("Sc"));
1571 case JogWheel::scrub:
1572 port.write (builder.two_char_display ("Sb"));
1574 case JogWheel::shuttle:
1575 port.write (builder.two_char_display ("Sh"));
1577 case JogWheel::speed:
1578 port.write (builder.two_char_display ("Sp"));
1580 case JogWheel::select:
1581 port.write (builder.two_char_display ("Se"));
1587 MackieControlProtocol::zoom_press (Mackie::Button &)
1589 _jog_wheel.zoom_state_toggle();
1590 update_global_button ("scrub", _jog_wheel.jog_wheel_state() == JogWheel::scrub);
1591 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1592 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1596 MackieControlProtocol::zoom_release (Mackie::Button &)
1598 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1602 MackieControlProtocol::scrub_press (Mackie::Button &)
1604 _jog_wheel.scrub_state_cycle();
1605 update_global_button ("zoom", _jog_wheel.jog_wheel_state() == JogWheel::zoom);
1606 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1608 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1610 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1615 MackieControlProtocol::scrub_release (Mackie::Button &)
1618 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1620 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1625 MackieControlProtocol::drop_press (Button &)
1627 session->remove_last_capture();
1632 MackieControlProtocol::drop_release (Button &)
1638 MackieControlProtocol::save_press (Button &)
1640 session->save_state ("");
1645 MackieControlProtocol::save_release (Button &)
1651 MackieControlProtocol::timecode_beats_press (Button &)
1653 switch (_timecode_type) {
1654 case ARDOUR::AnyTime::BBT:
1655 _timecode_type = ARDOUR::AnyTime::Timecode;
1657 case ARDOUR::AnyTime::Timecode:
1658 _timecode_type = ARDOUR::AnyTime::BBT;
1662 os << "Unknown Anytime::Type " << _timecode_type;
1663 throw runtime_error (os.str());
1665 update_timecode_beats_led();
1670 MackieControlProtocol::timecode_beats_release (Button &)
1675 list<boost::shared_ptr<ARDOUR::Bundle> >
1676 MackieControlProtocol::bundles ()
1678 list<boost::shared_ptr<ARDOUR::Bundle> > b;
1679 b.push_back (_input_bundle);
1680 b.push_back (_output_bundle);
1685 MackieControlProtocol::port_connected_or_disconnected (string a, string b, bool connected)
1687 /* If something is connected to one of our output ports, send MIDI to update the surface
1688 to whatever state it should have.
1695 MackiePorts::const_iterator i = _ports.begin();
1696 while (i != _ports.end()) {
1698 string const n = AudioEngine::instance()->make_port_name_non_relative ((*i)->output_port().name ());
1700 if (a == n || b == n) {
1707 if (i != _ports.end ()) {
1713 MackieControlProtocol::do_request (MackieControlUIRequest* req)
1715 if (req->type == CallSlot) {
1717 call_slot (MISSING_INVALIDATOR, req->the_slot);
1719 } else if (req->type == Quit) {
1726 MackieControlProtocol::stop ()