0d95b3cdc75c6eeca10e84cd3324ee8cc8015a68
[ardour.git] / libs / surfaces / faderport8 / actions.cc
1 /* Faderport 8 Control Surface
2  * This is the button "Controller" of the MVC surface inteface,
3  * see callbacks.cc for the "View".
4  *
5  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22 #include "ardour/dB.h"
23 #include "ardour/session.h"
24 #include "ardour/session_configuration.h"
25 #include "ardour/types.h"
26
27 #include "gtkmm2ext/actions.h"
28
29 #include "faderport8.h"
30
31 #include "pbd/i18n.h"
32
33 using namespace ARDOUR;
34 using namespace ArdourSurface;
35 using namespace std;
36 using namespace ArdourSurface::FP8Types;
37
38 #define BindMethod(ID, CB) \
39         _ctrls.button (FP8Controls::ID).released.connect_same_thread (button_connections, boost::bind (&FaderPort8:: CB, this));
40
41 #define BindFunction(ID, ACT, CB, ...) \
42         _ctrls.button (FP8Controls::ID). ACT .connect_same_thread (button_connections, boost::bind (&FaderPort8:: CB, this, __VA_ARGS__));
43
44 #define BindAction(ID, GRP, ITEM) \
45         _ctrls.button (FP8Controls::ID).released.connect_same_thread (button_connections, boost::bind (&FaderPort8::button_action, this, GRP, ITEM));
46
47 #define BindUserAction(ID) \
48         _ctrls.button (ID).pressed.connect_same_thread (button_connections, boost::bind (&FaderPort8::button_user, this, true, ID)); \
49 _ctrls.button (ID).released.connect_same_thread (button_connections, boost::bind (&FaderPort8::button_user, this, false, ID));
50
51 void
52 FaderPort8::setup_actions ()
53 {
54         BindMethod (BtnPlay, button_play);
55         BindMethod (BtnStop, button_stop);
56         BindMethod (BtnLoop, button_loop);
57         BindMethod (BtnRecord, button_record);
58         BindMethod (BtnClick, button_metronom);
59         BindAction (BtnRedo, "Editor", "redo");
60
61         BindAction (BtnSave, "Common", "Save");
62         BindAction (BtnUndo, "Editor", "undo");
63         BindAction (BtnRedo, "Editor", "redo");
64
65         BindAction (BtnSoloClear, "Main", "cancel-solo");
66         BindMethod (BtnMuteClear, button_mute_clear);
67
68         BindMethod (FP8Controls::BtnArmAll, button_arm_all);
69
70         BindFunction (BtnRewind, pressed, button_varispeed, false);
71         BindFunction (BtnFastForward, pressed, button_varispeed, true);
72
73         BindFunction (BtnPrev, released, button_prev_next, false);
74         BindFunction (BtnNext, released, button_prev_next, true);
75
76         BindFunction (BtnArm, pressed, button_arm, true);
77         BindFunction (BtnArm, released, button_arm, false);
78
79         BindFunction (BtnAOff, released, button_automation, ARDOUR::Off);
80         BindFunction (BtnATouch, released, button_automation, ARDOUR::Touch);
81         BindFunction (BtnARead, released, button_automation, ARDOUR::Play);
82         BindFunction (BtnAWrite, released, button_automation, ARDOUR::Write);
83
84         _ctrls.button (FP8Controls::BtnEncoder).pressed.connect_same_thread (button_connections, boost::bind (&FaderPort8::button_encoder, this));
85         _ctrls.button (FP8Controls::BtnParam).pressed.connect_same_thread (button_connections, boost::bind (&FaderPort8::button_parameter, this));
86
87
88         BindAction (BtnBypass, "Mixer", "ab-plugins");
89         BindAction (BtnBypassAll, "Mixer", "ab-plugins"); // XXX
90
91         BindAction (BtnMacro, "Mixer", "show-editor");
92         BindAction (BtnLink, "Window", "show-mixer");
93
94         BindAction (BtnOpen, "Common", "addExistingAudioFiles");
95         BindAction (BtnLock, "Editor", "lock");
96
97         // user-specific
98         for (FP8Controls::UserButtonMap::const_iterator i = _ctrls.user_buttons ().begin ();
99                         i != _ctrls.user_buttons ().end (); ++i) {
100                 BindUserAction ((*i).first);
101         }
102 }
103
104 void
105 FaderPort8::button_play ()
106 {
107         if (session->transport_rolling ()) {
108                 if (session->transport_speed () != 1.0) {
109                         session->request_transport_speed (1.0);
110                 } else {
111                         transport_stop ();
112                 }
113         } else {
114                 transport_play ();
115         }
116 }
117
118 void
119 FaderPort8::button_stop ()
120 {
121         transport_stop ();
122 }
123
124 void
125 FaderPort8::button_record ()
126 {
127         set_record_enable (!get_record_enabled ());
128 }
129
130 void
131 FaderPort8::button_loop ()
132 {
133         loop_toggle ();
134 }
135
136 void
137 FaderPort8::button_metronom ()
138 {
139         Config->set_clicking (!Config->get_clicking ());
140 }
141
142 void
143 FaderPort8::button_automation (ARDOUR::AutoState as)
144 {
145         FaderMode fadermode = _ctrls.fader_mode ();
146         switch (fadermode) {
147                 case ModePlugins:
148 #if 0 // Plugin Control Automation Mode
149                         for ( std::list <ProcessorCtrl>::iterator i = _proc_params.begin(); i != _proc_params.end(); ++i) {
150                                 ((*i).ac)->set_automation_state (as);
151                         }
152 #endif
153                         return;
154                 case ModeSend:
155                         if (first_selected_stripable()) {
156 #if 0 // Send Level Automation
157                                 boost::shared_ptr<Stripable> s = first_selected_stripable();
158                                 boost::shared_ptr<AutomationControl> send;
159                                 uint32_t i = 0;
160                                 while (0 != (send = s->send_level_controllable (i))) {
161                                         send->set_automation_state (as);
162                                         ++i;
163                                 }
164 #endif
165                         }
166                         return;
167                 default:
168                         break;
169         }
170
171         // apply to all selected tracks
172         StripableList all;
173         session->get_stripables (all);
174         for (StripableList::const_iterator i = all.begin(); i != all.end(); ++i) {
175                 if ((*i)->is_master() || (*i)->is_monitor()) {
176                         continue;
177                 }
178                 if (!(*i)->is_selected()) {
179                         continue;
180                 }
181                 boost::shared_ptr<AutomationControl> ac;
182                 switch (fadermode) {
183                         case ModeTrack:
184                                 ac = (*i)->gain_control ();
185                                 break;
186                         case ModePan:
187                                 ac = (*i)->pan_azimuth_control ();
188                                 break;
189                         default:
190                                 break;
191                 }
192                 if (ac) {
193                         ac->set_automation_state (as);
194                 }
195         }
196 }
197
198 void
199 FaderPort8::button_varispeed (bool ffw)
200 {
201         /* pressing both rew + ffwd -> return to zero */
202         FP8ButtonInterface& b_rew = _ctrls.button (FP8Controls::BtnRewind);
203         FP8ButtonInterface& b_ffw = _ctrls.button (FP8Controls::BtnFastForward);
204         if (b_rew.is_pressed () && b_ffw.is_pressed ()){
205                 // stop key-repeat
206                 dynamic_cast<FP8RepeatButton*>(&b_ffw)->stop_repeat();
207                 dynamic_cast<FP8RepeatButton*>(&b_rew)->stop_repeat();
208                 AccessAction ("Transport", "GotoStart");
209                 return;
210         }
211
212         // switch play direction, if needed
213         if (ffw) {
214                 if (session->transport_speed () <= 0) {
215                         session->request_transport_speed (1.0);
216                         return ;
217                 }
218         } else {
219                 if (session->transport_speed () >= 0) {
220                         session->request_transport_speed (-1.0);
221                         return ;
222                 }
223         }
224         // incremetally increase speed. double speed every 10 clicks
225         // (keypress auto-repeat is 100ms)
226         float maxspeed = Config->get_shuttle_max_speed();
227         float speed = exp2f(0.1f) * session->transport_speed ();
228         speed = std::max (-maxspeed, std::min (maxspeed, speed));
229         session->request_transport_speed (speed, false);
230 }
231
232 void
233 FaderPort8::button_mute_clear ()
234 {
235         StripableList all;
236         session->get_stripables (all);
237         boost::shared_ptr<ControlList> cl (new ControlList);
238         for (StripableList::const_iterator i = all.begin(); i != all.end(); ++i) {
239                 if ((*i)->is_master() || (*i)->is_monitor()) {
240                         continue;
241                 }
242                 boost::shared_ptr<AutomationControl> ac = (*i)->mute_control();
243                 if (ac && ac->get_value () > 0) {
244                         if (ac->automation_state() == Touch && !ac->touching ()) {
245                                 ac->start_touch (ac->session().transport_frame());
246                         }
247                         cl->push_back (ac);
248                 }
249         }
250         session->set_controls (cl, 0.0, PBD::Controllable::UseGroup);
251 }
252
253 void
254 FaderPort8::button_arm (bool press)
255 {
256         FaderMode fadermode = _ctrls.fader_mode ();
257         if (fadermode == ModeTrack || fadermode == ModePan) {
258                 _ctrls.button (FP8Controls::BtnArm).set_active (press);
259                 ARMButtonChange (press);
260         }
261 }
262
263 void
264 FaderPort8::button_arm_all ()
265 {
266         BasicUI::all_tracks_rec_in ();
267 }
268
269 void
270 FaderPort8::button_action (const std::string& group, const std::string& item)
271 {
272         AccessAction (group, item);
273 }
274
275 void
276 FaderPort8::button_prev_next (bool next)
277 {
278         switch (_ctrls.nav_mode()) {
279                 case NavChannel:
280                         select_prev_next (next);
281                         break;
282                 case NavMaster:
283                 case NavScroll:
284                         bank (!next, false);
285                         break;
286                 case NavBank:
287                         bank (!next, true);
288                         break;
289                 case NavZoom:
290                         if (next) {
291                                 VerticalZoomInSelected ();
292                         } else {
293                                 VerticalZoomOutSelected ();
294                         }
295                         break;
296                 case NavSection:
297                         if (next) {
298                                 AccessAction ("Region", "nudge-forward");
299                         } else {
300                                 AccessAction ("Region", "nudge-backward");
301                         }
302                         break;
303                 case NavMarker:
304                         if (next) {
305                                 next_marker ();
306                         } else {
307                                 prev_marker ();
308                         }
309                         break;
310         }
311 }
312
313 /* handle navigation encoder press */
314 void
315 FaderPort8::button_encoder ()
316 {
317         switch (_ctrls.nav_mode()) {
318                 case NavZoom:
319                         ZoomToSession (); // XXX undo zoom
320                         break;
321                 case NavScroll:
322                         ZoomToSession ();
323                         break;
324                 case NavChannel:
325                 case NavBank:
326                         move_selected_into_view ();
327                         break;
328                 case NavMaster:
329                         {
330                                 /* master || monitor level -- reset to 0dB */
331                                 boost::shared_ptr<AutomationControl> ac;
332                                 if (session->monitor_active() && !_ctrls.button (FP8Controls::BtnMaster).is_pressed ()) {
333                                         ac = session->monitor_out()->gain_control ();
334                                 } else if (session->master_out()) {
335                                         ac = session->master_out()->gain_control ();
336                                 }
337                                 if (ac) {
338                                         if (ac->automation_state() == Touch && !ac->touching ()) {
339                                                 ac->start_touch (ac->session().transport_frame());
340                                         }
341                                         ac->set_value (ac->normal(), PBD::Controllable::NoGroup);
342                                 }
343                         }
344                         break;
345                 case NavSection:
346                         // TODO nudge
347                         break;
348                 case NavMarker:
349                         {
350                                 string markername;
351                                 /* Don't add another mark if one exists within 1/100th of a second of
352                                  * the current position and we're not rolling.
353                                  */
354                                 framepos_t where = session->audible_frame();
355                                 if (session->transport_stopped() && session->locations()->mark_at (where, session->frame_rate() / 100.0)) {
356                                         return;
357                                 }
358
359                                 session->locations()->next_available_name (markername,"mark");
360                                 add_marker (markername);
361                         }
362                         break;
363         }
364 }
365
366 /* handle navigation encoder turn */
367 void
368 FaderPort8::encoder_navigate (bool neg, int steps)
369 {
370         /* special-case metronome level */
371         if (_ctrls.button (FP8Controls::BtnClick).is_pressed ()) {
372                 // compare to ARDOUR_UI::click_button_scroll()
373                 gain_t gain = Config->get_click_gain();
374                 float gain_db = accurate_coefficient_to_dB (gain);
375                 gain_db += (neg ? -1.f : 1.f) * steps;
376                 gain_db = std::max (-60.f, gain_db);
377                 gain = dB_to_coefficient (gain_db);
378                 gain = std::min (gain, Config->get_max_gain());
379                 Config->set_click_gain (gain);
380                 _ctrls.button (FP8Controls::BtnClick).ignore_release();
381                 return;
382         }
383
384         switch (_ctrls.nav_mode()) {
385                 case NavChannel:
386                         if (neg) {
387                                 StepTracksUp ();
388                         } else {
389                                 StepTracksDown ();
390                         }
391                         break;
392                 case NavZoom:
393                         if (neg) {
394                                 ZoomOut ();
395                         } else {
396                                 ZoomIn ();
397                         }
398                         break;
399                 case NavMarker:
400                 case NavScroll:
401                         ScrollTimeline ((neg ? -1.f : 1.f) * steps / (shift_mod() ? 1024.f : 256.f));
402                         break;
403                 case NavBank:
404                         bank (neg, false);
405                         break;
406                 case NavMaster:
407                         {
408                                 /* master || monitor level */
409                                 boost::shared_ptr<AutomationControl> ac;
410                                 if (session->monitor_active() && !_ctrls.button (FP8Controls::BtnMaster).is_pressed ()) {
411                                         ac = session->monitor_out()->gain_control ();
412                                 } else if (session->master_out()) {
413                                         ac = session->master_out()->gain_control ();
414                                 }
415                                 if (ac) {
416                                         double v = ac->internal_to_interface (ac->get_value());
417                                         v = std::max (0.0, std::min (1.0, v + steps * (neg ? -.01 : .01)));
418                                         if (ac->automation_state() == Touch && !ac->touching ()) {
419                                                 ac->start_touch (ac->session().transport_frame());
420                                         }
421                                         ac->set_value (ac->interface_to_internal(v), PBD::Controllable::NoGroup);
422                                 }
423                         }
424                         break;
425                 case NavSection:
426                         if (neg) {
427                                 AccessAction ("Common", "nudge-playhead-backward");
428                         } else {
429                                 AccessAction ("Common", "nudge-playhead-forward");
430                         }
431                         break;
432         }
433 }
434
435 /* handle pan/param encoder press */
436 void
437 FaderPort8::button_parameter ()
438 {
439         switch (_ctrls.fader_mode()) {
440                 case ModeTrack:
441                 case ModePan:
442                         {
443                                 boost::shared_ptr<Stripable> s = first_selected_stripable();
444                                 if (s) {
445                                         boost::shared_ptr<AutomationControl> ac;
446                                         if (shift_mod ()) {
447                                                 ac = s->pan_width_control ();
448                                         } else {
449                                                 ac = s->pan_azimuth_control ();
450                                         }
451                                         if (ac) {
452                                                 if (ac->automation_state() == Touch && !ac->touching ()) {
453                                                         ac->start_touch (ac->session().transport_frame());
454                                                 }
455                                                 ac->set_value (ac->normal(), PBD::Controllable::UseGroup);
456                                         }
457                                 }
458                         }
459                         break;
460                 case ModePlugins:
461                         break;
462                 case ModeSend:
463                         break;
464         }
465 }
466
467 /* handle pan/param encoder turn */
468 void
469 FaderPort8::encoder_parameter (bool neg, int steps)
470 {
471         switch (_ctrls.fader_mode()) {
472                 case ModeTrack:
473                 case ModePan:
474                         {
475                                 boost::shared_ptr<Stripable> s = first_selected_stripable();
476                                 if (s) {
477                                         boost::shared_ptr<AutomationControl> ac;
478                                         if (shift_mod ()) {
479                                                 ac = s->pan_width_control ();
480                                         } else {
481                                                 ac = s->pan_azimuth_control ();
482                                         }
483                                         if (ac) {
484                                                 double v = ac->internal_to_interface (ac->get_value());
485                                                 v = std::max (0.0, std::min (1.0, v + steps * (neg ? -.01 : .01)));
486                                                 if (ac->automation_state() == Touch && !ac->touching ()) {
487                                                         ac->start_touch (ac->session().transport_frame());
488                                                 }
489                                                 ac->set_value (ac->interface_to_internal(v), PBD::Controllable::UseGroup);
490                                         }
491                                 }
492                         }
493                         break;
494                 case ModePlugins:
495                 case ModeSend:
496                         while (steps > 0) {
497                                 bank_param (neg, false);
498                                 --steps;
499                         }
500                         break;
501         }
502 }
503
504 /* handle user-specific actions */
505 void
506 FaderPort8::button_user (bool press, FP8Controls::ButtonId btn)
507 {
508         _user_action_map[btn].call (*this, press);
509 }