use new canvas meter in push2 track mix layout
[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)
75         : Push2Layout (p, s)
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, 200, ArdourCanvas::Meter::Horizontal);
146         meter->set_position (Duple (10 + (4 * Push2Canvas::inter_button_spacing()), 50));
147
148         ControlProtocol::StripableSelectionChanged.connect (selection_connection, invalidator (*this), boost::bind (&TrackMixLayout::selection_changed, this), &p2);
149 }
150
151 TrackMixLayout::~TrackMixLayout ()
152 {
153         for (int n = 0; n < 8; ++n) {
154                 delete knobs[n];
155         }
156 }
157
158 void
159 TrackMixLayout::selection_changed ()
160 {
161         boost::shared_ptr<Stripable> s = ControlProtocol::first_selected_stripable();
162         if (s) {
163                 set_stripable (s);
164         }
165 }
166
167 void
168 TrackMixLayout::show ()
169 {
170         selection_changed ();
171
172         Push2::ButtonID lower_buttons[] = { Push2::Lower1, Push2::Lower2, Push2::Lower3, Push2::Lower4,
173                                             Push2::Lower5, Push2::Lower6, Push2::Lower7, Push2::Lower8 };
174
175         for (size_t n = 0; n < sizeof (lower_buttons) / sizeof (lower_buttons[0]); ++n) {
176                 Push2::Button* b = p2.button_by_id (lower_buttons[n]);
177                 b->set_color (Push2::LED::DarkGray);
178                 b->set_state (Push2::LED::OneShot24th);
179                 p2.write (b->state_msg());
180         }
181
182         Container::show ();
183 }
184
185 void
186 TrackMixLayout::hide ()
187 {
188         set_stripable (boost::shared_ptr<Stripable>());
189 }
190
191 void
192 TrackMixLayout::render (ArdourCanvas::Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
193 {
194         Container::render (area, context);
195 }
196
197 void
198 TrackMixLayout::button_upper (uint32_t n)
199 {
200 }
201
202 void
203 TrackMixLayout::button_lower (uint32_t n)
204 {
205         if (!stripable) {
206                 return;
207         }
208
209         MonitorChoice mc;
210
211         switch (n) {
212         case 0:
213                 stripable->mute_control()->set_value (!stripable->mute_control()->get_value(), PBD::Controllable::UseGroup);
214                 break;
215         case 1:
216                 stripable->solo_control()->set_value (!stripable->solo_control()->get_value(), PBD::Controllable::UseGroup);
217                 break;
218         case 2:
219                 stripable->rec_enable_control()->set_value (!stripable->rec_enable_control()->get_value(), PBD::Controllable::UseGroup);
220                 break;
221         case 3:
222                 mc = stripable->monitoring_control()->monitoring_choice();
223                 switch (mc) {
224                 case MonitorInput:
225                         stripable->monitoring_control()->set_value (MonitorAuto, PBD::Controllable::UseGroup);
226                         break;
227                 default:
228                         stripable->monitoring_control()->set_value (MonitorInput, PBD::Controllable::UseGroup);
229                         break;
230                 }
231                 break;
232         case 4:
233                 mc = stripable->monitoring_control()->monitoring_choice();
234                 switch (mc) {
235                 case MonitorDisk:
236                         stripable->monitoring_control()->set_value (MonitorAuto, PBD::Controllable::UseGroup);
237                         break;
238                 default:
239                         stripable->monitoring_control()->set_value (MonitorDisk, PBD::Controllable::UseGroup);
240                         break;
241                 }
242                 break;
243         case 5:
244                 stripable->solo_isolate_control()->set_value (!stripable->solo_isolate_control()->get_value(), PBD::Controllable::UseGroup);
245                 break;
246         case 6:
247                 stripable->solo_safe_control()->set_value (!stripable->solo_safe_control()->get_value(), PBD::Controllable::UseGroup);
248                 break;
249         case 7:
250                 /* nothing here */
251                 break;
252         }
253 }
254
255 void
256 TrackMixLayout::button_left ()
257 {
258         p2.access_action ("Editor/select-prev-route");
259 }
260
261 void
262 TrackMixLayout::button_right ()
263 {
264         p2.access_action ("Editor/select-next-route");
265 }
266
267 void
268 TrackMixLayout::simple_control_change (boost::shared_ptr<AutomationControl> ac, Push2::ButtonID bid)
269 {
270         if (!ac) {
271                 return;
272         }
273
274         Push2::Button* b = p2.button_by_id (bid);
275
276         if (!bid) {
277                 return;
278         }
279
280
281         if (ac->get_value()) {
282                 b->set_color (selection_color);
283         } else {
284                 b->set_color (Push2::LED::DarkGray);
285         }
286         b->set_state (Push2::LED::OneShot24th);
287         p2.write (b->state_msg());
288 }
289
290 void
291 TrackMixLayout::solo_change ()
292 {
293         if (!stripable) {
294                 return;
295         }
296
297         simple_control_change (stripable->solo_control(), Push2::Lower2);
298 }
299
300 void
301 TrackMixLayout::mute_change ()
302 {
303         if (!stripable) {
304                 return;
305         }
306
307         simple_control_change (stripable->mute_control(), Push2::Lower1);
308 }
309
310 void
311 TrackMixLayout::rec_enable_change ()
312 {
313         if (!stripable) {
314                 return;
315         }
316
317         simple_control_change (stripable->rec_enable_control(), Push2::Lower3);
318 }
319
320 void
321 TrackMixLayout::solo_iso_change ()
322 {
323         if (!stripable) {
324                 return;
325         }
326
327         simple_control_change (stripable->solo_isolate_control(), Push2::Lower6);
328 }
329 void
330 TrackMixLayout::solo_safe_change ()
331 {
332         if (!stripable) {
333                 return;
334         }
335
336         simple_control_change (stripable->solo_safe_control(), Push2::Lower7);
337 }
338
339 void
340 TrackMixLayout::monitoring_change ()
341 {
342         if (!stripable) {
343                 return;
344         }
345
346         if (!stripable->monitoring_control()) {
347                 return;
348         }
349
350         Push2::Button* b1 = p2.button_by_id (Push2::Lower4);
351         Push2::Button* b2 = p2.button_by_id (Push2::Lower5);
352         uint8_t b1_color;
353         uint8_t b2_color;
354
355         MonitorChoice mc = stripable->monitoring_control()->monitoring_choice ();
356
357         switch (mc) {
358         case MonitorAuto:
359                 b1_color = Push2::LED::DarkGray;
360                 b2_color = Push2::LED::DarkGray;
361                 break;
362         case MonitorInput:
363                 b1_color = selection_color;
364                 b2_color = Push2::LED::DarkGray;
365                 break;
366         case MonitorDisk:
367                 b1_color = Push2::LED::DarkGray;
368                 b2_color = selection_color;
369                 break;
370         case MonitorCue:
371                 b1_color = selection_color;
372                 b2_color = selection_color;
373                 break;
374         }
375
376         b1->set_color (b1_color);
377         b1->set_state (Push2::LED::OneShot24th);
378         p2.write (b1->state_msg());
379
380         b2->set_color (b2_color);
381         b2->set_state (Push2::LED::OneShot24th);
382         p2.write (b2->state_msg());
383 }
384
385 void
386 TrackMixLayout::set_stripable (boost::shared_ptr<Stripable> s)
387 {
388         stripable_connections.drop_connections ();
389
390         stripable = s;
391
392         if (stripable) {
393
394                 stripable->DropReferences.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::drop_stripable, this), &p2);
395
396                 stripable->PropertyChanged.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::stripable_property_change, this, _1), &p2);
397                 stripable->presentation_info().PropertyChanged.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::stripable_property_change, this, _1), &p2);
398
399                 stripable->solo_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_change, this), &p2);
400                 stripable->mute_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::mute_change, this), &p2);
401                 stripable->solo_isolate_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_iso_change, this), &p2);
402                 stripable->solo_safe_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_safe_change, this), &p2);
403
404                 if (stripable->rec_enable_control()) {
405                         stripable->rec_enable_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::rec_enable_change, this), &p2);
406                 }
407
408                 if (stripable->monitoring_control()) {
409                         stripable->monitoring_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::monitoring_change, this), &p2);
410                 }
411
412                 knobs[0]->set_controllable (stripable->gain_control());
413                 knobs[1]->set_controllable (stripable->pan_azimuth_control());
414                 knobs[1]->add_flag (Push2Knob::ArcToZero);
415                 knobs[2]->set_controllable (stripable->pan_width_control());
416                 knobs[3]->set_controllable (stripable->trim_control());
417                 knobs[3]->add_flag (Push2Knob::ArcToZero);
418                 knobs[4]->set_controllable (boost::shared_ptr<AutomationControl>());
419                 knobs[5]->set_controllable (boost::shared_ptr<AutomationControl>());
420                 knobs[6]->set_controllable (boost::shared_ptr<AutomationControl>());
421                 knobs[7]->set_controllable (boost::shared_ptr<AutomationControl>());
422
423                 name_changed ();
424                 color_changed ();
425                 solo_change ();
426                 mute_change ();
427                 rec_enable_change ();
428                 solo_iso_change ();
429                 solo_safe_change ();
430                 monitoring_change ();
431
432                 meter->set_meter (stripable->peak_meter ().get());
433         } else {
434                 meter->set_meter (0);
435         }
436 }
437
438 void
439 TrackMixLayout::drop_stripable ()
440 {
441         stripable_connections.drop_connections ();
442         stripable.reset ();
443 }
444
445 void
446 TrackMixLayout::name_changed ()
447 {
448         if (stripable) {
449                 /* poor-man's right justification */
450                 char buf[96];
451                 snprintf (buf, sizeof (buf), "%*s", (int) sizeof (buf) - 1, stripable->name().c_str());
452                 name_text->set (buf);
453         }
454 }
455
456 void
457 TrackMixLayout::color_changed ()
458 {
459         if (!parent()) {
460                 return;
461         }
462
463         Color rgba = stripable->presentation_info().color();
464         selection_color = p2.get_color_index (rgba);
465
466         name_text->set_color (rgba);
467
468         for (int n = 0; n < 8; ++n) {
469                 knobs[n]->set_text_color (rgba);
470                 knobs[n]->set_arc_start_color (rgba);
471                 knobs[n]->set_arc_end_color (rgba);
472         }
473 }
474
475 void
476 TrackMixLayout::stripable_property_change (PropertyChange const& what_changed)
477 {
478         if (what_changed.contains (Properties::color)) {
479                 color_changed ();
480         }
481         if (what_changed.contains (Properties::name)) {
482                 name_changed ();
483         }
484 }
485
486 void
487 TrackMixLayout::strip_vpot (int n, int delta)
488 {
489         boost::shared_ptr<Controllable> ac = knobs[n]->controllable();
490
491         if (ac) {
492                 ac->set_value (ac->get_value() + ((2.0/64.0) * delta), PBD::Controllable::UseGroup);
493         }
494 }
495
496 void
497 TrackMixLayout::strip_vpot_touch (int n, bool touching)
498 {
499         boost::shared_ptr<AutomationControl> ac = knobs[n]->controllable();
500         if (ac) {
501                 if (touching) {
502                         ac->start_touch (session.audible_frame());
503                 } else {
504                         ac->stop_touch (true, session.audible_frame());
505                 }
506         }
507 }
508
509 void
510 TrackMixLayout::update_meters ()
511 {
512         if (!stripable) {
513                 return;
514         }
515
516         meter->update_meters ();
517 }