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;
111 Mackie::Surface & MackieControlProtocol::surface()
115 throw MackieControlException( "_surface is 0 in MackieControlProtocol::surface" );
120 const Mackie::MackiePort & MackieControlProtocol::mcu_port() const
122 return dynamic_cast<const MackiePort &>( *_ports[0] );
125 Mackie::MackiePort & MackieControlProtocol::mcu_port()
127 return dynamic_cast<const MackiePort &>( *_ports[0] );
130 // go to the previous track.
131 // Assume that get_sorted_routes().size() > route_table.size()
132 void MackieControlProtocol::prev_track()
134 if ( _current_initial_bank >= 1 )
136 session->set_dirty();
137 switch_banks( _current_initial_bank - 1 );
141 // go to the next track.
142 // Assume that get_sorted_routes().size() > route_table.size()
143 void MackieControlProtocol::next_track()
145 Sorted sorted = get_sorted_routes();
146 if ( _current_initial_bank + route_table.size() < sorted.size() )
148 session->set_dirty();
149 switch_banks( _current_initial_bank + 1 );
153 void MackieControlProtocol::clear_route_signals()
155 for( RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it )
159 route_signals.clear();
162 // return the port for a given id - 0 based
163 // throws an exception if no port found
164 MackiePort & MackieControlProtocol::port_for_id( uint32_t index )
166 uint32_t current_max = 0;
167 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
169 current_max += (*it)->strips();
170 if ( index < current_max ) return **it;
173 // oops - no matching port
175 os << "No port for index " << index;
176 throw MackieControlException( os.str() );
179 // predicate for sort call in get_sorted_routes
180 struct RouteByRemoteId
182 bool operator () ( const shared_ptr<Route> & a, const shared_ptr<Route> & b ) const
184 return a->remote_control_id() < b->remote_control_id();
187 bool operator () ( const Route & a, const Route & b ) const
189 return a.remote_control_id() < b.remote_control_id();
192 bool operator () ( const Route * a, const Route * b ) const
194 return a->remote_control_id() < b->remote_control_id();
198 MackieControlProtocol::Sorted MackieControlProtocol::get_sorted_routes()
203 boost::shared_ptr<Session::RouteList> routes = session->get_routes();
204 set<uint32_t> remote_ids;
206 // routes with remote_id 0 should never be added
207 // TODO verify this with ardour devs
208 // remote_ids.insert( 0 );
210 // sort in remote_id order, and exclude master, control and hidden routes
211 // and any routes that are already set.
212 for ( Session::RouteList::iterator it = routes->begin(); it != routes->end(); ++it )
214 Route & route = **it;
220 && remote_ids.find( route.remote_control_id() ) == remote_ids.end()
223 sorted.push_back( *it );
224 remote_ids.insert( route.remote_control_id() );
227 sort( sorted.begin(), sorted.end(), RouteByRemoteId() );
231 void MackieControlProtocol::refresh_current_bank()
233 switch_banks( _current_initial_bank );
236 void MackieControlProtocol::switch_banks( int initial )
238 // DON'T prevent bank switch if initial == _current_initial_bank
239 // because then this method can't be used as a refresh
242 Sorted sorted = get_sorted_routes();
243 int delta = sorted.size() - route_table.size();
244 if ( initial < 0 || ( delta > 0 && initial > delta ) )
246 cout << "not switching to " << initial << endl;
249 _current_initial_bank = initial;
251 // first clear the signals from old routes
252 // taken care of by the RouteSignal destructors
253 clear_route_signals();
255 // now set the signals for new routes
256 if ( _current_initial_bank <= sorted.size() )
258 // fetch the bank start and end to switch to
259 uint32_t end_pos = min( route_table.size(), sorted.size() );
260 Sorted::iterator it = sorted.begin() + _current_initial_bank;
261 Sorted::iterator end = sorted.begin() + _current_initial_bank + end_pos;
262 //cout << "switch to " << _current_initial_bank << ", " << end_pos << endl;
264 // link routes to strips
266 for ( ; it != end && it != sorted.end(); ++it, ++i )
268 boost::shared_ptr<Route> route = *it;
269 Strip & strip = *surface().strips[i];
270 //cout << "remote id " << route->remote_control_id() << " connecting " << route->name() << " to " << strip.name() << " with port " << port_for_id(i) << endl;
271 route_table[i] = route;
272 RouteSignal * rs = new RouteSignal( *route, *this, strip, port_for_id(i) );
273 route_signals.push_back( rs );
274 // update strip from route
278 // create dead strips if there aren't enough routes to
280 for ( ; i < route_table.size(); ++i )
282 Strip & strip = *surface().strips[i];
283 // send zero for this strip
284 port_for_id(i).write( builder.zero_strip( strip ) );
288 // display the current start bank.
289 if ( mcu_port().emulation() == MackiePort::bcf2000 )
291 if ( _current_initial_bank == 0 )
293 // send Ar. to 2-char display on the master
294 mcu_port().write( builder.two_char_display( "Ar", ".." ) );
298 // write the current first remote_id to the 2-char display
299 mcu_port().write( builder.two_char_display( _current_initial_bank ) );
304 void MackieControlProtocol::zero_all()
306 // TODO turn off 55-char and SMPTE displays
308 if ( mcu_port().emulation() == MackiePort::bcf2000 )
310 // clear 2-char display
311 mcu_port().write( builder.two_char_display( "LC" ) );
315 for ( Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it )
317 port_for_id( (*it)->index() ).write( builder.zero_strip( **it ) );
320 // and the master strip
321 mcu_port().write( builder.zero_strip( master_strip() ) );
323 // and the led ring for the master strip, in bcf mode
324 if ( mcu_port().emulation() == MackiePort::bcf2000 )
326 Control & control = *surface().controls_by_name["jog"];
327 mcu_port().write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
330 // turn off global buttons and leds
331 // global buttons are only ever on mcu_port, so we don't have
332 // to figure out which port.
333 for ( Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it )
335 Control & control = **it;
336 if ( !control.group().is_strip() && control.accepts_feedback() )
338 mcu_port().write( builder.zero_control( control ) );
343 int MackieControlProtocol::set_active (bool yn)
349 // the reason for the locking and unlocking is that
350 // glibmm can't do a condition wait on a RecMutex
353 // TODO what happens if this fails half way?
354 cout << "set_active true" << endl;
356 // create MackiePorts
358 Glib::Mutex::Lock lock( update_mutex );
362 // wait until poll thread is running, with ports to poll
363 // the mutex is only there because conditions require a mutex
365 Glib::Mutex::Lock lock( update_mutex );
366 while ( nfds == 0 ) update_cond.wait( update_mutex );
369 // now initialise MackiePorts - ie exchange sysex messages
370 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
375 // wait until all ports are active
376 // TODO a more sophisticate approach would
377 // allow things to start up with only an MCU, even if
378 // extenders were specified but not responding.
379 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
381 (*it)->wait_for_init();
384 // create surface object. This depends on the ports being
385 // correctly initialised
386 initialize_surface();
387 connect_session_signals();
392 // send current control positions to surface
393 // must come after _active = true otherwise it won't run
398 cout << "set_active false" << endl;
403 catch( exception & e )
405 cout << "set_active to false because " << e.what() << endl;
414 bool MackieControlProtocol::handle_strip_button( Control & control, ButtonState bs, boost::shared_ptr<Route> route )
420 if ( control.name() == "recenable" )
422 state = !route->record_enabled();
423 route->set_record_enable( state, this );
425 else if ( control.name() == "mute" )
427 state = !route->muted();
428 route->set_mute( state, this );
430 else if ( control.name() == "solo" )
432 state = !route->soloed();
433 route->set_solo( state, this );
435 else if ( control.name() == "select" )
437 // TODO make the track selected. Whatever that means.
438 //state = default_button_press( dynamic_cast<Button&>( control ) );
440 else if ( control.name() == "vselect" )
442 // TODO could be used to select different things to apply the pot to?
443 //state = default_button_press( dynamic_cast<Button&>( control ) );
447 if ( control.name() == "fader_touch" )
450 control.strip().gain().touch( state );
456 void MackieControlProtocol::update_led( Mackie::Button & button, Mackie::LedState ls )
458 MackiePort * port = 0;
459 if ( button.group().is_strip() )
461 if ( button.group().is_master() )
467 port = &port_for_id( dynamic_cast<const Strip&>( button.group() ).index() );
474 if ( ls != none ) port->write( builder.build_led( button, ls ) );
477 void MackieControlProtocol::update_global_button( const string & name, LedState ls )
479 if ( surface().controls_by_name.find( name ) !=surface().controls_by_name.end() )
481 Button * button = dynamic_cast<Button*>( surface().controls_by_name[name] );
482 mcu_port().write( builder.build_led( button->led(), ls ) );
486 cout << "Button " << name << " not found" << endl;
490 // send messages to surface to set controls to correct values
491 void MackieControlProtocol::update_surface()
495 // do the initial bank switch to connect signals
496 // _current_initial_bank is initialised by set_state
497 switch_banks( _current_initial_bank );
499 // create a RouteSignal for the master route
500 // but only the first time around
501 master_route_signal = shared_ptr<RouteSignal>( new RouteSignal( *master_route(), *this, master_strip(), mcu_port() ) );
502 // update strip from route
503 master_route_signal->notify_all();
505 // update global buttons and displays
506 notify_record_state_changed();
507 notify_transport_state_changed();
511 void MackieControlProtocol::connect_session_signals()
513 // receive routes added
514 connections_back = session->RouteAdded.connect( ( mem_fun (*this, &MackieControlProtocol::notify_route_added) ) );
515 // receive record state toggled
516 connections_back = session->RecordStateChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_record_state_changed) ) );
517 // receive transport state changed
518 connections_back = session->TransportStateChange.connect( ( mem_fun (*this, &MackieControlProtocol::notify_transport_state_changed) ) );
519 // receive punch-in and punch-out
520 connections_back = Config->ParameterChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_parameter_changed) ) );
521 // receive rude solo changed
522 connections_back = session->SoloActive.connect( ( mem_fun (*this, &MackieControlProtocol::notify_solo_active_changed) ) );
524 // make sure remote id changed signals reach here
525 // see also notify_route_added
526 Sorted sorted = get_sorted_routes();
527 for ( Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it )
529 (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
533 void MackieControlProtocol::add_port( MIDI::Port & midi_port, int number )
535 MackiePort * sport = new MackiePort( *this, midi_port, number );
536 _ports.push_back( sport );
538 connections_back = sport->init_event.connect(
540 mem_fun (*this, &MackieControlProtocol::handle_port_init)
545 connections_back = sport->active_event.connect(
547 mem_fun (*this, &MackieControlProtocol::handle_port_changed)
553 connections_back = sport->inactive_event.connect(
555 mem_fun (*this, &MackieControlProtocol::handle_port_changed)
561 _ports_changed = true;
564 void MackieControlProtocol::create_ports()
566 MIDI::Manager * mm = MIDI::Manager::instance();
570 MIDI::Port * midi_port = mm->port( default_port_name );
572 if ( midi_port == 0 ) {
574 os << string_compose( _("no MIDI port named \"%1\" exists - Mackie control disabled"), default_port_name );
575 error << os.str() << endmsg;
576 throw MackieControlException( os.str() );
578 add_port( *midi_port, 0 );
581 // open extender ports. Up to 9. Should be enough.
582 // could also use mm->get_midi_ports()
583 string ext_port_base = "mcu_xt_";
584 for ( int index = 1; index <= 9; ++index )
587 os << ext_port_base << index;
588 MIDI::Port * midi_port = mm->port( os.str() );
589 if ( midi_port != 0 ) add_port( *midi_port, index );
593 shared_ptr<Route> MackieControlProtocol::master_route()
595 shared_ptr<Route> retval;
596 retval = session->route_by_name( "master" );
599 // TODO search through all routes for one with the master attribute set
604 Strip & MackieControlProtocol::master_strip()
606 return dynamic_cast<Strip&>( *surface().groups["master"] );
609 void MackieControlProtocol::initialize_surface()
611 // set up the route table
613 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
615 strips += (*it)->strips();
618 set_route_table_size( strips );
620 switch ( mcu_port().emulation() )
622 case MackiePort::bcf2000: _surface = new BcfSurface( strips ); break;
623 case MackiePort::mackie: _surface = new MackieSurface( strips ); break;
626 os << "no Surface class found for emulation: " << mcu_port().emulation();
627 throw MackieControlException( os.str() );
631 // Connect events. Must be after route table otherwise there will be trouble
632 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
634 (*it)->control_event.connect( ( mem_fun (*this, &MackieControlProtocol::handle_control_event) ) );
638 void MackieControlProtocol::close()
640 // TODO disconnect port active/inactive signals
641 // Or at least put a lock here
645 // These will fail if the port has gone away.
646 // So catch the exception and do the rest of the
648 // because the bcf doesn't respond to the next 3 sysex messages
653 catch ( exception & e )
655 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
658 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
662 MackiePort & port = **it;
664 port.write_sysex( 0x61 );
666 port.write_sysex( 0x62 );
667 // Reset (reboot into offline mode)
668 port.write_sysex( 0x63 );
670 catch ( exception & e )
672 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
676 // disconnect global signals from Session
677 // TODO Since *this is a sigc::trackable, this shouldn't be necessary
678 for( vector<sigc::connection>::iterator it = _connections.begin(); it != _connections.end(); ++it )
683 // disconnect routes from strips
684 clear_route_signals();
687 // stop polling, and wait for it...
688 pthread_cancel_one( thread );
689 pthread_join( thread, 0 );
691 // shut down MackiePorts
692 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
701 void* MackieControlProtocol::_monitor_work (void* arg)
703 return static_cast<MackieControlProtocol*>(arg)->monitor_work ();
706 XMLNode & MackieControlProtocol::get_state()
708 cout << "MackieControlProtocol::get_state" << endl;
710 // add name of protocol
711 XMLNode* node = new XMLNode( X_("Protocol") );
712 node->add_property( X_("name"), _name );
716 os << _current_initial_bank;
717 node->add_property( X_("bank"), os.str() );
722 int MackieControlProtocol::set_state( const XMLNode & node )
724 cout << "MackieControlProtocol::set_state: active " << _active << endl;
727 // fetch current bank
728 if ( node.property( X_("bank") ) != 0 )
730 string bank = node.property( X_("bank") )->value();
734 uint32_t new_bank = atoi( bank.c_str() );
735 if ( _current_initial_bank != new_bank ) switch_banks( new_bank );
737 catch ( exception & e )
739 cout << "exception in MackieControlProtocol::set_state: " << e.what() << endl;
747 void MackieControlProtocol::handle_control_event( SurfacePort & port, Control & control, const ControlState & state )
749 uint32_t index = control.ordinal() - 1 + ( port.number() * port.strips() );
750 boost::shared_ptr<Route> route;
751 if ( control.group().is_strip() )
753 if ( control.group().is_master() )
755 route = master_route();
757 else if ( index < route_table.size() )
758 route = route_table[index];
760 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
763 // This handles control element events from the surface
764 // the state of the controls on the surface is usually updated
766 switch ( control.type() )
768 case Control::type_fader:
769 if ( control.group().is_strip() )
771 // find the route in the route table for the id
772 // if the route isn't available, skip it
773 // at which point the fader should just reset itself
776 route->set_gain( slider_position_to_gain( state.pos ), this );
778 // must echo bytes back to slider now, because
779 // the notifier only works if the fader is not being
780 // touched. Which it is if we're getting input.
781 port.write( builder.build_fader( (Fader&)control, state.pos ) );
787 boost::shared_ptr<Route> route = master_route();
790 route->set_gain( slider_position_to_gain( state.pos ), this );
791 port.write( builder.build_fader( (Fader&)control, state.pos ) );
796 case Control::type_button:
797 if ( control.group().is_strip() )
802 handle_strip_button( control, state.button_state, route );
806 // no route so always switch the light off
807 // because no signals will be emitted by a non-route
808 port.write( builder.build_led( control.led(), off ) );
811 else if ( control.group().is_master() )
813 // master fader touch
814 boost::shared_ptr<Route> route = master_route();
816 handle_strip_button( control, state.button_state, route );
820 // handle all non-strip buttons
821 surface().handle_button( *this, state.button_state, dynamic_cast<Button&>( control ) );
825 // pot (jog wheel, external control)
826 case Control::type_pot:
827 if ( control.group().is_strip() )
831 if ( route->panner().size() == 1 )
833 // assume pan for now
835 route->panner()[0]->get_effective_position (xpos);
837 // calculate new value, and trim
841 else if ( xpos < 0.0 )
844 route->panner()[0]->set_position( xpos );
849 // it's a pot for an umnapped route, so turn all the lights off
850 port.write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
855 if ( control.name() == "jog" )
857 // TODO use current snap-to setting?
858 long delta = state.ticks * 1000;
859 nframes_t next = session->transport_frame() + delta;
860 if ( delta < 0 && session->transport_frame() < (nframes_t) abs( delta ) )
862 next = session->current_start_frame();
864 else if ( next > session->current_end_frame() )
866 next = session->current_end_frame();
869 // doesn't work very well
870 session->request_locate( next, session->transport_rolling() );
872 // turn off the led ring, for bcf emulation mode
873 port.write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
877 cout << "external controller" << state.ticks << endl;
883 cout << "Control::type not handled: " << control.type() << endl;
887 /////////////////////////////////////////////////////////
888 // Notifications from UI
889 /////////////////////////////////////////////////////////
890 struct RouteSignalByRoute
892 RouteSignalByRoute( Route & route ): _route( route ) {}
894 bool operator () ( const RouteSignal & rs ) const
896 return rs.route().id() == _route.id();
899 bool operator () ( const RouteSignal * rs ) const
901 return rs->route().id() == _route.id();
907 Strip & MackieControlProtocol::strip_from_route( Route * route )
911 throw MackieControlException( "route is null in MackieControlProtocol::strip_from_route" );
914 if ( route->master() )
916 return master_strip();
919 RouteSignals::iterator it = find_if(
920 route_signals.begin()
921 , route_signals.end()
922 , RouteSignalByRoute( *route )
925 if ( it == route_signals.end() )
928 os << "No strip for route " << route->name();
929 throw MackieControlException( os.str() );
932 return (*it)->strip();
935 /////////////////////////////////////////////////
936 // handlers for Route signals
937 // TODO should these be part of RouteSignal?
938 /////////////////////////////////////////////////
940 void MackieControlProtocol::notify_solo_changed( ARDOUR::Route * route, MackiePort * port )
944 Button & button = strip_from_route( route ).solo();
945 port->write( builder.build_led( button, route->soloed() ) );
947 catch( exception & e )
949 cout << e.what() << endl;
953 void MackieControlProtocol::notify_mute_changed( ARDOUR::Route * route, MackiePort * port )
957 Button & button = strip_from_route( route ).mute();
958 port->write( builder.build_led( button, route->muted() ) );
960 catch( exception & e )
962 cout << e.what() << endl;
966 void MackieControlProtocol::notify_record_enable_changed( ARDOUR::Route * route, MackiePort * port )
970 Button & button = strip_from_route( route ).recenable();
971 port->write( builder.build_led( button, route->record_enabled() ) );
973 catch( exception & e )
975 cout << e.what() << endl;
979 void MackieControlProtocol::notify_gain_changed( ARDOUR::Route * route, MackiePort * port )
983 Fader & fader = strip_from_route( route ).gain();
984 if ( !fader.touch() )
986 port->write( builder.build_fader( fader, gain_to_slider_position( route->gain() ) ) );
989 catch( exception & e )
991 cout << e.what() << endl;
995 void MackieControlProtocol::notify_name_changed( void *, ARDOUR::Route * route, MackiePort * port )
999 // TODO implement MackieControlProtocol::notify_name_changed
1001 catch( exception & e )
1003 cout << e.what() << endl;
1007 // TODO deal with > 1 channel being panned
1008 void MackieControlProtocol::notify_panner_changed( ARDOUR::Route * route, MackiePort * port )
1012 Pot & pot = strip_from_route( route ).vpot();
1013 if ( route->panner().size() == 1 )
1016 route->panner()[0]->get_effective_position( pos);
1017 port->write( builder.build_led_ring( pot, ControlState( on, pos ) ) );
1021 port->write( builder.zero_control( pot ) );
1024 catch( exception & e )
1026 cout << e.what() << endl;
1030 /////////////////////////////////////
1031 // Transport Buttons
1032 /////////////////////////////////////
1034 LedState MackieControlProtocol::rewind_press( Button & button )
1036 // can use first_mark_before/after as well
1037 Location * loc = session->locations()->first_location_before (
1038 session->transport_frame()
1040 if ( loc != 0 ) session->request_locate( loc->start(), session->transport_rolling() );
1044 LedState MackieControlProtocol::rewind_release( Button & button )
1049 LedState MackieControlProtocol::ffwd_press( Button & button )
1051 // can use first_mark_before/after as well
1052 Location * loc = session->locations()->first_location_after (
1053 session->transport_frame()
1055 if ( loc != 0 ) session->request_locate( loc->start(), session->transport_rolling() );
1059 LedState MackieControlProtocol::ffwd_release( Button & button )
1064 LedState MackieControlProtocol::stop_press( Button & button )
1066 session->request_stop();
1070 LedState MackieControlProtocol::stop_release( Button & button )
1072 return session->transport_stopped();
1075 LedState MackieControlProtocol::play_press( Button & button )
1077 session->request_transport_speed( 1.0 );
1081 LedState MackieControlProtocol::play_release( Button & button )
1083 return session->transport_rolling();
1086 LedState MackieControlProtocol::record_press( Button & button )
1088 if ( session->get_record_enabled() )
1089 session->disable_record( false );
1091 session->maybe_enable_record();
1095 LedState MackieControlProtocol::record_release( Button & button )
1097 if ( session->get_record_enabled() )
1099 if ( session->transport_rolling() )
1108 ///////////////////////////////////////////
1110 ///////////////////////////////////////////
1112 void MackieControlProtocol::notify_parameter_changed( const char * name_str )
1114 string name( name_str );
1115 if ( name == "punch-in" )
1117 update_global_button( "punch_in", Config->get_punch_in() );
1119 else if ( name == "punch-out" )
1121 update_global_button( "punch_out", Config->get_punch_out() );
1125 // RouteList is the set of routes that have just been added
1126 void MackieControlProtocol::notify_route_added( ARDOUR::Session::RouteList & rl )
1128 // currently assigned banks are less than the full set of
1129 // strips, so activate the new strip now.
1130 if ( route_signals.size() < route_table.size() )
1132 refresh_current_bank();
1134 // otherwise route added, but current bank needs no updating
1136 // make sure remote id changes in the new route are handled
1137 typedef ARDOUR::Session::RouteList ARS;
1138 for ( ARS::iterator it = rl.begin(); it != rl.end(); ++it )
1140 (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
1144 void MackieControlProtocol::notify_solo_active_changed( bool active )
1146 Button * rude_solo = reinterpret_cast<Button*>( surface().controls_by_name["solo"] );
1147 mcu_port().write( builder.build_led( *rude_solo, active ? flashing : off ) );
1150 void MackieControlProtocol::notify_remote_id_changed()
1152 Sorted sorted = get_sorted_routes();
1154 // if a remote id has been moved off the end, we need to shift
1155 // the current bank backwards.
1156 if ( sorted.size() - _current_initial_bank < route_signals.size() )
1158 // but don't shift backwards past the zeroth channel
1159 switch_banks( max((Sorted::size_type) 0, sorted.size() - route_signals.size() ) );
1161 // Otherwise just refresh the current bank
1164 refresh_current_bank();
1168 ///////////////////////////////////////////
1169 // Transport signals
1170 ///////////////////////////////////////////
1172 void MackieControlProtocol::notify_record_state_changed()
1174 // switch rec button on / off / flashing
1175 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1176 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1179 void MackieControlProtocol::notify_transport_state_changed()
1181 // switch various play and stop buttons on / off
1182 update_global_button( "play", session->transport_rolling() );
1183 update_global_button( "stop", !session->transport_rolling() );
1184 update_global_button( "loop", session->get_play_loop() );
1186 // rec is special because it's tristate
1187 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1188 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1191 LedState MackieControlProtocol::loop_press( Button & button )
1193 session->request_play_loop( !session->get_play_loop() );
1197 LedState MackieControlProtocol::loop_release( Button & button )
1199 return session->get_play_loop();
1202 LedState MackieControlProtocol::punch_in_press( Button & button )
1204 bool state = !Config->get_punch_in();
1205 Config->set_punch_in( state );
1209 LedState MackieControlProtocol::punch_in_release( Button & button )
1211 return Config->get_punch_in();
1214 LedState MackieControlProtocol::punch_out_press( Button & button )
1216 bool state = !Config->get_punch_out();
1217 Config->set_punch_out( state );
1221 LedState MackieControlProtocol::punch_out_release( Button & button )
1223 return Config->get_punch_out();
1226 LedState MackieControlProtocol::home_press( Button & button )
1228 session->goto_start();
1232 LedState MackieControlProtocol::home_release( Button & button )
1237 LedState MackieControlProtocol::end_press( Button & button )
1239 session->goto_end();
1243 LedState MackieControlProtocol::end_release( Button & button )
1248 /////////////////////////////////////
1250 /////////////////////////////////////
1251 LedState MackieControlProtocol::left_press( Button & button )
1253 Sorted sorted = get_sorted_routes();
1254 if ( sorted.size() > route_table.size() )
1256 int new_initial = _current_initial_bank - route_table.size();
1257 if ( new_initial < 0 ) new_initial = 0;
1258 if ( new_initial != int( _current_initial_bank ) )
1260 session->set_dirty();
1261 switch_banks( new_initial );
1272 LedState MackieControlProtocol::left_release( Button & button )
1277 LedState MackieControlProtocol::right_press( Button & button )
1279 Sorted sorted = get_sorted_routes();
1280 if ( sorted.size() > route_table.size() )
1282 uint32_t delta = sorted.size() - ( route_table.size() + _current_initial_bank );
1283 if ( delta > route_table.size() ) delta = route_table.size();
1286 session->set_dirty();
1287 switch_banks( _current_initial_bank + delta );
1298 LedState MackieControlProtocol::right_release( Button & button )
1303 LedState MackieControlProtocol::channel_left_press( Button & button )
1305 Sorted sorted = get_sorted_routes();
1306 if ( sorted.size() > route_table.size() )
1317 LedState MackieControlProtocol::channel_left_release( Button & button )
1322 LedState MackieControlProtocol::channel_right_press( Button & button )
1324 Sorted sorted = get_sorted_routes();
1325 if ( sorted.size() > route_table.size() )
1336 LedState MackieControlProtocol::channel_right_release( Button & button )