2 Copyright (C) 2015 Paul Davis
3 Copyright (C) 2016 W.P. van Paassen
5 Thanks to Rolf Meyerhoff for reverse engineering the CC121 protocol.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <glibmm/fileutils.h>
30 #include <glibmm/miscutils.h>
32 #include "pbd/error.h"
33 #include "pbd/failed_constructor.h"
34 #include "pbd/file_utils.h"
35 #include "pbd/pthread_utils.h"
36 #include "pbd/compose.h"
37 #include "pbd/xml++.h"
39 #include "midi++/port.h"
41 #include "control_protocol/basic_ui.h"
43 #include "ardour/async_midi_port.h"
44 #include "ardour/audioengine.h"
45 #include "ardour/amp.h"
46 #include "ardour/bundle.h"
47 #include "ardour/controllable_descriptor.h"
48 #include "ardour/debug.h"
49 #include "ardour/filesystem_paths.h"
50 #include "ardour/midi_port.h"
51 #include "ardour/midiport_manager.h"
52 #include "ardour/monitor_processor.h"
53 #include "ardour/profile.h"
54 #include "ardour/rc_configuration.h"
55 #include "ardour/record_enable_control.h"
56 #include "ardour/stripable.h"
57 #include "ardour/session.h"
58 #include "ardour/session_configuration.h"
59 #include "ardour/track.h"
63 using namespace ARDOUR;
64 using namespace ArdourSurface;
71 #include "pbd/abstract_ui.cc" // instantiate template
73 CC121::CC121 (Session& s)
74 : ControlProtocol (s, _("Steinberg CC121"))
75 , AbstractUI<CC121Request> (name())
77 , connection_state (ConnectionState (0))
78 , _device_active (false)
81 , fader_is_touched (false)
83 , button_state (ButtonState (0))
85 , rec_enable_state (false)
87 last_encoder_time = 0;
89 boost::shared_ptr<ARDOUR::Port> inp;
90 boost::shared_ptr<ARDOUR::Port> outp;
92 inp = AudioEngine::instance()->register_input_port (DataType::MIDI, "CC121 Recv", true);
93 outp = AudioEngine::instance()->register_output_port (DataType::MIDI, "CC121 Send", true);
95 _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(inp);
96 _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(outp);
98 if (_input_port == 0 || _output_port == 0) {
99 throw failed_constructor();
102 _input_bundle.reset (new ARDOUR::Bundle (_("CC121 Support (Receive)"), true));
103 _output_bundle.reset (new ARDOUR::Bundle (_("CC121 Support (Send) "), false));
105 _input_bundle->add_channel (
107 ARDOUR::DataType::MIDI,
108 session->engine().make_port_name_non_relative (inp->name())
111 _output_bundle->add_channel (
113 ARDOUR::DataType::MIDI,
114 session->engine().make_port_name_non_relative (outp->name())
118 /* Catch port connections and disconnections */
119 ARDOUR::AudioEngine::instance()->PortConnectedOrDisconnected.connect (port_connection, MISSING_INVALIDATOR, boost::bind (&CC121::connection_handler, this, _1, _2, _3, _4, _5), this);
120 buttons.insert (std::make_pair (EButton, Button (*this, _("EButton"), EButton)));
121 buttons.insert (std::make_pair (OpenVST, Button (*this, _("OpenVST"), OpenVST)));
122 buttons.insert (std::make_pair (InputMonitor, Button (*this, _("InputMonitor"), InputMonitor)));
123 buttons.insert (std::make_pair (EQ1Enable, Button (*this, _("EQ1Enable"), EQ1Enable)));
124 buttons.insert (std::make_pair (EQ2Enable, Button (*this, _("EQ2Enable"), EQ2Enable)));
125 buttons.insert (std::make_pair (EQ3Enable, Button (*this, _("EQ3Enable"), EQ3Enable)));
126 buttons.insert (std::make_pair (EQ4Enable, Button (*this, _("EQ4Enable"), EQ4Enable)));
127 buttons.insert (std::make_pair (EQType, Button (*this, _("EQType"), EQType)));
128 buttons.insert (std::make_pair (AllBypass, Button (*this, _("AllBypass"), AllBypass)));
129 buttons.insert (std::make_pair (Function1, Button (*this, _("Function1"), Function1)));
130 buttons.insert (std::make_pair (Function2, Button (*this, _("Function2"), Function2)));
131 buttons.insert (std::make_pair (Function3, Button (*this, _("Function3"), Function3)));
132 buttons.insert (std::make_pair (Function4, Button (*this, _("Function4"), Function4)));
133 buttons.insert (std::make_pair (Value, Button (*this, _("Value"), Value)));
134 buttons.insert (std::make_pair (Jog, Button (*this, _("Jog"), Jog)));
135 buttons.insert (std::make_pair (Lock, Button (*this, _("Lock"), Lock)));
136 buttons.insert (std::make_pair (ToStart, Button (*this, _("ToStart"), ToStart)));
137 buttons.insert (std::make_pair (ToEnd, Button (*this, _("ToEnd"), ToEnd)));
138 buttons.insert (std::make_pair (Mute, Button (*this, _("Mute"), Mute)));
139 buttons.insert (std::make_pair (Solo, Button (*this, _("Solo"), Solo)));
140 buttons.insert (std::make_pair (Rec, Button (*this, _("Rec"), Rec)));
141 buttons.insert (std::make_pair (Left, Button (*this, _("Left"), Left)));
142 buttons.insert (std::make_pair (Right, Button (*this, _("Right"), Right)));
143 buttons.insert (std::make_pair (Output, Button (*this, _("Output"), Output)));
144 buttons.insert (std::make_pair (FP_Read, Button (*this, _("Read"), FP_Read)));
145 buttons.insert (std::make_pair (FP_Write, Button (*this, _("Write"), FP_Write)));
146 buttons.insert (std::make_pair (Loop, Button (*this, _("Loop"), Loop)));
147 buttons.insert (std::make_pair (Rewind, Button (*this, _("Rewind"), Rewind)));
148 buttons.insert (std::make_pair (Ffwd, Button (*this, _("Ffwd"), Ffwd)));
149 buttons.insert (std::make_pair (Stop, Button (*this, _("Stop"), Stop)));
150 buttons.insert (std::make_pair (Play, Button (*this, _("Play"), Play)));
151 buttons.insert (std::make_pair (RecEnable, Button (*this, _("RecEnable"), RecEnable)));
152 buttons.insert (std::make_pair (Footswitch, Button (*this, _("Footswitch"), Footswitch)));
153 buttons.insert (std::make_pair (FaderTouch, Button (*this, _("Fader (touch)"), FaderTouch)));
155 get_button (Left).set_action ( boost::bind (&CC121::left, this), true);
156 get_button (Right).set_action ( boost::bind (&CC121::right, this), true);
158 get_button (FP_Read).set_action (boost::bind (&CC121::read, this), true);
159 get_button (FP_Write).set_action (boost::bind (&CC121::write, this), true);
160 get_button (EButton).set_action (boost::bind (&CC121::touch, this), true);
161 get_button (OpenVST).set_action (boost::bind (&CC121::off, this), true);
163 get_button (Play).set_action (boost::bind (&BasicUI::transport_play, this, true), true);
164 get_button (ToStart).set_action (boost::bind (&BasicUI::prev_marker, this), true);
165 get_button (ToEnd).set_action (boost::bind (&BasicUI::next_marker, this), true);
166 get_button (RecEnable).set_action (boost::bind (&BasicUI::rec_enable_toggle, this), true);
167 get_button (Stop).set_action (boost::bind (&BasicUI::transport_stop, this), true);
168 get_button (Ffwd).set_action (boost::bind (&BasicUI::ffwd, this), true);
170 get_button (Rewind).set_action (boost::bind (&BasicUI::rewind, this), true);
171 get_button (Loop).set_action (boost::bind (&BasicUI::loop_toggle, this), true);
173 get_button (Jog).set_action (boost::bind (&CC121::jog, this), true);
174 get_button (Mute).set_action (boost::bind (&CC121::mute, this), true);
175 get_button (Solo).set_action (boost::bind (&CC121::solo, this), true);
176 get_button (Rec).set_action (boost::bind (&CC121::rec_enable, this), true);
178 get_button (InputMonitor).set_action (boost::bind (&CC121::input_monitor, this), true);
186 DEBUG_TRACE (DEBUG::CC121, string_compose ("unregistering input port %1\n", boost::shared_ptr<ARDOUR::Port>(_input_port)->name()));
187 AudioEngine::instance()->unregister_port (_input_port);
188 _input_port.reset ();
192 _output_port->drain (10000, 250000); /* check every 10 msecs, wait up to 1/4 second for the port to drain */
193 DEBUG_TRACE (DEBUG::CC121, string_compose ("unregistering output port %1\n", boost::shared_ptr<ARDOUR::Port>(_output_port)->name()));
194 AudioEngine::instance()->unregister_port (_output_port);
195 _output_port.reset ();
200 /* stop event loop */
201 DEBUG_TRACE (DEBUG::CC121, "BaseUI::quit ()\n");
206 CC121::request_factory (uint32_t num_requests)
208 /* AbstractUI<T>::request_buffer_factory() is a template method only
209 instantiated in this source module. To provide something visible for
210 use in the interface/descriptor, we have this static method that is
213 return request_buffer_factory (num_requests);
217 CC121::start_midi_handling ()
219 /* handle buttons press */
220 _input_port->parser()->channel_note_on[0].connect_same_thread (midi_connections, boost::bind (&CC121::button_press_handler, this, _1, _2));
221 /* handle buttons release*/
222 _input_port->parser()->channel_note_off[0].connect_same_thread (midi_connections, boost::bind (&CC121::button_release_handler, this, _1, _2));
224 _input_port->parser()->pitchbend.connect_same_thread (midi_connections, boost::bind (&CC121::fader_handler, this, _1, _2));
226 _input_port->parser()->controller.connect_same_thread (midi_connections, boost::bind (&CC121::encoder_handler, this, _1, _2));
228 /* This connection means that whenever data is ready from the input
229 * port, the relevant thread will invoke our ::midi_input_handler()
230 * method, which will read the data, and invoke the parser.
233 _input_port->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &CC121::midi_input_handler), _input_port));
234 _input_port->xthread().attach (main_loop()->get_context());
238 CC121::stop_midi_handling ()
240 midi_connections.drop_connections ();
242 /* Note: the input handler is still active at this point, but we're no
243 * longer connected to any of the parser signals
248 CC121::do_request (CC121Request* req)
250 if (req->type == CallSlot) {
252 call_slot (MISSING_INVALIDATOR, req->the_slot);
254 } else if (req->type == Quit) {
269 CC121::thread_init ()
271 pthread_set_name (event_loop_name().c_str());
273 PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
274 ARDOUR::SessionEvent::create_per_thread_pool (event_loop_name(), 128);
276 set_thread_priority ();
280 CC121::all_lights_out ()
282 for (ButtonMap::iterator b = buttons.begin(); b != buttons.end(); ++b) {
283 b->second.set_led_state (_output_port, false);
288 CC121::get_button (ButtonID id) const
290 ButtonMap::const_iterator b = buttons.find (id);
291 assert (b != buttons.end());
292 return const_cast<Button&>(b->second);
296 CC121::button_press_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
298 DEBUG_TRACE (DEBUG::CC121, string_compose ("button press event for ID %1 press ? %2\n", (int) tb->controller_number, (tb->value ? "yes" : "no")));
300 ButtonID id (ButtonID (tb->controller_number));
301 Button& button (get_button (id));
303 buttons_down.insert (id);
304 ButtonState bs (ButtonState (0));
308 fader_is_touched = true;
309 if (_current_stripable) {
310 boost::shared_ptr<AutomationControl> gain = _current_stripable->gain_control ();
312 framepos_t now = session->engine().sample_time();
313 gain->start_touch (now);
322 button_state = ButtonState (button_state|bs);
323 DEBUG_TRACE (DEBUG::CC121, string_compose ("reset button state to %1 using %2\n", button_state, (int) bs));
326 if (button.uses_flash()) {
327 button.set_led_state (_output_port, (int)tb->value);
330 set<ButtonID>::iterator c = consumed.find (id);
332 if (c == consumed.end()) {
333 button.invoke (button_state, true);
335 DEBUG_TRACE (DEBUG::CC121, "button was consumed, ignored\n");
341 CC121::button_release_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
343 DEBUG_TRACE (DEBUG::CC121, string_compose ("button release event for ID %1 release ? %2\n", (int) tb->controller_number, (tb->value ? "yes" : "no")));
345 ButtonID id (ButtonID (tb->controller_number));
346 Button& button (get_button (id));
348 buttons_down.erase (id);
349 button.timeout_connection.disconnect ();
351 ButtonState bs (ButtonState (0));
355 fader_is_touched = false;
356 if (_current_stripable) {
357 boost::shared_ptr<AutomationControl> gain = _current_stripable->gain_control ();
359 framepos_t now = session->engine().sample_time();
360 gain->stop_touch (now);
369 button_state = ButtonState (button_state&~bs);
370 DEBUG_TRACE (DEBUG::CC121, string_compose ("reset button state to %1 using %2\n", button_state, (int) bs));
373 if (button.uses_flash()) {
374 button.set_led_state (_output_port, (int)tb->value);
377 set<ButtonID>::iterator c = consumed.find (id);
379 if (c == consumed.end()) {
380 button.invoke (button_state, false);
382 DEBUG_TRACE (DEBUG::CC121, "button was consumed, ignored\n");
388 CC121::encoder_handler (MIDI::Parser &, MIDI::EventTwoBytes* tb)
390 DEBUG_TRACE (DEBUG::CC121, "encoder handler");
392 /* Extract absolute value*/
393 float adj = static_cast<float>(tb->value & ~0x40);
395 /* Get direction (negative values start at 0x40)*/
396 float sign = (tb->value & 0x40) ? -1.0 : 1.0;
397 switch(tb->controller_number) {
400 DEBUG_TRACE (DEBUG::CC121, "PAN encoder");
401 if (_current_stripable) {
402 /* Get amount of change (encoder clicks) * (change per click)*/
403 /*Create an exponential curve*/
404 float curve = sign * powf (adj, (1.f + 10.f) / 10.f);
405 adj = curve * (31.f / 1000.f);
406 ardour_pan_azimuth (adj);
436 if (_jogmode == scroll) {
437 ScrollTimeline(-0.05);
444 if (_jogmode == scroll) {
445 ScrollTimeline(0.05);
473 CC121::fader_handler (MIDI::Parser &, MIDI::pitchbend_t pb)
475 DEBUG_TRACE (DEBUG::CC121, "fader handler");
477 if (_current_stripable) {
478 boost::shared_ptr<AutomationControl> gain = _current_stripable->gain_control ();
480 float val = gain->interface_to_internal (pb/16384.0);
481 /* even though the cc121 only controls a
482 single stripable at a time, allow the fader to
483 modify the group, if appropriate.
485 _current_stripable->gain_control()->set_value (val, Controllable::UseGroup);
491 CC121::set_active (bool yn)
493 DEBUG_TRACE (DEBUG::CC121, string_compose("CC121::set_active init with yn: '%1'\n", yn));
495 if (yn == active()) {
501 /* start event loop */
505 connect_session_signals ();
507 Glib::RefPtr<Glib::TimeoutSource> blink_timeout = Glib::TimeoutSource::create (200); // milliseconds
508 blink_connection = blink_timeout->connect (sigc::mem_fun (*this, &CC121::blink));
509 blink_timeout->attach (main_loop()->get_context());
511 Glib::RefPtr<Glib::TimeoutSource> heartbeat_timeout = Glib::TimeoutSource::create (800); // milliseconds
512 heartbeat_connection = heartbeat_timeout->connect (sigc::mem_fun (*this, &CC121::beat));
513 heartbeat_timeout->attach (main_loop()->get_context());
515 Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (100); // milliseconds
516 periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &CC121::periodic));
517 periodic_timeout->attach (main_loop()->get_context());
526 ControlProtocol::set_active (yn);
528 DEBUG_TRACE (DEBUG::CC121, string_compose("CC121::set_active done with yn: '%1'\n", yn));
536 if (!_current_stripable) {
540 ARDOUR::AutoState gain_state = _current_stripable->gain_control()->automation_state();
542 if (gain_state == ARDOUR::Touch || gain_state == ARDOUR::Play) {
550 CC121::stop_blinking (ButtonID id)
552 blinkers.remove (id);
553 get_button (id).set_led_state (_output_port, false);
557 CC121::start_blinking (ButtonID id)
559 blinkers.push_back (id);
560 get_button (id).set_led_state (_output_port, true);
577 _output_port->write (buf, 8, 0);
585 blink_state = !blink_state;
587 for (Blinkers::iterator b = blinkers.begin(); b != blinkers.end(); b++) {
588 get_button(*b).set_led_state (_output_port, blink_state);
591 map_recenable_state ();
601 stop_midi_handling ();
602 session_connections.drop_connections ();
603 port_connection.disconnect ();
604 blink_connection.disconnect ();
605 heartbeat_connection.disconnect ();
606 selection_connection.disconnect ();
607 stripable_connections.drop_connections ();
610 stripable_connections.drop_connections ();
615 CC121::map_recenable_state ()
617 /* special case for RecEnable because its status can change as a
618 * confluence of unrelated parameters: (a) session rec-enable state (b)
619 * rec-enabled tracks. So we don't add the button to the blinkers list,
622 * * from the blink callback
623 * * when the session tells us about a status change
625 * We do the last one so that the button changes state promptly rather
626 * than waiting for the next blink callback. The change in "blinking"
627 * based on having record-enabled tracks isn't urgent, and that happens
628 * during the blink callback.
633 switch (session->record_status()) {
634 case Session::Disabled:
637 case Session::Enabled:
640 case Session::Recording:
641 if (session->have_rec_enabled_track ()) {
649 if (onoff != rec_enable_state) {
650 get_button(RecEnable).set_led_state (_output_port, onoff);
651 rec_enable_state = onoff;
656 CC121::map_transport_state ()
658 get_button (Loop).set_led_state (_output_port, session->get_play_loop());
660 float ts = session->transport_speed();
663 stop_blinking (Play);
664 } else if (fabs (ts) == 1.0) {
665 stop_blinking (Play);
666 get_button (Play).set_led_state (_output_port, true);
668 start_blinking (Play);
671 get_button (Stop).set_led_state (_output_port, session->transport_stopped ());
672 get_button (Rewind).set_led_state (_output_port, session->transport_speed() < 0.0);
673 get_button (Ffwd).set_led_state (_output_port, session->transport_speed() > 1.0);
674 get_button (Jog).set_led_state (_output_port, _jogmode == scroll);
678 CC121::connect_session_signals()
680 session->RecordStateChanged.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&CC121::map_recenable_state, this), this);
681 session->TransportStateChange.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&CC121::map_transport_state, this), this);
685 CC121::midi_input_handler (Glib::IOCondition ioc, boost::shared_ptr<ARDOUR::AsyncMIDIPort> port)
687 DEBUG_TRACE (DEBUG::CC121, string_compose ("something happend on %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
696 DEBUG_TRACE (DEBUG::CC121, string_compose ("data available on %1\n", boost::shared_ptr<MIDI::Port>(port)->name()));
697 framepos_t now = session->engine().sample_time();
708 XMLNode& node (ControlProtocol::get_state());
712 child = new XMLNode (X_("Input"));
713 child->add_child_nocopy (boost::shared_ptr<ARDOUR::Port>(_input_port)->get_state());
714 node.add_child_nocopy (*child);
717 child = new XMLNode (X_("Output"));
718 child->add_child_nocopy (boost::shared_ptr<ARDOUR::Port>(_output_port)->get_state());
719 node.add_child_nocopy (*child);
721 /* Save action state for Function1..4, Lock, Value, EQnEnable, EQType,
722 * AllBypass and Footswitch buttons, since these
723 * are user controlled. We can only save named-action operations, since
724 * internal functions are just pointers to functions and hard to
725 * serialize without enumerating them all somewhere.
728 node.add_child_nocopy (get_button (Function1).get_state());
729 node.add_child_nocopy (get_button (Function2).get_state());
730 node.add_child_nocopy (get_button (Function3).get_state());
731 node.add_child_nocopy (get_button (Function4).get_state());
732 node.add_child_nocopy (get_button (Value).get_state());
733 node.add_child_nocopy (get_button (Lock).get_state());
734 node.add_child_nocopy (get_button (EQ1Enable).get_state());
735 node.add_child_nocopy (get_button (EQ2Enable).get_state());
736 node.add_child_nocopy (get_button (EQ3Enable).get_state());
737 node.add_child_nocopy (get_button (EQ4Enable).get_state());
738 node.add_child_nocopy (get_button (EQType).get_state());
739 node.add_child_nocopy (get_button (AllBypass).get_state());
740 node.add_child_nocopy (get_button (Footswitch).get_state());
746 CC121::set_state (const XMLNode& node, int version)
749 XMLNodeConstIterator niter;
750 XMLNode const* child;
752 if (ControlProtocol::set_state (node, version)) {
756 if ((child = node.child (X_("Input"))) != 0) {
757 XMLNode* portnode = child->child (Port::state_node_name.c_str());
759 boost::shared_ptr<ARDOUR::Port>(_input_port)->set_state (*portnode, version);
763 if ((child = node.child (X_("Output"))) != 0) {
764 XMLNode* portnode = child->child (Port::state_node_name.c_str());
766 boost::shared_ptr<ARDOUR::Port>(_output_port)->set_state (*portnode, version);
770 for (XMLNodeList::const_iterator n = node.children().begin(); n != node.children().end(); ++n) {
771 if ((*n)->name() == X_("Button")) {
773 if (!node.get_property ("id", xid)) {
776 ButtonMap::iterator b = buttons.find (ButtonID (xid));
777 if (b == buttons.end ()) {
780 b->second.set_state (**n);
788 CC121::connection_handler (boost::weak_ptr<ARDOUR::Port>, std::string name1, boost::weak_ptr<ARDOUR::Port>, std::string name2, bool yn)
790 DEBUG_TRACE (DEBUG::CC121, "CC121::connection_handler start\n");
791 if (!_input_port || !_output_port) {
795 string ni = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_input_port)->name());
796 string no = ARDOUR::AudioEngine::instance()->make_port_name_non_relative (boost::shared_ptr<ARDOUR::Port>(_output_port)->name());
798 if (ni == name1 || ni == name2) {
800 connection_state |= InputConnected;
802 connection_state &= ~InputConnected;
804 } else if (no == name1 || no == name2) {
806 connection_state |= OutputConnected;
808 connection_state &= ~OutputConnected;
811 DEBUG_TRACE (DEBUG::CC121, string_compose ("Connections between %1 and %2 changed, but I ignored it\n", name1, name2));
816 if ((connection_state & (InputConnected|OutputConnected)) == (InputConnected|OutputConnected)) {
818 /* XXX this is a horrible hack. Without a short sleep here,
819 something prevents the device wakeup messages from being
820 sent and/or the responses from being received.
824 DEBUG_TRACE (DEBUG::CC121, "device now connected for both input and output\n");
828 DEBUG_TRACE (DEBUG::CC121, "Device disconnected (input or output or both) or not yet fully connected\n");
829 _device_active = false;
832 ConnectionChange (); /* emit signal for our GUI */
834 DEBUG_TRACE (DEBUG::CC121, "CC121::connection_handler end\n");
836 return true; /* connection status changed */
842 DEBUG_TRACE (DEBUG::CC121, "connected");
844 _device_active = true;
846 start_midi_handling ();
850 /* catch up on state */
852 /* make sure that rec_enable_state is consistent with current device state */
853 get_button (RecEnable).set_led_state (_output_port, rec_enable_state);
855 map_transport_state ();
856 map_recenable_state ();
860 CC121::Button::invoke (CC121::ButtonState bs, bool press)
862 DEBUG_TRACE (DEBUG::CC121, string_compose ("invoke button %1 for %2 state %3%4%5\n", id, (press ? "press":"release"), hex, bs, dec));
867 if ((x = on_press.find (bs)) == on_press.end()) {
868 DEBUG_TRACE (DEBUG::CC121, string_compose ("no press action for button %1 state %2 @ %3 in %4\n", id, bs, this, &on_press));
872 if ((x = on_release.find (bs)) == on_release.end()) {
873 DEBUG_TRACE (DEBUG::CC121, string_compose ("no release action for button %1 state %2 @%3 in %4\n", id, bs, this, &on_release));
878 switch (x->second.type) {
880 if (!x->second.action_name.empty()) {
881 fp.access_action (x->second.action_name);
884 case InternalFunction:
885 if (x->second.function) {
886 x->second.function ();
892 CC121::Button::set_action (string const& name, bool when_pressed, CC121::ButtonState bs)
896 todo.type = NamedAction;
902 DEBUG_TRACE (DEBUG::CC121, string_compose ("set button %1 to action %2 on press + %3%4%5\n", id, name, bs));
903 todo.action_name = name;
908 on_release.erase (bs);
910 DEBUG_TRACE (DEBUG::CC121, string_compose ("set button %1 to action %2 on release + %3%4%5\n", id, name, bs));
911 todo.action_name = name;
912 on_release[bs] = todo;
918 CC121::Button::get_action (bool press, CC121::ButtonState bs)
923 if ((x = on_press.find (bs)) == on_press.end()) {
926 if (x->second.type != NamedAction) {
929 return x->second.action_name;
931 if ((x = on_release.find (bs)) == on_release.end()) {
934 if (x->second.type != NamedAction) {
937 return x->second.action_name;
942 CC121::Button::set_action (boost::function<void()> f, bool when_pressed, CC121::ButtonState bs)
945 todo.type = InternalFunction;
948 DEBUG_TRACE (DEBUG::CC121, string_compose ("set button %1 (%2) @ %5 to some functor on press + %3 in %4\n", id, name, bs, &on_press, this));
952 DEBUG_TRACE (DEBUG::CC121, string_compose ("set button %1 (%2) @ %5 to some functor on release + %3\n", id, name, bs, this));
954 on_release[bs] = todo;
959 CC121::Button::set_led_state (boost::shared_ptr<MIDI::Port> port, bool onoff)
962 DEBUG_TRACE(DEBUG::CC121, "Set Led State\n");
965 buf[2] = (onoff ? 0x7F:0x00);
966 port->write (buf, 3, 0);
970 CC121::Button::set_state (XMLNode const& node)
973 if (node.get_property ("id", xid) && xid != id) {
977 typedef pair<string,CC121::ButtonState> state_pair_t;
978 vector<state_pair_t> state_pairs;
980 state_pairs.push_back (make_pair (string ("plain"), ButtonState (0)));
982 for (vector<state_pair_t>::const_iterator sp = state_pairs.begin(); sp != state_pairs.end(); ++sp) {
986 prop_name = sp->first + X_("-press");
987 if (node.get_property (prop_name.c_str(), prop_value)) {
988 set_action (prop_value, true, sp->second);
991 prop_name = sp->first + X_("-release");
992 if (node.get_property (prop_name.c_str(), prop_value)) {
993 set_action (prop_value, false, sp->second);
1001 CC121::Button::get_state () const
1003 XMLNode* node = new XMLNode (X_("Button"));
1005 node->set_property (X_("id"), (int32_t)id);
1007 ToDoMap::const_iterator x;
1009 null.type = NamedAction;
1011 typedef pair<string,CC121::ButtonState> state_pair_t;
1012 vector<state_pair_t> state_pairs;
1014 state_pairs.push_back (make_pair (string ("plain"), ButtonState (0)));
1016 for (vector<state_pair_t>::const_iterator sp = state_pairs.begin(); sp != state_pairs.end(); ++sp) {
1017 if ((x = on_press.find (sp->second)) != on_press.end()) {
1018 if (x->second.type == NamedAction) {
1019 node->set_property (string (sp->first + X_("-press")).c_str(), x->second.action_name);
1023 if ((x = on_release.find (sp->second)) != on_release.end()) {
1024 if (x->second.type == NamedAction) {
1025 node->set_property (string (sp->first + X_("-release")).c_str(), x->second.action_name);
1034 CC121::stripable_selection_changed ()
1036 set_current_stripable (first_selected_stripable());
1040 CC121::drop_current_stripable ()
1042 if (_current_stripable) {
1043 if (_current_stripable == session->monitor_out()) {
1044 set_current_stripable (session->master_out());
1046 set_current_stripable (boost::shared_ptr<Stripable>());
1052 CC121::set_current_stripable (boost::shared_ptr<Stripable> r)
1054 stripable_connections.drop_connections ();
1056 _current_stripable = r;
1058 if (_current_stripable) {
1059 _current_stripable->DropReferences.connect (stripable_connections, MISSING_INVALIDATOR, boost::bind (&CC121::drop_current_stripable, this), this);
1061 _current_stripable->mute_control()->Changed.connect (stripable_connections, MISSING_INVALIDATOR, boost::bind (&CC121::map_mute, this), this);
1062 _current_stripable->solo_control()->Changed.connect (stripable_connections, MISSING_INVALIDATOR, boost::bind (&CC121::map_solo, this), this);
1064 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (_current_stripable);
1066 t->rec_enable_control()->Changed.connect (stripable_connections, MISSING_INVALIDATOR, boost::bind (&CC121::map_recenable, this), this);
1069 boost::shared_ptr<AutomationControl> control = _current_stripable->gain_control ();
1071 control->Changed.connect (stripable_connections, MISSING_INVALIDATOR, boost::bind (&CC121::map_gain, this), this);
1072 control->alist()->automation_state_changed.connect (stripable_connections, MISSING_INVALIDATOR, boost::bind (&CC121::map_auto, this), this);
1075 boost::shared_ptr<MonitorProcessor> mp = _current_stripable->monitor_control();
1077 mp->cut_control()->Changed.connect (stripable_connections, MISSING_INVALIDATOR, boost::bind (&CC121::map_cut, this), this);
1081 //ToDo: subscribe to the fader automation modes so we can light the LEDs
1083 map_stripable_state ();
1089 boost::shared_ptr<AutomationControl> control = _current_stripable->gain_control ();
1090 const AutoState as = control->automation_state ();
1094 get_button (FP_Read).set_led_state (_output_port, true);
1095 get_button (FP_Write).set_led_state (_output_port, false);
1096 get_button (EButton).set_led_state (_output_port, false);
1097 get_button (OpenVST).set_led_state (_output_port, false);
1100 get_button (FP_Read).set_led_state (_output_port, false);
1101 get_button (FP_Write).set_led_state (_output_port, true);
1102 get_button (EButton).set_led_state (_output_port, false);
1103 get_button (OpenVST).set_led_state (_output_port, false);
1106 get_button (EButton).set_led_state (_output_port, true);
1107 get_button (FP_Read).set_led_state (_output_port, false);
1108 get_button (FP_Write).set_led_state(_output_port, false);
1109 get_button (OpenVST).set_led_state (_output_port, false);
1112 get_button (OpenVST).set_led_state (_output_port, true);
1113 get_button (FP_Read).set_led_state (_output_port, false);
1114 get_button (FP_Write).set_led_state (_output_port, false);
1115 get_button (EButton).set_led_state (_output_port, false);
1123 boost::shared_ptr<MonitorProcessor> mp = _current_stripable->monitor_control();
1126 bool yn = mp->cut_all ();
1128 start_blinking (Mute);
1130 stop_blinking (Mute);
1133 stop_blinking (Mute);
1140 if (_current_stripable) {
1141 if (_current_stripable->mute_control()->muted()) {
1142 stop_blinking (Mute);
1143 get_button (Mute).set_led_state (_output_port, true);
1144 } else if (_current_stripable->mute_control()->muted_by_others_soloing () || _current_stripable->mute_control()->muted_by_masters()) {
1145 start_blinking (Mute);
1147 stop_blinking (Mute);
1150 stop_blinking (Mute);
1157 if (_current_stripable) {
1158 get_button (Solo).set_led_state (_output_port, _current_stripable->solo_control()->soloed());
1160 get_button (Solo).set_led_state (_output_port, false);
1165 CC121::map_recenable ()
1167 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (_current_stripable);
1169 get_button (Rec).set_led_state (_output_port, t->rec_enable_control()->get_value());
1171 get_button (Rec).set_led_state (_output_port, false);
1178 if (fader_is_touched) {
1179 /* Do not send fader moves while the user is touching the fader */
1183 if (!_current_stripable) {
1187 boost::shared_ptr<AutomationControl> control = _current_stripable->gain_control ();
1193 val = control->internal_to_interface (control->get_value ());
1196 int ival = (int)((val * 16384.0f) + 0.5f);
1200 else if (ival > 16383) {
1207 buf[1] = ival & 0x7F;
1208 buf[2] = (ival >> 7) & 0x7F;
1210 _output_port->write (buf, 3, 0);
1214 CC121::map_stripable_state ()
1216 if (!_current_stripable) {
1217 stop_blinking (Mute);
1218 stop_blinking (Solo);
1219 get_button (Rec).set_led_state (_output_port, false);
1226 if (_current_stripable == session->monitor_out()) {
1234 list<boost::shared_ptr<ARDOUR::Bundle> >
1237 list<boost::shared_ptr<ARDOUR::Bundle> > b;
1239 if (_input_bundle) {
1240 b.push_back (_input_bundle);
1241 b.push_back (_output_bundle);
1247 boost::shared_ptr<Port>
1248 CC121::output_port()
1250 return _output_port;
1253 boost::shared_ptr<Port>
1260 CC121::set_action (ButtonID id, std::string const& action_name, bool on_press, ButtonState bs)
1262 get_button(id).set_action (action_name, on_press, bs);
1266 CC121::get_action (ButtonID id, bool press, ButtonState bs)
1268 return get_button(id).get_action (press, bs);