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.
26 #define __STDC_FORMAT_MACROS
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/route.h"
49 #include "ardour/session.h"
50 #include "ardour/tempo.h"
51 #include "ardour/types.h"
53 #include "mackie_control_protocol.h"
55 #include "midi_byte_array.h"
56 #include "mackie_control_exception.h"
57 #include "route_signal.h"
58 #include "mackie_midi_builder.h"
59 #include "surface_port.h"
61 #include "bcf_surface.h"
62 #include "mackie_surface.h"
64 using namespace ARDOUR;
66 using namespace Mackie;
69 using boost::shared_ptr;
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)
85 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
88 MackieControlProtocol::~MackieControlProtocol()
90 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol\n");
95 catch (exception & e) {
96 cout << "~MackieControlProtocol caught " << e.what() << endl;
99 cout << "~MackieControlProtocol caught unknown" << endl;
102 DEBUG_TRACE (DEBUG::MackieControl, "finished ~MackieControlProtocol::MackieControlProtocol\n");
106 MackieControlProtocol::surface()
109 throw MackieControlException ("_surface is 0 in MackieControlProtocol::surface");
114 const Mackie::SurfacePort&
115 MackieControlProtocol::mcu_port() const
117 if (_ports.size() < 1) {
120 return dynamic_cast<const MackiePort &> (*_ports[0]);
125 MackieControlProtocol::mcu_port()
127 if (_ports.size() < 1) {
130 return dynamic_cast<MackiePort &> (*_ports[0]);
134 // go to the previous track.
135 // Assume that get_sorted_routes().size() > route_table.size()
137 MackieControlProtocol::prev_track()
139 if (_current_initial_bank >= 1) {
140 session->set_dirty();
141 switch_banks (_current_initial_bank - 1);
145 // go to the next track.
146 // Assume that get_sorted_routes().size() > route_table.size()
148 MackieControlProtocol::next_track()
150 Sorted sorted = get_sorted_routes();
151 if (_current_initial_bank + route_table.size() < sorted.size())
153 session->set_dirty();
154 switch_banks (_current_initial_bank + 1);
159 MackieControlProtocol::clear_route_signals()
161 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
164 route_signals.clear();
167 // return the port for a given id - 0 based
168 // throws an exception if no port found
170 MackieControlProtocol::port_for_id (uint32_t index)
172 uint32_t current_max = 0;
173 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
174 current_max += (*it)->strips();
175 if (index < current_max) return **it;
178 // oops - no matching port
180 os << "No port for index " << index;
181 throw MackieControlException (os.str());
184 // predicate for sort call in get_sorted_routes
185 struct RouteByRemoteId
187 bool operator () (const shared_ptr<Route> & a, const shared_ptr<Route> & b) const
189 return a->remote_control_id() < b->remote_control_id();
192 bool operator () (const Route & a, const Route & b) const
194 return a.remote_control_id() < b.remote_control_id();
197 bool operator () (const Route * a, const Route * b) const
199 return a->remote_control_id() < b->remote_control_id();
203 MackieControlProtocol::Sorted
204 MackieControlProtocol::get_sorted_routes()
209 boost::shared_ptr<RouteList> routes = session->get_routes();
210 set<uint32_t> remote_ids;
212 // routes with remote_id 0 should never be added
213 // TODO verify this with ardour devs
214 // remote_ids.insert (0);
216 // sort in remote_id order, and exclude master, control and hidden routes
217 // and any routes that are already set.
218 for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it)
220 Route & route = **it;
223 && !route.is_master()
224 && !route.is_hidden()
225 && !route.is_monitor()
226 && remote_ids.find (route.remote_control_id()) == remote_ids.end()
229 sorted.push_back (*it);
230 remote_ids.insert (route.remote_control_id());
233 sort (sorted.begin(), sorted.end(), RouteByRemoteId());
238 MackieControlProtocol::refresh_current_bank()
240 switch_banks (_current_initial_bank);
244 MackieControlProtocol::switch_banks (int initial)
246 // DON'T prevent bank switch if initial == _current_initial_bank
247 // because then this method can't be used as a refresh
250 Sorted sorted = get_sorted_routes();
251 int delta = sorted.size() - route_table.size();
252 if (initial < 0 || (delta > 0 && initial > delta))
254 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("not switching to %1\n", initial));
257 _current_initial_bank = initial;
259 // first clear the signals from old routes
260 // taken care of by the RouteSignal destructors
261 clear_route_signals();
263 // now set the signals for new routes
264 if (_current_initial_bank <= sorted.size())
266 // fetch the bank start and end to switch to
267 uint32_t end_pos = min (route_table.size(), sorted.size());
268 Sorted::iterator it = sorted.begin() + _current_initial_bank;
269 Sorted::iterator end = sorted.begin() + _current_initial_bank + end_pos;
271 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch to %1, %2\n", _current_initial_bank, end_pos));
273 // link routes to strips
275 for (; it != end && it != sorted.end(); ++it, ++i)
277 boost::shared_ptr<Route> route = *it;
278 Strip & strip = *surface().strips[i];
280 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("remote id %1 connecting %2 to %3 with port %4\n",
281 route->remote_control_id(), route->name(), strip.name(), port_for_id(i)));
282 route_table[i] = route;
283 RouteSignal * rs = new RouteSignal (route, *this, strip, port_for_id(i));
284 route_signals.push_back (rs);
285 // update strip from route
289 // create dead strips if there aren't enough routes to
291 for (; i < route_table.size(); ++i)
293 Strip & strip = *surface().strips[i];
294 // send zero for this strip
295 MackiePort & port = port_for_id(i);
296 port.write (builder.zero_strip (port, strip));
300 // display the current start bank.
301 surface().display_bank_start (mcu_port(), builder, _current_initial_bank);
305 MackieControlProtocol::zero_all()
307 // TODO turn off Timecode displays
310 for (Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it)
312 MackiePort & port = port_for_id ((*it)->index());
313 port.write (builder.zero_strip (port, **it));
316 // and the master strip
317 mcu_port().write (builder.zero_strip (dynamic_cast<MackiePort&> (mcu_port()), master_strip()));
319 // turn off global buttons and leds
320 // global buttons are only ever on mcu_port, so we don't have
321 // to figure out which port.
322 for (Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it)
324 Control & control = **it;
325 if (!control.group().is_strip() && control.accepts_feedback())
327 mcu_port().write (builder.zero_control (control));
331 // any hardware-specific stuff
332 surface().zero_all (mcu_port(), builder);
336 MackieControlProtocol::set_active (bool yn)
342 // the reason for the locking and unlocking is that
343 // glibmm can't do a condition wait on a RecMutex
346 // TODO what happens if this fails half way?
348 // create MackiePorts
350 Glib::Mutex::Lock lock (update_mutex);
354 // now initialise MackiePorts - ie exchange sysex messages
355 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
359 // wait until all ports are active
360 // TODO a more sophisticated approach would
361 // allow things to start up with only an MCU, even if
362 // extenders were specified but not responding.
363 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
364 (*it)->wait_for_init();
367 // create surface object. This depends on the ports being
368 // correctly initialised
369 initialize_surface();
370 connect_session_signals();
375 // send current control positions to surface
376 // must come after _active = true otherwise it won't run
384 catch (exception & e) {
385 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("set_active to false because exception caught: %1\n", e.what()));
395 MackieControlProtocol::handle_strip_button (Control & control, ButtonState bs, boost::shared_ptr<Route> route)
401 if (control.name() == "recenable")
403 state = !route->record_enabled();
404 route->set_record_enable (state, this);
406 else if (control.name() == "mute")
408 state = !route->muted();
409 route->set_mute (state, this);
411 else if (control.name() == "solo")
413 state = !route->soloed();
414 route->set_solo (state, this);
416 else if (control.name() == "select")
418 // TODO make the track selected. Whatever that means.
419 //state = default_button_press (dynamic_cast<Button&> (control));
421 else if (control.name() == "vselect")
423 // TODO could be used to select different things to apply the pot to?
424 //state = default_button_press (dynamic_cast<Button&> (control));
428 if (control.name() == "fader_touch")
431 control.strip().gain().in_use (state);
438 MackieControlProtocol::update_led (Mackie::Button & button, Mackie::LedState ls)
442 SurfacePort * port = 0;
443 if (button.group().is_strip())
445 if (button.group().is_master())
451 port = &port_for_id (dynamic_cast<const Strip&> (button.group()).index());
458 port->write (builder.build_led (button, ls));
463 MackieControlProtocol::update_timecode_beats_led()
465 switch (_timecode_type)
467 case ARDOUR::AnyTime::BBT:
468 update_global_led ("beats", on);
469 update_global_led ("timecode", off);
471 case ARDOUR::AnyTime::Timecode:
472 update_global_led ("timecode", on);
473 update_global_led ("beats", off);
477 os << "Unknown Anytime::Type " << _timecode_type;
478 throw runtime_error (os.str());
483 MackieControlProtocol::update_global_button (const string & name, LedState ls)
485 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));
492 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Button %1 not found\n", name));
497 MackieControlProtocol::update_global_led (const string & name, LedState ls)
499 if (surface().controls_by_name.find (name) != surface().controls_by_name.end())
501 Led * led = dynamic_cast<Led*> (surface().controls_by_name[name]);
502 mcu_port().write (builder.build_led (*led, ls));
506 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Led %1 not found\n", name));
510 // send messages to surface to set controls to correct values
512 MackieControlProtocol::update_surface()
516 // do the initial bank switch to connect signals
517 // _current_initial_bank is initialised by set_state
518 switch_banks (_current_initial_bank);
520 // create a RouteSignal for the master route
522 boost::shared_ptr<Route> mr = master_route ();
524 master_route_signal = shared_ptr<RouteSignal> (new RouteSignal (mr, *this, master_strip(), mcu_port()));
525 // update strip from route
526 master_route_signal->notify_all();
529 // sometimes the jog wheel is a pot
530 surface().blank_jog_ring (mcu_port(), builder);
532 // update global buttons and displays
533 notify_record_state_changed();
534 notify_transport_state_changed();
535 update_timecode_beats_led();
540 MackieControlProtocol::connect_session_signals()
542 // receive routes added
543 session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_route_added, this, _1), midi_ui_context());
544 // receive record state toggled
545 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_record_state_changed, this), midi_ui_context());
546 // receive transport state changed
547 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_transport_state_changed, this), midi_ui_context());
548 // receive punch-in and punch-out
549 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
550 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
551 // receive rude solo changed
552 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_solo_active_changed, this, _1), midi_ui_context());
554 // make sure remote id changed signals reach here
555 // see also notify_route_added
556 Sorted sorted = get_sorted_routes();
558 for (Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it) {
559 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind(&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
564 MackieControlProtocol::add_port (MIDI::Port & midi_port, int number)
566 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("add port %1\n", midi_port.name()));
568 MackiePort * sport = new MackiePort (*this, midi_port, number);
569 _ports.push_back (sport);
571 sport->init_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_init, this, sport));
572 sport->active_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_active, this, sport));
573 sport->inactive_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_inactive, this, sport));
577 MackieControlProtocol::create_ports()
579 MIDI::Manager * mm = MIDI::Manager::instance();
580 MIDI::Port * midi_port = mm->port (default_port_name);
584 if (midi_port == 0) {
586 os << string_compose (_("no MIDI port named \"%1\" exists - Mackie control disabled"), default_port_name);
587 error << os.str() << endmsg;
588 throw MackieControlException (os.str());
591 add_port (*midi_port, 0);
593 // open extender ports. Up to 9. Should be enough.
594 // could also use mm->get_midi_ports()
596 string ext_port_base = "mcu_xt_";
598 for (int index = 1; index <= 9; ++index) {
600 os << ext_port_base << index;
601 MIDI::Port * midi_port = mm->port (os.str());
602 if (midi_port != 0) {
603 add_port (*midi_port, index);
609 MackieControlProtocol::master_route()
611 return session->master_out ();
615 MackieControlProtocol::master_strip()
617 return dynamic_cast<Strip&> (*surface().groups["master"]);
621 MackieControlProtocol::initialize_surface()
623 // set up the route table
625 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
626 strips += (*it)->strips();
629 set_route_table_size (strips);
631 // TODO same as code in mackie_port.cc
632 string emulation = ARDOUR::Config->get_mackie_emulation();
633 if (emulation == "bcf")
635 _surface = new BcfSurface (strips);
637 else if (emulation == "mcu")
639 _surface = new MackieSurface (strips);
644 os << "no Surface class found for emulation: " << emulation;
645 throw MackieControlException (os.str());
650 // Connect events. Must be after route table otherwise there will be trouble
652 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
653 (*it)->control_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_control_event, this, _1, _2, _3));
658 MackieControlProtocol::close()
661 // must be before other shutdown otherwise polling loop
662 // calls methods on objects that are deleted
664 port_connections.drop_connections ();
665 session_connections.drop_connections ();
666 route_connections.drop_connections ();
669 // These will fail if the port has gone away.
670 // So catch the exception and do the rest of the
672 // because the bcf doesn't respond to the next 3 sysex messages
677 catch (exception & e) {
678 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
681 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
683 MackiePort & port = **it;
685 port.write_sysex (0x61);
687 port.write_sysex (0x62);
688 // Reset (reboot into offline mode)
689 port.write_sysex (0x63);
691 catch (exception & e) {
692 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
696 // disconnect routes from strips
697 clear_route_signals();
702 // shut down MackiePorts
703 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
711 MackieControlProtocol::get_state()
713 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state\n");
715 // add name of protocol
716 XMLNode* node = new XMLNode (X_("Protocol"));
717 node->add_property (X_("name"), _name);
721 os << _current_initial_bank;
722 node->add_property (X_("bank"), os.str());
728 MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
730 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::set_state: active %1\n", _active));
734 // fetch current bank
736 if (node.property (X_("bank")) != 0) {
737 string bank = node.property (X_("bank"))->value();
740 uint32_t new_bank = atoi (bank.c_str());
741 if (_current_initial_bank != new_bank) {
742 switch_banks (new_bank);
745 catch (exception & e) {
746 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("exception in MackieControlProtocol::set_state: %1\n", e.what()));
755 MackieControlProtocol::handle_control_event (SurfacePort & port, Control & control, const ControlState & state)
757 // find the route for the control, if there is one
758 boost::shared_ptr<Route> route;
759 if (control.group().is_strip()) {
760 if (control.group().is_master()) {
761 route = master_route();
763 uint32_t index = control.ordinal() - 1 + (port.number() * port.strips());
764 if (index < route_table.size())
765 route = route_table[index];
767 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
771 // This handles control element events from the surface
772 // the state of the controls on the surface is usually updated
774 switch (control.type()) {
775 case Control::type_fader:
776 // find the route in the route table for the id
777 // if the route isn't available, skip it
778 // at which point the fader should just reset itself
781 route->gain_control()->set_value (state.pos);
783 // must echo bytes back to slider now, because
784 // the notifier only works if the fader is not being
785 // touched. Which it is if we're getting input.
786 port.write (builder.build_fader ((Fader&)control, state.pos));
790 case Control::type_button:
791 if (control.group().is_strip()) {
794 handle_strip_button (control, state.button_state, route);
796 // no route so always switch the light off
797 // because no signals will be emitted by a non-route
798 port.write (builder.build_led (control.led(), off));
800 } else if (control.group().is_master()) {
801 // master fader touch
803 handle_strip_button (control, state.button_state, route);
806 // handle all non-strip buttons
807 surface().handle_button (*this, state.button_state, dynamic_cast<Button&> (control));
811 // pot (jog wheel, external control)
812 case Control::type_pot:
813 if (control.group().is_strip()) {
814 if (route != 0 && route->panner())
816 // pan for mono input routes, or stereo linked panners
817 if (route->panner()->npanners() == 1 || (route->panner()->npanners() == 2 && route->panner()->linked()))
819 // assume pan for now
821 route->panner()->streampanner (0).get_effective_position (xpos);
823 // calculate new value, and trim
824 xpos += state.delta * state.sign;
830 route->panner()->streampanner (0).set_position (xpos);
835 // it's a pot for an umnapped route, so turn all the lights off
836 port.write (builder.build_led_ring (dynamic_cast<Pot &> (control), off));
841 if (control.is_jog())
843 _jog_wheel.jog_event (port, control, state);
847 cout << "external controller" << state.ticks * state.sign << endl;
853 cout << "Control::type not handled: " << control.type() << endl;
857 /////////////////////////////////////////////////
858 // handlers for Route signals
859 // TODO should these be part of RouteSignal?
860 // They started off as signal/slot handlers for signals
861 // from Route, but they're also used in polling for automation
862 /////////////////////////////////////////////////
865 MackieControlProtocol::notify_solo_changed (RouteSignal * route_signal)
869 Button & button = route_signal->strip().solo();
870 route_signal->port().write (builder.build_led (button, route_signal->route()->soloed()));
872 catch (exception & e)
874 cout << e.what() << endl;
879 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)
888 cout << e.what() << endl;
893 MackieControlProtocol::notify_record_enable_changed (RouteSignal * route_signal)
897 Button & button = route_signal->strip().recenable();
898 route_signal->port().write (builder.build_led (button, route_signal->route()->record_enabled()));
900 catch (exception & e)
902 cout << e.what() << endl;
906 void MackieControlProtocol::notify_active_changed (RouteSignal *)
910 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::notify_active_changed\n");
911 refresh_current_bank();
913 catch (exception & e)
915 cout << e.what() << endl;
920 MackieControlProtocol::notify_gain_changed (RouteSignal * route_signal, bool force_update)
924 Fader & fader = route_signal->strip().gain();
927 float gain_value = route_signal->route()->gain_control()->get_value();
928 // check that something has actually changed
929 if (force_update || gain_value != route_signal->last_gain_written())
931 route_signal->port().write (builder.build_fader (fader, gain_value));
932 route_signal->last_gain_written (gain_value);
936 catch (exception & e)
938 cout << e.what() << endl;
943 MackieControlProtocol::notify_property_changed (const PropertyChange& what_changed, RouteSignal * route_signal)
945 if (!what_changed.contains (Properties::name)) {
951 Strip & strip = route_signal->strip();
952 if (!strip.is_master())
955 string fullname = route_signal->route()->name();
957 if (fullname.length() <= 6)
963 line1 = PBD::short_version (fullname, 6);
966 SurfacePort & port = route_signal->port();
967 port.write (builder.strip_display (port, strip, 0, line1));
968 port.write (builder.strip_display_blank (port, strip, 1));
971 catch (exception & e)
973 cout << e.what() << endl;
978 MackieControlProtocol::notify_panner_changed (RouteSignal * route_signal, bool force_update)
982 Pot & pot = route_signal->strip().vpot();
983 boost::shared_ptr<Panner> panner = route_signal->route()->panner();
984 if ((panner && panner->npanners() == 1) || (panner->npanners() == 2 && panner->linked()))
987 route_signal->route()->panner()->streampanner(0).get_effective_position (pos);
989 // cache the MidiByteArray here, because the mackie led control is much lower
990 // resolution than the panner control. So we save lots of byte
991 // sends in spite of more work on the comparison
992 MidiByteArray bytes = builder.build_led_ring (pot, ControlState (on, pos), MackieMidiBuilder::midi_pot_mode_dot);
993 // check that something has actually changed
994 if (force_update || bytes != route_signal->last_pan_written())
996 route_signal->port().write (bytes);
997 route_signal->last_pan_written (bytes);
1002 route_signal->port().write (builder.zero_control (pot));
1005 catch (exception & e)
1007 cout << e.what() << endl;
1011 // TODO handle plugin automation polling
1013 MackieControlProtocol::update_automation (RouteSignal & rs)
1015 ARDOUR::AutoState gain_state = rs.route()->gain_control()->automation_state();
1017 if (gain_state == Touch || gain_state == Play)
1019 notify_gain_changed (&rs, false);
1022 if (rs.route()->panner()) {
1023 ARDOUR::AutoState panner_state = rs.route()->panner()->automation_state();
1024 if (panner_state == Touch || panner_state == Play)
1026 notify_panner_changed (&rs, false);
1032 MackieControlProtocol::format_bbt_timecode (nframes_t now_frame)
1035 session->bbt_time (now_frame, bbt_time);
1037 // According to the Logic docs
1038 // digits: 888/88/88/888
1039 // BBT mode: Bars/Beats/Subdivisions/Ticks
1041 os << setw(3) << setfill('0') << bbt_time.bars;
1042 os << setw(2) << setfill('0') << bbt_time.beats;
1044 // figure out subdivisions per beat
1045 const Meter & meter = session->tempo_map().meter_at (now_frame);
1047 if (meter.note_divisor() == 8 && (meter.beats_per_bar() == 12.0 || meter.beats_per_bar() == 9.0 || meter.beats_per_bar() == 6.0))
1052 uint32_t subdivisions = bbt_time.ticks / uint32_t (Meter::ticks_per_beat / subdiv);
1053 uint32_t ticks = bbt_time.ticks % uint32_t (Meter::ticks_per_beat / subdiv);
1055 os << setw(2) << setfill('0') << subdivisions + 1;
1056 os << setw(3) << setfill('0') << ticks;
1062 MackieControlProtocol::format_timecode_timecode (nframes_t now_frame)
1064 Timecode::Time timecode;
1065 session->timecode_time (now_frame, timecode);
1067 // According to the Logic docs
1068 // digits: 888/88/88/888
1069 // Timecode mode: Hours/Minutes/Seconds/Frames
1071 os << setw(3) << setfill('0') << timecode.hours;
1072 os << setw(2) << setfill('0') << timecode.minutes;
1073 os << setw(2) << setfill('0') << timecode.seconds;
1074 os << setw(3) << setfill('0') << timecode.frames;
1080 MackieControlProtocol::update_timecode_display()
1082 if (surface().has_timecode_display())
1084 // do assignment here so current_frame is fixed
1085 nframes_t current_frame = session->transport_frame();
1088 switch (_timecode_type)
1090 case ARDOUR::AnyTime::BBT:
1091 timecode = format_bbt_timecode (current_frame);
1093 case ARDOUR::AnyTime::Timecode:
1094 timecode = format_timecode_timecode (current_frame);
1098 os << "Unknown timecode: " << _timecode_type;
1099 throw runtime_error (os.str());
1102 // only write the timecode string to the MCU if it's changed
1103 // since last time. This is to reduce midi bandwidth used.
1104 if (timecode != _timecode_last)
1106 surface().display_timecode (mcu_port(), builder, timecode, _timecode_last);
1107 _timecode_last = timecode;
1113 MackieControlProtocol::poll_session_data()
1115 // XXX need to attach this to a timer in the MIDI UI event loop (20msec)
1118 // do all currently mapped routes
1119 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
1120 update_automation (**it);
1123 // and the master strip
1124 if (master_route_signal != 0) {
1125 update_automation (*master_route_signal);
1128 update_timecode_display();
1132 /////////////////////////////////////
1133 // Transport Buttons
1134 /////////////////////////////////////
1137 MackieControlProtocol::frm_left_press (Button &)
1139 // can use first_mark_before/after as well
1140 unsigned long elapsed = _frm_left_last.restart();
1142 Location * loc = session->locations()->first_location_before (
1143 session->transport_frame()
1146 // allow a quick double to go past a previous mark
1147 if (session->transport_rolling() && elapsed < 500 && loc != 0)
1149 Location * loc_two_back = session->locations()->first_location_before (loc->start());
1150 if (loc_two_back != 0)
1156 // move to the location, if it's valid
1159 session->request_locate (loc->start(), session->transport_rolling());
1166 MackieControlProtocol::frm_left_release (Button &)
1172 MackieControlProtocol::frm_right_press (Button &)
1174 // can use first_mark_before/after as well
1175 Location * loc = session->locations()->first_location_after (
1176 session->transport_frame()
1178 if (loc != 0) session->request_locate (loc->start(), session->transport_rolling());
1183 MackieControlProtocol::frm_right_release (Button &)
1189 MackieControlProtocol::stop_press (Button &)
1191 session->request_stop();
1196 MackieControlProtocol::stop_release (Button &)
1198 return session->transport_stopped();
1202 MackieControlProtocol::play_press (Button &)
1204 session->request_transport_speed (1.0);
1209 MackieControlProtocol::play_release (Button &)
1211 return session->transport_rolling();
1215 MackieControlProtocol::record_press (Button &)
1217 if (session->get_record_enabled()) {
1218 session->disable_record (false);
1220 session->maybe_enable_record();
1226 MackieControlProtocol::record_release (Button &)
1228 if (session->get_record_enabled()) {
1229 if (session->transport_rolling()) {
1240 MackieControlProtocol::rewind_press (Button &)
1242 _jog_wheel.push (JogWheel::speed);
1243 _jog_wheel.transport_direction (-1);
1244 session->request_transport_speed (-_jog_wheel.transport_speed());
1249 MackieControlProtocol::rewind_release (Button &)
1252 _jog_wheel.transport_direction (0);
1253 if (_transport_previously_rolling)
1254 session->request_transport_speed (1.0);
1256 session->request_stop();
1261 MackieControlProtocol::ffwd_press (Button &)
1263 _jog_wheel.push (JogWheel::speed);
1264 _jog_wheel.transport_direction (1);
1265 session->request_transport_speed (_jog_wheel.transport_speed());
1270 MackieControlProtocol::ffwd_release (Button &)
1273 _jog_wheel.transport_direction (0);
1274 if (_transport_previously_rolling)
1275 session->request_transport_speed (1.0);
1277 session->request_stop();
1282 MackieControlProtocol::loop_press (Button &)
1284 session->request_play_loop (!session->get_play_loop());
1289 MackieControlProtocol::loop_release (Button &)
1291 return session->get_play_loop();
1295 MackieControlProtocol::punch_in_press (Button &)
1297 bool state = !session->config.get_punch_in();
1298 session->config.set_punch_in (state);
1303 MackieControlProtocol::punch_in_release (Button &)
1305 return session->config.get_punch_in();
1309 MackieControlProtocol::punch_out_press (Button &)
1311 bool state = !session->config.get_punch_out();
1312 session->config.set_punch_out (state);
1317 MackieControlProtocol::punch_out_release (Button &)
1319 return session->config.get_punch_out();
1323 MackieControlProtocol::home_press (Button &)
1325 session->goto_start();
1330 MackieControlProtocol::home_release (Button &)
1336 MackieControlProtocol::end_press (Button &)
1338 session->goto_end();
1343 MackieControlProtocol::end_release (Button &)
1349 MackieControlProtocol::clicking_press (Button &)
1351 bool state = !Config->get_clicking();
1352 Config->set_clicking (state);
1357 MackieControlProtocol::clicking_release (Button &)
1359 return Config->get_clicking();
1362 LedState MackieControlProtocol::global_solo_press (Button &)
1364 bool state = !session->soloing();
1365 session->set_solo (session->get_routes(), state);
1369 LedState MackieControlProtocol::global_solo_release (Button &)
1371 return session->soloing();
1374 ///////////////////////////////////////////
1376 ///////////////////////////////////////////
1378 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
1380 if (p == "punch-in")
1382 update_global_button ("punch_in", session->config.get_punch_in());
1384 else if (p == "punch-out")
1386 update_global_button ("punch_out", session->config.get_punch_out());
1388 else if (p == "clicking")
1390 update_global_button ("clicking", Config->get_clicking());
1394 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("parameter changed: %1\n", p));
1398 // RouteList is the set of routes that have just been added
1400 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
1402 // currently assigned banks are less than the full set of
1403 // strips, so activate the new strip now.
1404 if (route_signals.size() < route_table.size())
1406 refresh_current_bank();
1408 // otherwise route added, but current bank needs no updating
1410 // make sure remote id changes in the new route are handled
1411 typedef ARDOUR::RouteList ARS;
1413 for (ARS::iterator it = rl.begin(); it != rl.end(); ++it) {
1414 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
1419 MackieControlProtocol::notify_solo_active_changed (bool active)
1421 Button * rude_solo = reinterpret_cast<Button*> (surface().controls_by_name["solo"]);
1422 mcu_port().write (builder.build_led (*rude_solo, active ? flashing : off));
1426 MackieControlProtocol::notify_remote_id_changed()
1428 Sorted sorted = get_sorted_routes();
1430 // if a remote id has been moved off the end, we need to shift
1431 // the current bank backwards.
1432 if (sorted.size() - _current_initial_bank < route_signals.size())
1434 // but don't shift backwards past the zeroth channel
1435 switch_banks (max((Sorted::size_type) 0, sorted.size() - route_signals.size()));
1437 // Otherwise just refresh the current bank
1440 refresh_current_bank();
1444 ///////////////////////////////////////////
1445 // Transport signals
1446 ///////////////////////////////////////////
1449 MackieControlProtocol::notify_record_state_changed()
1451 // switch rec button on / off / flashing
1452 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1453 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1457 MackieControlProtocol::notify_transport_state_changed()
1459 // switch various play and stop buttons on / off
1460 update_global_button ("play", session->transport_rolling());
1461 update_global_button ("stop", !session->transport_rolling());
1462 update_global_button ("loop", session->get_play_loop());
1464 _transport_previously_rolling = session->transport_rolling();
1466 // rec is special because it's tristate
1467 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1468 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1471 /////////////////////////////////////
1473 /////////////////////////////////////
1475 MackieControlProtocol::left_press (Button &)
1477 Sorted sorted = get_sorted_routes();
1478 if (sorted.size() > route_table.size())
1480 int new_initial = _current_initial_bank - route_table.size();
1481 if (new_initial < 0) new_initial = 0;
1482 if (new_initial != int (_current_initial_bank))
1484 session->set_dirty();
1485 switch_banks (new_initial);
1497 MackieControlProtocol::left_release (Button &)
1503 MackieControlProtocol::right_press (Button &)
1505 Sorted sorted = get_sorted_routes();
1506 if (sorted.size() > route_table.size()) {
1507 uint32_t delta = sorted.size() - (route_table.size() + _current_initial_bank);
1508 if (delta > route_table.size()) delta = route_table.size();
1510 session->set_dirty();
1511 switch_banks (_current_initial_bank + delta);
1521 MackieControlProtocol::right_release (Button &)
1527 MackieControlProtocol::channel_left_press (Button &)
1529 Sorted sorted = get_sorted_routes();
1530 if (sorted.size() > route_table.size())
1542 MackieControlProtocol::channel_left_release (Button &)
1548 MackieControlProtocol::channel_right_press (Button &)
1550 Sorted sorted = get_sorted_routes();
1551 if (sorted.size() > route_table.size())
1563 MackieControlProtocol::channel_right_release (Button &)
1568 /////////////////////////////////////
1570 /////////////////////////////////////
1572 MackieControlProtocol::marker_press (Button &)
1574 // cut'n'paste from LocationUI::add_new_location()
1576 nframes_t where = session->audible_frame();
1577 session->locations()->next_available_name(markername,"mcu");
1578 Location *location = new Location (where, where, markername, Location::IsMark);
1579 session->begin_reversible_command (_("add marker"));
1580 XMLNode &before = session->locations()->get_state();
1581 session->locations()->add (location, true);
1582 XMLNode &after = session->locations()->get_state();
1583 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1584 session->commit_reversible_command ();
1589 MackieControlProtocol::marker_release (Button &)
1595 jog_wheel_state_display (JogWheel::State state, SurfacePort & port)
1599 case JogWheel::zoom: port.write (builder.two_char_display ("Zm")); break;
1600 case JogWheel::scroll: port.write (builder.two_char_display ("Sc")); break;
1601 case JogWheel::scrub: port.write (builder.two_char_display ("Sb")); break;
1602 case JogWheel::shuttle: port.write (builder.two_char_display ("Sh")); break;
1603 case JogWheel::speed: port.write (builder.two_char_display ("Sp")); break;
1604 case JogWheel::select: port.write (builder.two_char_display ("Se")); break;
1609 MackieControlProtocol::zoom_press (Mackie::Button &)
1611 _jog_wheel.zoom_state_toggle();
1612 update_global_button ("scrub", _jog_wheel.jog_wheel_state() == JogWheel::scrub);
1613 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1614 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1618 MackieControlProtocol::zoom_release (Mackie::Button &)
1620 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1624 MackieControlProtocol::scrub_press (Mackie::Button &)
1626 _jog_wheel.scrub_state_cycle();
1627 update_global_button ("zoom", _jog_wheel.jog_wheel_state() == JogWheel::zoom);
1628 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1630 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1632 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1637 MackieControlProtocol::scrub_release (Mackie::Button &)
1640 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1642 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1647 MackieControlProtocol::drop_press (Button &)
1649 session->remove_last_capture();
1654 MackieControlProtocol::drop_release (Button &)
1660 MackieControlProtocol::save_press (Button &)
1662 session->save_state ("");
1667 MackieControlProtocol::save_release (Button &)
1673 MackieControlProtocol::timecode_beats_press (Button &)
1675 switch (_timecode_type)
1677 case ARDOUR::AnyTime::BBT:
1678 _timecode_type = ARDOUR::AnyTime::Timecode;
1680 case ARDOUR::AnyTime::Timecode:
1681 _timecode_type = ARDOUR::AnyTime::BBT;
1685 os << "Unknown Anytime::Type " << _timecode_type;
1686 throw runtime_error (os.str());
1688 update_timecode_beats_led();
1693 MackieControlProtocol::timecode_beats_release (Button &)