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 MackieMidiBuilder builder;
75 #define midi_ui_context() MidiControlUI::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
76 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
78 MackieControlProtocol::MackieControlProtocol (Session& session)
79 : ControlProtocol (session, X_("Mackie"), MidiControlUI::instance())
80 , _current_initial_bank (0)
83 , _timecode_type (ARDOUR::AnyTime::BBT)
84 , _input_bundle (new ARDOUR::Bundle (_("Mackie Control In"), true))
85 , _output_bundle (new ARDOUR::Bundle (_("Mackie Control Out"), false))
87 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
90 MackieControlProtocol::~MackieControlProtocol()
92 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol\n");
97 catch (exception & e) {
98 cout << "~MackieControlProtocol caught " << e.what() << endl;
101 cout << "~MackieControlProtocol caught unknown" << endl;
104 DEBUG_TRACE (DEBUG::MackieControl, "finished ~MackieControlProtocol::MackieControlProtocol\n");
108 MackieControlProtocol::surface()
111 throw MackieControlException ("_surface is 0 in MackieControlProtocol::surface");
116 const Mackie::SurfacePort&
117 MackieControlProtocol::mcu_port() const
119 if (_ports.size() < 1) {
122 return dynamic_cast<const MackiePort &> (*_ports[0]);
127 MackieControlProtocol::mcu_port()
129 if (_ports.size() < 1) {
132 return dynamic_cast<MackiePort &> (*_ports[0]);
136 // go to the previous track.
137 // Assume that get_sorted_routes().size() > route_table.size()
139 MackieControlProtocol::prev_track()
141 if (_current_initial_bank >= 1) {
142 session->set_dirty();
143 switch_banks (_current_initial_bank - 1);
147 // go to the next track.
148 // Assume that get_sorted_routes().size() > route_table.size()
150 MackieControlProtocol::next_track()
152 Sorted sorted = get_sorted_routes();
153 if (_current_initial_bank + route_table.size() < sorted.size())
155 session->set_dirty();
156 switch_banks (_current_initial_bank + 1);
161 MackieControlProtocol::clear_route_signals()
163 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
166 route_signals.clear();
169 // return the port for a given id - 0 based
170 // throws an exception if no port found
172 MackieControlProtocol::port_for_id (uint32_t index)
174 uint32_t current_max = 0;
175 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
176 current_max += (*it)->strips();
177 if (index < current_max) return **it;
180 // oops - no matching port
182 os << "No port for index " << index;
183 throw MackieControlException (os.str());
186 // predicate for sort call in get_sorted_routes
187 struct RouteByRemoteId
189 bool operator () (const boost::shared_ptr<Route> & a, const boost::shared_ptr<Route> & b) const
191 return a->remote_control_id() < b->remote_control_id();
194 bool operator () (const Route & a, const Route & b) const
196 return a.remote_control_id() < b.remote_control_id();
199 bool operator () (const Route * a, const Route * b) const
201 return a->remote_control_id() < b->remote_control_id();
205 MackieControlProtocol::Sorted
206 MackieControlProtocol::get_sorted_routes()
211 boost::shared_ptr<RouteList> routes = session->get_routes();
212 set<uint32_t> remote_ids;
214 // routes with remote_id 0 should never be added
215 // TODO verify this with ardour devs
216 // remote_ids.insert (0);
218 // sort in remote_id order, and exclude master, control and hidden routes
219 // and any routes that are already set.
220 for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it)
222 Route & route = **it;
225 && !route.is_master()
226 && !route.is_hidden()
227 && !route.is_monitor()
228 && remote_ids.find (route.remote_control_id()) == remote_ids.end()
231 sorted.push_back (*it);
232 remote_ids.insert (route.remote_control_id());
235 sort (sorted.begin(), sorted.end(), RouteByRemoteId());
240 MackieControlProtocol::refresh_current_bank()
242 switch_banks (_current_initial_bank);
246 MackieControlProtocol::switch_banks (int initial)
248 // DON'T prevent bank switch if initial == _current_initial_bank
249 // because then this method can't be used as a refresh
252 Sorted sorted = get_sorted_routes();
253 int delta = sorted.size() - route_table.size();
254 if (initial < 0 || (delta > 0 && initial > delta))
256 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("not switching to %1\n", initial));
259 _current_initial_bank = initial;
261 // first clear the signals from old routes
262 // taken care of by the RouteSignal destructors
263 clear_route_signals();
265 // now set the signals for new routes
266 if (_current_initial_bank <= sorted.size())
268 // fetch the bank start and end to switch to
269 uint32_t end_pos = min (route_table.size(), sorted.size());
270 Sorted::iterator it = sorted.begin() + _current_initial_bank;
271 Sorted::iterator end = sorted.begin() + _current_initial_bank + end_pos;
273 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch to %1, %2\n", _current_initial_bank, end_pos));
275 // link routes to strips
277 for (; it != end && it != sorted.end(); ++it, ++i)
279 boost::shared_ptr<Route> route = *it;
281 assert (surface().strips[i]);
282 Strip & strip = *surface().strips[i];
284 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("remote id %1 connecting %2 to %3 with port %4\n",
285 route->remote_control_id(), route->name(), strip.name(), port_for_id(i)));
286 route_table[i] = route;
287 RouteSignal * rs = new RouteSignal (route, *this, strip, port_for_id(i));
288 route_signals.push_back (rs);
289 // update strip from route
293 // create dead strips if there aren't enough routes to
295 for (; i < route_table.size(); ++i)
297 Strip & strip = *surface().strips[i];
298 // send zero for this strip
299 MackiePort & port = port_for_id(i);
300 port.write (builder.zero_strip (port, strip));
304 // display the current start bank.
305 surface().display_bank_start (mcu_port(), builder, _current_initial_bank);
309 MackieControlProtocol::zero_all()
311 // TODO turn off Timecode displays
314 for (Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it)
316 MackiePort & port = port_for_id ((*it)->index());
317 port.write (builder.zero_strip (port, **it));
320 // and the master strip
321 mcu_port().write (builder.zero_strip (dynamic_cast<MackiePort&> (mcu_port()), master_strip()));
323 // turn off global buttons and leds
324 // global buttons are only ever on mcu_port, so we don't have
325 // to figure out which port.
326 for (Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it)
328 Control & control = **it;
329 if (!control.group().is_strip() && control.accepts_feedback())
331 mcu_port().write (builder.zero_control (control));
335 // any hardware-specific stuff
336 surface().zero_all (mcu_port(), builder);
340 MackieControlProtocol::set_active (bool yn)
346 // the reason for the locking and unlocking is that
347 // glibmm can't do a condition wait on a RecMutex
350 // TODO what happens if this fails half way?
352 // create MackiePorts
354 Glib::Mutex::Lock lock (update_mutex);
358 // now initialise MackiePorts - ie exchange sysex messages
359 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
363 // wait until all ports are active
364 // TODO a more sophisticated approach would
365 // allow things to start up with only an MCU, even if
366 // extenders were specified but not responding.
367 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
368 (*it)->wait_for_init();
371 // create surface object. This depends on the ports being
372 // correctly initialised
373 initialize_surface();
374 connect_session_signals();
379 // send current control positions to surface
380 // must come after _active = true otherwise it won't run
388 catch (exception & e) {
389 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("set_active to false because exception caught: %1\n", e.what()));
399 MackieControlProtocol::handle_strip_button (Control & control, ButtonState bs, boost::shared_ptr<Route> route)
405 if (control.name() == "recenable")
407 state = !route->record_enabled();
408 route->set_record_enabled (state, this);
410 else if (control.name() == "mute")
412 state = !route->muted();
413 route->set_mute (state, this);
415 else if (control.name() == "solo")
417 state = !route->soloed();
418 route->set_solo (state, this);
420 else if (control.name() == "select")
422 // TODO make the track selected. Whatever that means.
423 //state = default_button_press (dynamic_cast<Button&> (control));
425 else if (control.name() == "vselect")
427 // TODO could be used to select different things to apply the pot to?
428 //state = default_button_press (dynamic_cast<Button&> (control));
432 if (control.name() == "fader_touch")
435 control.strip().gain().in_use (state);
442 MackieControlProtocol::update_led (Mackie::Button & button, Mackie::LedState ls)
446 SurfacePort * port = 0;
447 if (button.group().is_strip())
449 if (button.group().is_master())
455 port = &port_for_id (dynamic_cast<const Strip&> (button.group()).index());
462 port->write (builder.build_led (button, ls));
467 MackieControlProtocol::update_timecode_beats_led()
469 switch (_timecode_type)
471 case ARDOUR::AnyTime::BBT:
472 update_global_led ("beats", on);
473 update_global_led ("timecode", off);
475 case ARDOUR::AnyTime::Timecode:
476 update_global_led ("timecode", on);
477 update_global_led ("beats", off);
481 os << "Unknown Anytime::Type " << _timecode_type;
482 throw runtime_error (os.str());
487 MackieControlProtocol::update_global_button (const string & name, LedState ls)
489 if (surface().controls_by_name.find (name) != surface().controls_by_name.end())
491 Button * button = dynamic_cast<Button*> (surface().controls_by_name[name]);
492 mcu_port().write (builder.build_led (button->led(), ls));
496 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Button %1 not found\n", name));
501 MackieControlProtocol::update_global_led (const string & name, LedState ls)
503 if (surface().controls_by_name.find (name) != surface().controls_by_name.end())
505 Led * led = dynamic_cast<Led*> (surface().controls_by_name[name]);
506 mcu_port().write (builder.build_led (*led, ls));
510 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Led %1 not found\n", name));
514 // send messages to surface to set controls to correct values
516 MackieControlProtocol::update_surface()
520 // do the initial bank switch to connect signals
521 // _current_initial_bank is initialised by set_state
522 switch_banks (_current_initial_bank);
524 // create a RouteSignal for the master route
526 boost::shared_ptr<Route> mr = master_route ();
528 master_route_signal = boost::shared_ptr<RouteSignal> (new RouteSignal (mr, *this, master_strip(), mcu_port()));
529 // update strip from route
530 master_route_signal->notify_all();
533 // sometimes the jog wheel is a pot
534 surface().blank_jog_ring (mcu_port(), builder);
536 // update global buttons and displays
537 notify_record_state_changed();
538 notify_transport_state_changed();
539 update_timecode_beats_led();
544 MackieControlProtocol::connect_session_signals()
546 // receive routes added
547 session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_route_added, this, _1), midi_ui_context());
548 // receive record state toggled
549 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_record_state_changed, this), midi_ui_context());
550 // receive transport state changed
551 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_transport_state_changed, this), midi_ui_context());
552 // receive punch-in and punch-out
553 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
554 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
555 // receive rude solo changed
556 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_solo_active_changed, this, _1), midi_ui_context());
558 // make sure remote id changed signals reach here
559 // see also notify_route_added
560 Sorted sorted = get_sorted_routes();
562 for (Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it) {
563 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind(&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
568 MackieControlProtocol::add_port (MIDI::Port & midi_input_port, MIDI::Port & midi_output_port, int number)
570 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("add port %1 %2\n", midi_input_port.name(), midi_output_port.name()));
572 MackiePort * sport = new MackiePort (*this, midi_input_port, midi_output_port, number);
573 _ports.push_back (sport);
575 sport->init_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_init, this, sport));
576 sport->active_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_active, this, sport));
577 sport->inactive_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_inactive, this, sport));
579 _input_bundle->add_channel (
580 midi_input_port.name(),
581 ARDOUR::DataType::MIDI,
582 session->engine().make_port_name_non_relative (midi_input_port.name())
585 _output_bundle->add_channel (
586 midi_output_port.name(),
587 ARDOUR::DataType::MIDI,
588 session->engine().make_port_name_non_relative (midi_output_port.name())
593 MackieControlProtocol::create_ports()
595 MIDI::Manager * mm = MIDI::Manager::instance();
596 MIDI::Port * midi_input_port = mm->add_port (
597 new MIDI::Port (string_compose (_("%1 in"), default_port_name), MIDI::Port::IsInput, session->engine().jack())
599 MIDI::Port * midi_output_port = mm->add_port (
600 new MIDI::Port (string_compose (_("%1 out"), default_port_name), MIDI::Port::IsOutput, session->engine().jack())
605 if (!midi_input_port->ok() || !midi_output_port->ok()) {
607 os << _("Mackie control MIDI ports could not be created; Mackie control disabled");
608 error << os.str() << endmsg;
609 throw MackieControlException (os.str());
612 add_port (*midi_input_port, *midi_output_port, 0);
614 // open extender ports. Up to 9. Should be enough.
615 // could also use mm->get_midi_ports()
617 for (int index = 1; index <= 9; ++index) {
618 MIDI::Port * midi_input_port = mm->add_port (
619 new MIDI::Port (string_compose (_("mcu_xt_%1 in"), index), MIDI::Port::IsInput, session->engine().jack())
621 MIDI::Port * midi_output_port = mm->add_port (
622 new MIDI::Port (string_compose (_("mcu_xt_%1 out"), index), MIDI::Port::IsOutput, session->engine().jack())
624 if (midi_input_port->ok() && midi_output_port->ok()) {
625 add_port (*midi_input_port, *midi_output_port, index);
630 boost::shared_ptr<Route>
631 MackieControlProtocol::master_route()
633 return session->master_out ();
637 MackieControlProtocol::master_strip()
639 return dynamic_cast<Strip&> (*surface().groups["master"]);
643 MackieControlProtocol::initialize_surface()
645 // set up the route table
647 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
648 strips += (*it)->strips();
651 set_route_table_size (strips);
653 // TODO same as code in mackie_port.cc
654 string emulation = ARDOUR::Config->get_mackie_emulation();
655 if (emulation == "bcf")
657 _surface = new BcfSurface (strips);
659 else if (emulation == "mcu")
661 _surface = new MackieSurface (strips);
666 os << "no Surface class found for emulation: " << emulation;
667 throw MackieControlException (os.str());
672 // Connect events. Must be after route table otherwise there will be trouble
674 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
675 (*it)->control_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_control_event, this, _1, _2, _3));
680 MackieControlProtocol::close()
683 // must be before other shutdown otherwise polling loop
684 // calls methods on objects that are deleted
686 port_connections.drop_connections ();
687 session_connections.drop_connections ();
688 route_connections.drop_connections ();
691 // These will fail if the port has gone away.
692 // So catch the exception and do the rest of the
694 // because the bcf doesn't respond to the next 3 sysex messages
699 catch (exception & e) {
700 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
703 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
705 MackiePort & port = **it;
707 port.write_sysex (0x61);
709 port.write_sysex (0x62);
710 // Reset (reboot into offline mode)
711 port.write_sysex (0x63);
713 catch (exception & e) {
714 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
718 // disconnect routes from strips
719 clear_route_signals();
724 // shut down MackiePorts
725 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
733 MackieControlProtocol::get_state()
735 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state\n");
737 // add name of protocol
738 XMLNode* node = new XMLNode (X_("Protocol"));
739 node->add_property (X_("name"), _name);
743 os << _current_initial_bank;
744 node->add_property (X_("bank"), os.str());
750 MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
752 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::set_state: active %1\n", _active));
756 // fetch current bank
758 if (node.property (X_("bank")) != 0) {
759 string bank = node.property (X_("bank"))->value();
762 uint32_t new_bank = atoi (bank.c_str());
763 if (_current_initial_bank != new_bank) {
764 switch_banks (new_bank);
767 catch (exception & e) {
768 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("exception in MackieControlProtocol::set_state: %1\n", e.what()));
777 MackieControlProtocol::handle_control_event (SurfacePort & port, Control & control, const ControlState & state)
779 // find the route for the control, if there is one
780 boost::shared_ptr<Route> route;
781 if (control.group().is_strip()) {
782 if (control.group().is_master()) {
783 route = master_route();
785 uint32_t index = control.ordinal() - 1 + (port.number() * port.strips());
786 if (index < route_table.size())
787 route = route_table[index];
789 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
793 // This handles control element events from the surface
794 // the state of the controls on the surface is usually updated
796 switch (control.type()) {
797 case Control::type_fader:
798 // find the route in the route table for the id
799 // if the route isn't available, skip it
800 // at which point the fader should just reset itself
803 route->gain_control()->set_value (state.pos);
805 // must echo bytes back to slider now, because
806 // the notifier only works if the fader is not being
807 // touched. Which it is if we're getting input.
808 port.write (builder.build_fader ((Fader&)control, state.pos));
812 case Control::type_button:
813 if (control.group().is_strip()) {
816 handle_strip_button (control, state.button_state, route);
818 // no route so always switch the light off
819 // because no signals will be emitted by a non-route
820 port.write (builder.build_led (control.led(), off));
822 } else if (control.group().is_master()) {
823 // master fader touch
825 handle_strip_button (control, state.button_state, route);
828 // handle all non-strip buttons
829 surface().handle_button (*this, state.button_state, dynamic_cast<Button&> (control));
833 // pot (jog wheel, external control)
834 case Control::type_pot:
835 if (control.group().is_strip()) {
837 boost::shared_ptr<Panner> panner = route->panner_shell()->panner();
838 // pan for mono input routes, or stereo linked panners
840 double p = panner->position ();
842 // calculate new value, and adjust
843 p += state.delta * state.sign;
846 panner->set_position (p);
851 // it's a pot for an umnapped route, so turn all the lights off
852 port.write (builder.build_led_ring (dynamic_cast<Pot &> (control), off));
857 if (control.is_jog())
859 _jog_wheel.jog_event (port, control, state);
863 cout << "external controller" << state.ticks * state.sign << endl;
869 cout << "Control::type not handled: " << control.type() << endl;
873 /////////////////////////////////////////////////
874 // handlers for Route signals
875 // TODO should these be part of RouteSignal?
876 // They started off as signal/slot handlers for signals
877 // from Route, but they're also used in polling for automation
878 /////////////////////////////////////////////////
881 MackieControlProtocol::notify_solo_changed (RouteSignal * route_signal)
885 Button & button = route_signal->strip().solo();
886 route_signal->port().write (builder.build_led (button, route_signal->route()->soloed()));
888 catch (exception & e)
890 cout << e.what() << endl;
895 MackieControlProtocol::notify_mute_changed (RouteSignal * route_signal)
899 Button & button = route_signal->strip().mute();
900 route_signal->port().write (builder.build_led (button, route_signal->route()->muted()));
902 catch (exception & e)
904 cout << e.what() << endl;
909 MackieControlProtocol::notify_record_enable_changed (RouteSignal * route_signal)
913 Button & button = route_signal->strip().recenable();
914 route_signal->port().write (builder.build_led (button, route_signal->route()->record_enabled()));
916 catch (exception & e)
918 cout << e.what() << endl;
922 void MackieControlProtocol::notify_active_changed (RouteSignal *)
926 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::notify_active_changed\n");
927 refresh_current_bank();
929 catch (exception & e)
931 cout << e.what() << endl;
936 MackieControlProtocol::notify_gain_changed (RouteSignal * route_signal, bool force_update)
940 Fader & fader = route_signal->strip().gain();
943 float gain_value = route_signal->route()->gain_control()->get_value();
944 // check that something has actually changed
945 if (force_update || gain_value != route_signal->last_gain_written())
947 route_signal->port().write (builder.build_fader (fader, gain_value));
948 route_signal->last_gain_written (gain_value);
952 catch (exception & e)
954 cout << e.what() << endl;
959 MackieControlProtocol::notify_property_changed (const PropertyChange& what_changed, RouteSignal * route_signal)
961 if (!what_changed.contains (Properties::name)) {
967 Strip & strip = route_signal->strip();
969 /* XXX: not sure about this check to only display stuff for strips of index < 8 */
970 if (!strip.is_master() && strip.index() < 8)
973 string fullname = route_signal->route()->name();
975 if (fullname.length() <= 6)
981 line1 = PBD::short_version (fullname, 6);
984 SurfacePort & port = route_signal->port();
985 port.write (builder.strip_display (port, strip, 0, line1));
986 port.write (builder.strip_display_blank (port, strip, 1));
989 catch (exception & e)
991 cout << e.what() << endl;
996 MackieControlProtocol::notify_panner_changed (RouteSignal * route_signal, bool force_update)
1000 Pot & pot = route_signal->strip().vpot();
1001 boost::shared_ptr<Panner> panner = route_signal->route()->panner();
1003 double pos = panner->position ();
1005 // cache the MidiByteArray here, because the mackie led control is much lower
1006 // resolution than the panner control. So we save lots of byte
1007 // sends in spite of more work on the comparison
1008 MidiByteArray bytes = builder.build_led_ring (pot, ControlState (on, pos), MackieMidiBuilder::midi_pot_mode_dot);
1009 // check that something has actually changed
1010 if (force_update || bytes != route_signal->last_pan_written())
1012 route_signal->port().write (bytes);
1013 route_signal->last_pan_written (bytes);
1018 route_signal->port().write (builder.zero_control (pot));
1021 catch (exception & e)
1023 cout << e.what() << endl;
1027 // TODO handle plugin automation polling
1029 MackieControlProtocol::update_automation (RouteSignal & rs)
1031 ARDOUR::AutoState gain_state = rs.route()->gain_control()->automation_state();
1033 if (gain_state == Touch || gain_state == Play)
1035 notify_gain_changed (&rs, false);
1038 if (rs.route()->panner()) {
1039 ARDOUR::AutoState panner_state = rs.route()->panner()->automation_state();
1040 if (panner_state == Touch || panner_state == Play)
1042 notify_panner_changed (&rs, false);
1048 MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
1050 Timecode::BBT_Time bbt_time;
1051 session->bbt_time (now_frame, bbt_time);
1053 // According to the Logic docs
1054 // digits: 888/88/88/888
1055 // BBT mode: Bars/Beats/Subdivisions/Ticks
1057 os << setw(3) << setfill('0') << bbt_time.bars;
1058 os << setw(2) << setfill('0') << bbt_time.beats;
1060 // figure out subdivisions per beat
1061 const Meter & meter = session->tempo_map().meter_at (now_frame);
1063 if (meter.note_divisor() == 8 && (meter.beats_per_bar() == 12.0 || meter.beats_per_bar() == 9.0 || meter.beats_per_bar() == 6.0))
1068 uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
1069 uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
1071 os << setw(2) << setfill('0') << subdivisions + 1;
1072 os << setw(3) << setfill('0') << ticks;
1078 MackieControlProtocol::format_timecode_timecode (framepos_t now_frame)
1080 Timecode::Time timecode;
1081 session->timecode_time (now_frame, timecode);
1083 // According to the Logic docs
1084 // digits: 888/88/88/888
1085 // Timecode mode: Hours/Minutes/Seconds/Frames
1087 os << setw(3) << setfill('0') << timecode.hours;
1088 os << setw(2) << setfill('0') << timecode.minutes;
1089 os << setw(2) << setfill('0') << timecode.seconds;
1090 os << setw(3) << setfill('0') << timecode.frames;
1096 MackieControlProtocol::update_timecode_display()
1098 if (surface().has_timecode_display())
1100 // do assignment here so current_frame is fixed
1101 framepos_t current_frame = session->transport_frame();
1104 switch (_timecode_type)
1106 case ARDOUR::AnyTime::BBT:
1107 timecode = format_bbt_timecode (current_frame);
1109 case ARDOUR::AnyTime::Timecode:
1110 timecode = format_timecode_timecode (current_frame);
1114 os << "Unknown timecode: " << _timecode_type;
1115 throw runtime_error (os.str());
1118 // only write the timecode string to the MCU if it's changed
1119 // since last time. This is to reduce midi bandwidth used.
1120 if (timecode != _timecode_last)
1122 surface().display_timecode (mcu_port(), builder, timecode, _timecode_last);
1123 _timecode_last = timecode;
1129 MackieControlProtocol::poll_session_data()
1131 // XXX need to attach this to a timer in the MIDI UI event loop (20msec)
1134 // do all currently mapped routes
1135 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
1136 update_automation (**it);
1139 // and the master strip
1140 if (master_route_signal != 0) {
1141 update_automation (*master_route_signal);
1144 update_timecode_display();
1148 /////////////////////////////////////
1149 // Transport Buttons
1150 /////////////////////////////////////
1153 MackieControlProtocol::frm_left_press (Button &)
1155 // can use first_mark_before/after as well
1156 unsigned long elapsed = _frm_left_last.restart();
1158 Location * loc = session->locations()->first_location_before (
1159 session->transport_frame()
1162 // allow a quick double to go past a previous mark
1163 if (session->transport_rolling() && elapsed < 500 && loc != 0)
1165 Location * loc_two_back = session->locations()->first_location_before (loc->start());
1166 if (loc_two_back != 0)
1172 // move to the location, if it's valid
1175 session->request_locate (loc->start(), session->transport_rolling());
1182 MackieControlProtocol::frm_left_release (Button &)
1188 MackieControlProtocol::frm_right_press (Button &)
1190 // can use first_mark_before/after as well
1191 Location * loc = session->locations()->first_location_after (
1192 session->transport_frame()
1194 if (loc != 0) session->request_locate (loc->start(), session->transport_rolling());
1199 MackieControlProtocol::frm_right_release (Button &)
1205 MackieControlProtocol::stop_press (Button &)
1207 session->request_stop();
1212 MackieControlProtocol::stop_release (Button &)
1214 return session->transport_stopped();
1218 MackieControlProtocol::play_press (Button &)
1220 session->request_transport_speed (1.0);
1225 MackieControlProtocol::play_release (Button &)
1227 return session->transport_rolling();
1231 MackieControlProtocol::record_press (Button &)
1233 if (session->get_record_enabled()) {
1234 session->disable_record (false);
1236 session->maybe_enable_record();
1242 MackieControlProtocol::record_release (Button &)
1244 if (session->get_record_enabled()) {
1245 if (session->transport_rolling()) {
1256 MackieControlProtocol::rewind_press (Button &)
1258 _jog_wheel.push (JogWheel::speed);
1259 _jog_wheel.transport_direction (-1);
1260 session->request_transport_speed (-_jog_wheel.transport_speed());
1265 MackieControlProtocol::rewind_release (Button &)
1268 _jog_wheel.transport_direction (0);
1269 if (_transport_previously_rolling)
1270 session->request_transport_speed (1.0);
1272 session->request_stop();
1277 MackieControlProtocol::ffwd_press (Button &)
1279 _jog_wheel.push (JogWheel::speed);
1280 _jog_wheel.transport_direction (1);
1281 session->request_transport_speed (_jog_wheel.transport_speed());
1286 MackieControlProtocol::ffwd_release (Button &)
1289 _jog_wheel.transport_direction (0);
1290 if (_transport_previously_rolling)
1291 session->request_transport_speed (1.0);
1293 session->request_stop();
1298 MackieControlProtocol::loop_press (Button &)
1300 session->request_play_loop (!session->get_play_loop());
1305 MackieControlProtocol::loop_release (Button &)
1307 return session->get_play_loop();
1311 MackieControlProtocol::punch_in_press (Button &)
1313 bool state = !session->config.get_punch_in();
1314 session->config.set_punch_in (state);
1319 MackieControlProtocol::punch_in_release (Button &)
1321 return session->config.get_punch_in();
1325 MackieControlProtocol::punch_out_press (Button &)
1327 bool state = !session->config.get_punch_out();
1328 session->config.set_punch_out (state);
1333 MackieControlProtocol::punch_out_release (Button &)
1335 return session->config.get_punch_out();
1339 MackieControlProtocol::home_press (Button &)
1341 session->goto_start();
1346 MackieControlProtocol::home_release (Button &)
1352 MackieControlProtocol::end_press (Button &)
1354 session->goto_end();
1359 MackieControlProtocol::end_release (Button &)
1365 MackieControlProtocol::clicking_press (Button &)
1367 bool state = !Config->get_clicking();
1368 Config->set_clicking (state);
1373 MackieControlProtocol::clicking_release (Button &)
1375 return Config->get_clicking();
1378 LedState MackieControlProtocol::global_solo_press (Button &)
1380 bool state = !session->soloing();
1381 session->set_solo (session->get_routes(), state);
1385 LedState MackieControlProtocol::global_solo_release (Button &)
1387 return session->soloing();
1390 ///////////////////////////////////////////
1392 ///////////////////////////////////////////
1394 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
1396 if (p == "punch-in")
1398 update_global_button ("punch_in", session->config.get_punch_in());
1400 else if (p == "punch-out")
1402 update_global_button ("punch_out", session->config.get_punch_out());
1404 else if (p == "clicking")
1406 update_global_button ("clicking", Config->get_clicking());
1410 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("parameter changed: %1\n", p));
1414 // RouteList is the set of routes that have just been added
1416 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
1418 // currently assigned banks are less than the full set of
1419 // strips, so activate the new strip now.
1420 if (route_signals.size() < route_table.size())
1422 refresh_current_bank();
1424 // otherwise route added, but current bank needs no updating
1426 // make sure remote id changes in the new route are handled
1427 typedef ARDOUR::RouteList ARS;
1429 for (ARS::iterator it = rl.begin(); it != rl.end(); ++it) {
1430 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
1435 MackieControlProtocol::notify_solo_active_changed (bool active)
1437 Button * rude_solo = reinterpret_cast<Button*> (surface().controls_by_name["solo"]);
1438 mcu_port().write (builder.build_led (*rude_solo, active ? flashing : off));
1442 MackieControlProtocol::notify_remote_id_changed()
1444 Sorted sorted = get_sorted_routes();
1446 // if a remote id has been moved off the end, we need to shift
1447 // the current bank backwards.
1448 if (sorted.size() - _current_initial_bank < route_signals.size())
1450 // but don't shift backwards past the zeroth channel
1451 switch_banks (max((Sorted::size_type) 0, sorted.size() - route_signals.size()));
1453 // Otherwise just refresh the current bank
1456 refresh_current_bank();
1460 ///////////////////////////////////////////
1461 // Transport signals
1462 ///////////////////////////////////////////
1465 MackieControlProtocol::notify_record_state_changed()
1467 // switch rec button on / off / flashing
1468 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1469 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1473 MackieControlProtocol::notify_transport_state_changed()
1475 // switch various play and stop buttons on / off
1476 update_global_button ("play", session->transport_rolling());
1477 update_global_button ("stop", !session->transport_rolling());
1478 update_global_button ("loop", session->get_play_loop());
1480 _transport_previously_rolling = session->transport_rolling();
1482 // rec is special because it's tristate
1483 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1484 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1487 /////////////////////////////////////
1489 /////////////////////////////////////
1491 MackieControlProtocol::left_press (Button &)
1493 Sorted sorted = get_sorted_routes();
1494 if (sorted.size() > route_table.size())
1496 int new_initial = _current_initial_bank - route_table.size();
1497 if (new_initial < 0) new_initial = 0;
1498 if (new_initial != int (_current_initial_bank))
1500 session->set_dirty();
1501 switch_banks (new_initial);
1513 MackieControlProtocol::left_release (Button &)
1519 MackieControlProtocol::right_press (Button &)
1521 Sorted sorted = get_sorted_routes();
1522 if (sorted.size() > route_table.size()) {
1523 uint32_t delta = sorted.size() - (route_table.size() + _current_initial_bank);
1524 if (delta > route_table.size()) delta = route_table.size();
1526 session->set_dirty();
1527 switch_banks (_current_initial_bank + delta);
1537 MackieControlProtocol::right_release (Button &)
1543 MackieControlProtocol::channel_left_press (Button &)
1545 Sorted sorted = get_sorted_routes();
1546 if (sorted.size() > route_table.size())
1558 MackieControlProtocol::channel_left_release (Button &)
1564 MackieControlProtocol::channel_right_press (Button &)
1566 Sorted sorted = get_sorted_routes();
1567 if (sorted.size() > route_table.size())
1579 MackieControlProtocol::channel_right_release (Button &)
1584 /////////////////////////////////////
1586 /////////////////////////////////////
1588 MackieControlProtocol::marker_press (Button &)
1590 // cut'n'paste from LocationUI::add_new_location()
1592 framepos_t where = session->audible_frame();
1593 session->locations()->next_available_name(markername,"mcu");
1594 Location *location = new Location (*session, where, where, markername, Location::IsMark);
1595 session->begin_reversible_command (_("add marker"));
1596 XMLNode &before = session->locations()->get_state();
1597 session->locations()->add (location, true);
1598 XMLNode &after = session->locations()->get_state();
1599 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1600 session->commit_reversible_command ();
1605 MackieControlProtocol::marker_release (Button &)
1611 jog_wheel_state_display (JogWheel::State state, SurfacePort & port)
1615 case JogWheel::zoom: port.write (builder.two_char_display ("Zm")); break;
1616 case JogWheel::scroll: port.write (builder.two_char_display ("Sc")); break;
1617 case JogWheel::scrub: port.write (builder.two_char_display ("Sb")); break;
1618 case JogWheel::shuttle: port.write (builder.two_char_display ("Sh")); break;
1619 case JogWheel::speed: port.write (builder.two_char_display ("Sp")); break;
1620 case JogWheel::select: port.write (builder.two_char_display ("Se")); break;
1625 MackieControlProtocol::zoom_press (Mackie::Button &)
1627 _jog_wheel.zoom_state_toggle();
1628 update_global_button ("scrub", _jog_wheel.jog_wheel_state() == JogWheel::scrub);
1629 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1630 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1634 MackieControlProtocol::zoom_release (Mackie::Button &)
1636 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1640 MackieControlProtocol::scrub_press (Mackie::Button &)
1642 _jog_wheel.scrub_state_cycle();
1643 update_global_button ("zoom", _jog_wheel.jog_wheel_state() == JogWheel::zoom);
1644 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1646 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1648 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1653 MackieControlProtocol::scrub_release (Mackie::Button &)
1656 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1658 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1663 MackieControlProtocol::drop_press (Button &)
1665 session->remove_last_capture();
1670 MackieControlProtocol::drop_release (Button &)
1676 MackieControlProtocol::save_press (Button &)
1678 session->save_state ("");
1683 MackieControlProtocol::save_release (Button &)
1689 MackieControlProtocol::timecode_beats_press (Button &)
1691 switch (_timecode_type)
1693 case ARDOUR::AnyTime::BBT:
1694 _timecode_type = ARDOUR::AnyTime::Timecode;
1696 case ARDOUR::AnyTime::Timecode:
1697 _timecode_type = ARDOUR::AnyTime::BBT;
1701 os << "Unknown Anytime::Type " << _timecode_type;
1702 throw runtime_error (os.str());
1704 update_timecode_beats_led();
1709 MackieControlProtocol::timecode_beats_release (Button &)
1714 list<boost::shared_ptr<ARDOUR::Bundle> >
1715 MackieControlProtocol::bundles ()
1717 list<boost::shared_ptr<ARDOUR::Bundle> > b;
1718 b.push_back (_input_bundle);
1719 b.push_back (_output_bundle);