2 Copyright (C) 2006,2007 John Anderson
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define __STDC_FORMAT_MACROS
33 #include <boost/shared_array.hpp>
35 #include <midi++/types.h>
36 #include <midi++/port.h>
37 #include <midi++/manager.h>
38 #include <pbd/pthread_utils.h>
39 #include <pbd/error.h>
40 #include <pbd/memento_command.h>
41 #include <pbd/convert.h>
43 #include <ardour/route.h>
44 #include <ardour/session.h>
45 #include <ardour/location.h>
46 #include <ardour/dB.h>
47 #include <ardour/panner.h>
48 #include <ardour/tempo.h>
49 #include <ardour/types.h>
51 #include "mackie_control_protocol.h"
53 #include "midi_byte_array.h"
54 #include "mackie_control_exception.h"
55 #include "route_signal.h"
56 #include "mackie_midi_builder.h"
57 #include "surface_port.h"
59 #include "bcf_surface.h"
60 #include "mackie_surface.h"
62 using namespace ARDOUR;
65 using namespace Mackie;
68 using boost::shared_ptr;
72 MackieMidiBuilder builder;
74 MackieControlProtocol::MackieControlProtocol (Session& session)
75 : ControlProtocol (session, X_("Mackie"))
76 , _current_initial_bank( 0 )
77 , connections_back( _connections )
79 , _ports_changed( false )
84 , _timecode_type( ARDOUR::AnyTime::BBT )
87 cout << "MackieControlProtocol::MackieControlProtocol" << endl;
89 // will start reading from ports, as soon as there are some
90 pthread_create_and_store (X_("mackie monitor"), &thread, 0, _monitor_work, this);
93 MackieControlProtocol::~MackieControlProtocol()
96 cout << "~MackieControlProtocol::MackieControlProtocol" << endl;
102 catch ( exception & e )
104 cout << "~MackieControlProtocol caught " << e.what() << endl;
108 cout << "~MackieControlProtocol caught unknown" << endl;
111 cout << "finished ~MackieControlProtocol::MackieControlProtocol" << endl;
115 Mackie::Surface & MackieControlProtocol::surface()
119 throw MackieControlException( "_surface is 0 in MackieControlProtocol::surface" );
124 const Mackie::SurfacePort & MackieControlProtocol::mcu_port() const
126 if ( _ports.size() < 1 )
132 return dynamic_cast<const MackiePort &>( *_ports[0] );
136 Mackie::SurfacePort & MackieControlProtocol::mcu_port()
138 if ( _ports.size() < 1 )
144 return dynamic_cast<MackiePort &>( *_ports[0] );
148 // go to the previous track.
149 // Assume that get_sorted_routes().size() > route_table.size()
150 void MackieControlProtocol::prev_track()
152 if ( _current_initial_bank >= 1 )
154 session->set_dirty();
155 switch_banks( _current_initial_bank - 1 );
159 // go to the next track.
160 // Assume that get_sorted_routes().size() > route_table.size()
161 void MackieControlProtocol::next_track()
163 Sorted sorted = get_sorted_routes();
164 if ( _current_initial_bank + route_table.size() < sorted.size() )
166 session->set_dirty();
167 switch_banks( _current_initial_bank + 1 );
171 void MackieControlProtocol::clear_route_signals()
173 for( RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it )
177 route_signals.clear();
180 // return the port for a given id - 0 based
181 // throws an exception if no port found
182 MackiePort & MackieControlProtocol::port_for_id( uint32_t index )
184 uint32_t current_max = 0;
185 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
187 current_max += (*it)->strips();
188 if ( index < current_max ) return **it;
191 // oops - no matching port
193 os << "No port for index " << index;
194 throw MackieControlException( os.str() );
197 // predicate for sort call in get_sorted_routes
198 struct RouteByRemoteId
200 bool operator () ( const shared_ptr<Route> & a, const shared_ptr<Route> & b ) const
202 return a->remote_control_id() < b->remote_control_id();
205 bool operator () ( const Route & a, const Route & b ) const
207 return a.remote_control_id() < b.remote_control_id();
210 bool operator () ( const Route * a, const Route * b ) const
212 return a->remote_control_id() < b->remote_control_id();
216 MackieControlProtocol::Sorted MackieControlProtocol::get_sorted_routes()
221 boost::shared_ptr<RouteList> routes = session->get_routes();
222 set<uint32_t> remote_ids;
224 // routes with remote_id 0 should never be added
225 // TODO verify this with ardour devs
226 // remote_ids.insert( 0 );
228 // sort in remote_id order, and exclude master, control and hidden routes
229 // and any routes that are already set.
230 for (RouteList::iterator it = routes->begin(); it != routes->end(); ++it )
232 Route & route = **it;
235 && !route.is_master()
236 && !route.is_hidden()
237 && !route.is_control()
238 && remote_ids.find( route.remote_control_id() ) == remote_ids.end()
241 sorted.push_back( *it );
242 remote_ids.insert( route.remote_control_id() );
245 sort( sorted.begin(), sorted.end(), RouteByRemoteId() );
249 void MackieControlProtocol::refresh_current_bank()
251 switch_banks( _current_initial_bank );
254 void MackieControlProtocol::switch_banks( int initial )
256 // DON'T prevent bank switch if initial == _current_initial_bank
257 // because then this method can't be used as a refresh
260 Sorted sorted = get_sorted_routes();
261 int delta = sorted.size() - route_table.size();
262 if ( initial < 0 || ( delta > 0 && initial > delta ) )
265 cout << "not switching to " << initial << endl;
269 _current_initial_bank = initial;
271 // first clear the signals from old routes
272 // taken care of by the RouteSignal destructors
273 clear_route_signals();
275 // now set the signals for new routes
276 if ( _current_initial_bank <= sorted.size() )
278 // fetch the bank start and end to switch to
279 uint32_t end_pos = min( route_table.size(), sorted.size() );
280 Sorted::iterator it = sorted.begin() + _current_initial_bank;
281 Sorted::iterator end = sorted.begin() + _current_initial_bank + end_pos;
283 cout << "switch to " << _current_initial_bank << ", " << end_pos << endl;
286 // link routes to strips
288 for ( ; it != end && it != sorted.end(); ++it, ++i )
290 boost::shared_ptr<Route> route = *it;
291 Strip & strip = *surface().strips[i];
293 cout << "remote id " << route->remote_control_id() << " connecting " << route->name() << " to " << strip.name() << " with port " << port_for_id(i) << endl;
295 route_table[i] = route;
296 RouteSignal * rs = new RouteSignal (route, *this, strip, port_for_id(i) );
297 route_signals.push_back( rs );
298 // update strip from route
302 // create dead strips if there aren't enough routes to
304 for ( ; i < route_table.size(); ++i )
306 Strip & strip = *surface().strips[i];
307 // send zero for this strip
308 MackiePort & port = port_for_id(i);
309 port.write( builder.zero_strip( port, strip ) );
313 // display the current start bank.
314 surface().display_bank_start( mcu_port(), builder, _current_initial_bank );
317 void MackieControlProtocol::zero_all()
319 // TODO turn off SMPTE displays
322 for ( Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it )
324 MackiePort & port = port_for_id( (*it)->index() );
325 port.write( builder.zero_strip( port, **it ) );
328 // and the master strip
329 mcu_port().write( builder.zero_strip( dynamic_cast<MackiePort&>( mcu_port() ), master_strip() ) );
331 // turn off global buttons and leds
332 // global buttons are only ever on mcu_port, so we don't have
333 // to figure out which port.
334 for ( Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it )
336 Control & control = **it;
337 if ( !control.group().is_strip() && control.accepts_feedback() )
339 mcu_port().write( builder.zero_control( control ) );
343 // any hardware-specific stuff
344 surface().zero_all( mcu_port(), builder );
347 int MackieControlProtocol::set_active( bool yn )
353 // the reason for the locking and unlocking is that
354 // glibmm can't do a condition wait on a RecMutex
357 // TODO what happens if this fails half way?
359 // create MackiePorts
361 Glib::Mutex::Lock lock( update_mutex );
365 // make sure the ports are being listened to
368 // wait until poll thread is running, with ports to poll
369 // the mutex is only there because conditions require a mutex
371 Glib::Mutex::Lock lock( update_mutex );
372 while ( nfds == 0 ) update_cond.wait( update_mutex );
375 // now initialise MackiePorts - ie exchange sysex messages
376 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
381 // wait until all ports are active
382 // TODO a more sophisticated approach would
383 // allow things to start up with only an MCU, even if
384 // extenders were specified but not responding.
385 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
387 (*it)->wait_for_init();
390 // create surface object. This depends on the ports being
391 // correctly initialised
392 initialize_surface();
393 connect_session_signals();
398 // send current control positions to surface
399 // must come after _active = true otherwise it won't run
408 catch( exception & e )
411 cout << "set_active to false because exception caught: " << e.what() << endl;
421 bool MackieControlProtocol::handle_strip_button( Control & control, ButtonState bs, boost::shared_ptr<Route> route )
427 if ( control.name() == "recenable" )
429 state = !route->record_enabled();
430 route->set_record_enable( state, this );
432 else if ( control.name() == "mute" )
434 state = !route->muted();
435 route->set_mute( state, this );
437 else if ( control.name() == "solo" )
439 state = !route->soloed();
440 route->set_solo( state, this );
442 else if ( control.name() == "select" )
444 // TODO make the track selected. Whatever that means.
445 //state = default_button_press( dynamic_cast<Button&>( control ) );
447 else if ( control.name() == "vselect" )
449 // TODO could be used to select different things to apply the pot to?
450 //state = default_button_press( dynamic_cast<Button&>( control ) );
454 if ( control.name() == "fader_touch" )
457 control.strip().gain().in_use( state );
463 void MackieControlProtocol::update_led( Mackie::Button & button, Mackie::LedState ls )
467 SurfacePort * port = 0;
468 if ( button.group().is_strip() )
470 if ( button.group().is_master() )
476 port = &port_for_id( dynamic_cast<const Strip&>( button.group() ).index() );
483 port->write( builder.build_led( button, ls ) );
487 void MackieControlProtocol::update_smpte_beats_led()
489 switch ( _timecode_type )
491 case ARDOUR::AnyTime::BBT:
492 update_global_led( "beats", on );
493 update_global_led( "smpte", off );
495 case ARDOUR::AnyTime::SMPTE:
496 update_global_led( "smpte", on );
497 update_global_led( "beats", off );
501 os << "Unknown Anytime::Type " << _timecode_type;
502 throw runtime_error( os.str() );
506 void MackieControlProtocol::update_global_button( const string & name, LedState ls )
508 if ( surface().controls_by_name.find( name ) != surface().controls_by_name.end() )
510 Button * button = dynamic_cast<Button*>( surface().controls_by_name[name] );
511 mcu_port().write( builder.build_led( button->led(), ls ) );
516 cout << "Button " << name << " not found" << endl;
521 void MackieControlProtocol::update_global_led( const string & name, LedState ls )
523 if ( surface().controls_by_name.find( name ) != surface().controls_by_name.end() )
525 Led * led = dynamic_cast<Led*>( surface().controls_by_name[name] );
526 mcu_port().write( builder.build_led( *led, ls ) );
531 cout << "Led " << name << " not found" << endl;
536 // send messages to surface to set controls to correct values
537 void MackieControlProtocol::update_surface()
541 // do the initial bank switch to connect signals
542 // _current_initial_bank is initialised by set_state
543 switch_banks( _current_initial_bank );
545 // create a RouteSignal for the master route
547 boost::shared_ptr<Route> mr = master_route ();
549 master_route_signal = shared_ptr<RouteSignal> (new RouteSignal (mr, *this, master_strip(), mcu_port()) );
550 // update strip from route
551 master_route_signal->notify_all();
554 // sometimes the jog wheel is a pot
555 surface().blank_jog_ring( mcu_port(), builder );
557 // update global buttons and displays
558 notify_record_state_changed();
559 notify_transport_state_changed();
560 update_smpte_beats_led();
564 void MackieControlProtocol::connect_session_signals()
566 // receive routes added
567 connections_back = session->RouteAdded.connect( ( mem_fun (*this, &MackieControlProtocol::notify_route_added) ) );
568 // receive record state toggled
569 connections_back = session->RecordStateChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_record_state_changed) ) );
570 // receive transport state changed
571 connections_back = session->TransportStateChange.connect( ( mem_fun (*this, &MackieControlProtocol::notify_transport_state_changed) ) );
572 // receive punch-in and punch-out
573 connections_back = Config->ParameterChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_parameter_changed) ) );
574 session->config.ParameterChanged.connect ( ( mem_fun (*this, &MackieControlProtocol::notify_parameter_changed) ) );
575 // receive rude solo changed
576 connections_back = session->SoloActive.connect( ( mem_fun (*this, &MackieControlProtocol::notify_solo_active_changed) ) );
578 // make sure remote id changed signals reach here
579 // see also notify_route_added
580 Sorted sorted = get_sorted_routes();
581 for ( Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it )
583 connections_back = (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
587 void MackieControlProtocol::add_port( MIDI::Port & midi_port, int number )
590 cout << "add port " << midi_port.name() << ", " << midi_port.device() << ", " << midi_port.type() << endl;
591 cout << "MIDI::Port::ALSA_Sequencer " << MIDI::Port::ALSA_Sequencer << endl;
592 cout << "MIDI::Port::Unknown " << MIDI::Port::Unknown << endl;
594 if ( string( midi_port.device() ) == string( "ardour" ) )
596 throw MackieControlException( "The Mackie MCU driver will not use a port with device=ardour" );
598 else if ( midi_port.type() == MIDI::Port::ALSA_Sequencer )
600 throw MackieControlException( "alsa/sequencer ports don't work with the Mackie MCU driver right now" );
604 MackiePort * sport = new MackiePort( *this, midi_port, number );
605 _ports.push_back( sport );
607 connections_back = sport->init_event.connect(
609 mem_fun (*this, &MackieControlProtocol::handle_port_init)
614 connections_back = sport->active_event.connect(
616 mem_fun (*this, &MackieControlProtocol::handle_port_active)
621 connections_back = sport->inactive_event.connect(
623 mem_fun (*this, &MackieControlProtocol::handle_port_inactive)
628 _ports_changed = true;
632 void MackieControlProtocol::create_ports()
634 MIDI::Manager * mm = MIDI::Manager::instance();
638 MIDI::Port * midi_port = mm->port( default_port_name );
640 if ( midi_port == 0 ) {
642 os << string_compose( _("no MIDI port named \"%1\" exists - Mackie control disabled"), default_port_name );
643 error << os.str() << endmsg;
644 throw MackieControlException( os.str() );
646 add_port( *midi_port, 0 );
649 // open extender ports. Up to 9. Should be enough.
650 // could also use mm->get_midi_ports()
651 string ext_port_base = "mcu_xt_";
652 for ( int index = 1; index <= 9; ++index )
655 os << ext_port_base << index;
656 MIDI::Port * midi_port = mm->port( os.str() );
657 if ( midi_port != 0 ) add_port( *midi_port, index );
661 shared_ptr<Route> MackieControlProtocol::master_route()
663 boost::shared_ptr<IO> mo = session->master_out ();
664 return boost::dynamic_pointer_cast<Route>(mo);
667 Strip & MackieControlProtocol::master_strip()
669 return dynamic_cast<Strip&>( *surface().groups["master"] );
672 void MackieControlProtocol::initialize_surface()
674 // set up the route table
676 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
678 strips += (*it)->strips();
681 set_route_table_size( strips );
683 // TODO same as code in mackie_port.cc
684 string emulation = ARDOUR::Config->get_mackie_emulation();
685 if ( emulation == "bcf" )
687 _surface = new BcfSurface( strips );
689 else if ( emulation == "mcu" )
691 _surface = new MackieSurface( strips );
696 os << "no Surface class found for emulation: " << emulation;
697 throw MackieControlException( os.str() );
702 // Connect events. Must be after route table otherwise there will be trouble
703 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
705 connections_back = (*it)->control_event.connect( ( mem_fun (*this, &MackieControlProtocol::handle_control_event) ) );
709 void MackieControlProtocol::close()
711 // stop polling, and wait for it...
712 // must be before other shutdown otherwise polling loop
713 // calls methods on objects that are deleted
715 pthread_join( thread, 0 );
717 // TODO disconnect port active/inactive signals
718 // Or at least put a lock here
720 // disconnect global signals from Session
721 // TODO Since *this is a sigc::trackable, this shouldn't be necessary
722 // but it is for some reason
724 for( vector<sigc::connection>::iterator it = _connections.begin(); it != _connections.end(); ++it )
732 // These will fail if the port has gone away.
733 // So catch the exception and do the rest of the
735 // because the bcf doesn't respond to the next 3 sysex messages
740 catch ( exception & e )
743 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
747 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
751 MackiePort & port = **it;
753 port.write_sysex( 0x61 );
755 port.write_sysex( 0x62 );
756 // Reset (reboot into offline mode)
757 port.write_sysex( 0x63 );
759 catch ( exception & e )
762 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
767 // disconnect routes from strips
768 clear_route_signals();
774 // shut down MackiePorts
775 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
781 // this is done already in monitor_work. But it's here so we know.
787 void* MackieControlProtocol::_monitor_work (void* arg)
789 return static_cast<MackieControlProtocol*>(arg)->monitor_work ();
792 XMLNode & MackieControlProtocol::get_state()
795 cout << "MackieControlProtocol::get_state" << endl;
798 // add name of protocol
799 XMLNode* node = new XMLNode( X_("Protocol") );
800 node->add_property( X_("name"), _name );
804 os << _current_initial_bank;
805 node->add_property( X_("bank"), os.str() );
810 int MackieControlProtocol::set_state( const XMLNode & node )
813 cout << "MackieControlProtocol::set_state: active " << _active << endl;
817 // fetch current bank
818 if ( node.property( X_("bank") ) != 0 )
820 string bank = node.property( X_("bank") )->value();
824 uint32_t new_bank = atoi( bank.c_str() );
825 if ( _current_initial_bank != new_bank ) switch_banks( new_bank );
827 catch ( exception & e )
830 cout << "exception in MackieControlProtocol::set_state: " << e.what() << endl;
839 void MackieControlProtocol::handle_control_event( SurfacePort & port, Control & control, const ControlState & state )
841 // find the route for the control, if there is one
842 boost::shared_ptr<Route> route;
843 if ( control.group().is_strip() )
845 if ( control.group().is_master() )
847 route = master_route();
851 uint32_t index = control.ordinal() - 1 + ( port.number() * port.strips() );
852 if ( index < route_table.size() )
853 route = route_table[index];
855 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
859 // This handles control element events from the surface
860 // the state of the controls on the surface is usually updated
862 switch ( control.type() )
864 case Control::type_fader:
865 // find the route in the route table for the id
866 // if the route isn't available, skip it
867 // at which point the fader should just reset itself
870 route->gain_control()->set_value( state.pos );
872 // must echo bytes back to slider now, because
873 // the notifier only works if the fader is not being
874 // touched. Which it is if we're getting input.
875 port.write( builder.build_fader( (Fader&)control, state.pos ) );
879 case Control::type_button:
880 if ( control.group().is_strip() )
885 handle_strip_button( control, state.button_state, route );
889 // no route so always switch the light off
890 // because no signals will be emitted by a non-route
891 port.write( builder.build_led( control.led(), off ) );
894 else if ( control.group().is_master() )
896 // master fader touch
899 handle_strip_button( control, state.button_state, route );
904 // handle all non-strip buttons
905 surface().handle_button( *this, state.button_state, dynamic_cast<Button&>( control ) );
909 // pot (jog wheel, external control)
910 case Control::type_pot:
911 if ( control.group().is_strip() )
913 if ( route != 0 && route->panner() )
915 // pan for mono input routes, or stereo linked panners
916 if ( route->panner()->npanners() == 1 || ( route->panner()->npanners() == 2 && route->panner()->linked() ) )
918 // assume pan for now
920 route->panner()->streampanner (0).get_effective_position (xpos);
922 // calculate new value, and trim
923 xpos += state.delta * state.sign;
926 else if ( xpos < 0.0 )
929 route->panner()->streampanner (0).set_position( xpos );
934 // it's a pot for an umnapped route, so turn all the lights off
935 port.write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
940 if ( control.is_jog() )
942 _jog_wheel.jog_event( port, control, state );
946 cout << "external controller" << state.ticks * state.sign << endl;
952 cout << "Control::type not handled: " << control.type() << endl;
956 /////////////////////////////////////////////////
957 // handlers for Route signals
958 // TODO should these be part of RouteSignal?
959 // They started off as sigc handlers for signals
960 // from Route, but they're also used in polling for automation
961 /////////////////////////////////////////////////
963 void MackieControlProtocol::notify_solo_changed( RouteSignal * route_signal )
967 Button & button = route_signal->strip().solo();
968 route_signal->port().write( builder.build_led( button, route_signal->route()->soloed() ) );
970 catch( exception & e )
972 cout << e.what() << endl;
976 void MackieControlProtocol::notify_mute_changed( RouteSignal * route_signal )
980 Button & button = route_signal->strip().mute();
981 route_signal->port().write( builder.build_led( button, route_signal->route()->muted() ) );
983 catch( exception & e )
985 cout << e.what() << endl;
989 void MackieControlProtocol::notify_record_enable_changed( RouteSignal * route_signal )
993 Button & button = route_signal->strip().recenable();
994 route_signal->port().write( builder.build_led( button, route_signal->route()->record_enabled() ) );
996 catch( exception & e )
998 cout << e.what() << endl;
1002 void MackieControlProtocol::notify_active_changed( RouteSignal * route_signal )
1007 cout << "MackieControlProtocol::notify_active_changed" << endl;
1009 refresh_current_bank();
1011 catch( exception & e )
1013 cout << e.what() << endl;
1017 void MackieControlProtocol::notify_gain_changed( RouteSignal * route_signal, bool force_update )
1021 Fader & fader = route_signal->strip().gain();
1022 if ( !fader.in_use() )
1024 float gain_value = route_signal->route()->gain_control()->get_value();
1025 // check that something has actually changed
1026 if ( force_update || gain_value != route_signal->last_gain_written() )
1028 route_signal->port().write( builder.build_fader( fader, gain_value ) );
1029 route_signal->last_gain_written( gain_value );
1033 catch( exception & e )
1035 cout << e.what() << endl;
1039 void MackieControlProtocol::notify_name_changed( RouteSignal * route_signal )
1043 Strip & strip = route_signal->strip();
1044 if ( !strip.is_master() )
1047 string fullname = route_signal->route()->name();
1049 if ( fullname.length() <= 6 )
1055 line1 = PBD::short_version( fullname, 6 );
1058 SurfacePort & port = route_signal->port();
1059 port.write( builder.strip_display( port, strip, 0, line1 ) );
1060 port.write( builder.strip_display_blank( port, strip, 1 ) );
1063 catch( exception & e )
1065 cout << e.what() << endl;
1069 void MackieControlProtocol::notify_panner_changed( RouteSignal * route_signal, bool force_update )
1073 Pot & pot = route_signal->strip().vpot();
1074 boost::shared_ptr<Panner> panner = route_signal->route()->panner();
1075 if ( panner && panner->npanners() == 1 || ( panner->npanners() == 2 && panner->linked() ) )
1078 route_signal->route()->panner()->streampanner(0).get_effective_position( pos );
1080 // cache the MidiByteArray here, because the mackie led control is much lower
1081 // resolution than the panner control. So we save lots of byte
1082 // sends in spite of more work on the comparison
1083 MidiByteArray bytes = builder.build_led_ring( pot, ControlState( on, pos ), MackieMidiBuilder::midi_pot_mode_dot );
1084 // check that something has actually changed
1085 if ( force_update || bytes != route_signal->last_pan_written() )
1087 route_signal->port().write( bytes );
1088 route_signal->last_pan_written( bytes );
1093 route_signal->port().write( builder.zero_control( pot ) );
1096 catch( exception & e )
1098 cout << e.what() << endl;
1102 // TODO handle plugin automation polling
1103 void MackieControlProtocol::update_automation( RouteSignal & rs )
1105 ARDOUR::AutoState gain_state = rs.route()->gain_control()->automation_state();
1106 if ( gain_state == Touch || gain_state == Play )
1108 notify_gain_changed( &rs, false );
1111 if ( rs.route()->panner() ) {
1112 ARDOUR::AutoState panner_state = rs.route()->panner()->automation_state();
1113 if ( panner_state == Touch || panner_state == Play )
1115 notify_panner_changed( &rs, false );
1118 _automation_last.start();
1121 string MackieControlProtocol::format_bbt_timecode( nframes_t now_frame )
1124 session->bbt_time( now_frame, bbt_time );
1126 // According to the Logic docs
1127 // digits: 888/88/88/888
1128 // BBT mode: Bars/Beats/Subdivisions/Ticks
1130 os << setw(3) << setfill('0') << bbt_time.bars;
1131 os << setw(2) << setfill('0') << bbt_time.beats;
1133 // figure out subdivisions per beat
1134 const Meter & meter = session->tempo_map().meter_at( now_frame );
1136 if ( meter.note_divisor() == 8 && (meter.beats_per_bar() == 12.0 || meter.beats_per_bar() == 9.0 || meter.beats_per_bar() == 6.0) )
1141 uint32_t subdivisions = bbt_time.ticks / uint32_t( Meter::ticks_per_beat / subdiv );
1142 uint32_t ticks = bbt_time.ticks % uint32_t( Meter::ticks_per_beat / subdiv );
1144 os << setw(2) << setfill('0') << subdivisions + 1;
1145 os << setw(3) << setfill('0') << ticks;
1150 string MackieControlProtocol::format_smpte_timecode( nframes_t now_frame )
1153 session->smpte_time( now_frame, smpte );
1155 // According to the Logic docs
1156 // digits: 888/88/88/888
1157 // SMPTE mode: Hours/Minutes/Seconds/Frames
1159 os << setw(3) << setfill('0') << smpte.hours;
1160 os << setw(2) << setfill('0') << smpte.minutes;
1161 os << setw(2) << setfill('0') << smpte.seconds;
1162 os << setw(3) << setfill('0') << smpte.frames;
1167 void MackieControlProtocol::update_timecode_display()
1169 if ( surface().has_timecode_display() )
1171 // do assignment here so current_frame is fixed
1172 nframes_t current_frame = session->transport_frame();
1175 switch ( _timecode_type )
1177 case ARDOUR::AnyTime::BBT:
1178 timecode = format_bbt_timecode( current_frame );
1180 case ARDOUR::AnyTime::SMPTE:
1181 timecode = format_smpte_timecode( current_frame );
1185 os << "Unknown timecode: " << _timecode_type;
1186 throw runtime_error( os.str() );
1189 // only write the timecode string to the MCU if it's changed
1190 // since last time. This is to reduce midi bandwidth used.
1191 if ( timecode != _timecode_last )
1193 surface().display_timecode( mcu_port(), builder, timecode, _timecode_last );
1194 _timecode_last = timecode;
1199 void MackieControlProtocol::poll_session_data()
1201 if ( _active && _automation_last.elapsed() >= 20 )
1203 // do all currently mapped routes
1204 for( RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it )
1206 update_automation( **it );
1209 // and the master strip
1210 if ( master_route_signal != 0 )
1212 update_automation( *master_route_signal );
1215 update_timecode_display();
1217 _automation_last.start();
1221 /////////////////////////////////////
1222 // Transport Buttons
1223 /////////////////////////////////////
1225 LedState MackieControlProtocol::frm_left_press( Button & button )
1227 // can use first_mark_before/after as well
1228 unsigned long elapsed = _frm_left_last.restart();
1230 Location * loc = session->locations()->first_location_before (
1231 session->transport_frame()
1234 // allow a quick double to go past a previous mark
1235 if ( session->transport_rolling() && elapsed < 500 && loc != 0 )
1237 Location * loc_two_back = session->locations()->first_location_before ( loc->start() );
1238 if ( loc_two_back != 0 )
1244 // move to the location, if it's valid
1247 session->request_locate( loc->start(), session->transport_rolling() );
1253 LedState MackieControlProtocol::frm_left_release( Button & button )
1258 LedState MackieControlProtocol::frm_right_press( Button & button )
1260 // can use first_mark_before/after as well
1261 Location * loc = session->locations()->first_location_after (
1262 session->transport_frame()
1264 if ( loc != 0 ) session->request_locate( loc->start(), session->transport_rolling() );
1268 LedState MackieControlProtocol::frm_right_release( Button & button )
1273 LedState MackieControlProtocol::stop_press( Button & button )
1275 session->request_stop();
1279 LedState MackieControlProtocol::stop_release( Button & button )
1281 return session->transport_stopped();
1284 LedState MackieControlProtocol::play_press( Button & button )
1286 session->request_transport_speed( 1.0 );
1290 LedState MackieControlProtocol::play_release( Button & button )
1292 return session->transport_rolling();
1295 LedState MackieControlProtocol::record_press( Button & button )
1297 if ( session->get_record_enabled() )
1298 session->disable_record( false );
1300 session->maybe_enable_record();
1304 LedState MackieControlProtocol::record_release( Button & button )
1306 if ( session->get_record_enabled() )
1308 if ( session->transport_rolling() )
1317 LedState MackieControlProtocol::rewind_press( Button & button )
1319 _jog_wheel.push( JogWheel::speed );
1320 _jog_wheel.transport_direction( -1 );
1321 session->request_transport_speed( -_jog_wheel.transport_speed() );
1325 LedState MackieControlProtocol::rewind_release( Button & button )
1328 _jog_wheel.transport_direction( 0 );
1329 if ( _transport_previously_rolling )
1330 session->request_transport_speed( 1.0 );
1332 session->request_stop();
1336 LedState MackieControlProtocol::ffwd_press( Button & button )
1338 _jog_wheel.push( JogWheel::speed );
1339 _jog_wheel.transport_direction( 1 );
1340 session->request_transport_speed( _jog_wheel.transport_speed() );
1344 LedState MackieControlProtocol::ffwd_release( Button & button )
1347 _jog_wheel.transport_direction( 0 );
1348 if ( _transport_previously_rolling )
1349 session->request_transport_speed( 1.0 );
1351 session->request_stop();
1355 LedState MackieControlProtocol::loop_press( Button & button )
1357 session->request_play_loop( !session->get_play_loop() );
1361 LedState MackieControlProtocol::loop_release( Button & button )
1363 return session->get_play_loop();
1366 LedState MackieControlProtocol::punch_in_press( Button & button )
1368 bool state = !session->config.get_punch_in();
1369 session->config.set_punch_in( state );
1373 LedState MackieControlProtocol::punch_in_release( Button & button )
1375 return session->config.get_punch_in();
1378 LedState MackieControlProtocol::punch_out_press( Button & button )
1380 bool state = !session->config.get_punch_out();
1381 session->config.set_punch_out( state );
1385 LedState MackieControlProtocol::punch_out_release( Button & button )
1387 return session->config.get_punch_out();
1390 LedState MackieControlProtocol::home_press( Button & button )
1392 session->goto_start();
1396 LedState MackieControlProtocol::home_release( Button & button )
1401 LedState MackieControlProtocol::end_press( Button & button )
1403 session->goto_end();
1407 LedState MackieControlProtocol::end_release( Button & button )
1412 LedState MackieControlProtocol::clicking_press( Button & button )
1414 bool state = !Config->get_clicking();
1415 Config->set_clicking( state );
1419 LedState MackieControlProtocol::clicking_release( Button & button )
1421 return Config->get_clicking();
1424 LedState MackieControlProtocol::global_solo_press( Button & button )
1426 bool state = !session->soloing();
1427 session->set_all_solo ( state );
1431 LedState MackieControlProtocol::global_solo_release( Button & button )
1433 return session->soloing();
1436 ///////////////////////////////////////////
1438 ///////////////////////////////////////////
1440 void MackieControlProtocol::notify_parameter_changed (std::string const & p)
1442 if ( p == "punch-in" )
1444 update_global_button( "punch_in", session->config.get_punch_in() );
1446 else if ( p == "punch-out" )
1448 update_global_button( "punch_out", session->config.get_punch_out() );
1450 else if ( p == "clicking" )
1452 update_global_button( "clicking", Config->get_clicking() );
1457 cout << "parameter changed: " << p << endl;
1462 // RouteList is the set of routes that have just been added
1463 void MackieControlProtocol::notify_route_added( ARDOUR::RouteList & rl )
1465 // currently assigned banks are less than the full set of
1466 // strips, so activate the new strip now.
1467 if ( route_signals.size() < route_table.size() )
1469 refresh_current_bank();
1471 // otherwise route added, but current bank needs no updating
1473 // make sure remote id changes in the new route are handled
1474 typedef ARDOUR::RouteList ARS;
1475 for ( ARS::iterator it = rl.begin(); it != rl.end(); ++it )
1477 connections_back = (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
1481 void MackieControlProtocol::notify_solo_active_changed( bool active )
1483 Button * rude_solo = reinterpret_cast<Button*>( surface().controls_by_name["solo"] );
1484 mcu_port().write( builder.build_led( *rude_solo, active ? flashing : off ) );
1487 void MackieControlProtocol::notify_remote_id_changed()
1489 Sorted sorted = get_sorted_routes();
1491 // if a remote id has been moved off the end, we need to shift
1492 // the current bank backwards.
1493 if ( sorted.size() - _current_initial_bank < route_signals.size() )
1495 // but don't shift backwards past the zeroth channel
1496 switch_banks( max((Sorted::size_type) 0, sorted.size() - route_signals.size() ) );
1498 // Otherwise just refresh the current bank
1501 refresh_current_bank();
1505 ///////////////////////////////////////////
1506 // Transport signals
1507 ///////////////////////////////////////////
1509 void MackieControlProtocol::notify_record_state_changed()
1511 // switch rec button on / off / flashing
1512 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1513 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1516 void MackieControlProtocol::notify_transport_state_changed()
1518 // switch various play and stop buttons on / off
1519 update_global_button( "play", session->transport_rolling() );
1520 update_global_button( "stop", !session->transport_rolling() );
1521 update_global_button( "loop", session->get_play_loop() );
1523 _transport_previously_rolling = session->transport_rolling();
1525 // rec is special because it's tristate
1526 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1527 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1530 /////////////////////////////////////
1532 /////////////////////////////////////
1533 LedState MackieControlProtocol::left_press( Button & button )
1535 Sorted sorted = get_sorted_routes();
1536 if ( sorted.size() > route_table.size() )
1538 int new_initial = _current_initial_bank - route_table.size();
1539 if ( new_initial < 0 ) new_initial = 0;
1540 if ( new_initial != int( _current_initial_bank ) )
1542 session->set_dirty();
1543 switch_banks( new_initial );
1554 LedState MackieControlProtocol::left_release( Button & button )
1559 LedState MackieControlProtocol::right_press( Button & button )
1561 Sorted sorted = get_sorted_routes();
1562 if ( sorted.size() > route_table.size() )
1564 uint32_t delta = sorted.size() - ( route_table.size() + _current_initial_bank );
1565 if ( delta > route_table.size() ) delta = route_table.size();
1568 session->set_dirty();
1569 switch_banks( _current_initial_bank + delta );
1580 LedState MackieControlProtocol::right_release( Button & button )
1585 LedState MackieControlProtocol::channel_left_press( Button & button )
1587 Sorted sorted = get_sorted_routes();
1588 if ( sorted.size() > route_table.size() )
1599 LedState MackieControlProtocol::channel_left_release( Button & button )
1604 LedState MackieControlProtocol::channel_right_press( Button & button )
1606 Sorted sorted = get_sorted_routes();
1607 if ( sorted.size() > route_table.size() )
1618 LedState MackieControlProtocol::channel_right_release( Button & button )
1623 /////////////////////////////////////
1625 /////////////////////////////////////
1626 LedState MackieControlProtocol::marker_press( Button & button )
1628 // cut'n'paste from LocationUI::add_new_location()
1630 nframes_t where = session->audible_frame();
1631 session->locations()->next_available_name(markername,"mcu");
1632 Location *location = new Location (where, where, markername, Location::IsMark);
1633 session->begin_reversible_command (_("add marker"));
1634 XMLNode &before = session->locations()->get_state();
1635 session->locations()->add (location, true);
1636 XMLNode &after = session->locations()->get_state();
1637 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1638 session->commit_reversible_command ();
1642 LedState MackieControlProtocol::marker_release( Button & button )
1647 void jog_wheel_state_display( JogWheel::State state, SurfacePort & port )
1651 case JogWheel::zoom: port.write( builder.two_char_display( "Zm" ) ); break;
1652 case JogWheel::scroll: port.write( builder.two_char_display( "Sc" ) ); break;
1653 case JogWheel::scrub: port.write( builder.two_char_display( "Sb" ) ); break;
1654 case JogWheel::shuttle: port.write( builder.two_char_display( "Sh" ) ); break;
1655 case JogWheel::speed: port.write( builder.two_char_display( "Sp" ) ); break;
1656 case JogWheel::select: port.write( builder.two_char_display( "Se" ) ); break;
1660 Mackie::LedState MackieControlProtocol::zoom_press( Mackie::Button & )
1662 _jog_wheel.zoom_state_toggle();
1663 update_global_button( "scrub", _jog_wheel.jog_wheel_state() == JogWheel::scrub );
1664 jog_wheel_state_display( _jog_wheel.jog_wheel_state(), mcu_port() );
1665 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1668 Mackie::LedState MackieControlProtocol::zoom_release( Mackie::Button & )
1670 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1673 Mackie::LedState MackieControlProtocol::scrub_press( Mackie::Button & )
1675 _jog_wheel.scrub_state_cycle();
1676 update_global_button( "zoom", _jog_wheel.jog_wheel_state() == JogWheel::zoom );
1677 jog_wheel_state_display( _jog_wheel.jog_wheel_state(), mcu_port() );
1679 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1681 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1685 Mackie::LedState MackieControlProtocol::scrub_release( Mackie::Button & )
1688 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1690 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1694 LedState MackieControlProtocol::drop_press( Button & button )
1696 session->remove_last_capture();
1700 LedState MackieControlProtocol::drop_release( Button & button )
1705 LedState MackieControlProtocol::save_press( Button & button )
1707 session->save_state( "" );
1711 LedState MackieControlProtocol::save_release( Button & button )
1716 LedState MackieControlProtocol::smpte_beats_press( Button & )
1718 switch ( _timecode_type )
1720 case ARDOUR::AnyTime::BBT:
1721 _timecode_type = ARDOUR::AnyTime::SMPTE;
1723 case ARDOUR::AnyTime::SMPTE:
1724 _timecode_type = ARDOUR::AnyTime::BBT;
1728 os << "Unknown Anytime::Type " << _timecode_type;
1729 throw runtime_error( os.str() );
1731 update_smpte_beats_led();
1735 LedState MackieControlProtocol::smpte_beats_release( Button & )