2 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "ardour/automation_control.h"
20 #include "ardour/gain_control.h"
21 #include "ardour/meter.h"
22 #include "ardour/plugin_insert.h"
23 #include "ardour/session.h"
24 #include "ardour/stripable.h"
25 #include "ardour/track.h"
26 #include "ardour/value_as_string.h"
28 #include "control_protocol/control_protocol.h"
30 #include "fp8_strip.h"
32 using namespace ARDOUR;
33 using namespace ArdourSurface;
34 using namespace ArdourSurface::FP8Types;
36 FP8Strip::FP8Strip (FP8Base& b, uint8_t id)
39 , _solo (b, 0x08 + id)
40 , _mute (b, 0x10 + id)
41 , _selrec (b, 0x18 + id, true)
45 , _displaymode (Stripables)
50 _last_meter = _last_redux = _last_barpos = 0xff;
52 _mute.StateChange.connect_same_thread (_button_connections, boost::bind (&FP8Strip::set_mute, this, _1));
53 _solo.StateChange.connect_same_thread (_button_connections, boost::bind (&FP8Strip::set_solo, this, _1));
54 select_button ().released.connect_same_thread (_button_connections, boost::bind (&FP8Strip::set_select, this));
55 recarm_button ().released.connect_same_thread (_button_connections, boost::bind (&FP8Strip::set_recarm, this));
56 b.Periodic.connect_same_thread (_base_connection, boost::bind (&FP8Strip::periodic, this));
59 FP8Strip::~FP8Strip ()
61 drop_automation_controls ();
62 _base_connection.disconnect ();
63 _button_connections.drop_connections ();
67 FP8Strip::drop_automation_controls ()
69 _fader_connection.disconnect ();
70 _mute_connection.disconnect ();
71 _solo_connection.disconnect ();
72 _rec_connection.disconnect ();
73 _pan_connection.disconnect ();
74 _x_select_connection.disconnect ();
81 _x_select_ctrl.reset ();
84 _select_plugin_functor.clear ();
88 FP8Strip::initialize ()
90 /* this is called once midi transmission is possible,
91 * ie from FaderPort8::connected()
93 _solo.set_active (false);
94 _mute.set_active (false);
96 /* reset momentary button state */
100 drop_automation_controls ();
102 select_button ().set_color (0xffffffff);
103 select_button ().set_active (false);
104 select_button ().set_blinking (false);
106 recarm_button ().set_active (false);
107 recarm_button ().set_color (0xffffffff);
109 set_strip_mode (0, true);
112 _last_line[0].clear ();
113 _last_line[1].clear ();
114 _last_line[2].clear ();
115 _last_line[3].clear ();
116 _base.tx_sysex (4, 0x12, _id, 0x00, 0x00);
117 _base.tx_sysex (4, 0x12, _id, 0x01, 0x00);
118 _base.tx_sysex (4, 0x12, _id, 0x02, 0x00);
119 _base.tx_sysex (4, 0x12, _id, 0x03, 0x00);
121 set_bar_mode (4); // off
123 _base.tx_midi2 (0xd0 + _id, 0); // reset meter
124 _base.tx_midi2 (0xd8 + _id, 0); // reset redux
126 _base.tx_midi3 (0xe0 + _id, 0, 0); // fader
128 /* clear cached values */
130 _last_meter = _last_redux = _last_barpos = 0xff;
134 #define GENERATE_SET_CTRL_FUNCTION(NAME) \
136 FP8Strip::set_ ##NAME##_controllable (boost::shared_ptr<AutomationControl> ac) \
138 if (_##NAME##_ctrl == ac) { \
141 _##NAME##_connection.disconnect(); \
142 _##NAME##_ctrl = ac; \
145 ac->Changed.connect (_##NAME##_connection, MISSING_INVALIDATOR, \
146 boost::bind (&FP8Strip::notify_##NAME##_changed, this), fp8_context()); \
148 notify_##NAME##_changed (); \
152 GENERATE_SET_CTRL_FUNCTION (fader)
153 GENERATE_SET_CTRL_FUNCTION (mute)
154 GENERATE_SET_CTRL_FUNCTION (solo)
155 GENERATE_SET_CTRL_FUNCTION (rec)
156 GENERATE_SET_CTRL_FUNCTION (pan)
157 GENERATE_SET_CTRL_FUNCTION (x_select)
159 #undef GENERATE_SET_CTRL_FUNCTION
161 // special case -- w/_select_plugin_functor
163 FP8Strip::set_select_controllable (boost::shared_ptr<AutomationControl> ac)
165 _select_plugin_functor.clear ();
166 set_x_select_controllable (ac);
170 FP8Strip::set_select_cb (boost::function<void ()>& functor)
172 set_select_controllable (boost::shared_ptr<AutomationControl>());
173 _select_plugin_functor = functor;
177 FP8Strip::unset_controllables (int which)
179 _peak_meter = boost::shared_ptr<ARDOUR::PeakMeter>();
180 _redux_ctrl = boost::shared_ptr<ARDOUR::ReadOnlyControl>();
182 if (which & CTRL_FADER) {
183 set_fader_controllable (boost::shared_ptr<AutomationControl>());
185 if (which & CTRL_MUTE) {
186 set_mute_controllable (boost::shared_ptr<AutomationControl>());
188 if (which & CTRL_SOLO) {
189 set_solo_controllable (boost::shared_ptr<AutomationControl>());
191 if (which & CTRL_REC) {
192 set_rec_controllable (boost::shared_ptr<AutomationControl>());
194 if (which & CTRL_PAN) {
195 set_pan_controllable (boost::shared_ptr<AutomationControl>());
197 if (which & CTRL_SELECT) {
198 set_select_controllable (boost::shared_ptr<AutomationControl>());
199 select_button ().set_color (0xffffffff);
200 select_button ().set_active (false);
201 select_button ().set_blinking (false);
203 if (which & CTRL_TEXT0) {
204 set_text_line (0x00, "");
206 if (which & CTRL_TEXT1) {
207 set_text_line (0x01, "");
209 if (which & CTRL_TEXT2) {
210 set_text_line (0x02, "");
212 if (which & CTRL_TEXT3) {
213 set_text_line (0x03, "");
215 set_bar_mode (4); // Off
219 FP8Strip::set_stripable (boost::shared_ptr<Stripable> s, bool panmode)
224 set_fader_controllable (s->pan_azimuth_control ());
226 set_fader_controllable (s->gain_control ());
228 set_pan_controllable (s->pan_azimuth_control ());
230 if (s->is_monitor ()) {
231 set_mute_controllable (boost::shared_ptr<AutomationControl>());
233 set_mute_controllable (s->mute_control ());
235 set_solo_controllable (s->solo_control ());
237 if (boost::dynamic_pointer_cast<Track> (s)) {
238 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track>(s);
239 set_rec_controllable (t->rec_enable_control ());
240 recarm_button ().set_color (0xff0000ff);
242 set_rec_controllable (boost::shared_ptr<AutomationControl>());
243 recarm_button ().set_color (0xffffffff);
244 recarm_button ().set_active (false);
246 _peak_meter = s->peak_meter ();
247 _redux_ctrl = s->comp_redux_controllable ();
249 set_select_controllable (boost::shared_ptr<AutomationControl>());
250 select_button ().set_active (s->is_selected ());
251 select_button ().set_color (s->presentation_info ().color());
252 //select_button ().set_blinking (false);
254 set_strip_mode (0x05);
255 set_text_line (0x00, s->name ());
256 set_text_line (0x01, _pan_ctrl ? _pan_ctrl->get_user_string () : "");
257 set_text_line (0x02, "");
258 set_text_line (0x03, "");
261 /* *****************************************************************************
262 * Parse Strip Specifig MIDI Events
266 FP8Strip::midi_touch (bool t)
269 boost::shared_ptr<AutomationControl> ac = _fader_ctrl;
274 if (!ac->touching ()) {
275 ac->start_touch (ac->session().transport_frame());
278 ac->stop_touch (true, ac->session().transport_frame());
284 FP8Strip::midi_fader (float val)
286 assert (val >= 0.f && val <= 1.f);
290 boost::shared_ptr<AutomationControl> ac = _fader_ctrl;
294 if (ac->automation_state() == Touch && !ac->touching ()) {
295 ac->start_touch (ac->session().transport_frame());
297 ac->set_value (ac->interface_to_internal (val), group_mode ());
301 /* *****************************************************************************
302 * Actions from Controller, Update Model
305 PBD::Controllable::GroupControlDisposition
306 FP8Strip::group_mode () const
308 if (_base.shift_mod ()) {
309 return PBD::Controllable::InverseGroup;
311 return PBD::Controllable::UseGroup;
316 FP8Strip::set_mute (bool on)
319 if (_mute_ctrl->automation_state() == Touch && !_mute_ctrl->touching ()) {
320 _mute_ctrl->start_touch (_mute_ctrl->session().transport_frame());
322 _mute_ctrl->set_value (on ? 1.0 : 0.0, group_mode ());
327 FP8Strip::set_solo (bool on)
330 if (_solo_ctrl->automation_state() == Touch && !_solo_ctrl->touching ()) {
331 _solo_ctrl->start_touch (_solo_ctrl->session().transport_frame());
333 _solo_ctrl->set_value (on ? 1.0 : 0.0, group_mode ());
338 FP8Strip::set_recarm ()
341 const bool on = !recarm_button ().is_active();
342 _rec_ctrl->set_value (on ? 1.0 : 0.0, group_mode ());
347 FP8Strip::set_select ()
349 if (!_select_plugin_functor.empty ()) {
350 assert (!_x_select_ctrl);
351 _select_plugin_functor ();
352 } else if (_x_select_ctrl) {
353 if (_x_select_ctrl->automation_state() == Touch && !_x_select_ctrl->touching ()) {
354 _x_select_ctrl->start_touch (_x_select_ctrl->session().transport_frame());
356 const bool on = !select_button ().is_active();
357 _x_select_ctrl->set_value (on ? 1.0 : 0.0, group_mode ());
361 /* *****************************************************************************
362 * Callbacks from Stripable, Update View
366 FP8Strip::notify_fader_changed ()
368 boost::shared_ptr<AutomationControl> ac = _fader_ctrl;
374 val = ac->internal_to_interface (ac->get_value());
375 val = std::max (0.f, std::min (1.f, val)) * 16368.f; /* 16 * 1023 */
377 unsigned short mv = lrintf (val);
378 if (mv == _last_fader) {
382 _base.tx_midi3 (0xe0 + _id, (mv & 0x7f), (mv >> 7) & 0x7f);
386 FP8Strip::notify_solo_changed ()
389 _solo.set_active (_solo_ctrl->get_value () > 0);
391 _solo.set_active (false);
396 FP8Strip::notify_mute_changed ()
399 _mute.set_active (_mute_ctrl->get_value () > 0);
401 _mute.set_active (false);
406 FP8Strip::notify_rec_changed ()
409 recarm_button ().set_active (_rec_ctrl->get_value() > 0.);
411 recarm_button ().set_active (false);
416 FP8Strip::notify_pan_changed ()
422 FP8Strip::notify_x_select_changed ()
424 if (!_select_plugin_functor.empty ()) {
425 assert (!_x_select_ctrl);
429 if (_x_select_ctrl) {
430 assert (_select_plugin_functor.empty ());
431 select_button ().set_active (_x_select_ctrl->get_value() > 0.);
432 select_button ().set_color (0xffff00ff);
433 select_button ().set_blinking (false);
439 /* *****************************************************************************
440 * Periodic View Updates
444 FP8Strip::periodic_update_fader ()
446 boost::shared_ptr<AutomationControl> ac = _fader_ctrl;
447 if (!ac || _touching) {
451 ARDOUR::AutoState state = ac->automation_state();
452 if (state == Touch || state == Play) {
453 notify_fader_changed ();
458 FP8Strip::set_periodic_display_mode (DisplayMode m) {
460 if (_displaymode == SendDisplay || _displaymode == PluginParam) {
461 // need to change to 4 lines before calling set_text()
462 set_strip_mode (2); // 4 lines of small text
467 FP8Strip::periodic_update_meter ()
469 bool have_meter = false;
470 bool have_panner = false;
474 float dB = _peak_meter->meter_level (0, MeterMCP);
475 // TODO: deflect meter
476 int val = std::min (127.f, std::max (0.f, 2.f * dB + 127.f));
477 if (val != _last_meter || val > 0) {
478 _base.tx_midi2 (0xd0 + _id, val & 0x7f); // falls off automatically
483 if (0 != _last_meter) {
484 _base.tx_midi2 (0xd0 + _id, 0);
489 // show redux only if there's a meter, too (strip display mode 5)
490 if (_peak_meter && _redux_ctrl) {
491 float rx = (1.f - _redux_ctrl->get_parameter ()) * 127.f;
492 // TODO: deflect redux
493 int val = std::min (127.f, std::max (0.f, rx));
494 if (val != _last_redux) {
495 _base.tx_midi2 (0xd8 + _id, val & 0x7f);
499 if (0 != _last_redux) {
500 _base.tx_midi2 (0xd8 + _id, 0);
505 if (_displaymode == PluginParam) {
507 set_bar_mode (2); // Fill
508 set_text_line (0x01, value_as_string(_fader_ctrl->desc(), _fader_ctrl->get_value()));
509 float barpos = _fader_ctrl->internal_to_interface (_fader_ctrl->get_value());
510 int val = std::min (127.f, std::max (0.f, barpos * 128.f));
511 if (val != _last_barpos) {
512 _base.tx_midi3 (0xb0, 0x30 + _id, val & 0x7f);
516 set_bar_mode (4); // Off
517 set_text_line (0x01, "");
520 else if (_displaymode == SendDisplay) {
521 set_bar_mode (4); // Off
523 set_text_line (0x01, value_as_string(_fader_ctrl->desc(), _fader_ctrl->get_value()));
525 set_text_line (0x01, "");
527 } else if (_pan_ctrl) {
529 float panpos = _pan_ctrl->internal_to_interface (_pan_ctrl->get_value());
530 int val = std::min (127.f, std::max (0.f, panpos * 128.f));
531 set_bar_mode (1); // Bipolar
532 if (val != _last_barpos) {
533 _base.tx_midi3 (0xb0, 0x30 + _id, val & 0x7f);
536 set_text_line (0x01, _pan_ctrl->get_user_string ());
538 set_bar_mode (4); // Off
541 if (_displaymode == SendDisplay || _displaymode == PluginParam) {
542 set_strip_mode (2); // 4 lines of small text + value-bar
544 else if (have_meter && have_panner) {
545 set_strip_mode (5); // small meters + 3 lines of text (3rd is large) + value-bar
547 else if (have_meter) {
548 set_strip_mode (4); // big meters + 3 lines of text (3rd line is large)
550 else if (have_panner) {
551 set_strip_mode (0); // 3 lines of text (3rd line is large) + value-bar
553 set_strip_mode (0); // 3 lines of text (3rd line is large) + value-bar
558 FP8Strip::set_strip_mode (uint8_t strip_mode, bool clear)
560 if (strip_mode == _strip_mode && !clear) {
563 _strip_mode = strip_mode;
564 _base.tx_sysex (3, 0x13, _id, (_strip_mode & 0x07) | (clear ? 0x10 : 0));
565 //_base.tx_midi3 (0xb0, 0x38 + _id, _bar_mode);
569 FP8Strip::set_bar_mode (uint8_t bar_mode)
571 if (bar_mode == _bar_mode) {
574 _bar_mode = bar_mode;
575 _base.tx_midi3 (0xb0, 0x38 + _id, bar_mode);
579 FP8Strip::set_text_line (uint8_t line, std::string const& txt, bool inv)
582 if (_last_line[line] == txt) {
585 _base.tx_text (_id, line, inv ? 0x04 : 0x00, txt);
586 _last_line[line] = txt;
590 FP8Strip::periodic_update_timecode ()
592 if (_id >= 2 && _id < 6) {
593 std::string const& tc = _base.timecode();
596 if (tc.size () == 12) {
597 t = tc.substr (1 + (_id - 2) * 3, 2);
599 set_text_line (0x02, t);
604 FP8Strip::periodic ()
606 periodic_update_fader ();
607 periodic_update_meter ();
608 if (_displaymode != PluginSelect) {
609 periodic_update_timecode ();