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>
42 #include <ardour/route.h>
43 #include <ardour/session.h>
44 #include <ardour/location.h>
45 #include <ardour/dB.h>
46 #include <ardour/panner.h>
48 #include "mackie_control_protocol.h"
50 #include "midi_byte_array.h"
51 #include "mackie_control_exception.h"
52 #include "route_signal.h"
53 #include "mackie_midi_builder.h"
54 #include "surface_port.h"
56 #include "bcf_surface.h"
57 #include "mackie_surface.h"
59 using namespace ARDOUR;
62 using namespace Mackie;
65 using boost::shared_ptr;
69 MackieMidiBuilder builder;
71 // Copied from tranzport_control_protocol.cc
73 gain_to_slider_position (ARDOUR::gain_t g)
76 return pow((6.0*log(g)/log(2.0)+192.0)/198.0, 8.0);
80 Copied from tranzport_control_protocol.cc
81 TODO this seems to return slightly wrong values, namely
82 with the UI slider at max, we get a 0.99something value.
84 static inline ARDOUR::gain_t
85 slider_position_to_gain (double pos)
87 /* XXX Marcus writes: this doesn't seem right to me. but i don't have a better answer ... */
88 if (pos == 0.0) return 0;
89 return pow (2.0,(sqrt(sqrt(sqrt(pos)))*198.0-192.0)/6.0);
92 MackieControlProtocol::MackieControlProtocol (Session& session)
93 : ControlProtocol (session, X_("Mackie"))
94 , _current_initial_bank( 0 )
95 , connections_back( _connections )
97 , _ports_changed( false )
102 //cout << "MackieControlProtocol::MackieControlProtocol" << endl;
103 // will start reading from ports, as soon as there are some
104 pthread_create_and_store (X_("mackie monitor"), &thread, 0, _monitor_work, this);
107 MackieControlProtocol::~MackieControlProtocol()
109 //cout << "~MackieControlProtocol::MackieControlProtocol" << endl;
114 catch ( exception & e )
116 cout << "~MackieControlProtocol caught " << e.what() << endl;
120 cout << "~MackieControlProtocol caught unknown" << endl;
122 //cout << "finished ~MackieControlProtocol::MackieControlProtocol" << endl;
125 Mackie::Surface & MackieControlProtocol::surface()
129 throw MackieControlException( "_surface is 0 in MackieControlProtocol::surface" );
134 const Mackie::MackiePort & MackieControlProtocol::mcu_port() const
136 return dynamic_cast<const MackiePort &>( *_ports[0] );
139 Mackie::MackiePort & MackieControlProtocol::mcu_port()
141 return dynamic_cast<const MackiePort &>( *_ports[0] );
144 // go to the previous track.
145 // Assume that get_sorted_routes().size() > route_table.size()
146 void MackieControlProtocol::prev_track()
148 if ( _current_initial_bank >= 1 )
150 session->set_dirty();
151 switch_banks( _current_initial_bank - 1 );
155 // go to the next track.
156 // Assume that get_sorted_routes().size() > route_table.size()
157 void MackieControlProtocol::next_track()
159 Sorted sorted = get_sorted_routes();
160 if ( _current_initial_bank + route_table.size() < sorted.size() )
162 session->set_dirty();
163 switch_banks( _current_initial_bank + 1 );
167 void MackieControlProtocol::clear_route_signals()
169 for( RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it )
173 route_signals.clear();
176 // return the port for a given id - 0 based
177 // throws an exception if no port found
178 MackiePort & MackieControlProtocol::port_for_id( uint32_t index )
180 uint32_t current_max = 0;
181 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
183 current_max += (*it)->strips();
184 if ( index < current_max ) return **it;
187 // oops - no matching port
189 os << "No port for index " << index;
190 throw MackieControlException( os.str() );
193 // predicate for sort call in get_sorted_routes
194 struct RouteByRemoteId
196 bool operator () ( const shared_ptr<Route> & a, const shared_ptr<Route> & b ) const
198 return a->remote_control_id() < b->remote_control_id();
201 bool operator () ( const Route & a, const Route & b ) const
203 return a.remote_control_id() < b.remote_control_id();
206 bool operator () ( const Route * a, const Route * b ) const
208 return a->remote_control_id() < b->remote_control_id();
212 MackieControlProtocol::Sorted MackieControlProtocol::get_sorted_routes()
217 boost::shared_ptr<Session::RouteList> routes = session->get_routes();
218 set<uint32_t> remote_ids;
220 // routes with remote_id 0 should never be added
221 // TODO verify this with ardour devs
222 // remote_ids.insert( 0 );
224 // sort in remote_id order, and exclude master, control and hidden routes
225 // and any routes that are already set.
226 for ( Session::RouteList::iterator it = routes->begin(); it != routes->end(); ++it )
228 Route & route = **it;
234 && remote_ids.find( route.remote_control_id() ) == remote_ids.end()
237 sorted.push_back( *it );
238 remote_ids.insert( route.remote_control_id() );
241 sort( sorted.begin(), sorted.end(), RouteByRemoteId() );
245 void MackieControlProtocol::refresh_current_bank()
247 switch_banks( _current_initial_bank );
250 void MackieControlProtocol::switch_banks( int initial )
252 // DON'T prevent bank switch if initial == _current_initial_bank
253 // because then this method can't be used as a refresh
256 Sorted sorted = get_sorted_routes();
257 int delta = sorted.size() - route_table.size();
258 if ( initial < 0 || ( delta > 0 && initial > delta ) )
260 cout << "not switching to " << initial << endl;
263 _current_initial_bank = initial;
265 // first clear the signals from old routes
266 // taken care of by the RouteSignal destructors
267 clear_route_signals();
269 // now set the signals for new routes
270 if ( _current_initial_bank <= sorted.size() )
272 // fetch the bank start and end to switch to
273 uint32_t end_pos = min( route_table.size(), sorted.size() );
274 Sorted::iterator it = sorted.begin() + _current_initial_bank;
275 Sorted::iterator end = sorted.begin() + _current_initial_bank + end_pos;
276 //cout << "switch to " << _current_initial_bank << ", " << end_pos << endl;
278 // link routes to strips
280 for ( ; it != end && it != sorted.end(); ++it, ++i )
282 boost::shared_ptr<Route> route = *it;
283 Strip & strip = *surface().strips[i];
284 //cout << "remote id " << route->remote_control_id() << " connecting " << route->name() << " to " << strip.name() << " with port " << port_for_id(i) << endl;
285 route_table[i] = route;
286 RouteSignal * rs = new RouteSignal( *route, *this, strip, port_for_id(i) );
287 route_signals.push_back( rs );
288 // update strip from route
292 // create dead strips if there aren't enough routes to
294 for ( ; i < route_table.size(); ++i )
296 Strip & strip = *surface().strips[i];
297 // send zero for this strip
298 port_for_id(i).write( builder.zero_strip( strip ) );
302 // display the current start bank.
303 if ( mcu_port().emulation() == MackiePort::bcf2000 )
305 if ( _current_initial_bank == 0 )
307 // send Ar. to 2-char display on the master
308 mcu_port().write( builder.two_char_display( "Ar", ".." ) );
312 // write the current first remote_id to the 2-char display
313 mcu_port().write( builder.two_char_display( _current_initial_bank ) );
318 void MackieControlProtocol::zero_all()
320 // TODO turn off 55-char and SMPTE displays
322 if ( mcu_port().emulation() == MackiePort::bcf2000 )
324 // clear 2-char display
325 mcu_port().write( builder.two_char_display( "LC" ) );
329 for ( Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it )
331 port_for_id( (*it)->index() ).write( builder.zero_strip( **it ) );
334 // and the master strip
335 mcu_port().write( builder.zero_strip( master_strip() ) );
337 // and the led ring for the master strip, in bcf mode
338 if ( mcu_port().emulation() == MackiePort::bcf2000 )
340 Control & control = *surface().controls_by_name["jog"];
341 mcu_port().write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
344 // turn off global buttons and leds
345 // global buttons are only ever on mcu_port, so we don't have
346 // to figure out which port.
347 for ( Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it )
349 Control & control = **it;
350 if ( !control.group().is_strip() && control.accepts_feedback() )
352 mcu_port().write( builder.zero_control( control ) );
357 int MackieControlProtocol::set_active (bool yn)
363 // the reason for the locking and unlocking is that
364 // glibmm can't do a condition wait on a RecMutex
367 // TODO what happens if this fails half way?
369 // create MackiePorts
371 Glib::Mutex::Lock lock( update_mutex );
375 // make sure the ports are being listened to
378 // wait until poll thread is running, with ports to poll
379 // the mutex is only there because conditions require a mutex
381 Glib::Mutex::Lock lock( update_mutex );
382 while ( nfds == 0 ) update_cond.wait( update_mutex );
385 // now initialise MackiePorts - ie exchange sysex messages
386 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
391 // wait until all ports are active
392 // TODO a more sophisticated approach would
393 // allow things to start up with only an MCU, even if
394 // extenders were specified but not responding.
395 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
397 (*it)->wait_for_init();
400 // create surface object. This depends on the ports being
401 // correctly initialised
402 initialize_surface();
403 connect_session_signals();
408 // send current control positions to surface
409 // must come after _active = true otherwise it won't run
418 catch( exception & e )
420 cout << "set_active to false because exception caught: " << e.what() << endl;
429 bool MackieControlProtocol::handle_strip_button( Control & control, ButtonState bs, boost::shared_ptr<Route> route )
435 if ( control.name() == "recenable" )
437 state = !route->record_enabled();
438 route->set_record_enable( state, this );
440 else if ( control.name() == "mute" )
442 state = !route->muted();
443 route->set_mute( state, this );
445 else if ( control.name() == "solo" )
447 state = !route->soloed();
448 route->set_solo( state, this );
450 else if ( control.name() == "select" )
452 // TODO make the track selected. Whatever that means.
453 //state = default_button_press( dynamic_cast<Button&>( control ) );
455 else if ( control.name() == "vselect" )
457 // TODO could be used to select different things to apply the pot to?
458 //state = default_button_press( dynamic_cast<Button&>( control ) );
462 if ( control.name() == "fader_touch" )
465 control.strip().gain().touch( state );
471 void MackieControlProtocol::update_led( Mackie::Button & button, Mackie::LedState ls )
473 MackiePort * port = 0;
474 if ( button.group().is_strip() )
476 if ( button.group().is_master() )
482 port = &port_for_id( dynamic_cast<const Strip&>( button.group() ).index() );
489 if ( ls != none ) 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 ) );
501 cout << "Button " << name << " not found" << endl;
505 // send messages to surface to set controls to correct values
506 void MackieControlProtocol::update_surface()
510 // do the initial bank switch to connect signals
511 // _current_initial_bank is initialised by set_state
512 switch_banks( _current_initial_bank );
514 // create a RouteSignal for the master route
515 // but only the first time around
516 master_route_signal = shared_ptr<RouteSignal>( new RouteSignal( *master_route(), *this, master_strip(), mcu_port() ) );
517 // update strip from route
518 master_route_signal->notify_all();
520 // update global buttons and displays
521 notify_record_state_changed();
522 notify_transport_state_changed();
526 void MackieControlProtocol::connect_session_signals()
528 // receive routes added
529 connections_back = session->RouteAdded.connect( ( mem_fun (*this, &MackieControlProtocol::notify_route_added) ) );
530 // receive record state toggled
531 connections_back = session->RecordStateChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_record_state_changed) ) );
532 // receive transport state changed
533 connections_back = session->TransportStateChange.connect( ( mem_fun (*this, &MackieControlProtocol::notify_transport_state_changed) ) );
534 // receive punch-in and punch-out
535 connections_back = Config->ParameterChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_parameter_changed) ) );
536 // receive rude solo changed
537 connections_back = session->SoloActive.connect( ( mem_fun (*this, &MackieControlProtocol::notify_solo_active_changed) ) );
539 // make sure remote id changed signals reach here
540 // see also notify_route_added
541 Sorted sorted = get_sorted_routes();
542 for ( Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it )
544 connections_back = (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
548 void MackieControlProtocol::add_port( MIDI::Port & midi_port, int number )
550 MackiePort * sport = new MackiePort( *this, midi_port, number );
551 _ports.push_back( sport );
553 connections_back = sport->init_event.connect(
555 mem_fun (*this, &MackieControlProtocol::handle_port_init)
560 connections_back = sport->active_event.connect(
562 mem_fun (*this, &MackieControlProtocol::handle_port_active)
567 connections_back = sport->inactive_event.connect(
569 mem_fun (*this, &MackieControlProtocol::handle_port_inactive)
574 _ports_changed = true;
577 void MackieControlProtocol::create_ports()
579 MIDI::Manager * mm = MIDI::Manager::instance();
583 MIDI::Port * midi_port = mm->port( default_port_name );
585 if ( midi_port == 0 ) {
587 os << string_compose( _("no MIDI port named \"%1\" exists - Mackie control disabled"), default_port_name );
588 error << os.str() << endmsg;
589 throw MackieControlException( os.str() );
591 add_port( *midi_port, 0 );
594 // open extender ports. Up to 9. Should be enough.
595 // could also use mm->get_midi_ports()
596 string ext_port_base = "mcu_xt_";
597 for ( int index = 1; index <= 9; ++index )
600 os << ext_port_base << index;
601 MIDI::Port * midi_port = mm->port( os.str() );
602 if ( midi_port != 0 ) add_port( *midi_port, index );
606 shared_ptr<Route> MackieControlProtocol::master_route()
608 shared_ptr<Route> retval;
609 retval = session->route_by_name( "master" );
612 // TODO search through all routes for one with the master attribute set
617 Strip & MackieControlProtocol::master_strip()
619 return dynamic_cast<Strip&>( *surface().groups["master"] );
622 void MackieControlProtocol::initialize_surface()
624 // set up the route table
626 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
628 strips += (*it)->strips();
631 set_route_table_size( strips );
633 switch ( mcu_port().emulation() )
635 case MackiePort::bcf2000: _surface = new BcfSurface( strips ); break;
636 case MackiePort::mackie: _surface = new MackieSurface( strips ); break;
639 os << "no Surface class found for emulation: " << mcu_port().emulation();
640 throw MackieControlException( os.str() );
644 // Connect events. Must be after route table otherwise there will be trouble
645 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
647 connections_back = (*it)->control_event.connect( ( mem_fun (*this, &MackieControlProtocol::handle_control_event) ) );
651 void MackieControlProtocol::close()
653 // TODO disconnect port active/inactive signals
654 // Or at least put a lock here
656 // disconnect global signals from Session
657 // TODO Since *this is a sigc::trackable, this shouldn't be necessary
658 // but it is for some reason
660 for( vector<sigc::connection>::iterator it = _connections.begin(); it != _connections.end(); ++it )
668 // These will fail if the port has gone away.
669 // So catch the exception and do the rest of the
671 // because the bcf doesn't respond to the next 3 sysex messages
676 catch ( exception & e )
678 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
681 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
685 MackiePort & port = **it;
687 port.write_sysex( 0x61 );
689 port.write_sysex( 0x62 );
690 // Reset (reboot into offline mode)
691 port.write_sysex( 0x63 );
693 catch ( exception & e )
695 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
699 // disconnect routes from strips
700 clear_route_signals();
706 // stop polling, and wait for it...
708 pthread_join( thread, 0 );
710 // shut down MackiePorts
711 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
717 // this is done already in monitor_work. But it's here so we know.
723 void* MackieControlProtocol::_monitor_work (void* arg)
725 return static_cast<MackieControlProtocol*>(arg)->monitor_work ();
728 XMLNode & MackieControlProtocol::get_state()
730 //cout << "MackieControlProtocol::get_state" << endl;
732 // add name of protocol
733 XMLNode* node = new XMLNode( X_("Protocol") );
734 node->add_property( X_("name"), _name );
738 os << _current_initial_bank;
739 node->add_property( X_("bank"), os.str() );
744 int MackieControlProtocol::set_state( const XMLNode & node )
746 //cout << "MackieControlProtocol::set_state: active " << _active << endl;
749 // fetch current bank
750 if ( node.property( X_("bank") ) != 0 )
752 string bank = node.property( X_("bank") )->value();
756 uint32_t new_bank = atoi( bank.c_str() );
757 if ( _current_initial_bank != new_bank ) switch_banks( new_bank );
759 catch ( exception & e )
761 cout << "exception in MackieControlProtocol::set_state: " << e.what() << endl;
769 void MackieControlProtocol::handle_control_event( SurfacePort & port, Control & control, const ControlState & state )
771 uint32_t index = control.ordinal() - 1 + ( port.number() * port.strips() );
772 boost::shared_ptr<Route> route;
773 if ( control.group().is_strip() )
775 if ( control.group().is_master() )
777 route = master_route();
779 else if ( index < route_table.size() )
780 route = route_table[index];
782 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
785 // This handles control element events from the surface
786 // the state of the controls on the surface is usually updated
788 switch ( control.type() )
790 case Control::type_fader:
791 if ( control.group().is_strip() )
793 // find the route in the route table for the id
794 // if the route isn't available, skip it
795 // at which point the fader should just reset itself
798 route->set_gain( slider_position_to_gain( state.pos ), this );
800 // must echo bytes back to slider now, because
801 // the notifier only works if the fader is not being
802 // touched. Which it is if we're getting input.
803 port.write( builder.build_fader( (Fader&)control, state.pos ) );
809 boost::shared_ptr<Route> route = master_route();
812 route->set_gain( slider_position_to_gain( state.pos ), this );
813 port.write( builder.build_fader( (Fader&)control, state.pos ) );
818 case Control::type_button:
819 if ( control.group().is_strip() )
824 handle_strip_button( control, state.button_state, route );
828 // no route so always switch the light off
829 // because no signals will be emitted by a non-route
830 port.write( builder.build_led( control.led(), off ) );
833 else if ( control.group().is_master() )
835 // master fader touch
836 boost::shared_ptr<Route> route = master_route();
838 handle_strip_button( control, state.button_state, route );
842 // handle all non-strip buttons
843 surface().handle_button( *this, state.button_state, dynamic_cast<Button&>( control ) );
847 // pot (jog wheel, external control)
848 case Control::type_pot:
849 if ( control.group().is_strip() )
853 if ( route->panner().size() == 1 )
855 // assume pan for now
857 route->panner()[0]->get_effective_position (xpos);
859 // calculate new value, and trim
863 else if ( xpos < 0.0 )
866 route->panner()[0]->set_position( xpos );
871 // it's a pot for an umnapped route, so turn all the lights off
872 port.write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
877 if ( control.name() == "jog" )
879 // TODO use current snap-to setting?
880 long delta = state.ticks * 1000;
881 nframes_t next = session->transport_frame() + delta;
882 if ( delta < 0 && session->transport_frame() < (nframes_t) abs( delta ) )
884 next = session->current_start_frame();
886 else if ( next > session->current_end_frame() )
888 next = session->current_end_frame();
891 // doesn't work very well
892 session->request_locate( next, session->transport_rolling() );
894 // turn off the led ring, for bcf emulation mode
895 port.write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
899 cout << "external controller" << state.ticks << 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();
921 route_signal->port().write( builder.build_led( button, route_signal->route().soloed() ) );
923 catch( exception & e )
925 cout << e.what() << endl;
929 void MackieControlProtocol::notify_mute_changed( RouteSignal * route_signal )
933 Button & button = route_signal->strip().mute();
934 route_signal->port().write( builder.build_led( button, route_signal->route().muted() ) );
936 catch( exception & e )
938 cout << e.what() << endl;
942 void MackieControlProtocol::notify_record_enable_changed( RouteSignal * route_signal )
946 Button & button = route_signal->strip().recenable();
947 route_signal->port().write( builder.build_led( button, route_signal->route().record_enabled() ) );
949 catch( exception & e )
951 cout << e.what() << endl;
955 void MackieControlProtocol::notify_gain_changed( RouteSignal * route_signal )
959 Fader & fader = route_signal->strip().gain();
960 if ( !fader.touch() )
962 route_signal->port().write( builder.build_fader( fader, gain_to_slider_position( route_signal->route().effective_gain() ) ) );
965 catch( exception & e )
967 cout << e.what() << endl;
971 void MackieControlProtocol::notify_name_changed( void *, RouteSignal * route_signal )
975 // TODO implement MackieControlProtocol::notify_name_changed
977 catch( exception & e )
979 cout << e.what() << endl;
983 // TODO deal with > 1 channel being panned
984 void MackieControlProtocol::notify_panner_changed( RouteSignal * route_signal )
988 Pot & pot = route_signal->strip().vpot();
990 if ( route_signal->route().panner().size() == 1 )
993 route_signal->route().panner()[0]->get_effective_position( pos);
994 route_signal->port().write( builder.build_led_ring( pot, ControlState( on, pos ) ) );
998 route_signal->port().write( builder.zero_control( pot ) );
1001 catch( exception & e )
1003 cout << e.what() << endl;
1007 // TODO handle plugin automation polling
1008 void MackieControlProtocol::update_automation( RouteSignal & rs )
1010 ARDOUR::AutoState gain_state = rs.route().gain_automation_state();
1011 if ( gain_state == Touch || gain_state == Play )
1013 notify_gain_changed( &rs );
1016 ARDOUR::AutoState panner_state = rs.route().panner().automation_state();
1017 if ( panner_state == Touch || panner_state == Play )
1019 notify_panner_changed( &rs );
1023 void MackieControlProtocol::poll_automation()
1027 // do all currently mapped routes
1028 for( RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it )
1030 update_automation( **it );
1033 // and the master strip
1034 if ( master_route_signal != 0 ) update_automation( *master_route_signal );
1038 /////////////////////////////////////
1039 // Transport Buttons
1040 /////////////////////////////////////
1042 LedState MackieControlProtocol::frm_left_press( Button & button )
1044 // can use first_mark_before/after as well
1045 Location * loc = session->locations()->first_location_before (
1046 session->transport_frame()
1048 if ( loc != 0 ) session->request_locate( loc->start(), session->transport_rolling() );
1052 LedState MackieControlProtocol::frm_left_release( Button & button )
1057 LedState MackieControlProtocol::frm_right_press( Button & button )
1059 // can use first_mark_before/after as well
1060 Location * loc = session->locations()->first_location_after (
1061 session->transport_frame()
1063 if ( loc != 0 ) session->request_locate( loc->start(), session->transport_rolling() );
1067 LedState MackieControlProtocol::frm_right_release( Button & button )
1072 LedState MackieControlProtocol::stop_press( Button & button )
1074 session->request_stop();
1078 LedState MackieControlProtocol::stop_release( Button & button )
1080 return session->transport_stopped();
1083 LedState MackieControlProtocol::play_press( Button & button )
1085 session->request_transport_speed( 1.0 );
1089 LedState MackieControlProtocol::play_release( Button & button )
1091 return session->transport_rolling();
1094 LedState MackieControlProtocol::record_press( Button & button )
1096 if ( session->get_record_enabled() )
1097 session->disable_record( false );
1099 session->maybe_enable_record();
1103 LedState MackieControlProtocol::record_release( Button & button )
1105 if ( session->get_record_enabled() )
1107 if ( session->transport_rolling() )
1116 LedState MackieControlProtocol::rewind_press( Button & button )
1118 session->request_transport_speed( -2.0 );
1122 LedState MackieControlProtocol::rewind_release( Button & button )
1124 if ( _transport_previously_rolling )
1125 session->request_transport_speed( 1.0 );
1127 session->request_stop();
1131 LedState MackieControlProtocol::ffwd_press( Button & button )
1133 session->request_transport_speed( 2.0 );
1137 LedState MackieControlProtocol::ffwd_release( Button & button )
1139 if ( _transport_previously_rolling )
1140 session->request_transport_speed( 1.0 );
1142 session->request_stop();
1146 ///////////////////////////////////////////
1148 ///////////////////////////////////////////
1150 void MackieControlProtocol::notify_parameter_changed( const char * name_str )
1152 string name( name_str );
1153 if ( name == "punch-in" )
1155 update_global_button( "punch_in", Config->get_punch_in() );
1157 else if ( name == "punch-out" )
1159 update_global_button( "punch_out", Config->get_punch_out() );
1161 else if ( name == "clicking" )
1163 update_global_button( "clicking", Config->get_clicking() );
1167 cout << "parameter changed: " << name << endl;
1171 // RouteList is the set of routes that have just been added
1172 void MackieControlProtocol::notify_route_added( ARDOUR::Session::RouteList & rl )
1174 // currently assigned banks are less than the full set of
1175 // strips, so activate the new strip now.
1176 if ( route_signals.size() < route_table.size() )
1178 refresh_current_bank();
1180 // otherwise route added, but current bank needs no updating
1182 // make sure remote id changes in the new route are handled
1183 typedef ARDOUR::Session::RouteList ARS;
1184 for ( ARS::iterator it = rl.begin(); it != rl.end(); ++it )
1186 connections_back = (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
1190 void MackieControlProtocol::notify_solo_active_changed( bool active )
1192 Button * rude_solo = reinterpret_cast<Button*>( surface().controls_by_name["solo"] );
1193 mcu_port().write( builder.build_led( *rude_solo, active ? flashing : off ) );
1196 void MackieControlProtocol::notify_remote_id_changed()
1198 Sorted sorted = get_sorted_routes();
1200 // if a remote id has been moved off the end, we need to shift
1201 // the current bank backwards.
1202 if ( sorted.size() - _current_initial_bank < route_signals.size() )
1204 // but don't shift backwards past the zeroth channel
1205 switch_banks( max((Sorted::size_type) 0, sorted.size() - route_signals.size() ) );
1207 // Otherwise just refresh the current bank
1210 refresh_current_bank();
1214 ///////////////////////////////////////////
1215 // Transport signals
1216 ///////////////////////////////////////////
1218 void MackieControlProtocol::notify_record_state_changed()
1220 // switch rec button on / off / flashing
1221 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1222 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1225 void MackieControlProtocol::notify_transport_state_changed()
1227 // switch various play and stop buttons on / off
1228 update_global_button( "play", session->transport_rolling() );
1229 update_global_button( "stop", !session->transport_rolling() );
1230 update_global_button( "loop", session->get_play_loop() );
1232 _transport_previously_rolling = session->transport_rolling();
1234 // rec is special because it's tristate
1235 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1236 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1239 LedState MackieControlProtocol::loop_press( Button & button )
1241 session->request_play_loop( !session->get_play_loop() );
1245 LedState MackieControlProtocol::loop_release( Button & button )
1247 return session->get_play_loop();
1250 LedState MackieControlProtocol::punch_in_press( Button & button )
1252 bool state = !Config->get_punch_in();
1253 Config->set_punch_in( state );
1257 LedState MackieControlProtocol::punch_in_release( Button & button )
1259 return Config->get_punch_in();
1262 LedState MackieControlProtocol::punch_out_press( Button & button )
1264 bool state = !Config->get_punch_out();
1265 Config->set_punch_out( state );
1269 LedState MackieControlProtocol::punch_out_release( Button & button )
1271 return Config->get_punch_out();
1274 LedState MackieControlProtocol::home_press( Button & button )
1276 session->goto_start();
1280 LedState MackieControlProtocol::home_release( Button & button )
1285 LedState MackieControlProtocol::end_press( Button & button )
1287 session->goto_end();
1291 LedState MackieControlProtocol::end_release( Button & button )
1296 LedState MackieControlProtocol::clicking_press( Button & button )
1298 bool state = !Config->get_clicking();
1299 Config->set_clicking( state );
1303 LedState MackieControlProtocol::clicking_release( Button & button )
1305 return Config->get_clicking();
1308 LedState MackieControlProtocol::global_solo_press( Button & button )
1310 bool state = !session->soloing();
1311 session->set_all_solo ( state );
1315 LedState MackieControlProtocol::global_solo_release( Button & button )
1317 return session->soloing();
1320 /////////////////////////////////////
1322 /////////////////////////////////////
1323 LedState MackieControlProtocol::left_press( Button & button )
1325 Sorted sorted = get_sorted_routes();
1326 if ( sorted.size() > route_table.size() )
1328 int new_initial = _current_initial_bank - route_table.size();
1329 if ( new_initial < 0 ) new_initial = 0;
1330 if ( new_initial != int( _current_initial_bank ) )
1332 session->set_dirty();
1333 switch_banks( new_initial );
1344 LedState MackieControlProtocol::left_release( Button & button )
1349 LedState MackieControlProtocol::right_press( Button & button )
1351 Sorted sorted = get_sorted_routes();
1352 if ( sorted.size() > route_table.size() )
1354 uint32_t delta = sorted.size() - ( route_table.size() + _current_initial_bank );
1355 if ( delta > route_table.size() ) delta = route_table.size();
1358 session->set_dirty();
1359 switch_banks( _current_initial_bank + delta );
1370 LedState MackieControlProtocol::right_release( Button & button )
1375 LedState MackieControlProtocol::channel_left_press( Button & button )
1377 Sorted sorted = get_sorted_routes();
1378 if ( sorted.size() > route_table.size() )
1389 LedState MackieControlProtocol::channel_left_release( Button & button )
1394 LedState MackieControlProtocol::channel_right_press( Button & button )
1396 Sorted sorted = get_sorted_routes();
1397 if ( sorted.size() > route_table.size() )
1408 LedState MackieControlProtocol::channel_right_release( Button & button )
1413 /////////////////////////////////////
1415 /////////////////////////////////////
1416 LedState MackieControlProtocol::marker_press( Button & button )
1418 // cut'n'paste from LocationUI::add_new_location()
1420 nframes_t where = session->audible_frame();
1421 session->locations()->next_available_name(markername,"mcu");
1422 Location *location = new Location (where, where, markername, Location::IsMark);
1423 session->begin_reversible_command (_("add marker"));
1424 XMLNode &before = session->locations()->get_state();
1425 session->locations()->add (location, true);
1426 XMLNode &after = session->locations()->get_state();
1427 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1428 session->commit_reversible_command ();
1432 LedState MackieControlProtocol::marker_release( Button & button )