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)
73 /* build the controls for this track, which will automatically add them
77 for (uint32_t i = 0; ctls[i].name[0]; ++i) {
78 ctls[i].factory (*_surface, ctls[i].base_id + index, ctls[i].name, *this);
88 TODO could optimise this to use enum, but it's only
89 called during the protocol class instantiation.
91 void Strip::add (Control & control)
95 if (control.name() == "gain") {
96 _gain = reinterpret_cast<Fader*>(&control);
97 } else if (control.name() == "vpot") {
98 _vpot = reinterpret_cast<Pot*>(&control);
99 } else if (control.name() == "recenable") {
100 _recenable = reinterpret_cast<Button*>(&control);
101 } else if (control.name() == "solo") {
102 _solo = reinterpret_cast<Button*>(&control);
103 } else if (control.name() == "mute") {
104 _mute = reinterpret_cast<Button*>(&control);
105 } else if (control.name() == "select") {
106 _select = reinterpret_cast<Button*>(&control);
107 } else if (control.name() == "vselect") {
108 _vselect = reinterpret_cast<Button*>(&control);
109 } else if (control.name() == "fader_touch") {
110 _fader_touch = reinterpret_cast<Button*>(&control);
111 } else if (control.name() == "meter") {
112 _meter = reinterpret_cast<Meter*>(&control);
122 throw MackieControlException ("gain is null");
131 throw MackieControlException ("vpot is null");
139 if (_recenable == 0) {
140 throw MackieControlException ("recenable is null");
149 throw MackieControlException ("solo is null");
157 throw MackieControlException ("mute is null");
166 throw MackieControlException ("select is null");
175 throw MackieControlException ("vselect is null");
183 if (_fader_touch == 0) {
184 throw MackieControlException ("fader_touch is null");
186 return *_fader_touch;
193 throw MackieControlException ("meter is null");
198 std::ostream & Mackie::operator << (std::ostream & os, const Strip & strip)
200 os << typeid (strip).name();
202 os << "has_solo: " << boolalpha << strip.has_solo();
204 os << "has_recenable: " << boolalpha << strip.has_recenable();
206 os << "has_mute: " << boolalpha << strip.has_mute();
208 os << "has_select: " << boolalpha << strip.has_select();
210 os << "has_vselect: " << boolalpha << strip.has_vselect();
212 os << "has_fader_touch: " << boolalpha << strip.has_fader_touch();
214 os << "has_vpot: " << boolalpha << strip.has_vpot();
216 os << "has_gain: " << boolalpha << strip.has_gain();
223 Strip::set_route (boost::shared_ptr<Route> r)
225 route_connections.drop_connections ();
231 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
232 _surface->number(), _index, _route->name()));
236 _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
239 _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
243 _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
246 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
248 if (_route->pannable()) {
249 _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
250 _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
253 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
256 trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
259 // TODO this works when a currently-banked route is made inactive, but not
260 // when a route is activated which should be currently banked.
262 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
263 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
267 // RemoteControlIDChanged. Better handled at Session level.
273 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
274 _surface->number(), _index));
282 notify_solo_changed ();
286 notify_mute_changed ();
290 notify_gain_changed ();
293 notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
296 notify_panner_changed ();
299 if (has_recenable()) {
300 notify_record_enable_changed ();
305 Strip::notify_solo_changed ()
307 if (_route && _solo) {
308 _surface->write (_solo->led().set_state (_route->soloed() ? on : off));
313 Strip::notify_mute_changed ()
315 if (_route && _mute) {
316 _surface->write (_mute->led().set_state (_route->muted() ? on : off));
321 Strip::notify_record_enable_changed ()
323 if (_route && _recenable) {
324 _surface->write (_recenable->led().set_state (_route->record_enabled() ? on : off));
329 Strip::notify_active_changed ()
331 _surface->mcp().refresh_current_bank();
335 Strip::notify_route_deleted ()
337 _surface->mcp().refresh_current_bank();
341 Strip::notify_gain_changed (bool force_update)
344 Fader & fader = gain();
346 if (!fader.in_use()) {
347 float position = gain_to_slider_position (_route->gain_control()->get_value());
348 // check that something has actually changed
349 if (force_update || position != _last_gain_written) {
350 _surface->write (fader.set_position (position));
351 _last_gain_written = position;
358 Strip::notify_property_changed (const PropertyChange& what_changed)
360 if (!what_changed.contains (ARDOUR::Properties::name)) {
366 string fullname = _route->name();
368 if (fullname.length() <= 6) {
371 line1 = PBD::short_version (fullname, 6);
374 _surface->write (display (0, line1));
375 _surface->write (blank_display (1));
380 Strip::notify_panner_changed (bool force_update)
384 boost::shared_ptr<Panner> panner = _route->panner();
387 double pos = panner->position ();
389 // cache the MidiByteArray here, because the mackie led control is much lower
390 // resolution than the panner control. So we save lots of byte
391 // sends in spite of more work on the comparison
393 MidiByteArray bytes = pot.set_all (pos, true, Pot::dot);
395 // check that something has actually changed
396 if (force_update || bytes != _last_pan_written)
398 _surface->write (bytes);
399 _last_pan_written = bytes;
402 _surface->write (pot.zero());
408 Strip::handle_button (Button& button, ButtonState bs)
410 button.set_in_use (bs == press);
413 // no route so always switch the light off
414 // because no signals will be emitted by a non-route
415 _surface->write (button.led().set_state (off));
420 if (button.raw_id() >= Control::recenable_button_base_id &&
421 button.raw_id() < Control::recenable_button_base_id + 8) {
423 _route->set_record_enabled (!_route->record_enabled(), this);
425 } else if (button.raw_id() >= Control::mute_button_base_id &&
426 button.raw_id() < Control::mute_button_base_id + 8) {
428 _route->set_mute (!_route->muted(), this);
430 } else if (button.raw_id() >= Control::solo_button_base_id &&
431 button.raw_id() < Control::solo_button_base_id + 8) {
433 _route->set_solo (!_route->soloed(), this);
435 } else if (button.raw_id() >= Control::select_button_base_id &&
436 button.raw_id() < Control::select_button_base_id + 8) {
438 _surface->mcp().select_track (_route);
440 } else if (button.raw_id() >= Control::vselect_button_base_id &&
441 button.raw_id() < Control::vselect_button_base_id + 8) {
446 if (button.name() == "fader_touch") {
448 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
450 bool state = (bs == press);
452 _gain->set_in_use (state);
454 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
456 /* BCF faders don't support touch, so add a timeout to reset
457 their `in_use' state.
460 _surface->mcp().add_in_use_timeout (*_surface, gain(), &fader_touch());
466 Strip::handle_fader (Fader& fader, float position)
468 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader to %1\n", position));
471 _route->gain_control()->set_value (slider_position_to_gain (position));
474 if (ARDOUR::Config->get_mackie_emulation() == "bcf") {
475 /* reset the timeout while we're still moving the fader */
476 _surface->mcp().add_in_use_timeout (*_surface, fader, fader.in_use_touch_control);
479 // must echo bytes back to slider now, because
480 // the notifier only works if the fader is not being
481 // touched. Which it is if we're getting input.
483 _surface->write (fader.set_position (position));
487 Strip::handle_pot (Pot& pot, ControlState& state)
490 _surface->write (pot.set_onoff (false));
494 boost::shared_ptr<Panner> panner = _route->panner_shell()->panner();
495 // pan for mono input routes, or stereo linked panners
497 double p = panner->position ();
499 // calculate new value, and adjust
500 p += state.delta * state.sign;
503 panner->set_position (p);
514 update_automation ();
519 Strip::update_automation ()
521 ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
523 if (gain_state == Touch || gain_state == Play) {
524 notify_gain_changed (false);
527 if (_route->panner()) {
528 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
529 if (panner_state == Touch || panner_state == Play) {
530 notify_panner_changed (false);
536 Strip::update_meter ()
538 float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
539 _surface->write (meter().update_message (dB));
545 MidiByteArray retval;
547 for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
548 retval << (*it)->zero ();
551 retval << blank_display (0);
552 retval << blank_display (1);
558 Strip::blank_display (uint32_t line_number)
560 return display (line_number, string());
564 Strip::display (uint32_t line_number, const std::string& line)
566 assert (line_number <= 1);
568 MidiByteArray retval;
570 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
573 retval << _surface->sysex_hdr();
577 // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
578 retval << (_index * 7 + (line_number * 0x38));
580 // ascii data to display
582 // pad with " " out to 6 chars
583 for (int i = line.length(); i < 6; ++i) {
587 // column spacer, unless it's the right-hand column
595 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));