2 Copyright (C) 2012 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 "midi++/port.h"
28 #include "ardour/automation_control.h"
29 #include "ardour/debug.h"
30 #include "ardour/route.h"
31 #include "ardour/panner.h"
32 #include "ardour/panner_shell.h"
33 #include "ardour/rc_configuration.h"
34 #include "ardour/session.h"
35 #include "ardour/utils.h"
37 #include "control_group.h"
38 #include "surface_port.h"
41 #include "mackie_control_protocol.h"
42 #include "jog_wheel.h"
56 using namespace Mackie;
59 using ARDOUR::Pannable;
60 using ARDOUR::AutomationControl;
62 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
64 // The MCU sysex header.4th byte Will be overwritten
65 // when we get an incoming sysex that identifies
67 static MidiByteArray mackie_sysex_hdr (5, MIDI::sysex, 0x0, 0x0, 0x66, 0x14);
69 // The MCU extender sysex header.4th byte Will be overwritten
70 // when we get an incoming sysex that identifies
72 static MidiByteArray mackie_sysex_hdr_xt (5, MIDI::sysex, 0x0, 0x0, 0x66, 0x15);
74 static MidiByteArray empty_midi_byte_array;
76 Surface::Surface (MackieControlProtocol& mcp, const std::string& device_name, uint32_t number, surface_type_t stype)
85 , _last_master_gain_written (-0.0f)
87 DEBUG_TRACE (DEBUG::MackieControl, "Surface::Surface init\n");
89 _port = new SurfacePort (*this);
91 /* only the first Surface object has global controls */
94 DEBUG_TRACE (DEBUG::MackieControl, "Surface is first. Might have global controls.\n");
95 if (_mcp.device_info().has_global_controls()) {
97 DEBUG_TRACE (DEBUG::MackieControl, "init_controls done\n");
100 if (_mcp.device_info().has_master_fader()) {
102 DEBUG_TRACE (DEBUG::MackieControl, "setup_master done\n");
106 uint32_t n = _mcp.device_info().strip_cnt();
110 DEBUG_TRACE (DEBUG::MackieControl, "init_strips done\n");
113 connect_to_signals ();
115 DEBUG_TRACE (DEBUG::MackieControl, "Surface::Surface done\n");
120 DEBUG_TRACE (DEBUG::MackieControl, "Surface::~Surface init\n");
125 for (Groups::iterator it = groups.begin(); it != groups.end(); ++it) {
130 for (Controls::iterator it = controls.begin(); it != controls.end(); ++it) {
137 DEBUG_TRACE (DEBUG::MackieControl, "Surface::~Surface done\n");
141 Surface::sysex_hdr() const
144 case mcu: return mackie_sysex_hdr;
145 case ext: return mackie_sysex_hdr_xt;
147 cout << "SurfacePort::sysex_hdr _port_type not known" << endl;
148 return mackie_sysex_hdr;
151 static GlobalControlDefinition mackie_global_controls[] = {
152 { "external", Pot::External, Pot::factory, "none" },
153 { "fader_touch", Led::FaderTouch, Led::factory, "master" },
154 { "timecode", Led::Timecode, Led::factory, "none" },
155 { "beats", Led::Beats, Led::factory, "none" },
156 { "solo", Led::RudeSolo, Led::factory, "none" },
157 { "relay_click", Led::RelayClick, Led::factory, "none" },
158 { "", 0, Led::factory, "" }
162 Surface::init_controls()
166 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init_controls: creating groups\n");
167 groups["assignment"] = new Group ("assignment");
168 groups["automation"] = new Group ("automation");
169 groups["bank"] = new Group ("bank");
170 groups["cursor"] = new Group ("cursor");
171 groups["display"] = new Group ("display");
172 groups["function select"] = new Group ("function select");
173 groups["global view"] = new Group ("global view");
174 groups["master"] = new Group ("master");
175 groups["modifiers"] = new Group ("modifiers");
176 groups["none"] = new Group ("none");
177 groups["transport"] = new Group ("transport");
178 groups["user"] = new Group ("user");
179 groups["utilities"] = new Group ("utilities");
181 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init_controls: creating jog wheel\n");
182 if (_mcp.device_info().has_jog_wheel()) {
183 _jog_wheel = new Mackie::JogWheel (_mcp);
186 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init_controls: creating global controls\n");
187 for (uint32_t n = 0; mackie_global_controls[n].name[0]; ++n) {
188 group = groups[mackie_global_controls[n].group_name];
189 Control* control = mackie_global_controls[n].factory (*this, mackie_global_controls[n].id, mackie_global_controls[n].name, *group);
190 controls_by_device_independent_id[mackie_global_controls[n].id] = control;
193 /* add global buttons */
194 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init_controls: adding global buttons\n");
195 const map<Button::ID,GlobalButtonInfo>& global_buttons (_mcp.device_info().global_buttons());
197 for (map<Button::ID,GlobalButtonInfo>::const_iterator b = global_buttons.begin(); b != global_buttons.end(); ++b){
198 group = groups[b->second.group];
199 controls_by_device_independent_id[b->first] = Button::factory (*this, b->first, b->second.id, b->second.label, *group);
204 Surface::init_strips (uint32_t n)
206 const map<Button::ID,StripButtonInfo>& strip_buttons (_mcp.device_info().strip_buttons());
208 for (uint32_t i = 0; i < n; ++i) {
212 snprintf (name, sizeof (name), "strip_%d", (8* _number) + i);
214 Strip* strip = new Strip (*this, name, i, strip_buttons);
216 groups[name] = strip;
217 strips.push_back (strip);
222 Surface::setup_master ()
224 boost::shared_ptr<Route> m;
226 if ((m = _mcp.get_session().monitor_out()) == 0) {
227 m = _mcp.get_session().master_out();
234 _master_fader = dynamic_cast<Fader*> (Fader::factory (*this, _mcp.device_info().strip_cnt(), "master", *groups["master"]));
236 _master_fader->set_control (m->gain_control());
237 m->gain_control()->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&Surface::master_gain_changed, this), ui_context());
239 Groups::iterator group_it;
240 group_it = groups.find("master");
242 DeviceInfo device_info = _mcp.device_info();
243 GlobalButtonInfo master_button = device_info.get_global_button(Button::MasterFaderTouch);
244 Button* bb = dynamic_cast<Button*> (Button::factory (
246 Button::MasterFaderTouch,
251 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface %1 Master Fader new button BID %2 id %3\n",
252 number(), Button::MasterFaderTouch, bb->id()));
256 Surface::master_gain_changed ()
258 if (!_master_fader) {
262 boost::shared_ptr<AutomationControl> ac = _master_fader->control();
267 float normalized_position = ac->internal_to_interface (ac->get_value());
268 if (normalized_position == _last_master_gain_written) {
272 DEBUG_TRACE (DEBUG::MackieControl, "Surface::master_gain_changed: updating surface master fader\n");
274 _port->write (_master_fader->set_position (normalized_position));
275 _last_master_gain_written = normalized_position;
279 Surface::scaled_delta (float delta, float current_speed)
281 /* XXX needs work before use */
282 const float sign = delta < 0.0 ? -1.0 : 1.0;
284 return ((sign * std::pow (delta + 1.0, 2.0)) + current_speed) / 100.0;
288 Surface::display_bank_start (uint32_t current_bank)
290 if (current_bank == 0) {
291 // send Ar. to 2-char display on the master
292 show_two_char_display ("Ar", "..");
294 // write the current first remote_id to the 2-char display
295 show_two_char_display (current_bank);
300 Surface::blank_jog_ring ()
302 Control* control = controls_by_device_independent_id[Jog::ID];
305 Pot* pot = dynamic_cast<Pot*> (control);
307 _port->write (pot->set (0.0, false, Pot::spread));
313 Surface::scrub_scaling_factor () const
319 Surface::connect_to_signals ()
324 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 connecting to signals on port %2\n",
325 number(), _port->input_port().name()));
327 MIDI::Parser* p = _port->input_port().parser();
330 p->sysex.connect_same_thread (*this, boost::bind (&Surface::handle_midi_sysex, this, _1, _2, _3));
331 /* V-Pot messages are Controller */
332 p->controller.connect_same_thread (*this, boost::bind (&Surface::handle_midi_controller_message, this, _1, _2));
333 /* Button messages are NoteOn */
334 p->note_on.connect_same_thread (*this, boost::bind (&Surface::handle_midi_note_on_message, this, _1, _2));
335 /* Button messages are NoteOn. libmidi++ sends note-on w/velocity = 0 as note-off so catch them too */
336 p->note_off.connect_same_thread (*this, boost::bind (&Surface::handle_midi_note_on_message, this, _1, _2));
337 /* Fader messages are Pitchbend */
339 for (i = 0; i < _mcp.device_info().strip_cnt(); i++) {
340 p->channel_pitchbend[i].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, i));
343 p->channel_pitchbend[_mcp.device_info().strip_cnt()].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, _mcp.device_info().strip_cnt()));
350 Surface::handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t pb, uint32_t fader_id)
352 /* Pitchbend messages are fader messages. Nothing in the data we get
353 * from the MIDI::Parser conveys the fader ID, which was given by the
354 * channel ID in the status byte.
356 * Instead, we have used bind() to supply the fader-within-strip ID
357 * when we connected to the per-channel pitchbend events.
361 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface::handle_midi_pitchbend_message on port %3, fader = %1 value = %2\n",
362 fader_id, pb, _number));
364 if (_mcp.device_info().no_handshake()) {
368 Fader* fader = faders[fader_id];
371 Strip* strip = dynamic_cast<Strip*> (&fader->group());
372 float pos = (pb >> 4)/1023.0; // only the top 10 bytes are used
374 strip->handle_fader (*fader, pos);
376 DEBUG_TRACE (DEBUG::MackieControl, "Handling master fader\n");
378 fader->set_value (pos); // alter master gain
379 _port->write (fader->set_position (pos)); // write back value (required for servo)
382 DEBUG_TRACE (DEBUG::MackieControl, "fader not found\n");
387 Surface::handle_midi_note_on_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
389 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface::handle_midi_note_on_message %1 = %2\n", (int) ev->note_number, (int) ev->velocity));
391 if (_mcp.device_info().no_handshake()) {
395 Button* button = buttons[ev->note_number];
398 Strip* strip = dynamic_cast<Strip*> (&button->group());
401 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip %1 button %2 pressed ? %3\n",
402 strip->index(), button->name(), (ev->velocity > 64)));
403 strip->handle_button (*button, ev->velocity > 64 ? press : release);
406 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("global button %1\n", button->id()));
407 _mcp.handle_button_event (*this, *button, ev->velocity > 64 ? press : release);
410 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("no button found for %1\n", (int) ev->note_number));
415 Surface::handle_midi_controller_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
417 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("SurfacePort::handle_midi_controller %1 = %2\n", (int) ev->controller_number, (int) ev->value));
419 if (_mcp.device_info().no_handshake()) {
423 Pot* pot = pots[ev->controller_number];
425 // bit 6 gives the sign
426 float sign = (ev->value & 0x40) == 0 ? 1.0 : -1.0;
427 // bits 0..5 give the velocity. we interpret this as "ticks
428 // moved before this message was sent"
429 float ticks = (ev->value & 0x3f);
431 /* euphonix and perhaps other devices send zero
432 when they mean 1, we think.
436 float delta = sign * (ticks / (float) 0x3f);
439 if (ev->controller_number == Jog::ID && _jog_wheel) {
441 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Jog wheel moved %1\n", ticks));
442 _jog_wheel->jog_event (delta);
449 Strip* strip = dynamic_cast<Strip*> (&pot->group());
451 strip->handle_pot (*pot, delta);
456 Surface::handle_midi_sysex (MIDI::Parser &, MIDI::byte * raw_bytes, size_t count)
458 MidiByteArray bytes (count, raw_bytes);
460 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handle_midi_sysex: %1\n", bytes));
462 if (_mcp.device_info().no_handshake()) {
466 /* always save the device type ID so that our outgoing sysex messages
471 mackie_sysex_hdr[4] = bytes[4];
473 mackie_sysex_hdr_xt[4] = bytes[4];
479 LCP: Connection Challenge
481 if (bytes[4] == 0x10 || bytes[4] == 0x11) {
482 write_sysex (host_connection_query (bytes));
490 case 0x03: /* LCP Connection Confirmation */
491 if (bytes[4] == 0x10 || bytes[4] == 0x11) {
492 write_sysex (host_connection_confirmation (bytes));
497 case 0x04: /* LCP: Confirmation Denied */
501 error << "MCP: unknown sysex: " << bytes << endmsg;
506 calculate_challenge_response (MidiByteArray::iterator begin, MidiByteArray::iterator end)
509 back_insert_iterator<MidiByteArray> back (l);
510 copy (begin, end, back);
512 MidiByteArray retval;
514 // this is how to calculate the response to the challenge.
515 // from the Logic docs.
516 retval << (0x7f & (l[0] + (l[1] ^ 0xa) - l[3]));
517 retval << (0x7f & ( (l[2] >> l[3]) ^ (l[0] + l[3])));
518 retval << (0x7f & ((l[3] - (l[2] << 2)) ^ (l[0] | l[1])));
519 retval << (0x7f & (l[1] - l[2] + (0xf0 ^ (l[3] << 4))));
524 // not used right now
526 Surface::host_connection_query (MidiByteArray & bytes)
528 MidiByteArray response;
530 if (bytes[4] != 0x10 && bytes[4] != 0x11) {
531 /* not a Logic Control device - no response required */
535 // handle host connection query
536 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host connection query: %1\n", bytes));
538 if (bytes.size() != 18) {
539 cerr << "expecting 18 bytes, read " << bytes << " from " << _port->input_port().name() << endl;
543 // build and send host connection reply
545 copy (bytes.begin() + 6, bytes.begin() + 6 + 7, back_inserter (response));
546 response << calculate_challenge_response (bytes.begin() + 6 + 7, bytes.begin() + 6 + 7 + 4);
550 // not used right now
552 Surface::host_connection_confirmation (const MidiByteArray & bytes)
554 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host_connection_confirmation: %1\n", bytes));
556 // decode host connection confirmation
557 if (bytes.size() != 14) {
559 os << "expecting 14 bytes, read " << bytes << " from " << _port->input_port().name();
560 throw MackieControlException (os.str());
563 // send version request
564 return MidiByteArray (2, 0x13, 0x00);
568 Surface::turn_it_on ()
576 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
580 update_view_mode_display ();
582 if (_mcp.device_info ().has_global_controls ()) {
583 _mcp.update_global_button (Button::Read, _mcp.metering_active ());
588 Surface::handle_port_inactive (SurfacePort*)
594 Surface::write_sysex (const MidiByteArray & mba)
601 buf << sysex_hdr() << mba << MIDI::eox;
606 Surface::write_sysex (MIDI::byte msg)
609 buf << sysex_hdr() << msg << MIDI::eox;
614 Surface::n_strips (bool with_locked_strips) const
616 if (with_locked_strips) {
617 return strips.size();
622 for (Strips::const_iterator it = strips.begin(); it != strips.end(); ++it) {
623 if (!(*it)->locked()) {
631 Surface::nth_strip (uint32_t n) const
633 if (n > n_strips()) {
642 if (_mcp.device_info().has_timecode_display ()) {
643 display_timecode (string (10, '0'), string (10, ' '));
646 if (_mcp.device_info().has_two_character_display()) {
647 show_two_char_display (string (2, '0'), string (2, ' '));
650 if (_mcp.device_info().has_master_fader () && _master_fader) {
651 _port->write (_master_fader->zero ());
655 for (Strips::iterator it = strips.begin(); it != strips.end(); ++it) {
663 Surface::zero_controls ()
665 if (!_mcp.device_info().has_global_controls()) {
669 // turn off global buttons and leds
670 // global buttons are only ever on mcu_port, so we don't have
671 // to figure out which port.
673 for (Controls::iterator it = controls.begin(); it != controls.end(); ++it) {
674 Control & control = **it;
675 if (!control.group().is_strip()) {
676 _port->write (control.zero());
680 // and the led ring for the master strip
683 _last_master_gain_written = 0.0f;
687 Surface::periodic (uint64_t now_usecs)
689 master_gain_changed();
690 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
691 (*s)->periodic (now_usecs);
696 Surface::write (const MidiByteArray& data)
701 DEBUG_TRACE (DEBUG::MackieControl, "surface not active, write ignored\n");
706 Surface::map_routes (const vector<boost::shared_ptr<Route> >& routes)
708 vector<boost::shared_ptr<Route> >::const_iterator r;
709 Strips::iterator s = strips.begin();
711 for (r = routes.begin(); r != routes.end() && s != strips.end(); ++s) {
713 /* don't try to assign routes to a locked strip. it won't
714 use it anyway, but if we do, then we get out of sync
715 with the proposed mapping.
718 if (!(*s)->locked()) {
719 (*s)->set_route (*r);
724 for (; s != strips.end(); ++s) {
725 (*s)->set_route (boost::shared_ptr<Route>());
732 translate_seven_segment (char achar)
734 achar = toupper (achar);
736 if (achar >= 0x40 && achar <= 0x60) {
738 } else if (achar >= 0x21 && achar <= 0x3f) {
746 Surface::show_two_char_display (const std::string & msg, const std::string & dots)
748 if (_stype != mcu || !_mcp.device_info().has_two_character_display() || msg.length() != 2 || dots.length() != 2) {
752 MidiByteArray right (3, 0xb0, 0x4b, 0x00);
753 MidiByteArray left (3, 0xb0, 0x4a, 0x00);
755 right[2] = translate_seven_segment (msg[0]) + (dots[0] == '.' ? 0x40 : 0x00);
756 left[2] = translate_seven_segment (msg[1]) + (dots[1] == '.' ? 0x40 : 0x00);
758 _port->write (right);
763 Surface::show_two_char_display (unsigned int value, const std::string & /*dots*/)
766 os << setfill('0') << setw(2) << value % 100;
767 show_two_char_display (os.str());
771 Surface::display_timecode (const std::string & timecode, const std::string & last_timecode)
773 if (!_active || !_mcp.device_info().has_timecode_display()) {
776 // if there's no change, send nothing, not even sysex header
777 if (timecode == last_timecode) return;
779 // length sanity checking
780 string local_timecode = timecode;
782 // truncate to 10 characters
783 if (local_timecode.length() > 10) {
784 local_timecode = local_timecode.substr (0, 10);
787 // pad to 10 characters
788 while (local_timecode.length() < 10) {
789 local_timecode += " ";
792 // translate characters.
793 // Only the characters that actually changed are sent.
796 for (i = local_timecode.length () - 1; i >= 0; i--) {
798 if (local_timecode[i] == last_timecode[i]) {
801 MidiByteArray retval (2, 0xb0, position);
802 retval << translate_seven_segment (local_timecode[i]);
803 _port->write (retval);
808 Surface::update_flip_mode_display ()
810 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
811 (*s)->flip_mode_changed (true);
816 Surface::update_view_mode_display ()
825 switch (_mcp.view_mode()) {
826 case MackieControlProtocol::Mixer:
827 show_two_char_display ("Mx");
830 case MackieControlProtocol::Dynamics:
831 show_two_char_display ("Dy");
834 case MackieControlProtocol::EQ:
835 show_two_char_display ("EQ");
838 case MackieControlProtocol::Loop:
839 show_two_char_display ("LP");
842 case MackieControlProtocol::AudioTracks:
843 show_two_char_display ("AT");
845 case MackieControlProtocol::MidiTracks:
846 show_two_char_display ("MT");
848 case MackieControlProtocol::Sends:
849 show_two_char_display ("Sn");
852 case MackieControlProtocol::Plugins:
853 show_two_char_display ("Pl");
862 /* we are attempting to turn a global button/LED on */
864 map<int,Control*>::iterator x = controls_by_device_independent_id.find (id);
866 if (x != controls_by_device_independent_id.end()) {
867 Button* button = dynamic_cast<Button*> (x->second);
869 _port->write (button->set_state (on));
875 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
876 _port->write ((*s)->display (1, text));
882 Surface::gui_selection_changed (const ARDOUR::StrongRouteNotificationList& routes)
884 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
885 (*s)->gui_selection_changed (routes);
890 Surface::say_hello ()
892 /* wakeup for Mackie Control */
893 MidiByteArray wakeup (7, MIDI::sysex, 0x00, 0x00, 0x66, 0x14, 0x00, MIDI::eox);
894 _port->write (wakeup);
895 wakeup[4] = 0x15; /* wakup Mackie XT */
896 _port->write (wakeup);
897 wakeup[4] = 0x10; /* wakupe Logic Control */
898 _port->write (wakeup);
899 wakeup[4] = 0x11; /* wakeup Logic Control XT */
900 _port->write (wakeup);
904 Surface::next_jog_mode ()
909 Surface::set_jog_mode (JogWheel::Mode)
914 Surface::route_is_locked_to_strip (boost::shared_ptr<Route> r) const
916 for (Strips::const_iterator s = strips.begin(); s != strips.end(); ++s) {
917 if ((*s)->route() == r && (*s)->locked()) {
925 Surface::notify_metering_state_changed()
927 for (Strips::const_iterator s = strips.begin(); s != strips.end(); ++s) {
928 (*s)->notify_metering_state_changed ();