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"
48 using namespace Mackie;
50 using namespace ARDOUR;
53 #define ui_context() MackieControlProtocol::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
54 #define ui_bind(f, ...) boost::protect (boost::bind (f, __VA_ARGS__))
56 extern PBD::EventLoop::InvalidationRecord* __invalidator (sigc::trackable& trackable, const char*, int);
57 #define invalidator() __invalidator (*(MackieControlProtocol::instance()), __FILE__, __LINE__)
59 Strip::Strip (Surface& s, const std::string& name, int index, StripControlDefinition* ctls)
72 /* build the controls for this track, which will automatically add them
76 for (uint32_t i = 0; ctls[i].name[0]; ++i) {
77 ctls[i].factory (*_surface, ctls[i].base_id + index, ctls[i].name, *this);
87 TODO could optimise this to use enum, but it's only
88 called during the protocol class instantiation.
90 void Strip::add (Control & control)
94 if (control.name() == "gain") {
95 _gain = reinterpret_cast<Fader*>(&control);
96 } else if (control.name() == "vpot") {
97 _vpot = reinterpret_cast<Pot*>(&control);
98 } else if (control.name() == "recenable") {
99 _recenable = reinterpret_cast<Button*>(&control);
100 } else if (control.name() == "solo") {
101 _solo = reinterpret_cast<Button*>(&control);
102 } else if (control.name() == "mute") {
103 _mute = reinterpret_cast<Button*>(&control);
104 } else if (control.name() == "select") {
105 _select = reinterpret_cast<Button*>(&control);
106 } else if (control.name() == "vselect") {
107 _vselect = reinterpret_cast<Button*>(&control);
108 } else if (control.name() == "fader_touch") {
109 _fader_touch = reinterpret_cast<Button*>(&control);
110 } else if (control.name() == "meter") {
111 _meter = reinterpret_cast<Meter*>(&control);
121 throw MackieControlException ("gain is null");
130 throw MackieControlException ("vpot is null");
138 if (_recenable == 0) {
139 throw MackieControlException ("recenable is null");
148 throw MackieControlException ("solo is null");
156 throw MackieControlException ("mute is null");
165 throw MackieControlException ("select is null");
174 throw MackieControlException ("vselect is null");
182 if (_fader_touch == 0) {
183 throw MackieControlException ("fader_touch is null");
185 return *_fader_touch;
192 throw MackieControlException ("meter is null");
197 std::ostream & Mackie::operator << (std::ostream & os, const Strip & strip)
199 os << typeid (strip).name();
201 os << "has_solo: " << boolalpha << strip.has_solo();
203 os << "has_recenable: " << boolalpha << strip.has_recenable();
205 os << "has_mute: " << boolalpha << strip.has_mute();
207 os << "has_select: " << boolalpha << strip.has_select();
209 os << "has_vselect: " << boolalpha << strip.has_vselect();
211 os << "has_fader_touch: " << boolalpha << strip.has_fader_touch();
213 os << "has_vpot: " << boolalpha << strip.has_vpot();
215 os << "has_gain: " << boolalpha << strip.has_gain();
222 Strip::set_route (boost::shared_ptr<Route> r)
224 route_connections.drop_connections ();
230 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now mapping route %3\n",
231 _surface->number(), _index, _route->name()));
235 _route->solo_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_solo_changed, this), ui_context());
238 _route->mute_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_mute_changed, this), ui_context());
242 _route->gain_control()->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_gain_changed, this, false), ui_context());
245 _route->PropertyChanged.connect (route_connections, invalidator(), ui_bind (&Strip::notify_property_changed, this, _1), ui_context());
247 if (_route->pannable()) {
248 _route->pannable()->pan_azimuth_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
249 _route->pannable()->pan_width_control->Changed.connect(route_connections, invalidator(), ui_bind (&Strip::notify_panner_changed, this, false), ui_context());
252 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<ARDOUR::Track>(_route);
255 trk->rec_enable_control()->Changed .connect(route_connections, invalidator(), ui_bind (&Strip::notify_record_enable_changed, this), ui_context());
258 // TODO this works when a currently-banked route is made inactive, but not
259 // when a route is activated which should be currently banked.
261 _route->active_changed.connect (route_connections, invalidator(), ui_bind (&Strip::notify_active_changed, this), ui_context());
262 _route->DropReferences.connect (route_connections, invalidator(), ui_bind (&Strip::notify_route_deleted, this), ui_context());
266 // RemoteControlIDChanged. Better handled at Session level.
272 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Surface %1 strip %2 now unmapped\n",
273 _surface->number(), _index));
281 notify_solo_changed ();
285 notify_mute_changed ();
289 notify_gain_changed ();
292 notify_property_changed (PBD::PropertyChange (ARDOUR::Properties::name));
295 notify_panner_changed ();
298 if (has_recenable()) {
299 notify_record_enable_changed ();
304 Strip::notify_solo_changed ()
306 if (_route && _solo) {
307 _surface->write (_solo->led().set_state (_route->soloed() ? on : off));
312 Strip::notify_mute_changed ()
314 if (_route && _mute) {
315 _surface->write (_mute->led().set_state (_route->muted() ? on : off));
320 Strip::notify_record_enable_changed ()
322 if (_route && _recenable) {
323 _surface->write (_recenable->led().set_state (_route->record_enabled() ? on : off));
328 Strip::notify_active_changed ()
330 _surface->mcp().refresh_current_bank();
334 Strip::notify_route_deleted ()
336 _surface->mcp().refresh_current_bank();
340 Strip::notify_gain_changed (bool force_update)
343 Fader & fader = gain();
345 if (!fader.in_use()) {
346 float position = gain_to_slider_position (_route->gain_control()->get_value());
347 // check that something has actually changed
348 if (force_update || position != _last_gain_written) {
349 _surface->write (fader.set_position (position));
350 _last_gain_written = position;
357 Strip::notify_property_changed (const PropertyChange& what_changed)
359 if (!what_changed.contains (ARDOUR::Properties::name)) {
365 string fullname = _route->name();
367 if (fullname.length() <= 6) {
370 line1 = PBD::short_version (fullname, 6);
373 _surface->write (display (0, line1));
374 _surface->write (blank_display (1));
379 Strip::notify_panner_changed (bool force_update)
383 boost::shared_ptr<Panner> panner = _route->panner();
386 double pos = panner->position ();
388 // cache the MidiByteArray here, because the mackie led control is much lower
389 // resolution than the panner control. So we save lots of byte
390 // sends in spite of more work on the comparison
392 MidiByteArray bytes = pot.set_all (pos, true, Pot::dot);
394 // check that something has actually changed
395 if (force_update || bytes != _last_pan_written)
397 _surface->write (bytes);
398 _last_pan_written = bytes;
401 _surface->write (pot.zero());
407 Strip::handle_button (SurfacePort & port, Control & control, ButtonState bs)
409 Button* button = dynamic_cast<Button*>(&control);
416 // no route so always switch the light off
417 // because no signals will be emitted by a non-route
418 _surface->write (button->led().set_state (off));
425 if (control.name() == "recenable") {
426 state = !_route->record_enabled();
427 _route->set_record_enabled (state, this);
428 } else if (control.name() == "mute") {
429 state = !_route->muted();
430 _route->set_mute (state, this);
431 } else if (control.name() == "solo") {
432 state = !_route->soloed();
433 _route->set_solo (state, this);
434 } else if (control.name() == "select") {
435 _surface->mcp().select_track (_route);
436 } else if (control.name() == "vselect") {
437 // TODO could be used to select different things to apply the pot to?
438 //state = default_button_press (dynamic_cast<Button&> (control));
442 if (control.name() == "fader_touch") {
444 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("fader touch, press ? %1\n", (bs == press)));
446 state = (bs == press);
448 gain().set_in_use (state);
450 if (ARDOUR::Config->get_mackie_emulation() == "bcf" && state) {
452 /* BCF faders don't support touch, so add a timeout to reset
453 their `in_use' state.
456 _surface->mcp().add_in_use_timeout (*_surface, gain(), &fader_touch());
470 update_automation ();
475 Strip::update_automation ()
477 ARDOUR::AutoState gain_state = _route->gain_control()->automation_state();
479 if (gain_state == Touch || gain_state == Play) {
480 notify_gain_changed (false);
483 if (_route->panner()) {
484 ARDOUR::AutoState panner_state = _route->panner()->automation_state();
485 if (panner_state == Touch || panner_state == Play) {
486 notify_panner_changed (false);
492 Strip::update_meter ()
494 float dB = const_cast<PeakMeter&> (_route->peak_meter()).peak_power (0);
495 _surface->write (meter().update_message (dB));
501 MidiByteArray retval;
503 for (Group::Controls::const_iterator it = _controls.begin(); it != _controls.end(); ++it) {
504 retval << (*it)->zero ();
507 retval << blank_display (0);
508 retval << blank_display (1);
514 Strip::blank_display (uint32_t line_number)
516 return display (line_number, string());
520 Strip::display (uint32_t line_number, const std::string& line)
522 assert (line_number <= 1);
524 MidiByteArray retval;
526 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("strip_display index: %1, line %2 = %3\n", _index, line_number, line));
529 retval << _surface->sysex_hdr();
533 // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
534 retval << (_index * 7 + (line_number * 0x38));
536 // ascii data to display
538 // pad with " " out to 6 chars
539 for (int i = line.length(); i < 6; ++i) {
543 // column spacer, unless it's the right-hand column
551 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));