f0ee572455072c84c1c4356d788ceb246706a0bf
[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 <pangomm/layout.h>
20
21 #include "pbd/compose.h"
22 #include "pbd/convert.h"
23 #include "pbd/debug.h"
24 #include "pbd/failed_constructor.h"
25 #include "pbd/file_utils.h"
26 #include "pbd/search_path.h"
27 #include "pbd/enumwriter.h"
28
29 #include "midi++/parser.h"
30 #include "timecode/time.h"
31 #include "timecode/bbt_time.h"
32
33 #include "ardour/async_midi_port.h"
34 #include "ardour/audioengine.h"
35 #include "ardour/debug.h"
36 #include "ardour/filesystem_paths.h"
37 #include "ardour/midiport_manager.h"
38 #include "ardour/midi_track.h"
39 #include "ardour/midi_port.h"
40 #include "ardour/session.h"
41 #include "ardour/tempo.h"
42
43 #include "push2.h"
44 #include "mix.h"
45
46 #include "i18n.h"
47
48 using namespace ARDOUR;
49 using namespace std;
50 using namespace PBD;
51 using namespace Glib;
52 using namespace ArdourSurface;
53
54 MixLayout::MixLayout (Push2& p, Session& s, Cairo::RefPtr<Cairo::Context> context)
55         : Push2Layout (p, s)
56         , bank_start (0)
57 {
58         tc_clock_layout = Pango::Layout::create (context);
59         bbt_clock_layout = Pango::Layout::create (context);
60
61         Pango::FontDescription fd ("Sans Bold 24");
62         tc_clock_layout->set_font_description (fd);
63         bbt_clock_layout->set_font_description (fd);
64
65         Pango::FontDescription fd2 ("Sans 10");
66         for (int n = 0; n < 8; ++n) {
67                 upper_layout[n] = Pango::Layout::create (context);
68                 upper_layout[n]->set_font_description (fd2);
69                 upper_layout[n]->set_text ("solo");
70                 lower_layout[n] = Pango::Layout::create (context);
71                 lower_layout[n]->set_font_description (fd2);
72                 lower_layout[n]->set_text ("mute");
73         }
74
75         Pango::FontDescription fd3 ("Sans Bold 10");
76         for (int n = 0; n < 8; ++n) {
77                 mid_layout[n] = Pango::Layout::create (context);
78                 mid_layout[n]->set_font_description (fd3);
79         }
80
81         switch_bank (0);
82 }
83
84 MixLayout::~MixLayout ()
85 {
86 }
87
88 bool
89 MixLayout::redraw (Cairo::RefPtr<Cairo::Context> context) const
90 {
91         framepos_t audible = session.audible_frame();
92         Timecode::Time TC;
93         bool negative = false;
94         string tc_clock_text;
95         string bbt_clock_text;
96
97         if (audible < 0) {
98                 audible = -audible;
99                 negative = true;
100         }
101
102         session.timecode_time (audible, TC);
103
104         TC.negative = TC.negative || negative;
105
106         tc_clock_text = Timecode::timecode_format_time(TC);
107
108         Timecode::BBT_Time bbt = session.tempo_map().bbt_at_frame (audible);
109         char buf[16];
110
111 #define BBT_BAR_CHAR "|"
112
113         if (negative) {
114                 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
115                           bbt.bars, bbt.beats, bbt.ticks);
116         } else {
117                 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
118                           bbt.bars, bbt.beats, bbt.ticks);
119         }
120
121         bbt_clock_text = buf;
122
123         bool dirty = false;
124
125         if (tc_clock_text != tc_clock_layout->get_text()) {
126                 dirty = true;
127                 tc_clock_layout->set_text (tc_clock_text);
128         }
129
130         if (bbt_clock_text != tc_clock_layout->get_text()) {
131                 dirty = true;
132                 bbt_clock_layout->set_text (bbt_clock_text);
133         }
134
135         string mid_text;
136
137         for (int n = 0; n < 8; ++n) {
138                 if (stripable[n]) {
139                         mid_text = short_version (stripable[n]->name(), 10);
140                         if (mid_text != mid_layout[n]->get_text()) {
141                                 mid_layout[n]->set_text (mid_text);
142                                 dirty = true;
143                         }
144                 }
145         }
146
147         if (!dirty) {
148                 return false;
149         }
150
151         context->set_source_rgb (0.764, 0.882, 0.882);
152         context->rectangle (0, 0, 960, 160);
153         context->fill ();
154
155         /* clocks */
156
157         context->set_source_rgb (0.23, 0.0, 0.349);
158         context->move_to (650, 25);
159         tc_clock_layout->update_from_cairo_context (context);
160         tc_clock_layout->show_in_cairo_context (context);
161         context->move_to (650, 60);
162         bbt_clock_layout->update_from_cairo_context (context);
163         bbt_clock_layout->show_in_cairo_context (context);
164
165         for (int n = 0; n < 8; ++n) {
166                 context->move_to (10 + (n*120), 2);
167                 upper_layout[n]->update_from_cairo_context (context);
168                 upper_layout[n]->show_in_cairo_context (context);
169         }
170
171         for (int n = 0; n < 8; ++n) {
172                 context->move_to (10 + (n*120), 140);
173                 lower_layout[n]->update_from_cairo_context (context);
174                 lower_layout[n]->show_in_cairo_context (context);
175         }
176
177         for (int n = 0; n < 8; ++n) {
178                 if (stripable[n] && stripable[n]->presentation_info().selected()) {
179                         context->rectangle (10 + (n*120) - 5, 115, 120, 22);
180                         context->set_source_rgb (1.0, 0.737, 0.172);
181                         context->fill();
182                 }
183                 context->set_source_rgb (0.0, 0.0, 0.0);
184                 context->move_to (10 + (n*120), 120);
185                 mid_layout[n]->update_from_cairo_context (context);
186                 mid_layout[n]->show_in_cairo_context (context);
187         }
188
189         return true;
190 }
191
192 void
193 MixLayout::button_upper (uint32_t n)
194 {
195         if (!stripable[n]) {
196                 return;
197         }
198
199         if (p2.modifier_state() & Push2::ModShift) {
200                 boost::shared_ptr<AutomationControl> sc = stripable[n]->rec_enable_control ();
201                 if (sc) {
202                         sc->set_value (!sc->get_value(), PBD::Controllable::UseGroup);
203                 }
204         } else {
205                 boost::shared_ptr<SoloControl> sc = stripable[n]->solo_control ();
206                 if (sc) {
207                         sc->set_value (!sc->self_soloed(), PBD::Controllable::UseGroup);
208                 }
209         }
210 }
211
212 void
213 MixLayout::button_lower (uint32_t n)
214 {
215         if (!stripable[n]) {
216                 return;
217         }
218
219         if (p2.modifier_state() & Push2::ModSelect) {
220                 ControlProtocol::SetStripableSelection (stripable[n]);
221         } else {
222                 boost::shared_ptr<MuteControl> mc = stripable[n]->mute_control ();
223
224                 if (mc) {
225                         mc->set_value (!mc->muted_by_self(), PBD::Controllable::UseGroup);
226                 }
227         }
228 }
229
230 void
231 MixLayout::strip_vpot (int n, int delta)
232 {
233         if (stripable[n]) {
234                 boost::shared_ptr<AutomationControl> ac = stripable[n]->gain_control();
235                 if (ac) {
236                         ac->set_value (ac->get_value() + ((2.0/64.0) * delta), PBD::Controllable::UseGroup);
237                 }
238         }
239 }
240
241 void
242 MixLayout::strip_vpot_touch (int n, bool touching)
243 {
244         if (stripable[n]) {
245                 boost::shared_ptr<AutomationControl> ac = stripable[n]->gain_control();
246                 if (ac) {
247                         if (touching) {
248                                 ac->start_touch (session.audible_frame());
249                         } else {
250                                 ac->stop_touch (true, session.audible_frame());
251                         }
252                 }
253         }
254 }
255
256 void
257 MixLayout::stripable_property_change (PropertyChange const& what_changed, int which)
258 {
259         if (what_changed.contains (Properties::selected)) {
260                 if (!stripable[which]) {
261                         return;
262                 }
263
264                 /* cancel string, which will cause a redraw on the next update
265                  * cycle. The redraw will reflect selected status
266                  */
267
268                 mid_layout[which]->set_text (string());
269         }
270 }
271
272
273 void
274 MixLayout::solo_change (int n)
275 {
276         Push2::ButtonID bid;
277
278         switch (n) {
279         case 0:
280                 bid = Push2::Upper1;
281                 break;
282         case 1:
283                 bid = Push2::Upper2;
284                 break;
285         case 2:
286                 bid = Push2::Upper3;
287                 break;
288         case 3:
289                 bid = Push2::Upper4;
290                 break;
291         case 4:
292                 bid = Push2::Upper5;
293                 break;
294         case 5:
295                 bid = Push2::Upper6;
296                 break;
297         case 6:
298                 bid = Push2::Upper7;
299                 break;
300         case 7:
301                 bid = Push2::Upper8;
302                 break;
303         default:
304                 return;
305         }
306
307         boost::shared_ptr<SoloControl> ac = stripable[n]->solo_control ();
308         if (!ac) {
309                 return;
310         }
311
312         Push2::Button* b = p2.button_by_id (bid);
313
314         if (ac->soloed()) {
315                 b->set_color (Push2::LED::Green);
316         } else {
317                 b->set_color (Push2::LED::Black);
318         }
319
320         if (ac->soloed_by_others_upstream() || ac->soloed_by_others_downstream()) {
321                 b->set_state (Push2::LED::Blinking4th);
322         } else {
323                 b->set_state (Push2::LED::OneShot24th);
324         }
325
326         p2.write (b->state_msg());
327 }
328
329 void
330 MixLayout::mute_change (int n)
331 {
332         Push2::ButtonID bid;
333
334         if (!stripable[n]) {
335                 return;
336         }
337
338         cerr << "Mute changed on " << n << ' ' << stripable[n]->name() << endl;
339
340         switch (n) {
341         case 0:
342                 bid = Push2::Lower1;
343                 break;
344         case 1:
345                 bid = Push2::Lower2;
346                 break;
347         case 2:
348                 bid = Push2::Lower3;
349                 break;
350         case 3:
351                 bid = Push2::Lower4;
352                 break;
353         case 4:
354                 bid = Push2::Lower5;
355                 break;
356         case 5:
357                 bid = Push2::Lower6;
358                 break;
359         case 6:
360                 bid = Push2::Lower7;
361                 break;
362         case 7:
363                 bid = Push2::Lower8;
364                 break;
365         default:
366                 return;
367         }
368
369         boost::shared_ptr<MuteControl> mc = stripable[n]->mute_control ();
370
371         if (!mc) {
372                 return;
373         }
374
375         Push2::Button* b = p2.button_by_id (bid);
376
377         if (Config->get_show_solo_mutes() && !Config->get_solo_control_is_listen_control ()) {
378
379                 if (mc->muted_by_self ()) {
380                         /* full mute */
381                         b->set_color (Push2::LED::Blue);
382                         b->set_state (Push2::LED::OneShot24th);
383                         cerr << "FULL MUTE1\n";
384                 } else if (mc->muted_by_others_soloing () || mc->muted_by_masters ()) {
385                         /* this will reflect both solo mutes AND master mutes */
386                         b->set_color (Push2::LED::Blue);
387                         b->set_state (Push2::LED::Blinking4th);
388                         cerr << "OTHER MUTE1\n";
389                 } else {
390                         /* no mute at all */
391                         b->set_color (Push2::LED::Black);
392                         b->set_state (Push2::LED::OneShot24th);
393                         cerr << "NO MUTE1\n";
394                 }
395
396         } else {
397
398                 if (mc->muted_by_self()) {
399                         /* full mute */
400                         b->set_color (Push2::LED::Blue);
401                         b->set_state (Push2::LED::OneShot24th);
402                         cerr << "FULL MUTE2\n";
403                 } else if (mc->muted_by_masters ()) {
404                         /* this shows only master mutes, not mute-by-others-soloing */
405                         b->set_color (Push2::LED::Blue);
406                         b->set_state (Push2::LED::Blinking4th);
407                         cerr << "OTHER MUTE1\n";
408                 } else {
409                         /* no mute at all */
410                         b->set_color (Push2::LED::Black);
411                         b->set_state (Push2::LED::OneShot24th);
412                         cerr << "NO MUTE2\n";
413                 }
414         }
415
416         p2.write (b->state_msg());
417 }
418
419 void
420 MixLayout::switch_bank (uint32_t base)
421 {
422         stripable_connections.drop_connections ();
423
424         /* try to get the first stripable for the requested bank */
425
426         stripable[0] = session.get_remote_nth_stripable (base, PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::VCA));
427
428         if (!stripable[0]) {
429                 return;
430         }
431
432         /* at least one stripable in this bank */
433         bank_start = base;
434
435         stripable[1] = session.get_remote_nth_stripable (base+1, PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::VCA));
436         stripable[2] = session.get_remote_nth_stripable (base+2, PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::VCA));
437         stripable[3] = session.get_remote_nth_stripable (base+3, PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::VCA));
438         stripable[4] = session.get_remote_nth_stripable (base+4, PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::VCA));
439         stripable[5] = session.get_remote_nth_stripable (base+5, PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::VCA));
440         stripable[6] = session.get_remote_nth_stripable (base+6, PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::VCA));
441         stripable[7] = session.get_remote_nth_stripable (base+7, PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::VCA));
442
443
444         for (int n = 0; n < 8; ++n) {
445                 if (!stripable[n]) {
446                         continue;
447                 }
448
449                 /* stripable goes away? refill the bank, starting at the same point */
450
451                 stripable[n]->DropReferences.connect (stripable_connections, MISSING_INVALIDATOR, boost::bind (&MixLayout::switch_bank, this, bank_start), &p2);
452                 boost::shared_ptr<AutomationControl> sc = stripable[n]->solo_control();
453                 if (sc) {
454                         sc->Changed.connect (stripable_connections, MISSING_INVALIDATOR, boost::bind (&MixLayout::solo_change, this, n), &p2);
455                 }
456
457                 boost::shared_ptr<AutomationControl> mc = stripable[n]->mute_control();
458                 if (mc) {
459                         mc->Changed.connect (stripable_connections, MISSING_INVALIDATOR, boost::bind (&MixLayout::mute_change, this, n), &p2);
460                 }
461
462                 stripable[n]->presentation_info().PropertyChanged.connect (stripable_connections, MISSING_INVALIDATOR, boost::bind (&MixLayout::stripable_property_change, this, _1, n), &p2);
463
464                 solo_change (n);
465                 mute_change (n);
466
467         }
468 }
469
470 void
471 MixLayout::button_right ()
472 {
473         switch_bank (max (0, bank_start + 8));
474 }
475
476 void
477 MixLayout::button_left ()
478 {
479         switch_bank (max (0, bank_start - 8));
480 }
481
482 void
483 MixLayout::button_select_press ()
484 {
485 }
486
487 void
488 MixLayout::button_select_release ()
489 {
490         if (!(p2.modifier_state() & Push2::ModSelect)) {
491                 /* somebody else used us as a modifier */
492                 return;
493         }
494
495         int selected = -1;
496
497         for (int n = 0; n < 8; ++n) {
498                 if (stripable[n]) {
499                         if (stripable[n]->presentation_info().selected()) {
500                                         selected = n;
501                                         break;
502                         }
503                 }
504         }
505
506         if (selected < 0) {
507
508                 /* no visible track selected, select first (if any) */
509
510                 if (stripable[0]) {
511                         ControlProtocol::SetStripableSelection (stripable[0]);
512                 }
513
514         } else {
515
516                 if (p2.modifier_state() & Push2::ModShift) {
517                         std::cerr << "select prev\n";
518                         /* select prev */
519
520                         if (selected == 0) {
521                                 /* current selected is leftmost ... cancel selection,
522                                    switch banks by one, and select leftmost
523                                 */
524                                 if (bank_start != 0) {
525                                         ControlProtocol::ClearStripableSelection ();
526                                         switch_bank (bank_start-1);
527                                         if (stripable[0]) {
528                                                 ControlProtocol::SetStripableSelection (stripable[0]);
529                                         }
530                                 }
531                         } else {
532                                 /* select prev, if any */
533                                 int n = selected - 1;
534                                 while (n >= 0 && !stripable[n]) {
535                                         --n;
536                                 }
537                                 if (n >= 0) {
538                                         ControlProtocol::SetStripableSelection (stripable[n]);
539                                 }
540                         }
541
542                 } else {
543
544                         std::cerr << "select next\n";
545                         /* select next */
546
547                         if (selected == 7) {
548                                 /* current selected is rightmost ... cancel selection,
549                                    switch banks by one, and select righmost
550                                 */
551                                 ControlProtocol::ToggleStripableSelection (stripable[selected]);
552                                 switch_bank (bank_start+1);
553                                 if (stripable[7]) {
554                                         ControlProtocol::SetStripableSelection (stripable[7]);
555                                 }
556                         } else {
557                                 /* select next, if any */
558                                 int n = selected + 1;
559                                 while (n < 8 && !stripable[n]) {
560                                         ++n;
561                                 }
562
563                                 if (n != 8) {
564                                         ControlProtocol::SetStripableSelection (stripable[n]);
565                                 }
566                         }
567                 }
568         }
569 }
570
571