2 Copyright (C) 2015 Paul Davis
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 #include <glibmm/fileutils.h>
27 #include <glibmm/miscutils.h>
29 #include "pbd/controllable_descriptor.h"
30 #include "pbd/error.h"
31 #include "pbd/failed_constructor.h"
32 #include "pbd/file_utils.h"
33 #include "pbd/pthread_utils.h"
34 #include "pbd/compose.h"
35 #include "pbd/xml++.h"
37 #include "midi++/port.h"
39 #include "ardour/async_midi_port.h"
40 #include "ardour/audioengine.h"
41 #include "ardour/amp.h"
42 #include "ardour/bundle.h"
43 #include "ardour/debug.h"
44 #include "ardour/filesystem_paths.h"
45 #include "ardour/midi_port.h"
46 #include "ardour/midiport_manager.h"
47 #include "ardour/monitor_processor.h"
48 #include "ardour/profile.h"
49 #include "ardour/rc_configuration.h"
50 #include "ardour/route.h"
51 #include "ardour/session.h"
52 #include "ardour/session_configuration.h"
53 #include "ardour/track.h"
55 #include "faderport.h"
57 using namespace ARDOUR;
58 using namespace ArdourSurface;
65 #include "pbd/abstract_ui.cc" // instantiate template
67 FaderPort::FaderPort (Session& s)
68 : ControlProtocol (s, _("Faderport"))
69 , AbstractUI<FaderPortRequest> ("faderport")
71 , connection_state (ConnectionState (0))
72 , _device_active (false)
75 , fader_is_touched (false)
76 , button_state (ButtonState (0))
79 last_encoder_time = 0;
81 boost::shared_ptr<ARDOUR::Port> inp;
82 boost::shared_ptr<ARDOUR::Port> outp;
84 inp = AudioEngine::instance()->register_input_port (DataType::MIDI, "Faderport Recv", true);
85 outp = AudioEngine::instance()->register_output_port (DataType::MIDI, "Faderport Send", true);
87 _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(inp);
88 _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(outp);
90 if (_input_port == 0 || _output_port == 0) {
91 throw failed_constructor();
94 _input_bundle.reset (new ARDOUR::Bundle (_("Faderport Support (Receive)"), true));
95 _output_bundle.reset (new ARDOUR::Bundle (_("Faderport Support (Send) "), false));
97 _input_bundle->add_channel (
99 ARDOUR::DataType::MIDI,
100 session->engine().make_port_name_non_relative (inp->name())
103 _output_bundle->add_channel (
105 ARDOUR::DataType::MIDI,
106 session->engine().make_port_name_non_relative (outp->name())
110 TrackSelectionChanged.connect (selection_connection, MISSING_INVALIDATOR, boost::bind (&FaderPort::gui_track_selection_changed, this, _1), this);
112 /* Catch port connections and disconnections */
113 ARDOUR::AudioEngine::instance()->PortConnectedOrDisconnected.connect (port_connection, MISSING_INVALIDATOR, boost::bind (&FaderPort::connection_handler, this, _1, _2, _3, _4, _5), this);
115 buttons.insert (std::make_pair (Mute, Button (*this, _("Mute"), Mute, 21)));
116 buttons.insert (std::make_pair (Solo, Button (*this, _("Solo"), Solo, 22)));
117 buttons.insert (std::make_pair (Rec, Button (*this, _("Rec"), Rec, 23)));
118 buttons.insert (std::make_pair (Left, Button (*this, _("Left"), Left, 20)));
119 buttons.insert (std::make_pair (Bank, Button (*this, _("Bank"), Bank, 19)));
120 buttons.insert (std::make_pair (Right, Button (*this, _("Right"), Right, 18)));
121 buttons.insert (std::make_pair (Output, Button (*this, _("Output"), Output, 17)));
122 buttons.insert (std::make_pair (FP_Read, Button (*this, _("Read"), FP_Read, 13)));
123 buttons.insert (std::make_pair (FP_Write, Button (*this, _("Write"), FP_Write, 14)));
124 buttons.insert (std::make_pair (FP_Touch, Button (*this, _("Touch"), FP_Touch, 15)));
125 buttons.insert (std::make_pair (FP_Off, Button (*this, _("Off"), FP_Off, 16)));
126 buttons.insert (std::make_pair (Mix, Button (*this, _("Mix"), Mix, 12)));
127 buttons.insert (std::make_pair (Proj, Button (*this, _("Proj"), Proj, 11)));
128 buttons.insert (std::make_pair (Trns, Button (*this, _("Trns"), Trns, 10)));
129 buttons.insert (std::make_pair (Undo, Button (*this, _("Undo"), Undo, 9)));
130 buttons.insert (std::make_pair (Shift, Button (*this, _("Shift"), Shift, 5)));
131 buttons.insert (std::make_pair (Punch, Button (*this, _("Punch"), Punch, 6)));
132 buttons.insert (std::make_pair (User, Button (*this, _("User"), User, 7)));
133 buttons.insert (std::make_pair (Loop, Button (*this, _("Loop"), Loop, 8)));
134 buttons.insert (std::make_pair (Rewind, Button (*this, _("Rewind"), Rewind, 4)));
135 buttons.insert (std::make_pair (Ffwd, Button (*this, _("Ffwd"), Ffwd, 3)));
136 buttons.insert (std::make_pair (Stop, Button (*this, _("Stop"), Stop, 2)));
137 buttons.insert (std::make_pair (Play, Button (*this, _("Play"), Play, 1)));
138 buttons.insert (std::make_pair (RecEnable, Button (*this, _("RecEnable"), RecEnable, 0)));
139 buttons.insert (std::make_pair (FaderTouch, Button (*this, _("Fader (touch)"), FaderTouch, -1)));
141 get_button (Shift).set_flash (true);
142 get_button (Mix).set_flash (true);
143 get_button (Proj).set_flash (true);
144 get_button (Trns).set_flash (true);
145 get_button (User).set_flash (true);
147 get_button (Left).set_action ( boost::bind (&FaderPort::left, this), true);
148 get_button (Right).set_action ( boost::bind (&FaderPort::right, this), true);
150 get_button (Undo).set_action (boost::bind (&FaderPort::undo, this), true);
151 get_button (Undo).set_action (boost::bind (&FaderPort::redo, this), true, ShiftDown);
152 get_button (Undo).set_flash (true);
154 get_button (FP_Read).set_action (boost::bind (&FaderPort::read, this), true);
155 get_button (FP_Write).set_action (boost::bind (&FaderPort::write, this), true);
156 get_button (FP_Touch).set_action (boost::bind (&FaderPort::touch, this), true);
157 get_button (FP_Off).set_action (boost::bind (&FaderPort::off, this), true);
159 get_button (Play).set_action (boost::bind (&BasicUI::transport_play, this, true), true);
160 get_button (RecEnable).set_action (boost::bind (&BasicUI::rec_enable_toggle, this), true);
161 /* Stop is a modifier, so we have to use its own button state to get
162 the default action (since StopDown will be set when looking for the
165 get_button (Stop).set_action (boost::bind (&BasicUI::transport_stop, this), true, StopDown);
166 get_button (Ffwd).set_action (boost::bind (&BasicUI::ffwd, this), true);
168 /* See comments about Stop above .. */
169 get_button (Rewind).set_action (boost::bind (&BasicUI::rewind, this), true, RewindDown);
170 get_button (Rewind).set_action (boost::bind (&BasicUI::goto_zero, this), true, ButtonState (RewindDown|StopDown));
171 get_button (Rewind).set_action (boost::bind (&BasicUI::goto_start, this), true, ButtonState (RewindDown|ShiftDown));
173 get_button (Ffwd).set_action (boost::bind (&BasicUI::ffwd, this), true);
174 get_button (Ffwd).set_action (boost::bind (&BasicUI::goto_end, this), true, ShiftDown);
176 get_button (Punch).set_action (boost::bind (&FaderPort::punch, this), true);
178 get_button (Loop).set_action (boost::bind (&BasicUI::loop_toggle, this), true);
179 get_button (Loop).set_action (boost::bind (&BasicUI::add_marker, this, string()), true, ShiftDown);
181 get_button (Punch).set_action (boost::bind (&BasicUI::prev_marker, this), true, ShiftDown);
182 get_button (User).set_action (boost::bind (&BasicUI::next_marker, this), true, ShiftDown);
184 get_button (Mute).set_action (boost::bind (&FaderPort::mute, this), true);
185 get_button (Solo).set_action (boost::bind (&FaderPort::solo, this), true);
186 get_button (Rec).set_action (boost::bind (&FaderPort::rec_enable, this), true);
188 get_button (Output).set_action (boost::bind (&FaderPort::use_master, this), true);
189 get_button (Output).set_action (boost::bind (&FaderPort::use_monitor, this), true, ShiftDown);
192 FaderPort::~FaderPort ()
195 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("unregistering input port %1\n", boost::shared_ptr<ARDOUR::Port>(_input_port)->name()));
196 AudioEngine::instance()->unregister_port (_input_port);
197 _input_port.reset ();
201 // _output_port->drain (10000); //ToDo: is this necessary? It hangs the shutdown, for me
202 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("unregistering output port %1\n", boost::shared_ptr<ARDOUR::Port>(_output_port)->name()));
203 AudioEngine::instance()->unregister_port (_output_port);
204 _output_port.reset ();
211 FaderPort::start_midi_handling ()
213 /* handle device inquiry response */
214 _input_port->parser()->sysex.connect_same_thread (midi_connections, boost::bind (&FaderPort::sysex_handler, this, _1, _2, _3));
216 _input_port->parser()->poly_pressure.connect_same_thread (midi_connections, boost::bind (&FaderPort::button_handler, this, _1, _2));
218 _input_port->parser()->pitchbend.connect_same_thread (midi_connections, boost::bind (&FaderPort::encoder_handler, this, _1, _2));
220 _input_port->parser()->controller.connect_same_thread (midi_connections, boost::bind (&FaderPort::fader_handler, this, _1, _2));
222 /* This connection means that whenever data is ready from the input
223 * port, the relevant thread will invoke our ::midi_input_handler()
224 * method, which will read the data, and invoke the parser.
227 _input_port->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &FaderPort::midi_input_handler), _input_port));
228 _input_port->xthread().attach (main_loop()->get_context());
232 FaderPort::stop_midi_handling ()
234 midi_connections.drop_connections ();
236 /* Note: the input handler is still active at this point, but we're no
237 * longer connected to any of the parser signals
242 FaderPort::do_request (FaderPortRequest* req)
244 if (req->type == CallSlot) {
246 call_slot (MISSING_INVALIDATOR, req->the_slot);
248 } else if (req->type == Quit) {
263 FaderPort::thread_init ()
265 struct sched_param rtparam;
267 pthread_set_name (X_("FaderPort"));
269 PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("FaderPort"), 2048);
270 ARDOUR::SessionEvent::create_per_thread_pool (X_("FaderPort"), 128);
272 memset (&rtparam, 0, sizeof (rtparam));
273 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
275 if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
276 // do we care? not particularly.
281 FaderPort::all_lights_out ()
283 for (ButtonMap::iterator b = buttons.begin(); b != buttons.end(); ++b) {
284 b->second.set_led_state (_output_port, false, true);
289 FaderPort::get_button (ButtonID id) const
291 ButtonMap::const_iterator b = buttons.find (id);
292 assert (b != buttons.end());
293 return const_cast<Button&>(b->second);
297 FaderPort::button_long_press_timeout (ButtonID id)
299 if (buttons_down.find (id) != buttons_down.end()) {
300 get_button (id).invoke (ButtonState (LongPress|button_state), false);
302 /* release happened and somehow we were not cancelled */
305 return false; /* don't get called again */
309 FaderPort::start_press_timeout (Button& button, ButtonID id)
311 Glib::RefPtr<Glib::TimeoutSource> timeout = Glib::TimeoutSource::create (500); // milliseconds
312 button.timeout_connection = timeout->connect (sigc::bind (sigc::mem_fun (*this, &FaderPort::button_long_press_timeout), id));
313 timeout->attach (main_loop()->get_context());
317 FaderPort::button_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
319 ButtonID id (ButtonID (tb->controller_number));
320 Button& button (get_button (id));
323 buttons_down.insert (id);
325 buttons_down.erase (id);
326 button.timeout_connection.disconnect ();
329 ButtonState bs (ButtonState (0));
333 /* set this bit on press, do NOT clear it on release */
348 fader_is_touched = tb->value;
349 if (_current_route) {
350 boost::shared_ptr<AutomationControl> gain = _current_route->gain_control ();
352 framepos_t now = session->engine().sample_time();
354 gain->start_touch (now);
356 gain->stop_touch (true, now);
363 start_press_timeout (button, id);
369 button_state = (tb->value ? ButtonState (button_state|bs) : ButtonState (button_state&~bs));
370 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("reset button state to %1%2 using %3%4\n", hex, button_state, bs, dec));
373 if (button.uses_flash()) {
374 button.set_led_state (_output_port, (int)tb->value);
377 button.invoke (button_state, tb->value ? true : false);
379 if (!tb->value && (id != Shift)) {
380 /* non-shift key was released, clear shift modifier */
381 button_state = ButtonState (button_state&~ShiftDown);
382 DEBUG_TRACE (DEBUG::FaderPort, "clear shift modifier\n");
387 FaderPort::encoder_handler (MIDI::Parser &, MIDI::pitchbend_t pb)
395 //knob debouncing and hysteresis. The presonus encoder often sends bursts of events, or goes the wrong direction
397 last_last_encoder_delta = last_encoder_delta;
398 last_encoder_delta = delta;
399 microseconds_t now = get_microseconds ();
400 if ((now - last_encoder_time) < 10*1000) { //require at least 10ms interval between changes, because the device sometimes sends multiple deltas
403 if ((now - last_encoder_time) < 100*1000) { //avoid directional changes while "spinning", 100ms window
404 if ( (delta == last_encoder_delta) && (delta == last_last_encoder_delta) ) {
405 last_good_encoder_delta = delta; //3 in a row, grudgingly accept this as the new direction
407 if (delta != last_good_encoder_delta) { //otherwise ensure we keep going the same way
408 delta = last_good_encoder_delta;
410 } else { //we aren't yet in a spin window, just assume this move is really what we want
411 //NOTE: if you are worried about where these get initialized, here it is.
412 last_last_encoder_delta = delta;
413 last_encoder_delta = delta;
415 last_encoder_time = now;
416 last_good_encoder_delta = delta;
419 if (_current_route) {
421 ButtonState trim_modifier;
422 ButtonState width_modifier;
424 if (Profile->get_mixbus()) {
425 trim_modifier = ShiftDown;
426 width_modifier = ButtonState (0);
428 trim_modifier = UserDown;
429 width_modifier = ShiftDown;
432 if ((button_state & trim_modifier) == trim_modifier ) { // mod+encoder = input trim
433 boost::shared_ptr<AutomationControl> gain = _current_route->trim()->gain_control ();
435 float val = gain->get_user(); //for gain elements, the "user" value is in dB
439 } else if (width_modifier && ((button_state & width_modifier) == width_modifier)) {
440 ardour_pan_width (delta);
442 } else { // pan/balance
443 if (!Profile->get_mixbus()) {
444 ardour_pan_azimuth (delta);
453 FaderPort::fader_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
455 bool was_fader = false;
457 if (tb->controller_number == 0x0) {
458 fader_msb = tb->value;
460 } else if (tb->controller_number == 0x20) {
461 fader_lsb = tb->value;
466 if (_current_route) {
467 boost::shared_ptr<AutomationControl> gain = _current_route->gain_control ();
469 int ival = (fader_msb << 7) | fader_lsb;
470 float val = gain->interface_to_internal (ival/16384.0);
471 _current_route->set_gain (val, this);
478 FaderPort::sysex_handler (MIDI::Parser &p, MIDI::byte *buf, size_t sz)
484 if (buf[2] != 0x7f ||
497 _device_active = true;
499 DEBUG_TRACE (DEBUG::FaderPort, "FaderPort identified via MIDI Device Inquiry response\n");
501 /* put it into native mode */
503 MIDI::byte native[3];
508 _output_port->write (native, 3, 0);
512 /* catch up on state */
514 notify_transport_state_changed ();
515 notify_record_state_changed ();
519 FaderPort::set_active (bool yn)
521 DEBUG_TRACE (DEBUG::FaderPort, string_compose("MackieControlProtocol::set_active init with yn: '%1'\n", yn));
523 if (yn == active()) {
529 /* start event loop */
533 connect_session_signals ();
535 Glib::RefPtr<Glib::TimeoutSource> blink_timeout = Glib::TimeoutSource::create (200); // milliseconds
536 blink_connection = blink_timeout->connect (sigc::mem_fun (*this, &FaderPort::blink));
537 blink_timeout->attach (main_loop()->get_context());
546 ControlProtocol::set_active (yn);
548 DEBUG_TRACE (DEBUG::FaderPort, string_compose("MackieControlProtocol::set_active done with yn: '%1'\n", yn));
554 FaderPort::stop_blinking (ButtonID id)
556 blinkers.remove (id);
557 get_button (id).set_led_state (_output_port, false);
561 FaderPort::start_blinking (ButtonID id)
563 blinkers.push_back (id);
564 get_button (id).set_led_state (_output_port, true);
570 blink_state = !blink_state;
572 for (Blinkers::iterator b = blinkers.begin(); b != blinkers.end(); b++) {
573 get_button(*b).set_led_state (_output_port, blink_state);
584 stop_midi_handling ();
585 session_connections.drop_connections ();
586 port_connection.disconnect ();
587 blink_connection.disconnect ();
588 selection_connection.disconnect ();
589 route_connections.drop_connections ();
592 route_connections.drop_connections ();
597 FaderPort::notify_record_state_changed ()
599 switch (session->record_status()) {
600 case Session::Disabled:
601 stop_blinking (RecEnable);
603 case Session::Enabled:
604 start_blinking (RecEnable);
606 case Session::Recording:
607 stop_blinking (RecEnable);
613 FaderPort::notify_transport_state_changed ()
615 get_button (Loop).set_led_state (_output_port, session->get_play_loop());
616 get_button (Play).set_led_state (_output_port, session->transport_speed() == 1.0);
617 get_button (Stop).set_led_state (_output_port, session->transport_stopped ());
618 get_button (Rewind).set_led_state (_output_port, session->transport_speed() < 0.0);
619 get_button (Ffwd).set_led_state (_output_port, session->transport_speed() > 1.0);
623 FaderPort::parameter_changed (string what)
625 if (what == "punch-in" || what == "punch-out") {
626 bool in = session->config.get_punch_in ();
627 bool out = session->config.get_punch_out ();
629 get_button (Punch).set_led_state (_output_port, true);
630 blinkers.remove (Punch);
631 } else if (in || out) {
632 start_blinking (Punch);
634 stop_blinking (Punch);
640 FaderPort::connect_session_signals()
642 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::notify_record_state_changed, this), this);
643 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::notify_transport_state_changed, this), this);
644 /* not session, but treat it similarly */
645 session->config.ParameterChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::parameter_changed, this, _1), this);
649 FaderPort::midi_input_handler (Glib::IOCondition ioc, boost::shared_ptr<ARDOUR::AsyncMIDIPort> port)
651 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("something happend on %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
663 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("data available on %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
664 framepos_t now = session->engine().sample_time();
673 FaderPort::get_state ()
675 XMLNode& node (ControlProtocol::get_state());
679 child = new XMLNode (X_("Input"));
680 child->add_child_nocopy (boost::shared_ptr<ARDOUR::Port>(_input_port)->get_state());
681 node.add_child_nocopy (*child);
684 child = new XMLNode (X_("Output"));
685 child->add_child_nocopy (boost::shared_ptr<ARDOUR::Port>(_output_port)->get_state());
686 node.add_child_nocopy (*child);
688 /* Save action state for Mix, Proj, Trns and User buttons, since these
689 * are user controlled. We can only save named-action operations, since
690 * internal functions are just pointers to functions and hard to
691 * serialize without enumerating them all somewhere.
694 node.add_child_nocopy (get_button (Mix).get_state());
695 node.add_child_nocopy (get_button (Proj).get_state());
696 node.add_child_nocopy (get_button (Trns).get_state());
697 node.add_child_nocopy (get_button (User).get_state());
703 FaderPort::set_state (const XMLNode& node, int version)
706 XMLNodeConstIterator niter;
707 XMLNode const* child;
709 if (ControlProtocol::set_state (node, version)) {
713 if ((child = node.child (X_("Input"))) != 0) {
714 XMLNode* portnode = child->child (Port::state_node_name.c_str());
716 boost::shared_ptr<ARDOUR::Port>(_input_port)->set_state (*portnode, version);
720 if ((child = node.child (X_("Output"))) != 0) {
721 XMLNode* portnode = child->child (Port::state_node_name.c_str());
723 boost::shared_ptr<ARDOUR::Port>(_output_port)->set_state (*portnode, version);
727 for (XMLNodeList::const_iterator n = node.children().begin(); n != node.children().end(); ++n) {
728 if ((*n)->name() == X_("Button")) {
729 XMLProperty const * prop = (*n)->property (X_("id"));
733 int xid = atoi (prop->value());
734 ButtonMap::iterator b = buttons.find (ButtonID (xid));
735 if (b == buttons.end()) {
738 b->second.set_state (**n);
746 FaderPort::connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1, boost::weak_ptr<ARDOUR::Port>, std::string name2, bool yn)
748 if (!_input_port || !_output_port) {
752 string ni = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_input_port)->name());
753 string no = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_output_port)->name());
755 if (ni == name1 || ni == name2) {
757 connection_state |= InputConnected;
759 connection_state &= ~InputConnected;
761 } else if (no == name1 || no == name2) {
763 connection_state |= OutputConnected;
765 connection_state &= ~OutputConnected;
772 if ((connection_state & (InputConnected|OutputConnected)) == (InputConnected|OutputConnected)) {
774 /* XXX this is a horrible hack. Without a short sleep here,
775 something prevents the device wakeup messages from being
776 sent and/or the responses from being received.
783 DEBUG_TRACE (DEBUG::FaderPort, "Device disconnected (input or output or both) or not yet fully connected\n");
784 _device_active = false;
787 ConnectionChange (); /* emit signal for our GUI */
789 return true; /* connection status changed */
793 FaderPort::connected ()
795 DEBUG_TRACE (DEBUG::FaderPort, "connection status changed\n");
797 start_midi_handling ();
799 /* send device inquiry */
810 _output_port->write (buf, 6, 0);
814 FaderPort::Button::invoke (FaderPort::ButtonState bs, bool press)
816 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("invoke button %1 for %2 state %3%4%5\n", id, (press ? "press":"release"), hex, bs, dec));
821 if ((x = on_press.find (bs)) == on_press.end()) {
822 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("no press action for button %1 state %2%3\%4\n", id, hex, bs, dec));
826 if ((x = on_release.find (bs)) == on_release.end()) {
827 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("no release action for button %1 state %2%3\%4\n", id, hex, bs, dec));
832 switch (x->second.type) {
834 if (!x->second.action_name.empty()) {
835 fp.access_action (x->second.action_name);
838 case InternalFunction:
839 if (x->second.function) {
840 x->second.function ();
846 FaderPort::Button::set_action (string const& name, bool when_pressed, FaderPort::ButtonState bs)
850 todo.type = NamedAction;
856 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("set button %1 to action %2 on press + %3%4%5\n", id, name, bs));
857 todo.action_name = name;
862 on_release.erase (bs);
864 DEBUG_TRACE (DEBUG::FaderPort, string_compose ("set button %1 to action %2 on release + %3%4%5\n", id, name, bs));
865 todo.action_name = name;
866 on_release[bs] = todo;
872 FaderPort::Button::get_action (bool press, FaderPort::ButtonState bs)
877 if ((x = on_press.find (bs)) == on_press.end()) {
880 if (x->second.type != NamedAction) {
883 return x->second.action_name;
885 if ((x = on_release.find (bs)) == on_release.end()) {
888 if (x->second.type != NamedAction) {
891 return x->second.action_name;
896 FaderPort::Button::set_action (boost::function<void()> f, bool when_pressed, FaderPort::ButtonState bs)
899 todo.type = InternalFunction;
906 on_release[bs] = todo;
911 FaderPort::Button::set_led_state (boost::shared_ptr<MIDI::Port> port, int onoff, bool force)
913 if (!force && (led_on == (bool) onoff)) {
919 /* fader button ID - no LED */
926 buf[2] = onoff ? 1 : 0;
927 port->write (buf, 3, 0);
928 led_on = (onoff ? true : false);
932 FaderPort::Button::set_state (XMLNode const& node)
934 const XMLProperty* prop = node.property ("id");
939 int xid = atoi (prop->value());
944 typedef pair<string,FaderPort::ButtonState> state_pair_t;
945 vector<state_pair_t> state_pairs;
947 state_pairs.push_back (make_pair (string ("plain"), ButtonState (0)));
948 state_pairs.push_back (make_pair (string ("shift"), ShiftDown));
949 state_pairs.push_back (make_pair (string ("long"), LongPress));
954 for (vector<state_pair_t>::const_iterator sp = state_pairs.begin(); sp != state_pairs.end(); ++sp) {
957 propname = sp->first + X_("-press");
958 if ((prop = node.property (propname)) != 0) {
959 set_action (prop->value(), true, sp->second);
962 propname = sp->first + X_("-release");
963 if ((prop = node.property (propname)) != 0) {
964 set_action (prop->value(), false, sp->second);
972 FaderPort::Button::get_state () const
974 XMLNode* node = new XMLNode (X_("Button"));
976 snprintf (buf, sizeof (buf), "%d", id);
978 node->add_property (X_("id"), buf);
980 ToDoMap::const_iterator x;
982 null.type = NamedAction;
984 typedef pair<string,FaderPort::ButtonState> state_pair_t;
985 vector<state_pair_t> state_pairs;
987 state_pairs.push_back (make_pair (string ("plain"), ButtonState (0)));
988 state_pairs.push_back (make_pair (string ("shift"), ShiftDown));
989 state_pairs.push_back (make_pair (string ("long"), LongPress));
991 for (vector<state_pair_t>::const_iterator sp = state_pairs.begin(); sp != state_pairs.end(); ++sp) {
992 if ((x = on_press.find (sp->second)) != on_press.end()) {
993 if (x->second.type == NamedAction) {
994 node->add_property (string (sp->first + X_("-press")).c_str(), x->second.action_name);
998 if ((x = on_release.find (sp->second)) != on_release.end()) {
999 if (x->second.type == NamedAction) {
1000 node->add_property (string (sp->first + X_("-release")).c_str(), x->second.action_name);
1009 FaderPort::gui_track_selection_changed (RouteNotificationListPtr routes)
1011 boost::shared_ptr<Route> r;
1013 if (!routes->empty()) {
1014 r = routes->front().lock();
1017 set_current_route (r);
1021 FaderPort::drop_current_route ()
1023 if (_current_route) {
1024 if (_current_route == session->monitor_out()) {
1025 set_current_route (session->master_out());
1027 set_current_route (boost::shared_ptr<Route>());
1033 FaderPort::set_current_route (boost::shared_ptr<Route> r)
1035 route_connections.drop_connections ();
1039 /* turn this off. It will be turned on back on in use_master() or
1040 use_monitor() as appropriate.
1042 get_button(Output).set_led_state (_output_port, false);
1044 if (_current_route) {
1045 _current_route->DropReferences.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::drop_current_route, this), this);
1047 _current_route->mute_changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_mute, this, _1), this);
1048 _current_route->solo_changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_solo, this, _1, _2, _3), this);
1049 _current_route->listen_changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_listen, this, _1, _2), this);
1051 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (_current_route);
1053 t->RecordEnableChanged.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_recenable, this), this);
1056 boost::shared_ptr<AutomationControl> control = _current_route->gain_control ();
1058 control->Changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_gain, this), this);
1059 control->alist()->automation_state_changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_auto, this), this);
1062 boost::shared_ptr<MonitorProcessor> mp = _current_route->monitor_control();
1064 mp->cut_control()->Changed.connect (route_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort::map_cut, this), this);
1068 //ToDo: subscribe to the fader automation modes so we can light the LEDs
1074 FaderPort::map_auto ()
1076 boost::shared_ptr<AutomationControl> control = _current_route->gain_control ();
1077 const AutoState as = control->automation_state ();
1081 get_button (FP_Read).set_led_state (_output_port, true);
1082 get_button (FP_Write).set_led_state (_output_port, false);
1083 get_button (FP_Touch).set_led_state (_output_port, false);
1084 get_button (FP_Off).set_led_state (_output_port, false);
1087 get_button (FP_Read).set_led_state (_output_port, false);
1088 get_button (FP_Write).set_led_state (_output_port, true);
1089 get_button (FP_Touch).set_led_state (_output_port, false);
1090 get_button (FP_Off).set_led_state (_output_port, false);
1093 get_button (FP_Read).set_led_state (_output_port, false);
1094 get_button (FP_Write).set_led_state (_output_port, false);
1095 get_button (FP_Touch).set_led_state (_output_port, true);
1096 get_button (FP_Off).set_led_state (_output_port, false);
1099 get_button (FP_Read).set_led_state (_output_port, false);
1100 get_button (FP_Write).set_led_state (_output_port, false);
1101 get_button (FP_Touch).set_led_state (_output_port, false);
1102 get_button (FP_Off).set_led_state (_output_port, true);
1110 FaderPort::map_cut ()
1112 boost::shared_ptr<MonitorProcessor> mp = _current_route->monitor_control();
1115 bool yn = mp->cut_all ();
1117 start_blinking (Mute);
1119 stop_blinking (Mute);
1122 stop_blinking (Mute);
1127 FaderPort::map_mute (void*)
1129 get_button (Mute).set_led_state (_output_port, _current_route->muted());
1133 FaderPort::map_solo (bool, void*, bool)
1135 get_button (Solo).set_led_state (_output_port, _current_route->soloed() || _current_route->listening_via_monitor());
1139 FaderPort::map_listen (void*, bool)
1141 get_button (Solo).set_led_state (_output_port, _current_route->listening_via_monitor());
1145 FaderPort::map_recenable ()
1147 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (_current_route);
1149 get_button (Rec).set_led_state (_output_port, t->record_enabled());
1151 get_button (Rec).set_led_state (_output_port, false);
1156 FaderPort::map_gain ()
1158 if (fader_is_touched) {
1159 /* Do not send fader moves while the user is touching the fader */
1163 if (!_current_route) {
1167 boost::shared_ptr<AutomationControl> control = _current_route->gain_control ();
1173 val = control->internal_to_interface (control->get_value ());
1176 /* Faderport sends fader position with range 0..16384 (though some of
1177 * the least-significant bits at the top end are missing - it may only
1178 * get to 1636X or so).
1180 * But ... position must be sent in the range 0..1023.
1185 int ival = (int) lrintf (val * 1023.0);
1187 /* MIDI normalization requires that we send two separate messages here,
1188 * not one single 6 byte one.
1197 _output_port->write (buf, 3, 0);
1200 buf[2] = ival & 0x7f;
1202 _output_port->write (buf, 3, 0);
1206 FaderPort::map_route_state ()
1208 if (!_current_route) {
1209 stop_blinking (Mute);
1210 stop_blinking (Solo);
1211 get_button (Rec).set_led_state (_output_port, false);
1213 /* arguments to these map_*() methods are all ignored */
1215 map_solo (false, 0, false);
1223 list<boost::shared_ptr<ARDOUR::Bundle> >
1224 FaderPort::bundles ()
1226 list<boost::shared_ptr<ARDOUR::Bundle> > b;
1228 if (_input_bundle) {
1229 b.push_back (_input_bundle);
1230 b.push_back (_output_bundle);
1236 boost::shared_ptr<Port>
1237 FaderPort::output_port()
1239 return _output_port;
1242 boost::shared_ptr<Port>
1243 FaderPort::input_port()
1249 FaderPort::set_action (ButtonID id, std::string const& action_name, bool on_press, ButtonState bs)
1251 get_button(id).set_action (action_name, on_press, bs);
1255 FaderPort::get_action (ButtonID id, bool press, ButtonState bs)
1257 return get_button(id).get_action (press, bs);