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))
88 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
91 MackieControlProtocol::~MackieControlProtocol()
93 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol\n");
98 catch (exception & e) {
99 cout << "~MackieControlProtocol caught " << e.what() << endl;
102 cout << "~MackieControlProtocol caught unknown" << endl;
105 DEBUG_TRACE (DEBUG::MackieControl, "finished ~MackieControlProtocol::MackieControlProtocol\n");
109 MackieControlProtocol::surface()
112 throw MackieControlException ("_surface is 0 in MackieControlProtocol::surface");
117 const Mackie::SurfacePort&
118 MackieControlProtocol::mcu_port() const
120 if (_ports.size() < 1) {
123 return dynamic_cast<const MackiePort &> (*_ports[0]);
128 MackieControlProtocol::mcu_port()
130 if (_ports.size() < 1) {
133 return dynamic_cast<MackiePort &> (*_ports[0]);
137 // go to the previous track.
138 // Assume that get_sorted_routes().size() > route_table.size()
140 MackieControlProtocol::prev_track()
142 if (_current_initial_bank >= 1) {
143 session->set_dirty();
144 switch_banks (_current_initial_bank - 1);
148 // go to the next track.
149 // Assume that get_sorted_routes().size() > route_table.size()
151 MackieControlProtocol::next_track()
153 Sorted sorted = get_sorted_routes();
154 if (_current_initial_bank + route_table.size() < sorted.size())
156 session->set_dirty();
157 switch_banks (_current_initial_bank + 1);
162 MackieControlProtocol::clear_route_signals()
164 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
167 route_signals.clear();
170 // return the port for a given id - 0 based
171 // throws an exception if no port found
173 MackieControlProtocol::port_for_id (uint32_t index)
175 uint32_t current_max = 0;
176 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
177 current_max += (*it)->strips();
178 if (index < current_max) return **it;
181 // oops - no matching port
183 os << "No port for index " << index;
184 throw MackieControlException (os.str());
187 // predicate for sort call in get_sorted_routes
188 struct RouteByRemoteId
190 bool operator () (const boost::shared_ptr<Route> & a, const boost::shared_ptr<Route> & b) const
192 return a->remote_control_id() < b->remote_control_id();
195 bool operator () (const Route & a, const Route & b) const
197 return a.remote_control_id() < b.remote_control_id();
200 bool operator () (const Route * a, const Route * b) const
202 return a->remote_control_id() < b->remote_control_id();
206 MackieControlProtocol::Sorted
207 MackieControlProtocol::get_sorted_routes()
212 boost::shared_ptr<RouteList> routes = session->get_routes();
213 set<uint32_t> remote_ids;
215 // routes with remote_id 0 should never be added
216 // TODO verify this with ardour devs
217 // remote_ids.insert (0);
219 // sort in remote_id order, and exclude master, control and hidden routes
220 // and any routes that are already set.
221 for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it)
223 Route & route = **it;
226 && !route.is_master()
227 && !route.is_hidden()
228 && !route.is_monitor()
229 && remote_ids.find (route.remote_control_id()) == remote_ids.end()
232 sorted.push_back (*it);
233 remote_ids.insert (route.remote_control_id());
236 sort (sorted.begin(), sorted.end(), RouteByRemoteId());
241 MackieControlProtocol::refresh_current_bank()
243 switch_banks (_current_initial_bank);
247 MackieControlProtocol::switch_banks (int initial)
249 // DON'T prevent bank switch if initial == _current_initial_bank
250 // because then this method can't be used as a refresh
253 Sorted sorted = get_sorted_routes();
254 int delta = sorted.size() - route_table.size();
255 if (initial < 0 || (delta > 0 && initial > delta))
257 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("not switching to %1\n", initial));
260 _current_initial_bank = initial;
262 // first clear the signals from old routes
263 // taken care of by the RouteSignal destructors
264 clear_route_signals();
266 // now set the signals for new routes
267 if (_current_initial_bank <= sorted.size())
269 // fetch the bank start and end to switch to
270 uint32_t end_pos = min (route_table.size(), sorted.size());
271 Sorted::iterator it = sorted.begin() + _current_initial_bank;
272 Sorted::iterator end = sorted.begin() + _current_initial_bank + end_pos;
274 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch to %1, %2\n", _current_initial_bank, end_pos));
276 // link routes to strips
278 for (; it != end && it != sorted.end(); ++it, ++i)
280 boost::shared_ptr<Route> route = *it;
282 assert (surface().strips[i]);
283 Strip & strip = *surface().strips[i];
285 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("remote id %1 connecting %2 to %3 with port %4\n",
286 route->remote_control_id(), route->name(), strip.name(), port_for_id(i)));
287 route_table[i] = route;
288 RouteSignal * rs = new RouteSignal (route, *this, strip, port_for_id(i));
289 route_signals.push_back (rs);
290 // update strip from route
294 // create dead strips if there aren't enough routes to
296 for (; i < route_table.size(); ++i)
298 Strip & strip = *surface().strips[i];
299 // send zero for this strip
300 MackiePort & port = port_for_id(i);
301 port.write (builder.zero_strip (port, strip));
305 // display the current start bank.
306 surface().display_bank_start (mcu_port(), builder, _current_initial_bank);
310 MackieControlProtocol::zero_all()
312 // TODO turn off Timecode displays
315 for (Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it)
317 MackiePort & port = port_for_id ((*it)->index());
318 port.write (builder.zero_strip (port, **it));
321 // and the master strip
322 mcu_port().write (builder.zero_strip (dynamic_cast<MackiePort&> (mcu_port()), master_strip()));
324 // turn off global buttons and leds
325 // global buttons are only ever on mcu_port, so we don't have
326 // to figure out which port.
327 for (Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it)
329 Control & control = **it;
330 if (!control.group().is_strip() && control.accepts_feedback())
332 mcu_port().write (builder.zero_control (control));
336 // any hardware-specific stuff
337 surface().zero_all (mcu_port(), builder);
341 MackieControlProtocol::set_active (bool yn)
347 // the reason for the locking and unlocking is that
348 // glibmm can't do a condition wait on a RecMutex
351 // TODO what happens if this fails half way?
353 // create MackiePorts
355 Glib::Mutex::Lock lock (update_mutex);
359 // now initialise MackiePorts - ie exchange sysex messages
360 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
364 // wait until all ports are active
365 // TODO a more sophisticated approach would
366 // allow things to start up with only an MCU, even if
367 // extenders were specified but not responding.
368 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
369 (*it)->wait_for_init();
372 // create surface object. This depends on the ports being
373 // correctly initialised
374 initialize_surface();
375 connect_session_signals();
380 // send current control positions to surface
381 // must come after _active = true otherwise it won't run
389 catch (exception & e) {
390 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("set_active to false because exception caught: %1\n", e.what()));
400 MackieControlProtocol::handle_strip_button (SurfacePort & port, Control & control, ButtonState bs, boost::shared_ptr<Route> route)
406 if (control.name() == "recenable")
408 state = !route->record_enabled();
409 route->set_record_enabled (state, this);
411 else if (control.name() == "mute")
413 state = !route->muted();
414 route->set_mute (state, this);
416 else if (control.name() == "solo")
418 state = !route->soloed();
419 route->set_solo (state, this);
421 else if (control.name() == "select")
423 // TODO make the track selected. Whatever that means.
424 //state = default_button_press (dynamic_cast<Button&> (control));
426 else if (control.name() == "vselect")
428 // TODO could be used to select different things to apply the pot to?
429 //state = default_button_press (dynamic_cast<Button&> (control));
433 if (control.name() == "fader_touch")
436 control.strip().gain().set_in_use (state);
438 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
439 /* BCF faders don't support touch, so add a timeout to reset
440 their `in_use' state.
442 port.add_in_use_timeout (control.strip().gain(), &control.strip().fader_touch());
450 MackieControlProtocol::update_led (Mackie::Button & button, Mackie::LedState ls)
454 SurfacePort * port = 0;
455 if (button.group().is_strip())
457 if (button.group().is_master())
463 port = &port_for_id (dynamic_cast<const Strip&> (button.group()).index());
470 port->write (builder.build_led (button, ls));
475 MackieControlProtocol::update_timecode_beats_led()
477 switch (_timecode_type)
479 case ARDOUR::AnyTime::BBT:
480 update_global_led ("beats", on);
481 update_global_led ("timecode", off);
483 case ARDOUR::AnyTime::Timecode:
484 update_global_led ("timecode", on);
485 update_global_led ("beats", off);
489 os << "Unknown Anytime::Type " << _timecode_type;
490 throw runtime_error (os.str());
495 MackieControlProtocol::update_global_button (const string & name, LedState ls)
497 if (surface().controls_by_name.find (name) != surface().controls_by_name.end())
499 Button * button = dynamic_cast<Button*> (surface().controls_by_name[name]);
500 mcu_port().write (builder.build_led (button->led(), ls));
504 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Button %1 not found\n", name));
509 MackieControlProtocol::update_global_led (const string & name, LedState ls)
511 if (surface().controls_by_name.find (name) != surface().controls_by_name.end())
513 Led * led = dynamic_cast<Led*> (surface().controls_by_name[name]);
514 mcu_port().write (builder.build_led (*led, ls));
518 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Led %1 not found\n", name));
522 // send messages to surface to set controls to correct values
524 MackieControlProtocol::update_surface()
528 // do the initial bank switch to connect signals
529 // _current_initial_bank is initialised by set_state
530 switch_banks (_current_initial_bank);
532 // create a RouteSignal for the master route
534 boost::shared_ptr<Route> mr = master_route ();
536 master_route_signal = boost::shared_ptr<RouteSignal> (new RouteSignal (mr, *this, master_strip(), mcu_port()));
537 // update strip from route
538 master_route_signal->notify_all();
541 // sometimes the jog wheel is a pot
542 surface().blank_jog_ring (mcu_port(), builder);
544 // update global buttons and displays
545 notify_record_state_changed();
546 notify_transport_state_changed();
547 update_timecode_beats_led();
552 MackieControlProtocol::connect_session_signals()
554 // receive routes added
555 session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_route_added, this, _1), midi_ui_context());
556 // receive record state toggled
557 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_record_state_changed, this), midi_ui_context());
558 // receive transport state changed
559 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_transport_state_changed, this), midi_ui_context());
560 // receive punch-in and punch-out
561 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
562 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
563 // receive rude solo changed
564 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_solo_active_changed, this, _1), midi_ui_context());
566 // make sure remote id changed signals reach here
567 // see also notify_route_added
568 Sorted sorted = get_sorted_routes();
570 for (Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it) {
571 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind(&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
576 MackieControlProtocol::add_port (MIDI::Port & midi_input_port, MIDI::Port & midi_output_port, int number)
578 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("add port %1 %2\n", midi_input_port.name(), midi_output_port.name()));
580 MackiePort * sport = new MackiePort (*this, midi_input_port, midi_output_port, number);
581 _ports.push_back (sport);
583 sport->init_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_init, this, sport));
584 sport->active_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_active, this, sport));
585 sport->inactive_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_inactive, this, sport));
587 _input_bundle->add_channel (
588 midi_input_port.name(),
589 ARDOUR::DataType::MIDI,
590 session->engine().make_port_name_non_relative (midi_input_port.name())
593 _output_bundle->add_channel (
594 midi_output_port.name(),
595 ARDOUR::DataType::MIDI,
596 session->engine().make_port_name_non_relative (midi_output_port.name())
601 MackieControlProtocol::create_ports()
603 MIDI::Manager * mm = MIDI::Manager::instance();
604 MIDI::Port * midi_input_port = mm->add_port (
605 new MIDI::Port (string_compose (_("%1 in"), default_port_name), MIDI::Port::IsInput, session->engine().jack())
607 MIDI::Port * midi_output_port = mm->add_port (
608 new MIDI::Port (string_compose (_("%1 out"), default_port_name), MIDI::Port::IsOutput, session->engine().jack())
611 /* Create main port */
613 if (!midi_input_port->ok() || !midi_output_port->ok()) {
615 os << _("Mackie control MIDI ports could not be created; Mackie control disabled");
616 error << os.str() << endmsg;
617 throw MackieControlException (os.str());
620 add_port (*midi_input_port, *midi_output_port, 0);
622 /* Create extender ports */
624 for (uint32_t index = 1; index <= Config->get_mackie_extenders(); ++index) {
625 MIDI::Port * midi_input_port = mm->add_port (
626 new MIDI::Port (string_compose (_("mcu_xt_%1 in"), index), MIDI::Port::IsInput, session->engine().jack())
628 MIDI::Port * midi_output_port = mm->add_port (
629 new MIDI::Port (string_compose (_("mcu_xt_%1 out"), index), MIDI::Port::IsOutput, session->engine().jack())
631 if (midi_input_port->ok() && midi_output_port->ok()) {
632 add_port (*midi_input_port, *midi_output_port, index);
637 boost::shared_ptr<Route>
638 MackieControlProtocol::master_route()
640 return session->master_out ();
644 MackieControlProtocol::master_strip()
646 return dynamic_cast<Strip&> (*surface().groups["master"]);
650 MackieControlProtocol::initialize_surface()
652 // set up the route table
654 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
655 strips += (*it)->strips();
658 set_route_table_size (strips);
660 // TODO same as code in mackie_port.cc
661 string emulation = ARDOUR::Config->get_mackie_emulation();
662 if (emulation == "bcf")
664 _surface = new BcfSurface (strips);
666 else if (emulation == "mcu")
668 _surface = new MackieSurface (strips);
673 os << "no Surface class found for emulation: " << emulation;
674 throw MackieControlException (os.str());
679 // Connect events. Must be after route table otherwise there will be trouble
681 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
682 (*it)->control_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_control_event, this, _1, _2, _3));
687 MackieControlProtocol::close()
690 // must be before other shutdown otherwise polling loop
691 // calls methods on objects that are deleted
693 port_connections.drop_connections ();
694 session_connections.drop_connections ();
695 route_connections.drop_connections ();
698 // These will fail if the port has gone away.
699 // So catch the exception and do the rest of the
701 // because the bcf doesn't respond to the next 3 sysex messages
706 catch (exception & e) {
707 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
710 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
712 MackiePort & port = **it;
714 port.write_sysex (0x61);
716 port.write_sysex (0x62);
717 // Reset (reboot into offline mode)
718 port.write_sysex (0x63);
720 catch (exception & e) {
721 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
725 // disconnect routes from strips
726 clear_route_signals();
731 // shut down MackiePorts
732 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
740 MackieControlProtocol::get_state()
742 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state\n");
744 // add name of protocol
745 XMLNode* node = new XMLNode (X_("Protocol"));
746 node->add_property (X_("name"), _name);
750 os << _current_initial_bank;
751 node->add_property (X_("bank"), os.str());
757 MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
759 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::set_state: active %1\n", _active));
763 // fetch current bank
765 if (node.property (X_("bank")) != 0) {
766 string bank = node.property (X_("bank"))->value();
769 uint32_t new_bank = atoi (bank.c_str());
770 if (_current_initial_bank != new_bank) {
771 switch_banks (new_bank);
774 catch (exception & e) {
775 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("exception in MackieControlProtocol::set_state: %1\n", e.what()));
784 MackieControlProtocol::handle_control_event (SurfacePort & port, Control & control, const ControlState & state)
786 // find the route for the control, if there is one
787 boost::shared_ptr<Route> route;
788 if (control.group().is_strip()) {
789 if (control.group().is_master()) {
790 route = master_route();
792 uint32_t index = control.ordinal() - 1 + (port.number() * port.strips());
793 if (index < route_table.size())
794 route = route_table[index];
796 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
800 // This handles control element events from the surface
801 // the state of the controls on the surface is usually updated
803 switch (control.type()) {
804 case Control::type_fader:
805 // find the route in the route table for the id
806 // if the route isn't available, skip it
807 // at which point the fader should just reset itself
810 route->gain_control()->set_value (slider_position_to_gain (state.pos));
812 if (ARDOUR::Config->get_mackie_emulation() == "bcf") {
813 /* reset the timeout while we're still moving the fader */
814 port.add_in_use_timeout (control, control.in_use_touch_control);
817 // must echo bytes back to slider now, because
818 // the notifier only works if the fader is not being
819 // touched. Which it is if we're getting input.
820 port.write (builder.build_fader ((Fader&)control, state.pos));
824 case Control::type_button:
825 if (control.group().is_strip()) {
828 handle_strip_button (port, control, state.button_state, route);
830 // no route so always switch the light off
831 // because no signals will be emitted by a non-route
832 port.write (builder.build_led (control.led(), off));
834 } else if (control.group().is_master()) {
835 // master fader touch
837 handle_strip_button (port, control, state.button_state, route);
840 // handle all non-strip buttons
841 surface().handle_button (*this, state.button_state, dynamic_cast<Button&> (control));
845 // pot (jog wheel, external control)
846 case Control::type_pot:
847 if (control.group().is_strip()) {
849 boost::shared_ptr<Panner> panner = route->panner_shell()->panner();
850 // pan for mono input routes, or stereo linked panners
852 double p = panner->position ();
854 // calculate new value, and adjust
855 p += state.delta * state.sign;
858 panner->set_position (p);
863 // it's a pot for an umnapped route, so turn all the lights off
864 port.write (builder.build_led_ring (dynamic_cast<Pot &> (control), off));
869 if (control.is_jog())
871 _jog_wheel.jog_event (port, control, state);
875 cout << "external controller" << state.ticks * state.sign << endl;
881 cout << "Control::type not handled: " << control.type() << endl;
885 /////////////////////////////////////////////////
886 // handlers for Route signals
887 // TODO should these be part of RouteSignal?
888 // They started off as signal/slot handlers for signals
889 // from Route, but they're also used in polling for automation
890 /////////////////////////////////////////////////
893 MackieControlProtocol::notify_solo_changed (RouteSignal * route_signal)
897 Button & button = route_signal->strip().solo();
898 route_signal->port().write (builder.build_led (button, route_signal->route()->soloed()));
900 catch (exception & e)
902 cout << e.what() << endl;
907 MackieControlProtocol::notify_mute_changed (RouteSignal * route_signal)
911 Button & button = route_signal->strip().mute();
912 route_signal->port().write (builder.build_led (button, route_signal->route()->muted()));
914 catch (exception & e)
916 cout << e.what() << endl;
921 MackieControlProtocol::notify_record_enable_changed (RouteSignal * route_signal)
925 Button & button = route_signal->strip().recenable();
926 route_signal->port().write (builder.build_led (button, route_signal->route()->record_enabled()));
928 catch (exception & e)
930 cout << e.what() << endl;
934 void MackieControlProtocol::notify_active_changed (RouteSignal *)
938 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::notify_active_changed\n");
939 refresh_current_bank();
941 catch (exception & e)
943 cout << e.what() << endl;
948 MackieControlProtocol::notify_gain_changed (RouteSignal * route_signal, bool force_update)
952 Fader & fader = route_signal->strip().gain();
955 float gain_value = gain_to_slider_position (route_signal->route()->gain_control()->get_value());
956 // check that something has actually changed
957 if (force_update || gain_value != route_signal->last_gain_written())
959 route_signal->port().write (builder.build_fader (fader, gain_value));
960 route_signal->last_gain_written (gain_value);
964 catch (exception & e)
966 cout << e.what() << endl;
971 MackieControlProtocol::notify_property_changed (const PropertyChange& what_changed, RouteSignal * route_signal)
973 if (!what_changed.contains (Properties::name)) {
979 Strip & strip = route_signal->strip();
981 /* XXX: not sure about this check to only display stuff for strips of index < 8 */
982 if (!strip.is_master() && strip.index() < 8)
985 string fullname = route_signal->route()->name();
987 if (fullname.length() <= 6)
993 line1 = PBD::short_version (fullname, 6);
996 SurfacePort & port = route_signal->port();
997 port.write (builder.strip_display (port, strip, 0, line1));
998 port.write (builder.strip_display_blank (port, strip, 1));
1001 catch (exception & e)
1003 cout << e.what() << endl;
1008 MackieControlProtocol::notify_panner_changed (RouteSignal * route_signal, bool force_update)
1012 Pot & pot = route_signal->strip().vpot();
1013 boost::shared_ptr<Panner> panner = route_signal->route()->panner();
1015 double pos = panner->position ();
1017 // cache the MidiByteArray here, because the mackie led control is much lower
1018 // resolution than the panner control. So we save lots of byte
1019 // sends in spite of more work on the comparison
1020 MidiByteArray bytes = builder.build_led_ring (pot, ControlState (on, pos), MackieMidiBuilder::midi_pot_mode_dot);
1021 // check that something has actually changed
1022 if (force_update || bytes != route_signal->last_pan_written())
1024 route_signal->port().write (bytes);
1025 route_signal->last_pan_written (bytes);
1030 route_signal->port().write (builder.zero_control (pot));
1033 catch (exception & e)
1035 cout << e.what() << endl;
1039 // TODO handle plugin automation polling
1041 MackieControlProtocol::update_automation (RouteSignal & rs)
1043 ARDOUR::AutoState gain_state = rs.route()->gain_control()->automation_state();
1045 if (gain_state == Touch || gain_state == Play)
1047 notify_gain_changed (&rs, false);
1050 if (rs.route()->panner()) {
1051 ARDOUR::AutoState panner_state = rs.route()->panner()->automation_state();
1052 if (panner_state == Touch || panner_state == Play)
1054 notify_panner_changed (&rs, false);
1060 MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
1062 Timecode::BBT_Time bbt_time;
1063 session->bbt_time (now_frame, bbt_time);
1065 // According to the Logic docs
1066 // digits: 888/88/88/888
1067 // BBT mode: Bars/Beats/Subdivisions/Ticks
1069 os << setw(3) << setfill('0') << bbt_time.bars;
1070 os << setw(2) << setfill('0') << bbt_time.beats;
1072 // figure out subdivisions per beat
1073 const Meter & meter = session->tempo_map().meter_at (now_frame);
1075 if (meter.note_divisor() == 8 && (meter.beats_per_bar() == 12.0 || meter.beats_per_bar() == 9.0 || meter.beats_per_bar() == 6.0))
1080 uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
1081 uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
1083 os << setw(2) << setfill('0') << subdivisions + 1;
1084 os << setw(3) << setfill('0') << ticks;
1090 MackieControlProtocol::format_timecode_timecode (framepos_t now_frame)
1092 Timecode::Time timecode;
1093 session->timecode_time (now_frame, timecode);
1095 // According to the Logic docs
1096 // digits: 888/88/88/888
1097 // Timecode mode: Hours/Minutes/Seconds/Frames
1099 os << setw(3) << setfill('0') << timecode.hours;
1100 os << setw(2) << setfill('0') << timecode.minutes;
1101 os << setw(2) << setfill('0') << timecode.seconds;
1102 os << setw(3) << setfill('0') << timecode.frames;
1108 MackieControlProtocol::update_timecode_display()
1110 if (surface().has_timecode_display())
1112 // do assignment here so current_frame is fixed
1113 framepos_t current_frame = session->transport_frame();
1116 switch (_timecode_type)
1118 case ARDOUR::AnyTime::BBT:
1119 timecode = format_bbt_timecode (current_frame);
1121 case ARDOUR::AnyTime::Timecode:
1122 timecode = format_timecode_timecode (current_frame);
1126 os << "Unknown timecode: " << _timecode_type;
1127 throw runtime_error (os.str());
1130 // only write the timecode string to the MCU if it's changed
1131 // since last time. This is to reduce midi bandwidth used.
1132 if (timecode != _timecode_last)
1134 surface().display_timecode (mcu_port(), builder, timecode, _timecode_last);
1135 _timecode_last = timecode;
1141 MackieControlProtocol::poll_session_data()
1143 // XXX need to attach this to a timer in the MIDI UI event loop (20msec)
1146 // do all currently mapped routes
1147 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
1148 update_automation (**it);
1151 // and the master strip
1152 if (master_route_signal != 0) {
1153 update_automation (*master_route_signal);
1156 update_timecode_display();
1160 /////////////////////////////////////
1161 // Transport Buttons
1162 /////////////////////////////////////
1165 MackieControlProtocol::frm_left_press (Button &)
1167 // can use first_mark_before/after as well
1168 unsigned long elapsed = _frm_left_last.restart();
1170 Location * loc = session->locations()->first_location_before (
1171 session->transport_frame()
1174 // allow a quick double to go past a previous mark
1175 if (session->transport_rolling() && elapsed < 500 && loc != 0)
1177 Location * loc_two_back = session->locations()->first_location_before (loc->start());
1178 if (loc_two_back != 0)
1184 // move to the location, if it's valid
1187 session->request_locate (loc->start(), session->transport_rolling());
1194 MackieControlProtocol::frm_left_release (Button &)
1200 MackieControlProtocol::frm_right_press (Button &)
1202 // can use first_mark_before/after as well
1203 Location * loc = session->locations()->first_location_after (
1204 session->transport_frame()
1206 if (loc != 0) session->request_locate (loc->start(), session->transport_rolling());
1211 MackieControlProtocol::frm_right_release (Button &)
1217 MackieControlProtocol::stop_press (Button &)
1219 session->request_stop();
1224 MackieControlProtocol::stop_release (Button &)
1226 return session->transport_stopped();
1230 MackieControlProtocol::play_press (Button &)
1232 session->request_transport_speed (1.0);
1237 MackieControlProtocol::play_release (Button &)
1239 return session->transport_rolling();
1243 MackieControlProtocol::record_press (Button &)
1245 if (session->get_record_enabled()) {
1246 session->disable_record (false);
1248 session->maybe_enable_record();
1254 MackieControlProtocol::record_release (Button &)
1256 if (session->get_record_enabled()) {
1257 if (session->transport_rolling()) {
1268 MackieControlProtocol::rewind_press (Button &)
1270 _jog_wheel.push (JogWheel::speed);
1271 _jog_wheel.transport_direction (-1);
1272 session->request_transport_speed (-_jog_wheel.transport_speed());
1277 MackieControlProtocol::rewind_release (Button &)
1280 _jog_wheel.transport_direction (0);
1281 if (_transport_previously_rolling)
1282 session->request_transport_speed (1.0);
1284 session->request_stop();
1289 MackieControlProtocol::ffwd_press (Button &)
1291 _jog_wheel.push (JogWheel::speed);
1292 _jog_wheel.transport_direction (1);
1293 session->request_transport_speed (_jog_wheel.transport_speed());
1298 MackieControlProtocol::ffwd_release (Button &)
1301 _jog_wheel.transport_direction (0);
1302 if (_transport_previously_rolling)
1303 session->request_transport_speed (1.0);
1305 session->request_stop();
1310 MackieControlProtocol::loop_press (Button &)
1312 session->request_play_loop (!session->get_play_loop());
1317 MackieControlProtocol::loop_release (Button &)
1319 return session->get_play_loop();
1323 MackieControlProtocol::punch_in_press (Button &)
1325 bool state = !session->config.get_punch_in();
1326 session->config.set_punch_in (state);
1331 MackieControlProtocol::punch_in_release (Button &)
1333 return session->config.get_punch_in();
1337 MackieControlProtocol::punch_out_press (Button &)
1339 bool state = !session->config.get_punch_out();
1340 session->config.set_punch_out (state);
1345 MackieControlProtocol::punch_out_release (Button &)
1347 return session->config.get_punch_out();
1351 MackieControlProtocol::home_press (Button &)
1353 session->goto_start();
1358 MackieControlProtocol::home_release (Button &)
1364 MackieControlProtocol::end_press (Button &)
1366 session->goto_end();
1371 MackieControlProtocol::end_release (Button &)
1377 MackieControlProtocol::clicking_press (Button &)
1379 bool state = !Config->get_clicking();
1380 Config->set_clicking (state);
1385 MackieControlProtocol::clicking_release (Button &)
1387 return Config->get_clicking();
1390 LedState MackieControlProtocol::global_solo_press (Button &)
1392 bool state = !session->soloing();
1393 session->set_solo (session->get_routes(), state);
1397 LedState MackieControlProtocol::global_solo_release (Button &)
1399 return session->soloing();
1402 ///////////////////////////////////////////
1404 ///////////////////////////////////////////
1406 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
1408 if (p == "punch-in")
1410 update_global_button ("punch_in", session->config.get_punch_in());
1412 else if (p == "punch-out")
1414 update_global_button ("punch_out", session->config.get_punch_out());
1416 else if (p == "clicking")
1418 update_global_button ("clicking", Config->get_clicking());
1422 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("parameter changed: %1\n", p));
1426 // RouteList is the set of routes that have just been added
1428 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
1430 // currently assigned banks are less than the full set of
1431 // strips, so activate the new strip now.
1432 if (route_signals.size() < route_table.size())
1434 refresh_current_bank();
1436 // otherwise route added, but current bank needs no updating
1438 // make sure remote id changes in the new route are handled
1439 typedef ARDOUR::RouteList ARS;
1441 for (ARS::iterator it = rl.begin(); it != rl.end(); ++it) {
1442 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
1447 MackieControlProtocol::notify_solo_active_changed (bool active)
1449 Button * rude_solo = reinterpret_cast<Button*> (surface().controls_by_name["solo"]);
1450 mcu_port().write (builder.build_led (*rude_solo, active ? flashing : off));
1454 MackieControlProtocol::notify_remote_id_changed()
1456 Sorted sorted = get_sorted_routes();
1458 // if a remote id has been moved off the end, we need to shift
1459 // the current bank backwards.
1460 if (sorted.size() - _current_initial_bank < route_signals.size())
1462 // but don't shift backwards past the zeroth channel
1463 switch_banks (max((Sorted::size_type) 0, sorted.size() - route_signals.size()));
1465 // Otherwise just refresh the current bank
1468 refresh_current_bank();
1472 ///////////////////////////////////////////
1473 // Transport signals
1474 ///////////////////////////////////////////
1477 MackieControlProtocol::notify_record_state_changed()
1479 // switch rec button on / off / flashing
1480 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1481 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1485 MackieControlProtocol::notify_transport_state_changed()
1487 // switch various play and stop buttons on / off
1488 update_global_button ("play", session->transport_rolling());
1489 update_global_button ("stop", !session->transport_rolling());
1490 update_global_button ("loop", session->get_play_loop());
1492 _transport_previously_rolling = session->transport_rolling();
1494 // rec is special because it's tristate
1495 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1496 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1499 /////////////////////////////////////
1501 /////////////////////////////////////
1503 MackieControlProtocol::left_press (Button &)
1505 Sorted sorted = get_sorted_routes();
1506 if (sorted.size() > route_table.size())
1508 int new_initial = _current_initial_bank - route_table.size();
1509 if (new_initial < 0) {
1513 if (new_initial != int (_current_initial_bank)) {
1514 session->set_dirty();
1515 switch_banks (new_initial);
1527 MackieControlProtocol::left_release (Button &)
1533 MackieControlProtocol::right_press (Button &)
1535 Sorted sorted = get_sorted_routes();
1536 if (sorted.size() > route_table.size()) {
1537 uint32_t delta = sorted.size() - (route_table.size() + _current_initial_bank);
1539 if (delta > route_table.size()) {
1540 delta = route_table.size();
1544 session->set_dirty();
1545 switch_banks (_current_initial_bank + delta);
1555 MackieControlProtocol::right_release (Button &)
1561 MackieControlProtocol::channel_left_press (Button &)
1563 Sorted sorted = get_sorted_routes();
1564 if (sorted.size() > route_table.size())
1576 MackieControlProtocol::channel_left_release (Button &)
1582 MackieControlProtocol::channel_right_press (Button &)
1584 Sorted sorted = get_sorted_routes();
1585 if (sorted.size() > route_table.size())
1597 MackieControlProtocol::channel_right_release (Button &)
1602 /////////////////////////////////////
1604 /////////////////////////////////////
1606 MackieControlProtocol::marker_press (Button &)
1608 // cut'n'paste from LocationUI::add_new_location()
1610 framepos_t where = session->audible_frame();
1611 session->locations()->next_available_name(markername,"mcu");
1612 Location *location = new Location (*session, where, where, markername, Location::IsMark);
1613 session->begin_reversible_command (_("add marker"));
1614 XMLNode &before = session->locations()->get_state();
1615 session->locations()->add (location, true);
1616 XMLNode &after = session->locations()->get_state();
1617 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1618 session->commit_reversible_command ();
1623 MackieControlProtocol::marker_release (Button &)
1629 jog_wheel_state_display (JogWheel::State state, SurfacePort & port)
1633 case JogWheel::zoom: port.write (builder.two_char_display ("Zm")); break;
1634 case JogWheel::scroll: port.write (builder.two_char_display ("Sc")); break;
1635 case JogWheel::scrub: port.write (builder.two_char_display ("Sb")); break;
1636 case JogWheel::shuttle: port.write (builder.two_char_display ("Sh")); break;
1637 case JogWheel::speed: port.write (builder.two_char_display ("Sp")); break;
1638 case JogWheel::select: port.write (builder.two_char_display ("Se")); break;
1643 MackieControlProtocol::zoom_press (Mackie::Button &)
1645 _jog_wheel.zoom_state_toggle();
1646 update_global_button ("scrub", _jog_wheel.jog_wheel_state() == JogWheel::scrub);
1647 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1648 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1652 MackieControlProtocol::zoom_release (Mackie::Button &)
1654 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1658 MackieControlProtocol::scrub_press (Mackie::Button &)
1660 _jog_wheel.scrub_state_cycle();
1661 update_global_button ("zoom", _jog_wheel.jog_wheel_state() == JogWheel::zoom);
1662 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1664 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1666 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1671 MackieControlProtocol::scrub_release (Mackie::Button &)
1674 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1676 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1681 MackieControlProtocol::drop_press (Button &)
1683 session->remove_last_capture();
1688 MackieControlProtocol::drop_release (Button &)
1694 MackieControlProtocol::save_press (Button &)
1696 session->save_state ("");
1701 MackieControlProtocol::save_release (Button &)
1707 MackieControlProtocol::timecode_beats_press (Button &)
1709 switch (_timecode_type)
1711 case ARDOUR::AnyTime::BBT:
1712 _timecode_type = ARDOUR::AnyTime::Timecode;
1714 case ARDOUR::AnyTime::Timecode:
1715 _timecode_type = ARDOUR::AnyTime::BBT;
1719 os << "Unknown Anytime::Type " << _timecode_type;
1720 throw runtime_error (os.str());
1722 update_timecode_beats_led();
1727 MackieControlProtocol::timecode_beats_release (Button &)
1732 list<boost::shared_ptr<ARDOUR::Bundle> >
1733 MackieControlProtocol::bundles ()
1735 list<boost::shared_ptr<ARDOUR::Bundle> > b;
1736 b.push_back (_input_bundle);
1737 b.push_back (_output_bundle);