2 Copyright (C) 2006,2007 John Anderson
3 Copyright (C) 2012 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "midi++/port.h"
28 #include "pbd/compose.h"
29 #include "pbd/convert.h"
31 #include "ardour/debug.h"
32 #include "ardour/midi_ui.h"
33 #include "ardour/route.h"
34 #include "ardour/track.h"
35 #include "ardour/pannable.h"
36 #include "ardour/panner.h"
37 #include "ardour/panner_shell.h"
38 #include "ardour/rc_configuration.h"
39 #include "ardour/meter.h"
41 #include "mackie_control_protocol.h"
42 #include "surface_port.h"
51 using namespace Mackie;
53 using namespace ARDOUR;
56 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
57 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
59 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
60 #define invalidator() __invalidator (*(MackieControlProtocol::instance()), __FILE__, __LINE__)
62 Strip::Strip (Surface& s, const std::string& name, int index, StripControlDefinition* ctls)
74 , _controls_locked (false)
75 , _reset_display_at (0)
76 , _last_gain_position_written (-1.0)
77 , _last_pan_position_written (-1.0)
79 /* build the controls for this track, which will automatically add them
83 for (uint32_t i = 0; ctls[i].name[0]; ++i) {
84 ctls[i].factory (*_surface, ctls[i].base_id + index, ctls[i].name, *this);
93 TODO could optimise this to use enum, but it's only
94 called during the protocol class instantiation.
96 void Strip::add (Control & control)
105 if ((fader = dynamic_cast<Fader*>(&control)) != 0) {
109 } else if ((pot = dynamic_cast<Pot*>(&control)) != 0) {
113 } else if ((button = dynamic_cast<Button*>(&control)) != 0) {
115 if (control.id() >= Button::recenable_base_id &&
116 control.id() < Button::recenable_base_id + 8) {
120 } else if (control.id() >= Button::mute_base_id &&
121 control.id() < Button::mute_base_id + 8) {
125 } else if (control.id() >= Button::solo_base_id &&
126 control.id() < Button::solo_base_id + 8) {
130 } else if (control.id() >= Button::select_base_id &&
131 control.id() < Button::select_base_id + 8) {
135 } else if (control.id() >= Button::vselect_base_id &&
136 control.id() < Button::vselect_base_id + 8) {
140 } else if (control.id() >= Button::fader_touch_base_id &&
141 control.id() < Button::fader_touch_base_id + 8) {
143 _fader_touch = button;
146 } else if ((meter = dynamic_cast<Meter*>(&control)) != 0) {
152 Strip::set_route (boost::shared_ptr<Route> r)
154 if (_controls_locked) {
158 route_connections.drop_connections ();
164 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
165 _surface->number(), _index, _route->name()));
169 _solo->set_normal_control (_route->solo_control());
170 _solo->set_modified_control (boost::shared_ptr<AutomationControl>());
171 _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
175 _mute->set_normal_control (_route->mute_control());
176 _mute->set_modified_control (boost::shared_ptr<AutomationControl>());
177 _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
180 _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
182 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
184 if (_route->pannable()) {
185 _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
186 _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
189 /* bind fader & pan pot, as appropriate for current flip mode */
191 flip_mode_changed (false);
193 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
196 _recenable->set_normal_control (trk->rec_enable_control());
197 _recenable->set_modified_control (boost::shared_ptr<AutomationControl>());
198 trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
201 // TODO this works when a currently-banked route is made inactive, but not
202 // when a route is activated which should be currently banked.
204 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
205 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
209 // RemoteControlIDChanged. Better handled at Session level.
215 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
216 _surface->number(), _index));
223 notify_solo_changed ();
224 notify_mute_changed ();
225 notify_gain_changed ();
226 notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
227 notify_panner_changed ();
228 notify_record_enable_changed ();
232 Strip::notify_solo_changed ()
234 if (_route && _solo) {
235 _surface->write (_solo->set_state (_route->soloed() ? on : off));
240 Strip::notify_mute_changed ()
242 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Strip %1 mute changed\n", _index));
243 if (_route && _mute) {
244 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("\troute muted ? %1\n", _route->muted()));
245 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("mute message: %1\n", _mute->set_state (_route->muted() ? on : off)));
247 _surface->write (_mute->set_state (_route->muted() ? on : off));
252 Strip::notify_record_enable_changed ()
254 if (_route && _recenable) {
255 _surface->write (_recenable->set_state (_route->record_enabled() ? on : off));
260 Strip::notify_active_changed ()
262 _surface->mcp().refresh_current_bank();
266 Strip::notify_route_deleted ()
268 _surface->mcp().refresh_current_bank();
272 Strip::notify_gain_changed (bool force_update)
274 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("gain changed for strip %1, flip mode %2\n", _index, _surface->mcp().flip_mode()));
280 if (_surface->mcp().flip_mode()) {
286 if (!control->in_use()) {
288 float pos = _route->gain_control()->internal_to_interface (_route->gain_control()->get_value());
290 if (force_update || pos != _last_gain_position_written) {
292 if (_surface->mcp().flip_mode()) {
293 _surface->write (_vpot->set_all (pos, true, Pot::wrap));
294 do_parameter_display (GainAutomation, pos);
296 _surface->write (_fader->set_position (pos));
297 do_parameter_display (GainAutomation, pos);
300 queue_display_reset (2000);
301 _last_gain_position_written = pos;
304 DEBUG_TRACE (DEBUG::MackieControl, "value is stale, no message sent\n");
307 DEBUG_TRACE (DEBUG::MackieControl, "fader in use, no message sent\n");
310 DEBUG_TRACE (DEBUG::MackieControl, "no route or no fader\n");
315 Strip::notify_property_changed (const PropertyChange& what_changed)
317 if (!what_changed.contains (ARDOUR::Properties::name)) {
323 string fullname = _route->name();
325 if (fullname.length() <= 6) {
328 line1 = PBD::short_version (fullname, 6);
331 _surface->write (display (0, line1));
336 Strip::notify_panner_changed (bool force_update)
340 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("pan change for strip %1\n", _index));
342 boost::shared_ptr<Pannable> pannable = _route->pannable();
345 _surface->write (_vpot->zero());
351 if (_surface->mcp().flip_mode()) {
357 if (!control->in_use()) {
359 double pos = pannable->pan_azimuth_control->internal_to_interface (pannable->pan_azimuth_control->get_value());
361 if (force_update || pos != _last_pan_position_written) {
363 if (_surface->mcp().flip_mode()) {
365 _surface->write (_fader->set_position (pos));
366 do_parameter_display (PanAzimuthAutomation, pos);
368 _surface->write (_vpot->set_all (pos, true, Pot::dot));
369 do_parameter_display (PanAzimuthAutomation, pos);
372 queue_display_reset (2000);
373 _last_pan_position_written = pos;
380 Strip::handle_button (Button& button, ButtonState bs)
383 button.set_in_use (true);
385 button.set_in_use (false);
388 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip %1 handling button %2 press ? %3\n", _index, button.id(), (bs == press)));
390 int lock_mod = (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT);
391 int ms = _surface->mcp().modifier_state();
392 bool modified = (ms & MackieControlProtocol::MODIFIER_CONTROL);
394 if (button.id() >= Button::select_base_id &&
395 button.id() < Button::select_base_id + 8) {
397 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("select touch, lock ? %1\n", ((ms & lock_mod) == lock_mod) ? 1 : 0));
401 if ((ms & lock_mod) == lock_mod) {
402 _controls_locked = !_controls_locked;
406 DEBUG_TRACE (DEBUG::MackieControl, "add select button on press\n");
407 _surface->mcp().add_down_select_button (_surface->number(), _index);
409 _surface->mcp().select_range ();
412 DEBUG_TRACE (DEBUG::MackieControl, "remove select button on release\n");
413 _surface->mcp().remove_down_select_button (_surface->number(), _index);
419 if ((button.id() >= Button::fader_touch_base_id &&
420 button.id() < Button::fader_touch_base_id + 8) ||
421 /* Hack for SSL Nucleus */
422 (button.id() >= 104 && button.id() < 112)) {
424 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
426 /* never use the modified control for fader stuff */
430 _fader->set_in_use (true);
431 _fader->start_touch (_surface->mcp().transport_frame(), false);
432 boost::shared_ptr<AutomationControl> ac = _fader->control (false);
434 do_parameter_display ((AutomationType) ac->parameter().type(), ac->internal_to_interface (ac->get_value()));
435 queue_display_reset (2000);
440 _fader->set_in_use (false);
441 _fader->stop_touch (_surface->mcp().transport_frame(), true, false);
448 boost::shared_ptr<AutomationControl> control = button.control (modified);
453 DEBUG_TRACE (DEBUG::MackieControl, "add button on release\n");
454 _surface->mcp().add_down_button ((AutomationType) control->parameter().type(), _surface->number(), _index);
458 if (ms & MackieControlProtocol::MODIFIER_OPTION) {
459 /* reset to default/normal value */
460 new_value = control->normal();
462 new_value = control->get_value() ? 0.0 : 1.0;
465 /* get all controls that either have their
466 * button down or are within a range of
467 * several down buttons
470 MackieControlProtocol::ControlList controls = _surface->mcp().down_controls ((AutomationType) control->parameter().type());
473 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("there are %1 buttons down for control type %2, new value = %3\n",
474 controls.size(), control->parameter().type(), new_value));
478 for (MackieControlProtocol::ControlList::iterator c = controls.begin(); c != controls.end(); ++c) {
479 (*c)->set_value (new_value);
483 DEBUG_TRACE (DEBUG::MackieControl, "remove button on release\n");
484 _surface->mcp().remove_down_button ((AutomationType) control->parameter().type(), _surface->number(), _index);
488 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("button has no control at present (modified ? %1)\n", modified));
493 Strip::do_parameter_display (AutomationType type, float val)
499 dB = fast_coefficient_to_dB (val);
501 _surface->write (display (1, " -inf "));
505 snprintf (buf, sizeof (buf), "%6.1f", dB);
506 _surface->write (display (1, buf));
510 case PanAzimuthAutomation:
512 boost::shared_ptr<Pannable> p = _route->pannable();
513 if (p && p->panner()) {
514 string str = p->panner()->value_as_string (p->pan_azimuth_control);
515 _surface->write (display (1, str));
524 Strip::handle_fader (Fader& fader, float position)
526 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", position));
528 bool modified = (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL);
530 fader.set_value (position, modified);
531 fader.start_touch (_surface->mcp().transport_frame(), modified);
532 queue_display_reset (2000);
534 // must echo bytes back to slider now, because
535 // the notifier only works if the fader is not being
536 // touched. Which it is if we're getting input.
538 _surface->write (fader.set_position (position));
542 Strip::handle_pot (Pot& pot, float delta)
544 /* Pots only emit events when they move, not when they
545 stop moving. So to get a stop event, we need to use a timeout.
548 bool modified = (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL);
549 pot.start_touch (_surface->mcp().transport_frame(), modified);
550 _surface->mcp().add_in_use_timeout (*_surface, pot, pot.control (modified));
552 double p = pot.get_value (modified);
556 pot.set_value (p, modified);
560 Strip::periodic (uint64_t usecs)
566 update_automation ();
569 if (_reset_display_at && _reset_display_at < usecs) {
575 Strip::update_automation ()
577 ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
579 if (gain_state == Touch || gain_state == Play) {
580 notify_gain_changed (false);
583 if (_route->panner()) {
584 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
585 if (panner_state == Touch || panner_state == Play) {
586 notify_panner_changed (false);
592 Strip::update_meter ()
595 float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
596 _surface->write (_meter->update_message (dB));
603 MidiByteArray retval;
605 for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
606 retval << (*it)->zero ();
609 retval << blank_display (0);
610 retval << blank_display (1);
616 Strip::blank_display (uint32_t line_number)
618 return display (line_number, string());
622 Strip::display (uint32_t line_number, const std::string& line)
624 assert (line_number <= 1);
626 MidiByteArray retval;
628 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
631 retval << _surface->sysex_hdr();
635 // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
636 retval << (_index * 7 + (line_number * 0x38));
638 // ascii data to display
640 // pad with " " out to 6 chars
641 for (int i = line.length(); i < 6; ++i) {
645 // column spacer, unless it's the right-hand column
653 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));
659 Strip::lock_controls ()
661 _controls_locked = true;
665 Strip::unlock_controls ()
667 _controls_locked = false;
671 Strip::gui_selection_changed (ARDOUR::RouteNotificationListPtr rl)
673 for (ARDOUR::RouteNotificationList::iterator i = rl->begin(); i != rl->end(); ++i) {
674 if ((*i) == _route) {
675 return _select->set_state (on);
679 return _select->set_state (off);
683 Strip::static_display_string () const
685 if (_surface->mcp().flip_mode()) {
693 Strip::flip_mode_changed (bool notify)
699 boost::shared_ptr<Pannable> pannable = _route->pannable();
701 if (_surface->mcp().flip_mode()) {
704 _fader->set_normal_control (pannable->pan_azimuth_control);
705 _fader->set_modified_control (pannable->pan_width_control);
707 _vpot->set_normal_control (_route->gain_control());
708 _vpot->set_modified_control (boost::shared_ptr<AutomationControl>());
710 _surface->write (display (1, static_display_string ()));
715 _vpot->set_normal_control (pannable->pan_azimuth_control);
716 _vpot->set_modified_control (pannable->pan_width_control);
718 _fader->set_normal_control (_route->gain_control());
719 _fader->set_modified_control (boost::shared_ptr<AutomationControl>());
721 _surface->write (display (1, static_display_string()));
730 Strip::queue_display_reset (uint32_t msecs)
733 struct timeval delta;
735 gettimeofday (&now, 0);
737 delta.tv_sec = msecs/1000;
738 delta.tv_usec = (msecs - ((msecs/1000) * 1000)) * 1000;
740 timeradd (&now, &delta, &when);
742 _reset_display_at = (when.tv_sec * 1000000) + when.tv_usec;
746 Strip::clear_display_reset ()
748 _reset_display_at = 0;
752 Strip::reset_display ()
754 _surface->write (display (1, static_display_string()));
755 clear_display_reset ();