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.
24 #include "midi++/port.h"
26 #include "pbd/compose.h"
27 #include "pbd/convert.h"
29 #include "ardour/debug.h"
30 #include "ardour/midi_ui.h"
31 #include "ardour/route.h"
32 #include "ardour/track.h"
33 #include "ardour/pannable.h"
34 #include "ardour/panner.h"
35 #include "ardour/panner_shell.h"
36 #include "ardour/rc_configuration.h"
37 #include "ardour/meter.h"
39 #include "mackie_control_protocol.h"
40 #include "surface_port.h"
49 using namespace Mackie;
51 using namespace ARDOUR;
54 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
55 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
57 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
58 #define invalidator() __invalidator (*(MackieControlProtocol::instance()), __FILE__, __LINE__)
60 Strip::Strip (Surface& s, const std::string& name, int index, StripControlDefinition* ctls)
72 , _controls_locked (false)
73 , _last_gain_position_written (-1.0)
74 , _last_pan_position_written (-1.0)
76 /* build the controls for this track, which will automatically add them
80 for (uint32_t i = 0; ctls[i].name[0]; ++i) {
81 ctls[i].factory (*_surface, ctls[i].base_id + index, ctls[i].name, *this);
90 TODO could optimise this to use enum, but it's only
91 called during the protocol class instantiation.
93 void Strip::add (Control & control)
102 if ((fader = dynamic_cast<Fader*>(&control)) != 0) {
106 } else if ((pot = dynamic_cast<Pot*>(&control)) != 0) {
110 } else if ((button = dynamic_cast<Button*>(&control)) != 0) {
112 if (control.id() >= Button::recenable_base_id &&
113 control.id() < Button::recenable_base_id + 8) {
117 } else if (control.id() >= Button::mute_base_id &&
118 control.id() < Button::mute_base_id + 8) {
122 } else if (control.id() >= Button::solo_base_id &&
123 control.id() < Button::solo_base_id + 8) {
127 } else if (control.id() >= Button::select_base_id &&
128 control.id() < Button::select_base_id + 8) {
132 } else if (control.id() >= Button::vselect_base_id &&
133 control.id() < Button::vselect_base_id + 8) {
137 } else if (control.id() >= Button::fader_touch_base_id &&
138 control.id() < Button::fader_touch_base_id + 8) {
140 _fader_touch = button;
143 } else if ((meter = dynamic_cast<Meter*>(&control)) != 0) {
149 Strip::set_route (boost::shared_ptr<Route> r)
151 if (_controls_locked) {
155 route_connections.drop_connections ();
161 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
162 _surface->number(), _index, _route->name()));
166 _solo->set_normal_control (_route->solo_control());
167 _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
171 _mute->set_normal_control (_route->mute_control());
172 _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
175 _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
177 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
179 if (_route->pannable()) {
180 _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
181 _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
184 flip_mode_changed (false);
186 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
189 // XXX FIX ME WHEN rec-enable IS-A AutomationControl
190 // _recenable->set_normal_control (trk->rec_enable_control());
191 trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
194 // TODO this works when a currently-banked route is made inactive, but not
195 // when a route is activated which should be currently banked.
197 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
198 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
202 // RemoteControlIDChanged. Better handled at Session level.
208 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
209 _surface->number(), _index));
216 notify_solo_changed ();
217 notify_mute_changed ();
218 notify_gain_changed ();
219 notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
220 notify_panner_changed ();
221 notify_record_enable_changed ();
225 Strip::notify_solo_changed ()
227 if (_route && _solo) {
228 _surface->write (_solo->set_state (_route->soloed() ? on : off));
233 Strip::notify_mute_changed ()
235 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Strip %1 mute changed\n", _index));
236 if (_route && _mute) {
237 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("\troute muted ? %1\n", _route->muted()));
238 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("mute message: %1\n", _mute->set_state (_route->muted() ? on : off)));
240 _surface->write (_mute->set_state (_route->muted() ? on : off));
245 Strip::notify_record_enable_changed ()
247 if (_route && _recenable) {
248 _surface->write (_recenable->set_state (_route->record_enabled() ? on : off));
253 Strip::notify_active_changed ()
255 _surface->mcp().refresh_current_bank();
259 Strip::notify_route_deleted ()
261 _surface->mcp().refresh_current_bank();
265 Strip::notify_gain_changed (bool force_update)
267 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("gain changed for strip %1, flip mode %2\n", _index, _surface->mcp().flip_mode()));
273 if (_surface->mcp().flip_mode()) {
279 if (!control->in_use()) {
281 float pos = _route->gain_control()->internal_to_interface (_route->gain_control()->get_value());
283 if (force_update || pos != _last_gain_position_written) {
285 if (_surface->mcp().flip_mode()) {
286 _surface->write (_vpot->set_all (pos, true, Pot::boost_cut));
288 _surface->write (_fader->set_position (pos));
290 _last_gain_position_written = pos;
293 DEBUG_TRACE (DEBUG::MackieControl, "value is stale, no message sent\n");
296 DEBUG_TRACE (DEBUG::MackieControl, "fader in use, no message sent\n");
299 DEBUG_TRACE (DEBUG::MackieControl, "no route or no fader\n");
304 Strip::notify_property_changed (const PropertyChange& what_changed)
306 if (!what_changed.contains (ARDOUR::Properties::name)) {
312 string fullname = _route->name();
314 if (fullname.length() <= 6) {
317 line1 = PBD::short_version (fullname, 6);
320 _surface->write (display (0, line1));
325 Strip::notify_panner_changed (bool force_update)
329 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("pan change for strip %1\n", _index));
331 boost::shared_ptr<Pannable> pannable = _route->pannable();
334 _surface->write (_vpot->zero());
340 if (_surface->mcp().flip_mode()) {
346 if (!control->in_use()) {
348 double pos = pannable->pan_azimuth_control->internal_to_interface (pannable->pan_azimuth_control->get_value());
350 if (force_update || pos != _last_pan_position_written) {
351 if (_surface->mcp().flip_mode()) {
352 _surface->write (_fader->set_position (pos));
354 _surface->write (_vpot->set_all (pos, true, Pot::dot));
356 _last_pan_position_written = pos;
363 Strip::handle_button (Button& button, ButtonState bs)
365 button.set_in_use (bs == press);
367 int lock_mod = (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT);
368 int ms = _surface->mcp().modifier_state();
369 bool modified = (ms & MackieControlProtocol::MODIFIER_CONTROL);
371 if (button.id() >= Button::select_base_id &&
372 button.id() < Button::select_base_id + 8) {
373 if ((ms & lock_mod) == lock_mod) {
374 _controls_locked = !_controls_locked;
379 if (button.id() >= Button::fader_touch_base_id &&
380 button.id() < Button::fader_touch_base_id + 8) {
382 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
384 bool state = (bs == press);
386 _fader->set_in_use (state);
387 _fader->start_touch (_surface->mcp().transport_frame(), modified);
389 if (!_surface->mcp().device_info().has_touch_sense_faders()) {
390 _surface->mcp().add_in_use_timeout (*_surface, *_fader, _fader->control (modified));
396 if (ms & MackieControlProtocol::MODIFIER_OPTION) {
397 /* reset to default/normal value */
399 boost::shared_ptr<AutomationControl> control = button.control (modified);
402 control->set_value (!control->get_value());
408 Strip::handle_fader (Fader& fader, float position)
410 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", position));
412 bool modified = (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL);
414 fader.set_value (position, modified);
415 fader.start_touch (_surface->mcp().transport_frame(), modified);
417 if (!_surface->mcp().device_info().has_touch_sense_faders()) {
418 _surface->mcp().add_in_use_timeout (*_surface, fader, fader.control (modified));
421 // must echo bytes back to slider now, because
422 // the notifier only works if the fader is not being
423 // touched. Which it is if we're getting input.
425 _surface->write (fader.set_position (position));
429 Strip::handle_pot (Pot& pot, float delta)
431 /* Pots only emit events when they move, not when they
432 stop moving. So to get a stop event, we need to use a timeout.
435 bool modified = (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL);
436 pot.start_touch (_surface->mcp().transport_frame(), modified);
437 _surface->mcp().add_in_use_timeout (*_surface, pot, pot.control (modified));
439 double p = pot.get_value (modified);
443 pot.set_value (p, modified);
453 update_automation ();
458 Strip::update_automation ()
460 ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
462 if (gain_state == Touch || gain_state == Play) {
463 notify_gain_changed (false);
466 if (_route->panner()) {
467 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
468 if (panner_state == Touch || panner_state == Play) {
469 notify_panner_changed (false);
475 Strip::update_meter ()
478 float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
479 _surface->write (_meter->update_message (dB));
486 MidiByteArray retval;
488 for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
489 retval << (*it)->zero ();
492 retval << blank_display (0);
493 retval << blank_display (1);
499 Strip::blank_display (uint32_t line_number)
501 return display (line_number, string());
505 Strip::display (uint32_t line_number, const std::string& line)
507 assert (line_number <= 1);
509 MidiByteArray retval;
511 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
514 retval << _surface->sysex_hdr();
518 // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
519 retval << (_index * 7 + (line_number * 0x38));
521 // ascii data to display
523 // pad with " " out to 6 chars
524 for (int i = line.length(); i < 6; ++i) {
528 // column spacer, unless it's the right-hand column
536 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));
542 Strip::lock_controls ()
544 _controls_locked = true;
548 Strip::unlock_controls ()
550 _controls_locked = false;
554 Strip::gui_selection_changed (ARDOUR::RouteNotificationListPtr rl)
556 for (ARDOUR::RouteNotificationList::iterator i = rl->begin(); i != rl->end(); ++i) {
557 if ((*i) == _route) {
558 std::cerr << "Strip " << _index << " selected, route = " << (*i)->name() << std::endl;
559 return _select->set_state (on);
563 std::cerr << "Strip " << _index << " NOT selected\n";
564 return _select->set_state (off);
568 Strip::flip_mode_changed (bool notify)
574 boost::shared_ptr<Pannable> pannable = _route->pannable();
576 if (_surface->mcp().flip_mode()) {
579 _fader->set_normal_control (pannable->pan_azimuth_control);
580 _fader->set_modified_control (pannable->pan_width_control);
582 _vpot->set_normal_control (_route->gain_control());
583 _vpot->set_modified_control (boost::shared_ptr<AutomationControl>());
585 _surface->write (display (1, "Pan"));
590 _vpot->set_normal_control (pannable->pan_azimuth_control);
591 _vpot->set_modified_control (pannable->pan_width_control);
593 _fader->set_normal_control (_route->gain_control());
594 _fader->set_modified_control (boost::shared_ptr<AutomationControl>());
596 _surface->write (display (1, "Fdr"));