2 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2015 Paul Davis
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "pbd/error.h"
27 #include "pbd/failed_constructor.h"
28 #include "pbd/pthread_utils.h"
29 #include "pbd/compose.h"
30 #include "pbd/xml++.h"
32 #include "midi++/port.h"
34 #include "ardour/audioengine.h"
35 #include "ardour/audio_track.h"
36 #include "ardour/bundle.h"
37 #include "ardour/debug.h"
38 #include "ardour/midi_track.h"
39 #include "ardour/midiport_manager.h"
40 #include "ardour/plugin_insert.h"
41 #include "ardour/processor.h"
42 #include "ardour/rc_configuration.h"
43 #include "ardour/route.h"
44 #include "ardour/session.h"
45 #include "ardour/session_configuration.h"
46 #include "ardour/vca.h"
48 #include "faderport8.h"
50 using namespace ARDOUR;
51 using namespace ArdourSurface;
55 using namespace ArdourSurface::FP8Types;
59 #include "pbd/abstract_ui.cc" // instantiate template
62 //#define VERBOSE_DEBUG
66 debug_2byte_msg (std::string const& msg, int b0, int b1)
69 if (DEBUG_ENABLED(DEBUG::FaderPort8)) {
71 DEBUG_STR_APPEND(a, "RECV: ");
72 DEBUG_STR_APPEND(a, msg);
73 DEBUG_STR_APPEND(a,' ');
74 DEBUG_STR_APPEND(a,hex);
75 DEBUG_STR_APPEND(a,"0x");
76 DEBUG_STR_APPEND(a, b0);
77 DEBUG_STR_APPEND(a,' ');
78 DEBUG_STR_APPEND(a,"0x");
79 DEBUG_STR_APPEND(a, b1);
80 DEBUG_STR_APPEND(a,'\n');
81 DEBUG_TRACE (DEBUG::FaderPort8, DEBUG_STR(a).str());
86 FaderPort8::FaderPort8 (Session& s)
87 : ControlProtocol (s, _("PreSonus FaderPort8"))
88 , AbstractUI<FaderPort8Request> (name())
89 , _connection_state (ConnectionState (0))
90 , _device_active (false)
95 , _blink_onoff (false)
100 boost::shared_ptr<ARDOUR::Port> inp;
101 boost::shared_ptr<ARDOUR::Port> outp;
103 inp = AudioEngine::instance()->register_input_port (DataType::MIDI, "FaderPort8 Recv", true);
104 outp = AudioEngine::instance()->register_output_port (DataType::MIDI, "FaderPort8 Send", true);
105 _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(inp);
106 _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(outp);
108 if (_input_port == 0 || _output_port == 0) {
109 throw failed_constructor();
112 _input_bundle.reset (new ARDOUR::Bundle (_("FaderPort8 (Receive)"), true));
113 _output_bundle.reset (new ARDOUR::Bundle (_("FaderPort8 (Send) "), false));
115 _input_bundle->add_channel (
117 ARDOUR::DataType::MIDI,
118 session->engine().make_port_name_non_relative (inp->name())
121 _output_bundle->add_channel (
123 ARDOUR::DataType::MIDI,
124 session->engine().make_port_name_non_relative (outp->name())
127 ARDOUR::AudioEngine::instance()->PortConnectedOrDisconnected.connect (port_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort8::connection_handler, this, _2, _4), this);
128 ARDOUR::AudioEngine::instance()->Stopped.connect (port_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort8::engine_reset, this), this);
129 ARDOUR::Port::PortDrop.connect (port_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort8::engine_reset, this), this);
131 StripableSelectionChanged.connect (selection_connection, MISSING_INVALIDATOR, boost::bind (&FaderPort8::gui_track_selection_changed, this), this);
134 _ctrls.FaderModeChanged.connect_same_thread (modechange_connections, boost::bind (&FaderPort8::notify_fader_mode_changed, this));
135 _ctrls.MixModeChanged.connect_same_thread (modechange_connections, boost::bind (&FaderPort8::assign_strips, this, true));
138 FaderPort8::~FaderPort8 ()
145 DEBUG_TRACE (DEBUG::FaderPort8, string_compose ("unregistering input port %1\n", boost::shared_ptr<ARDOUR::Port>(_input_port)->name()));
146 AudioEngine::instance()->unregister_port (_input_port);
147 _input_port.reset ();
151 _output_port->drain (10000, 250000); /* check every 10 msecs, wait up to 1/4 second for the port to drain */
152 DEBUG_TRACE (DEBUG::FaderPort8, string_compose ("unregistering output port %1\n", boost::shared_ptr<ARDOUR::Port>(_output_port)->name()));
153 AudioEngine::instance()->unregister_port (_output_port);
154 _output_port.reset ();
159 /* stop event loop */
160 DEBUG_TRACE (DEBUG::FaderPort8, "BaseUI::quit ()\n");
164 /* ****************************************************************************
169 FaderPort8::request_factory (uint32_t num_requests)
171 /* AbstractUI<T>::request_buffer_factory() is a template method only
172 * instantiated in this source module. To provide something visible for
173 * use in the interface/descriptor, we have this static method that is
176 return request_buffer_factory (num_requests);
180 FaderPort8::do_request (FaderPort8Request* req)
182 if (req->type == CallSlot) {
183 call_slot (MISSING_INVALIDATOR, req->the_slot);
184 } else if (req->type == Quit) {
197 FaderPort8::thread_init ()
199 struct sched_param rtparam;
201 pthread_set_name (event_loop_name().c_str());
203 PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
204 ARDOUR::SessionEvent::create_per_thread_pool (event_loop_name(), 128);
206 memset (&rtparam, 0, sizeof (rtparam));
207 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
209 if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
210 // do we care? not particularly.
215 FaderPort8::periodic ()
217 /* prepare TC display -- handled by stripable Periodic () */
218 if (_ctrls.display_timecode ()) {
219 // TODO allow BBT, HHMMSS
220 // used in FP8Strip::periodic_update_timecode
222 session->timecode_time (TC);
223 _timecode = Timecode::timecode_format_time(TC);
228 /* update stripables */
234 FaderPort8::blink_it ()
236 _blink_onoff = !_blink_onoff;
237 BlinkIt (_blink_onoff);
241 /* ****************************************************************************
242 * Port and Signal Connection Management
245 FaderPort8::set_active (bool yn)
247 DEBUG_TRACE (DEBUG::FaderPort8, string_compose("set_active init with yn: '%1'\n", yn));
249 if (yn == active()) {
254 /* start event loop */
256 connect_session_signals ();
262 ControlProtocol::set_active (yn);
263 DEBUG_TRACE (DEBUG::FaderPort8, string_compose("set_active done with yn: '%1'\n", yn));
270 DEBUG_TRACE (DEBUG::FaderPort8, "FaderPort8::close\n");
271 stop_midi_handling ();
272 session_connections.drop_connections ();
273 automation_state_connections.drop_connections ();
274 assigned_stripable_connections.drop_connections ();
275 _assigned_strips.clear ();
276 drop_ctrl_connections ();
277 port_connections.drop_connections ();
278 selection_connection.disconnect ();
282 FaderPort8::stop_midi_handling ()
284 _periodic_connection.disconnect ();
285 _blink_connection.disconnect ();
286 midi_connections.drop_connections ();
287 /* Note: the input handler is still active at this point, but we're no
288 * longer connected to any of the parser signals
293 FaderPort8::connected ()
295 DEBUG_TRACE (DEBUG::FaderPort8, "initializing\n");
296 assert (!_device_active);
298 if (_device_active) {
299 stop_midi_handling (); // re-init
302 // ideally check firmware version >= 1.01 (USB bcdDevice 0x0101) (vendor 0x194f prod 0x0202)
303 // but we don't have a handle to the underlying USB device here.
305 _channel_off = _plugin_off = _parameter_off = 0;
306 _blink_onoff = false;
310 start_midi_handling ();
311 _ctrls.initialize ();
313 /* highlight bound user-actions */
314 for (FP8Controls::UserButtonMap::const_iterator i = _ctrls.user_buttons ().begin ();
315 i != _ctrls.user_buttons ().end (); ++i) {
316 _ctrls.button (i->first).set_active (! _user_action_map[i->first].empty ());
318 /* shift button lights */
319 tx_midi3 (0x90, 0x06, 0x00);
320 tx_midi3 (0x90, 0x46, 0x00);
322 send_session_state ();
323 assign_strips (true);
325 Glib::RefPtr<Glib::TimeoutSource> blink_timer =
326 Glib::TimeoutSource::create (200);
327 _blink_connection = blink_timer->connect (sigc::mem_fun (*this, &FaderPort8::blink_it));
328 blink_timer->attach (main_loop()->get_context());
330 Glib::RefPtr<Glib::TimeoutSource> periodic_timer =
331 Glib::TimeoutSource::create (100);
332 _periodic_connection = periodic_timer->connect (sigc::mem_fun (*this, &FaderPort8::periodic));
333 periodic_timer->attach (main_loop()->get_context());
337 FaderPort8::disconnected ()
339 stop_midi_handling ();
340 if (_device_active) {
341 for (uint8_t id = 0; id < 8; ++id) {
342 _ctrls.strip(id).unset_controllables ();
344 _ctrls.all_lights_off ();
349 FaderPort8::engine_reset ()
351 /* Port::PortDrop is called when the engine is halted or stopped */
352 DEBUG_TRACE (DEBUG::FaderPort8, "FaderPort8::engine_reset\n");
353 _connection_state = 0;
354 _device_active = false;
359 FaderPort8::connection_handler (std::string name1, std::string name2)
362 DEBUG_TRACE (DEBUG::FaderPort8, "FaderPort8::connection_handler: start\n");
364 if (!_input_port || !_output_port) {
368 string ni = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_input_port)->name());
369 string no = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_output_port)->name());
371 if (ni == name1 || ni == name2) {
372 DEBUG_TRACE (DEBUG::FaderPort8, string_compose ("Connection notify %1 and %2\n", name1, name2));
373 if (_input_port->connected ()) {
374 if (_connection_state & InputConnected) {
377 _connection_state |= InputConnected;
379 _connection_state &= ~InputConnected;
381 } else if (no == name1 || no == name2) {
382 DEBUG_TRACE (DEBUG::FaderPort8, string_compose ("Connection notify %1 and %2\n", name1, name2));
383 if (_output_port->connected ()) {
384 if (_connection_state & OutputConnected) {
387 _connection_state |= OutputConnected;
389 _connection_state &= ~OutputConnected;
393 DEBUG_TRACE (DEBUG::FaderPort8, string_compose ("Connections between %1 and %2 changed, but I ignored it\n", name1, name2));
399 if ((_connection_state & (InputConnected|OutputConnected)) == (InputConnected|OutputConnected)) {
401 /* XXX this is a horrible hack. Without a short sleep here,
402 * something prevents the device wakeup messages from being
403 * sent and/or the responses from being received.
406 DEBUG_TRACE (DEBUG::FaderPort8, "device now connected for both input and output\n");
408 _device_active = true;
411 DEBUG_TRACE (DEBUG::FaderPort8, "Device disconnected (input or output or both) or not yet fully connected\n");
412 if (_device_active) {
415 _device_active = false;
418 ConnectionChange (); /* emit signal for our GUI */
421 DEBUG_TRACE (DEBUG::FaderPort8, "FaderPort8::connection_handler: end\n");
424 return true; /* connection status changed */
427 list<boost::shared_ptr<ARDOUR::Bundle> >
428 FaderPort8::bundles ()
430 list<boost::shared_ptr<ARDOUR::Bundle> > b;
433 b.push_back (_input_bundle);
434 b.push_back (_output_bundle);
440 /* ****************************************************************************
444 FaderPort8::midi_input_handler (Glib::IOCondition ioc, boost::weak_ptr<ARDOUR::AsyncMIDIPort> wport)
446 boost::shared_ptr<AsyncMIDIPort> port (wport.lock());
453 DEBUG_TRACE (DEBUG::FaderPort8, string_compose ("something happend on %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
464 DEBUG_TRACE (DEBUG::FaderPort8, string_compose ("data available on %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
466 framepos_t now = session->engine().sample_time();
474 FaderPort8::start_midi_handling ()
476 _input_port->parser()->sysex.connect_same_thread (midi_connections, boost::bind (&FaderPort8::sysex_handler, this, _1, _2, _3));
477 _input_port->parser()->poly_pressure.connect_same_thread (midi_connections, boost::bind (&FaderPort8::polypressure_handler, this, _1, _2));
478 for (uint8_t i = 0; i < 16; ++i) {
479 _input_port->parser()->channel_pitchbend[i].connect_same_thread (midi_connections, boost::bind (&FaderPort8::pitchbend_handler, this, _1, i, _2));
481 _input_port->parser()->controller.connect_same_thread (midi_connections, boost::bind (&FaderPort8::controller_handler, this, _1, _2));
482 _input_port->parser()->note_on.connect_same_thread (midi_connections, boost::bind (&FaderPort8::note_on_handler, this, _1, _2));
483 _input_port->parser()->note_off.connect_same_thread (midi_connections, boost::bind (&FaderPort8::note_off_handler, this, _1, _2));
485 /* This connection means that whenever data is ready from the input
486 * port, the relevant thread will invoke our ::midi_input_handler()
487 * method, which will read the data, and invoke the parser.
489 _input_port->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &FaderPort8::midi_input_handler), boost::weak_ptr<AsyncMIDIPort> (_input_port)));
490 _input_port->xthread().attach (main_loop()->get_context());
494 FaderPort8::tx_midi (std::vector<uint8_t> const& d) const
496 /* work around midi buffer overflow for batch changes */
497 if (d.size() == 3 && (d[0] == 0x91 || d[0] == 0x92)) {
498 /* set colors triplet in one go */
499 } else if (d.size() == 3 && (d[0] == 0x93)) {
502 g_usleep (400 * d.size());
505 size_t tx = _output_port->write (&d[0], d.size(), 0);
506 assert (tx == d.size());
509 return _output_port->write (&d[0], d.size(), 0);
513 /* ****************************************************************************
517 FaderPort8::polypressure_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
519 debug_2byte_msg ("PP", tb->controller_number, tb->value);
520 // outgoing only (meter)
524 FaderPort8::pitchbend_handler (MIDI::Parser &, uint8_t chan, MIDI::pitchbend_t pb)
526 debug_2byte_msg ("PB", chan, pb);
527 /* fader 0..16368 (0x3ff0 -- 1024 steps) */
528 bool handled = _ctrls.midi_fader (chan, pb);
529 /* if Shift key is held while moving a fader (group override), don't lock shift. */
530 if ((_shift_pressed > 0) && handled) {
531 _shift_connection.disconnect ();
537 FaderPort8::controller_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
539 debug_2byte_msg ("CC", tb->controller_number, tb->value);
541 // val Bit 7 = direction, Bits 0-6 = number of steps
542 if (tb->controller_number == 0x3c) {
543 encoder_navigate (tb->value & 0x40 ? true : false, tb->value & 0x3f);
545 if (tb->controller_number == 0x10) {
546 encoder_parameter (tb->value & 0x40 ? true : false, tb->value & 0x3f);
551 FaderPort8::note_on_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
553 debug_2byte_msg ("ON", tb->note_number, tb->velocity);
556 if (tb->note_number >= 0x68 && tb->note_number <= 0x6f) {
557 _ctrls.midi_touch (tb->note_number - 0x68, tb->velocity);
561 /* special case shift */
562 if (tb->note_number == 0x06 || tb->note_number == 0x46) {
563 _shift_pressed |= (tb->note_number == 0x06) ? 1 : 2;
564 if (_shift_pressed == 3) {
567 _shift_connection.disconnect ();
570 ShiftButtonChange (false);
571 tx_midi3 (0x90, 0x06, 0x00);
572 tx_midi3 (0x90, 0x46, 0x00);
576 Glib::RefPtr<Glib::TimeoutSource> shift_timer =
577 Glib::TimeoutSource::create (1000);
578 shift_timer->attach (main_loop()->get_context());
579 _shift_connection = shift_timer->connect (sigc::mem_fun (*this, &FaderPort8::shift_timeout));
581 ShiftButtonChange (true);
582 tx_midi3 (0x90, 0x06, 0x7f);
583 tx_midi3 (0x90, 0x46, 0x7f);
587 _ctrls.midi_event (tb->note_number, tb->velocity);
591 FaderPort8::note_off_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
593 debug_2byte_msg ("OF", tb->note_number, tb->velocity);
595 if (tb->note_number >= 0x68 && tb->note_number <= 0x6f) {
597 _ctrls.midi_touch (tb->note_number - 0x68, tb->velocity);
601 /* special case shift */
602 if (tb->note_number == 0x06 || tb->note_number == 0x46) {
603 _shift_pressed &= (tb->note_number == 0x06) ? 2 : 1;
604 if (_shift_pressed > 0) {
610 ShiftButtonChange (false);
611 tx_midi3 (0x90, 0x06, 0x00);
612 tx_midi3 (0x90, 0x46, 0x00);
613 /* just in case this happens concurrently */
614 _shift_connection.disconnect ();
619 bool handled = _ctrls.midi_event (tb->note_number, tb->velocity);
620 /* if Shift key is held while activating an action, don't lock shift. */
621 if ((_shift_pressed > 0) && handled) {
622 _shift_connection.disconnect ();
628 FaderPort8::sysex_handler (MIDI::Parser &p, MIDI::byte *buf, size_t size)
631 if (DEBUG_ENABLED(DEBUG::FaderPort8)) {
633 DEBUG_STR_APPEND(a, string_compose ("RECV sysex siz=%1", size));
634 for (size_t i=0; i < size; ++i) {
635 DEBUG_STR_APPEND(a,hex);
636 DEBUG_STR_APPEND(a,"0x");
637 DEBUG_STR_APPEND(a,(int)buf[i]);
638 DEBUG_STR_APPEND(a,' ');
640 DEBUG_STR_APPEND(a,'\n');
641 DEBUG_TRACE (DEBUG::FaderPort8, DEBUG_STR(a).str());
646 /* ****************************************************************************
650 FaderPort8::set_button_action (FP8Controls::ButtonId id, bool press, std::string const& action_name)
652 if (_ctrls.user_buttons().find (id) == _ctrls.user_buttons().end ()) {
655 _user_action_map[id].action (press).assign_action (action_name);
657 if (!_device_active) {
660 _ctrls.button (id).set_active (!_user_action_map[id].empty ());
664 FaderPort8::get_button_action (FP8Controls::ButtonId id, bool press)
666 return _user_action_map[id].action(press)._action_name;
669 /* ****************************************************************************
673 FaderPort8::get_state ()
675 DEBUG_TRACE (DEBUG::FaderPort8, "FaderPort8::get_state\n");
676 XMLNode& node (ControlProtocol::get_state());
680 child = new XMLNode (X_("Input"));
681 child->add_child_nocopy (boost::shared_ptr<ARDOUR::Port>(_input_port)->get_state());
682 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 for (UserActionMap::const_iterator i = _user_action_map.begin (); i != _user_action_map.end (); ++i) {
689 if (i->second.empty()) {
693 if (!_ctrls.button_enum_to_name (i->first, name)) {
696 XMLNode* btn = new XMLNode (X_("Button"));
697 btn->set_property (X_("id"), name);
698 if (!i->second.action(true).empty ()) {
699 btn->set_property ("press", i->second.action(true)._action_name);
701 if (!i->second.action(false).empty ()) {
702 btn->set_property ("release", i->second.action(false)._action_name);
704 node.add_child_nocopy (*btn);
711 FaderPort8::set_state (const XMLNode& node, int version)
713 DEBUG_TRACE (DEBUG::FaderPort8, "FaderPort8::set_state\n");
715 XMLNodeConstIterator niter;
716 XMLNode const* child;
718 if (ControlProtocol::set_state (node, version)) {
722 if ((child = node.child (X_("Input"))) != 0) {
723 XMLNode* portnode = child->child (Port::state_node_name.c_str());
725 DEBUG_TRACE (DEBUG::FaderPort8, "FaderPort8::set_state Input\n");
726 boost::shared_ptr<ARDOUR::Port>(_input_port)->set_state (*portnode, version);
730 if ((child = node.child (X_("Output"))) != 0) {
731 XMLNode* portnode = child->child (Port::state_node_name.c_str());
733 DEBUG_TRACE (DEBUG::FaderPort8, "FaderPort8::set_state Output\n");
734 boost::shared_ptr<ARDOUR::Port>(_output_port)->set_state (*portnode, version);
738 _user_action_map.clear ();
739 // TODO: When re-loading state w/o surface re-init becomes possible,
740 // unset lights and reset colors of user buttons.
742 for (XMLNodeList::const_iterator n = node.children().begin(); n != node.children().end(); ++n) {
743 if ((*n)->name() != X_("Button")) {
748 if (!(*n)->get_property (X_("id"), id_str)) {
752 FP8Controls::ButtonId id;
753 if (!_ctrls.button_name_to_enum (id_str, id)) {
757 std::string action_str;
758 if ((*n)->get_property (X_("press"), action_str)) {
759 set_button_action (id, true, action_str);
761 if ((*n)->get_property (X_("release"), action_str)) {
762 set_button_action (id, false, action_str);
769 /* ****************************************************************************
770 * Stripable Assignment
773 static bool flt_audio_track (boost::shared_ptr<Stripable> s) {
774 return boost::dynamic_pointer_cast<AudioTrack>(s) != 0;
777 static bool flt_midi_track (boost::shared_ptr<Stripable> s) {
778 return boost::dynamic_pointer_cast<MidiTrack>(s) != 0;
781 static bool flt_bus (boost::shared_ptr<Stripable> s) {
782 if (boost::dynamic_pointer_cast<Route>(s) == 0) {
786 if (s->mixbus () == 0) {
790 return boost::dynamic_pointer_cast<Track>(s) == 0;
793 static bool flt_auxbus (boost::shared_ptr<Stripable> s) {
794 if (boost::dynamic_pointer_cast<Route>(s) == 0) {
798 if (s->mixbus () > 0) {
802 return boost::dynamic_pointer_cast<Track>(s) == 0;
805 static bool flt_vca (boost::shared_ptr<Stripable> s) {
806 return boost::dynamic_pointer_cast<VCA>(s) != 0;
809 static bool flt_selected (boost::shared_ptr<Stripable> s) {
810 return s->is_selected ();
813 static bool flt_mains (boost::shared_ptr<Stripable> s) {
814 return (s->is_master() || s->is_monitor());
817 static bool flt_all (boost::shared_ptr<Stripable> s) {
821 static bool flt_rec_armed (boost::shared_ptr<Stripable> s) {
822 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track>(s);
826 return t->rec_enable_control ()->get_value () > 0.;
829 static bool flt_instrument (boost::shared_ptr<Stripable> s) {
830 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(s);
834 return 0 != r->the_instrument ();
837 struct FP8SortByNewDisplayOrder
839 // return strict (a < b)
840 bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
842 if (a->presentation_info().flags () == b->presentation_info().flags ()) {
843 return a->presentation_info().order() < b->presentation_info().order();
849 // see also gtk2_ardour/route_sorter.h
850 if (a->presentation_info().flags () & ARDOUR::PresentationInfo::VCA) {
854 else if (a->presentation_info().flags () & ARDOUR::PresentationInfo::MasterOut) {
857 else if (a->presentation_info().flags () & ARDOUR::PresentationInfo::Mixbus || a->mixbus()) {
862 if (b->presentation_info().flags () & ARDOUR::PresentationInfo::VCA) {
866 else if (b->presentation_info().flags () & ARDOUR::PresentationInfo::MasterOut) {
869 else if (b->presentation_info().flags () & ARDOUR::PresentationInfo::Mixbus || b->mixbus()) {
874 if (cmp_a == cmp_b) {
875 return a->presentation_info().order() < b->presentation_info().order();
877 return cmp_a < cmp_b;
882 FaderPort8::filter_stripables (StripableList& strips) const
884 typedef bool (*FilterFunction)(boost::shared_ptr<Stripable>);
887 bool allow_master = false;
888 bool allow_monitor = false;
890 switch (_ctrls.mix_mode ()) {
892 flt = &flt_audio_track;
895 flt = &flt_instrument;
904 flt = &flt_midi_track;
912 allow_monitor = true;
916 flt = &flt_rec_armed;
928 session->get_stripables (all);
930 for (StripableList::const_iterator s = all.begin(); s != all.end(); ++s) {
931 if ((*s)->is_auditioner ()) { continue; }
932 if ((*s)->is_hidden ()) { continue; }
934 if (!allow_master && (*s)->is_master ()) { continue; }
935 if (!allow_monitor && (*s)->is_monitor ()) { continue; }
938 strips.push_back (*s);
941 strips.sort (FP8SortByNewDisplayOrder());
944 /* Track/Pan mode: assign stripable to strips */
946 FaderPort8::assign_stripables (bool select_only)
948 StripableList strips;
949 filter_stripables (strips);
952 set_periodic_display_mode (FP8Strip::Stripables);
955 int n_strips = strips.size();
956 _channel_off = std::min (_channel_off, n_strips - 8);
957 _channel_off = std::max (0, _channel_off);
960 int skip = _channel_off;
961 for (StripableList::const_iterator s = strips.begin(); s != strips.end(); ++s) {
967 _assigned_strips[*s] = id;
968 (*s)->DropReferences.connect (assigned_stripable_connections, MISSING_INVALIDATOR,
969 boost::bind (&FaderPort8::notify_stripable_added_or_removed, this), this);
971 (*s)->PropertyChanged.connect (assigned_stripable_connections, MISSING_INVALIDATOR,
972 boost::bind (&FaderPort8::notify_stripable_property_changed, this, boost::weak_ptr<Stripable> (*s), _1), this);
973 (*s)->presentation_info ().PropertyChanged.connect (assigned_stripable_connections, MISSING_INVALIDATOR,
974 boost::bind (&FaderPort8::notify_stripable_property_changed, this, boost::weak_ptr<Stripable> (*s), _1), this);
977 _ctrls.strip(id).set_text_line (3, (*s)->name (), true);
978 _ctrls.strip(id).select_button ().set_color ((*s)->presentation_info ().color());
979 /* update selection lights */
980 _ctrls.strip(id).select_button ().set_active ((*s)->is_selected ());
981 _ctrls.strip(id).select_button ().set_blinking (*s == first_selected_stripable ());
983 _ctrls.strip(id).set_stripable (*s, _ctrls.fader_mode() == ModePan);
986 boost::function<void ()> cb (boost::bind (&FaderPort8::select_strip, this, boost::weak_ptr<Stripable> (*s)));
987 _ctrls.strip(id).set_select_cb (cb);
993 for (; id < 8; ++id) {
994 _ctrls.strip(id).unset_controllables (select_only ? (FP8Strip::CTRL_SELECT | FP8Strip::CTRL_TEXT3) : FP8Strip::CTRL_ALL);
999 FaderPort8::assign_processor_ctrls ()
1001 if (_proc_params.size() == 0) {
1002 _ctrls.set_fader_mode (ModeTrack);
1005 set_periodic_display_mode (FP8Strip::PluginParam);
1007 std::vector <ProcessorCtrl*> toggle_params;
1008 std::vector <ProcessorCtrl*> slider_params;
1010 for ( std::list <ProcessorCtrl>::iterator i = _proc_params.begin(); i != _proc_params.end(); ++i) {
1011 if ((*i).ac->toggled()) {
1012 toggle_params.push_back (&(*i));
1014 slider_params.push_back (&(*i));
1018 int n_parameters = std::max (toggle_params.size(), slider_params.size());
1020 _parameter_off = std::min (_parameter_off, n_parameters - 8);
1021 _parameter_off = std::max (0, _parameter_off);
1024 for (size_t i = _parameter_off; i < (size_t)n_parameters; ++i) {
1025 if (i >= toggle_params.size ()) {
1026 _ctrls.strip(id).unset_controllables (FP8Strip::CTRL_ALL & ~FP8Strip::CTRL_FADER & ~FP8Strip::CTRL_TEXT0 & ~FP8Strip::CTRL_TEXT1);
1028 else if (i >= slider_params.size ()) {
1029 _ctrls.strip(id).unset_controllables (FP8Strip::CTRL_ALL & ~FP8Strip::CTRL_SELECT & ~FP8Strip::CTRL_TEXT3);
1031 _ctrls.strip(id).unset_controllables (FP8Strip::CTRL_ALL & ~FP8Strip::CTRL_FADER & ~FP8Strip::CTRL_TEXT0 & ~FP8Strip::CTRL_TEXT1 & ~FP8Strip::CTRL_SELECT & ~FP8Strip::CTRL_TEXT3);
1034 if (i < slider_params.size ()) {
1035 _ctrls.strip(id).set_fader_controllable (slider_params[i]->ac);
1036 _ctrls.strip(id).set_text_line (0, slider_params[i]->name);
1038 if (i < toggle_params.size ()) {
1039 _ctrls.strip(id).set_select_controllable (toggle_params[i]->ac);
1040 _ctrls.strip(id).set_text_line (3, toggle_params[i]->name, true);
1048 for (; id < 8; ++id) {
1049 _ctrls.strip(id).unset_controllables ();
1054 FaderPort8::build_well_known_processor_ctrls (boost::shared_ptr<Stripable> s, bool eq)
1056 #define PUSH_BACK_NON_NULL(N, C) do {if (C) { _proc_params.push_back (ProcessorCtrl (N, C)); }} while (0)
1058 _proc_params.clear ();
1060 int cnt = s->eq_band_cnt();
1061 PUSH_BACK_NON_NULL ("Enable", s->eq_enable_controllable ());
1062 PUSH_BACK_NON_NULL ("HPF", s->eq_hpf_controllable ());
1063 for (int band = 0; band < cnt; ++band) {
1064 std::string bn = s->eq_band_name (band);
1065 PUSH_BACK_NON_NULL (string_compose ("Gain %1", bn), s->eq_gain_controllable (band));
1066 PUSH_BACK_NON_NULL (string_compose ("Freq %1", bn), s->eq_freq_controllable (band));
1067 PUSH_BACK_NON_NULL (string_compose ("Band %1", bn), s->eq_q_controllable (band));
1068 PUSH_BACK_NON_NULL (string_compose ("Shape %1", bn), s->eq_shape_controllable (band));
1071 PUSH_BACK_NON_NULL ("Enable", s->comp_enable_controllable ());
1072 PUSH_BACK_NON_NULL ("Threshold", s->comp_threshold_controllable ());
1073 PUSH_BACK_NON_NULL ("Speed", s->comp_speed_controllable ());
1074 PUSH_BACK_NON_NULL ("Mode", s->comp_mode_controllable ());
1079 FaderPort8::select_plugin (int num)
1081 // make sure drop_ctrl_connections() was called
1082 assert (_proc_params.size() == 0 && _showing_well_known == 0);
1084 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (first_selected_stripable());
1086 _ctrls.set_fader_mode (ModeTrack);
1090 build_well_known_processor_ctrls (r, num == -1);
1091 assign_processor_ctrls ();
1092 _showing_well_known = num;
1095 _showing_well_known = 0;
1097 boost::shared_ptr<Processor> proc = r->nth_plugin (num);
1099 _ctrls.set_fader_mode (ModeTrack);
1103 // switching to "Mode Track" -> calls FaderPort8::notify_fader_mode_changed()
1104 // which drops the references, disconnects the signal and re-spills tracks
1105 proc->DropReferences.connect (processor_connections, MISSING_INVALIDATOR, boost::bind (&FP8Controls::set_fader_mode, &_ctrls, ModeTrack), this);
1108 _proc_params.clear();
1109 set<Evoral::Parameter> p = proc->what_can_be_automated ();
1110 for (set<Evoral::Parameter>::iterator i = p.begin(); i != p.end(); ++i) {
1111 std::string n = proc->describe_parameter (*i);
1112 if (n == "hidden") {
1115 _proc_params.push_back (ProcessorCtrl (n, proc->automation_control (*i)));
1118 // TODO: open plugin GUI if (_proc_params.size() > 0)
1121 assign_processor_ctrls ();
1124 /* short 4 chars at most */
1125 static std::string plugintype (ARDOUR::PluginType t) {
1142 return enum_2_string (t);
1146 FaderPort8::spill_plugins ()
1148 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (first_selected_stripable());
1150 _ctrls.set_fader_mode (ModeTrack);
1154 drop_ctrl_connections ();
1156 // switching to "Mode Track" -> calls FaderPort8::notify_fader_mode_changed()
1157 // which drops the references, disconnects the signal and re-spills tracks
1158 r->DropReferences.connect (processor_connections, MISSING_INVALIDATOR, boost::bind (&FP8Controls::set_fader_mode, &_ctrls, ModeTrack), this);
1160 // update when processor change
1161 r->processors_changed.connect (processor_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort8::spill_plugins, this), this);
1164 boost::shared_ptr<Processor> proc;
1166 std::vector<uint32_t> procs;
1168 for (uint32_t i = 0; 0 != (proc = r->nth_plugin (i)); ++i) {
1169 if (!proc->display_to_user ()) {
1171 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
1172 if (pi->is_channelstrip ()) // don't skip MB PRE
1177 set<Evoral::Parameter> p = proc->what_can_be_automated ();
1178 for (set<Evoral::Parameter>::iterator i = p.begin(); i != p.end(); ++i) {
1179 std::string n = proc->describe_parameter (*i);
1180 if (n == "hidden") {
1185 if (n_controls > 0) {
1186 procs.push_back (i);
1190 int n_plugins = procs.size();
1192 bool have_well_known_eq = false;
1193 bool have_well_known_comp = false;
1195 // reserve last slot(s) for "well-known"
1196 if (r->eq_band_cnt() > 0) {
1198 have_well_known_eq = true;
1200 if (r->comp_enable_controllable ()) {
1202 have_well_known_comp = true;
1205 if (n_plugins == 0 && !have_well_known_eq && !have_well_known_comp) {
1206 _ctrls.set_fader_mode (ModeTrack);
1210 set_periodic_display_mode (FP8Strip::PluginSelect);
1212 _plugin_off = std::min (_plugin_off, n_plugins - spillwidth);
1213 _plugin_off = std::max (0, _plugin_off);
1216 for (uint32_t i = _plugin_off; ; ++i) {
1217 if (i >= procs.size()) {
1220 boost::shared_ptr<Processor> proc = r->nth_plugin (procs[i]);
1224 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (proc);
1225 boost::function<void ()> cb (boost::bind (&FaderPort8::select_plugin, this, procs[i]));
1227 _ctrls.strip(id).unset_controllables (FP8Strip::CTRL_ALL & ~FP8Strip::CTRL_TEXT & ~FP8Strip::CTRL_SELECT);
1228 _ctrls.strip(id).set_select_cb (cb);
1229 _ctrls.strip(id).select_button ().set_color (0x00ff00ff);
1230 _ctrls.strip(id).select_button ().set_active (true /*proc->enabled()*/);
1231 _ctrls.strip(id).select_button ().set_blinking (false);
1232 _ctrls.strip(id).set_text_line (0, proc->name());
1233 _ctrls.strip(id).set_text_line (1, pi->plugin()->maker());
1234 _ctrls.strip(id).set_text_line (2, plugintype (pi->type()));
1235 _ctrls.strip(id).set_text_line (3, "");
1237 if (++id == spillwidth) {
1242 for (; id < spillwidth; ++id) {
1243 _ctrls.strip(id).unset_controllables ();
1246 if (have_well_known_comp) {
1248 boost::function<void ()> cb (boost::bind (&FaderPort8::select_plugin, this, -2));
1249 _ctrls.strip(id).unset_controllables (FP8Strip::CTRL_ALL & ~FP8Strip::CTRL_TEXT & ~FP8Strip::CTRL_SELECT);
1250 _ctrls.strip(id).set_select_cb (cb);
1251 _ctrls.strip(id).select_button ().set_color (0xffff00ff);
1252 _ctrls.strip(id).select_button ().set_active (true);
1253 _ctrls.strip(id).select_button ().set_blinking (false);
1254 _ctrls.strip(id).set_text_line (0, "Comp");
1255 _ctrls.strip(id).set_text_line (1, "Built-In");
1256 _ctrls.strip(id).set_text_line (2, "--");
1257 _ctrls.strip(id).set_text_line (3, "");
1260 if (have_well_known_eq) {
1262 boost::function<void ()> cb (boost::bind (&FaderPort8::select_plugin, this, -1));
1263 _ctrls.strip(id).unset_controllables (FP8Strip::CTRL_ALL & ~FP8Strip::CTRL_TEXT & ~FP8Strip::CTRL_SELECT);
1264 _ctrls.strip(id).set_select_cb (cb);
1265 _ctrls.strip(id).select_button ().set_color (0xffff00ff);
1266 _ctrls.strip(id).select_button ().set_active (true);
1267 _ctrls.strip(id).select_button ().set_blinking (false);
1268 _ctrls.strip(id).set_text_line (0, "EQ");
1269 _ctrls.strip(id).set_text_line (1, "Built-In");
1270 _ctrls.strip(id).set_text_line (2, "--");
1271 _ctrls.strip(id).set_text_line (3, "");
1278 FaderPort8::assign_sends ()
1280 boost::shared_ptr<Stripable> s = first_selected_stripable();
1282 _ctrls.set_fader_mode (ModeTrack);
1287 while (0 != s->send_level_controllable (n_sends)) {
1291 _ctrls.set_fader_mode (ModeTrack);
1295 drop_ctrl_connections ();
1296 s->DropReferences.connect (processor_connections, MISSING_INVALIDATOR, boost::bind (&FP8Controls::set_fader_mode, &_ctrls, ModeTrack), this);
1298 set_periodic_display_mode (FP8Strip::SendDisplay);
1300 _plugin_off = std::min (_plugin_off, n_sends - 8);
1301 _plugin_off = std::max (0, _plugin_off);
1304 int skip = _parameter_off;
1305 for (uint32_t i = _plugin_off; ; ++i) {
1310 boost::shared_ptr<AutomationControl> send = s->send_level_controllable (i);
1315 _ctrls.strip(id).unset_controllables (FP8Strip::CTRL_ALL & ~FP8Strip::CTRL_FADER & ~FP8Strip::CTRL_TEXT0 & ~FP8Strip::CTRL_TEXT1 & ~FP8Strip::CTRL_TEXT3 & ~FP8Strip::CTRL_SELECT);
1316 _ctrls.strip(id).set_fader_controllable (send);
1317 _ctrls.strip(id).set_text_line (0, s->send_name (i));
1318 _ctrls.strip(id).set_mute_controllable (s->send_enable_controllable (i));
1325 for (; id < 8; ++id) {
1326 _ctrls.strip(id).unset_controllables (FP8Strip::CTRL_ALL & ~FP8Strip::CTRL_TEXT3 & ~FP8Strip::CTRL_SELECT);
1328 #ifdef MIXBUS // master-assign on last solo
1329 _ctrls.strip(7).set_solo_controllable (s->master_send_enable_controllable ());
1331 /* set select buttons */
1332 assigned_stripable_connections.drop_connections ();
1333 _assigned_strips.clear ();
1334 assign_stripables (true);
1338 FaderPort8::set_periodic_display_mode (FP8Strip::DisplayMode m)
1340 for (uint8_t id = 0; id < 8; ++id) {
1341 _ctrls.strip(id).set_periodic_display_mode (m);
1346 FaderPort8::assign_strips (bool reset_bank)
1352 assigned_stripable_connections.drop_connections ();
1353 _assigned_strips.clear ();
1355 FaderMode fadermode = _ctrls.fader_mode ();
1356 switch (fadermode) {
1359 assign_stripables ();
1360 gui_track_selection_changed (); // update selection, automation-state
1363 if (_proc_params.size() > 0) {
1364 assign_processor_ctrls ();
1377 FaderPort8::drop_ctrl_connections ()
1379 _proc_params.clear();
1380 processor_connections.drop_connections ();
1381 _showing_well_known = 0;
1385 FaderPort8::notify_fader_mode_changed ()
1387 FaderMode fadermode = _ctrls.fader_mode ();
1389 boost::shared_ptr<Stripable> s = first_selected_stripable();
1390 if (!s && (fadermode == ModePlugins || fadermode == ModeSend)) {
1391 _ctrls.set_fader_mode (ModeTrack);
1395 drop_ctrl_connections ();
1397 switch (fadermode) {
1405 // force unset rec-arm button, see also FaderPort8::button_arm
1406 _ctrls.button (FP8Controls::BtnArm).set_active (false);
1407 ARMButtonChange (false);
1410 assign_strips (false);
1411 notify_automation_mode_changed ();
1414 /* ****************************************************************************
1415 * Assigned Stripable Callbacks
1419 FaderPort8::notify_stripable_added_or_removed ()
1423 * - session->RouteAdded
1424 * - PresentationInfo::Change
1425 * - Properties::hidden
1426 * - Properties::order
1428 assign_strips (false);
1431 /* functor for FP8Strip's select button */
1433 FaderPort8::select_strip (boost::weak_ptr<Stripable> ws)
1435 boost::shared_ptr<Stripable> s = ws.lock();
1439 #if 1 /* single exclusive selection by default, toggle via shift */
1441 ToggleStripableSelection (s);
1443 SetStripableSelection (s);
1446 /* tri-state selection: This allows to set the "first selected"
1447 * with a single click without clearing the selection.
1448 * Single de/select via shift.
1451 if (s->is_selected ()) {
1452 RemoveStripableFromSelection (s);
1454 SetStripableSelection (s);
1458 if (s->is_selected () && s != first_selected_stripable ()) {
1459 set_first_selected_stripable (s);
1460 gui_track_selection_changed ();
1462 ToggleStripableSelection (s);
1467 /* called from static PresentationInfo::Change */
1469 FaderPort8::notify_pi_property_changed (const PropertyChange& what_changed)
1471 if (what_changed.contains (Properties::hidden)) {
1472 notify_stripable_added_or_removed ();
1474 if (what_changed.contains (Properties::order)) {
1475 notify_stripable_added_or_removed ();
1477 // Properties::selected is handled via StripableSelectionChanged
1481 FaderPort8::notify_stripable_property_changed (boost::weak_ptr<Stripable> ws, const PropertyChange& what_changed)
1483 boost::shared_ptr<Stripable> s = ws.lock();
1485 assert (0); // this should not happen
1488 if (_assigned_strips.find (s) == _assigned_strips.end()) {
1489 /* it can happen that signal emission is delayed.
1490 * A signal may already be in the queue but the
1491 * _assigned_strips has meanwhile changed.
1493 * before _assigned_strips changes, the connections are dropped
1494 * but that does not seem to invalidate pending requests :(
1496 * Seen when creating a new MB session and Mixbusses are added
1501 uint8_t id = _assigned_strips[s];
1503 if (what_changed.contains (Properties::color)) {
1504 _ctrls.strip(id).select_button ().set_color (s->presentation_info ().color());
1507 if (what_changed.contains (Properties::name)) {
1508 switch (_ctrls.fader_mode ()) {
1510 _ctrls.strip(id).set_text_line (3, s->name(), true);
1514 _ctrls.strip(id).set_text_line (0, s->name());
1524 FaderPort8::gui_track_selection_changed (/*ARDOUR::StripableNotificationListPtr*/)
1526 automation_state_connections.drop_connections();
1528 switch (_ctrls.fader_mode ()) {
1530 if (_proc_params.size () > 0 && _showing_well_known < 0) {
1531 /* w/well-known -> re-assign to new strip */
1532 int wk = _showing_well_known;
1533 drop_ctrl_connections ();
1546 /* update selection lights */
1547 for (StripAssignmentMap::const_iterator i = _assigned_strips.begin(); i != _assigned_strips.end(); ++i) {
1548 boost::shared_ptr<ARDOUR::Stripable> s = i->first;
1549 uint8_t id = i->second;
1550 bool sel = s->is_selected ();
1551 _ctrls.strip(id).select_button ().set_active (sel);
1552 _ctrls.strip(id).select_button ().set_blinking (sel && s == first_selected_stripable ());
1555 /* track automation-mode of primary selection */
1556 boost::shared_ptr<Stripable> s = first_selected_stripable();
1558 boost::shared_ptr<AutomationControl> ac;
1559 ac = s->gain_control();
1560 if (ac && ac->alist()) {
1561 ac->alist()->automation_state_changed.connect (automation_state_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort8::notify_automation_mode_changed, this), this);
1563 ac = s->pan_azimuth_control();
1564 if (ac && ac->alist()) {
1565 ac->alist()->automation_state_changed.connect (automation_state_connections, MISSING_INVALIDATOR, boost::bind (&FaderPort8::notify_automation_mode_changed, this), this);
1569 notify_automation_mode_changed ();
1573 /* ****************************************************************************
1578 FaderPort8::move_selected_into_view ()
1580 boost::shared_ptr<Stripable> selected = first_selected_stripable ();
1585 StripableList strips;
1586 filter_stripables (strips);
1588 StripableList::iterator it = std::find (strips.begin(), strips.end(), selected);
1589 if (it == strips.end()) {
1592 int off = std::distance (strips.begin(), it);
1594 if (_channel_off <= off && off < _channel_off + 8) {
1598 if (_channel_off > off) {
1601 _channel_off = off - 7;
1603 assign_strips (false);
1607 FaderPort8::select_prev_next (bool next)
1609 StripableList strips;
1610 filter_stripables (strips);
1612 boost::shared_ptr<Stripable> selected = first_selected_stripable ();
1614 if (strips.size() > 0) {
1616 SetStripableSelection (strips.front ());
1618 SetStripableSelection (strips.back ());
1625 boost::shared_ptr<Stripable> toselect;
1626 for (StripableList::const_iterator s = strips.begin(); s != strips.end(); ++s) {
1627 if (*s == selected) {
1633 if (s != strips.end()) {
1644 if (found && toselect) {
1645 SetStripableSelection (toselect);
1650 FaderPort8::bank (bool down, bool page)
1652 int dt = page ? 8 : 1;
1657 assign_strips (false);
1661 FaderPort8::bank_param (bool down, bool page)
1663 int dt = page ? 8 : 1;
1667 switch (_ctrls.fader_mode ()) {
1669 if (_proc_params.size() > 0) {
1670 _parameter_off += dt;
1671 assign_processor_ctrls ();