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>
41 #include <ardour/route.h>
42 #include <ardour/session.h>
43 #include <ardour/location.h>
44 #include <ardour/dB.h>
45 #include <ardour/panner.h>
47 #include "mackie_control_protocol.h"
49 #include "midi_byte_array.h"
50 #include "mackie_control_exception.h"
51 #include "route_signal.h"
52 #include "mackie_midi_builder.h"
53 #include "surface_port.h"
55 #include "bcf_surface.h"
56 #include "mackie_surface.h"
58 using namespace ARDOUR;
61 using namespace Mackie;
64 using boost::shared_ptr;
68 MackieMidiBuilder builder;
70 // Copied from tranzport_control_protocol.cc
72 gain_to_slider_position (ARDOUR::gain_t g)
75 return pow((6.0*log(g)/log(2.0)+192.0)/198.0, 8.0);
79 Copied from tranzport_control_protocol.cc
80 TODO this seems to return slightly wrong values, namely
81 with the UI slider at max, we get a 0.99something value.
83 static inline ARDOUR::gain_t
84 slider_position_to_gain (double pos)
86 /* XXX Marcus writes: this doesn't seem right to me. but i don't have a better answer ... */
87 if (pos == 0.0) return 0;
88 return pow (2.0,(sqrt(sqrt(sqrt(pos)))*198.0-192.0)/6.0);
91 MackieControlProtocol::MackieControlProtocol (Session& session)
92 : ControlProtocol (session, X_("Mackie"))
93 , _current_initial_bank( 0 )
94 , connections_back( _connections )
96 , _ports_changed( false )
100 cout << "MackieControlProtocol::MackieControlProtocol" << endl;
101 // will start reading from ports, as soon as there are some
102 pthread_create_and_store (X_("mackie monitor"), &thread, 0, _monitor_work, this);
105 MackieControlProtocol::~MackieControlProtocol()
107 cout << "~MackieControlProtocol::MackieControlProtocol" << endl;
112 catch ( exception & e )
114 cout << "~MackieControlProtocol caught " << e.what() << endl;
118 cout << "~MackieControlProtocol caught unknown" << endl;
122 Mackie::Surface & MackieControlProtocol::surface()
126 throw MackieControlException( "_surface is 0 in MackieControlProtocol::surface" );
131 const Mackie::MackiePort & MackieControlProtocol::mcu_port() const
133 return dynamic_cast<const MackiePort &>( *_ports[0] );
136 Mackie::MackiePort & MackieControlProtocol::mcu_port()
138 return dynamic_cast<const MackiePort &>( *_ports[0] );
141 // go to the previous track.
142 // Assume that get_sorted_routes().size() > route_table.size()
143 void MackieControlProtocol::prev_track()
145 if ( _current_initial_bank >= 1 )
147 session->set_dirty();
148 switch_banks( _current_initial_bank - 1 );
152 // go to the next track.
153 // Assume that get_sorted_routes().size() > route_table.size()
154 void MackieControlProtocol::next_track()
156 Sorted sorted = get_sorted_routes();
157 if ( _current_initial_bank + route_table.size() < sorted.size() )
159 session->set_dirty();
160 switch_banks( _current_initial_bank + 1 );
164 void MackieControlProtocol::clear_route_signals()
166 for( RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it )
170 route_signals.clear();
173 // return the port for a given id - 0 based
174 // throws an exception if no port found
175 MackiePort & MackieControlProtocol::port_for_id( uint32_t index )
177 uint32_t current_max = 0;
178 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
180 current_max += (*it)->strips();
181 if ( index < current_max ) return **it;
184 // oops - no matching port
186 os << "No port for index " << index;
187 throw MackieControlException( os.str() );
190 // predicate for sort call in get_sorted_routes
191 struct RouteByRemoteId
193 bool operator () ( const shared_ptr<Route> & a, const shared_ptr<Route> & b ) const
195 return a->remote_control_id() < b->remote_control_id();
198 bool operator () ( const Route & a, const Route & b ) const
200 return a.remote_control_id() < b.remote_control_id();
203 bool operator () ( const Route * a, const Route * b ) const
205 return a->remote_control_id() < b->remote_control_id();
209 MackieControlProtocol::Sorted MackieControlProtocol::get_sorted_routes()
214 boost::shared_ptr<Session::RouteList> routes = session->get_routes();
215 set<uint32_t> remote_ids;
217 // routes with remote_id 0 should never be added
218 // TODO verify this with ardour devs
219 // remote_ids.insert( 0 );
221 // sort in remote_id order, and exclude master, control and hidden routes
222 // and any routes that are already set.
223 for ( Session::RouteList::iterator it = routes->begin(); it != routes->end(); ++it )
225 Route & route = **it;
231 && remote_ids.find( route.remote_control_id() ) == remote_ids.end()
234 sorted.push_back( *it );
235 remote_ids.insert( route.remote_control_id() );
238 sort( sorted.begin(), sorted.end(), RouteByRemoteId() );
242 void MackieControlProtocol::refresh_current_bank()
244 switch_banks( _current_initial_bank );
247 void MackieControlProtocol::switch_banks( int initial )
249 // DON'T prevent bank switch if initial == _current_initial_bank
250 // because then this method can't be used as a refresh
253 Sorted sorted = get_sorted_routes();
254 int delta = sorted.size() - route_table.size();
255 if ( initial < 0 || ( delta > 0 && initial > delta ) )
257 cout << "not switching to " << initial << endl;
260 _current_initial_bank = initial;
262 // first clear the signals from old routes
263 // taken care of by the RouteSignal destructors
264 clear_route_signals();
266 // now set the signals for new routes
267 if ( _current_initial_bank <= sorted.size() )
269 // fetch the bank start and end to switch to
270 uint32_t end_pos = min( route_table.size(), sorted.size() );
271 Sorted::iterator it = sorted.begin() + _current_initial_bank;
272 Sorted::iterator end = sorted.begin() + _current_initial_bank + end_pos;
273 //cout << "switch to " << _current_initial_bank << ", " << end_pos << endl;
275 // link routes to strips
277 for ( ; it != end && it != sorted.end(); ++it, ++i )
279 boost::shared_ptr<Route> route = *it;
280 Strip & strip = *surface().strips[i];
281 //cout << "remote id " << route->remote_control_id() << " connecting " << route->name() << " to " << strip.name() << " with port " << port_for_id(i) << endl;
282 route_table[i] = route;
283 RouteSignal * rs = new RouteSignal( *route, *this, strip, port_for_id(i) );
284 route_signals.push_back( rs );
285 // update strip from route
289 // create dead strips if there aren't enough routes to
291 for ( ; i < route_table.size(); ++i )
293 Strip & strip = *surface().strips[i];
294 // send zero for this strip
295 port_for_id(i).write( builder.zero_strip( strip ) );
299 // display the current start bank.
300 if ( mcu_port().emulation() == MackiePort::bcf2000 )
302 if ( _current_initial_bank == 0 )
304 // send Ar. to 2-char display on the master
305 mcu_port().write( builder.two_char_display( "Ar", ".." ) );
309 // write the current first remote_id to the 2-char display
310 mcu_port().write( builder.two_char_display( _current_initial_bank ) );
315 void MackieControlProtocol::zero_all()
317 // TODO turn off 55-char and SMPTE displays
319 if ( mcu_port().emulation() == MackiePort::bcf2000 )
321 // clear 2-char display
322 mcu_port().write( builder.two_char_display( "LC" ) );
326 for ( Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it )
328 port_for_id( (*it)->index() ).write( builder.zero_strip( **it ) );
331 // and the master strip
332 mcu_port().write( builder.zero_strip( master_strip() ) );
334 // and the led ring for the master strip, in bcf mode
335 if ( mcu_port().emulation() == MackiePort::bcf2000 )
337 Control & control = *surface().controls_by_name["jog"];
338 mcu_port().write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
341 // turn off global buttons and leds
342 // global buttons are only ever on mcu_port, so we don't have
343 // to figure out which port.
344 for ( Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it )
346 Control & control = **it;
347 if ( !control.group().is_strip() && control.accepts_feedback() )
349 mcu_port().write( builder.zero_control( control ) );
354 int MackieControlProtocol::set_active (bool yn)
360 // the reason for the locking and unlocking is that
361 // glibmm can't do a condition wait on a RecMutex
364 // TODO what happens if this fails half way?
365 cout << "set_active true" << endl;
367 // create MackiePorts
369 Glib::Mutex::Lock lock( update_mutex );
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 sophisticate 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
409 cout << "set_active false" << endl;
414 catch( exception & e )
416 cout << "set_active to false because " << e.what() << endl;
425 bool MackieControlProtocol::handle_strip_button( Control & control, ButtonState bs, boost::shared_ptr<Route> route )
431 if ( control.name() == "recenable" )
433 state = !route->record_enabled();
434 route->set_record_enable( state, this );
436 else if ( control.name() == "mute" )
438 state = !route->muted();
439 route->set_mute( state, this );
441 else if ( control.name() == "solo" )
443 state = !route->soloed();
444 route->set_solo( state, this );
446 else if ( control.name() == "select" )
448 // TODO make the track selected. Whatever that means.
449 //state = default_button_press( dynamic_cast<Button&>( control ) );
451 else if ( control.name() == "vselect" )
453 // TODO could be used to select different things to apply the pot to?
454 //state = default_button_press( dynamic_cast<Button&>( control ) );
458 if ( control.name() == "fader_touch" )
461 control.strip().gain().touch( state );
467 void MackieControlProtocol::update_led( Mackie::Button & button, Mackie::LedState ls )
469 MackiePort * port = 0;
470 if ( button.group().is_strip() )
472 if ( button.group().is_master() )
478 port = &port_for_id( dynamic_cast<const Strip&>( button.group() ).index() );
485 if ( ls != none ) port->write( builder.build_led( button, ls ) );
488 void MackieControlProtocol::update_global_button( const string & name, LedState ls )
490 if ( surface().controls_by_name.find( name ) !=surface().controls_by_name.end() )
492 Button * button = dynamic_cast<Button*>( surface().controls_by_name[name] );
493 mcu_port().write( builder.build_led( button->led(), ls ) );
497 cout << "Button " << name << " not found" << endl;
501 // send messages to surface to set controls to correct values
502 void MackieControlProtocol::update_surface()
506 // do the initial bank switch to connect signals
507 // _current_initial_bank is initialised by set_state
508 switch_banks( _current_initial_bank );
510 // create a RouteSignal for the master route
511 // but only the first time around
512 master_route_signal = shared_ptr<RouteSignal>( new RouteSignal( *master_route(), *this, master_strip(), mcu_port() ) );
513 // update strip from route
514 master_route_signal->notify_all();
516 // update global buttons and displays
517 notify_record_state_changed();
518 notify_transport_state_changed();
522 void MackieControlProtocol::connect_session_signals()
524 // receive routes added
525 connections_back = session->RouteAdded.connect( ( mem_fun (*this, &MackieControlProtocol::notify_route_added) ) );
526 // receive record state toggled
527 connections_back = session->RecordStateChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_record_state_changed) ) );
528 // receive transport state changed
529 connections_back = session->TransportStateChange.connect( ( mem_fun (*this, &MackieControlProtocol::notify_transport_state_changed) ) );
530 // receive punch-in and punch-out
531 connections_back = Config->ParameterChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_parameter_changed) ) );
532 // receive rude solo changed
533 connections_back = session->SoloActive.connect( ( mem_fun (*this, &MackieControlProtocol::notify_solo_active_changed) ) );
535 // make sure remote id changed signals reach here
536 // see also notify_route_added
537 Sorted sorted = get_sorted_routes();
538 for ( Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it )
540 (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
544 void MackieControlProtocol::add_port( MIDI::Port & midi_port, int number )
546 MackiePort * sport = new MackiePort( *this, midi_port, number );
547 _ports.push_back( sport );
549 connections_back = sport->init_event.connect(
551 mem_fun (*this, &MackieControlProtocol::handle_port_init)
556 connections_back = sport->active_event.connect(
558 mem_fun (*this, &MackieControlProtocol::handle_port_changed)
564 connections_back = sport->inactive_event.connect(
566 mem_fun (*this, &MackieControlProtocol::handle_port_changed)
572 _ports_changed = true;
575 void MackieControlProtocol::create_ports()
577 MIDI::Manager * mm = MIDI::Manager::instance();
581 MIDI::Port * midi_port = mm->port( default_port_name );
583 if ( midi_port == 0 ) {
585 os << string_compose( _("no MIDI port named \"%1\" exists - Mackie control disabled"), default_port_name );
586 error << os.str() << endmsg;
587 throw MackieControlException( os.str() );
589 add_port( *midi_port, 0 );
592 // open extender ports. Up to 9. Should be enough.
593 // could also use mm->get_midi_ports()
594 string ext_port_base = "mcu_xt_";
595 for ( int index = 1; index <= 9; ++index )
598 os << ext_port_base << index;
599 MIDI::Port * midi_port = mm->port( os.str() );
600 if ( midi_port != 0 ) add_port( *midi_port, index );
604 shared_ptr<Route> MackieControlProtocol::master_route()
606 shared_ptr<Route> retval;
607 retval = session->route_by_name( "master" );
610 // TODO search through all routes for one with the master attribute set
615 Strip & MackieControlProtocol::master_strip()
617 return dynamic_cast<Strip&>( *surface().groups["master"] );
620 void MackieControlProtocol::initialize_surface()
622 // set up the route table
624 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
626 strips += (*it)->strips();
629 set_route_table_size( strips );
631 switch ( mcu_port().emulation() )
633 case MackiePort::bcf2000: _surface = new BcfSurface( strips ); break;
634 case MackiePort::mackie: _surface = new MackieSurface( strips ); break;
637 os << "no Surface class found for emulation: " << mcu_port().emulation();
638 throw MackieControlException( os.str() );
642 // Connect events. Must be after route table otherwise there will be trouble
643 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
645 (*it)->control_event.connect( ( mem_fun (*this, &MackieControlProtocol::handle_control_event) ) );
649 void MackieControlProtocol::close()
651 // TODO disconnect port active/inactive signals
652 // Or at least put a lock here
656 // These will fail if the port has gone away.
657 // So catch the exception and do the rest of the
659 // because the bcf doesn't respond to the next 3 sysex messages
664 catch ( exception & e )
666 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
669 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
673 MackiePort & port = **it;
675 port.write_sysex( 0x61 );
677 port.write_sysex( 0x62 );
678 // Reset (reboot into offline mode)
679 port.write_sysex( 0x63 );
681 catch ( exception & e )
683 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
687 // disconnect global signals from Session
688 // TODO Since *this is a sigc::trackable, this shouldn't be necessary
689 for( vector<sigc::connection>::iterator it = _connections.begin(); it != _connections.end(); ++it )
694 // disconnect routes from strips
695 clear_route_signals();
698 // stop polling, and wait for it...
699 pthread_cancel_one( thread );
700 pthread_join( thread, 0 );
702 // shut down MackiePorts
703 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
712 void* MackieControlProtocol::_monitor_work (void* arg)
714 return static_cast<MackieControlProtocol*>(arg)->monitor_work ();
717 XMLNode & MackieControlProtocol::get_state()
719 cout << "MackieControlProtocol::get_state" << endl;
721 // add name of protocol
722 XMLNode* node = new XMLNode( X_("Protocol") );
723 node->add_property( X_("name"), _name );
727 os << _current_initial_bank;
728 node->add_property( X_("bank"), os.str() );
733 int MackieControlProtocol::set_state( const XMLNode & node )
735 cout << "MackieControlProtocol::set_state: active " << _active << endl;
738 // fetch current bank
739 if ( node.property( X_("bank") ) != 0 )
741 string bank = node.property( X_("bank") )->value();
745 uint32_t new_bank = atoi( bank.c_str() );
746 if ( _current_initial_bank != new_bank ) switch_banks( new_bank );
748 catch ( exception & e )
750 cout << "exception in MackieControlProtocol::set_state: " << e.what() << endl;
758 void MackieControlProtocol::handle_control_event( SurfacePort & port, Control & control, const ControlState & state )
760 uint32_t index = control.ordinal() - 1 + ( port.number() * port.strips() );
761 boost::shared_ptr<Route> route;
762 if ( control.group().is_strip() )
764 if ( control.group().is_master() )
766 route = master_route();
768 else if ( index < route_table.size() )
769 route = route_table[index];
771 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
774 // This handles control element events from the surface
775 // the state of the controls on the surface is usually updated
777 switch ( control.type() )
779 case Control::type_fader:
780 if ( control.group().is_strip() )
782 // find the route in the route table for the id
783 // if the route isn't available, skip it
784 // at which point the fader should just reset itself
787 route->set_gain( slider_position_to_gain( state.pos ), this );
789 // must echo bytes back to slider now, because
790 // the notifier only works if the fader is not being
791 // touched. Which it is if we're getting input.
792 port.write( builder.build_fader( (Fader&)control, state.pos ) );
798 boost::shared_ptr<Route> route = master_route();
801 route->set_gain( slider_position_to_gain( state.pos ), this );
802 port.write( builder.build_fader( (Fader&)control, state.pos ) );
807 case Control::type_button:
808 if ( control.group().is_strip() )
813 handle_strip_button( control, state.button_state, route );
817 // no route so always switch the light off
818 // because no signals will be emitted by a non-route
819 port.write( builder.build_led( control.led(), off ) );
822 else if ( control.group().is_master() )
824 // master fader touch
825 boost::shared_ptr<Route> route = master_route();
827 handle_strip_button( control, state.button_state, route );
831 // handle all non-strip buttons
832 surface().handle_button( *this, state.button_state, dynamic_cast<Button&>( control ) );
836 // pot (jog wheel, external control)
837 case Control::type_pot:
838 if ( control.group().is_strip() )
842 if ( route->panner().size() == 1 )
844 // assume pan for now
846 route->panner()[0]->get_effective_position (xpos);
848 // calculate new value, and trim
852 else if ( xpos < 0.0 )
855 route->panner()[0]->set_position( xpos );
860 // it's a pot for an umnapped route, so turn all the lights off
861 port.write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
866 if ( control.name() == "jog" )
868 // TODO use current snap-to setting?
869 long delta = state.ticks * 1000;
870 nframes_t next = session->transport_frame() + delta;
871 if ( delta < 0 && session->transport_frame() < (nframes_t) abs( delta ) )
873 next = session->current_start_frame();
875 else if ( next > session->current_end_frame() )
877 next = session->current_end_frame();
880 // doesn't work very well
881 session->request_locate( next, session->transport_rolling() );
883 // turn off the led ring, for bcf emulation mode
884 port.write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
888 cout << "external controller" << state.ticks << endl;
894 cout << "Control::type not handled: " << control.type() << endl;
898 /////////////////////////////////////////////////////////
899 // Notifications from UI
900 /////////////////////////////////////////////////////////
901 struct RouteSignalByRoute
903 RouteSignalByRoute( Route & route ): _route( route ) {}
905 bool operator () ( const RouteSignal & rs ) const
907 return rs.route().id() == _route.id();
910 bool operator () ( const RouteSignal * rs ) const
912 return rs->route().id() == _route.id();
918 Strip & MackieControlProtocol::strip_from_route( Route * route )
922 throw MackieControlException( "route is null in MackieControlProtocol::strip_from_route" );
925 if ( route->master() )
927 return master_strip();
930 RouteSignals::iterator it = find_if(
931 route_signals.begin()
932 , route_signals.end()
933 , RouteSignalByRoute( *route )
936 if ( it == route_signals.end() )
939 os << "No strip for route " << route->name();
940 throw MackieControlException( os.str() );
943 return (*it)->strip();
946 /////////////////////////////////////////////////
947 // handlers for Route signals
948 // TODO should these be part of RouteSignal?
949 /////////////////////////////////////////////////
951 void MackieControlProtocol::notify_solo_changed( ARDOUR::Route * route, MackiePort * port )
955 Button & button = strip_from_route( route ).solo();
956 port->write( builder.build_led( button, route->soloed() ) );
958 catch( exception & e )
960 cout << e.what() << endl;
964 void MackieControlProtocol::notify_mute_changed( ARDOUR::Route * route, MackiePort * port )
968 Button & button = strip_from_route( route ).mute();
969 port->write( builder.build_led( button, route->muted() ) );
971 catch( exception & e )
973 cout << e.what() << endl;
977 void MackieControlProtocol::notify_record_enable_changed( ARDOUR::Route * route, MackiePort * port )
981 Button & button = strip_from_route( route ).recenable();
982 port->write( builder.build_led( button, route->record_enabled() ) );
984 catch( exception & e )
986 cout << e.what() << endl;
990 void MackieControlProtocol::notify_gain_changed( ARDOUR::Route * route, MackiePort * port )
994 Fader & fader = strip_from_route( route ).gain();
995 if ( !fader.touch() )
997 port->write( builder.build_fader( fader, gain_to_slider_position( route->effective_gain() ) ) );
1000 catch( exception & e )
1002 cout << e.what() << endl;
1006 void MackieControlProtocol::notify_name_changed( void *, ARDOUR::Route * route, MackiePort * port )
1010 // TODO implement MackieControlProtocol::notify_name_changed
1012 catch( exception & e )
1014 cout << e.what() << endl;
1018 // TODO deal with > 1 channel being panned
1019 void MackieControlProtocol::notify_panner_changed( ARDOUR::Route * route, MackiePort * port )
1023 Pot & pot = strip_from_route( route ).vpot();
1024 if ( route->panner().size() == 1 )
1027 route->panner()[0]->get_effective_position( pos);
1028 port->write( builder.build_led_ring( pot, ControlState( on, pos ) ) );
1032 port->write( builder.zero_control( pot ) );
1035 catch( exception & e )
1037 cout << e.what() << endl;
1041 void MackieControlProtocol::poll_automation()
1043 for( RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it )
1045 // update strip from route
1046 ARDOUR::AutoState state = (*it)->route().gain_automation_state();
1047 if ( state == Touch || state == Play )
1049 (*it)->notify_all();
1054 /////////////////////////////////////
1055 // Transport Buttons
1056 /////////////////////////////////////
1058 LedState MackieControlProtocol::rewind_press( Button & button )
1060 // can use first_mark_before/after as well
1061 Location * loc = session->locations()->first_location_before (
1062 session->transport_frame()
1064 if ( loc != 0 ) session->request_locate( loc->start(), session->transport_rolling() );
1068 LedState MackieControlProtocol::rewind_release( Button & button )
1073 LedState MackieControlProtocol::ffwd_press( Button & button )
1075 // can use first_mark_before/after as well
1076 Location * loc = session->locations()->first_location_after (
1077 session->transport_frame()
1079 if ( loc != 0 ) session->request_locate( loc->start(), session->transport_rolling() );
1083 LedState MackieControlProtocol::ffwd_release( Button & button )
1088 LedState MackieControlProtocol::stop_press( Button & button )
1090 session->request_stop();
1094 LedState MackieControlProtocol::stop_release( Button & button )
1096 return session->transport_stopped();
1099 LedState MackieControlProtocol::play_press( Button & button )
1101 session->request_transport_speed( 1.0 );
1105 LedState MackieControlProtocol::play_release( Button & button )
1107 return session->transport_rolling();
1110 LedState MackieControlProtocol::record_press( Button & button )
1112 if ( session->get_record_enabled() )
1113 session->disable_record( false );
1115 session->maybe_enable_record();
1119 LedState MackieControlProtocol::record_release( Button & button )
1121 if ( session->get_record_enabled() )
1123 if ( session->transport_rolling() )
1132 ///////////////////////////////////////////
1134 ///////////////////////////////////////////
1136 void MackieControlProtocol::notify_parameter_changed( const char * name_str )
1138 string name( name_str );
1139 if ( name == "punch-in" )
1141 update_global_button( "punch_in", Config->get_punch_in() );
1143 else if ( name == "punch-out" )
1145 update_global_button( "punch_out", Config->get_punch_out() );
1149 // RouteList is the set of routes that have just been added
1150 void MackieControlProtocol::notify_route_added( ARDOUR::Session::RouteList & rl )
1152 // currently assigned banks are less than the full set of
1153 // strips, so activate the new strip now.
1154 if ( route_signals.size() < route_table.size() )
1156 refresh_current_bank();
1158 // otherwise route added, but current bank needs no updating
1160 // make sure remote id changes in the new route are handled
1161 typedef ARDOUR::Session::RouteList ARS;
1162 for ( ARS::iterator it = rl.begin(); it != rl.end(); ++it )
1164 (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
1168 void MackieControlProtocol::notify_solo_active_changed( bool active )
1170 Button * rude_solo = reinterpret_cast<Button*>( surface().controls_by_name["solo"] );
1171 mcu_port().write( builder.build_led( *rude_solo, active ? flashing : off ) );
1174 void MackieControlProtocol::notify_remote_id_changed()
1176 Sorted sorted = get_sorted_routes();
1178 // if a remote id has been moved off the end, we need to shift
1179 // the current bank backwards.
1180 if ( sorted.size() - _current_initial_bank < route_signals.size() )
1182 // but don't shift backwards past the zeroth channel
1183 switch_banks( max((Sorted::size_type) 0, sorted.size() - route_signals.size() ) );
1185 // Otherwise just refresh the current bank
1188 refresh_current_bank();
1192 ///////////////////////////////////////////
1193 // Transport signals
1194 ///////////////////////////////////////////
1196 void MackieControlProtocol::notify_record_state_changed()
1198 // switch rec button on / off / flashing
1199 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1200 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1203 void MackieControlProtocol::notify_transport_state_changed()
1205 // switch various play and stop buttons on / off
1206 update_global_button( "play", session->transport_rolling() );
1207 update_global_button( "stop", !session->transport_rolling() );
1208 update_global_button( "loop", session->get_play_loop() );
1210 // rec is special because it's tristate
1211 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1212 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1215 LedState MackieControlProtocol::loop_press( Button & button )
1217 session->request_play_loop( !session->get_play_loop() );
1221 LedState MackieControlProtocol::loop_release( Button & button )
1223 return session->get_play_loop();
1226 LedState MackieControlProtocol::punch_in_press( Button & button )
1228 bool state = !Config->get_punch_in();
1229 Config->set_punch_in( state );
1233 LedState MackieControlProtocol::punch_in_release( Button & button )
1235 return Config->get_punch_in();
1238 LedState MackieControlProtocol::punch_out_press( Button & button )
1240 bool state = !Config->get_punch_out();
1241 Config->set_punch_out( state );
1245 LedState MackieControlProtocol::punch_out_release( Button & button )
1247 return Config->get_punch_out();
1250 LedState MackieControlProtocol::home_press( Button & button )
1252 session->goto_start();
1256 LedState MackieControlProtocol::home_release( Button & button )
1261 LedState MackieControlProtocol::end_press( Button & button )
1263 session->goto_end();
1267 LedState MackieControlProtocol::end_release( Button & button )
1272 /////////////////////////////////////
1274 /////////////////////////////////////
1275 LedState MackieControlProtocol::left_press( Button & button )
1277 Sorted sorted = get_sorted_routes();
1278 if ( sorted.size() > route_table.size() )
1280 int new_initial = _current_initial_bank - route_table.size();
1281 if ( new_initial < 0 ) new_initial = 0;
1282 if ( new_initial != int( _current_initial_bank ) )
1284 session->set_dirty();
1285 switch_banks( new_initial );
1296 LedState MackieControlProtocol::left_release( Button & button )
1301 LedState MackieControlProtocol::right_press( Button & button )
1303 Sorted sorted = get_sorted_routes();
1304 if ( sorted.size() > route_table.size() )
1306 uint32_t delta = sorted.size() - ( route_table.size() + _current_initial_bank );
1307 if ( delta > route_table.size() ) delta = route_table.size();
1310 session->set_dirty();
1311 switch_banks( _current_initial_bank + delta );
1322 LedState MackieControlProtocol::right_release( Button & button )
1327 LedState MackieControlProtocol::channel_left_press( Button & button )
1329 Sorted sorted = get_sorted_routes();
1330 if ( sorted.size() > route_table.size() )
1341 LedState MackieControlProtocol::channel_left_release( Button & button )
1346 LedState MackieControlProtocol::channel_right_press( Button & button )
1348 Sorted sorted = get_sorted_routes();
1349 if ( sorted.size() > route_table.size() )
1360 LedState MackieControlProtocol::channel_right_release( Button & button )