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 , _route_locked (false)
73 , _last_fader_position_written (-1.0)
74 , _last_vpot_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);
91 TODO could optimise this to use enum, but it's only
92 called during the protocol class instantiation.
94 void Strip::add (Control & control)
103 if ((fader = dynamic_cast<Fader*>(&control)) != 0) {
107 } else if ((pot = dynamic_cast<Pot*>(&control)) != 0) {
111 } else if ((button = dynamic_cast<Button*>(&control)) != 0) {
113 if (control.id() >= Button::recenable_base_id &&
114 control.id() < Button::recenable_base_id + 8) {
118 } else if (control.id() >= Button::mute_base_id &&
119 control.id() < Button::mute_base_id + 8) {
123 } else if (control.id() >= Button::solo_base_id &&
124 control.id() < Button::solo_base_id + 8) {
128 } else if (control.id() >= Button::select_base_id &&
129 control.id() < Button::select_base_id + 8) {
133 } else if (control.id() >= Button::vselect_base_id &&
134 control.id() < Button::vselect_base_id + 8) {
138 } else if (control.id() >= Button::fader_touch_base_id &&
139 control.id() < Button::fader_touch_base_id + 8) {
141 _fader_touch = button;
144 } else if ((meter = dynamic_cast<Meter*>(&control)) != 0) {
150 Strip::set_route (boost::shared_ptr<Route> r)
156 route_connections.drop_connections ();
162 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
163 _surface->number(), _index, _route->name()));
167 _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
171 _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
174 _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
176 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
178 if (_route->pannable()) {
179 _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
180 _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
183 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
186 trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
189 // TODO this works when a currently-banked route is made inactive, but not
190 // when a route is activated which should be currently banked.
192 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
193 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
197 // RemoteControlIDChanged. Better handled at Session level.
203 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
204 _surface->number(), _index));
211 notify_solo_changed ();
212 notify_mute_changed ();
213 notify_gain_changed ();
214 notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
215 notify_panner_changed ();
216 notify_record_enable_changed ();
220 Strip::notify_solo_changed ()
222 if (_route && _solo) {
223 _surface->write (_solo->set_state (_route->soloed() ? on : off));
228 Strip::notify_mute_changed ()
230 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Strip %1 mute changed\n", _index));
231 if (_route && _mute) {
232 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("\troute muted ? %1\n", _route->muted()));
233 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("mute message: %1\n", _mute->set_state (_route->muted() ? on : off)));
235 _surface->write (_mute->set_state (_route->muted() ? on : off));
240 Strip::notify_record_enable_changed ()
242 if (_route && _recenable) {
243 _surface->write (_recenable->set_state (_route->record_enabled() ? on : off));
248 Strip::notify_active_changed ()
250 _surface->mcp().refresh_current_bank();
254 Strip::notify_route_deleted ()
256 _surface->mcp().refresh_current_bank();
260 Strip::notify_gain_changed (bool force_update)
262 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("gain changed for strip %1, flip mode %2\n", _index, _surface->mcp().flip_mode()));
264 if (_route && _fader) {
266 if (!_fader->in_use()) {
270 switch (_surface->mcp().flip_mode()) {
271 case MackieControlProtocol::Normal:
272 pos = _route->gain_control()->get_value();
275 case MackieControlProtocol::Swap:
276 case MackieControlProtocol::Zero:
277 case MackieControlProtocol::Mirror:
278 /* fader is used for something else and/or
284 pos = gain_to_slider_position (pos);
286 if (force_update || pos != _last_fader_position_written) {
287 _surface->write (_fader->set_position (pos));
288 _last_fader_position_written = pos;
290 DEBUG_TRACE (DEBUG::MackieControl, "value is stale, no message sent\n");
293 DEBUG_TRACE (DEBUG::MackieControl, "fader in use, no message sent\n");
296 DEBUG_TRACE (DEBUG::MackieControl, "no route or no fader\n");
301 Strip::notify_property_changed (const PropertyChange& what_changed)
303 if (!what_changed.contains (ARDOUR::Properties::name)) {
309 string fullname = _route->name();
311 if (fullname.length() <= 6) {
314 line1 = PBD::short_version (fullname, 6);
317 _surface->write (display (0, line1));
322 Strip::notify_panner_changed (bool force_update)
324 if (_route && _vpot) {
326 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("pan change for strip %1\n", _index));
328 boost::shared_ptr<Pannable> pannable = _route->pannable();
331 _surface->write (_vpot->zero());
337 switch (_surface->mcp().flip_mode()) {
338 case MackieControlProtocol::Swap:
339 /* pot is controlling the gain */
342 case MackieControlProtocol::Normal:
343 case MackieControlProtocol::Zero:
344 case MackieControlProtocol::Mirror:
345 pos = pannable->pan_azimuth_control->get_value();
349 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("\t\tnew position %1\n", pos));
351 if (force_update || pos != _last_vpot_position_written) {
352 _surface->write (_vpot->set_all (pos, true, Pot::dot));
353 _last_vpot_position_written = pos;
359 Strip::handle_button (Button& button, ButtonState bs)
361 button.set_in_use (bs == press);
364 // no route so always switch the light off
365 // because no signals will be emitted by a non-route
366 _surface->write (button.set_state (off));
371 if (button.id() >= Button::recenable_base_id &&
372 button.id() < Button::recenable_base_id + 8) {
374 _route->set_record_enabled (!_route->record_enabled(), this);
376 } else if (button.id() >= Button::mute_base_id &&
377 button.id() < Button::mute_base_id + 8) {
379 _route->set_mute (!_route->muted(), this);
381 } else if (button.id() >= Button::solo_base_id &&
382 button.id() < Button::solo_base_id + 8) {
384 _route->set_solo (!_route->soloed(), this);
386 } else if (button.id() >= Button::select_base_id &&
387 button.id() < Button::select_base_id + 8) {
389 int lock_mod = (MackieControlProtocol::MODIFIER_CONTROL|MackieControlProtocol::MODIFIER_SHIFT);
391 if ((_surface->mcp().modifier_state() & lock_mod) == lock_mod) {
393 _route_locked = !_route_locked;
395 } else if (_surface->mcp().modifier_state() == MackieControlProtocol::MODIFIER_SHIFT) {
396 /* reset gain value to unity */
397 _route->set_gain (1.0, this);
399 _surface->mcp().select_track (_route);
402 } else if (button.id() >= Button::vselect_base_id &&
403 button.id() < Button::vselect_base_id + 8) {
408 if (button.id() >= Button::fader_touch_base_id &&
409 button.id() < Button::fader_touch_base_id + 8) {
411 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
413 bool state = (bs == press);
415 _fader->set_in_use (state);
417 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
419 /* BCF faders don't support touch, so add a timeout to reset
420 their `in_use' state.
423 _surface->mcp().add_in_use_timeout (*_surface, *_fader, _fader_touch);
429 Strip::handle_fader (Fader& fader, float position)
431 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", position));
437 switch (_surface->mcp().flip_mode()) {
438 case MackieControlProtocol::Normal:
439 _route->gain_control()->set_value (slider_position_to_gain (position));
441 case MackieControlProtocol::Zero:
443 case MackieControlProtocol::Mirror:
445 case MackieControlProtocol::Swap:
446 _route->pannable()->pan_azimuth_control->set_value (position);
450 if (ARDOUR::Config->get_mackie_emulation() == "bcf") {
451 /* reset the timeout while we're still moving the fader */
452 _surface->mcp().add_in_use_timeout (*_surface, fader, fader.in_use_touch_control);
455 // must echo bytes back to slider now, because
456 // the notifier only works if the fader is not being
457 // touched. Which it is if we're getting input.
459 _surface->write (fader.set_position (position));
463 Strip::handle_pot (Pot& pot, float delta)
466 _surface->write (pot.set_onoff (false));
470 boost::shared_ptr<Pannable> pannable = _route->pannable();
473 boost::shared_ptr<AutomationControl> ac;
475 switch (_surface->mcp().flip_mode()) {
476 case MackieControlProtocol::Normal: /* pot controls pan */
477 case MackieControlProtocol::Mirror: /* pot + fader control pan */
478 case MackieControlProtocol::Zero: /* pot controls pan, faders don't move */
479 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("modifier state %1\n", _surface->mcp().modifier_state()));
480 if (_surface->mcp().modifier_state() & MackieControlProtocol::MODIFIER_CONTROL) {
481 DEBUG_TRACE (DEBUG::MackieControl, "pot using control to alter width\n");
482 ac = pannable->pan_width_control;
484 DEBUG_TRACE (DEBUG::MackieControl, "pot using control to alter position\n");
485 ac = pannable->pan_azimuth_control;
488 case MackieControlProtocol::Swap: /* pot controls gain */
489 ac = _route->gain_control();
494 double p = ac->get_value();
496 // calculate new value, and adjust
513 update_automation ();
518 Strip::update_automation ()
520 ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
522 if (gain_state == Touch || gain_state == Play) {
523 notify_gain_changed (false);
526 if (_route->panner()) {
527 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
528 if (panner_state == Touch || panner_state == Play) {
529 notify_panner_changed (false);
535 Strip::update_meter ()
538 float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
539 _surface->write (_meter->update_message (dB));
546 MidiByteArray retval;
548 for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
549 retval << (*it)->zero ();
552 retval << blank_display (0);
553 retval << blank_display (1);
559 Strip::blank_display (uint32_t line_number)
561 return display (line_number, string());
565 Strip::display (uint32_t line_number, const std::string& line)
567 assert (line_number <= 1);
569 MidiByteArray retval;
571 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
574 retval << _surface->sysex_hdr();
578 // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
579 retval << (_index * 7 + (line_number * 0x38));
581 // ascii data to display
583 // pad with " " out to 6 chars
584 for (int i = line.length(); i < 6; ++i) {
588 // column spacer, unless it's the right-hand column
596 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));
604 /* don't lock unless we have a route */
606 _route_locked = true;
611 Strip::unlock_route ()
613 _route_locked = false;