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"
17 #include "ardour/utils.h"
19 #include "control_group.h"
20 #include "surface_port.h"
23 #include "mackie_control_protocol.h"
24 #include "jog_wheel.h"
38 using namespace Mackie;
41 using ARDOUR::Pannable;
42 using ARDOUR::AutomationControl;
44 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
46 // The MCU sysex header.4th byte Will be overwritten
47 // when we get an incoming sysex that identifies
49 static MidiByteArray mackie_sysex_hdr (5, MIDI::sysex, 0x0, 0x0, 0x66, 0x14);
51 // The MCU extender sysex header.4th byte Will be overwritten
52 // when we get an incoming sysex that identifies
54 static MidiByteArray mackie_sysex_hdr_xt (5, MIDI::sysex, 0x0, 0x0, 0x66, 0x15);
56 static MidiByteArray empty_midi_byte_array;
58 Surface::Surface (MackieControlProtocol& mcp, const std::string& device_name, uint32_t number, surface_type_t stype)
67 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init\n");
69 _port = new SurfacePort (*this);
71 /* only the first Surface object has global controls */
74 if (_mcp.device_info().has_global_controls()) {
78 if (_mcp.device_info().has_master_fader()) {
83 uint32_t n = _mcp.device_info().strip_cnt();
89 connect_to_signals ();
91 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init finish\n");
96 DEBUG_TRACE (DEBUG::MackieControl, "Surface: destructor\n");
101 for (Groups::iterator it = groups.begin(); it != groups.end(); ++it) {
106 for (Controls::iterator it = controls.begin(); it != controls.end(); ++it) {
115 Surface::sysex_hdr() const
118 case mcu: return mackie_sysex_hdr;
119 case ext: return mackie_sysex_hdr_xt;
121 cout << "SurfacePort::sysex_hdr _port_type not known" << endl;
122 return mackie_sysex_hdr;
125 static GlobalControlDefinition mackie_global_controls[] = {
126 { "external", Pot::External, Pot::factory, "none" },
127 { "fader_touch", Led::FaderTouch, Led::factory, "master" },
128 { "timecode", Led::Timecode, Led::factory, "none" },
129 { "beats", Led::Beats, Led::factory, "none" },
130 { "solo", Led::RudeSolo, Led::factory, "none" },
131 { "relay_click", Led::RelayClick, Led::factory, "none" },
132 { "", 0, Led::factory, "" }
136 Surface::init_controls()
140 groups["assignment"] = new Group ("assignment");
141 groups["automation"] = new Group ("automation");
142 groups["bank"] = new Group ("bank");
143 groups["cursor"] = new Group ("cursor");
144 groups["display"] = new Group ("display");
145 groups["functions"] = new Group ("functions");
146 groups["modifiers"] = new Group ("modifiers");
147 groups["none"] = new Group ("none");
148 groups["transport"] = new Group ("transport");
149 groups["user"] = new Group ("user");
150 groups["master"] = new Group ("master");
151 groups["view"] = new Group ("view");
153 if (_mcp.device_info().has_jog_wheel()) {
154 _jog_wheel = new Mackie::JogWheel (_mcp);
157 for (uint32_t n = 0; mackie_global_controls[n].name[0]; ++n) {
158 group = groups[mackie_global_controls[n].group_name];
159 Control* control = mackie_global_controls[n].factory (*this, mackie_global_controls[n].id, mackie_global_controls[n].name, *group);
160 controls_by_device_independent_id[mackie_global_controls[n].id] = control;
163 /* add global buttons */
165 const map<Button::ID,GlobalButtonInfo>& global_buttons (_mcp.device_info().global_buttons());
167 for (map<Button::ID,GlobalButtonInfo>::const_iterator b = global_buttons.begin(); b != global_buttons.end(); ++b){
168 group = groups[b->second.group];
169 controls_by_device_independent_id[b->first] = Button::factory (*this, b->first, b->second.id, b->second.label, *group);
174 Surface::init_strips (uint32_t n)
176 const map<Button::ID,StripButtonInfo>& strip_buttons (_mcp.device_info().strip_buttons());
178 for (uint32_t i = 0; i < n; ++i) {
182 snprintf (name, sizeof (name), "strip_%d", (8* _number) + i);
184 Strip* strip = new Strip (*this, name, i, strip_buttons);
186 groups[name] = strip;
187 strips.push_back (strip);
192 Surface::setup_master ()
194 _master_fader = dynamic_cast<Fader*> (Fader::factory (*this, 8, "master", *groups["master"]));
196 boost::shared_ptr<Route> m;
198 if ((m = _mcp.get_session().monitor_out()) == 0) {
199 m = _mcp.get_session().master_out();
206 _master_fader->set_control (m->gain_control());
207 m->gain_control()->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&Surface::master_gain_changed, this), ui_context());
211 Surface::master_gain_changed ()
213 boost::shared_ptr<AutomationControl> ac = _master_fader->control();
214 float pos = ac->internal_to_interface (ac->get_value());
215 _port->write (_master_fader->set_position (pos));
219 Surface::scaled_delta (float delta, float current_speed)
221 /* XXX needs work before use */
222 const float sign = delta < 0.0 ? -1.0 : 1.0;
224 return ((sign * std::pow (delta + 1.0, 2.0)) + current_speed) / 100.0;
228 Surface::display_bank_start (uint32_t current_bank)
230 if (current_bank == 0) {
231 // send Ar. to 2-char display on the master
232 show_two_char_display ("Ar", "..");
234 // write the current first remote_id to the 2-char display
235 show_two_char_display (current_bank);
240 Surface::blank_jog_ring ()
242 Control* control = controls_by_device_independent_id[Jog::ID];
245 Pot* pot = dynamic_cast<Pot*> (control);
247 _port->write (pot->set (0.0, false, Pot::spread));
253 Surface::scrub_scaling_factor () const
259 Surface::connect_to_signals ()
264 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 connecting to signals on port %2\n",
265 number(), _port->input_port().name()));
267 MIDI::Parser* p = _port->input_port().parser();
270 p->sysex.connect_same_thread (*this, boost::bind (&Surface::handle_midi_sysex, this, _1, _2, _3));
271 /* V-Pot messages are Controller */
272 p->controller.connect_same_thread (*this, boost::bind (&Surface::handle_midi_controller_message, this, _1, _2));
273 /* Button messages are NoteOn */
274 p->note_on.connect_same_thread (*this, boost::bind (&Surface::handle_midi_note_on_message, this, _1, _2));
275 /* Button messages are NoteOn. libmidi++ sends note-on w/velocity = 0 as note-off so catch them too */
276 p->note_off.connect_same_thread (*this, boost::bind (&Surface::handle_midi_note_on_message, this, _1, _2));
277 /* Fader messages are Pitchbend */
278 p->channel_pitchbend[0].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 0U));
279 p->channel_pitchbend[1].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 1U));
280 p->channel_pitchbend[2].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 2U));
281 p->channel_pitchbend[3].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 3U));
282 p->channel_pitchbend[4].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 4U));
283 p->channel_pitchbend[5].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 5U));
284 p->channel_pitchbend[6].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 6U));
285 p->channel_pitchbend[7].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, 7U));
292 Surface::handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t pb, uint32_t fader_id)
294 /* Pitchbend messages are fader messages. Nothing in the data we get
295 * from the MIDI::Parser conveys the fader ID, which was given by the
296 * channel ID in the status byte.
298 * Instead, we have used bind() to supply the fader-within-strip ID
299 * when we connected to the per-channel pitchbend events.
302 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handle_midi pitchbend on port %3, fader = %1 value = %2\n",
303 fader_id, pb, _number));
305 Fader* fader = faders[fader_id];
308 Strip* strip = dynamic_cast<Strip*> (&fader->group());
309 float pos = (pb >> 4)/1023.0; // only the top 10 bytes are used
311 strip->handle_fader (*fader, pos);
314 fader->set_value (pos); // alter master gain
315 _port->write (fader->set_position (pos)); // write back value (required for servo)
318 DEBUG_TRACE (DEBUG::MackieControl, "fader not found\n");
323 Surface::handle_midi_note_on_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
325 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("SurfacePort::handle_note_on %1 = %2\n", (int) ev->note_number, (int) ev->velocity));
327 Button* button = buttons[ev->note_number];
330 Strip* strip = dynamic_cast<Strip*> (&button->group());
333 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip %1 button %2 pressed ? %3\n",
334 strip->index(), button->name(), (ev->velocity > 64)));
335 strip->handle_button (*button, ev->velocity > 64 ? press : release);
338 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("global button %1\n", button->id()));
339 _mcp.handle_button_event (*this, *button, ev->velocity > 64 ? press : release);
342 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("no button found for %1\n", ev->note_number));
347 Surface::handle_midi_controller_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
349 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("SurfacePort::handle_midi_controller %1 = %2\n", (int) ev->controller_number, (int) ev->value));
351 Pot* pot = pots[ev->controller_number];
353 // bit 6 gives the sign
354 float sign = (ev->value & 0x40) == 0 ? 1.0 : -1.0;
355 // bits 0..5 give the velocity. we interpret this as "ticks
356 // moved before this message was sent"
357 float ticks = (ev->value & 0x3f);
359 /* euphonix and perhaps other devices send zero
360 when they mean 1, we think.
364 float delta = sign * (ticks / (float) 0x3f);
367 if (ev->controller_number == Jog::ID && _jog_wheel) {
369 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Jog wheel moved %1\n", ticks));
370 _jog_wheel->jog_event (delta);
377 Strip* strip = dynamic_cast<Strip*> (&pot->group());
379 strip->handle_pot (*pot, delta);
384 Surface::handle_midi_sysex (MIDI::Parser &, MIDI::byte * raw_bytes, size_t count)
386 MidiByteArray bytes (count, raw_bytes);
388 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handle_midi_sysex: %1\n", bytes));
390 /* always save the device type ID so that our outgoing sysex messages
395 mackie_sysex_hdr[3] = bytes[4];
397 mackie_sysex_hdr_xt[3] = bytes[4];
403 LCP: Connection Challenge
405 if (bytes[4] == 0x10 || bytes[4] == 0x11) {
406 write_sysex (host_connection_query (bytes));
411 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
414 update_view_mode_display ();
419 case 0x03: /* LCP Connection Confirmation */
420 if (bytes[4] == 0x10 || bytes[4] == 0x11) {
421 write_sysex (host_connection_confirmation (bytes));
426 case 0x04: /* LCP: Confirmation Denied */
430 error << "MCP: unknown sysex: " << bytes << endmsg;
435 calculate_challenge_response (MidiByteArray::iterator begin, MidiByteArray::iterator end)
438 back_insert_iterator<MidiByteArray> back (l);
439 copy (begin, end, back);
441 MidiByteArray retval;
443 // this is how to calculate the response to the challenge.
444 // from the Logic docs.
445 retval << (0x7f & (l[0] + (l[1] ^ 0xa) - l[3]));
446 retval << (0x7f & ( (l[2] >> l[3]) ^ (l[0] + l[3])));
447 retval << (0x7f & ((l[3] - (l[2] << 2)) ^ (l[0] | l[1])));
448 retval << (0x7f & (l[1] - l[2] + (0xf0 ^ (l[3] << 4))));
453 // not used right now
455 Surface::host_connection_query (MidiByteArray & bytes)
457 MidiByteArray response;
459 if (bytes[4] != 0x10 && bytes[4] != 0x11) {
460 /* not a Logic Control device - no response required */
464 // handle host connection query
465 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host connection query: %1\n", bytes));
467 if (bytes.size() != 18) {
468 cerr << "expecting 18 bytes, read " << bytes << " from " << _port->input_port().name() << endl;
472 // build and send host connection reply
474 copy (bytes.begin() + 6, bytes.begin() + 6 + 7, back_inserter (response));
475 response << calculate_challenge_response (bytes.begin() + 6 + 7, bytes.begin() + 6 + 7 + 4);
479 // not used right now
481 Surface::host_connection_confirmation (const MidiByteArray & bytes)
483 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host_connection_confirmation: %1\n", bytes));
485 // decode host connection confirmation
486 if (bytes.size() != 14) {
488 os << "expecting 14 bytes, read " << bytes << " from " << _port->input_port().name();
489 throw MackieControlException (os.str());
492 // send version request
493 return MidiByteArray (2, 0x13, 0x00);
497 Surface::handle_port_inactive (SurfacePort*)
503 Surface::write_sysex (const MidiByteArray & mba)
510 buf << sysex_hdr() << mba << MIDI::eox;
515 Surface::write_sysex (MIDI::byte msg)
518 buf << sysex_hdr() << msg << MIDI::eox;
523 Surface::n_strips (bool with_locked_strips) const
525 if (with_locked_strips) {
526 return strips.size();
531 for (Strips::const_iterator it = strips.begin(); it != strips.end(); ++it) {
532 if (!(*it)->locked()) {
540 Surface::nth_strip (uint32_t n) const
542 if (n > n_strips()) {
551 // TODO turn off Timecode displays
554 for (Strips::iterator it = strips.begin(); it != strips.end(); ++it) {
562 Surface::zero_controls ()
564 if (_stype != mcu || !_mcp.device_info().has_global_controls()) {
568 // turn off global buttons and leds
569 // global buttons are only ever on mcu_port, so we don't have
570 // to figure out which port.
572 for (Controls::iterator it = controls.begin(); it != controls.end(); ++it) {
573 Control & control = **it;
574 if (!control.group().is_strip()) {
575 _port->write (control.zero());
579 if (_number == 0 && _mcp.device_info().has_two_character_display()) {
580 // any hardware-specific stuff
581 // clear 2-char display
582 show_two_char_display (" ");
585 // and the led ring for the master strip
590 Surface::periodic (uint64_t now_usecs)
592 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
593 (*s)->periodic (now_usecs);
598 Surface::write (const MidiByteArray& data)
606 Surface::map_routes (const vector<boost::shared_ptr<Route> >& routes)
608 vector<boost::shared_ptr<Route> >::const_iterator r;
609 Strips::iterator s = strips.begin();
611 for (r = routes.begin(); r != routes.end() && s != strips.end(); ++s) {
613 /* don't try to assign routes to a locked strip. it won't
614 use it anyway, but if we do, then we get out of sync
615 with the proposed mapping.
618 if (!(*s)->locked()) {
619 (*s)->set_route (*r);
624 for (; s != strips.end(); ++s) {
625 (*s)->set_route (boost::shared_ptr<Route>());
632 translate_seven_segment (char achar)
634 achar = toupper (achar);
636 if (achar >= 0x40 && achar <= 0x60) {
638 } else if (achar >= 0x21 && achar <= 0x3f) {
646 Surface::show_two_char_display (const std::string & msg, const std::string & dots)
648 if (_stype != mcu || !_mcp.device_info().has_two_character_display() || msg.length() != 2 || dots.length() != 2) {
652 MidiByteArray right (3, 0xb0, 0x4b, 0x00);
653 MidiByteArray left (3, 0xb0, 0x4a, 0x00);
655 right[2] = translate_seven_segment (msg[0]) + (dots[0] == '.' ? 0x40 : 0x00);
656 left[2] = translate_seven_segment (msg[1]) + (dots[1] == '.' ? 0x40 : 0x00);
658 _port->write (right);
663 Surface::show_two_char_display (unsigned int value, const std::string & /*dots*/)
666 os << setfill('0') << setw(2) << value % 100;
667 show_two_char_display (os.str());
671 Surface::display_timecode (const std::string & timecode, const std::string & timecode_last)
673 if (_active && _mcp.device_info().has_timecode_display()) {
674 _port->write (timecode_display (timecode, timecode_last));
679 Surface::timecode_display (const std::string & timecode, const std::string & last_timecode)
681 // if there's no change, send nothing, not even sysex header
682 if (timecode == last_timecode) return MidiByteArray();
684 // length sanity checking
685 string local_timecode = timecode;
687 // truncate to 10 characters
688 if (local_timecode.length() > 10) {
689 local_timecode = local_timecode.substr (0, 10);
692 // pad to 10 characters
693 while (local_timecode.length() < 10) {
694 local_timecode += " ";
697 // find the suffix of local_timecode that differs from last_timecode
698 std::pair<string::const_iterator,string::iterator> pp = mismatch (last_timecode.begin(), last_timecode.end(), local_timecode.begin());
700 MidiByteArray retval;
703 retval << sysex_hdr();
705 // code for timecode display
708 // translate characters. These are sent in reverse order of display
709 // hence the reverse iterators
710 string::reverse_iterator rend = reverse_iterator<string::iterator> (pp.second);
711 for (string::reverse_iterator it = local_timecode.rbegin(); it != rend; ++it) {
712 retval << translate_seven_segment (*it);
722 Surface::update_flip_mode_display ()
724 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
725 (*s)->flip_mode_changed (true);
730 Surface::update_view_mode_display ()
739 switch (_mcp.view_mode()) {
740 case MackieControlProtocol::Mixer:
741 show_two_char_display ("Mx");
744 case MackieControlProtocol::Dynamics:
745 show_two_char_display ("Dy");
748 case MackieControlProtocol::EQ:
749 show_two_char_display ("EQ");
752 case MackieControlProtocol::Loop:
753 show_two_char_display ("LP");
756 case MackieControlProtocol::AudioTracks:
757 show_two_char_display ("AT");
759 case MackieControlProtocol::MidiTracks:
760 show_two_char_display ("MT");
762 case MackieControlProtocol::Sends:
763 show_two_char_display ("Sn");
766 case MackieControlProtocol::Plugins:
767 show_two_char_display ("Pl");
776 /* we are attempting to turn a global button/LED on */
778 map<int,Control*>::iterator x = controls_by_device_independent_id.find (id);
780 if (x != controls_by_device_independent_id.end()) {
781 Button* button = dynamic_cast<Button*> (x->second);
783 _port->write (button->set_state (on));
789 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
790 _port->write ((*s)->display (1, text));
796 Surface::gui_selection_changed (const ARDOUR::StrongRouteNotificationList& routes)
798 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
799 (*s)->gui_selection_changed (routes);
804 Surface::say_hello ()
806 cerr << "Sending hello to port " << _port->output_port().name() << endl;
808 /* wakeup for Mackie Control */
809 MidiByteArray wakeup (7, MIDI::sysex, 0x00, 0x00, 0x66, 0x14, 0x00, MIDI::eox);
810 _port->write (wakeup);
811 wakeup[4] = 0x15; /* wakup Mackie XT */
812 _port->write (wakeup);
813 wakeup[4] = 0x10; /* wakupe Logic Control */
814 _port->write (wakeup);
815 wakeup[4] = 0x11; /* wakeup Logic Control XT */
816 _port->write (wakeup);
820 Surface::next_jog_mode ()
825 Surface::set_jog_mode (JogWheel::Mode)
830 Surface::route_is_locked_to_strip (boost::shared_ptr<Route> r) const
832 for (Strips::const_iterator s = strips.begin(); s != strips.end(); ++s) {
833 if ((*s)->route() == r && (*s)->locked()) {