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.
27 #define __STDC_FORMAT_MACROS
34 #include <boost/shared_array.hpp>
36 #include "midi++/types.h"
37 #include "midi++/port.h"
38 #include "midi++/manager.h"
39 #include "pbd/pthread_utils.h"
40 #include "pbd/error.h"
41 #include "pbd/memento_command.h"
42 #include "pbd/convert.h"
44 #include "ardour/dB.h"
45 #include "ardour/debug.h"
46 #include "ardour/location.h"
47 #include "ardour/midi_ui.h"
48 #include "ardour/panner.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;
71 using boost::shared_ptr;
75 MackieMidiBuilder builder;
77 #define midi_ui_context() MidiControlUI::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
78 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
80 MackieControlProtocol::MackieControlProtocol (Session& session)
81 : ControlProtocol (session, X_("Mackie"), MidiControlUI::instance())
82 , _current_initial_bank (0)
85 , _timecode_type (ARDOUR::AnyTime::BBT)
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 shared_ptr<Route> & a, const 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;
280 Strip & strip = *surface().strips[i];
282 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("remote id %1 connecting %2 to %3 with port %4\n",
283 route->remote_control_id(), route->name(), strip.name(), port_for_id(i)));
284 route_table[i] = route;
285 RouteSignal * rs = new RouteSignal (route, *this, strip, port_for_id(i));
286 route_signals.push_back (rs);
287 // update strip from route
291 // create dead strips if there aren't enough routes to
293 for (; i < route_table.size(); ++i)
295 Strip & strip = *surface().strips[i];
296 // send zero for this strip
297 MackiePort & port = port_for_id(i);
298 port.write (builder.zero_strip (port, strip));
302 // display the current start bank.
303 surface().display_bank_start (mcu_port(), builder, _current_initial_bank);
307 MackieControlProtocol::zero_all()
309 // TODO turn off Timecode displays
312 for (Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it)
314 MackiePort & port = port_for_id ((*it)->index());
315 port.write (builder.zero_strip (port, **it));
318 // and the master strip
319 mcu_port().write (builder.zero_strip (dynamic_cast<MackiePort&> (mcu_port()), master_strip()));
321 // turn off global buttons and leds
322 // global buttons are only ever on mcu_port, so we don't have
323 // to figure out which port.
324 for (Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it)
326 Control & control = **it;
327 if (!control.group().is_strip() && control.accepts_feedback())
329 mcu_port().write (builder.zero_control (control));
333 // any hardware-specific stuff
334 surface().zero_all (mcu_port(), builder);
338 MackieControlProtocol::set_active (bool yn)
344 // the reason for the locking and unlocking is that
345 // glibmm can't do a condition wait on a RecMutex
348 // TODO what happens if this fails half way?
350 // create MackiePorts
352 Glib::Mutex::Lock lock (update_mutex);
356 // now initialise MackiePorts - ie exchange sysex messages
357 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
361 // wait until all ports are active
362 // TODO a more sophisticated approach would
363 // allow things to start up with only an MCU, even if
364 // extenders were specified but not responding.
365 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
366 (*it)->wait_for_init();
369 // create surface object. This depends on the ports being
370 // correctly initialised
371 initialize_surface();
372 connect_session_signals();
377 // send current control positions to surface
378 // must come after _active = true otherwise it won't run
386 catch (exception & e) {
387 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("set_active to false because exception caught: %1\n", e.what()));
397 MackieControlProtocol::handle_strip_button (Control & control, ButtonState bs, boost::shared_ptr<Route> route)
403 if (control.name() == "recenable")
405 state = !route->record_enabled();
406 route->set_record_enable (state, this);
408 else if (control.name() == "mute")
410 state = !route->muted();
411 route->set_mute (state, this);
413 else if (control.name() == "solo")
415 state = !route->soloed();
416 route->set_solo (state, this);
418 else if (control.name() == "select")
420 // TODO make the track selected. Whatever that means.
421 //state = default_button_press (dynamic_cast<Button&> (control));
423 else if (control.name() == "vselect")
425 // TODO could be used to select different things to apply the pot to?
426 //state = default_button_press (dynamic_cast<Button&> (control));
430 if (control.name() == "fader_touch")
433 control.strip().gain().in_use (state);
440 MackieControlProtocol::update_led (Mackie::Button & button, Mackie::LedState ls)
444 SurfacePort * port = 0;
445 if (button.group().is_strip())
447 if (button.group().is_master())
453 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)
469 case ARDOUR::AnyTime::BBT:
470 update_global_led ("beats", on);
471 update_global_led ("timecode", off);
473 case ARDOUR::AnyTime::Timecode:
474 update_global_led ("timecode", on);
475 update_global_led ("beats", off);
479 os << "Unknown Anytime::Type " << _timecode_type;
480 throw runtime_error (os.str());
485 MackieControlProtocol::update_global_button (const string & name, LedState ls)
487 if (surface().controls_by_name.find (name) != surface().controls_by_name.end())
489 Button * button = dynamic_cast<Button*> (surface().controls_by_name[name]);
490 mcu_port().write (builder.build_led (button->led(), ls));
494 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Button %1 not found\n", name));
499 MackieControlProtocol::update_global_led (const string & name, LedState ls)
501 if (surface().controls_by_name.find (name) != surface().controls_by_name.end())
503 Led * led = dynamic_cast<Led*> (surface().controls_by_name[name]);
504 mcu_port().write (builder.build_led (*led, ls));
508 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Led %1 not found\n", name));
512 // send messages to surface to set controls to correct values
514 MackieControlProtocol::update_surface()
518 // do the initial bank switch to connect signals
519 // _current_initial_bank is initialised by set_state
520 switch_banks (_current_initial_bank);
522 // create a RouteSignal for the master route
524 boost::shared_ptr<Route> mr = master_route ();
526 master_route_signal = shared_ptr<RouteSignal> (new RouteSignal (mr, *this, master_strip(), mcu_port()));
527 // update strip from route
528 master_route_signal->notify_all();
531 // sometimes the jog wheel is a pot
532 surface().blank_jog_ring (mcu_port(), builder);
534 // update global buttons and displays
535 notify_record_state_changed();
536 notify_transport_state_changed();
537 update_timecode_beats_led();
542 MackieControlProtocol::connect_session_signals()
544 // receive routes added
545 session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_route_added, this, _1), midi_ui_context());
546 // receive record state toggled
547 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_record_state_changed, this), midi_ui_context());
548 // receive transport state changed
549 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_transport_state_changed, this), midi_ui_context());
550 // receive punch-in and punch-out
551 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
552 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
553 // receive rude solo changed
554 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_solo_active_changed, this, _1), midi_ui_context());
556 // make sure remote id changed signals reach here
557 // see also notify_route_added
558 Sorted sorted = get_sorted_routes();
560 for (Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it) {
561 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind(&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
566 MackieControlProtocol::add_port (MIDI::Port & midi_port, int number)
568 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("add port %1\n", midi_port.name()));
570 MackiePort * sport = new MackiePort (*this, midi_port, number);
571 _ports.push_back (sport);
573 sport->init_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_init, this, sport));
574 sport->active_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_active, this, sport));
575 sport->inactive_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_inactive, this, sport));
579 MackieControlProtocol::create_ports()
581 MIDI::Manager * mm = MIDI::Manager::instance();
582 MIDI::Port * midi_port = mm->add_port (new MIDI::Port (default_port_name, O_RDWR, session->engine().jack()));
586 if (!midi_port->ok()) {
588 os << string_compose (_("no MIDI port named \"%1\" exists - Mackie control disabled"), default_port_name);
589 error << os.str() << endmsg;
590 throw MackieControlException (os.str());
593 add_port (*midi_port, 0);
595 // open extender ports. Up to 9. Should be enough.
596 // could also use mm->get_midi_ports()
598 string ext_port_base = "mcu_xt_";
600 for (int index = 1; index <= 9; ++index) {
602 os << ext_port_base << index;
603 MIDI::Port * midi_port = mm->add_port (new MIDI::Port (os.str(), O_RDWR, session->engine().jack()));
604 if (midi_port->ok()) {
605 add_port (*midi_port, index);
611 MackieControlProtocol::master_route()
613 return session->master_out ();
617 MackieControlProtocol::master_strip()
619 return dynamic_cast<Strip&> (*surface().groups["master"]);
623 MackieControlProtocol::initialize_surface()
625 // set up the route table
627 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
628 strips += (*it)->strips();
631 set_route_table_size (strips);
633 // TODO same as code in mackie_port.cc
634 string emulation = ARDOUR::Config->get_mackie_emulation();
635 if (emulation == "bcf")
637 _surface = new BcfSurface (strips);
639 else if (emulation == "mcu")
641 _surface = new MackieSurface (strips);
646 os << "no Surface class found for emulation: " << emulation;
647 throw MackieControlException (os.str());
652 // Connect events. Must be after route table otherwise there will be trouble
654 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
655 (*it)->control_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_control_event, this, _1, _2, _3));
660 MackieControlProtocol::close()
663 // must be before other shutdown otherwise polling loop
664 // calls methods on objects that are deleted
666 port_connections.drop_connections ();
667 session_connections.drop_connections ();
668 route_connections.drop_connections ();
671 // These will fail if the port has gone away.
672 // So catch the exception and do the rest of the
674 // because the bcf doesn't respond to the next 3 sysex messages
679 catch (exception & e) {
680 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
683 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
685 MackiePort & port = **it;
687 port.write_sysex (0x61);
689 port.write_sysex (0x62);
690 // Reset (reboot into offline mode)
691 port.write_sysex (0x63);
693 catch (exception & e) {
694 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
698 // disconnect routes from strips
699 clear_route_signals();
704 // shut down MackiePorts
705 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
713 MackieControlProtocol::get_state()
715 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state\n");
717 // add name of protocol
718 XMLNode* node = new XMLNode (X_("Protocol"));
719 node->add_property (X_("name"), _name);
723 os << _current_initial_bank;
724 node->add_property (X_("bank"), os.str());
730 MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
732 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::set_state: active %1\n", _active));
736 // fetch current bank
738 if (node.property (X_("bank")) != 0) {
739 string bank = node.property (X_("bank"))->value();
742 uint32_t new_bank = atoi (bank.c_str());
743 if (_current_initial_bank != new_bank) {
744 switch_banks (new_bank);
747 catch (exception & e) {
748 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("exception in MackieControlProtocol::set_state: %1\n", e.what()));
757 MackieControlProtocol::handle_control_event (SurfacePort & port, Control & control, const ControlState & state)
759 // find the route for the control, if there is one
760 boost::shared_ptr<Route> route;
761 if (control.group().is_strip()) {
762 if (control.group().is_master()) {
763 route = master_route();
765 uint32_t index = control.ordinal() - 1 + (port.number() * port.strips());
766 if (index < route_table.size())
767 route = route_table[index];
769 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
773 // This handles control element events from the surface
774 // the state of the controls on the surface is usually updated
776 switch (control.type()) {
777 case Control::type_fader:
778 // find the route in the route table for the id
779 // if the route isn't available, skip it
780 // at which point the fader should just reset itself
783 route->gain_control()->set_value (state.pos);
785 // must echo bytes back to slider now, because
786 // the notifier only works if the fader is not being
787 // touched. Which it is if we're getting input.
788 port.write (builder.build_fader ((Fader&)control, state.pos));
792 case Control::type_button:
793 if (control.group().is_strip()) {
796 handle_strip_button (control, state.button_state, route);
798 // no route so always switch the light off
799 // because no signals will be emitted by a non-route
800 port.write (builder.build_led (control.led(), off));
802 } else if (control.group().is_master()) {
803 // master fader touch
805 handle_strip_button (control, state.button_state, route);
808 // handle all non-strip buttons
809 surface().handle_button (*this, state.button_state, dynamic_cast<Button&> (control));
813 // pot (jog wheel, external control)
814 case Control::type_pot:
815 if (control.group().is_strip()) {
816 if (route != 0 && route->panner())
818 // pan for mono input routes, or stereo linked panners
819 if (route->panner()->npanners() == 1 || (route->panner()->npanners() == 2 && route->panner()->linked()))
821 // assume pan for now
823 route->panner()->streampanner (0).get_effective_position (xpos);
825 // calculate new value, and trim
826 xpos += state.delta * state.sign;
832 route->panner()->streampanner (0).set_position (xpos);
837 // it's a pot for an umnapped route, so turn all the lights off
838 port.write (builder.build_led_ring (dynamic_cast<Pot &> (control), off));
843 if (control.is_jog())
845 _jog_wheel.jog_event (port, control, state);
849 cout << "external controller" << state.ticks * state.sign << endl;
855 cout << "Control::type not handled: " << control.type() << endl;
859 /////////////////////////////////////////////////
860 // handlers for Route signals
861 // TODO should these be part of RouteSignal?
862 // They started off as signal/slot handlers for signals
863 // from Route, but they're also used in polling for automation
864 /////////////////////////////////////////////////
867 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)
876 cout << e.what() << endl;
881 MackieControlProtocol::notify_mute_changed (RouteSignal * route_signal)
885 Button & button = route_signal->strip().mute();
886 route_signal->port().write (builder.build_led (button, route_signal->route()->muted()));
888 catch (exception & e)
890 cout << e.what() << endl;
895 MackieControlProtocol::notify_record_enable_changed (RouteSignal * route_signal)
899 Button & button = route_signal->strip().recenable();
900 route_signal->port().write (builder.build_led (button, route_signal->route()->record_enabled()));
902 catch (exception & e)
904 cout << e.what() << endl;
908 void MackieControlProtocol::notify_active_changed (RouteSignal *)
912 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::notify_active_changed\n");
913 refresh_current_bank();
915 catch (exception & e)
917 cout << e.what() << endl;
922 MackieControlProtocol::notify_gain_changed (RouteSignal * route_signal, bool force_update)
926 Fader & fader = route_signal->strip().gain();
929 float gain_value = route_signal->route()->gain_control()->get_value();
930 // check that something has actually changed
931 if (force_update || gain_value != route_signal->last_gain_written())
933 route_signal->port().write (builder.build_fader (fader, gain_value));
934 route_signal->last_gain_written (gain_value);
938 catch (exception & e)
940 cout << e.what() << endl;
945 MackieControlProtocol::notify_property_changed (const PropertyChange& what_changed, RouteSignal * route_signal)
947 if (!what_changed.contains (Properties::name)) {
953 Strip & strip = route_signal->strip();
954 if (!strip.is_master())
957 string fullname = route_signal->route()->name();
959 if (fullname.length() <= 6)
965 line1 = PBD::short_version (fullname, 6);
968 SurfacePort & port = route_signal->port();
969 port.write (builder.strip_display (port, strip, 0, line1));
970 port.write (builder.strip_display_blank (port, strip, 1));
973 catch (exception & e)
975 cout << e.what() << endl;
980 MackieControlProtocol::notify_panner_changed (RouteSignal * route_signal, bool force_update)
984 Pot & pot = route_signal->strip().vpot();
985 boost::shared_ptr<Panner> panner = route_signal->route()->panner();
986 if ((panner && panner->npanners() == 1) || (panner->npanners() == 2 && panner->linked()))
989 route_signal->route()->panner()->streampanner(0).get_effective_position (pos);
991 // cache the MidiByteArray here, because the mackie led control is much lower
992 // resolution than the panner control. So we save lots of byte
993 // sends in spite of more work on the comparison
994 MidiByteArray bytes = builder.build_led_ring (pot, ControlState (on, pos), MackieMidiBuilder::midi_pot_mode_dot);
995 // check that something has actually changed
996 if (force_update || bytes != route_signal->last_pan_written())
998 route_signal->port().write (bytes);
999 route_signal->last_pan_written (bytes);
1004 route_signal->port().write (builder.zero_control (pot));
1007 catch (exception & e)
1009 cout << e.what() << endl;
1013 // TODO handle plugin automation polling
1015 MackieControlProtocol::update_automation (RouteSignal & rs)
1017 ARDOUR::AutoState gain_state = rs.route()->gain_control()->automation_state();
1019 if (gain_state == Touch || gain_state == Play)
1021 notify_gain_changed (&rs, false);
1024 if (rs.route()->panner()) {
1025 ARDOUR::AutoState panner_state = rs.route()->panner()->automation_state();
1026 if (panner_state == Touch || panner_state == Play)
1028 notify_panner_changed (&rs, false);
1034 MackieControlProtocol::format_bbt_timecode (nframes_t now_frame)
1037 session->bbt_time (now_frame, bbt_time);
1039 // According to the Logic docs
1040 // digits: 888/88/88/888
1041 // BBT mode: Bars/Beats/Subdivisions/Ticks
1043 os << setw(3) << setfill('0') << bbt_time.bars;
1044 os << setw(2) << setfill('0') << bbt_time.beats;
1046 // figure out subdivisions per beat
1047 const Meter & meter = session->tempo_map().meter_at (now_frame);
1049 if (meter.note_divisor() == 8 && (meter.beats_per_bar() == 12.0 || meter.beats_per_bar() == 9.0 || meter.beats_per_bar() == 6.0))
1054 uint32_t subdivisions = bbt_time.ticks / uint32_t (Meter::ticks_per_beat / subdiv);
1055 uint32_t ticks = bbt_time.ticks % uint32_t (Meter::ticks_per_beat / subdiv);
1057 os << setw(2) << setfill('0') << subdivisions + 1;
1058 os << setw(3) << setfill('0') << ticks;
1064 MackieControlProtocol::format_timecode_timecode (nframes_t now_frame)
1066 Timecode::Time timecode;
1067 session->timecode_time (now_frame, timecode);
1069 // According to the Logic docs
1070 // digits: 888/88/88/888
1071 // Timecode mode: Hours/Minutes/Seconds/Frames
1073 os << setw(3) << setfill('0') << timecode.hours;
1074 os << setw(2) << setfill('0') << timecode.minutes;
1075 os << setw(2) << setfill('0') << timecode.seconds;
1076 os << setw(3) << setfill('0') << timecode.frames;
1082 MackieControlProtocol::update_timecode_display()
1084 if (surface().has_timecode_display())
1086 // do assignment here so current_frame is fixed
1087 nframes_t current_frame = session->transport_frame();
1090 switch (_timecode_type)
1092 case ARDOUR::AnyTime::BBT:
1093 timecode = format_bbt_timecode (current_frame);
1095 case ARDOUR::AnyTime::Timecode:
1096 timecode = format_timecode_timecode (current_frame);
1100 os << "Unknown timecode: " << _timecode_type;
1101 throw runtime_error (os.str());
1104 // only write the timecode string to the MCU if it's changed
1105 // since last time. This is to reduce midi bandwidth used.
1106 if (timecode != _timecode_last)
1108 surface().display_timecode (mcu_port(), builder, timecode, _timecode_last);
1109 _timecode_last = timecode;
1115 MackieControlProtocol::poll_session_data()
1117 // XXX need to attach this to a timer in the MIDI UI event loop (20msec)
1120 // do all currently mapped routes
1121 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
1122 update_automation (**it);
1125 // and the master strip
1126 if (master_route_signal != 0) {
1127 update_automation (*master_route_signal);
1130 update_timecode_display();
1134 /////////////////////////////////////
1135 // Transport Buttons
1136 /////////////////////////////////////
1139 MackieControlProtocol::frm_left_press (Button &)
1141 // can use first_mark_before/after as well
1142 unsigned long elapsed = _frm_left_last.restart();
1144 Location * loc = session->locations()->first_location_before (
1145 session->transport_frame()
1148 // allow a quick double to go past a previous mark
1149 if (session->transport_rolling() && elapsed < 500 && loc != 0)
1151 Location * loc_two_back = session->locations()->first_location_before (loc->start());
1152 if (loc_two_back != 0)
1158 // move to the location, if it's valid
1161 session->request_locate (loc->start(), session->transport_rolling());
1168 MackieControlProtocol::frm_left_release (Button &)
1174 MackieControlProtocol::frm_right_press (Button &)
1176 // can use first_mark_before/after as well
1177 Location * loc = session->locations()->first_location_after (
1178 session->transport_frame()
1180 if (loc != 0) session->request_locate (loc->start(), session->transport_rolling());
1185 MackieControlProtocol::frm_right_release (Button &)
1191 MackieControlProtocol::stop_press (Button &)
1193 session->request_stop();
1198 MackieControlProtocol::stop_release (Button &)
1200 return session->transport_stopped();
1204 MackieControlProtocol::play_press (Button &)
1206 session->request_transport_speed (1.0);
1211 MackieControlProtocol::play_release (Button &)
1213 return session->transport_rolling();
1217 MackieControlProtocol::record_press (Button &)
1219 if (session->get_record_enabled()) {
1220 session->disable_record (false);
1222 session->maybe_enable_record();
1228 MackieControlProtocol::record_release (Button &)
1230 if (session->get_record_enabled()) {
1231 if (session->transport_rolling()) {
1242 MackieControlProtocol::rewind_press (Button &)
1244 _jog_wheel.push (JogWheel::speed);
1245 _jog_wheel.transport_direction (-1);
1246 session->request_transport_speed (-_jog_wheel.transport_speed());
1251 MackieControlProtocol::rewind_release (Button &)
1254 _jog_wheel.transport_direction (0);
1255 if (_transport_previously_rolling)
1256 session->request_transport_speed (1.0);
1258 session->request_stop();
1263 MackieControlProtocol::ffwd_press (Button &)
1265 _jog_wheel.push (JogWheel::speed);
1266 _jog_wheel.transport_direction (1);
1267 session->request_transport_speed (_jog_wheel.transport_speed());
1272 MackieControlProtocol::ffwd_release (Button &)
1275 _jog_wheel.transport_direction (0);
1276 if (_transport_previously_rolling)
1277 session->request_transport_speed (1.0);
1279 session->request_stop();
1284 MackieControlProtocol::loop_press (Button &)
1286 session->request_play_loop (!session->get_play_loop());
1291 MackieControlProtocol::loop_release (Button &)
1293 return session->get_play_loop();
1297 MackieControlProtocol::punch_in_press (Button &)
1299 bool state = !session->config.get_punch_in();
1300 session->config.set_punch_in (state);
1305 MackieControlProtocol::punch_in_release (Button &)
1307 return session->config.get_punch_in();
1311 MackieControlProtocol::punch_out_press (Button &)
1313 bool state = !session->config.get_punch_out();
1314 session->config.set_punch_out (state);
1319 MackieControlProtocol::punch_out_release (Button &)
1321 return session->config.get_punch_out();
1325 MackieControlProtocol::home_press (Button &)
1327 session->goto_start();
1332 MackieControlProtocol::home_release (Button &)
1338 MackieControlProtocol::end_press (Button &)
1340 session->goto_end();
1345 MackieControlProtocol::end_release (Button &)
1351 MackieControlProtocol::clicking_press (Button &)
1353 bool state = !Config->get_clicking();
1354 Config->set_clicking (state);
1359 MackieControlProtocol::clicking_release (Button &)
1361 return Config->get_clicking();
1364 LedState MackieControlProtocol::global_solo_press (Button &)
1366 bool state = !session->soloing();
1367 session->set_solo (session->get_routes(), state);
1371 LedState MackieControlProtocol::global_solo_release (Button &)
1373 return session->soloing();
1376 ///////////////////////////////////////////
1378 ///////////////////////////////////////////
1380 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
1382 if (p == "punch-in")
1384 update_global_button ("punch_in", session->config.get_punch_in());
1386 else if (p == "punch-out")
1388 update_global_button ("punch_out", session->config.get_punch_out());
1390 else if (p == "clicking")
1392 update_global_button ("clicking", Config->get_clicking());
1396 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("parameter changed: %1\n", p));
1400 // RouteList is the set of routes that have just been added
1402 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
1404 // currently assigned banks are less than the full set of
1405 // strips, so activate the new strip now.
1406 if (route_signals.size() < route_table.size())
1408 refresh_current_bank();
1410 // otherwise route added, but current bank needs no updating
1412 // make sure remote id changes in the new route are handled
1413 typedef ARDOUR::RouteList ARS;
1415 for (ARS::iterator it = rl.begin(); it != rl.end(); ++it) {
1416 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
1421 MackieControlProtocol::notify_solo_active_changed (bool active)
1423 Button * rude_solo = reinterpret_cast<Button*> (surface().controls_by_name["solo"]);
1424 mcu_port().write (builder.build_led (*rude_solo, active ? flashing : off));
1428 MackieControlProtocol::notify_remote_id_changed()
1430 Sorted sorted = get_sorted_routes();
1432 // if a remote id has been moved off the end, we need to shift
1433 // the current bank backwards.
1434 if (sorted.size() - _current_initial_bank < route_signals.size())
1436 // but don't shift backwards past the zeroth channel
1437 switch_banks (max((Sorted::size_type) 0, sorted.size() - route_signals.size()));
1439 // Otherwise just refresh the current bank
1442 refresh_current_bank();
1446 ///////////////////////////////////////////
1447 // Transport signals
1448 ///////////////////////////////////////////
1451 MackieControlProtocol::notify_record_state_changed()
1453 // switch rec button on / off / flashing
1454 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1455 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1459 MackieControlProtocol::notify_transport_state_changed()
1461 // switch various play and stop buttons on / off
1462 update_global_button ("play", session->transport_rolling());
1463 update_global_button ("stop", !session->transport_rolling());
1464 update_global_button ("loop", session->get_play_loop());
1466 _transport_previously_rolling = session->transport_rolling();
1468 // rec is special because it's tristate
1469 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1470 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1473 /////////////////////////////////////
1475 /////////////////////////////////////
1477 MackieControlProtocol::left_press (Button &)
1479 Sorted sorted = get_sorted_routes();
1480 if (sorted.size() > route_table.size())
1482 int new_initial = _current_initial_bank - route_table.size();
1483 if (new_initial < 0) new_initial = 0;
1484 if (new_initial != int (_current_initial_bank))
1486 session->set_dirty();
1487 switch_banks (new_initial);
1499 MackieControlProtocol::left_release (Button &)
1505 MackieControlProtocol::right_press (Button &)
1507 Sorted sorted = get_sorted_routes();
1508 if (sorted.size() > route_table.size()) {
1509 uint32_t delta = sorted.size() - (route_table.size() + _current_initial_bank);
1510 if (delta > route_table.size()) delta = route_table.size();
1512 session->set_dirty();
1513 switch_banks (_current_initial_bank + delta);
1523 MackieControlProtocol::right_release (Button &)
1529 MackieControlProtocol::channel_left_press (Button &)
1531 Sorted sorted = get_sorted_routes();
1532 if (sorted.size() > route_table.size())
1544 MackieControlProtocol::channel_left_release (Button &)
1550 MackieControlProtocol::channel_right_press (Button &)
1552 Sorted sorted = get_sorted_routes();
1553 if (sorted.size() > route_table.size())
1565 MackieControlProtocol::channel_right_release (Button &)
1570 /////////////////////////////////////
1572 /////////////////////////////////////
1574 MackieControlProtocol::marker_press (Button &)
1576 // cut'n'paste from LocationUI::add_new_location()
1578 nframes_t where = session->audible_frame();
1579 session->locations()->next_available_name(markername,"mcu");
1580 Location *location = new Location (where, where, markername, Location::IsMark);
1581 session->begin_reversible_command (_("add marker"));
1582 XMLNode &before = session->locations()->get_state();
1583 session->locations()->add (location, true);
1584 XMLNode &after = session->locations()->get_state();
1585 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1586 session->commit_reversible_command ();
1591 MackieControlProtocol::marker_release (Button &)
1597 jog_wheel_state_display (JogWheel::State state, SurfacePort & port)
1601 case JogWheel::zoom: port.write (builder.two_char_display ("Zm")); break;
1602 case JogWheel::scroll: port.write (builder.two_char_display ("Sc")); break;
1603 case JogWheel::scrub: port.write (builder.two_char_display ("Sb")); break;
1604 case JogWheel::shuttle: port.write (builder.two_char_display ("Sh")); break;
1605 case JogWheel::speed: port.write (builder.two_char_display ("Sp")); break;
1606 case JogWheel::select: port.write (builder.two_char_display ("Se")); break;
1611 MackieControlProtocol::zoom_press (Mackie::Button &)
1613 _jog_wheel.zoom_state_toggle();
1614 update_global_button ("scrub", _jog_wheel.jog_wheel_state() == JogWheel::scrub);
1615 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1616 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1620 MackieControlProtocol::zoom_release (Mackie::Button &)
1622 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1626 MackieControlProtocol::scrub_press (Mackie::Button &)
1628 _jog_wheel.scrub_state_cycle();
1629 update_global_button ("zoom", _jog_wheel.jog_wheel_state() == JogWheel::zoom);
1630 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1632 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1634 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1639 MackieControlProtocol::scrub_release (Mackie::Button &)
1642 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1644 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1649 MackieControlProtocol::drop_press (Button &)
1651 session->remove_last_capture();
1656 MackieControlProtocol::drop_release (Button &)
1662 MackieControlProtocol::save_press (Button &)
1664 session->save_state ("");
1669 MackieControlProtocol::save_release (Button &)
1675 MackieControlProtocol::timecode_beats_press (Button &)
1677 switch (_timecode_type)
1679 case ARDOUR::AnyTime::BBT:
1680 _timecode_type = ARDOUR::AnyTime::Timecode;
1682 case ARDOUR::AnyTime::Timecode:
1683 _timecode_type = ARDOUR::AnyTime::BBT;
1687 os << "Unknown Anytime::Type " << _timecode_type;
1688 throw runtime_error (os.str());
1690 update_timecode_beats_led();
1695 MackieControlProtocol::timecode_beats_release (Button &)