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