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