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/rc_configuration.h"
36 #include "ardour/meter.h"
38 #include "mackie_control_protocol.h"
39 #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);
113 } else if (control.type() == Control::type_led || control.type() == Control::type_led_ring) {
117 os << "Strip::add: unknown control type " << control;
118 throw MackieControlException (os.str());
126 throw MackieControlException ("gain is null");
135 throw MackieControlException ("vpot is null");
143 if (_recenable == 0) {
144 throw MackieControlException ("recenable is null");
153 throw MackieControlException ("solo is null");
161 throw MackieControlException ("mute is null");
170 throw MackieControlException ("select is null");
179 throw MackieControlException ("vselect is null");
187 if (_fader_touch == 0) {
188 throw MackieControlException ("fader_touch is null");
190 return *_fader_touch;
197 throw MackieControlException ("meter is null");
202 std::ostream & Mackie::operator << (std::ostream & os, const Strip & strip)
204 os << typeid (strip).name();
206 os << "has_solo: " << boolalpha << strip.has_solo();
208 os << "has_recenable: " << boolalpha << strip.has_recenable();
210 os << "has_mute: " << boolalpha << strip.has_mute();
212 os << "has_select: " << boolalpha << strip.has_select();
214 os << "has_vselect: " << boolalpha << strip.has_vselect();
216 os << "has_fader_touch: " << boolalpha << strip.has_fader_touch();
218 os << "has_vpot: " << boolalpha << strip.has_vpot();
220 os << "has_gain: " << boolalpha << strip.has_gain();
227 Strip::set_route (boost::shared_ptr<Route> r)
229 route_connections.drop_connections ();
235 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
236 _surface->number(), _index, _route->name()));
240 _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
243 _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
247 _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
250 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
252 if (_route->pannable()) {
253 _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
254 _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
257 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
260 trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
263 // TODO this works when a currently-banked route is made inactive, but not
264 // when a route is activated which should be currently banked.
266 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
267 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
271 // RemoteControlIDChanged. Better handled at Session level.
277 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
278 _surface->number(), _index));
286 notify_solo_changed ();
290 notify_mute_changed ();
294 notify_gain_changed ();
297 notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
300 notify_panner_changed ();
303 if (has_recenable()) {
304 notify_record_enable_changed ();
309 Strip::notify_solo_changed ()
312 Button& button = solo();
313 _surface->write (builder.build_led (button, _route->soloed()));
318 Strip::notify_mute_changed ()
321 Button & button = mute();
322 _surface->write (builder.build_led (button, _route->muted()));
327 Strip::notify_record_enable_changed ()
330 Button & button = recenable();
331 _surface->write (builder.build_led (button, _route->record_enabled()));
336 Strip::notify_active_changed ()
338 _surface->mcp().refresh_current_bank();
342 Strip::notify_route_deleted ()
344 _surface->mcp().refresh_current_bank();
348 Strip::notify_gain_changed (bool force_update)
351 Fader & fader = gain();
353 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("route %1 gain change, update fader %2 on port %3 in-use ? %4\n",
356 _surface->port().output_port().name(),
358 if (!fader.in_use()) {
359 float gain_value = gain_to_slider_position (_route->gain_control()->get_value());
360 // check that something has actually changed
361 if (force_update || gain_value != _last_gain_written) {
362 _surface->write (builder.build_fader (fader, gain_value));
363 _last_gain_written = gain_value;
365 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader not updated because gain still equals %1\n", gain_value));
372 Strip::notify_property_changed (const PropertyChange& what_changed)
374 if (!what_changed.contains (ARDOUR::Properties::name)) {
380 string fullname = _route->name();
382 if (fullname.length() <= 6) {
385 line1 = PBD::short_version (fullname, 6);
388 _surface->write (builder.strip_display (*_surface, *this, 0, line1));
389 _surface->write (builder.strip_display_blank (*_surface, *this, 1));
394 Strip::notify_panner_changed (bool force_update)
398 boost::shared_ptr<Panner> panner = _route->panner();
400 double pos = panner->position ();
402 // cache the MidiByteArray here, because the mackie led control is much lower
403 // resolution than the panner control. So we save lots of byte
404 // sends in spite of more work on the comparison
405 MidiByteArray bytes = builder.build_led_ring (pot, ControlState (on, pos), MackieMidiBuilder::midi_pot_mode_dot);
406 // check that something has actually changed
407 if (force_update || bytes != _last_pan_written)
409 _surface->write (bytes);
410 _last_pan_written = bytes;
413 _surface->write (builder.zero_control (pot));
419 Strip::handle_button (SurfacePort & port, Control & control, ButtonState bs)
422 // no route so always switch the light off
423 // because no signals will be emitted by a non-route
424 _surface->write (builder.build_led (control.led(), off));
431 if (control.name() == "recenable") {
432 state = !_route->record_enabled();
433 _route->set_record_enabled (state, this);
434 } else if (control.name() == "mute") {
435 state = !_route->muted();
436 _route->set_mute (state, this);
437 } else if (control.name() == "solo") {
438 state = !_route->soloed();
439 _route->set_solo (state, this);
440 } else if (control.name() == "select") {
441 _surface->mcp().select_track (_route);
442 } else if (control.name() == "vselect") {
443 // TODO could be used to select different things to apply the pot to?
444 //state = default_button_press (dynamic_cast<Button&> (control));
448 if (control.name() == "fader_touch") {
450 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
452 state = (bs == press);
454 gain().set_in_use (state);
456 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
458 /* BCF faders don't support touch, so add a timeout to reset
459 their `in_use' state.
462 _surface->mcp().add_in_use_timeout (*_surface, gain(), &fader_touch());
476 update_automation ();
481 Strip::update_automation ()
483 ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
485 if (gain_state == Touch || gain_state == Play) {
486 notify_gain_changed (false);
489 if (_route->panner()) {
490 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
491 if (panner_state == Touch || panner_state == Play) {
492 notify_panner_changed (false);
498 Strip::update_meter ()
500 float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
501 _surface->write (meter().update_message (dB));