7 #include "midi++/port.h"
8 #include "midi++/manager.h"
10 #include "ardour/automation_control.h"
11 #include "ardour/debug.h"
12 #include "ardour/route.h"
13 #include "ardour/panner.h"
14 #include "ardour/panner_shell.h"
15 #include "ardour/rc_configuration.h"
16 #include "ardour/session.h"
18 #include "control_group.h"
19 #include "surface_port.h"
22 #include "mackie_control_protocol.h"
23 #include "mackie_jog_wheel.h"
37 using namespace Mackie;
40 using ARDOUR::Pannable;
41 using ARDOUR::AutomationControl;
43 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
44 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
45 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
46 #define invalidator() __invalidator (*(MackieControlProtocol::instance()), __FILE__, __LINE__)
48 // The MCU sysex header.4th byte Will be overwritten
49 // when we get an incoming sysex that identifies
51 static MidiByteArray mackie_sysex_hdr (5, MIDI::sysex, 0x0, 0x0, 0x66, 0x14);
53 // The MCU extender sysex header.4th byte Will be overwritten
54 // when we get an incoming sysex that identifies
56 static MidiByteArray mackie_sysex_hdr_xt (5, MIDI::sysex, 0x0, 0x0, 0x66, 0x15);
58 static MidiByteArray empty_midi_byte_array;
60 Surface::Surface (MackieControlProtocol& mcp, const std::string& device_name, uint32_t number, surface_type_t stype)
69 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init\n");
71 _port = new SurfacePort (*this);
73 /* only the first Surface object has global controls */
76 if (_mcp.device_info().has_global_controls()) {
80 if (_mcp.device_info().has_master_fader()) {
85 uint32_t n = _mcp.device_info().strip_cnt();
91 connect_to_signals ();
95 MidiByteArray wakeup (7, MIDI::sysex, 0x00, 0x00, 0x66, 0x14, 0x00, MIDI::eox);
96 _port->write (wakeup);
97 wakeup[4] = 0x15; /* wakup Mackie XT */
98 _port->write (wakeup);
99 wakeup[4] = 0x10; /* wakupe Logic Control */
100 _port->write (wakeup);
101 wakeup[4] = 0x11; /* wakeup Logic Control XT */
102 _port->write (wakeup);
104 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init finish\n");
109 DEBUG_TRACE (DEBUG::MackieControl, "Surface: destructor\n");
115 // Reset (reboot into offline mode)
116 // _write_sysex (0x63);
119 for (Groups::iterator it = groups.begin(); it != groups.end(); ++it) {
124 for (Controls::iterator it = controls.begin(); it != controls.end(); ++it) {
133 Surface::sysex_hdr() const
136 case mcu: return mackie_sysex_hdr;
137 case ext: return mackie_sysex_hdr_xt;
139 cout << "SurfacePort::sysex_hdr _port_type not known" << endl;
140 return mackie_sysex_hdr;
143 static GlobalControlDefinition mackie_global_controls[] = {
144 { "external", Pot::External, Pot::factory, "none" },
145 { "fader_touch", Led::FaderTouch, Led::factory, "master" },
146 { "timecode", Led::Timecode, Led::factory, "none" },
147 { "beats", Led::Beats, Led::factory, "none" },
148 { "solo", Led::RudeSolo, Led::factory, "none" },
149 { "relay_click", Led::RelayClick, Led::factory, "none" },
150 { "", 0, Led::factory, "" }
154 Surface::init_controls()
158 groups["assignment"] = new Group ("assignment");
159 groups["automation"] = new Group ("automation");
160 groups["bank"] = new Group ("bank");
161 groups["cursor"] = new Group ("cursor");
162 groups["display"] = new Group ("display");
163 groups["functions"] = new Group ("functions");
164 groups["modifiers"] = new Group ("modifiers");
165 groups["none"] = new Group ("none");
166 groups["transport"] = new Group ("transport");
167 groups["user"] = new Group ("user");
168 groups["master"] = new Group ("master");
169 groups["view"] = new Group ("view");
171 if (_mcp.device_info().has_jog_wheel()) {
172 _jog_wheel = new Mackie::JogWheel (_mcp);
175 for (uint32_t n = 0; mackie_global_controls[n].name[0]; ++n) {
176 group = groups[mackie_global_controls[n].group_name];
177 Control* control = mackie_global_controls[n].factory (*this, mackie_global_controls[n].id, mackie_global_controls[n].name, *group);
178 controls_by_device_independent_id[mackie_global_controls[n].id] = control;
181 /* add global buttons */
183 const map<Button::ID,GlobalButtonInfo>& global_buttons (_mcp.device_info().global_buttons());
185 for (map<Button::ID,GlobalButtonInfo>::const_iterator b = global_buttons.begin(); b != global_buttons.end(); ++b){
186 group = groups[b->second.group];
187 controls_by_device_independent_id[b->first] = Button::factory (*this, b->first, b->second.id, b->second.label, *group);
192 Surface::init_strips (uint32_t n)
194 const map<Button::ID,StripButtonInfo>& strip_buttons (_mcp.device_info().strip_buttons());
196 for (uint32_t i = 0; i < n; ++i) {
200 snprintf (name, sizeof (name), "strip_%d", (8* _number) + i);
202 std::cerr << "*** Surface " << _number << " Setup strips for index " << i << endl;
204 Strip* strip = new Strip (*this, name, i, strip_buttons);
206 groups[name] = strip;
207 strips.push_back (strip);
212 Surface::setup_master ()
214 _master_fader = dynamic_cast<Fader*> (Fader::factory (*this, 8, "master", *groups["master"]));
216 boost::shared_ptr<Route> m;
218 if ((m = _mcp.get_session().monitor_out()) == 0) {
219 m = _mcp.get_session().master_out();
226 _master_fader->set_control (m->gain_control());
227 m->gain_control()->Changed.connect (*this, invalidator(), ui_bind (&Surface::master_gain_changed, this), ui_context());
231 Surface::master_gain_changed ()
233 boost::shared_ptr<AutomationControl> ac = _master_fader->control();
234 float pos = ac->internal_to_interface (ac->get_value());
235 _port->write (_master_fader->set_position (pos));
239 Surface::scaled_delta (float delta, float current_speed)
241 /* XXX needs work before use */
242 return (std::pow (float(delta + 1), 2) + current_speed) / 100.0;
246 Surface::display_bank_start (uint32_t current_bank)
248 if (current_bank == 0) {
249 // send Ar. to 2-char display on the master
250 _port->write (two_char_display ("Ar", ".."));
252 // write the current first remote_id to the 2-char display
253 _port->write (two_char_display (current_bank));
258 Surface::blank_jog_ring ()
260 Control* control = controls_by_device_independent_id[Jog::ID];
263 Pot* pot = dynamic_cast<Pot*> (control);
265 _port->write (pot->set_onoff (false));
271 Surface::has_timecode_display () const
277 Surface::scrub_scaling_factor () const
283 Surface::connect_to_signals ()
288 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 connecting to signals on port %2\n",
289 number(), _port->input_port().name()));
291 MIDI::Parser* p = _port->input_port().parser();
294 p->sysex.connect_same_thread (*this, boost::bind (&Surface::handle_midi_sysex, this, _1, _2, _3));
295 /* V-Pot messages are Controller */
296 p->controller.connect_same_thread (*this, boost::bind (&Surface::handle_midi_controller_message, this, _1, _2));
297 /* Button messages are NoteOn */
298 p->note_on.connect_same_thread (*this, boost::bind (&Surface::handle_midi_note_on_message, this, _1, _2));
299 /* Button messages are NoteOn. libmidi++ sends note-on w/velocity = 0 as note-off so catch them too */
300 p->note_off.connect_same_thread (*this, boost::bind (&Surface::handle_midi_note_on_message, this, _1, _2));
301 /* Fader messages are Pitchbend */
302 p->channel_pitchbend[0].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 0U));
303 p->channel_pitchbend[1].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 1U));
304 p->channel_pitchbend[2].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 2U));
305 p->channel_pitchbend[3].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 3U));
306 p->channel_pitchbend[4].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 4U));
307 p->channel_pitchbend[5].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 5U));
308 p->channel_pitchbend[6].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 6U));
309 p->channel_pitchbend[7].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 7U));
316 Surface::handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t pb, uint32_t fader_id)
318 /* Pitchbend messages are fader messages. Nothing in the data we get
319 * from the MIDI::Parser conveys the fader ID, which was given by the
320 * channel ID in the status byte.
322 * Instead, we have used bind() to supply the fader-within-strip ID
323 * when we connected to the per-channel pitchbend events.
326 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handle_midi pitchbend on port %3, fader = %1 value = %2\n",
327 fader_id, pb, _number));
329 Fader* fader = faders[fader_id];
332 Strip* strip = dynamic_cast<Strip*> (&fader->group());
333 float pos = (pb >> 4)/1023.0; // only the top 10 bytes are used
335 strip->handle_fader (*fader, pos);
338 fader->set_value (pos); // alter master gain
339 _port->write (fader->set_position (pos)); // write back value (required for servo)
342 DEBUG_TRACE (DEBUG::MackieControl, "fader not found\n");
347 Surface::handle_midi_note_on_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
349 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("SurfacePort::handle_note_on %1 = %2\n", (int) ev->note_number, (int) ev->velocity));
351 Button* button = buttons[ev->note_number];
354 Strip* strip = dynamic_cast<Strip*> (&button->group());
357 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip %1 button %2 pressed ? %3\n",
358 strip->index(), button->name(), (ev->velocity > 64)));
359 strip->handle_button (*button, ev->velocity > 64 ? press : release);
362 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("global button %1\n", button->id()));
363 _mcp.handle_button_event (*this, *button, ev->velocity > 64 ? press : release);
366 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("no button found for %1\n", ev->note_number));
371 Surface::handle_midi_controller_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
373 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("SurfacePort::handle_midi_controller %1 = %2\n", (int) ev->controller_number, (int) ev->value));
375 Pot* pot = pots[ev->controller_number];
380 // bit 6 gives the sign
381 float sign = (ev->value & 0x40) == 0 ? 1.0 : -1.0;
382 // bits 0..5 give the velocity. we interpret this as "ticks
383 // moved before this message was sent"
384 float ticks = (ev->value & 0x3f);
386 /* euphonix and perhaps other devices send zero
387 when they mean 1, we think.
391 float delta = sign * (ticks / (float) 0x3f);
393 Strip* strip = dynamic_cast<Strip*> (&pot->group());
396 strip->handle_pot (*pot, delta);
398 JogWheel* wheel = dynamic_cast<JogWheel*> (pot);
400 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Jog wheel moved %1\n", state.ticks));
401 wheel->jog_event (*_port, *pot, delta);
403 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("External controller moved %1\n", state.ticks));
404 cout << "external controller" << delta << endl;
408 DEBUG_TRACE (DEBUG::MackieControl, "pot not found\n");
413 Surface::handle_midi_sysex (MIDI::Parser &, MIDI::byte * raw_bytes, size_t count)
415 MidiByteArray bytes (count, raw_bytes);
418 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handle_midi_sysex: %1\n", bytes));
420 /* always save the device type ID so that our outgoing sysex messages
425 mackie_sysex_hdr[3] = bytes[4];
427 mackie_sysex_hdr_xt[3] = bytes[4];
433 LCP: Connection Challenge
435 if (bytes[4] == 0x10 || bytes[4] == 0x11) {
436 write_sysex (host_connection_query (bytes));
442 case 0x03: /* LCP Connection Confirmation */
443 if (bytes[4] == 0x10 || bytes[4] == 0x11) {
444 write_sysex (host_connection_confirmation (bytes));
449 case 0x04: /* LCP: Confirmation Denied */
453 error << "MCP: unknown sysex: " << bytes << endmsg;
458 calculate_challenge_response (MidiByteArray::iterator begin, MidiByteArray::iterator end)
461 back_insert_iterator<MidiByteArray> back (l);
462 copy (begin, end, back);
464 MidiByteArray retval;
466 // this is how to calculate the response to the challenge.
467 // from the Logic docs.
468 retval << (0x7f & (l[0] + (l[1] ^ 0xa) - l[3]));
469 retval << (0x7f & ( (l[2] >> l[3]) ^ (l[0] + l[3])));
470 retval << (0x7f & ((l[3] - (l[2] << 2)) ^ (l[0] | l[1])));
471 retval << (0x7f & (l[1] - l[2] + (0xf0 ^ (l[3] << 4))));
476 // not used right now
478 Surface::host_connection_query (MidiByteArray & bytes)
480 MidiByteArray response;
482 if (bytes[4] != 0x10 && bytes[4] != 0x11) {
483 /* not a Logic Control device - no response required */
487 // handle host connection query
488 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host connection query: %1\n", bytes));
490 if (bytes.size() != 18) {
491 cerr << "expecting 18 bytes, read " << bytes << " from " << _port->input_port().name() << endl;
495 // build and send host connection reply
497 copy (bytes.begin() + 6, bytes.begin() + 6 + 7, back_inserter (response));
498 response << calculate_challenge_response (bytes.begin() + 6 + 7, bytes.begin() + 6 + 7 + 4);
502 // not used right now
504 Surface::host_connection_confirmation (const MidiByteArray & bytes)
506 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host_connection_confirmation: %1\n", bytes));
508 // decode host connection confirmation
509 if (bytes.size() != 14) {
511 os << "expecting 14 bytes, read " << bytes << " from " << _port->input_port().name();
512 throw MackieControlException (os.str());
515 // send version request
516 return MidiByteArray (2, 0x13, 0x00);
520 Surface::handle_port_inactive (SurfacePort * port)
526 Surface::write_sysex (const MidiByteArray & mba)
533 buf << sysex_hdr() << mba << MIDI::eox;
538 Surface::write_sysex (MIDI::byte msg)
541 buf << sysex_hdr() << msg << MIDI::eox;
546 Surface::drop_routes ()
548 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
549 (*s)->set_route (boost::shared_ptr<Route>());
554 Surface::n_strips () const
556 return strips.size();
560 Surface::nth_strip (uint32_t n) const
562 if (n > n_strips()) {
571 // TODO turn off Timecode displays
574 for (Strips::iterator it = strips.begin(); it != strips.end(); ++it) {
575 _port->write ((*it)->zero());
578 // turn off global buttons and leds
579 // global buttons are only ever on mcu_port, so we don't have
580 // to figure out which port.
582 for (Controls::iterator it = controls.begin(); it != controls.end(); ++it) {
583 Control & control = **it;
584 if (!control.group().is_strip()) {
585 _port->write (control.zero());
589 // any hardware-specific stuff
590 // clear 2-char display
591 _port->write (two_char_display (" "));
593 // and the led ring for the master strip
598 Surface::periodic (uint64_t now_usecs)
600 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
601 (*s)->periodic (now_usecs);
607 Surface::write (const MidiByteArray& data)
613 Surface::jog_wheel_state_display (JogWheel::State state)
617 _port->write (two_char_display ("Zm"));
619 case JogWheel::scroll:
620 _port->write (two_char_display ("Sc"));
622 case JogWheel::scrub:
623 _port->write (two_char_display ("Sb"));
625 case JogWheel::shuttle:
626 _port->write (two_char_display ("Sh"));
628 case JogWheel::speed:
629 _port->write (two_char_display ("Sp"));
631 case JogWheel::select:
632 _port->write (two_char_display ("Se"));
638 Surface::map_routes (const vector<boost::shared_ptr<Route> >& routes)
640 vector<boost::shared_ptr<Route> >::const_iterator r;
643 for (s = strips.begin(); s != strips.end(); ++s) {
644 (*s)->set_route (boost::shared_ptr<Route>());
647 for (r = routes.begin(), s = strips.begin(); r != routes.end() && s != strips.end(); ++r, ++s) {
648 (*s)->set_route (*r);
652 static char translate_seven_segment (char achar)
654 achar = toupper (achar);
655 if (achar >= 0x40 && achar <= 0x60)
657 else if (achar >= 0x21 && achar <= 0x3f)
664 Surface::two_char_display (const std::string & msg, const std::string & dots)
667 return MidiByteArray();
670 if (msg.length() != 2) throw MackieControlException ("MackieMidiBuilder::two_char_display: msg must be exactly 2 characters");
671 if (dots.length() != 2) throw MackieControlException ("MackieMidiBuilder::two_char_display: dots must be exactly 2 characters");
673 MidiByteArray bytes (6, 0xb0, 0x4a, 0x00, 0xb0, 0x4b, 0x00);
675 // chars are understood by the surface in right-to-left order
676 // could also exchange the 0x4a and 0x4b, above
677 bytes[5] = translate_seven_segment (msg[0]) + (dots[0] == '.' ? 0x40 : 0x00);
678 bytes[2] = translate_seven_segment (msg[1]) + (dots[1] == '.' ? 0x40 : 0x00);
684 Surface::two_char_display (unsigned int value, const std::string & /*dots*/)
687 os << setfill('0') << setw(2) << value % 100;
688 return two_char_display (os.str());
692 Surface::display_timecode (const std::string & timecode, const std::string & timecode_last)
694 if (has_timecode_display()) {
695 _port->write (timecode_display (timecode, timecode_last));
700 Surface::timecode_display (const std::string & timecode, const std::string & last_timecode)
702 // if there's no change, send nothing, not even sysex header
703 if (timecode == last_timecode) return MidiByteArray();
705 // length sanity checking
706 string local_timecode = timecode;
708 // truncate to 10 characters
709 if (local_timecode.length() > 10) {
710 local_timecode = local_timecode.substr (0, 10);
713 // pad to 10 characters
714 while (local_timecode.length() < 10) {
715 local_timecode += " ";
718 // find the suffix of local_timecode that differs from last_timecode
719 std::pair<string::const_iterator,string::iterator> pp = mismatch (last_timecode.begin(), last_timecode.end(), local_timecode.begin());
721 MidiByteArray retval;
724 retval << sysex_hdr();
726 // code for timecode display
729 // translate characters. These are sent in reverse order of display
730 // hence the reverse iterators
731 string::reverse_iterator rend = reverse_iterator<string::iterator> (pp.second);
732 for (string::reverse_iterator it = local_timecode.rbegin(); it != rend; ++it) {
733 retval << translate_seven_segment (*it);
743 Surface::update_flip_mode_display ()
745 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
746 (*s)->flip_mode_changed (true);
751 Surface::update_view_mode_display ()
756 switch (_mcp.view_mode()) {
757 case MackieControlProtocol::Mixer:
758 _port->write (two_char_display ("Mx"));
759 button = buttons[Button::Pan];
761 case MackieControlProtocol::Dynamics:
762 _port->write (two_char_display ("Dy"));
763 button = buttons[Button::Dyn];
765 case MackieControlProtocol::EQ:
766 _port->write (two_char_display ("EQ"));
767 button = buttons[Button::Eq];
769 case MackieControlProtocol::Loop:
770 _port->write (two_char_display ("LP"));
771 button = buttons[Button::Loop];
773 case MackieControlProtocol::AudioTracks:
774 _port->write (two_char_display ("AT"));
776 case MackieControlProtocol::MidiTracks:
777 _port->write (two_char_display ("MT"));
779 case MackieControlProtocol::Busses:
780 _port->write (two_char_display ("Bs"));
782 case MackieControlProtocol::Sends:
783 _port->write (two_char_display ("Sn"));
784 button = buttons[Button::Sends];
786 case MackieControlProtocol::Plugins:
787 _port->write (two_char_display ("Pl"));
788 button = buttons[Button::Plugin];
793 _port->write (button->set_state (on));
797 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
798 _port->write ((*s)->display (1, text));
804 Surface::gui_selection_changed (ARDOUR::RouteNotificationListPtr routes)
806 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
807 _port->write ((*s)->gui_selection_changed (routes));