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>
49 #include "mackie_control_protocol.h"
51 #include "midi_byte_array.h"
52 #include "mackie_control_exception.h"
53 #include "route_signal.h"
54 #include "mackie_midi_builder.h"
55 #include "surface_port.h"
57 #include "bcf_surface.h"
58 #include "mackie_surface.h"
60 using namespace ARDOUR;
63 using namespace Mackie;
66 using boost::shared_ptr;
70 MackieMidiBuilder builder;
72 MackieControlProtocol::MackieControlProtocol (Session& session)
73 : ControlProtocol (session, X_("Mackie"))
74 , _current_initial_bank( 0 )
75 , connections_back( _connections )
77 , _ports_changed( false )
84 cout << "MackieControlProtocol::MackieControlProtocol" << endl;
86 // will start reading from ports, as soon as there are some
87 pthread_create_and_store (X_("mackie monitor"), &thread, 0, _monitor_work, this);
90 MackieControlProtocol::~MackieControlProtocol()
93 cout << "~MackieControlProtocol::MackieControlProtocol" << endl;
99 catch ( exception & e )
101 cout << "~MackieControlProtocol caught " << e.what() << endl;
105 cout << "~MackieControlProtocol caught unknown" << endl;
108 cout << "finished ~MackieControlProtocol::MackieControlProtocol" << endl;
112 Mackie::Surface & MackieControlProtocol::surface()
116 throw MackieControlException( "_surface is 0 in MackieControlProtocol::surface" );
121 const Mackie::MackiePort & MackieControlProtocol::mcu_port() const
123 return dynamic_cast<const MackiePort &>( *_ports[0] );
126 Mackie::MackiePort & MackieControlProtocol::mcu_port()
128 return dynamic_cast<const MackiePort &>( *_ports[0] );
131 // go to the previous track.
132 // Assume that get_sorted_routes().size() > route_table.size()
133 void MackieControlProtocol::prev_track()
135 if ( _current_initial_bank >= 1 )
137 session->set_dirty();
138 switch_banks( _current_initial_bank - 1 );
142 // go to the next track.
143 // Assume that get_sorted_routes().size() > route_table.size()
144 void MackieControlProtocol::next_track()
146 Sorted sorted = get_sorted_routes();
147 if ( _current_initial_bank + route_table.size() < sorted.size() )
149 session->set_dirty();
150 switch_banks( _current_initial_bank + 1 );
154 void MackieControlProtocol::clear_route_signals()
156 for( RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it )
160 route_signals.clear();
163 // return the port for a given id - 0 based
164 // throws an exception if no port found
165 MackiePort & MackieControlProtocol::port_for_id( uint32_t index )
167 uint32_t current_max = 0;
168 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
170 current_max += (*it)->strips();
171 if ( index < current_max ) return **it;
174 // oops - no matching port
176 os << "No port for index " << index;
177 throw MackieControlException( os.str() );
180 // predicate for sort call in get_sorted_routes
181 struct RouteByRemoteId
183 bool operator () ( const shared_ptr<Route> & a, const shared_ptr<Route> & b ) const
185 return a->remote_control_id() < b->remote_control_id();
188 bool operator () ( const Route & a, const Route & b ) const
190 return a.remote_control_id() < b.remote_control_id();
193 bool operator () ( const Route * a, const Route * b ) const
195 return a->remote_control_id() < b->remote_control_id();
199 MackieControlProtocol::Sorted MackieControlProtocol::get_sorted_routes()
204 boost::shared_ptr<Session::RouteList> routes = session->get_routes();
205 set<uint32_t> remote_ids;
207 // routes with remote_id 0 should never be added
208 // TODO verify this with ardour devs
209 // remote_ids.insert( 0 );
211 // sort in remote_id order, and exclude master, control and hidden routes
212 // and any routes that are already set.
213 for ( Session::RouteList::iterator it = routes->begin(); it != routes->end(); ++it )
215 Route & route = **it;
221 && remote_ids.find( route.remote_control_id() ) == remote_ids.end()
224 sorted.push_back( *it );
225 remote_ids.insert( route.remote_control_id() );
228 sort( sorted.begin(), sorted.end(), RouteByRemoteId() );
232 void MackieControlProtocol::refresh_current_bank()
234 switch_banks( _current_initial_bank );
237 void MackieControlProtocol::switch_banks( int initial )
239 // DON'T prevent bank switch if initial == _current_initial_bank
240 // because then this method can't be used as a refresh
243 Sorted sorted = get_sorted_routes();
244 int delta = sorted.size() - route_table.size();
245 if ( initial < 0 || ( delta > 0 && initial > delta ) )
248 cout << "not switching to " << initial << endl;
252 _current_initial_bank = initial;
254 // first clear the signals from old routes
255 // taken care of by the RouteSignal destructors
256 clear_route_signals();
258 // now set the signals for new routes
259 if ( _current_initial_bank <= sorted.size() )
261 // fetch the bank start and end to switch to
262 uint32_t end_pos = min( route_table.size(), sorted.size() );
263 Sorted::iterator it = sorted.begin() + _current_initial_bank;
264 Sorted::iterator end = sorted.begin() + _current_initial_bank + end_pos;
266 cout << "switch to " << _current_initial_bank << ", " << end_pos << endl;
269 // link routes to strips
271 for ( ; it != end && it != sorted.end(); ++it, ++i )
273 boost::shared_ptr<Route> route = *it;
274 Strip & strip = *surface().strips[i];
276 cout << "remote id " << route->remote_control_id() << " connecting " << route->name() << " to " << strip.name() << " with port " << port_for_id(i) << endl;
278 route_table[i] = route;
279 RouteSignal * rs = new RouteSignal( *route, *this, strip, port_for_id(i) );
280 route_signals.push_back( rs );
281 // update strip from route
285 // create dead strips if there aren't enough routes to
287 for ( ; i < route_table.size(); ++i )
289 Strip & strip = *surface().strips[i];
290 // send zero for this strip
291 MackiePort & port = port_for_id(i);
292 port.write( builder.zero_strip( port, strip ) );
296 // display the current start bank.
297 if ( mcu_port().emulation() == MackiePort::bcf2000 )
299 if ( _current_initial_bank == 0 )
301 // send Ar. to 2-char display on the master
302 mcu_port().write( builder.two_char_display( "Ar", ".." ) );
306 // write the current first remote_id to the 2-char display
307 mcu_port().write( builder.two_char_display( _current_initial_bank ) );
312 void MackieControlProtocol::zero_all()
314 // TODO turn off SMPTE displays
316 if ( mcu_port().emulation() == MackiePort::bcf2000 )
318 // clear 2-char display
319 mcu_port().write( builder.two_char_display( "LC" ) );
323 for ( Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it )
325 MackiePort & port = port_for_id( (*it)->index() );
326 port.write( builder.zero_strip( port, **it ) );
329 // and the master strip
330 mcu_port().write( builder.zero_strip( mcu_port(), master_strip() ) );
332 // and the led ring for the master strip, in bcf mode
333 if ( mcu_port().emulation() == MackiePort::bcf2000 )
335 Control & control = *surface().controls_by_name["jog"];
336 mcu_port().write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
339 // turn off global buttons and leds
340 // global buttons are only ever on mcu_port, so we don't have
341 // to figure out which port.
342 for ( Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it )
344 Control & control = **it;
345 if ( !control.group().is_strip() && control.accepts_feedback() )
347 mcu_port().write( builder.zero_control( control ) );
352 int MackieControlProtocol::set_active( bool yn )
358 // the reason for the locking and unlocking is that
359 // 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 // make sure the ports are being listened to
373 // wait until poll thread is running, with ports to poll
374 // the mutex is only there because conditions require a mutex
376 Glib::Mutex::Lock lock( update_mutex );
377 while ( nfds == 0 ) update_cond.wait( update_mutex );
380 // now initialise MackiePorts - ie exchange sysex messages
381 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
386 // wait until all ports are active
387 // TODO a more sophisticated approach would
388 // allow things to start up with only an MCU, even if
389 // extenders were specified but not responding.
390 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
392 (*it)->wait_for_init();
395 // create surface object. This depends on the ports being
396 // correctly initialised
397 initialize_surface();
398 connect_session_signals();
403 // send current control positions to surface
404 // must come after _active = true otherwise it won't run
413 catch( exception & e )
416 cout << "set_active to false because exception caught: " << e.what() << endl;
426 bool MackieControlProtocol::handle_strip_button( Control & control, ButtonState bs, boost::shared_ptr<Route> route )
432 if ( control.name() == "recenable" )
434 state = !route->record_enabled();
435 route->set_record_enable( state, this );
437 else if ( control.name() == "mute" )
439 state = !route->muted();
440 route->set_mute( state, this );
442 else if ( control.name() == "solo" )
444 state = !route->soloed();
445 route->set_solo( state, this );
447 else if ( control.name() == "select" )
449 // TODO make the track selected. Whatever that means.
450 //state = default_button_press( dynamic_cast<Button&>( control ) );
452 else if ( control.name() == "vselect" )
454 // TODO could be used to select different things to apply the pot to?
455 //state = default_button_press( dynamic_cast<Button&>( control ) );
459 if ( control.name() == "fader_touch" )
462 control.strip().gain().in_use( state );
468 void MackieControlProtocol::update_led( Mackie::Button & button, Mackie::LedState ls )
472 MackiePort * port = 0;
473 if ( button.group().is_strip() )
475 if ( button.group().is_master() )
481 port = &port_for_id( dynamic_cast<const Strip&>( button.group() ).index() );
488 port->write( builder.build_led( button, ls ) );
492 void MackieControlProtocol::update_global_button( const string & name, LedState ls )
494 if ( surface().controls_by_name.find( name ) !=surface().controls_by_name.end() )
496 Button * button = dynamic_cast<Button*>( surface().controls_by_name[name] );
497 mcu_port().write( builder.build_led( button->led(), ls ) );
502 cout << "Button " << name << " not found" << endl;
507 // send messages to surface to set controls to correct values
508 void MackieControlProtocol::update_surface()
512 // do the initial bank switch to connect signals
513 // _current_initial_bank is initialised by set_state
514 switch_banks( _current_initial_bank );
516 // create a RouteSignal for the master route
517 // but only the first time around
518 master_route_signal = shared_ptr<RouteSignal>( new RouteSignal( *master_route(), *this, master_strip(), mcu_port() ) );
519 // update strip from route
520 master_route_signal->notify_all();
522 // turn off the led ring, for bcf emulation mode
523 if ( mcu_port().emulation() == MackiePort::bcf2000 )
525 Control & control = *surface().controls_by_name["jog"];
526 mcu_port().write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
529 // update global buttons and displays
530 notify_record_state_changed();
531 notify_transport_state_changed();
535 void MackieControlProtocol::connect_session_signals()
537 // receive routes added
538 connections_back = session->RouteAdded.connect( ( mem_fun (*this, &MackieControlProtocol::notify_route_added) ) );
539 // receive record state toggled
540 connections_back = session->RecordStateChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_record_state_changed) ) );
541 // receive transport state changed
542 connections_back = session->TransportStateChange.connect( ( mem_fun (*this, &MackieControlProtocol::notify_transport_state_changed) ) );
543 // receive punch-in and punch-out
544 connections_back = Config->ParameterChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_parameter_changed) ) );
545 // receive rude solo changed
546 connections_back = session->SoloActive.connect( ( mem_fun (*this, &MackieControlProtocol::notify_solo_active_changed) ) );
548 // make sure remote id changed signals reach here
549 // see also notify_route_added
550 Sorted sorted = get_sorted_routes();
551 for ( Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it )
553 connections_back = (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
557 void MackieControlProtocol::add_port( MIDI::Port & midi_port, int number )
560 cout << "add port " << midi_port.name() << ", " << midi_port.device() << endl;
563 MackiePort * sport = new MackiePort( *this, midi_port, number );
564 _ports.push_back( sport );
566 connections_back = sport->init_event.connect(
568 mem_fun (*this, &MackieControlProtocol::handle_port_init)
573 connections_back = sport->active_event.connect(
575 mem_fun (*this, &MackieControlProtocol::handle_port_active)
580 connections_back = sport->inactive_event.connect(
582 mem_fun (*this, &MackieControlProtocol::handle_port_inactive)
587 _ports_changed = true;
590 void MackieControlProtocol::create_ports()
592 MIDI::Manager * mm = MIDI::Manager::instance();
596 MIDI::Port * midi_port = mm->port( default_port_name );
598 if ( midi_port == 0 ) {
600 os << string_compose( _("no MIDI port named \"%1\" exists - Mackie control disabled"), default_port_name );
601 error << os.str() << endmsg;
602 throw MackieControlException( os.str() );
604 add_port( *midi_port, 0 );
607 // open extender ports. Up to 9. Should be enough.
608 // could also use mm->get_midi_ports()
609 string ext_port_base = "mcu_xt_";
610 for ( int index = 1; index <= 9; ++index )
613 os << ext_port_base << index;
614 MIDI::Port * midi_port = mm->port( os.str() );
615 if ( midi_port != 0 ) add_port( *midi_port, index );
619 shared_ptr<Route> MackieControlProtocol::master_route()
621 shared_ptr<Route> retval;
622 retval = session->route_by_name( "master" );
625 // TODO search through all routes for one with the master attribute set
630 Strip & MackieControlProtocol::master_strip()
632 return dynamic_cast<Strip&>( *surface().groups["master"] );
635 void MackieControlProtocol::initialize_surface()
637 // set up the route table
639 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
641 strips += (*it)->strips();
644 set_route_table_size( strips );
646 switch ( mcu_port().emulation() )
648 case MackiePort::bcf2000: _surface = new BcfSurface( strips ); break;
649 case MackiePort::mackie: _surface = new MackieSurface( strips ); break;
652 os << "no Surface class found for emulation: " << mcu_port().emulation();
653 throw MackieControlException( os.str() );
657 // Connect events. Must be after route table otherwise there will be trouble
658 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
660 connections_back = (*it)->control_event.connect( ( mem_fun (*this, &MackieControlProtocol::handle_control_event) ) );
664 void MackieControlProtocol::close()
666 // TODO disconnect port active/inactive signals
667 // Or at least put a lock here
669 // disconnect global signals from Session
670 // TODO Since *this is a sigc::trackable, this shouldn't be necessary
671 // but it is for some reason
673 for( vector<sigc::connection>::iterator it = _connections.begin(); it != _connections.end(); ++it )
681 // These will fail if the port has gone away.
682 // So catch the exception and do the rest of the
684 // because the bcf doesn't respond to the next 3 sysex messages
689 catch ( exception & e )
692 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
696 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
700 MackiePort & port = **it;
702 port.write_sysex( 0x61 );
704 port.write_sysex( 0x62 );
705 // Reset (reboot into offline mode)
706 port.write_sysex( 0x63 );
708 catch ( exception & e )
711 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
716 // disconnect routes from strips
717 clear_route_signals();
723 // stop polling, and wait for it...
725 pthread_join( thread, 0 );
727 // shut down MackiePorts
728 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
734 // this is done already in monitor_work. But it's here so we know.
740 void* MackieControlProtocol::_monitor_work (void* arg)
742 return static_cast<MackieControlProtocol*>(arg)->monitor_work ();
745 XMLNode & MackieControlProtocol::get_state()
748 cout << "MackieControlProtocol::get_state" << endl;
751 // add name of protocol
752 XMLNode* node = new XMLNode( X_("Protocol") );
753 node->add_property( X_("name"), _name );
757 os << _current_initial_bank;
758 node->add_property( X_("bank"), os.str() );
763 int MackieControlProtocol::set_state( const XMLNode & node )
766 cout << "MackieControlProtocol::set_state: active " << _active << endl;
770 // fetch current bank
771 if ( node.property( X_("bank") ) != 0 )
773 string bank = node.property( X_("bank") )->value();
777 uint32_t new_bank = atoi( bank.c_str() );
778 if ( _current_initial_bank != new_bank ) switch_banks( new_bank );
780 catch ( exception & e )
783 cout << "exception in MackieControlProtocol::set_state: " << e.what() << endl;
792 void MackieControlProtocol::handle_control_event( SurfacePort & port, Control & control, const ControlState & state )
794 // find the route for the control, if there is one
795 boost::shared_ptr<Route> route;
796 if ( control.group().is_strip() )
798 if ( control.group().is_master() )
800 route = master_route();
804 uint32_t index = control.ordinal() - 1 + ( port.number() * port.strips() );
805 if ( index < route_table.size() )
806 route = route_table[index];
808 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
812 // This handles control element events from the surface
813 // the state of the controls on the surface is usually updated
815 switch ( control.type() )
817 case Control::type_fader:
818 // find the route in the route table for the id
819 // if the route isn't available, skip it
820 // at which point the fader should just reset itself
823 route->gain_control().set_value( state.pos );
825 // must echo bytes back to slider now, because
826 // the notifier only works if the fader is not being
827 // touched. Which it is if we're getting input.
828 port.write( builder.build_fader( (Fader&)control, state.pos ) );
832 case Control::type_button:
833 if ( control.group().is_strip() )
838 handle_strip_button( control, state.button_state, route );
842 // no route so always switch the light off
843 // because no signals will be emitted by a non-route
844 port.write( builder.build_led( control.led(), off ) );
847 else if ( control.group().is_master() )
849 // master fader touch
852 handle_strip_button( control, state.button_state, route );
857 // handle all non-strip buttons
858 surface().handle_button( *this, state.button_state, dynamic_cast<Button&>( control ) );
862 // pot (jog wheel, external control)
863 case Control::type_pot:
864 if ( control.group().is_strip() )
868 // pan for mono input routes, or stereo linked panners
869 if ( route->panner().size() == 1 || ( route->panner().size() == 2 && route->panner().linked() ) )
871 // assume pan for now
873 route->panner()[0]->get_effective_position (xpos);
875 // calculate new value, and trim
876 xpos += state.delta * state.sign;
879 else if ( xpos < 0.0 )
882 route->panner()[0]->set_position( xpos );
887 // it's a pot for an umnapped route, so turn all the lights off
888 port.write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
893 if ( control.is_jog() )
895 _jog_wheel.jog_event( port, control, state );
899 cout << "external controller" << state.ticks * state.sign << endl;
905 cout << "Control::type not handled: " << control.type() << endl;
909 /////////////////////////////////////////////////
910 // handlers for Route signals
911 // TODO should these be part of RouteSignal?
912 // They started off as sigc handlers for signals
913 // from Route, but they're also used in polling for automation
914 /////////////////////////////////////////////////
916 void MackieControlProtocol::notify_solo_changed( RouteSignal * route_signal )
920 Button & button = route_signal->strip().solo();
922 cout << "updating " << button << endl;
924 route_signal->port().write( builder.build_led( button, route_signal->route().soloed() ) );
926 catch( exception & e )
928 cout << e.what() << endl;
932 void MackieControlProtocol::notify_mute_changed( RouteSignal * route_signal )
936 Button & button = route_signal->strip().mute();
938 cout << "updating " << button << endl;
940 route_signal->port().write( builder.build_led( button, route_signal->route().muted() ) );
942 catch( exception & e )
944 cout << e.what() << endl;
948 void MackieControlProtocol::notify_record_enable_changed( RouteSignal * route_signal )
952 Button & button = route_signal->strip().recenable();
954 cout << "updating " << button << endl;
956 route_signal->port().write( builder.build_led( button, route_signal->route().record_enabled() ) );
958 catch( exception & e )
960 cout << e.what() << endl;
964 void MackieControlProtocol::notify_gain_changed( RouteSignal * route_signal )
968 Fader & fader = route_signal->strip().gain();
970 cout << "updating " << fader << endl;
972 if ( !fader.in_use() )
974 route_signal->port().write( builder.build_fader( fader, route_signal->route().gain_control().get_value() ) );
977 catch( exception & e )
979 cout << e.what() << endl;
983 void MackieControlProtocol::notify_name_changed( void *, RouteSignal * route_signal )
987 Strip & strip = route_signal->strip();
988 if ( !strip.is_master() )
991 string fullname = route_signal->route().name();
993 if ( fullname.length() <= 6 )
999 line1 = PBD::short_version( fullname, 6 );
1002 MackiePort & port = route_signal->port();
1003 port.write( builder.strip_display( port, strip, 0, line1 ) );
1004 port.write( builder.strip_display_blank( port, strip, 1 ) );
1007 catch( exception & e )
1009 cout << e.what() << endl;
1013 void MackieControlProtocol::notify_panner_changed( RouteSignal * route_signal )
1017 Pot & pot = route_signal->strip().vpot();
1019 cout << "updating " << pot << endl;
1021 const Panner & panner = route_signal->route().panner();
1022 if ( panner.size() == 1 || ( panner.size() == 2 && panner.linked() ) )
1025 route_signal->route().panner()[0]->get_effective_position( pos );
1026 route_signal->port().write( builder.build_led_ring( pot, ControlState( on, pos ), MackieMidiBuilder::midi_pot_mode_dot ) );
1030 route_signal->port().write( builder.zero_control( pot ) );
1033 catch( exception & e )
1035 cout << e.what() << endl;
1039 // TODO handle plugin automation polling
1040 void MackieControlProtocol::update_automation( RouteSignal & rs )
1042 ARDOUR::AutoState gain_state = rs.route().gain_automation_state();
1043 if ( gain_state == Touch || gain_state == Play )
1045 notify_gain_changed( &rs );
1048 ARDOUR::AutoState panner_state = rs.route().panner().automation_state();
1049 if ( panner_state == Touch || panner_state == Play )
1051 notify_panner_changed( &rs );
1055 void MackieControlProtocol::poll_automation()
1059 // do all currently mapped routes
1060 for( RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it )
1062 update_automation( **it );
1065 // and the master strip
1066 if ( master_route_signal != 0 ) update_automation( *master_route_signal );
1070 /////////////////////////////////////
1071 // Transport Buttons
1072 /////////////////////////////////////
1074 LedState MackieControlProtocol::frm_left_press( Button & button )
1076 // can use first_mark_before/after as well
1077 unsigned long elapsed = _frm_left_last.restart();
1079 Location * loc = session->locations()->first_location_before (
1080 session->transport_frame()
1083 // allow a quick double to go past a previous mark
1084 if ( elapsed < 500 && loc != 0)
1086 Location * loc_two_back = session->locations()->first_location_before ( loc->start() );
1087 if ( loc_two_back != 0 )
1093 // move to the location, if it's valid
1096 session->request_locate( loc->start(), session->transport_rolling() );
1102 LedState MackieControlProtocol::frm_left_release( Button & button )
1107 LedState MackieControlProtocol::frm_right_press( Button & button )
1109 // can use first_mark_before/after as well
1110 Location * loc = session->locations()->first_location_after (
1111 session->transport_frame()
1113 if ( loc != 0 ) session->request_locate( loc->start(), session->transport_rolling() );
1117 LedState MackieControlProtocol::frm_right_release( Button & button )
1122 LedState MackieControlProtocol::stop_press( Button & button )
1124 session->request_stop();
1128 LedState MackieControlProtocol::stop_release( Button & button )
1130 return session->transport_stopped();
1133 LedState MackieControlProtocol::play_press( Button & button )
1135 session->request_transport_speed( 1.0 );
1139 LedState MackieControlProtocol::play_release( Button & button )
1141 return session->transport_rolling();
1144 LedState MackieControlProtocol::record_press( Button & button )
1146 if ( session->get_record_enabled() )
1147 session->disable_record( false );
1149 session->maybe_enable_record();
1153 LedState MackieControlProtocol::record_release( Button & button )
1155 if ( session->get_record_enabled() )
1157 if ( session->transport_rolling() )
1166 LedState MackieControlProtocol::rewind_press( Button & button )
1168 _jog_wheel.push( JogWheel::speed );
1169 _jog_wheel.transport_direction( -1 );
1170 session->request_transport_speed( -_jog_wheel.transport_speed() );
1174 LedState MackieControlProtocol::rewind_release( Button & button )
1177 _jog_wheel.transport_direction( 0 );
1178 if ( _transport_previously_rolling )
1179 session->request_transport_speed( 1.0 );
1181 session->request_stop();
1185 LedState MackieControlProtocol::ffwd_press( Button & button )
1187 _jog_wheel.push( JogWheel::speed );
1188 _jog_wheel.transport_direction( 1 );
1189 session->request_transport_speed( _jog_wheel.transport_speed() );
1193 LedState MackieControlProtocol::ffwd_release( Button & button )
1196 _jog_wheel.transport_direction( 0 );
1197 if ( _transport_previously_rolling )
1198 session->request_transport_speed( 1.0 );
1200 session->request_stop();
1204 LedState MackieControlProtocol::loop_press( Button & button )
1206 session->request_play_loop( !session->get_play_loop() );
1210 LedState MackieControlProtocol::loop_release( Button & button )
1212 return session->get_play_loop();
1215 LedState MackieControlProtocol::punch_in_press( Button & button )
1217 bool state = !Config->get_punch_in();
1218 Config->set_punch_in( state );
1222 LedState MackieControlProtocol::punch_in_release( Button & button )
1224 return Config->get_punch_in();
1227 LedState MackieControlProtocol::punch_out_press( Button & button )
1229 bool state = !Config->get_punch_out();
1230 Config->set_punch_out( state );
1234 LedState MackieControlProtocol::punch_out_release( Button & button )
1236 return Config->get_punch_out();
1239 LedState MackieControlProtocol::home_press( Button & button )
1241 session->goto_start();
1245 LedState MackieControlProtocol::home_release( Button & button )
1250 LedState MackieControlProtocol::end_press( Button & button )
1252 session->goto_end();
1256 LedState MackieControlProtocol::end_release( Button & button )
1261 LedState MackieControlProtocol::clicking_press( Button & button )
1263 bool state = !Config->get_clicking();
1264 Config->set_clicking( state );
1268 LedState MackieControlProtocol::clicking_release( Button & button )
1270 return Config->get_clicking();
1273 LedState MackieControlProtocol::global_solo_press( Button & button )
1275 bool state = !session->soloing();
1276 session->set_all_solo ( state );
1280 LedState MackieControlProtocol::global_solo_release( Button & button )
1282 return session->soloing();
1285 ///////////////////////////////////////////
1287 ///////////////////////////////////////////
1289 void MackieControlProtocol::notify_parameter_changed( const char * name_str )
1291 string name( name_str );
1292 if ( name == "punch-in" )
1294 update_global_button( "punch_in", Config->get_punch_in() );
1296 else if ( name == "punch-out" )
1298 update_global_button( "punch_out", Config->get_punch_out() );
1300 else if ( name == "clicking" )
1302 update_global_button( "clicking", Config->get_clicking() );
1307 cout << "parameter changed: " << name << endl;
1312 // RouteList is the set of routes that have just been added
1313 void MackieControlProtocol::notify_route_added( ARDOUR::Session::RouteList & rl )
1315 // currently assigned banks are less than the full set of
1316 // strips, so activate the new strip now.
1317 if ( route_signals.size() < route_table.size() )
1319 refresh_current_bank();
1321 // otherwise route added, but current bank needs no updating
1323 // make sure remote id changes in the new route are handled
1324 typedef ARDOUR::Session::RouteList ARS;
1325 for ( ARS::iterator it = rl.begin(); it != rl.end(); ++it )
1327 connections_back = (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
1331 void MackieControlProtocol::notify_solo_active_changed( bool active )
1333 Button * rude_solo = reinterpret_cast<Button*>( surface().controls_by_name["solo"] );
1334 mcu_port().write( builder.build_led( *rude_solo, active ? flashing : off ) );
1337 void MackieControlProtocol::notify_remote_id_changed()
1339 Sorted sorted = get_sorted_routes();
1341 // if a remote id has been moved off the end, we need to shift
1342 // the current bank backwards.
1343 if ( sorted.size() - _current_initial_bank < route_signals.size() )
1345 // but don't shift backwards past the zeroth channel
1346 switch_banks( max((Sorted::size_type) 0, sorted.size() - route_signals.size() ) );
1348 // Otherwise just refresh the current bank
1351 refresh_current_bank();
1355 ///////////////////////////////////////////
1356 // Transport signals
1357 ///////////////////////////////////////////
1359 void MackieControlProtocol::notify_record_state_changed()
1361 // switch rec button on / off / flashing
1362 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1363 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1366 void MackieControlProtocol::notify_transport_state_changed()
1368 // switch various play and stop buttons on / off
1369 update_global_button( "play", session->transport_rolling() );
1370 update_global_button( "stop", !session->transport_rolling() );
1371 update_global_button( "loop", session->get_play_loop() );
1373 _transport_previously_rolling = session->transport_rolling();
1375 // rec is special because it's tristate
1376 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1377 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1380 /////////////////////////////////////
1382 /////////////////////////////////////
1383 LedState MackieControlProtocol::left_press( Button & button )
1385 Sorted sorted = get_sorted_routes();
1386 if ( sorted.size() > route_table.size() )
1388 int new_initial = _current_initial_bank - route_table.size();
1389 if ( new_initial < 0 ) new_initial = 0;
1390 if ( new_initial != int( _current_initial_bank ) )
1392 session->set_dirty();
1393 switch_banks( new_initial );
1404 LedState MackieControlProtocol::left_release( Button & button )
1409 LedState MackieControlProtocol::right_press( Button & button )
1411 Sorted sorted = get_sorted_routes();
1412 if ( sorted.size() > route_table.size() )
1414 uint32_t delta = sorted.size() - ( route_table.size() + _current_initial_bank );
1415 if ( delta > route_table.size() ) delta = route_table.size();
1418 session->set_dirty();
1419 switch_banks( _current_initial_bank + delta );
1430 LedState MackieControlProtocol::right_release( Button & button )
1435 LedState MackieControlProtocol::channel_left_press( Button & button )
1437 Sorted sorted = get_sorted_routes();
1438 if ( sorted.size() > route_table.size() )
1449 LedState MackieControlProtocol::channel_left_release( Button & button )
1454 LedState MackieControlProtocol::channel_right_press( Button & button )
1456 Sorted sorted = get_sorted_routes();
1457 if ( sorted.size() > route_table.size() )
1468 LedState MackieControlProtocol::channel_right_release( Button & button )
1473 /////////////////////////////////////
1475 /////////////////////////////////////
1476 LedState MackieControlProtocol::marker_press( Button & button )
1478 // cut'n'paste from LocationUI::add_new_location()
1480 nframes_t where = session->audible_frame();
1481 session->locations()->next_available_name(markername,"mcu");
1482 Location *location = new Location (where, where, markername, Location::IsMark);
1483 session->begin_reversible_command (_("add marker"));
1484 XMLNode &before = session->locations()->get_state();
1485 session->locations()->add (location, true);
1486 XMLNode &after = session->locations()->get_state();
1487 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1488 session->commit_reversible_command ();
1492 LedState MackieControlProtocol::marker_release( Button & button )
1497 void jog_wheel_state_display( JogWheel::State state, MackiePort & port )
1501 case JogWheel::zoom: port.write( builder.two_char_display( "Zm" ) ); break;
1502 case JogWheel::scroll: port.write( builder.two_char_display( "Sc" ) ); break;
1503 case JogWheel::scrub: port.write( builder.two_char_display( "Sb" ) ); break;
1504 case JogWheel::shuttle: port.write( builder.two_char_display( "Sh" ) ); break;
1505 case JogWheel::speed: port.write( builder.two_char_display( "Sp" ) ); break;
1506 case JogWheel::select: port.write( builder.two_char_display( "Se" ) ); break;
1510 Mackie::LedState MackieControlProtocol::zoom_press( Mackie::Button & )
1512 _jog_wheel.zoom_state_toggle();
1513 update_global_button( "scrub", _jog_wheel.jog_wheel_state() == JogWheel::scrub );
1514 jog_wheel_state_display( _jog_wheel.jog_wheel_state(), mcu_port() );
1515 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1518 Mackie::LedState MackieControlProtocol::zoom_release( Mackie::Button & )
1520 return _jog_wheel.jog_wheel_state() == JogWheel::zoom;
1523 Mackie::LedState MackieControlProtocol::scrub_press( Mackie::Button & )
1525 _jog_wheel.scrub_state_cycle();
1526 update_global_button( "zoom", _jog_wheel.jog_wheel_state() == JogWheel::zoom );
1527 jog_wheel_state_display( _jog_wheel.jog_wheel_state(), mcu_port() );
1529 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1531 _jog_wheel.jog_wheel_state() == JogWheel::shuttle
1535 Mackie::LedState MackieControlProtocol::scrub_release( Mackie::Button & )
1538 _jog_wheel.jog_wheel_state() == JogWheel::scrub
1540 _jog_wheel.jog_wheel_state() == JogWheel::shuttle