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 #include "pbd/abstract_ui.cc" // instantiate template
75 #define NUCLEUS_DEBUG 1
77 MackieMidiBuilder builder;
79 #define midi_ui_context() MidiControlUI::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
80 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
82 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
83 #define invalidator(x) __invalidator ((x), __FILE__, __LINE__)
85 MackieControlProtocol::MackieControlProtocol (Session& session)
86 : ControlProtocol (session, X_("Mackie"), MidiControlUI::instance())
87 , AbstractUI<MackieControlUIRequest> ("mackie")
88 , _current_initial_bank (0)
91 , _timecode_type (ARDOUR::AnyTime::BBT)
92 , _input_bundle (new ARDOUR::Bundle (_("Mackie Control In"), true))
93 , _output_bundle (new ARDOUR::Bundle (_("Mackie Control Out"), false))
96 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::MackieControlProtocol\n");
98 AudioEngine::instance()->PortConnectedOrDisconnected.connect (
99 audio_engine_connections, invalidator (*this), ui_bind (&MackieControlProtocol::port_connected_or_disconnected, this, _2, _4, _5),
104 MackieControlProtocol::~MackieControlProtocol()
106 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::~MackieControlProtocol\n");
111 catch (exception & e) {
112 cout << "~MackieControlProtocol caught " << e.what() << endl;
115 cout << "~MackieControlProtocol caught unknown" << endl;
118 DEBUG_TRACE (DEBUG::MackieControl, "finished ~MackieControlProtocol::MackieControlProtocol\n");
122 MackieControlProtocol::surface()
125 throw MackieControlException ("_surface is 0 in MackieControlProtocol::surface");
130 const Mackie::SurfacePort&
131 MackieControlProtocol::mcu_port() const
133 if (_ports.size() < 1) {
136 return dynamic_cast<const MackiePort &> (*_ports[0]);
141 MackieControlProtocol::mcu_port()
143 if (_ports.size() < 1) {
146 return dynamic_cast<MackiePort &> (*_ports[0]);
150 // go to the previous track.
151 // Assume that get_sorted_routes().size() > route_table.size()
153 MackieControlProtocol::prev_track()
155 if (_current_initial_bank >= 1) {
156 session->set_dirty();
157 switch_banks (_current_initial_bank - 1);
161 // go to the next track.
162 // Assume that get_sorted_routes().size() > route_table.size()
164 MackieControlProtocol::next_track()
166 Sorted sorted = get_sorted_routes();
167 if (_current_initial_bank + route_table.size() < sorted.size()) {
168 session->set_dirty();
169 switch_banks (_current_initial_bank + 1);
174 MackieControlProtocol::clear_route_signals()
176 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
179 route_signals.clear();
182 // return the port for a given id - 0 based
183 // throws an exception if no port found
185 MackieControlProtocol::port_for_id (uint32_t index)
187 uint32_t current_max = 0;
189 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
190 current_max += (*it)->strips();
191 if (index < current_max) {
196 // oops - no matching port
198 os << "No port for index " << index;
199 cerr << "No port for index " << index << endl;
200 throw MackieControlException (os.str());
203 // predicate for sort call in get_sorted_routes
204 struct RouteByRemoteId
206 bool operator () (const boost::shared_ptr<Route> & a, const boost::shared_ptr<Route> & b) const
208 return a->remote_control_id() < b->remote_control_id();
211 bool operator () (const Route & a, const Route & b) const
213 return a.remote_control_id() < b.remote_control_id();
216 bool operator () (const Route * a, const Route * b) const
218 return a->remote_control_id() < b->remote_control_id();
222 MackieControlProtocol::Sorted
223 MackieControlProtocol::get_sorted_routes()
228 boost::shared_ptr<RouteList> routes = session->get_routes();
229 set<uint32_t> remote_ids;
231 // routes with remote_id 0 should never be added
232 // TODO verify this with ardour devs
233 // remote_ids.insert (0);
235 // sort in remote_id order, and exclude master, control and hidden routes
236 // and any routes that are already set.
237 for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it) {
238 Route & route = **it;
241 && !route.is_master()
242 && !route.is_hidden()
243 && !route.is_monitor()
244 && remote_ids.find (route.remote_control_id()) == remote_ids.end()
246 sorted.push_back (*it);
247 remote_ids.insert (route.remote_control_id());
250 sort (sorted.begin(), sorted.end(), RouteByRemoteId());
255 MackieControlProtocol::refresh_current_bank()
257 switch_banks (_current_initial_bank);
261 MackieControlProtocol::switch_banks (int initial)
263 // DON'T prevent bank switch if initial == _current_initial_bank
264 // because then this method can't be used as a refresh
267 Sorted sorted = get_sorted_routes();
268 int delta = sorted.size() - route_table.size();
269 if (initial < 0 || (delta > 0 && initial > delta)) {
270 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("not switching to %1\n", initial));
273 _current_initial_bank = initial;
275 // first clear the signals from old routes
276 // taken care of by the RouteSignal destructors
277 clear_route_signals();
279 // now set the signals for new routes
280 if (_current_initial_bank <= sorted.size()) {
281 // fetch the bank start and end to switch to
282 uint32_t end_pos = min (route_table.size(), sorted.size());
283 Sorted::iterator it = sorted.begin() + _current_initial_bank;
284 Sorted::iterator end = sorted.begin() + _current_initial_bank + end_pos;
286 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("switch to %1, %2\n", _current_initial_bank, end_pos));
288 route_table.clear ();
289 set_route_table_size (surface().strips.size());
291 // link routes to strips
293 for (; it != end && it != sorted.end(); ++it, ++i) {
294 boost::shared_ptr<Route> route = *it;
296 assert (surface().strips[i]);
297 Strip & strip = *surface().strips[i];
299 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("remote id %1 connecting %2 to %3 with port %4\n",
300 route->remote_control_id(), route->name(), strip.name(), port_for_id(i)));
301 set_route_table (i, route);
302 RouteSignal * rs = new RouteSignal (route, *this, strip, port_for_id(i));
303 route_signals.push_back (rs);
304 // update strip from route
308 // create dead strips if there aren't enough routes to
310 for (; i < route_table.size(); ++i) {
311 Strip & strip = *surface().strips[i];
312 // send zero for this strip
313 MackiePort & port = port_for_id(i);
314 port.write (builder.zero_strip (port, strip));
318 // display the current start bank.
319 surface().display_bank_start (mcu_port(), builder, _current_initial_bank);
323 MackieControlProtocol::zero_all()
325 // TODO turn off Timecode displays
328 for (Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it) {
329 MackiePort & port = port_for_id ((*it)->index());
330 port.write (builder.zero_strip (port, **it));
333 // and the master strip
334 mcu_port().write (builder.zero_strip (dynamic_cast<MackiePort&> (mcu_port()), master_strip()));
336 // turn off global buttons and leds
337 // global buttons are only ever on mcu_port, so we don't have
338 // to figure out which port.
339 for (Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it) {
340 Control & control = **it;
341 if (!control.group().is_strip() && control.accepts_feedback()) {
342 mcu_port().write (builder.zero_control (control));
346 // any hardware-specific stuff
347 surface().zero_all (mcu_port(), builder);
351 MackieControlProtocol::set_active (bool yn)
359 // the reason for the locking and unlocking is that
360 // glibmm can't do a condition wait on a RecMutex
362 // TODO what happens if this fails half way?
364 // create MackiePorts
366 Glib::Mutex::Lock lock (update_mutex);
370 // now initialise MackiePorts - ie exchange sysex messages
371 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
375 // wait until all ports are active
376 // TODO a more sophisticated approach would
377 // allow things to start up with only an MCU, even if
378 // extenders were specified but not responding.
379 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
380 (*it)->wait_for_init();
383 // create surface object. This depends on the ports being
384 // correctly initialised
385 initialize_surface();
386 connect_session_signals();
391 // send current control positions to surface
392 // must come after _active = true otherwise it won't run
400 catch (exception & e) {
401 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("set_active to false because exception caught: %1\n", e.what()));
410 MackieControlProtocol::handle_strip_button (SurfacePort & port, Control & control, ButtonState bs, boost::shared_ptr<Route> route)
415 if (control.name() == "recenable") {
416 state = !route->record_enabled();
417 route->set_record_enabled (state, this);
418 } else if (control.name() == "mute") {
419 state = !route->muted();
420 route->set_mute (state, this);
421 } else if (control.name() == "solo") {
422 state = !route->soloed();
423 route->set_solo (state, this);
424 } else if (control.name() == "select") {
425 // TODO make the track selected. Whatever that means.
426 //state = default_button_press (dynamic_cast<Button&> (control));
427 } 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") {
435 control.strip().gain().set_in_use (state);
437 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
438 /* BCF faders don't support touch, so add a timeout to reset
439 their `in_use' state.
441 add_in_use_timeout (port, control.strip().gain(), &control.strip().fader_touch());
449 MackieControlProtocol::update_led (Mackie::Button & button, Mackie::LedState ls)
452 SurfacePort * port = 0;
453 if (button.group().is_strip()) {
454 if (button.group().is_master()) {
457 port = &port_for_id (dynamic_cast<const Strip&> (button.group()).index());
462 port->write (builder.build_led (button, ls));
467 MackieControlProtocol::update_timecode_beats_led()
469 switch (_timecode_type) {
470 case ARDOUR::AnyTime::BBT:
471 update_global_led ("beats", on);
472 update_global_led ("timecode", off);
474 case ARDOUR::AnyTime::Timecode:
475 update_global_led ("timecode", on);
476 update_global_led ("beats", off);
480 os << "Unknown Anytime::Type " << _timecode_type;
481 throw runtime_error (os.str());
486 MackieControlProtocol::update_global_button (const string & name, LedState ls)
488 if (surface().controls_by_name.find (name) != surface().controls_by_name.end()) {
489 Button * button = dynamic_cast<Button*> (surface().controls_by_name[name]);
490 mcu_port().write (builder.build_led (button->led(), ls));
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()) {
500 Led * led = dynamic_cast<Led*> (surface().controls_by_name[name]);
501 mcu_port().write (builder.build_led (*led, ls));
503 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Led %1 not found\n", name));
507 // send messages to surface to set controls to correct values
509 MackieControlProtocol::update_surface()
515 // do the initial bank switch to connect signals
516 // _current_initial_bank is initialised by set_state
517 switch_banks (_current_initial_bank);
519 /* Create a RouteSignal for the master route, if we don't already have one */
520 if (!master_route_signal) {
521 boost::shared_ptr<Route> mr = master_route ();
523 master_route_signal = boost::shared_ptr<RouteSignal> (new RouteSignal (mr, *this, master_strip(), mcu_port()));
524 // update strip from route
525 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();
539 MackieControlProtocol::connect_session_signals()
541 // receive routes added
542 session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_route_added, this, _1), midi_ui_context());
543 // receive record state toggled
544 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_record_state_changed, this), midi_ui_context());
545 // receive transport state changed
546 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_transport_state_changed, this), midi_ui_context());
547 // receive punch-in and punch-out
548 Config->ParameterChanged.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
549 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_parameter_changed, this, _1), midi_ui_context());
550 // receive rude solo changed
551 session->SoloActive.connect(session_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_solo_active_changed, this, _1), midi_ui_context());
553 // make sure remote id changed signals reach here
554 // see also notify_route_added
555 Sorted sorted = get_sorted_routes();
557 for (Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it) {
558 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind(&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
563 MackieControlProtocol::add_port (MIDI::Port & midi_input_port, MIDI::Port & midi_output_port, int number, MackiePort::port_type_t port_type)
565 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("add port %1 %2\n", midi_input_port.name(), midi_output_port.name()));
567 MackiePort * sport = new MackiePort (*this, midi_input_port, midi_output_port, number, port_type);
568 _ports.push_back (sport);
570 sport->init_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_init, this, sport));
571 sport->active_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_active, this, sport));
572 sport->inactive_event.connect_same_thread (port_connections, boost::bind (&MackieControlProtocol::handle_port_inactive, this, sport));
574 _input_bundle->add_channel (
575 midi_input_port.name(),
576 ARDOUR::DataType::MIDI,
577 session->engine().make_port_name_non_relative (midi_input_port.name())
580 _output_bundle->add_channel (
581 midi_output_port.name(),
582 ARDOUR::DataType::MIDI,
583 session->engine().make_port_name_non_relative (midi_output_port.name())
588 MackieControlProtocol::create_ports()
590 MIDI::Manager * mm = MIDI::Manager::instance();
591 MIDI::Port * midi_input_port = mm->add_port (
592 new MIDI::Port (string_compose (_("%1 in"), default_port_name), MIDI::Port::IsInput, session->engine().jack())
594 MIDI::Port * midi_output_port = mm->add_port (
595 new MIDI::Port (string_compose (_("%1 out"), default_port_name), MIDI::Port::IsOutput, session->engine().jack())
598 /* Create main port */
600 if (!midi_input_port->ok() || !midi_output_port->ok()) {
602 os << _("Mackie control MIDI ports could not be created; Mackie control disabled");
603 error << os.str() << endmsg;
604 throw MackieControlException (os.str());
607 add_port (*midi_input_port, *midi_output_port, 0, MackiePort::mcu);
609 /* Create extender ports */
611 for (uint32_t index = 1; index <= Config->get_mackie_extenders(); ++index) {
612 MIDI::Port * midi_input_port = mm->add_port (
613 new MIDI::Port (string_compose (_("mcu_xt_%1 in"), index), MIDI::Port::IsInput, session->engine().jack())
615 MIDI::Port * midi_output_port = mm->add_port (
616 new MIDI::Port (string_compose (_("mcu_xt_%1 out"), index), MIDI::Port::IsOutput, session->engine().jack())
618 if (midi_input_port->ok() && midi_output_port->ok()) {
619 add_port (*midi_input_port, *midi_output_port, index, MackiePort::ext);
624 boost::shared_ptr<Route>
625 MackieControlProtocol::master_route()
627 return session->master_out ();
631 MackieControlProtocol::master_strip()
633 return dynamic_cast<Strip&> (*surface().groups["master"]);
637 MackieControlProtocol::initialize_surface()
639 // set up the route table
641 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
642 strips += (*it)->strips();
645 set_route_table_size (strips);
647 // TODO same as code in mackie_port.cc
648 string emulation = ARDOUR::Config->get_mackie_emulation();
649 if (emulation == "bcf") {
650 _surface = new BcfSurface (strips);
651 } else if (emulation == "mcu") {
652 _surface = new MackieSurface (strips);
655 os << "no Surface class found for emulation: " << emulation;
656 throw MackieControlException (os.str());
663 MackieControlProtocol::close()
666 // must be before other shutdown otherwise polling loop
667 // calls methods on objects that are deleted
669 port_connections.drop_connections ();
670 session_connections.drop_connections ();
671 route_connections.drop_connections ();
674 // These will fail if the port has gone away.
675 // So catch the exception and do the rest of the
677 // because the bcf doesn't respond to the next 3 sysex messages
682 catch (exception & e) {
683 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
686 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
688 MackiePort & port = **it;
690 port.write_sysex (0x61);
692 port.write_sysex (0x62);
693 // Reset (reboot into offline mode)
694 port.write_sysex (0x63);
696 catch (exception & e) {
697 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::close caught exception: %1\n", e.what()));
701 // disconnect routes from strips
702 clear_route_signals();
707 // shut down MackiePorts
708 for (MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it) {
716 MackieControlProtocol::get_state()
718 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::get_state\n");
720 // add name of protocol
721 XMLNode* node = new XMLNode (X_("Protocol"));
722 node->add_property (X_("name"), ARDOUR::ControlProtocol::_name);
726 os << _current_initial_bank;
727 node->add_property (X_("bank"), os.str());
733 MackieControlProtocol::set_state (const XMLNode & node, int /*version*/)
735 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieControlProtocol::set_state: active %1\n", _active));
739 // fetch current bank
741 if (node.property (X_("bank")) != 0) {
742 string bank = node.property (X_("bank"))->value();
745 uint32_t new_bank = atoi (bank.c_str());
746 if (_current_initial_bank != new_bank) {
747 switch_banks (new_bank);
750 catch (exception & e) {
751 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("exception in MackieControlProtocol::set_state: %1\n", e.what()));
760 MackieControlProtocol::handle_control_event (SurfacePort & port, Control & control, const ControlState & state)
762 // find the route for the control, if there is one
763 boost::shared_ptr<Route> route;
765 if (control.group().is_strip()) {
766 if (control.group().is_master()) {
767 DEBUG_TRACE (DEBUG::MackieControl, "master strip control event\n");
768 route = master_route();
770 uint32_t index = control.ordinal() - 1 + (port.number() * port.strips());
771 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip control event, index = %1, rt size = %2\n",
772 index, route_table.size()));
773 if (index < route_table.size()) {
774 route = route_table[index];
776 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("modifying %1\n", route->name()));
778 DEBUG_TRACE (DEBUG::MackieControl, "no route found!\n");
781 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
782 DEBUG_TRACE (DEBUG::MackieControl, "illegal route index found!\n");
787 // This handles control element events from the surface
788 // the state of the controls on the surface is usually updated
790 switch (control.type()) {
791 case Control::type_fader:
792 // find the route in the route table for the id
793 // if the route isn't available, skip it
794 // at which point the fader should just reset itself
797 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", state.pos));
799 route->gain_control()->set_value (slider_position_to_gain (state.pos));
801 if (ARDOUR::Config->get_mackie_emulation() == "bcf") {
802 /* reset the timeout while we're still moving the fader */
803 add_in_use_timeout (port, control, control.in_use_touch_control);
806 // must echo bytes back to slider now, because
807 // the notifier only works if the fader is not being
808 // touched. Which it is if we're getting input.
809 port.write (builder.build_fader ((Fader&)control, state.pos));
813 case Control::type_button:
814 if (control.group().is_strip()) {
816 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip button %1\n", control.id()));
818 handle_strip_button (port, control, state.button_state, route);
820 // no route so always switch the light off
821 // because no signals will be emitted by a non-route
822 port.write (builder.build_led (control.led(), off));
824 } else if (control.group().is_master()) {
825 // master fader touch
826 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("master strip button %1\n", control.id()));
828 handle_strip_button (port, control, state.button_state, route);
831 // handle all non-strip buttons
832 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("global button %1\n", control.id()));
833 surface().handle_button (*this, state.button_state, dynamic_cast<Button&> (control));
837 // pot (jog wheel, external control)
838 case Control::type_pot:
839 if (control.group().is_strip()) {
840 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip pot %1\n", control.id()));
842 boost::shared_ptr<Panner> panner = route->panner_shell()->panner();
843 // pan for mono input routes, or stereo linked panners
845 double p = panner->position ();
847 // calculate new value, and adjust
848 p += state.delta * state.sign;
851 panner->set_position (p);
854 // it's a pot for an umnapped route, so turn all the lights off
855 port.write (builder.build_led_ring (dynamic_cast<Pot &> (control), off));
858 if (control.is_jog()) {
859 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Jog wheel moved %1\n", state.ticks));
860 _jog_wheel.jog_event (port, control, state);
862 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("External controller moved %1\n", state.ticks));
863 cout << "external controller" << state.ticks * state.sign << endl;
869 cout << "Control::type not handled: " << control.type() << endl;
873 /////////////////////////////////////////////////
874 // handlers for Route signals
875 // TODO should these be part of RouteSignal?
876 // They started off as signal/slot handlers for signals
877 // from Route, but they're also used in polling for automation
878 /////////////////////////////////////////////////
881 MackieControlProtocol::notify_solo_changed (RouteSignal * route_signal)
884 Button & button = route_signal->strip().solo();
885 route_signal->port().write (builder.build_led (button, route_signal->route()->soloed()));
887 catch (exception & e) {
888 cout << e.what() << endl;
893 MackieControlProtocol::notify_mute_changed (RouteSignal * route_signal)
896 Button & button = route_signal->strip().mute();
897 route_signal->port().write (builder.build_led (button, route_signal->route()->muted()));
899 catch (exception & e) {
900 cout << e.what() << endl;
905 MackieControlProtocol::notify_record_enable_changed (RouteSignal * route_signal)
908 Button & button = route_signal->strip().recenable();
909 route_signal->port().write (builder.build_led (button, route_signal->route()->record_enabled()));
911 catch (exception & e) {
912 cout << e.what() << endl;
916 void MackieControlProtocol::notify_active_changed (RouteSignal *)
919 DEBUG_TRACE (DEBUG::MackieControl, "MackieControlProtocol::notify_active_changed\n");
920 refresh_current_bank();
922 catch (exception & e) {
923 cout << e.what() << endl;
928 MackieControlProtocol::notify_gain_changed (RouteSignal * route_signal, bool force_update)
931 Fader & fader = route_signal->strip().gain();
932 if (!fader.in_use()) {
933 float gain_value = gain_to_slider_position (route_signal->route()->gain_control()->get_value());
934 // check that something has actually changed
935 if (force_update || gain_value != route_signal->last_gain_written()) {
936 route_signal->port().write (builder.build_fader (fader, gain_value));
937 route_signal->last_gain_written (gain_value);
941 catch (exception & e) {
942 cout << e.what() << endl;
947 MackieControlProtocol::notify_property_changed (const PropertyChange& what_changed, RouteSignal * route_signal)
949 if (!what_changed.contains (Properties::name)) {
954 Strip & strip = route_signal->strip();
956 if (!strip.is_master()) {
958 string fullname = route_signal->route()->name();
960 if (fullname.length() <= 6) {
963 line1 = PBD::short_version (fullname, 6);
967 cerr << "show strip name from " << fullname << " as " << line1 << endl;
970 SurfacePort & port = route_signal->port();
971 port.write (builder.strip_display (port, strip, 0, line1));
972 port.write (builder.strip_display_blank (port, strip, 1));
975 catch (exception & e) {
976 cout << e.what() << endl;
981 MackieControlProtocol::notify_panner_changed (RouteSignal * route_signal, bool force_update)
984 Pot & pot = route_signal->strip().vpot();
985 boost::shared_ptr<Panner> panner = route_signal->route()->panner();
987 double pos = panner->position ();
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);
1000 route_signal->port().write (builder.zero_control (pot));
1003 catch (exception & e) {
1004 cout << e.what() << endl;
1008 // TODO handle plugin automation polling
1010 MackieControlProtocol::update_automation (RouteSignal & rs)
1012 ARDOUR::AutoState gain_state = rs.route()->gain_control()->automation_state();
1014 if (gain_state == Touch || gain_state == Play) {
1015 notify_gain_changed (&rs, false);
1018 if (rs.route()->panner()) {
1019 ARDOUR::AutoState panner_state = rs.route()->panner()->automation_state();
1020 if (panner_state == Touch || panner_state == Play) {
1021 notify_panner_changed (&rs, false);
1027 MackieControlProtocol::format_bbt_timecode (framepos_t now_frame)
1029 Timecode::BBT_Time bbt_time;
1030 session->bbt_time (now_frame, bbt_time);
1032 // According to the Logic docs
1033 // digits: 888/88/88/888
1034 // BBT mode: Bars/Beats/Subdivisions/Ticks
1036 os << setw(3) << setfill('0') << bbt_time.bars;
1037 os << setw(2) << setfill('0') << bbt_time.beats;
1039 // figure out subdivisions per beat
1040 const Meter & meter = session->tempo_map().meter_at (now_frame);
1042 if (meter.note_divisor() == 8 && (meter.divisions_per_bar() == 12.0 || meter.divisions_per_bar() == 9.0 || meter.divisions_per_bar() == 6.0)) {
1046 uint32_t subdivisions = bbt_time.ticks / uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
1047 uint32_t ticks = bbt_time.ticks % uint32_t (Timecode::BBT_Time::ticks_per_beat / subdiv);
1049 os << setw(2) << setfill('0') << subdivisions + 1;
1050 os << setw(3) << setfill('0') << ticks;
1056 MackieControlProtocol::format_timecode_timecode (framepos_t now_frame)
1058 Timecode::Time timecode;
1059 session->timecode_time (now_frame, timecode);
1061 // According to the Logic docs
1062 // digits: 888/88/88/888
1063 // Timecode mode: Hours/Minutes/Seconds/Frames
1065 os << setw(3) << setfill('0') << timecode.hours;
1066 os << setw(2) << setfill('0') << timecode.minutes;
1067 os << setw(2) << setfill('0') << timecode.seconds;
1068 os << setw(3) << setfill('0') << timecode.frames;
1074 MackieControlProtocol::update_timecode_display()
1076 if (surface().has_timecode_display()) {
1077 // do assignment here so current_frame is fixed
1078 framepos_t current_frame = session->transport_frame();
1081 switch (_timecode_type) {
1082 case ARDOUR::AnyTime::BBT:
1083 timecode = format_bbt_timecode (current_frame);
1085 case ARDOUR::AnyTime::Timecode:
1086 timecode = format_timecode_timecode (current_frame);
1090 os << "Unknown timecode: " << _timecode_type;
1091 throw runtime_error (os.str());
1094 // only write the timecode string to the MCU if it's changed
1095 // since last time. This is to reduce midi bandwidth used.
1096 if (timecode != _timecode_last) {
1097 surface().display_timecode (mcu_port(), builder, timecode, _timecode_last);
1098 _timecode_last = timecode;
1104 MackieControlProtocol::poll_session_data()
1106 // XXX need to attach this to a timer in the MIDI UI event loop (20msec)
1109 // do all currently mapped routes
1110 for (RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it) {
1111 update_automation (**it);
1114 // and the master strip
1115 if (master_route_signal != 0) {
1116 update_automation (*master_route_signal);
1119 update_timecode_display();
1123 /////////////////////////////////////
1124 // Transport Buttons
1125 /////////////////////////////////////
1128 MackieControlProtocol::frm_left_press (Button &)
1130 // can use first_mark_before/after as well
1131 unsigned long elapsed = _frm_left_last.restart();
1133 Location * loc = session->locations()->first_location_before (
1134 session->transport_frame()
1137 // allow a quick double to go past a previous mark
1138 if (session->transport_rolling() && elapsed < 500 && loc != 0) {
1139 Location * loc_two_back = session->locations()->first_location_before (loc->start());
1140 if (loc_two_back != 0)
1146 // move to the location, if it's valid
1148 session->request_locate (loc->start(), session->transport_rolling());
1155 MackieControlProtocol::frm_left_release (Button &)
1161 MackieControlProtocol::frm_right_press (Button &)
1163 // can use first_mark_before/after as well
1164 Location * loc = session->locations()->first_location_after (session->transport_frame());
1167 session->request_locate (loc->start(), session->transport_rolling());
1174 MackieControlProtocol::frm_right_release (Button &)
1180 MackieControlProtocol::stop_press (Button &)
1182 session->request_stop();
1187 MackieControlProtocol::stop_release (Button &)
1189 return session->transport_stopped();
1193 MackieControlProtocol::play_press (Button &)
1195 session->request_transport_speed (1.0);
1200 MackieControlProtocol::play_release (Button &)
1202 return session->transport_rolling();
1206 MackieControlProtocol::record_press (Button &)
1208 if (session->get_record_enabled()) {
1209 session->disable_record (false);
1211 session->maybe_enable_record();
1217 MackieControlProtocol::record_release (Button &)
1219 if (session->get_record_enabled()) {
1220 if (session->transport_rolling()) {
1231 MackieControlProtocol::rewind_press (Button &)
1233 _jog_wheel.push (JogWheel::speed);
1234 _jog_wheel.transport_direction (-1);
1235 session->request_transport_speed (-_jog_wheel.transport_speed());
1240 MackieControlProtocol::rewind_release (Button &)
1243 _jog_wheel.transport_direction (0);
1244 if (_transport_previously_rolling) {
1245 session->request_transport_speed (1.0);
1247 session->request_stop();
1253 MackieControlProtocol::ffwd_press (Button &)
1255 _jog_wheel.push (JogWheel::speed);
1256 _jog_wheel.transport_direction (1);
1257 session->request_transport_speed (_jog_wheel.transport_speed());
1262 MackieControlProtocol::ffwd_release (Button &)
1265 _jog_wheel.transport_direction (0);
1266 if (_transport_previously_rolling) {
1267 session->request_transport_speed (1.0);
1269 session->request_stop();
1275 MackieControlProtocol::loop_press (Button &)
1277 session->request_play_loop (!session->get_play_loop());
1282 MackieControlProtocol::loop_release (Button &)
1284 return session->get_play_loop();
1288 MackieControlProtocol::punch_in_press (Button &)
1290 bool const state = !session->config.get_punch_in();
1291 session->config.set_punch_in (state);
1296 MackieControlProtocol::punch_in_release (Button &)
1298 return session->config.get_punch_in();
1302 MackieControlProtocol::punch_out_press (Button &)
1304 bool const state = !session->config.get_punch_out();
1305 session->config.set_punch_out (state);
1310 MackieControlProtocol::punch_out_release (Button &)
1312 return session->config.get_punch_out();
1316 MackieControlProtocol::home_press (Button &)
1318 session->goto_start();
1323 MackieControlProtocol::home_release (Button &)
1329 MackieControlProtocol::end_press (Button &)
1331 session->goto_end();
1336 MackieControlProtocol::end_release (Button &)
1342 MackieControlProtocol::clicking_press (Button &)
1344 bool state = !Config->get_clicking();
1345 Config->set_clicking (state);
1350 MackieControlProtocol::clicking_release (Button &)
1352 return Config->get_clicking();
1355 LedState MackieControlProtocol::global_solo_press (Button &)
1357 bool state = !session->soloing();
1358 session->set_solo (session->get_routes(), state);
1362 LedState MackieControlProtocol::global_solo_release (Button &)
1364 return session->soloing();
1367 ///////////////////////////////////////////
1369 ///////////////////////////////////////////
1371 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
1373 if (p == "punch-in") {
1374 update_global_button ("punch_in", session->config.get_punch_in());
1375 } else if (p == "punch-out") {
1376 update_global_button ("punch_out", session->config.get_punch_out());
1377 } else if (p == "clicking") {
1378 update_global_button ("clicking", Config->get_clicking());
1380 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("parameter changed: %1\n", p));
1384 // RouteList is the set of routes that have just been added
1386 MackieControlProtocol::notify_route_added (ARDOUR::RouteList & rl)
1388 // currently assigned banks are less than the full set of
1389 // strips, so activate the new strip now.
1390 if (route_signals.size() < route_table.size()) {
1391 refresh_current_bank();
1393 // otherwise route added, but current bank needs no updating
1395 // make sure remote id changes in the new route are handled
1396 typedef ARDOUR::RouteList ARS;
1398 for (ARS::iterator it = rl.begin(); it != rl.end(); ++it) {
1399 (*it)->RemoteControlIDChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&MackieControlProtocol::notify_remote_id_changed, this), midi_ui_context());
1404 MackieControlProtocol::notify_solo_active_changed (bool active)
1406 Button * rude_solo = reinterpret_cast<Button*> (surface().controls_by_name["solo"]);
1407 mcu_port().write (builder.build_led (*rude_solo, active ? flashing : off));
1411 MackieControlProtocol::notify_remote_id_changed()
1413 Sorted sorted = get_sorted_routes();
1415 // if a remote id has been moved off the end, we need to shift
1416 // the current bank backwards.
1417 if (sorted.size() - _current_initial_bank < route_signals.size()) {
1418 // but don't shift backwards past the zeroth channel
1419 switch_banks (max((Sorted::size_type) 0, sorted.size() - route_signals.size()));
1421 // Otherwise just refresh the current bank
1422 refresh_current_bank();
1426 ///////////////////////////////////////////
1427 // Transport signals
1428 ///////////////////////////////////////////
1431 MackieControlProtocol::notify_record_state_changed()
1433 // switch rec button on / off / flashing
1434 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1435 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1439 MackieControlProtocol::notify_transport_state_changed()
1441 // switch various play and stop buttons on / off
1442 update_global_button ("play", session->transport_rolling());
1443 update_global_button ("stop", !session->transport_rolling());
1444 update_global_button ("loop", session->get_play_loop());
1446 _transport_previously_rolling = session->transport_rolling();
1448 // rec is special because it's tristate
1449 Button * rec = reinterpret_cast<Button*> (surface().controls_by_name["record"]);
1450 mcu_port().write (builder.build_led (*rec, record_release (*rec)));
1453 /////////////////////////////////////
1455 /////////////////////////////////////
1457 MackieControlProtocol::left_press (Button &)
1459 Sorted sorted = get_sorted_routes();
1460 if (sorted.size() > route_table.size()) {
1461 int new_initial = _current_initial_bank - route_table.size();
1462 if (new_initial < 0) {
1466 if (new_initial != int (_current_initial_bank)) {
1467 session->set_dirty();
1468 switch_banks (new_initial);
1478 MackieControlProtocol::left_release (Button &)
1484 MackieControlProtocol::right_press (Button &)
1486 Sorted sorted = get_sorted_routes();
1487 if (sorted.size() > route_table.size()) {
1488 uint32_t delta = sorted.size() - (route_table.size() + _current_initial_bank);
1490 if (delta > route_table.size()) {
1491 delta = route_table.size();
1495 session->set_dirty();
1496 switch_banks (_current_initial_bank + delta);
1506 MackieControlProtocol::right_release (Button &)
1512 MackieControlProtocol::channel_left_press (Button &)
1514 Sorted sorted = get_sorted_routes();
1515 if (sorted.size() > route_table.size()) {
1524 MackieControlProtocol::channel_left_release (Button &)
1530 MackieControlProtocol::channel_right_press (Button &)
1532 Sorted sorted = get_sorted_routes();
1533 if (sorted.size() > route_table.size()) {
1542 MackieControlProtocol::channel_right_release (Button &)
1547 /////////////////////////////////////
1549 /////////////////////////////////////
1551 MackieControlProtocol::marker_press (Button &)
1553 // cut'n'paste from LocationUI::add_new_location()
1555 framepos_t where = session->audible_frame();
1556 session->locations()->next_available_name(markername,"mcu");
1557 Location *location = new Location (*session, where, where, markername, Location::IsMark);
1558 session->begin_reversible_command (_("add marker"));
1559 XMLNode &before = session->locations()->get_state();
1560 session->locations()->add (location, true);
1561 XMLNode &after = session->locations()->get_state();
1562 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1563 session->commit_reversible_command ();
1568 MackieControlProtocol::marker_release (Button &)
1574 jog_wheel_state_display (JogWheel::State state, SurfacePort & port)
1577 case JogWheel::zoom:
1578 port.write (builder.two_char_display ("Zm"));
1580 case JogWheel::scroll:
1581 port.write (builder.two_char_display ("Sc"));
1583 case JogWheel::scrub:
1584 port.write (builder.two_char_display ("Sb"));
1586 case JogWheel::shuttle:
1587 port.write (builder.two_char_display ("Sh"));
1589 case JogWheel::speed:
1590 port.write (builder.two_char_display ("Sp"));
1592 case JogWheel::select:
1593 port.write (builder.two_char_display ("Se"));
1599 MackieControlProtocol::zoom_press (Mackie::Button &)
1601 _jog_wheel.zoom_state_toggle();
1602 update_global_button ("scrub", _jog_wheel.jog_wheel_state() == JogWheel::scrub);
1603 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1604 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1608 MackieControlProtocol::zoom_release (Mackie::Button &)
1610 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1614 MackieControlProtocol::scrub_press (Mackie::Button &)
1616 _jog_wheel.scrub_state_cycle();
1617 update_global_button ("zoom", _jog_wheel.jog_wheel_state() == JogWheel::zoom);
1618 jog_wheel_state_display (_jog_wheel.jog_wheel_state(), mcu_port());
1620 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1622 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1627 MackieControlProtocol::scrub_release (Mackie::Button &)
1630 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1632 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1637 MackieControlProtocol::drop_press (Button &)
1639 session->remove_last_capture();
1644 MackieControlProtocol::drop_release (Button &)
1650 MackieControlProtocol::save_press (Button &)
1652 session->save_state ("");
1657 MackieControlProtocol::save_release (Button &)
1663 MackieControlProtocol::timecode_beats_press (Button &)
1665 switch (_timecode_type) {
1666 case ARDOUR::AnyTime::BBT:
1667 _timecode_type = ARDOUR::AnyTime::Timecode;
1669 case ARDOUR::AnyTime::Timecode:
1670 _timecode_type = ARDOUR::AnyTime::BBT;
1674 os << "Unknown Anytime::Type " << _timecode_type;
1675 throw runtime_error (os.str());
1677 update_timecode_beats_led();
1682 MackieControlProtocol::timecode_beats_release (Button &)
1687 list<boost::shared_ptr<ARDOUR::Bundle> >
1688 MackieControlProtocol::bundles ()
1690 list<boost::shared_ptr<ARDOUR::Bundle> > b;
1691 b.push_back (_input_bundle);
1692 b.push_back (_output_bundle);
1697 MackieControlProtocol::port_connected_or_disconnected (string a, string b, bool connected)
1699 /* If something is connected to one of our output ports, send MIDI to update the surface
1700 to whatever state it should have.
1707 MackiePorts::const_iterator i = _ports.begin();
1708 while (i != _ports.end()) {
1710 string const n = AudioEngine::instance()->make_port_name_non_relative ((*i)->output_port().name ());
1712 if (a == n || b == n) {
1719 if (i != _ports.end ()) {
1725 MackieControlProtocol::do_request (MackieControlUIRequest* req)
1727 if (req->type == CallSlot) {
1729 call_slot (MISSING_INVALIDATOR, req->the_slot);
1731 } else if (req->type == Quit) {
1738 MackieControlProtocol::stop ()
1745 /** Add a timeout so that a control's in_use flag will be reset some time in the future.
1746 * @param in_use_control the control whose in_use flag to reset.
1747 * @param touch_control a touch control to emit an event for, or 0.
1750 MackieControlProtocol::add_in_use_timeout (SurfacePort& port, Control& in_use_control, Control* touch_control)
1752 Glib::RefPtr<Glib::TimeoutSource> timeout (Glib::TimeoutSource::create (250)); // milliseconds
1754 in_use_control.in_use_connection.disconnect ();
1755 in_use_control.in_use_connection = timeout->connect (
1756 sigc::bind (sigc::mem_fun (*this, &MackieControlProtocol::control_in_use_timeout), &port, &in_use_control, touch_control));
1757 in_use_control.in_use_touch_control = touch_control;
1759 timeout->attach (main_loop()->get_context());
1761 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("timeout queued for port %1, control %2 touch control %3\n",
1762 &port, &in_use_control, touch_control));}
1764 /** Handle timeouts to reset in_use for controls that can't
1765 * do this by themselves (e.g. pots, and faders without touch support).
1766 * @param in_use_control the control whose in_use flag to reset.
1767 * @param touch_control a touch control to emit an event for, or 0.
1770 MackieControlProtocol::control_in_use_timeout (SurfacePort* port, Control* in_use_control, Control* touch_control)
1772 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("timeout elapsed for port %1, control %2 touch control %3\n",
1773 port, in_use_control, touch_control));
1775 in_use_control->set_in_use (false);
1777 if (touch_control) {
1778 // empty control_state
1779 ControlState control_state;
1780 handle_control_event (*port, *touch_control, control_state);
1783 // only call this method once from the timer