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");
90 AudioEngine::instance()->PortConnectedOrDisconnected.connect (
91 audio_engine_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::port_connected_or_disconnected, this, _2, _4, _5),
96 MackieControlProtocol::~MackieControlProtocol()
98 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol\n");
103 catch (exception & e) {
104 cout << "~MackieControlProtocol caught " << e.what() << endl;
107 cout << "~MackieControlProtocol caught unknown" << endl;
110 DEBUG_TRACE (DEBUG::MackieControl, "finished ~MackieControlProtocol::MackieControlProtocol\n");
114 MackieControlProtocol::surface()
117 throw MackieControlException ("_surface is 0 in MackieControlProtocol::surface");
122 const Mackie::SurfacePort&
123 MackieControlProtocol::mcu_port() const
125 if (_ports.size() < 1) {
128 return dynamic_cast<const MackiePort &> (*_ports[0]);
133 MackieControlProtocol::mcu_port()
135 if (_ports.size() < 1) {
138 return dynamic_cast<MackiePort &> (*_ports[0]);
142 // go to the previous track.
143 // Assume that get_sorted_routes().size() > route_table.size()
145 MackieControlProtocol::prev_track()
147 if (_current_initial_bank >= 1) {
148 session->set_dirty();
149 switch_banks (_current_initial_bank - 1);
153 // go to the next track.
154 // Assume that get_sorted_routes().size() > route_table.size()
156 MackieControlProtocol::next_track()
158 Sorted sorted = get_sorted_routes();
159 if (_current_initial_bank + route_table.size() < sorted.size()) {
160 session->set_dirty();
161 switch_banks (_current_initial_bank + 1);
166 MackieControlProtocol::clear_route_signals()
168 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
171 route_signals.clear();
174 // return the port for a given id - 0 based
175 // throws an exception if no port found
177 MackieControlProtocol::port_for_id (uint32_t index)
179 uint32_t current_max = 0;
180 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
181 current_max += (*it)->strips();
182 if (index < current_max) return **it;
185 // oops - no matching port
187 os << "No port for index " << index;
188 throw MackieControlException (os.str());
191 // predicate for sort call in get_sorted_routes
192 struct RouteByRemoteId
194 bool operator () (const boost::shared_ptr<Route> & a, const boost::shared_ptr<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();
204 bool operator () (const Route * a, const Route * b) const
206 return a->remote_control_id() < b->remote_control_id();
210 MackieControlProtocol::Sorted
211 MackieControlProtocol::get_sorted_routes()
216 boost::shared_ptr<RouteList> routes = session->get_routes();
217 set<uint32_t> remote_ids;
219 // routes with remote_id 0 should never be added
220 // TODO verify this with ardour devs
221 // remote_ids.insert (0);
223 // sort in remote_id order, and exclude master, control and hidden routes
224 // and any routes that are already set.
225 for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it) {
226 Route & route = **it;
229 && !route.is_master()
230 && !route.is_hidden()
231 && !route.is_monitor()
232 && remote_ids.find (route.remote_control_id()) == remote_ids.end()
234 sorted.push_back (*it);
235 remote_ids.insert (route.remote_control_id());
238 sort (sorted.begin(), sorted.end(), RouteByRemoteId());
243 MackieControlProtocol::refresh_current_bank()
245 switch_banks (_current_initial_bank);
249 MackieControlProtocol::switch_banks (int initial)
251 // DON'T prevent bank switch if initial == _current_initial_bank
252 // because then this method can't be used as a refresh
255 Sorted sorted = get_sorted_routes();
256 int delta = sorted.size() - route_table.size();
257 if (initial < 0 || (delta > 0 && initial > delta)) {
258 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("not switching to %1\n", initial));
261 _current_initial_bank = initial;
263 // first clear the signals from old routes
264 // taken care of by the RouteSignal destructors
265 clear_route_signals();
267 // now set the signals for new routes
268 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 // clear out routes from our table in case any have been deleted
277 for (vector<boost::shared_ptr<Route> >::iterator i = route_table.begin(); i != route_table.end(); ++i) {
281 // link routes to strips
283 for (; it != end && it != sorted.end(); ++it, ++i) {
284 boost::shared_ptr<Route> route = *it;
286 assert (surface().strips[i]);
287 Strip & strip = *surface().strips[i];
289 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("remote id %1 connecting %2 to %3 with port %4\n",
290 route->remote_control_id(), route->name(), strip.name(), port_for_id(i)));
291 route_table[i] = route;
292 RouteSignal * rs = new RouteSignal (route, *this, strip, port_for_id(i));
293 route_signals.push_back (rs);
294 // update strip from route
298 // create dead strips if there aren't enough routes to
300 for (; i < route_table.size(); ++i) {
301 Strip & strip = *surface().strips[i];
302 // send zero for this strip
303 MackiePort & port = port_for_id(i);
304 port.write (builder.zero_strip (port, strip));
308 // display the current start bank.
309 surface().display_bank_start (mcu_port(), builder, _current_initial_bank);
313 MackieControlProtocol::zero_all()
315 // TODO turn off Timecode displays
318 for (Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it) {
319 MackiePort & port = port_for_id ((*it)->index());
320 port.write (builder.zero_strip (port, **it));
323 // and the master strip
324 mcu_port().write (builder.zero_strip (dynamic_cast<MackiePort&> (mcu_port()), master_strip()));
326 // turn off global buttons and leds
327 // global buttons are only ever on mcu_port, so we don't have
328 // to figure out which port.
329 for (Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it) {
330 Control & control = **it;
331 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)
349 // the reason for the locking and unlocking is that
350 // glibmm can't do a condition wait on a RecMutex
352 // TODO what happens if this fails half way?
354 // create MackiePorts
356 Glib::Mutex::Lock lock (update_mutex);
360 // now initialise MackiePorts - ie exchange sysex messages
361 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
365 // wait until all ports are active
366 // TODO a more sophisticated approach would
367 // allow things to start up with only an MCU, even if
368 // extenders were specified but not responding.
369 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
370 (*it)->wait_for_init();
373 // create surface object. This depends on the ports being
374 // correctly initialised
375 initialize_surface();
376 connect_session_signals();
381 // send current control positions to surface
382 // must come after _active = true otherwise it won't run
390 catch (exception & e) {
391 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)
405 if (control.name() == "recenable") {
406 state = !route->record_enabled();
407 route->set_record_enabled (state, this);
408 } else if (control.name() == "mute") {
409 state = !route->muted();
410 route->set_mute (state, this);
411 } else if (control.name() == "solo") {
412 state = !route->soloed();
413 route->set_solo (state, this);
414 } else if (control.name() == "select") {
415 // TODO make the track selected. Whatever that means.
416 //state = default_button_press (dynamic_cast<Button&> (control));
417 } else if (control.name() == "vselect") {
418 // TODO could be used to select different things to apply the pot to?
419 //state = default_button_press (dynamic_cast<Button&> (control));
423 if (control.name() == "fader_touch") {
425 control.strip().gain().set_in_use (state);
427 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
428 /* BCF faders don't support touch, so add a timeout to reset
429 their `in_use' state.
431 port.add_in_use_timeout (control.strip().gain(), &control.strip().fader_touch());
439 MackieControlProtocol::update_led (Mackie::Button & button, Mackie::LedState ls)
442 SurfacePort * port = 0;
443 if (button.group().is_strip()) {
444 if (button.group().is_master()) {
447 port = &port_for_id (dynamic_cast<const Strip&> (button.group()).index());
452 port->write (builder.build_led (button, ls));
457 MackieControlProtocol::update_timecode_beats_led()
459 switch (_timecode_type) {
460 case ARDOUR::AnyTime::BBT:
461 update_global_led ("beats", on);
462 update_global_led ("timecode", off);
464 case ARDOUR::AnyTime::Timecode:
465 update_global_led ("timecode", on);
466 update_global_led ("beats", off);
470 os << "Unknown Anytime::Type " << _timecode_type;
471 throw runtime_error (os.str());
476 MackieControlProtocol::update_global_button (const string & name, LedState ls)
478 if (surface().controls_by_name.find (name) != surface().controls_by_name.end()) {
479 Button * button = dynamic_cast<Button*> (surface().controls_by_name[name]);
480 mcu_port().write (builder.build_led (button->led(), ls));
482 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Button %1 not found\n", name));
487 MackieControlProtocol::update_global_led (const string & name, LedState ls)
489 if (surface().controls_by_name.find (name) != surface().controls_by_name.end()) {
490 Led * led = dynamic_cast<Led*> (surface().controls_by_name[name]);
491 mcu_port().write (builder.build_led (*led, ls));
493 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Led %1 not found\n", name));
497 // send messages to surface to set controls to correct values
499 MackieControlProtocol::update_surface()
505 // do the initial bank switch to connect signals
506 // _current_initial_bank is initialised by set_state
507 switch_banks (_current_initial_bank);
509 /* Create a RouteSignal for the master route, if we don't already have one */
510 if (!master_route_signal) {
511 boost::shared_ptr<Route> mr = master_route ();
513 master_route_signal = boost::shared_ptr<RouteSignal> (new RouteSignal (mr, *this, master_strip(), mcu_port()));
514 // update strip from route
515 master_route_signal->notify_all();
519 // sometimes the jog wheel is a pot
520 surface().blank_jog_ring (mcu_port(), builder);
522 // update global buttons and displays
523 notify_record_state_changed();
524 notify_transport_state_changed();
525 update_timecode_beats_led();
529 MackieControlProtocol::connect_session_signals()
531 // receive routes added
532 session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_route_added, this, _1), midi_ui_context());
533 // receive record state toggled
534 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_record_state_changed, this), midi_ui_context());
535 // receive transport state changed
536 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_transport_state_changed, this), midi_ui_context());
537 // receive punch-in and punch-out
538 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
539 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
540 // receive rude solo changed
541 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_solo_active_changed, this, _1), midi_ui_context());
543 // make sure remote id changed signals reach here
544 // see also notify_route_added
545 Sorted sorted = get_sorted_routes();
547 for (Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it) {
548 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind(&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
553 MackieControlProtocol::add_port (MIDI::Port & midi_input_port, MIDI::Port & midi_output_port, int number)
555 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("add port %1 %2\n", midi_input_port.name(), midi_output_port.name()));
557 MackiePort * sport = new MackiePort (*this, midi_input_port, midi_output_port, number);
558 _ports.push_back (sport);
560 sport->init_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_init, this, sport));
561 sport->active_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_active, this, sport));
562 sport->inactive_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_inactive, this, sport));
564 _input_bundle->add_channel (
565 midi_input_port.name(),
566 ARDOUR::DataType::MIDI,
567 session->engine().make_port_name_non_relative (midi_input_port.name())
570 _output_bundle->add_channel (
571 midi_output_port.name(),
572 ARDOUR::DataType::MIDI,
573 session->engine().make_port_name_non_relative (midi_output_port.name())
578 MackieControlProtocol::create_ports()
580 MIDI::Manager * mm = MIDI::Manager::instance();
581 MIDI::Port * midi_input_port = mm->add_port (
582 new MIDI::Port (string_compose (_("%1 in"), default_port_name), MIDI::Port::IsInput, session->engine().jack())
584 MIDI::Port * midi_output_port = mm->add_port (
585 new MIDI::Port (string_compose (_("%1 out"), default_port_name), MIDI::Port::IsOutput, session->engine().jack())
588 /* Create main port */
590 if (!midi_input_port->ok() || !midi_output_port->ok()) {
592 os << _("Mackie control MIDI ports could not be created; Mackie control disabled");
593 error << os.str() << endmsg;
594 throw MackieControlException (os.str());
597 add_port (*midi_input_port, *midi_output_port, 0);
599 /* Create extender ports */
601 for (uint32_t index = 1; index <= Config->get_mackie_extenders(); ++index) {
602 MIDI::Port * midi_input_port = mm->add_port (
603 new MIDI::Port (string_compose (_("mcu_xt_%1 in"), index), MIDI::Port::IsInput, session->engine().jack())
605 MIDI::Port * midi_output_port = mm->add_port (
606 new MIDI::Port (string_compose (_("mcu_xt_%1 out"), index), MIDI::Port::IsOutput, session->engine().jack())
608 if (midi_input_port->ok() && midi_output_port->ok()) {
609 add_port (*midi_input_port, *midi_output_port, index);
614 boost::shared_ptr<Route>
615 MackieControlProtocol::master_route()
617 return session->master_out ();
621 MackieControlProtocol::master_strip()
623 return dynamic_cast<Strip&> (*surface().groups["master"]);
627 MackieControlProtocol::initialize_surface()
629 // set up the route table
631 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
632 strips += (*it)->strips();
635 set_route_table_size (strips);
637 // TODO same as code in mackie_port.cc
638 string emulation = ARDOUR::Config->get_mackie_emulation();
639 if (emulation == "bcf") {
640 _surface = new BcfSurface (strips);
641 } else if (emulation == "mcu") {
642 _surface = new MackieSurface (strips);
645 os << "no Surface class found for emulation: " << emulation;
646 throw MackieControlException (os.str());
651 // Connect events. Must be after route table otherwise there will be trouble
653 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
654 (*it)->control_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_control_event, this, _1, _2, _3));
659 MackieControlProtocol::close()
662 // must be before other shutdown otherwise polling loop
663 // calls methods on objects that are deleted
665 port_connections.drop_connections ();
666 session_connections.drop_connections ();
667 route_connections.drop_connections ();
670 // These will fail if the port has gone away.
671 // So catch the exception and do the rest of the
673 // because the bcf doesn't respond to the next 3 sysex messages
678 catch (exception & e) {
679 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
682 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
684 MackiePort & port = **it;
686 port.write_sysex (0x61);
688 port.write_sysex (0x62);
689 // Reset (reboot into offline mode)
690 port.write_sysex (0x63);
692 catch (exception & e) {
693 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
697 // disconnect routes from strips
698 clear_route_signals();
703 // shut down MackiePorts
704 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
712 MackieControlProtocol::get_state()
714 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state\n");
716 // add name of protocol
717 XMLNode* node = new XMLNode (X_("Protocol"));
718 node->add_property (X_("name"), _name);
722 os << _current_initial_bank;
723 node->add_property (X_("bank"), os.str());
729 MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
731 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::set_state: active %1\n", _active));
735 // fetch current bank
737 if (node.property (X_("bank")) != 0) {
738 string bank = node.property (X_("bank"))->value();
741 uint32_t new_bank = atoi (bank.c_str());
742 if (_current_initial_bank != new_bank) {
743 switch_banks (new_bank);
746 catch (exception & e) {
747 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("exception in MackieControlProtocol::set_state: %1\n", e.what()));
756 MackieControlProtocol::handle_control_event (SurfacePort & port, Control & control, const ControlState & state)
758 // find the route for the control, if there is one
759 boost::shared_ptr<Route> route;
760 if (control.group().is_strip()) {
761 if (control.group().is_master()) {
762 route = master_route();
764 uint32_t index = control.ordinal() - 1 + (port.number() * port.strips());
765 if (index < route_table.size())
766 route = route_table[index];
768 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
772 // This handles control element events from the surface
773 // the state of the controls on the surface is usually updated
775 switch (control.type()) {
776 case Control::type_fader:
777 // find the route in the route table for the id
778 // if the route isn't available, skip it
779 // at which point the fader should just reset itself
782 route->gain_control()->set_value (slider_position_to_gain (state.pos));
784 if (ARDOUR::Config->get_mackie_emulation() == "bcf") {
785 /* reset the timeout while we're still moving the fader */
786 port.add_in_use_timeout (control, control.in_use_touch_control);
789 // must echo bytes back to slider now, because
790 // the notifier only works if the fader is not being
791 // touched. Which it is if we're getting input.
792 port.write (builder.build_fader ((Fader&)control, state.pos));
796 case Control::type_button:
797 if (control.group().is_strip()) {
800 handle_strip_button (port, control, state.button_state, route);
802 // no route so always switch the light off
803 // because no signals will be emitted by a non-route
804 port.write (builder.build_led (control.led(), off));
806 } else if (control.group().is_master()) {
807 // master fader touch
809 handle_strip_button (port, control, state.button_state, route);
812 // handle all non-strip buttons
813 surface().handle_button (*this, state.button_state, dynamic_cast<Button&> (control));
817 // pot (jog wheel, external control)
818 case Control::type_pot:
819 if (control.group().is_strip()) {
821 boost::shared_ptr<Panner> panner = route->panner_shell()->panner();
822 // pan for mono input routes, or stereo linked panners
824 double p = panner->position ();
826 // calculate new value, and adjust
827 p += state.delta * state.sign;
830 panner->set_position (p);
833 // it's a pot for an umnapped route, so turn all the lights off
834 port.write (builder.build_led_ring (dynamic_cast<Pot &> (control), off));
839 if (control.is_jog()) {
840 _jog_wheel.jog_event (port, control, state);
842 cout << "external controller" << state.ticks * state.sign << endl;
848 cout << "Control::type not handled: " << control.type() << endl;
852 /////////////////////////////////////////////////
853 // handlers for Route signals
854 // TODO should these be part of RouteSignal?
855 // They started off as signal/slot handlers for signals
856 // from Route, but they're also used in polling for automation
857 /////////////////////////////////////////////////
860 MackieControlProtocol::notify_solo_changed (RouteSignal * route_signal)
863 Button & button = route_signal->strip().solo();
864 route_signal->port().write (builder.build_led (button, route_signal->route()->soloed()));
866 catch (exception & e) {
867 cout << e.what() << endl;
872 MackieControlProtocol::notify_mute_changed (RouteSignal * route_signal)
875 Button & button = route_signal->strip().mute();
876 route_signal->port().write (builder.build_led (button, route_signal->route()->muted()));
878 catch (exception & e) {
879 cout << e.what() << endl;
884 MackieControlProtocol::notify_record_enable_changed (RouteSignal * route_signal)
887 Button & button = route_signal->strip().recenable();
888 route_signal->port().write (builder.build_led (button, route_signal->route()->record_enabled()));
890 catch (exception & e) {
891 cout << e.what() << endl;
895 void MackieControlProtocol::notify_active_changed (RouteSignal *)
898 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::notify_active_changed\n");
899 refresh_current_bank();
901 catch (exception & e) {
902 cout << e.what() << endl;
907 MackieControlProtocol::notify_gain_changed (RouteSignal * route_signal, bool force_update)
910 Fader & fader = route_signal->strip().gain();
911 if (!fader.in_use()) {
912 float gain_value = gain_to_slider_position (route_signal->route()->gain_control()->get_value());
913 // check that something has actually changed
914 if (force_update || gain_value != route_signal->last_gain_written()) {
915 route_signal->port().write (builder.build_fader (fader, gain_value));
916 route_signal->last_gain_written (gain_value);
920 catch (exception & e) {
921 cout << e.what() << endl;
926 MackieControlProtocol::notify_property_changed (const PropertyChange& what_changed, RouteSignal * route_signal)
928 if (!what_changed.contains (Properties::name)) {
933 Strip & strip = route_signal->strip();
935 /* XXX: not sure about this check to only display stuff for strips of index < 8 */
936 if (!strip.is_master() && strip.index() < 8) {
938 string fullname = route_signal->route()->name();
940 if (fullname.length() <= 6) {
943 line1 = PBD::short_version (fullname, 6);
946 SurfacePort & port = route_signal->port();
947 port.write (builder.strip_display (port, strip, 0, line1));
948 port.write (builder.strip_display_blank (port, strip, 1));
951 catch (exception & e) {
952 cout << e.what() << endl;
957 MackieControlProtocol::notify_panner_changed (RouteSignal * route_signal, bool force_update)
960 Pot & pot = route_signal->strip().vpot();
961 boost::shared_ptr<Panner> panner = route_signal->route()->panner();
963 double pos = panner->position ();
965 // cache the MidiByteArray here, because the mackie led control is much lower
966 // resolution than the panner control. So we save lots of byte
967 // sends in spite of more work on the comparison
968 MidiByteArray bytes = builder.build_led_ring (pot, ControlState (on, pos), MackieMidiBuilder::midi_pot_mode_dot);
969 // check that something has actually changed
970 if (force_update || bytes != route_signal->last_pan_written())
972 route_signal->port().write (bytes);
973 route_signal->last_pan_written (bytes);
976 route_signal->port().write (builder.zero_control (pot));
979 catch (exception & e) {
980 cout << e.what() << endl;
984 // TODO handle plugin automation polling
986 MackieControlProtocol::update_automation (RouteSignal & rs)
988 ARDOUR::AutoState gain_state = rs.route()->gain_control()->automation_state();
990 if (gain_state == Touch || gain_state == Play) {
991 notify_gain_changed (&rs, false);
994 if (rs.route()->panner()) {
995 ARDOUR::AutoState panner_state = rs.route()->panner()->automation_state();
996 if (panner_state == Touch || panner_state == Play) {
997 notify_panner_changed (&rs, false);
1003 MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
1005 Timecode::BBT_Time bbt_time;
1006 session->bbt_time (now_frame, bbt_time);
1008 // According to the Logic docs
1009 // digits: 888/88/88/888
1010 // BBT mode: Bars/Beats/Subdivisions/Ticks
1012 os << setw(3) << setfill('0') << bbt_time.bars;
1013 os << setw(2) << setfill('0') << bbt_time.beats;
1015 // figure out subdivisions per beat
1016 const Meter & meter = session->tempo_map().meter_at (now_frame);
1018 if (meter.note_divisor() == 8 && (meter.beats_per_bar() == 12.0 || meter.beats_per_bar() == 9.0 || meter.beats_per_bar() == 6.0)) {
1022 uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
1023 uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
1025 os << setw(2) << setfill('0') << subdivisions + 1;
1026 os << setw(3) << setfill('0') << ticks;
1032 MackieControlProtocol::format_timecode_timecode (framepos_t now_frame)
1034 Timecode::Time timecode;
1035 session->timecode_time (now_frame, timecode);
1037 // According to the Logic docs
1038 // digits: 888/88/88/888
1039 // Timecode mode: Hours/Minutes/Seconds/Frames
1041 os << setw(3) << setfill('0') << timecode.hours;
1042 os << setw(2) << setfill('0') << timecode.minutes;
1043 os << setw(2) << setfill('0') << timecode.seconds;
1044 os << setw(3) << setfill('0') << timecode.frames;
1050 MackieControlProtocol::update_timecode_display()
1052 if (surface().has_timecode_display()) {
1053 // do assignment here so current_frame is fixed
1054 framepos_t current_frame = session->transport_frame();
1057 switch (_timecode_type) {
1058 case ARDOUR::AnyTime::BBT:
1059 timecode = format_bbt_timecode (current_frame);
1061 case ARDOUR::AnyTime::Timecode:
1062 timecode = format_timecode_timecode (current_frame);
1066 os << "Unknown timecode: " << _timecode_type;
1067 throw runtime_error (os.str());
1070 // only write the timecode string to the MCU if it's changed
1071 // since last time. This is to reduce midi bandwidth used.
1072 if (timecode != _timecode_last) {
1073 surface().display_timecode (mcu_port(), builder, timecode, _timecode_last);
1074 _timecode_last = timecode;
1080 MackieControlProtocol::poll_session_data()
1082 // XXX need to attach this to a timer in the MIDI UI event loop (20msec)
1085 // do all currently mapped routes
1086 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
1087 update_automation (**it);
1090 // and the master strip
1091 if (master_route_signal != 0) {
1092 update_automation (*master_route_signal);
1095 update_timecode_display();
1099 /////////////////////////////////////
1100 // Transport Buttons
1101 /////////////////////////////////////
1104 MackieControlProtocol::frm_left_press (Button &)
1106 // can use first_mark_before/after as well
1107 unsigned long elapsed = _frm_left_last.restart();
1109 Location * loc = session->locations()->first_location_before (
1110 session->transport_frame()
1113 // allow a quick double to go past a previous mark
1114 if (session->transport_rolling() && elapsed < 500 && loc != 0) {
1115 Location * loc_two_back = session->locations()->first_location_before (loc->start());
1116 if (loc_two_back != 0)
1122 // move to the location, if it's valid
1124 session->request_locate (loc->start(), session->transport_rolling());
1131 MackieControlProtocol::frm_left_release (Button &)
1137 MackieControlProtocol::frm_right_press (Button &)
1139 // can use first_mark_before/after as well
1140 Location * loc = session->locations()->first_location_after (session->transport_frame());
1143 session->request_locate (loc->start(), session->transport_rolling());
1150 MackieControlProtocol::frm_right_release (Button &)
1156 MackieControlProtocol::stop_press (Button &)
1158 session->request_stop();
1163 MackieControlProtocol::stop_release (Button &)
1165 return session->transport_stopped();
1169 MackieControlProtocol::play_press (Button &)
1171 session->request_transport_speed (1.0);
1176 MackieControlProtocol::play_release (Button &)
1178 return session->transport_rolling();
1182 MackieControlProtocol::record_press (Button &)
1184 if (session->get_record_enabled()) {
1185 session->disable_record (false);
1187 session->maybe_enable_record();
1193 MackieControlProtocol::record_release (Button &)
1195 if (session->get_record_enabled()) {
1196 if (session->transport_rolling()) {
1207 MackieControlProtocol::rewind_press (Button &)
1209 _jog_wheel.push (JogWheel::speed);
1210 _jog_wheel.transport_direction (-1);
1211 session->request_transport_speed (-_jog_wheel.transport_speed());
1216 MackieControlProtocol::rewind_release (Button &)
1219 _jog_wheel.transport_direction (0);
1220 if (_transport_previously_rolling) {
1221 session->request_transport_speed (1.0);
1223 session->request_stop();
1229 MackieControlProtocol::ffwd_press (Button &)
1231 _jog_wheel.push (JogWheel::speed);
1232 _jog_wheel.transport_direction (1);
1233 session->request_transport_speed (_jog_wheel.transport_speed());
1238 MackieControlProtocol::ffwd_release (Button &)
1241 _jog_wheel.transport_direction (0);
1242 if (_transport_previously_rolling) {
1243 session->request_transport_speed (1.0);
1245 session->request_stop();
1251 MackieControlProtocol::loop_press (Button &)
1253 session->request_play_loop (!session->get_play_loop());
1258 MackieControlProtocol::loop_release (Button &)
1260 return session->get_play_loop();
1264 MackieControlProtocol::punch_in_press (Button &)
1266 bool const state = !session->config.get_punch_in();
1267 session->config.set_punch_in (state);
1272 MackieControlProtocol::punch_in_release (Button &)
1274 return session->config.get_punch_in();
1278 MackieControlProtocol::punch_out_press (Button &)
1280 bool const state = !session->config.get_punch_out();
1281 session->config.set_punch_out (state);
1286 MackieControlProtocol::punch_out_release (Button &)
1288 return session->config.get_punch_out();
1292 MackieControlProtocol::home_press (Button &)
1294 session->goto_start();
1299 MackieControlProtocol::home_release (Button &)
1305 MackieControlProtocol::end_press (Button &)
1307 session->goto_end();
1312 MackieControlProtocol::end_release (Button &)
1318 MackieControlProtocol::clicking_press (Button &)
1320 bool state = !Config->get_clicking();
1321 Config->set_clicking (state);
1326 MackieControlProtocol::clicking_release (Button &)
1328 return Config->get_clicking();
1331 LedState MackieControlProtocol::global_solo_press (Button &)
1333 bool state = !session->soloing();
1334 session->set_solo (session->get_routes(), state);
1338 LedState MackieControlProtocol::global_solo_release (Button &)
1340 return session->soloing();
1343 ///////////////////////////////////////////
1345 ///////////////////////////////////////////
1347 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
1349 if (p == "punch-in") {
1350 update_global_button ("punch_in", session->config.get_punch_in());
1351 } else if (p == "punch-out") {
1352 update_global_button ("punch_out", session->config.get_punch_out());
1353 } else if (p == "clicking") {
1354 update_global_button ("clicking", Config->get_clicking());
1356 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("parameter changed: %1\n", p));
1360 // RouteList is the set of routes that have just been added
1362 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
1364 // currently assigned banks are less than the full set of
1365 // strips, so activate the new strip now.
1366 if (route_signals.size() < route_table.size()) {
1367 refresh_current_bank();
1369 // otherwise route added, but current bank needs no updating
1371 // make sure remote id changes in the new route are handled
1372 typedef ARDOUR::RouteList ARS;
1374 for (ARS::iterator it = rl.begin(); it != rl.end(); ++it) {
1375 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
1380 MackieControlProtocol::notify_solo_active_changed (bool active)
1382 Button * rude_solo = reinterpret_cast<Button*> (surface().controls_by_name["solo"]);
1383 mcu_port().write (builder.build_led (*rude_solo, active ? flashing : off));
1387 MackieControlProtocol::notify_remote_id_changed()
1389 Sorted sorted = get_sorted_routes();
1391 // if a remote id has been moved off the end, we need to shift
1392 // the current bank backwards.
1393 if (sorted.size() - _current_initial_bank < route_signals.size()) {
1394 // but don't shift backwards past the zeroth channel
1395 switch_banks (max((Sorted::size_type) 0, sorted.size() - route_signals.size()));
1397 // Otherwise just refresh the current bank
1398 refresh_current_bank();
1402 ///////////////////////////////////////////
1403 // Transport signals
1404 ///////////////////////////////////////////
1407 MackieControlProtocol::notify_record_state_changed()
1409 // switch rec button on / off / flashing
1410 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1411 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1415 MackieControlProtocol::notify_transport_state_changed()
1417 // switch various play and stop buttons on / off
1418 update_global_button ("play", session->transport_rolling());
1419 update_global_button ("stop", !session->transport_rolling());
1420 update_global_button ("loop", session->get_play_loop());
1422 _transport_previously_rolling = session->transport_rolling();
1424 // rec is special because it's tristate
1425 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1426 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1429 /////////////////////////////////////
1431 /////////////////////////////////////
1433 MackieControlProtocol::left_press (Button &)
1435 Sorted sorted = get_sorted_routes();
1436 if (sorted.size() > route_table.size()) {
1437 int new_initial = _current_initial_bank - route_table.size();
1438 if (new_initial < 0) {
1442 if (new_initial != int (_current_initial_bank)) {
1443 session->set_dirty();
1444 switch_banks (new_initial);
1454 MackieControlProtocol::left_release (Button &)
1460 MackieControlProtocol::right_press (Button &)
1462 Sorted sorted = get_sorted_routes();
1463 if (sorted.size() > route_table.size()) {
1464 uint32_t delta = sorted.size() - (route_table.size() + _current_initial_bank);
1466 if (delta > route_table.size()) {
1467 delta = route_table.size();
1471 session->set_dirty();
1472 switch_banks (_current_initial_bank + delta);
1482 MackieControlProtocol::right_release (Button &)
1488 MackieControlProtocol::channel_left_press (Button &)
1490 Sorted sorted = get_sorted_routes();
1491 if (sorted.size() > route_table.size()) {
1500 MackieControlProtocol::channel_left_release (Button &)
1506 MackieControlProtocol::channel_right_press (Button &)
1508 Sorted sorted = get_sorted_routes();
1509 if (sorted.size() > route_table.size()) {
1518 MackieControlProtocol::channel_right_release (Button &)
1523 /////////////////////////////////////
1525 /////////////////////////////////////
1527 MackieControlProtocol::marker_press (Button &)
1529 // cut'n'paste from LocationUI::add_new_location()
1531 framepos_t where = session->audible_frame();
1532 session->locations()->next_available_name(markername,"mcu");
1533 Location *location = new Location (*session, where, where, markername, Location::IsMark);
1534 session->begin_reversible_command (_("add marker"));
1535 XMLNode &before = session->locations()->get_state();
1536 session->locations()->add (location, true);
1537 XMLNode &after = session->locations()->get_state();
1538 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1539 session->commit_reversible_command ();
1544 MackieControlProtocol::marker_release (Button &)
1550 jog_wheel_state_display (JogWheel::State state, SurfacePort & port)
1553 case JogWheel::zoom:
1554 port.write (builder.two_char_display ("Zm"));
1556 case JogWheel::scroll:
1557 port.write (builder.two_char_display ("Sc"));
1559 case JogWheel::scrub:
1560 port.write (builder.two_char_display ("Sb"));
1562 case JogWheel::shuttle:
1563 port.write (builder.two_char_display ("Sh"));
1565 case JogWheel::speed:
1566 port.write (builder.two_char_display ("Sp"));
1568 case JogWheel::select:
1569 port.write (builder.two_char_display ("Se"));
1575 MackieControlProtocol::zoom_press (Mackie::Button &)
1577 _jog_wheel.zoom_state_toggle();
1578 update_global_button ("scrub", _jog_wheel.jog_wheel_state() == JogWheel::scrub);
1579 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1580 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1584 MackieControlProtocol::zoom_release (Mackie::Button &)
1586 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1590 MackieControlProtocol::scrub_press (Mackie::Button &)
1592 _jog_wheel.scrub_state_cycle();
1593 update_global_button ("zoom", _jog_wheel.jog_wheel_state() == JogWheel::zoom);
1594 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1596 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1598 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1603 MackieControlProtocol::scrub_release (Mackie::Button &)
1606 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1608 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1613 MackieControlProtocol::drop_press (Button &)
1615 session->remove_last_capture();
1620 MackieControlProtocol::drop_release (Button &)
1626 MackieControlProtocol::save_press (Button &)
1628 session->save_state ("");
1633 MackieControlProtocol::save_release (Button &)
1639 MackieControlProtocol::timecode_beats_press (Button &)
1641 switch (_timecode_type) {
1642 case ARDOUR::AnyTime::BBT:
1643 _timecode_type = ARDOUR::AnyTime::Timecode;
1645 case ARDOUR::AnyTime::Timecode:
1646 _timecode_type = ARDOUR::AnyTime::BBT;
1650 os << "Unknown Anytime::Type " << _timecode_type;
1651 throw runtime_error (os.str());
1653 update_timecode_beats_led();
1658 MackieControlProtocol::timecode_beats_release (Button &)
1663 list<boost::shared_ptr<ARDOUR::Bundle> >
1664 MackieControlProtocol::bundles ()
1666 list<boost::shared_ptr<ARDOUR::Bundle> > b;
1667 b.push_back (_input_bundle);
1668 b.push_back (_output_bundle);
1673 MackieControlProtocol::port_connected_or_disconnected (string a, string b, bool connected)
1675 /* If something is connected to one of our output ports, send MIDI to update the surface
1676 to whatever state it should have.
1683 MackiePorts::const_iterator i = _ports.begin();
1684 while (i != _ports.end()) {
1686 string const n = AudioEngine::instance()->make_port_name_non_relative ((*i)->output_port().name ());
1688 if (a == n || b == n) {
1695 if (i != _ports.end ()) {