push2: tweak layout APIs etc.
[ardour.git] / libs / surfaces / push2 / track_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/i18n.h"
28 #include "pbd/search_path.h"
29 #include "pbd/enumwriter.h"
30
31 #include "midi++/parser.h"
32 #include "timecode/time.h"
33 #include "timecode/bbt_time.h"
34
35 #include "ardour/async_midi_port.h"
36 #include "ardour/audioengine.h"
37 #include "ardour/debug.h"
38 #include "ardour/dsp_filter.h"
39 #include "ardour/filesystem_paths.h"
40 #include "ardour/midiport_manager.h"
41 #include "ardour/midi_track.h"
42 #include "ardour/midi_port.h"
43 #include "ardour/monitor_control.h"
44 #include "ardour/meter.h"
45 #include "ardour/session.h"
46 #include "ardour/solo_isolate_control.h"
47 #include "ardour/solo_safe_control.h"
48 #include "ardour/tempo.h"
49
50 #include "gtkmm2ext/gui_thread.h"
51 #include "gtkmm2ext/rgb_macros.h"
52
53 #include "canvas/box.h"
54 #include "canvas/line.h"
55 #include "canvas/meter.h"
56 #include "canvas/rectangle.h"
57 #include "canvas/text.h"
58
59 #include "canvas.h"
60 #include "knob.h"
61 #include "level_meter.h"
62 #include "menu.h"
63 #include "push2.h"
64 #include "track_mix.h"
65 #include "utils.h"
66
67 using namespace ARDOUR;
68 using namespace std;
69 using namespace PBD;
70 using namespace Glib;
71 using namespace ArdourSurface;
72 using namespace ArdourCanvas;
73
74 TrackMixLayout::TrackMixLayout (Push2& p, Session & s, std::string const & name)
75         : Push2Layout (p, s, name)
76 {
77         Pango::FontDescription fd ("Sans 10");
78
79         bg = new Rectangle (this);
80         bg->set (Rect (0, 0, display_width(), display_height()));
81         bg->set_fill_color (p2.get_color (Push2::DarkBackground));
82
83         upper_line = new Line (this);
84         upper_line->set (Duple (0, 22.5), Duple (display_width(), 22.5));
85         upper_line->set_outline_color (p2.get_color (Push2::LightBackground));
86
87         for (int n = 0; n < 8; ++n) {
88                 Text* t;
89
90                 if (n < 4) {
91                         t = new Text (this);
92                         t->set_font_description (fd);
93                         t->set_color (p2.get_color (Push2::ParameterName));
94                         t->set_position ( Duple (10 + (n*Push2Canvas::inter_button_spacing()), 2));
95                         upper_text.push_back (t);
96                 }
97
98                 t = new Text (this);
99                 t->set_font_description (fd);
100                 t->set_color (p2.get_color (Push2::ParameterName));
101                 t->set_position (Duple (10 + (n*Push2Canvas::inter_button_spacing()), 140));
102
103                 lower_text.push_back (t);
104
105                 switch (n) {
106                 case 0:
107                         upper_text[n]->set (_("Track Volume"));
108                         lower_text[n]->set (_("Mute"));
109                         break;
110                 case 1:
111                         upper_text[n]->set (_("Track Pan"));
112                         lower_text[n]->set (_("Solo"));
113                         break;
114                 case 2:
115                         upper_text[n]->set (_("Track Width"));
116                         lower_text[n]->set (_("Rec-enable"));
117                         break;
118                 case 3:
119                         upper_text[n]->set (_("Track Trim"));
120                         lower_text[n]->set (_("In"));
121                         break;
122                 case 4:
123                         lower_text[n]->set (_("Disk"));
124                         break;
125                 case 5:
126                         lower_text[n]->set (_("Solo Iso"));
127                         break;
128                 case 6:
129                         lower_text[n]->set (_("Solo Lock"));
130                         break;
131                 case 7:
132                         lower_text[n]->set (_(""));
133                         break;
134                 }
135
136                 knobs[n] = new Push2Knob (p2, this);
137                 knobs[n]->set_position (Duple (60 + (Push2Canvas::inter_button_spacing()*n), 95));
138                 knobs[n]->set_radius (25);
139         }
140
141         name_text = new Text (this);
142         name_text->set_font_description (fd);
143         name_text->set_position (Duple (10 + (4*Push2Canvas::inter_button_spacing()), 2));
144
145         meter = new LevelMeter (p2, this, 300, ArdourCanvas::Meter::Horizontal);
146         meter->set_position (Duple (10 + (4 * Push2Canvas::inter_button_spacing()), 30));
147
148         Pango::FontDescription fd2 ("Sans 18");
149         bbt_text = new Text (this);
150         bbt_text->set_font_description (fd2);
151         bbt_text->set_color (p2.get_color (Push2::LightBackground));
152         bbt_text->set_position (Duple (10 + (4 * Push2Canvas::inter_button_spacing()), 60));
153
154         minsec_text = new Text (this);
155         minsec_text->set_font_description (fd2);
156         minsec_text->set_color (p2.get_color (Push2::LightBackground));
157         minsec_text->set_position (Duple (10 + (4 * Push2Canvas::inter_button_spacing()), 90));
158
159         ControlProtocol::StripableSelectionChanged.connect (selection_connection, invalidator (*this), boost::bind (&TrackMixLayout::selection_changed, this), &p2);
160 }
161
162 TrackMixLayout::~TrackMixLayout ()
163 {
164         for (int n = 0; n < 8; ++n) {
165                 delete knobs[n];
166         }
167 }
168
169 void
170 TrackMixLayout::selection_changed ()
171 {
172         boost::shared_ptr<Stripable> s = ControlProtocol::first_selected_stripable();
173
174         if (s) {
175                 set_stripable (s);
176         }
177 }
178
179 void
180 TrackMixLayout::show ()
181 {
182         Push2::ButtonID lower_buttons[] = { Push2::Lower1, Push2::Lower2, Push2::Lower3, Push2::Lower4,
183                                             Push2::Lower5, Push2::Lower6, Push2::Lower7, Push2::Lower8 };
184
185         for (size_t n = 0; n < sizeof (lower_buttons) / sizeof (lower_buttons[0]); ++n) {
186                 Push2::Button* b = p2.button_by_id (lower_buttons[n]);
187                 b->set_color (Push2::LED::DarkGray);
188                 b->set_state (Push2::LED::OneShot24th);
189                 p2.write (b->state_msg());
190         }
191
192         show_state ();
193
194         Container::show ();
195 }
196
197 void
198 TrackMixLayout::hide ()
199 {
200
201 }
202
203 void
204 TrackMixLayout::render (ArdourCanvas::Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
205 {
206         Container::render (area, context);
207 }
208
209 void
210 TrackMixLayout::button_upper (uint32_t n)
211 {
212 }
213
214 void
215 TrackMixLayout::button_lower (uint32_t n)
216 {
217         if (!stripable) {
218                 return;
219         }
220
221         MonitorChoice mc;
222
223         switch (n) {
224         case 0:
225                 if (stripable->mute_control()) {
226                         stripable->mute_control()->set_value (!stripable->mute_control()->get_value(), PBD::Controllable::UseGroup);
227                 }
228                 break;
229         case 1:
230                 if (stripable->solo_control()) {
231                         stripable->solo_control()->set_value (!stripable->solo_control()->get_value(), PBD::Controllable::UseGroup);
232                 }
233                 break;
234         case 2:
235                 if (stripable->rec_enable_control()) {
236                         stripable->rec_enable_control()->set_value (!stripable->rec_enable_control()->get_value(), PBD::Controllable::UseGroup);
237                 }
238                 break;
239         case 3:
240                 if (stripable->monitor_control()) {
241                         mc = stripable->monitoring_control()->monitoring_choice();
242                         switch (mc) {
243                         case MonitorInput:
244                                 stripable->monitoring_control()->set_value (MonitorAuto, PBD::Controllable::UseGroup);
245                                 break;
246                         default:
247                                 stripable->monitoring_control()->set_value (MonitorInput, PBD::Controllable::UseGroup);
248                                 break;
249                         }
250                 }
251                 break;
252         case 4:
253                 mc = stripable->monitoring_control()->monitoring_choice();
254                 switch (mc) {
255                 case MonitorDisk:
256                         stripable->monitoring_control()->set_value (MonitorAuto, PBD::Controllable::UseGroup);
257                         break;
258                 default:
259                         stripable->monitoring_control()->set_value (MonitorDisk, PBD::Controllable::UseGroup);
260                         break;
261                 }
262                 break;
263         case 5:
264                 if (stripable->solo_isolate_control()) {
265                         stripable->solo_isolate_control()->set_value (!stripable->solo_isolate_control()->get_value(), PBD::Controllable::UseGroup);
266                 }
267                 break;
268         case 6:
269                 if (stripable->solo_safe_control()) {
270                         stripable->solo_safe_control()->set_value (!stripable->solo_safe_control()->get_value(), PBD::Controllable::UseGroup);
271                 }
272                 break;
273         case 7:
274                 /* nothing here */
275                 break;
276         }
277 }
278
279 void
280 TrackMixLayout::button_left ()
281 {
282         p2.access_action ("Editor/select-prev-route");
283 }
284
285 void
286 TrackMixLayout::button_right ()
287 {
288         p2.access_action ("Editor/select-next-route");
289 }
290
291 void
292 TrackMixLayout::simple_control_change (boost::shared_ptr<AutomationControl> ac, Push2::ButtonID bid)
293 {
294         if (!ac || !parent()) {
295                 return;
296         }
297
298         Push2::Button* b = p2.button_by_id (bid);
299
300         if (!b) {
301                 return;
302         }
303
304         if (ac->get_value()) {
305                 b->set_color (selection_color);
306         } else {
307                 b->set_color (Push2::LED::DarkGray);
308         }
309         b->set_state (Push2::LED::OneShot24th);
310         p2.write (b->state_msg());
311 }
312
313 void
314 TrackMixLayout::solo_mute_change ()
315 {
316         if (!stripable) {
317                 return;
318         }
319
320         Push2::Button* b = p2.button_by_id (Push2::Lower2);
321
322         if (b) {
323                 boost::shared_ptr<SoloControl> sc = stripable->solo_control();
324
325                 if (sc) {
326                         if (sc->soloed_by_self_or_masters()) {
327                                 b->set_color (selection_color);
328                                 b->set_state (Push2::LED::OneShot24th);
329                         } else if (sc->soloed_by_others_upstream() || sc->soloed_by_others_downstream()) {
330                                 b->set_color (selection_color);
331                                 b->set_state (Push2::LED::Blinking8th);
332                         } else {
333                                 b->set_color (Push2::LED::DarkGray);
334                                 b->set_state (Push2::LED::OneShot24th);
335                         }
336                 } else {
337                         b->set_color (Push2::LED::DarkGray);
338                         b->set_state (Push2::LED::OneShot24th);
339                 }
340
341                 p2.write (b->state_msg());
342         }
343
344         b = p2.button_by_id (Push2::Lower1);
345
346         if (b) {
347                 boost::shared_ptr<MuteControl> mc = stripable->mute_control();
348
349                 if (mc) {
350                         if (mc->muted_by_self_or_masters()) {
351                                 b->set_color (selection_color);
352                                 b->set_state (Push2::LED::OneShot24th);
353                         } else if (mc->muted_by_others_soloing()) {
354                                 b->set_color (selection_color);
355                                 b->set_state (Push2::LED::Blinking8th);
356                         } else {
357                                 b->set_color (Push2::LED::DarkGray);
358                                 b->set_state (Push2::LED::OneShot24th);
359                         }
360
361                 } else {
362                         b->set_color (Push2::LED::DarkGray);
363                         b->set_state (Push2::LED::OneShot24th);
364                 }
365
366                 p2.write (b->state_msg());
367         }
368
369 }
370
371 void
372 TrackMixLayout::rec_enable_change ()
373 {
374         if (!stripable) {
375                 return;
376         }
377
378         simple_control_change (stripable->rec_enable_control(), Push2::Lower3);
379 }
380
381 void
382 TrackMixLayout::solo_iso_change ()
383 {
384         if (!stripable) {
385                 return;
386         }
387
388         simple_control_change (stripable->solo_isolate_control(), Push2::Lower6);
389 }
390 void
391 TrackMixLayout::solo_safe_change ()
392 {
393         if (!stripable) {
394                 return;
395         }
396
397         simple_control_change (stripable->solo_safe_control(), Push2::Lower7);
398 }
399
400 void
401 TrackMixLayout::monitoring_change ()
402 {
403         if (!stripable) {
404                 return;
405         }
406
407         if (!stripable->monitoring_control()) {
408                 return;
409         }
410
411         Push2::Button* b1 = p2.button_by_id (Push2::Lower4);
412         Push2::Button* b2 = p2.button_by_id (Push2::Lower5);
413         uint8_t b1_color;
414         uint8_t b2_color;
415
416         MonitorChoice mc = stripable->monitoring_control()->monitoring_choice ();
417
418         switch (mc) {
419         case MonitorAuto:
420                 b1_color = Push2::LED::DarkGray;
421                 b2_color = Push2::LED::DarkGray;
422                 break;
423         case MonitorInput:
424                 b1_color = selection_color;
425                 b2_color = Push2::LED::DarkGray;
426                 break;
427         case MonitorDisk:
428                 b1_color = Push2::LED::DarkGray;
429                 b2_color = selection_color;
430                 break;
431         case MonitorCue:
432                 b1_color = selection_color;
433                 b2_color = selection_color;
434                 break;
435         }
436
437         b1->set_color (b1_color);
438         b1->set_state (Push2::LED::OneShot24th);
439         p2.write (b1->state_msg());
440
441         b2->set_color (b2_color);
442         b2->set_state (Push2::LED::OneShot24th);
443         p2.write (b2->state_msg());
444 }
445
446 void
447 TrackMixLayout::show_state ()
448 {
449         if (!parent()) {
450                 return;
451         }
452
453         if (stripable) {
454                 name_changed ();
455                 color_changed ();
456                 solo_mute_change ();
457                 rec_enable_change ();
458                 solo_iso_change ();
459                 solo_safe_change ();
460                 monitoring_change ();
461
462                 meter->set_meter (stripable->peak_meter ().get());
463         } else {
464                 meter->set_meter (0);
465         }
466 }
467
468 void
469 TrackMixLayout::set_stripable (boost::shared_ptr<Stripable> s)
470 {
471         stripable_connections.drop_connections ();
472
473         stripable = s;
474
475         if (stripable) {
476
477                 stripable->DropReferences.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::drop_stripable, this), &p2);
478
479                 stripable->PropertyChanged.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::stripable_property_change, this, _1), &p2);
480                 stripable->presentation_info().PropertyChanged.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::stripable_property_change, this, _1), &p2);
481
482                 stripable->solo_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_mute_change, this), &p2);
483                 stripable->mute_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_mute_change, this), &p2);
484                 stripable->solo_isolate_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_iso_change, this), &p2);
485                 stripable->solo_safe_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_safe_change, this), &p2);
486
487                 if (stripable->rec_enable_control()) {
488                         stripable->rec_enable_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::rec_enable_change, this), &p2);
489                 }
490
491                 if (stripable->monitoring_control()) {
492                         stripable->monitoring_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::monitoring_change, this), &p2);
493                 }
494
495                 knobs[0]->set_controllable (stripable->gain_control());
496                 knobs[1]->set_controllable (stripable->pan_azimuth_control());
497                 knobs[1]->add_flag (Push2Knob::ArcToZero);
498                 knobs[2]->set_controllable (stripable->pan_width_control());
499                 knobs[3]->set_controllable (stripable->trim_control());
500                 knobs[3]->add_flag (Push2Knob::ArcToZero);
501                 knobs[4]->set_controllable (boost::shared_ptr<AutomationControl>());
502                 knobs[5]->set_controllable (boost::shared_ptr<AutomationControl>());
503                 knobs[6]->set_controllable (boost::shared_ptr<AutomationControl>());
504                 knobs[7]->set_controllable (boost::shared_ptr<AutomationControl>());
505         }
506
507         show_state ();
508 }
509
510 void
511 TrackMixLayout::drop_stripable ()
512 {
513         stripable_connections.drop_connections ();
514         stripable.reset ();
515 }
516
517 void
518 TrackMixLayout::name_changed ()
519 {
520         if (stripable) {
521
522                 name_text->set (stripable->name());
523
524                 /* right justify */
525
526                 Duple pos;
527                 pos.y = name_text->position().y;
528                 pos.x = display_width() - 10 - name_text->width();
529
530                 name_text->set_position (pos);
531         }
532 }
533
534 void
535 TrackMixLayout::color_changed ()
536 {
537         if (!parent()) {
538                 return;
539         }
540
541         Color rgba = stripable->presentation_info().color();
542         selection_color = p2.get_color_index (rgba);
543
544         name_text->set_color (rgba);
545
546         for (int n = 0; n < 8; ++n) {
547                 knobs[n]->set_text_color (rgba);
548                 knobs[n]->set_arc_start_color (rgba);
549                 knobs[n]->set_arc_end_color (rgba);
550         }
551 }
552
553 void
554 TrackMixLayout::stripable_property_change (PropertyChange const& what_changed)
555 {
556         if (what_changed.contains (Properties::color)) {
557                 color_changed ();
558         }
559         if (what_changed.contains (Properties::name)) {
560                 name_changed ();
561         }
562 }
563
564 void
565 TrackMixLayout::strip_vpot (int n, int delta)
566 {
567         boost::shared_ptr<Controllable> ac = knobs[n]->controllable();
568
569         if (ac) {
570                 ac->set_value (ac->get_value() + ((2.0/64.0) * delta), PBD::Controllable::UseGroup);
571         }
572 }
573
574 void
575 TrackMixLayout::strip_vpot_touch (int n, bool touching)
576 {
577         boost::shared_ptr<AutomationControl> ac = knobs[n]->controllable();
578         if (ac) {
579                 if (touching) {
580                         ac->start_touch (session.audible_frame());
581                 } else {
582                         ac->stop_touch (true, session.audible_frame());
583                 }
584         }
585 }
586
587 void
588 TrackMixLayout::update_meters ()
589 {
590         if (!stripable) {
591                 return;
592         }
593
594         meter->update_meters ();
595 }
596
597 void
598 TrackMixLayout::update_clocks ()
599 {
600         framepos_t pos = session.audible_frame();
601         bool negative = false;
602
603         if (pos < 0) {
604                 pos = -pos;
605                 negative = true;
606         }
607
608         char buf[16];
609         Timecode::BBT_Time BBT = session.tempo_map().bbt_at_frame (pos);
610
611 #define BBT_BAR_CHAR "|"
612
613         if (negative) {
614                 snprintf (buf, sizeof (buf), "-%03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
615                           BBT.bars, BBT.beats, BBT.ticks);
616         } else {
617                 snprintf (buf, sizeof (buf), " %03" PRIu32 BBT_BAR_CHAR "%02" PRIu32 BBT_BAR_CHAR "%04" PRIu32,
618                           BBT.bars, BBT.beats, BBT.ticks);
619         }
620
621         bbt_text->set (buf);
622
623         framecnt_t left;
624         int hrs;
625         int mins;
626         int secs;
627         int millisecs;
628
629         const double frame_rate = session.frame_rate ();
630
631         left = pos;
632         hrs = (int) floor (left / (frame_rate * 60.0f * 60.0f));
633         left -= (framecnt_t) floor (hrs * frame_rate * 60.0f * 60.0f);
634         mins = (int) floor (left / (frame_rate * 60.0f));
635         left -= (framecnt_t) floor (mins * frame_rate * 60.0f);
636         secs = (int) floor (left / (float) frame_rate);
637         left -= (framecnt_t) floor ((double)(secs * frame_rate));
638         millisecs = floor (left * 1000.0 / (float) frame_rate);
639
640         if (negative) {
641                 snprintf (buf, sizeof (buf), "-%02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
642         } else {
643                 snprintf (buf, sizeof (buf), " %02" PRId32 ":%02" PRId32 ":%02" PRId32 ".%03" PRId32, hrs, mins, secs, millisecs);
644         }
645
646         minsec_text->set (buf);
647 }