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 midi_ui_context() ARDOUR::MidiControlUI::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(x) __invalidator (*(MidiControlUI::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 ();
236 _route->solo_control()->Changed.connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_solo_changed, this), midi_ui_context());
239 _route->mute_control()->Changed.connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_mute_changed, this), midi_ui_context());
243 _route->gain_control()->Changed.connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_gain_changed, this, false), midi_ui_context());
246 _route->PropertyChanged.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_property_changed, this, _1), midi_ui_context());
248 if (_route->pannable()) {
249 _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_panner_changed, this, false), midi_ui_context());
250 _route->pannable()->pan_width_control->Changed.connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_panner_changed, this, false), midi_ui_context());
253 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
256 trk->rec_enable_control()->Changed .connect(route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_record_enable_changed, this), midi_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, MISSING_INVALIDATOR, ui_bind (&Strip::notify_active_changed, this), midi_ui_context());
263 _route->DropReferences.connect (route_connections, MISSING_INVALIDATOR, ui_bind (&Strip::notify_route_deleted, this), midi_ui_context());
267 // RemoteControlIDChanged. Better handled at Session level.
279 notify_solo_changed ();
283 notify_mute_changed ();
287 notify_gain_changed ();
290 notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
293 notify_panner_changed ();
296 if (has_recenable()) {
297 notify_record_enable_changed ();
302 Strip::notify_solo_changed ()
305 Button& button = solo();
306 _surface->write (builder.build_led (button, _route->soloed()));
311 Strip::notify_mute_changed ()
314 Button & button = mute();
315 _surface->write (builder.build_led (button, _route->muted()));
320 Strip::notify_record_enable_changed ()
323 Button & button = recenable();
324 _surface->write (builder.build_led (button, _route->record_enabled()));
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();
345 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("route %1 gain change, update fader %2 on port %3\n",
348 _surface->port().output_port().name()));
349 if (!fader.in_use()) {
350 float gain_value = gain_to_slider_position (_route->gain_control()->get_value());
351 // check that something has actually changed
352 if (force_update || gain_value != _last_gain_written) {
353 _surface->write (builder.build_fader (fader, gain_value));
354 _last_gain_written = gain_value;
361 Strip::notify_property_changed (const PropertyChange& what_changed)
363 if (!what_changed.contains (ARDOUR::Properties::name)) {
369 string fullname = _route->name();
371 if (fullname.length() <= 6) {
374 line1 = PBD::short_version (fullname, 6);
377 _surface->write (builder.strip_display (*_surface, *this, 0, line1));
378 _surface->write (builder.strip_display_blank (*_surface, *this, 1));
383 Strip::notify_panner_changed (bool force_update)
387 boost::shared_ptr<Panner> panner = _route->panner();
389 double pos = panner->position ();
391 // cache the MidiByteArray here, because the mackie led control is much lower
392 // resolution than the panner control. So we save lots of byte
393 // sends in spite of more work on the comparison
394 MidiByteArray bytes = builder.build_led_ring (pot, ControlState (on, pos), MackieMidiBuilder::midi_pot_mode_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 (builder.zero_control (pot));
408 Strip::handle_button (SurfacePort & port, Control & control, ButtonState bs)
411 // no route so always switch the light off
412 // because no signals will be emitted by a non-route
413 _surface->write (builder.build_led (control.led(), off));
420 if (control.name() == "recenable") {
421 state = !_route->record_enabled();
422 _route->set_record_enabled (state, this);
423 } else if (control.name() == "mute") {
424 state = !_route->muted();
425 _route->set_mute (state, this);
426 } else if (control.name() == "solo") {
427 state = !_route->soloed();
428 _route->set_solo (state, this);
429 } else if (control.name() == "select") {
430 _surface->mcp().select_track (_route);
431 } else if (control.name() == "vselect") {
432 // TODO could be used to select different things to apply the pot to?
433 //state = default_button_press (dynamic_cast<Button&> (control));
437 if (control.name() == "fader_touch") {
439 state = (bs == press);
441 gain().set_in_use (state);
443 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
445 /* BCF faders don't support touch, so add a timeout to reset
446 their `in_use' state.
449 _surface->mcp().add_in_use_timeout (*_surface, gain(), &fader_touch());
463 update_automation ();
468 Strip::update_automation ()
470 ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
472 if (gain_state == Touch || gain_state == Play) {
473 notify_gain_changed (false);
476 if (_route->panner()) {
477 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
478 if (panner_state == Touch || panner_state == Play) {
479 notify_panner_changed (false);
485 Strip::update_meter ()
487 float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
488 _surface->write (meter().update_message (dB));