push2: substantial improvements to track mix mode, and a few other details
[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/filesystem_paths.h"
39 #include "ardour/midiport_manager.h"
40 #include "ardour/midi_track.h"
41 #include "ardour/midi_port.h"
42 #include "ardour/monitor_control.h"
43 #include "ardour/session.h"
44 #include "ardour/solo_isolate_control.h"
45 #include "ardour/solo_safe_control.h"
46 #include "ardour/tempo.h"
47
48 #include "gtkmm2ext/gui_thread.h"
49 #include "gtkmm2ext/rgb_macros.h"
50
51 #include "canvas/rectangle.h"
52 #include "canvas/line.h"
53
54 #include "canvas.h"
55 #include "knob.h"
56 #include "menu.h"
57 #include "push2.h"
58 #include "track_mix.h"
59 #include "utils.h"
60
61 using namespace ARDOUR;
62 using namespace std;
63 using namespace PBD;
64 using namespace Glib;
65 using namespace ArdourSurface;
66 using namespace ArdourCanvas;
67
68 TrackMixLayout::TrackMixLayout (Push2& p, Session& s)
69         : Push2Layout (p, s)
70 {
71         Pango::FontDescription fd ("Sans 10");
72
73         bg = new Rectangle (this);
74         bg->set (Rect (0, 0, display_width(), display_height()));
75         bg->set_fill_color (p2.get_color (Push2::DarkBackground));
76
77         upper_line = new Line (this);
78         upper_line->set (Duple (0, 22.5), Duple (display_width(), 22.5));
79         upper_line->set_outline_color (p2.get_color (Push2::LightBackground));
80
81         for (int n = 0; n < 8; ++n) {
82                 Text* t;
83
84                 if (n < 4) {
85                         t = new Text (this);
86                         t->set_font_description (fd);
87                         t->set_color (p2.get_color (Push2::ParameterName));
88                         t->set_position ( Duple (10 + (n*Push2Canvas::inter_button_spacing()), 2));
89                         upper_text.push_back (t);
90                 }
91
92                 t = new Text (this);
93                 t->set_font_description (fd);
94                 t->set_color (p2.get_color (Push2::ParameterName));
95                 t->set_position (Duple (10 + (n*Push2Canvas::inter_button_spacing()), 140));
96
97                 lower_text.push_back (t);
98
99                 switch (n) {
100                 case 0:
101                         upper_text[n]->set (_("Track Volume"));
102                         lower_text[n]->set (_("Mute"));
103                         break;
104                 case 1:
105                         upper_text[n]->set (_("Track Pan"));
106                         lower_text[n]->set (_("Solo"));
107                         break;
108                 case 2:
109                         upper_text[n]->set (_("Track Width"));
110                         lower_text[n]->set (_("Rec-enable"));
111                         break;
112                 case 3:
113                         upper_text[n]->set (_("Track Trim"));
114                         lower_text[n]->set (_("In"));
115                         break;
116                 case 4:
117                         lower_text[n]->set (_("Disk"));
118                         break;
119                 case 5:
120                         lower_text[n]->set (_("Solo Iso"));
121                         break;
122                 case 6:
123                         lower_text[n]->set (_("Solo Lock"));
124                         break;
125                 case 7:
126                         lower_text[n]->set (_(""));
127                         break;
128                 }
129
130                 knobs[n] = new Push2Knob (p2, this);
131                 knobs[n]->set_position (Duple (60 + (Push2Canvas::inter_button_spacing()*n), 95));
132                 knobs[n]->set_radius (25);
133         }
134
135         name_text = new Text (this);
136         name_text->set_font_description (fd);
137         name_text->set_position (Duple (10 + (4*Push2Canvas::inter_button_spacing()), 2));
138
139         ControlProtocol::StripableSelectionChanged.connect (selection_connection, invalidator (*this), boost::bind (&TrackMixLayout::selection_changed, this), &p2);
140 }
141
142 TrackMixLayout::~TrackMixLayout ()
143 {
144         for (int n = 0; n < 8; ++n) {
145                 delete knobs[n];
146         }
147 }
148
149 void
150 TrackMixLayout::selection_changed ()
151 {
152         boost::shared_ptr<Stripable> s = ControlProtocol::first_selected_stripable();
153         if (s) {
154                 set_stripable (s);
155         }
156 }
157 void
158 TrackMixLayout::show ()
159 {
160         selection_changed ();
161
162         Push2::ButtonID lower_buttons[] = { Push2::Lower1, Push2::Lower2, Push2::Lower3, Push2::Lower4,
163                                             Push2::Lower5, Push2::Lower6, Push2::Lower7, Push2::Lower8 };
164
165         for (size_t n = 0; n < sizeof (lower_buttons) / sizeof (lower_buttons[0]); ++n) {
166                 Push2::Button* b = p2.button_by_id (lower_buttons[n]);
167                 b->set_color (Push2::LED::DarkGray);
168                 b->set_state (Push2::LED::OneShot24th);
169                 p2.write (b->state_msg());
170         }
171
172         Container::show ();
173 }
174
175 void
176 TrackMixLayout::hide ()
177 {
178         set_stripable (boost::shared_ptr<Stripable>());
179 }
180
181 void
182 TrackMixLayout::render (ArdourCanvas::Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
183 {
184         Container::render (area, context);
185 }
186
187 void
188 TrackMixLayout::button_upper (uint32_t n)
189 {
190 }
191
192 void
193 TrackMixLayout::button_lower (uint32_t n)
194 {
195         if (!stripable) {
196                 return;
197         }
198
199         MonitorChoice mc;
200
201         switch (n) {
202         case 0:
203                 stripable->mute_control()->set_value (!stripable->mute_control()->get_value(), PBD::Controllable::UseGroup);
204                 break;
205         case 1:
206                 stripable->solo_control()->set_value (!stripable->solo_control()->get_value(), PBD::Controllable::UseGroup);
207                 break;
208         case 2:
209                 stripable->rec_enable_control()->set_value (!stripable->rec_enable_control()->get_value(), PBD::Controllable::UseGroup);
210                 break;
211         case 3:
212                 mc = stripable->monitoring_control()->monitoring_choice();
213                 switch (mc) {
214                 case MonitorInput:
215                         stripable->monitoring_control()->set_value (MonitorAuto, PBD::Controllable::UseGroup);
216                         break;
217                 default:
218                         stripable->monitoring_control()->set_value (MonitorInput, PBD::Controllable::UseGroup);
219                         break;
220                 }
221                 break;
222         case 4:
223                 mc = stripable->monitoring_control()->monitoring_choice();
224                 switch (mc) {
225                 case MonitorDisk:
226                         stripable->monitoring_control()->set_value (MonitorAuto, PBD::Controllable::UseGroup);
227                         break;
228                 default:
229                         stripable->monitoring_control()->set_value (MonitorDisk, PBD::Controllable::UseGroup);
230                         break;
231                 }
232                 break;
233         case 5:
234                 stripable->solo_isolate_control()->set_value (!stripable->solo_isolate_control()->get_value(), PBD::Controllable::UseGroup);
235                 break;
236         case 6:
237                 stripable->solo_safe_control()->set_value (!stripable->solo_safe_control()->get_value(), PBD::Controllable::UseGroup);
238                 break;
239         case 7:
240                 /* nothing here */
241                 break;
242         }
243 }
244
245 void
246 TrackMixLayout::button_left ()
247 {
248         p2.access_action ("Editor/select-prev-route");
249 }
250
251 void
252 TrackMixLayout::button_right ()
253 {
254         p2.access_action ("Editor/select-next-route");
255 }
256
257 void
258 TrackMixLayout::simple_control_change (boost::shared_ptr<AutomationControl> ac, Push2::ButtonID bid)
259 {
260         if (!ac) {
261                 return;
262         }
263
264         Push2::Button* b = p2.button_by_id (bid);
265
266         if (!bid) {
267                 return;
268         }
269
270
271         if (ac->get_value()) {
272                 b->set_color (selection_color);
273         } else {
274                 b->set_color (Push2::LED::DarkGray);
275         }
276         b->set_state (Push2::LED::OneShot24th);
277         p2.write (b->state_msg());
278 }
279
280 void
281 TrackMixLayout::solo_change ()
282 {
283         if (!stripable) {
284                 return;
285         }
286
287         simple_control_change (stripable->solo_control(), Push2::Lower2);
288 }
289
290 void
291 TrackMixLayout::mute_change ()
292 {
293         if (!stripable) {
294                 return;
295         }
296
297         simple_control_change (stripable->mute_control(), Push2::Lower1);
298 }
299
300 void
301 TrackMixLayout::rec_enable_change ()
302 {
303         if (!stripable) {
304                 return;
305         }
306
307         simple_control_change (stripable->rec_enable_control(), Push2::Lower3);
308 }
309
310 void
311 TrackMixLayout::solo_iso_change ()
312 {
313         if (!stripable) {
314                 return;
315         }
316
317         simple_control_change (stripable->solo_isolate_control(), Push2::Lower6);
318 }
319 void
320 TrackMixLayout::solo_safe_change ()
321 {
322         if (!stripable) {
323                 return;
324         }
325
326         simple_control_change (stripable->solo_safe_control(), Push2::Lower7);
327 }
328
329 void
330 TrackMixLayout::monitoring_change ()
331 {
332         if (!stripable) {
333                 return;
334         }
335
336         if (!stripable->monitoring_control()) {
337                 return;
338         }
339
340         Push2::Button* b1 = p2.button_by_id (Push2::Lower4);
341         Push2::Button* b2 = p2.button_by_id (Push2::Lower5);
342         uint8_t b1_color;
343         uint8_t b2_color;
344
345         MonitorChoice mc = stripable->monitoring_control()->monitoring_choice ();
346
347         switch (mc) {
348         case MonitorAuto:
349                 b1_color = Push2::LED::DarkGray;
350                 b2_color = Push2::LED::DarkGray;
351                 break;
352         case MonitorInput:
353                 b1_color = selection_color;
354                 b2_color = Push2::LED::DarkGray;
355                 break;
356         case MonitorDisk:
357                 b1_color = Push2::LED::DarkGray;
358                 b2_color = selection_color;
359                 break;
360         case MonitorCue:
361                 b1_color = selection_color;
362                 b2_color = selection_color;
363                 break;
364         }
365
366         b1->set_color (b1_color);
367         b1->set_state (Push2::LED::OneShot24th);
368         p2.write (b1->state_msg());
369
370         b2->set_color (b2_color);
371         b2->set_state (Push2::LED::OneShot24th);
372         p2.write (b2->state_msg());
373 }
374
375 void
376 TrackMixLayout::set_stripable (boost::shared_ptr<Stripable> s)
377 {
378         stripable_connections.drop_connections ();
379
380         stripable = s;
381
382         if (stripable) {
383
384                 stripable->DropReferences.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::drop_stripable, this), &p2);
385
386                 stripable->PropertyChanged.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::stripable_property_change, this, _1), &p2);
387                 stripable->presentation_info().PropertyChanged.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::stripable_property_change, this, _1), &p2);
388
389                 stripable->solo_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_change, this), &p2);
390                 stripable->mute_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::mute_change, this), &p2);
391                 stripable->solo_isolate_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_iso_change, this), &p2);
392                 stripable->solo_safe_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::solo_safe_change, this), &p2);
393
394                 if (stripable->rec_enable_control()) {
395                         stripable->rec_enable_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::rec_enable_change, this), &p2);
396                 }
397
398                 if (stripable->monitoring_control()) {
399                         stripable->monitoring_control()->Changed.connect (stripable_connections, invalidator (*this), boost::bind (&TrackMixLayout::monitoring_change, this), &p2);
400                 }
401
402                 knobs[0]->set_controllable (stripable->gain_control());
403                 knobs[1]->set_controllable (stripable->pan_azimuth_control());
404                 knobs[1]->add_flag (Push2Knob::ArcToZero);
405                 knobs[2]->set_controllable (stripable->pan_width_control());
406                 knobs[3]->set_controllable (stripable->trim_control());
407                 knobs[3]->add_flag (Push2Knob::ArcToZero);
408                 knobs[4]->set_controllable (boost::shared_ptr<AutomationControl>());
409                 knobs[5]->set_controllable (boost::shared_ptr<AutomationControl>());
410                 knobs[6]->set_controllable (boost::shared_ptr<AutomationControl>());
411                 knobs[7]->set_controllable (boost::shared_ptr<AutomationControl>());
412
413                 name_changed ();
414                 color_changed ();
415                 solo_change ();
416                 mute_change ();
417                 rec_enable_change ();
418                 solo_iso_change ();
419                 solo_safe_change ();
420                 monitoring_change ();
421         }
422 }
423
424 void
425 TrackMixLayout::drop_stripable ()
426 {
427         stripable_connections.drop_connections ();
428         stripable.reset ();
429 }
430
431 void
432 TrackMixLayout::name_changed ()
433 {
434         if (stripable) {
435                 /* poor-man's right justification */
436                 char buf[96];
437                 snprintf (buf, sizeof (buf), "%*s", (int) sizeof (buf) - 1, stripable->name().c_str());
438                 name_text->set (buf);
439         }
440 }
441
442 void
443 TrackMixLayout::color_changed ()
444 {
445         if (!parent()) {
446                 return;
447         }
448
449         Color rgba = stripable->presentation_info().color();
450         selection_color = p2.get_color_index (rgba);
451
452         name_text->set_color (rgba);
453
454         for (int n = 0; n < 8; ++n) {
455                 knobs[n]->set_text_color (rgba);
456                 knobs[n]->set_arc_start_color (rgba);
457                 knobs[n]->set_arc_end_color (rgba);
458         }
459 }
460
461 void
462 TrackMixLayout::stripable_property_change (PropertyChange const& what_changed)
463 {
464         if (what_changed.contains (Properties::color)) {
465                 color_changed ();
466         }
467         if (what_changed.contains (Properties::name)) {
468                 name_changed ();
469         }
470 }
471
472 void
473 TrackMixLayout::strip_vpot (int n, int delta)
474 {
475         boost::shared_ptr<Controllable> ac = knobs[n]->controllable();
476
477         if (ac) {
478                 ac->set_value (ac->get_value() + ((2.0/64.0) * delta), PBD::Controllable::UseGroup);
479         }
480 }
481
482 void
483 TrackMixLayout::strip_vpot_touch (int n, bool touching)
484 {
485         boost::shared_ptr<AutomationControl> ac = knobs[n]->controllable();
486         if (ac) {
487                 if (touching) {
488                         ac->start_touch (session.audible_frame());
489                 } else {
490                         ac->stop_touch (true, session.audible_frame());
491                 }
492         }
493 }