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)
66 , _last_master_gain_written (-0.0f)
68 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init\n");
70 _port = new SurfacePort (*this);
72 /* only the first Surface object has global controls */
75 DEBUG_TRACE (DEBUG::MackieControl, "Surface is first. Might have global controls.\n");
76 if (_mcp.device_info().has_global_controls()) {
78 DEBUG_TRACE (DEBUG::MackieControl, "init_controls done\n");
81 if (_mcp.device_info().has_master_fader()) {
83 DEBUG_TRACE (DEBUG::MackieControl, "setup_master done\n");
87 uint32_t n = _mcp.device_info().strip_cnt();
91 DEBUG_TRACE (DEBUG::MackieControl, "init_strips done\n");
94 connect_to_signals ();
96 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init finish\n");
101 DEBUG_TRACE (DEBUG::MackieControl, "Surface: destructor\n");
106 for (Groups::iterator it = groups.begin(); it != groups.end(); ++it) {
111 for (Controls::iterator it = controls.begin(); it != controls.end(); ++it) {
120 Surface::sysex_hdr() const
123 case mcu: return mackie_sysex_hdr;
124 case ext: return mackie_sysex_hdr_xt;
126 cout << "SurfacePort::sysex_hdr _port_type not known" << endl;
127 return mackie_sysex_hdr;
130 static GlobalControlDefinition mackie_global_controls[] = {
131 { "external", Pot::External, Pot::factory, "none" },
132 { "fader_touch", Led::FaderTouch, Led::factory, "master" },
133 { "timecode", Led::Timecode, Led::factory, "none" },
134 { "beats", Led::Beats, Led::factory, "none" },
135 { "solo", Led::RudeSolo, Led::factory, "none" },
136 { "relay_click", Led::RelayClick, Led::factory, "none" },
137 { "", 0, Led::factory, "" }
141 Surface::init_controls()
145 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init_controls: creating groups\n");
146 groups["assignment"] = new Group ("assignment");
147 groups["automation"] = new Group ("automation");
148 groups["bank"] = new Group ("bank");
149 groups["cursor"] = new Group ("cursor");
150 groups["display"] = new Group ("display");
151 groups["function select"] = new Group ("function select");
152 groups["global view"] = new Group ("global view");
153 groups["master"] = new Group ("master");
154 groups["modifiers"] = new Group ("modifiers");
155 groups["none"] = new Group ("none");
156 groups["transport"] = new Group ("transport");
157 groups["user"] = new Group ("user");
158 groups["utilities"] = new Group ("utilities");
160 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init_controls: creating jog wheel\n");
161 if (_mcp.device_info().has_jog_wheel()) {
162 _jog_wheel = new Mackie::JogWheel (_mcp);
165 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init_controls: creating global controls\n");
166 for (uint32_t n = 0; mackie_global_controls[n].name[0]; ++n) {
167 group = groups[mackie_global_controls[n].group_name];
168 Control* control = mackie_global_controls[n].factory (*this, mackie_global_controls[n].id, mackie_global_controls[n].name, *group);
169 controls_by_device_independent_id[mackie_global_controls[n].id] = control;
172 /* add global buttons */
173 DEBUG_TRACE (DEBUG::MackieControl, "Surface::init_controls: adding global buttons\n");
174 const map<Button::ID,GlobalButtonInfo>& global_buttons (_mcp.device_info().global_buttons());
176 for (map<Button::ID,GlobalButtonInfo>::const_iterator b = global_buttons.begin(); b != global_buttons.end(); ++b){
177 group = groups[b->second.group];
178 controls_by_device_independent_id[b->first] = Button::factory (*this, b->first, b->second.id, b->second.label, *group);
183 Surface::init_strips (uint32_t n)
185 const map<Button::ID,StripButtonInfo>& strip_buttons (_mcp.device_info().strip_buttons());
187 for (uint32_t i = 0; i < n; ++i) {
191 snprintf (name, sizeof (name), "strip_%d", (8* _number) + i);
193 Strip* strip = new Strip (*this, name, i, strip_buttons);
195 groups[name] = strip;
196 strips.push_back (strip);
201 Surface::setup_master ()
203 _master_fader = dynamic_cast<Fader*> (Fader::factory (*this, _mcp.device_info().strip_cnt(), "master", *groups["master"]));
205 boost::shared_ptr<Route> m;
207 if ((m = _mcp.get_session().monitor_out()) == 0) {
208 m = _mcp.get_session().master_out();
215 _master_fader->set_control (m->gain_control());
216 m->gain_control()->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&Surface::master_gain_changed, this), ui_context());
218 Groups::iterator group_it;
219 group_it = groups.find("master");
221 DeviceInfo device_info = _mcp.device_info();
222 GlobalButtonInfo master_button = device_info.get_global_button(Button::MasterFaderTouch);
223 Button* bb = dynamic_cast<Button*> (Button::factory (
225 Button::MasterFaderTouch,
230 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("surface %1 Master Fader new button BID %2 id %3\n",
231 number(), Button::MasterFaderTouch, bb->id()));
235 Surface::master_gain_changed ()
237 if (!_master_fader) {
241 boost::shared_ptr<AutomationControl> ac = _master_fader->control();
243 float normalized_position = ac->internal_to_interface (ac->get_value());
244 if (normalized_position == _last_master_gain_written) {
248 DEBUG_TRACE (DEBUG::MackieControl, "Surface::master_gain_changed: updating surface master fader\n");
250 _port->write (_master_fader->set_position (normalized_position));
251 _last_master_gain_written = normalized_position;
255 Surface::scaled_delta (float delta, float current_speed)
257 /* XXX needs work before use */
258 const float sign = delta < 0.0 ? -1.0 : 1.0;
260 return ((sign * std::pow (delta + 1.0, 2.0)) + current_speed) / 100.0;
264 Surface::display_bank_start (uint32_t current_bank)
266 if (current_bank == 0) {
267 // send Ar. to 2-char display on the master
268 show_two_char_display ("Ar", "..");
270 // write the current first remote_id to the 2-char display
271 show_two_char_display (current_bank);
276 Surface::blank_jog_ring ()
278 Control* control = controls_by_device_independent_id[Jog::ID];
281 Pot* pot = dynamic_cast<Pot*> (control);
283 _port->write (pot->set (0.0, false, Pot::spread));
289 Surface::scrub_scaling_factor () const
295 Surface::connect_to_signals ()
300 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 connecting to signals on port %2\n",
301 number(), _port->input_port().name()));
303 MIDI::Parser* p = _port->input_port().parser();
306 p->sysex.connect_same_thread (*this, boost::bind (&Surface::handle_midi_sysex, this, _1, _2, _3));
307 /* V-Pot messages are Controller */
308 p->controller.connect_same_thread (*this, boost::bind (&Surface::handle_midi_controller_message, this, _1, _2));
309 /* Button messages are NoteOn */
310 p->note_on.connect_same_thread (*this, boost::bind (&Surface::handle_midi_note_on_message, this, _1, _2));
311 /* Button messages are NoteOn. libmidi++ sends note-on w/velocity = 0 as note-off so catch them too */
312 p->note_off.connect_same_thread (*this, boost::bind (&Surface::handle_midi_note_on_message, this, _1, _2));
313 /* Fader messages are Pitchbend */
315 for (i = 0; i < _mcp.device_info().strip_cnt(); i++) {
316 p->channel_pitchbend[i].connect_same_thread (*this, boost::bind (&Surface::handle_midi_pitchbend_message, this, _1, _2, i));
319 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()));
326 Surface::handle_midi_pitchbend_message (MIDI::Parser&, MIDI::pitchbend_t pb, uint32_t fader_id)
328 /* Pitchbend messages are fader messages. Nothing in the data we get
329 * from the MIDI::Parser conveys the fader ID, which was given by the
330 * channel ID in the status byte.
332 * Instead, we have used bind() to supply the fader-within-strip ID
333 * when we connected to the per-channel pitchbend events.
337 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface::handle_midi_pitchbend_message on port %3, fader = %1 value = %2\n",
338 fader_id, pb, _number));
340 if (_mcp.device_info().no_handshake()) {
344 Fader* fader = faders[fader_id];
347 Strip* strip = dynamic_cast<Strip*> (&fader->group());
348 float pos = (pb >> 4)/1023.0; // only the top 10 bytes are used
350 strip->handle_fader (*fader, pos);
352 DEBUG_TRACE (DEBUG::MackieControl, "Handling master fader\n");
354 fader->set_value (pos); // alter master gain
355 _port->write (fader->set_position (pos)); // write back value (required for servo)
358 DEBUG_TRACE (DEBUG::MackieControl, "fader not found\n");
363 Surface::handle_midi_note_on_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
365 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface::handle_midi_note_on_message %1 = %2\n", (int) ev->note_number, (int) ev->velocity));
367 if (_mcp.device_info().no_handshake()) {
371 Button* button = buttons[ev->note_number];
374 Strip* strip = dynamic_cast<Strip*> (&button->group());
377 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip %1 button %2 pressed ? %3\n",
378 strip->index(), button->name(), (ev->velocity > 64)));
379 strip->handle_button (*button, ev->velocity > 64 ? press : release);
382 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("global button %1\n", button->id()));
383 _mcp.handle_button_event (*this, *button, ev->velocity > 64 ? press : release);
386 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("no button found for %1\n", (int) ev->note_number));
391 Surface::handle_midi_controller_message (MIDI::Parser &, MIDI::EventTwoBytes* ev)
393 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("SurfacePort::handle_midi_controller %1 = %2\n", (int) ev->controller_number, (int) ev->value));
395 if (_mcp.device_info().no_handshake()) {
399 Pot* pot = pots[ev->controller_number];
401 // bit 6 gives the sign
402 float sign = (ev->value & 0x40) == 0 ? 1.0 : -1.0;
403 // bits 0..5 give the velocity. we interpret this as "ticks
404 // moved before this message was sent"
405 float ticks = (ev->value & 0x3f);
407 /* euphonix and perhaps other devices send zero
408 when they mean 1, we think.
412 float delta = sign * (ticks / (float) 0x3f);
415 if (ev->controller_number == Jog::ID && _jog_wheel) {
417 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Jog wheel moved %1\n", ticks));
418 _jog_wheel->jog_event (delta);
425 Strip* strip = dynamic_cast<Strip*> (&pot->group());
427 strip->handle_pot (*pot, delta);
432 Surface::handle_midi_sysex (MIDI::Parser &, MIDI::byte * raw_bytes, size_t count)
434 MidiByteArray bytes (count, raw_bytes);
436 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handle_midi_sysex: %1\n", bytes));
438 if (_mcp.device_info().no_handshake()) {
442 /* always save the device type ID so that our outgoing sysex messages
447 mackie_sysex_hdr[4] = bytes[4];
449 mackie_sysex_hdr_xt[4] = bytes[4];
455 LCP: Connection Challenge
457 if (bytes[4] == 0x10 || bytes[4] == 0x11) {
458 write_sysex (host_connection_query (bytes));
466 case 0x03: /* LCP Connection Confirmation */
467 if (bytes[4] == 0x10 || bytes[4] == 0x11) {
468 write_sysex (host_connection_confirmation (bytes));
473 case 0x04: /* LCP: Confirmation Denied */
477 error << "MCP: unknown sysex: " << bytes << endmsg;
482 calculate_challenge_response (MidiByteArray::iterator begin, MidiByteArray::iterator end)
485 back_insert_iterator<MidiByteArray> back (l);
486 copy (begin, end, back);
488 MidiByteArray retval;
490 // this is how to calculate the response to the challenge.
491 // from the Logic docs.
492 retval << (0x7f & (l[0] + (l[1] ^ 0xa) - l[3]));
493 retval << (0x7f & ( (l[2] >> l[3]) ^ (l[0] + l[3])));
494 retval << (0x7f & ((l[3] - (l[2] << 2)) ^ (l[0] | l[1])));
495 retval << (0x7f & (l[1] - l[2] + (0xf0 ^ (l[3] << 4))));
500 // not used right now
502 Surface::host_connection_query (MidiByteArray & bytes)
504 MidiByteArray response;
506 if (bytes[4] != 0x10 && bytes[4] != 0x11) {
507 /* not a Logic Control device - no response required */
511 // handle host connection query
512 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host connection query: %1\n", bytes));
514 if (bytes.size() != 18) {
515 cerr << "expecting 18 bytes, read " << bytes << " from " << _port->input_port().name() << endl;
519 // build and send host connection reply
521 copy (bytes.begin() + 6, bytes.begin() + 6 + 7, back_inserter (response));
522 response << calculate_challenge_response (bytes.begin() + 6 + 7, bytes.begin() + 6 + 7 + 4);
526 // not used right now
528 Surface::host_connection_confirmation (const MidiByteArray & bytes)
530 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host_connection_confirmation: %1\n", bytes));
532 // decode host connection confirmation
533 if (bytes.size() != 14) {
535 os << "expecting 14 bytes, read " << bytes << " from " << _port->input_port().name();
536 throw MackieControlException (os.str());
539 // send version request
540 return MidiByteArray (2, 0x13, 0x00);
544 Surface::turn_it_on ()
552 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
556 update_view_mode_display ();
558 if (_mcp.device_info ().has_global_controls ()) {
559 _mcp.update_global_button (Button::Read, _mcp.metering_active ());
564 Surface::handle_port_inactive (SurfacePort*)
570 Surface::write_sysex (const MidiByteArray & mba)
577 buf << sysex_hdr() << mba << MIDI::eox;
582 Surface::write_sysex (MIDI::byte msg)
585 buf << sysex_hdr() << msg << MIDI::eox;
590 Surface::n_strips (bool with_locked_strips) const
592 if (with_locked_strips) {
593 return strips.size();
598 for (Strips::const_iterator it = strips.begin(); it != strips.end(); ++it) {
599 if (!(*it)->locked()) {
607 Surface::nth_strip (uint32_t n) const
609 if (n > n_strips()) {
618 if (_mcp.device_info().has_timecode_display ()) {
619 display_timecode (string (10, '0'), string (10, ' '));
622 if (_mcp.device_info().has_two_character_display()) {
623 show_two_char_display (string (2, '0'), string (2, ' '));
626 if (_mcp.device_info().has_master_fader ()) {
627 _port->write (_master_fader->zero ());
631 for (Strips::iterator it = strips.begin(); it != strips.end(); ++it) {
639 Surface::zero_controls ()
641 if (!_mcp.device_info().has_global_controls()) {
645 // turn off global buttons and leds
646 // global buttons are only ever on mcu_port, so we don't have
647 // to figure out which port.
649 for (Controls::iterator it = controls.begin(); it != controls.end(); ++it) {
650 Control & control = **it;
651 if (!control.group().is_strip()) {
652 _port->write (control.zero());
656 // and the led ring for the master strip
659 _last_master_gain_written = 0.0f;
663 Surface::periodic (uint64_t now_usecs)
665 master_gain_changed();
666 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
667 (*s)->periodic (now_usecs);
672 Surface::write (const MidiByteArray& data)
680 Surface::map_routes (const vector<boost::shared_ptr<Route> >& routes)
682 vector<boost::shared_ptr<Route> >::const_iterator r;
683 Strips::iterator s = strips.begin();
685 for (r = routes.begin(); r != routes.end() && s != strips.end(); ++s) {
687 /* don't try to assign routes to a locked strip. it won't
688 use it anyway, but if we do, then we get out of sync
689 with the proposed mapping.
692 if (!(*s)->locked()) {
693 (*s)->set_route (*r);
698 for (; s != strips.end(); ++s) {
699 (*s)->set_route (boost::shared_ptr<Route>());
706 translate_seven_segment (char achar)
708 achar = toupper (achar);
710 if (achar >= 0x40 && achar <= 0x60) {
712 } else if (achar >= 0x21 && achar <= 0x3f) {
720 Surface::show_two_char_display (const std::string & msg, const std::string & dots)
722 if (_stype != mcu || !_mcp.device_info().has_two_character_display() || msg.length() != 2 || dots.length() != 2) {
726 MidiByteArray right (3, 0xb0, 0x4b, 0x00);
727 MidiByteArray left (3, 0xb0, 0x4a, 0x00);
729 right[2] = translate_seven_segment (msg[0]) + (dots[0] == '.' ? 0x40 : 0x00);
730 left[2] = translate_seven_segment (msg[1]) + (dots[1] == '.' ? 0x40 : 0x00);
732 _port->write (right);
737 Surface::show_two_char_display (unsigned int value, const std::string & /*dots*/)
740 os << setfill('0') << setw(2) << value % 100;
741 show_two_char_display (os.str());
745 Surface::display_timecode (const std::string & timecode, const std::string & last_timecode)
747 if (!_active || !_mcp.device_info().has_timecode_display()) {
750 // if there's no change, send nothing, not even sysex header
751 if (timecode == last_timecode) return;
753 // length sanity checking
754 string local_timecode = timecode;
756 // truncate to 10 characters
757 if (local_timecode.length() > 10) {
758 local_timecode = local_timecode.substr (0, 10);
761 // pad to 10 characters
762 while (local_timecode.length() < 10) {
763 local_timecode += " ";
766 // translate characters.
767 // Only the characters that actually changed are sent.
770 for (i = local_timecode.length () - 1; i >= 0; i--) {
772 if (local_timecode[i] == last_timecode[i]) {
775 MidiByteArray retval (2, 0xb0, position);
776 retval << translate_seven_segment (local_timecode[i]);
777 _port->write (retval);
782 Surface::update_flip_mode_display ()
784 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
785 (*s)->flip_mode_changed (true);
790 Surface::update_view_mode_display ()
799 switch (_mcp.view_mode()) {
800 case MackieControlProtocol::Mixer:
801 show_two_char_display ("Mx");
804 case MackieControlProtocol::Dynamics:
805 show_two_char_display ("Dy");
808 case MackieControlProtocol::EQ:
809 show_two_char_display ("EQ");
812 case MackieControlProtocol::Loop:
813 show_two_char_display ("LP");
816 case MackieControlProtocol::AudioTracks:
817 show_two_char_display ("AT");
819 case MackieControlProtocol::MidiTracks:
820 show_two_char_display ("MT");
822 case MackieControlProtocol::Sends:
823 show_two_char_display ("Sn");
826 case MackieControlProtocol::Plugins:
827 show_two_char_display ("Pl");
836 /* we are attempting to turn a global button/LED on */
838 map<int,Control*>::iterator x = controls_by_device_independent_id.find (id);
840 if (x != controls_by_device_independent_id.end()) {
841 Button* button = dynamic_cast<Button*> (x->second);
843 _port->write (button->set_state (on));
849 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
850 _port->write ((*s)->display (1, text));
856 Surface::gui_selection_changed (const ARDOUR::StrongRouteNotificationList& routes)
858 for (Strips::iterator s = strips.begin(); s != strips.end(); ++s) {
859 (*s)->gui_selection_changed (routes);
864 Surface::say_hello ()
866 /* wakeup for Mackie Control */
867 MidiByteArray wakeup (7, MIDI::sysex, 0x00, 0x00, 0x66, 0x14, 0x00, MIDI::eox);
868 _port->write (wakeup);
869 wakeup[4] = 0x15; /* wakup Mackie XT */
870 _port->write (wakeup);
871 wakeup[4] = 0x10; /* wakupe Logic Control */
872 _port->write (wakeup);
873 wakeup[4] = 0x11; /* wakeup Logic Control XT */
874 _port->write (wakeup);
878 Surface::next_jog_mode ()
883 Surface::set_jog_mode (JogWheel::Mode)
888 Surface::route_is_locked_to_strip (boost::shared_ptr<Route> r) const
890 for (Strips::const_iterator s = strips.begin(); s != strips.end(); ++s) {
891 if ((*s)->route() == r && (*s)->locked()) {
899 Surface::notify_metering_state_changed()
901 for (Strips::const_iterator s = strips.begin(); s != strips.end(); ++s) {
902 (*s)->notify_metering_state_changed ();