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