58e29fc87708569ba73038b995d4b5b3ed8174cb
[ardour.git] / libs / surfaces / push2 / mix.cc
1 /*
2   Copyright (C) 2016 Paul Davis
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
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.
13
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cairomm/region.h>
20 #include <pangomm/layout.h>
21
22 #include "pbd/compose.h"
23 #include "pbd/convert.h"
24 #include "pbd/debug.h"
25 #include "pbd/failed_constructor.h"
26 #include "pbd/file_utils.h"
27 #include "pbd/search_path.h"
28 #include "pbd/enumwriter.h"
29
30 #include "midi++/parser.h"
31 #include "timecode/time.h"
32 #include "timecode/bbt_time.h"
33
34 #include "ardour/async_midi_port.h"
35 #include "ardour/audioengine.h"
36 #include "ardour/debug.h"
37 #include "ardour/filesystem_paths.h"
38 #include "ardour/midiport_manager.h"
39 #include "ardour/midi_track.h"
40 #include "ardour/midi_port.h"
41 #include "ardour/session.h"
42 #include "ardour/tempo.h"
43 #include "ardour/utils.h"
44 #include "ardour/vca_manager.h"
45
46 #include "canvas/colors.h"
47 #include "canvas/rectangle.h"
48 #include "canvas/line.h"
49
50 #include "gtkmm2ext/gui_thread.h"
51
52 #include "canvas.h"
53 #include "mix.h"
54 #include "knob.h"
55 #include "push2.h"
56 #include "utils.h"
57
58 #include "pbd/i18n.h"
59
60 using namespace ARDOUR;
61 using namespace std;
62 using namespace PBD;
63 using namespace Glib;
64 using namespace ArdourSurface;
65 using namespace ArdourCanvas;
66
67 MixLayout::MixLayout (Push2& p, Session& s)
68         : Push2Layout (p, s)
69         , bank_start (0)
70         , vpot_mode (Volume)
71 {
72         /* background */
73
74         bg = new Rectangle (this);
75         bg->set (Rect (0, 0, display_width(), display_height()));
76         bg->set_fill_color (p2.get_color (Push2::DarkBackground));
77
78         /* upper line */
79
80         upper_line = new Line (this);
81         upper_line->set (Duple (0, 22.5), Duple (display_width(), 22.5));
82         upper_line->set_outline_color (p2.get_color (Push2::LightBackground));
83
84         Pango::FontDescription fd2 ("Sans 10");
85         for (int n = 0; n < 8; ++n) {
86
87                 /* background for text labels for knob function */
88
89                 Rectangle* r = new Rectangle (this);
90                 Coord x0 = 10 + (n*Push2Canvas::inter_button_spacing()) - 5;
91                 r->set (Rect (x0, 2, x0 + Push2Canvas::inter_button_spacing(), 2 + 21));
92                 upper_backgrounds.push_back (r);
93
94                 r = new Rectangle (this);
95                 r->set (Rect (x0, 137, x0 + Push2Canvas::inter_button_spacing(), 137 + 21));
96                 lower_backgrounds.push_back (r);
97
98                 /* text labels for knob function*/
99
100                 Text* t = new Text (this);
101                 t->set_font_description (fd2);
102                 t->set_color (p2.get_color (Push2::ParameterName));
103                 t->set_position (Duple (10 + (n*Push2Canvas::inter_button_spacing()), 5));
104
105                 string txt;
106                 switch (n) {
107                 case 0:
108                         txt = _("Volumes");
109                         break;
110                 case 1:
111                         txt = _("Pans");
112                         break;
113                 case 2:
114                         txt = _("Pan Widths");
115                         break;
116                 case 3:
117                         txt = _("A Sends");
118                         break;
119                 case 4:
120                         txt = _("B Sends");
121                         break;
122                 case 5:
123                         txt = _("C Sends");
124                         break;
125                 case 6:
126                         txt = _("D Sends");
127                         break;
128                 case 7:
129                         txt = _("E Sends");
130                         break;
131                 }
132                 t->set (txt);
133                 upper_text.push_back (t);
134
135                 /* knobs */
136
137                 knobs[n] = new Push2Knob (p2, this);
138                 knobs[n]->set_position (Duple (60 + (n*Push2Canvas::inter_button_spacing()), 95));
139                 knobs[n]->set_radius (25);
140
141                 /* stripable names */
142
143                 t = new Text (this);
144                 t->set_font_description (fd2);
145                 t->set_color (p2.get_color (Push2::ParameterName));
146                 t->set_position (Duple (10 + (n*Push2Canvas::inter_button_spacing()), 140));
147                 lower_text.push_back (t);
148
149         }
150
151         mode_button = p2.button_by_id (Push2::Upper1);
152
153         session.RouteAdded.connect (session_connections, invalidator(*this), boost::bind (&MixLayout::stripables_added, this), &p2);
154         session.vca_manager().VCAAdded.connect (session_connections, invalidator (*this), boost::bind (&MixLayout::stripables_added, this), &p2);
155 }
156
157 MixLayout::~MixLayout ()
158 {
159         // Item destructor deletes all children
160 }
161
162 void
163 MixLayout::show ()
164 {
165         Push2::ButtonID upper_buttons[] = { Push2::Upper1, Push2::Upper2, Push2::Upper3, Push2::Upper4,
166                                             Push2::Upper5, Push2::Upper6, Push2::Upper7, Push2::Upper8 };
167
168
169         for (size_t n = 0; n < sizeof (upper_buttons) / sizeof (upper_buttons[0]); ++n) {
170                 Push2::Button* b = p2.button_by_id (upper_buttons[n]);
171
172                 if (b != mode_button) {
173                         b->set_color (Push2::LED::DarkGray);
174                 } else {
175                         b->set_color (Push2::LED::White);
176                 }
177                 b->set_state (Push2::LED::OneShot24th);
178                 p2.write (b->state_msg());
179         }
180
181         switch_bank (bank_start);
182
183         Container::show ();
184 }
185
186 void
187 MixLayout::render (Rect const& area, Cairo::RefPtr<Cairo::Context> context) const
188 {
189         Container::render (area, context);
190 }
191
192 void
193 MixLayout::button_upper (uint32_t n)
194 {
195         Push2::Button* b;
196         switch (n) {
197         case 0:
198                 vpot_mode = Volume;
199                 b = p2.button_by_id (Push2::Upper1);
200                 break;
201         case 1:
202                 vpot_mode = PanAzimuth;
203                 b = p2.button_by_id (Push2::Upper2);
204                 break;
205         case 2:
206                 vpot_mode = PanWidth;
207                 b = p2.button_by_id (Push2::Upper3);
208                 break;
209         case 3:
210                 vpot_mode = Send1;
211                 b = p2.button_by_id (Push2::Upper4);
212                 break;
213         case 4:
214                 vpot_mode = Send2;
215                 b = p2.button_by_id (Push2::Upper5);
216                 break;
217         case 5:
218                 vpot_mode = Send3;
219                 b = p2.button_by_id (Push2::Upper6);
220                 break;
221         case 6:
222                 vpot_mode = Send4;
223                 b = p2.button_by_id (Push2::Upper7);
224                 break;
225         case 7:
226                 vpot_mode = Send5;
227                 b = p2.button_by_id (Push2::Upper8);
228                 break;
229         }
230
231         if (b != mode_button) {
232                 mode_button->set_color (Push2::LED::Black);
233                 mode_button->set_state (Push2::LED::OneShot24th);
234                 p2.write (mode_button->state_msg());
235         }
236
237         mode_button = b;
238
239         show_vpot_mode ();
240 }
241
242 void
243 MixLayout::show_vpot_mode ()
244 {
245         mode_button->set_color (Push2::LED::White);
246         mode_button->set_state (Push2::LED::OneShot24th);
247         p2.write (mode_button->state_msg());
248
249         for (int s = 0; s < 8; ++s) {
250                 upper_backgrounds[s]->hide ();
251                 upper_text[s]->set_color (p2.get_color (Push2::ParameterName));
252         }
253
254         uint32_t n = 0;
255
256         boost::shared_ptr<AutomationControl> ac;
257         switch (vpot_mode) {
258         case Volume:
259                 for (int s = 0; s < 8; ++s) {
260                         if (stripable[s]) {
261                                 knobs[s]->set_controllable (stripable[s]->gain_control());
262                         } else {
263                                 knobs[s]->set_controllable (boost::shared_ptr<AutomationControl>());
264                         }
265                         knobs[s]->remove_flag (Push2Knob::ArcToZero);
266                 }
267                 n = 0;
268                 break;
269         case PanAzimuth:
270                 for (int s = 0; s < 8; ++s) {
271                         if (stripable[s]) {
272                                 knobs[s]->set_controllable (stripable[s]->pan_azimuth_control());
273                                 knobs[s]->add_flag (Push2Knob::ArcToZero);
274                         } else {
275                                 knobs[s]->set_controllable (boost::shared_ptr<AutomationControl>());
276
277                         }
278                 }
279                 n = 1;
280                 break;
281         case PanWidth:
282                 for (int s = 0; s < 8; ++s) {
283                         if (stripable[s]) {
284                                 knobs[s]->set_controllable (stripable[s]->pan_width_control());
285                         } else {
286                                 knobs[s]->set_controllable (boost::shared_ptr<AutomationControl>());
287
288                         }
289                         knobs[s]->remove_flag (Push2Knob::ArcToZero);
290                 }
291                 n = 2;
292                 break;
293         case Send1:
294                 for (int s = 0; s < 8; ++s) {
295                         if (stripable[s]) {
296                                 knobs[s]->set_controllable (stripable[s]->send_level_controllable (0));
297                         } else {
298                                 knobs[s]->set_controllable (boost::shared_ptr<AutomationControl>());
299
300                         }
301                         knobs[s]->remove_flag (Push2Knob::ArcToZero);
302                 }
303                 n = 3;
304                 break;
305         case Send2:
306                 for (int s = 0; s < 8; ++s) {
307                         if (stripable[s]) {
308                                 knobs[s]->set_controllable (stripable[s]->send_level_controllable (1));
309                         } else {
310                                 knobs[s]->set_controllable (boost::shared_ptr<AutomationControl>());
311
312                         }
313                         knobs[s]->remove_flag (Push2Knob::ArcToZero);
314                 }
315                 n = 4;
316                 break;
317         case Send3:
318                 for (int s = 0; s < 8; ++s) {
319                         if (stripable[s]) {
320                                 knobs[s]->set_controllable (stripable[s]->send_level_controllable (2));
321                         } else {
322                                 knobs[s]->set_controllable (boost::shared_ptr<AutomationControl>());
323
324                         }
325                         knobs[s]->remove_flag (Push2Knob::ArcToZero);
326                 }
327                 n = 5;
328                 break;
329         case Send4:
330                 for (int s = 0; s < 8; ++s) {
331                         if (stripable[s]) {
332                                 knobs[s]->set_controllable (stripable[s]->send_level_controllable (3));
333                         } else {
334                                 knobs[s]->set_controllable (boost::shared_ptr<AutomationControl>());
335
336                         }
337                         knobs[s]->remove_flag (Push2Knob::ArcToZero);
338                 }
339                 n = 6;
340                 break;
341         case Send5:
342                 for (int s = 0; s < 8; ++s) {
343                         if (stripable[s]) {
344                                 knobs[s]->set_controllable (stripable[s]->send_level_controllable (4));
345                         } else {
346                                 knobs[s]->set_controllable (boost::shared_ptr<AutomationControl>());
347
348                         }
349                         knobs[s]->remove_flag (Push2Knob::ArcToZero);
350                 }
351                 n = 7;
352                 break;
353         default:
354                 break;
355         }
356
357         upper_backgrounds[n]->set_fill_color (p2.get_color (Push2::ParameterName));
358         upper_backgrounds[n]->set_outline_color (p2.get_color (Push2::ParameterName));
359         upper_backgrounds[n]->show ();
360         upper_text[n]->set_color (contrasting_text_color (p2.get_color (Push2::ParameterName)));
361 }
362
363 void
364 MixLayout::button_mute ()
365 {
366         boost::shared_ptr<Stripable> s = ControlProtocol::first_selected_stripable();
367         if (s) {
368                 boost::shared_ptr<AutomationControl> ac = s->mute_control();
369                 if (ac) {
370                         ac->set_value (!ac->get_value(), PBD::Controllable::UseGroup);
371                 }
372         }
373 }
374
375 void
376 MixLayout::button_solo ()
377 {
378         boost::shared_ptr<Stripable> s = ControlProtocol::first_selected_stripable();
379         if (s) {
380                 boost::shared_ptr<AutomationControl> ac = s->solo_control();
381                 if (ac) {
382                         ac->set_value (!ac->get_value(), PBD::Controllable::UseGroup);
383                 }
384         }
385 }
386
387 void
388 MixLayout::button_lower (uint32_t n)
389 {
390         if (!stripable[n]) {
391                 return;
392         }
393
394         ControlProtocol::SetStripableSelection (stripable[n]);
395 }
396
397 void
398 MixLayout::strip_vpot (int n, int delta)
399 {
400         boost::shared_ptr<Controllable> ac = knobs[n]->controllable();
401
402         if (ac) {
403                 if (ac->is_gain_like()) {
404                         /* 128 steps from fader position 0 to 1.0 ..
405                          */
406                         const double new_fader_position = min (1.0, max (0.0, ac->internal_to_interface (ac->get_value()) + ((1.0 / 128.0) * delta)));
407                         ac->set_value (ac->interface_to_internal (new_fader_position), PBD::Controllable::UseGroup);
408                 } else {
409                         /* 128 steps from min to max */
410                         ac->set_value (ac->get_value() + (((ac->upper() - ac->lower()) / 128.0) * delta) , PBD::Controllable::UseGroup);
411                 }
412         }
413 }
414
415 void
416 MixLayout::strip_vpot_touch (int n, bool touching)
417 {
418         if (stripable[n]) {
419                 boost::shared_ptr<AutomationControl> ac = stripable[n]->gain_control();
420                 if (ac) {
421                         if (touching) {
422                                 ac->start_touch (session.audible_frame());
423                         } else {
424                                 ac->stop_touch (true, session.audible_frame());
425                         }
426                 }
427         }
428 }
429
430 void
431 MixLayout::stripable_property_change (PropertyChange const& what_changed, uint32_t which)
432 {
433         if (what_changed.contains (Properties::color)) {
434                 lower_backgrounds[which]->set_fill_color (stripable[which]->presentation_info().color());
435
436                 if (stripable[which]->presentation_info().selected()) {
437                         lower_text[which]->set_fill_color (contrasting_text_color (stripable[which]->presentation_info().color()));
438                         /* might not be a MIDI track, in which case this will
439                            do nothing
440                         */
441                         p2.update_selection_color ();
442                 }
443         }
444
445         if (what_changed.contains (Properties::hidden)) {
446                 switch_bank (bank_start);
447         }
448
449         if (what_changed.contains (Properties::selected)) {
450
451                 if (!stripable[which]) {
452                         return;
453                 }
454
455                 if (stripable[which]->presentation_info().selected()) {
456                         show_selection (which);
457                 } else {
458                         hide_selection (which);
459                 }
460         }
461
462 }
463
464 void
465 MixLayout::show_selection (uint32_t n)
466 {
467         lower_backgrounds[n]->show ();
468         lower_backgrounds[n]->set_fill_color (stripable[n]->presentation_info().color());
469         lower_text[n]->set_color (ArdourCanvas::contrasting_text_color (lower_backgrounds[n]->fill_color()));
470 }
471
472 void
473 MixLayout::hide_selection (uint32_t n)
474 {
475         lower_backgrounds[n]->hide ();
476         if (stripable[n]) {
477                 lower_text[n]->set_color (stripable[n]->presentation_info().color());
478         }
479 }
480
481 void
482 MixLayout::solo_changed (uint32_t n)
483 {
484         solo_mute_changed (n);
485 }
486
487 void
488 MixLayout::mute_changed (uint32_t n)
489 {
490         solo_mute_changed (n);
491 }
492
493 void
494 MixLayout::solo_mute_changed (uint32_t n)
495 {
496         string shortname = short_version (stripable[n]->name(), 10);
497         string text;
498         boost::shared_ptr<AutomationControl> ac;
499         ac = stripable[n]->solo_control();
500         if (ac && ac->get_value()) {
501                 text += "* ";
502         }
503         boost::shared_ptr<MuteControl> mc;
504         mc = stripable[n]->mute_control ();
505         if (mc) {
506                 if (mc->muted_by_self_or_masters()) {
507                         text += "! ";
508                 } else if (mc->muted_by_others_soloing()) {
509                         text += "- "; // it would be nice to use Unicode mute"\uD83D\uDD07 ";
510                 }
511         }
512         text += shortname;
513         lower_text[n]->set (text);
514 }
515
516 void
517 MixLayout::switch_bank (uint32_t base)
518 {
519         stripable_connections.drop_connections ();
520
521         /* work backwards so we can tell if we should actually switch banks */
522
523         boost::shared_ptr<Stripable> s[8];
524         uint32_t different = 0;
525
526         for (int n = 0; n < 8; ++n) {
527                 s[n] = session.get_remote_nth_stripable (base+n, PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::VCA));
528                 if (s[n] != stripable[n]) {
529                         different++;
530                 }
531         }
532
533         if (!s[0]) {
534                 /* not even the first stripable exists, do nothing */
535                 return;
536         }
537
538         for (int n = 0; n < 8; ++n) {
539                 stripable[n] = s[n];
540         }
541
542         /* at least one stripable in this bank */
543
544         bank_start = base;
545
546         for (int n = 0; n < 8; ++n) {
547
548                 if (!stripable[n]) {
549                         lower_text[n]->hide ();
550                         hide_selection (n);
551                 } else {
552
553                         lower_text[n]->show ();
554
555                         /* stripable goes away? refill the bank, starting at the same point */
556
557                         stripable[n]->DropReferences.connect (stripable_connections, invalidator (*this), boost::bind (&MixLayout::switch_bank, this, bank_start), &p2);
558                         stripable[n]->presentation_info().PropertyChanged.connect (stripable_connections, invalidator (*this), boost::bind (&MixLayout::stripable_property_change, this, _1, n), &p2);
559                         stripable[n]->solo_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&MixLayout::solo_changed, this, n), &p2);
560                         stripable[n]->mute_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&MixLayout::mute_changed, this, n), &p2);
561
562                         if (stripable[n]->presentation_info().selected()) {
563                                 show_selection (n);
564                         } else {
565                                 hide_selection (n);
566                         }
567
568                         /* this will set lower text to the correct value (basically
569                            the stripable name)
570                         */
571
572                         solo_mute_changed (n);
573
574                         knobs[n]->set_text_color (stripable[n]->presentation_info().color());
575                         knobs[n]->set_arc_start_color (stripable[n]->presentation_info().color());
576                         knobs[n]->set_arc_end_color (stripable[n]->presentation_info().color());
577                 }
578
579
580                 Push2::Button* b;
581
582                 switch (n) {
583                 case 0:
584                         b = p2.button_by_id (Push2::Lower1);
585                         break;
586                 case 1:
587                         b = p2.button_by_id (Push2::Lower2);
588                         break;
589                 case 2:
590                         b = p2.button_by_id (Push2::Lower3);
591                         break;
592                 case 3:
593                         b = p2.button_by_id (Push2::Lower4);
594                         break;
595                 case 4:
596                         b = p2.button_by_id (Push2::Lower5);
597                         break;
598                 case 5:
599                         b = p2.button_by_id (Push2::Lower6);
600                         break;
601                 case 6:
602                         b = p2.button_by_id (Push2::Lower7);
603                         break;
604                 case 7:
605                         b = p2.button_by_id (Push2::Lower8);
606                         break;
607                 }
608
609                 if (stripable[n]) {
610                         b->set_color (p2.get_color_index (stripable[n]->presentation_info().color()));
611                 } else {
612                         b->set_color (Push2::LED::Black);
613                 }
614
615                 b->set_state (Push2::LED::OneShot24th);
616                 p2.write (b->state_msg());
617         }
618
619         show_vpot_mode ();
620 }
621
622 void
623 MixLayout::button_right ()
624 {
625         switch_bank (max (0, bank_start + 8));
626 }
627
628 void
629 MixLayout::button_left ()
630 {
631         switch_bank (max (0, bank_start - 8));
632 }
633
634
635 void
636 MixLayout::button_select_press ()
637 {
638 }
639
640 void
641 MixLayout::button_select_release ()
642 {
643         if (!(p2.modifier_state() & Push2::ModSelect)) {
644                 /* somebody else used us as a modifier */
645                 return;
646         }
647
648         int selected = -1;
649
650         for (int n = 0; n < 8; ++n) {
651                 if (stripable[n]) {
652                         if (stripable[n]->presentation_info().selected()) {
653                                         selected = n;
654                                         break;
655                         }
656                 }
657         }
658
659         if (selected < 0) {
660
661                 /* no visible track selected, select first (if any) */
662
663                 if (stripable[0]) {
664                         ControlProtocol::SetStripableSelection (stripable[0]);
665                 }
666
667         } else {
668
669                 if (p2.modifier_state() & Push2::ModShift) {
670                         /* select prev */
671
672                         if (selected == 0) {
673                                 /* current selected is leftmost ... cancel selection,
674                                    switch banks by one, and select leftmost
675                                 */
676                                 if (bank_start != 0) {
677                                         ControlProtocol::ClearStripableSelection ();
678                                         switch_bank (bank_start-1);
679                                         if (stripable[0]) {
680                                                 ControlProtocol::SetStripableSelection (stripable[0]);
681                                         }
682                                 }
683                         } else {
684                                 /* select prev, if any */
685                                 int n = selected - 1;
686                                 while (n >= 0 && !stripable[n]) {
687                                         --n;
688                                 }
689                                 if (n >= 0) {
690                                         ControlProtocol::SetStripableSelection (stripable[n]);
691                                 }
692                         }
693
694                 } else {
695
696                         /* select next */
697
698                         if (selected == 7) {
699                                 /* current selected is rightmost ... cancel selection,
700                                    switch banks by one, and select righmost
701                                 */
702                                 ControlProtocol::ToggleStripableSelection (stripable[selected]);
703                                 switch_bank (bank_start+1);
704                                 if (stripable[7]) {
705                                         ControlProtocol::SetStripableSelection (stripable[7]);
706                                 }
707                         } else {
708                                 /* select next, if any */
709                                 int n = selected + 1;
710                                 while (n < 8 && !stripable[n]) {
711                                         ++n;
712                                 }
713
714                                 if (n != 8) {
715                                         ControlProtocol::SetStripableSelection (stripable[n]);
716                                 }
717                         }
718                 }
719         }
720 }
721
722 void
723 MixLayout::stripables_added ()
724 {
725         /* reload current bank */
726         switch_bank (bank_start);
727 }
728
729 void
730 MixLayout::button_down ()
731 {
732         p2.scroll_dn_1_track ();
733 }
734
735 void
736 MixLayout::button_up ()
737 {
738         p2.scroll_up_1_track ();
739 }