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 )
101 //cout << "MackieControlProtocol::MackieControlProtocol" << endl;
102 // will start reading from ports, as soon as there are some
103 pthread_create_and_store (X_("mackie monitor"), &thread, 0, _monitor_work, this);
106 MackieControlProtocol::~MackieControlProtocol()
108 //cout << "~MackieControlProtocol::MackieControlProtocol" << endl;
113 catch ( exception & e )
115 cout << "~MackieControlProtocol caught " << e.what() << endl;
119 cout << "~MackieControlProtocol caught unknown" << endl;
121 //cout << "finished ~MackieControlProtocol::MackieControlProtocol" << endl;
124 Mackie::Surface & MackieControlProtocol::surface()
128 throw MackieControlException( "_surface is 0 in MackieControlProtocol::surface" );
133 const Mackie::MackiePort & MackieControlProtocol::mcu_port() const
135 return dynamic_cast<const MackiePort &>( *_ports[0] );
138 Mackie::MackiePort & MackieControlProtocol::mcu_port()
140 return dynamic_cast<const MackiePort &>( *_ports[0] );
143 // go to the previous track.
144 // Assume that get_sorted_routes().size() > route_table.size()
145 void MackieControlProtocol::prev_track()
147 if ( _current_initial_bank >= 1 )
149 session->set_dirty();
150 switch_banks( _current_initial_bank - 1 );
154 // go to the next track.
155 // Assume that get_sorted_routes().size() > route_table.size()
156 void MackieControlProtocol::next_track()
158 Sorted sorted = get_sorted_routes();
159 if ( _current_initial_bank + route_table.size() < sorted.size() )
161 session->set_dirty();
162 switch_banks( _current_initial_bank + 1 );
166 void MackieControlProtocol::clear_route_signals()
168 for( RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it )
172 route_signals.clear();
175 // return the port for a given id - 0 based
176 // throws an exception if no port found
177 MackiePort & MackieControlProtocol::port_for_id( uint32_t index )
179 uint32_t current_max = 0;
180 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
182 current_max += (*it)->strips();
183 if ( index < current_max ) return **it;
186 // oops - no matching port
188 os << "No port for index " << index;
189 throw MackieControlException( os.str() );
192 // predicate for sort call in get_sorted_routes
193 struct RouteByRemoteId
195 bool operator () ( const shared_ptr<Route> & a, const shared_ptr<Route> & b ) const
197 return a->remote_control_id() < b->remote_control_id();
200 bool operator () ( const Route & a, const Route & b ) const
202 return a.remote_control_id() < b.remote_control_id();
205 bool operator () ( const Route * a, const Route * b ) const
207 return a->remote_control_id() < b->remote_control_id();
211 MackieControlProtocol::Sorted MackieControlProtocol::get_sorted_routes()
216 boost::shared_ptr<Session::RouteList> routes = session->get_routes();
217 set<uint32_t> remote_ids;
219 // routes with remote_id 0 should never be added
220 // TODO verify this with ardour devs
221 // remote_ids.insert( 0 );
223 // sort in remote_id order, and exclude master, control and hidden routes
224 // and any routes that are already set.
225 for ( Session::RouteList::iterator it = routes->begin(); it != routes->end(); ++it )
227 Route & route = **it;
233 && remote_ids.find( route.remote_control_id() ) == remote_ids.end()
236 sorted.push_back( *it );
237 remote_ids.insert( route.remote_control_id() );
240 sort( sorted.begin(), sorted.end(), RouteByRemoteId() );
244 void MackieControlProtocol::refresh_current_bank()
246 switch_banks( _current_initial_bank );
249 void MackieControlProtocol::switch_banks( int initial )
251 // DON'T prevent bank switch if initial == _current_initial_bank
252 // because then this method can't be used as a refresh
255 Sorted sorted = get_sorted_routes();
256 int delta = sorted.size() - route_table.size();
257 if ( initial < 0 || ( delta > 0 && initial > delta ) )
259 cout << "not switching to " << initial << endl;
262 _current_initial_bank = initial;
264 // first clear the signals from old routes
265 // taken care of by the RouteSignal destructors
266 clear_route_signals();
268 // now set the signals for new routes
269 if ( _current_initial_bank <= sorted.size() )
271 // fetch the bank start and end to switch to
272 uint32_t end_pos = min( route_table.size(), sorted.size() );
273 Sorted::iterator it = sorted.begin() + _current_initial_bank;
274 Sorted::iterator end = sorted.begin() + _current_initial_bank + end_pos;
275 //cout << "switch to " << _current_initial_bank << ", " << end_pos << endl;
277 // link routes to strips
279 for ( ; it != end && it != sorted.end(); ++it, ++i )
281 boost::shared_ptr<Route> route = *it;
282 Strip & strip = *surface().strips[i];
283 //cout << "remote id " << route->remote_control_id() << " connecting " << route->name() << " to " << strip.name() << " with port " << port_for_id(i) << endl;
284 route_table[i] = route;
285 RouteSignal * rs = new RouteSignal( *route, *this, strip, port_for_id(i) );
286 route_signals.push_back( rs );
287 // update strip from route
291 // create dead strips if there aren't enough routes to
293 for ( ; i < route_table.size(); ++i )
295 Strip & strip = *surface().strips[i];
296 // send zero for this strip
297 port_for_id(i).write( builder.zero_strip( strip ) );
301 // display the current start bank.
302 if ( mcu_port().emulation() == MackiePort::bcf2000 )
304 if ( _current_initial_bank == 0 )
306 // send Ar. to 2-char display on the master
307 mcu_port().write( builder.two_char_display( "Ar", ".." ) );
311 // write the current first remote_id to the 2-char display
312 mcu_port().write( builder.two_char_display( _current_initial_bank ) );
317 void MackieControlProtocol::zero_all()
319 // TODO turn off 55-char and SMPTE displays
321 if ( mcu_port().emulation() == MackiePort::bcf2000 )
323 // clear 2-char display
324 mcu_port().write( builder.two_char_display( "LC" ) );
328 for ( Surface::Strips::iterator it = surface().strips.begin(); it != surface().strips.end(); ++it )
330 port_for_id( (*it)->index() ).write( builder.zero_strip( **it ) );
333 // and the master strip
334 mcu_port().write( builder.zero_strip( master_strip() ) );
336 // and the led ring for the master strip, in bcf mode
337 if ( mcu_port().emulation() == MackiePort::bcf2000 )
339 Control & control = *surface().controls_by_name["jog"];
340 mcu_port().write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
343 // turn off global buttons and leds
344 // global buttons are only ever on mcu_port, so we don't have
345 // to figure out which port.
346 for ( Surface::Controls::iterator it = surface().controls.begin(); it != surface().controls.end(); ++it )
348 Control & control = **it;
349 if ( !control.group().is_strip() && control.accepts_feedback() )
351 mcu_port().write( builder.zero_control( control ) );
356 int MackieControlProtocol::set_active (bool yn)
362 // the reason for the locking and unlocking is that
363 // glibmm can't do a condition wait on a RecMutex
366 // TODO what happens if this fails half way?
368 // create MackiePorts
370 Glib::Mutex::Lock lock( update_mutex );
374 // make sure the ports are being listened to
377 // wait until poll thread is running, with ports to poll
378 // the mutex is only there because conditions require a mutex
380 Glib::Mutex::Lock lock( update_mutex );
381 while ( nfds == 0 ) update_cond.wait( update_mutex );
384 // now initialise MackiePorts - ie exchange sysex messages
385 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
390 // wait until all ports are active
391 // TODO a more sophisticated approach would
392 // allow things to start up with only an MCU, even if
393 // extenders were specified but not responding.
394 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
396 (*it)->wait_for_init();
399 // create surface object. This depends on the ports being
400 // correctly initialised
401 initialize_surface();
402 connect_session_signals();
407 // send current control positions to surface
408 // must come after _active = true otherwise it won't run
417 catch( exception & e )
419 cout << "set_active to false because exception caught: " << e.what() << endl;
428 bool MackieControlProtocol::handle_strip_button( Control & control, ButtonState bs, boost::shared_ptr<Route> route )
434 if ( control.name() == "recenable" )
436 state = !route->record_enabled();
437 route->set_record_enable( state, this );
439 else if ( control.name() == "mute" )
441 state = !route->muted();
442 route->set_mute( state, this );
444 else if ( control.name() == "solo" )
446 state = !route->soloed();
447 route->set_solo( state, this );
449 else if ( control.name() == "select" )
451 // TODO make the track selected. Whatever that means.
452 //state = default_button_press( dynamic_cast<Button&>( control ) );
454 else if ( control.name() == "vselect" )
456 // TODO could be used to select different things to apply the pot to?
457 //state = default_button_press( dynamic_cast<Button&>( control ) );
461 if ( control.name() == "fader_touch" )
464 control.strip().gain().touch( state );
470 void MackieControlProtocol::update_led( Mackie::Button & button, Mackie::LedState ls )
472 MackiePort * port = 0;
473 if ( button.group().is_strip() )
475 if ( button.group().is_master() )
481 port = &port_for_id( dynamic_cast<const Strip&>( button.group() ).index() );
488 if ( ls != none ) port->write( builder.build_led( button, ls ) );
491 void MackieControlProtocol::update_global_button( const string & name, LedState ls )
493 if ( surface().controls_by_name.find( name ) !=surface().controls_by_name.end() )
495 Button * button = dynamic_cast<Button*>( surface().controls_by_name[name] );
496 mcu_port().write( builder.build_led( button->led(), ls ) );
500 cout << "Button " << name << " not found" << endl;
504 // send messages to surface to set controls to correct values
505 void MackieControlProtocol::update_surface()
509 // do the initial bank switch to connect signals
510 // _current_initial_bank is initialised by set_state
511 switch_banks( _current_initial_bank );
513 // create a RouteSignal for the master route
514 // but only the first time around
515 master_route_signal = shared_ptr<RouteSignal>( new RouteSignal( *master_route(), *this, master_strip(), mcu_port() ) );
516 // update strip from route
517 master_route_signal->notify_all();
519 // update global buttons and displays
520 notify_record_state_changed();
521 notify_transport_state_changed();
525 void MackieControlProtocol::connect_session_signals()
527 // receive routes added
528 connections_back = session->RouteAdded.connect( ( mem_fun (*this, &MackieControlProtocol::notify_route_added) ) );
529 // receive record state toggled
530 connections_back = session->RecordStateChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_record_state_changed) ) );
531 // receive transport state changed
532 connections_back = session->TransportStateChange.connect( ( mem_fun (*this, &MackieControlProtocol::notify_transport_state_changed) ) );
533 // receive punch-in and punch-out
534 connections_back = Config->ParameterChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_parameter_changed) ) );
535 // receive rude solo changed
536 connections_back = session->SoloActive.connect( ( mem_fun (*this, &MackieControlProtocol::notify_solo_active_changed) ) );
538 // make sure remote id changed signals reach here
539 // see also notify_route_added
540 Sorted sorted = get_sorted_routes();
541 for ( Sorted::iterator it = sorted.begin(); it != sorted.end(); ++it )
543 connections_back = (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
547 void MackieControlProtocol::add_port( MIDI::Port & midi_port, int number )
549 MackiePort * sport = new MackiePort( *this, midi_port, number );
550 _ports.push_back( sport );
552 connections_back = sport->init_event.connect(
554 mem_fun (*this, &MackieControlProtocol::handle_port_init)
559 connections_back = sport->active_event.connect(
561 mem_fun (*this, &MackieControlProtocol::handle_port_active)
566 connections_back = sport->inactive_event.connect(
568 mem_fun (*this, &MackieControlProtocol::handle_port_inactive)
573 _ports_changed = true;
576 void MackieControlProtocol::create_ports()
578 MIDI::Manager * mm = MIDI::Manager::instance();
582 MIDI::Port * midi_port = mm->port( default_port_name );
584 if ( midi_port == 0 ) {
586 os << string_compose( _("no MIDI port named \"%1\" exists - Mackie control disabled"), default_port_name );
587 error << os.str() << endmsg;
588 throw MackieControlException( os.str() );
590 add_port( *midi_port, 0 );
593 // open extender ports. Up to 9. Should be enough.
594 // could also use mm->get_midi_ports()
595 string ext_port_base = "mcu_xt_";
596 for ( int index = 1; index <= 9; ++index )
599 os << ext_port_base << index;
600 MIDI::Port * midi_port = mm->port( os.str() );
601 if ( midi_port != 0 ) add_port( *midi_port, index );
605 shared_ptr<Route> MackieControlProtocol::master_route()
607 shared_ptr<Route> retval;
608 retval = session->route_by_name( "master" );
611 // TODO search through all routes for one with the master attribute set
616 Strip & MackieControlProtocol::master_strip()
618 return dynamic_cast<Strip&>( *surface().groups["master"] );
621 void MackieControlProtocol::initialize_surface()
623 // set up the route table
625 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
627 strips += (*it)->strips();
630 set_route_table_size( strips );
632 switch ( mcu_port().emulation() )
634 case MackiePort::bcf2000: _surface = new BcfSurface( strips ); break;
635 case MackiePort::mackie: _surface = new MackieSurface( strips ); break;
638 os << "no Surface class found for emulation: " << mcu_port().emulation();
639 throw MackieControlException( os.str() );
643 // Connect events. Must be after route table otherwise there will be trouble
644 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
646 connections_back = (*it)->control_event.connect( ( mem_fun (*this, &MackieControlProtocol::handle_control_event) ) );
650 void MackieControlProtocol::close()
652 // TODO disconnect port active/inactive signals
653 // Or at least put a lock here
655 // disconnect global signals from Session
656 // TODO Since *this is a sigc::trackable, this shouldn't be necessary
657 // but it is for some reason
659 for( vector<sigc::connection>::iterator it = _connections.begin(); it != _connections.end(); ++it )
667 // These will fail if the port has gone away.
668 // So catch the exception and do the rest of the
670 // because the bcf doesn't respond to the next 3 sysex messages
675 catch ( exception & e )
677 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
680 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
684 MackiePort & port = **it;
686 port.write_sysex( 0x61 );
688 port.write_sysex( 0x62 );
689 // Reset (reboot into offline mode)
690 port.write_sysex( 0x63 );
692 catch ( exception & e )
694 cout << "MackieControlProtocol::close caught exception: " << e.what() << endl;
698 // disconnect routes from strips
699 clear_route_signals();
705 // stop polling, and wait for it...
707 pthread_join( thread, 0 );
709 // shut down MackiePorts
710 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
716 // this is done already in monitor_work. But it's here so we know.
722 void* MackieControlProtocol::_monitor_work (void* arg)
724 return static_cast<MackieControlProtocol*>(arg)->monitor_work ();
727 XMLNode & MackieControlProtocol::get_state()
729 //cout << "MackieControlProtocol::get_state" << endl;
731 // add name of protocol
732 XMLNode* node = new XMLNode( X_("Protocol") );
733 node->add_property( X_("name"), _name );
737 os << _current_initial_bank;
738 node->add_property( X_("bank"), os.str() );
743 int MackieControlProtocol::set_state( const XMLNode & node )
745 //cout << "MackieControlProtocol::set_state: active " << _active << endl;
748 // fetch current bank
749 if ( node.property( X_("bank") ) != 0 )
751 string bank = node.property( X_("bank") )->value();
755 uint32_t new_bank = atoi( bank.c_str() );
756 if ( _current_initial_bank != new_bank ) switch_banks( new_bank );
758 catch ( exception & e )
760 cout << "exception in MackieControlProtocol::set_state: " << e.what() << endl;
768 void MackieControlProtocol::handle_control_event( SurfacePort & port, Control & control, const ControlState & state )
770 uint32_t index = control.ordinal() - 1 + ( port.number() * port.strips() );
771 boost::shared_ptr<Route> route;
772 if ( control.group().is_strip() )
774 if ( control.group().is_master() )
776 route = master_route();
778 else if ( index < route_table.size() )
779 route = route_table[index];
781 cerr << "Warning: index is " << index << " which is not in the route table, size: " << route_table.size() << endl;
784 // This handles control element events from the surface
785 // the state of the controls on the surface is usually updated
787 switch ( control.type() )
789 case Control::type_fader:
790 if ( control.group().is_strip() )
792 // find the route in the route table for the id
793 // if the route isn't available, skip it
794 // at which point the fader should just reset itself
797 route->set_gain( slider_position_to_gain( state.pos ), this );
799 // must echo bytes back to slider now, because
800 // the notifier only works if the fader is not being
801 // touched. Which it is if we're getting input.
802 port.write( builder.build_fader( (Fader&)control, state.pos ) );
808 boost::shared_ptr<Route> route = master_route();
811 route->set_gain( slider_position_to_gain( state.pos ), this );
812 port.write( builder.build_fader( (Fader&)control, state.pos ) );
817 case Control::type_button:
818 if ( control.group().is_strip() )
823 handle_strip_button( control, state.button_state, route );
827 // no route so always switch the light off
828 // because no signals will be emitted by a non-route
829 port.write( builder.build_led( control.led(), off ) );
832 else if ( control.group().is_master() )
834 // master fader touch
835 boost::shared_ptr<Route> route = master_route();
837 handle_strip_button( control, state.button_state, route );
841 // handle all non-strip buttons
842 surface().handle_button( *this, state.button_state, dynamic_cast<Button&>( control ) );
846 // pot (jog wheel, external control)
847 case Control::type_pot:
848 if ( control.group().is_strip() )
852 if ( route->panner().size() == 1 )
854 // assume pan for now
856 route->panner()[0]->get_effective_position (xpos);
858 // calculate new value, and trim
862 else if ( xpos < 0.0 )
865 route->panner()[0]->set_position( xpos );
870 // it's a pot for an umnapped route, so turn all the lights off
871 port.write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
876 if ( control.name() == "jog" )
878 // TODO use current snap-to setting?
879 long delta = state.ticks * 1000;
880 nframes_t next = session->transport_frame() + delta;
881 if ( delta < 0 && session->transport_frame() < (nframes_t) abs( delta ) )
883 next = session->current_start_frame();
885 else if ( next > session->current_end_frame() )
887 next = session->current_end_frame();
890 // doesn't work very well
891 session->request_locate( next, session->transport_rolling() );
893 // turn off the led ring, for bcf emulation mode
894 port.write( builder.build_led_ring( dynamic_cast<Pot &>( control ), off ) );
898 cout << "external controller" << state.ticks << endl;
904 cout << "Control::type not handled: " << control.type() << endl;
908 /////////////////////////////////////////////////
909 // handlers for Route signals
910 // TODO should these be part of RouteSignal?
911 // They started off as sigc handlers for signals
912 // from Route, but they're also used in polling for automation
913 /////////////////////////////////////////////////
915 void MackieControlProtocol::notify_solo_changed( RouteSignal * route_signal )
919 Button & button = route_signal->strip().solo();
920 route_signal->port().write( builder.build_led( button, route_signal->route().soloed() ) );
922 catch( exception & e )
924 cout << e.what() << endl;
928 void MackieControlProtocol::notify_mute_changed( RouteSignal * route_signal )
932 Button & button = route_signal->strip().mute();
933 route_signal->port().write( builder.build_led( button, route_signal->route().muted() ) );
935 catch( exception & e )
937 cout << e.what() << endl;
941 void MackieControlProtocol::notify_record_enable_changed( RouteSignal * route_signal )
945 Button & button = route_signal->strip().recenable();
946 route_signal->port().write( builder.build_led( button, route_signal->route().record_enabled() ) );
948 catch( exception & e )
950 cout << e.what() << endl;
954 void MackieControlProtocol::notify_gain_changed( RouteSignal * route_signal )
958 Fader & fader = route_signal->strip().gain();
959 if ( !fader.touch() )
961 route_signal->port().write( builder.build_fader( fader, gain_to_slider_position( route_signal->route().effective_gain() ) ) );
964 catch( exception & e )
966 cout << e.what() << endl;
970 void MackieControlProtocol::notify_name_changed( void *, RouteSignal * route_signal )
974 // TODO implement MackieControlProtocol::notify_name_changed
976 catch( exception & e )
978 cout << e.what() << endl;
982 // TODO deal with > 1 channel being panned
983 void MackieControlProtocol::notify_panner_changed( RouteSignal * route_signal )
987 Pot & pot = route_signal->strip().vpot();
989 if ( route_signal->route().panner().size() == 1 )
992 route_signal->route().panner()[0]->get_effective_position( pos);
993 route_signal->port().write( builder.build_led_ring( pot, ControlState( on, pos ) ) );
997 route_signal->port().write( builder.zero_control( pot ) );
1000 catch( exception & e )
1002 cout << e.what() << endl;
1006 // TODO handle plugin automation polling
1007 void MackieControlProtocol::update_automation( RouteSignal & rs )
1009 ARDOUR::AutoState gain_state = rs.route().gain_automation_state();
1010 if ( gain_state == Touch || gain_state == Play )
1012 notify_gain_changed( &rs );
1015 ARDOUR::AutoState panner_state = rs.route().panner().automation_state();
1016 if ( panner_state == Touch || panner_state == Play )
1018 notify_panner_changed( &rs );
1022 void MackieControlProtocol::poll_automation()
1026 // do all currently mapped routes
1027 for( RouteSignals::iterator it = route_signals.begin(); it != route_signals.end(); ++it )
1029 update_automation( **it );
1032 // and the master strip
1033 if ( master_route_signal != 0 ) update_automation( *master_route_signal );
1037 /////////////////////////////////////
1038 // Transport Buttons
1039 /////////////////////////////////////
1041 LedState MackieControlProtocol::frm_left_press( Button & button )
1043 // can use first_mark_before/after as well
1044 Location * loc = session->locations()->first_location_before (
1045 session->transport_frame()
1047 if ( loc != 0 ) session->request_locate( loc->start(), session->transport_rolling() );
1051 LedState MackieControlProtocol::frm_left_release( Button & button )
1056 LedState MackieControlProtocol::frm_right_press( Button & button )
1058 // can use first_mark_before/after as well
1059 Location * loc = session->locations()->first_location_after (
1060 session->transport_frame()
1062 if ( loc != 0 ) session->request_locate( loc->start(), session->transport_rolling() );
1066 LedState MackieControlProtocol::frm_right_release( Button & button )
1071 LedState MackieControlProtocol::stop_press( Button & button )
1073 session->request_stop();
1077 LedState MackieControlProtocol::stop_release( Button & button )
1079 return session->transport_stopped();
1082 LedState MackieControlProtocol::play_press( Button & button )
1084 session->request_transport_speed( 1.0 );
1088 LedState MackieControlProtocol::play_release( Button & button )
1090 return session->transport_rolling();
1093 LedState MackieControlProtocol::record_press( Button & button )
1095 if ( session->get_record_enabled() )
1096 session->disable_record( false );
1098 session->maybe_enable_record();
1102 LedState MackieControlProtocol::record_release( Button & button )
1104 if ( session->get_record_enabled() )
1106 if ( session->transport_rolling() )
1115 LedState MackieControlProtocol::rewind_press( Button & button )
1117 session->request_transport_speed( -2.0 );
1121 LedState MackieControlProtocol::rewind_release( Button & button )
1123 if ( _transport_previously_rolling )
1124 session->request_transport_speed( 1.0 );
1126 session->request_stop();
1130 LedState MackieControlProtocol::ffwd_press( Button & button )
1132 session->request_transport_speed( 2.0 );
1136 LedState MackieControlProtocol::ffwd_release( Button & button )
1138 if ( _transport_previously_rolling )
1139 session->request_transport_speed( 1.0 );
1141 session->request_stop();
1145 ///////////////////////////////////////////
1147 ///////////////////////////////////////////
1149 void MackieControlProtocol::notify_parameter_changed( const char * name_str )
1151 string name( name_str );
1152 if ( name == "punch-in" )
1154 update_global_button( "punch_in", Config->get_punch_in() );
1156 else if ( name == "punch-out" )
1158 update_global_button( "punch_out", Config->get_punch_out() );
1160 else if ( name == "clicking" )
1162 update_global_button( "clicking", Config->get_clicking() );
1166 cout << "parameter changed: " << name << endl;
1170 // RouteList is the set of routes that have just been added
1171 void MackieControlProtocol::notify_route_added( ARDOUR::Session::RouteList & rl )
1173 // currently assigned banks are less than the full set of
1174 // strips, so activate the new strip now.
1175 if ( route_signals.size() < route_table.size() )
1177 refresh_current_bank();
1179 // otherwise route added, but current bank needs no updating
1181 // make sure remote id changes in the new route are handled
1182 typedef ARDOUR::Session::RouteList ARS;
1183 for ( ARS::iterator it = rl.begin(); it != rl.end(); ++it )
1185 connections_back = (*it)->RemoteControlIDChanged.connect( ( mem_fun (*this, &MackieControlProtocol::notify_remote_id_changed) ) );
1189 void MackieControlProtocol::notify_solo_active_changed( bool active )
1191 Button * rude_solo = reinterpret_cast<Button*>( surface().controls_by_name["solo"] );
1192 mcu_port().write( builder.build_led( *rude_solo, active ? flashing : off ) );
1195 void MackieControlProtocol::notify_remote_id_changed()
1197 Sorted sorted = get_sorted_routes();
1199 // if a remote id has been moved off the end, we need to shift
1200 // the current bank backwards.
1201 if ( sorted.size() - _current_initial_bank < route_signals.size() )
1203 // but don't shift backwards past the zeroth channel
1204 switch_banks( max((Sorted::size_type) 0, sorted.size() - route_signals.size() ) );
1206 // Otherwise just refresh the current bank
1209 refresh_current_bank();
1213 ///////////////////////////////////////////
1214 // Transport signals
1215 ///////////////////////////////////////////
1217 void MackieControlProtocol::notify_record_state_changed()
1219 // switch rec button on / off / flashing
1220 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1221 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1224 void MackieControlProtocol::notify_transport_state_changed()
1226 // switch various play and stop buttons on / off
1227 update_global_button( "play", session->transport_rolling() );
1228 update_global_button( "stop", !session->transport_rolling() );
1229 update_global_button( "loop", session->get_play_loop() );
1231 _transport_previously_rolling = session->transport_rolling();
1233 // rec is special because it's tristate
1234 Button * rec = reinterpret_cast<Button*>( surface().controls_by_name["record"] );
1235 mcu_port().write( builder.build_led( *rec, record_release( *rec ) ) );
1238 LedState MackieControlProtocol::loop_press( Button & button )
1240 session->request_play_loop( !session->get_play_loop() );
1244 LedState MackieControlProtocol::loop_release( Button & button )
1246 return session->get_play_loop();
1249 LedState MackieControlProtocol::punch_in_press( Button & button )
1251 bool state = !Config->get_punch_in();
1252 Config->set_punch_in( state );
1256 LedState MackieControlProtocol::punch_in_release( Button & button )
1258 return Config->get_punch_in();
1261 LedState MackieControlProtocol::punch_out_press( Button & button )
1263 bool state = !Config->get_punch_out();
1264 Config->set_punch_out( state );
1268 LedState MackieControlProtocol::punch_out_release( Button & button )
1270 return Config->get_punch_out();
1273 LedState MackieControlProtocol::home_press( Button & button )
1275 session->goto_start();
1279 LedState MackieControlProtocol::home_release( Button & button )
1284 LedState MackieControlProtocol::end_press( Button & button )
1286 session->goto_end();
1290 LedState MackieControlProtocol::end_release( Button & button )
1295 LedState MackieControlProtocol::clicking_press( Button & button )
1297 bool state = !Config->get_clicking();
1298 Config->set_clicking( state );
1302 LedState MackieControlProtocol::clicking_release( Button & button )
1304 return Config->get_clicking();
1307 LedState MackieControlProtocol::global_solo_press( Button & button )
1309 bool state = !session->soloing();
1310 session->set_all_solo ( state );
1314 LedState MackieControlProtocol::global_solo_release( Button & button )
1316 return session->soloing();
1319 /////////////////////////////////////
1321 /////////////////////////////////////
1322 LedState MackieControlProtocol::left_press( Button & button )
1324 Sorted sorted = get_sorted_routes();
1325 if ( sorted.size() > route_table.size() )
1327 int new_initial = _current_initial_bank - route_table.size();
1328 if ( new_initial < 0 ) new_initial = 0;
1329 if ( new_initial != int( _current_initial_bank ) )
1331 session->set_dirty();
1332 switch_banks( new_initial );
1343 LedState MackieControlProtocol::left_release( Button & button )
1348 LedState MackieControlProtocol::right_press( Button & button )
1350 Sorted sorted = get_sorted_routes();
1351 if ( sorted.size() > route_table.size() )
1353 uint32_t delta = sorted.size() - ( route_table.size() + _current_initial_bank );
1354 if ( delta > route_table.size() ) delta = route_table.size();
1357 session->set_dirty();
1358 switch_banks( _current_initial_bank + delta );
1369 LedState MackieControlProtocol::right_release( Button & button )
1374 LedState MackieControlProtocol::channel_left_press( Button & button )
1376 Sorted sorted = get_sorted_routes();
1377 if ( sorted.size() > route_table.size() )
1388 LedState MackieControlProtocol::channel_left_release( Button & button )
1393 LedState MackieControlProtocol::channel_right_press( Button & button )
1395 Sorted sorted = get_sorted_routes();
1396 if ( sorted.size() > route_table.size() )
1407 LedState MackieControlProtocol::channel_right_release( Button & button )