use new method in MidiPatchManager to use MIDNAM data when setting a MidiTimeAxisView
[ardour.git] / gtk2_ardour / latency_gui.cc
1 /*
2  * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2008-2011 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <inttypes.h>
23 #include <iomanip>
24
25 #include <gtkmm/stock.h>
26
27 #include "pbd/convert.h"
28 #include "pbd/error.h"
29 #include "pbd/unwind.h"
30
31 #include "ardour/latent.h"
32
33 #include "gtkmm2ext/utils.h"
34
35 #include "latency_gui.h"
36 #include "utils.h"
37
38 #include "pbd/i18n.h"
39
40 using namespace PBD;
41 using namespace Gtk;
42 using namespace Gtkmm2ext;
43 using namespace ARDOUR;
44
45
46 static const gchar *_unit_strings[] = {
47         N_("sample"),
48         N_("msec"),
49         N_("period"),
50         0
51 };
52
53 std::vector<std::string> LatencyGUI::unit_strings;
54
55 std::string
56 LatencyBarController::get_label (double&)
57 {
58         return ARDOUR_UI_UTILS::samples_as_time_string (
59                         _latency_gui->adjustment.get_value(), _latency_gui->sample_rate, true);
60 }
61
62 LatencyGUI::LatencyGUI (Latent& l, samplepos_t sr, samplepos_t psz)
63         : _latent (l)
64         , sample_rate (sr)
65         , period_size (psz)
66         , ignored (new PBD::IgnorableControllable())
67         , _ignore_change (false)
68         , adjustment (0, 0.0, sample_rate, 1.0, sample_rate / 1000.0f) /* max 1 second, step by samples, page by msecs */
69         , bc (adjustment, this)
70         , reset_button (_("Reset"))
71 {
72         Widget* w;
73
74         if (unit_strings.empty()) {
75                 unit_strings = I18N (_unit_strings);
76         }
77
78         set_popdown_strings (units_combo, unit_strings);
79         units_combo.set_active_text (unit_strings.front());
80
81         w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
82         w->show ();
83         plus_button.add (*w);
84         w = manage (new Image (Stock::REMOVE, ICON_SIZE_BUTTON));
85         w->show ();
86         minus_button.add (*w);
87
88         hbox1.pack_start (bc, true, true);
89
90         hbox2.set_homogeneous (false);
91         hbox2.set_spacing (12);
92         hbox2.pack_start (reset_button);
93         hbox2.pack_start (minus_button);
94         hbox2.pack_start (plus_button);
95         hbox2.pack_start (units_combo, true, true);
96
97         minus_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &LatencyGUI::change_latency_from_button), -1));
98         plus_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &LatencyGUI::change_latency_from_button), 1));
99         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &LatencyGUI::reset));
100
101         /* Limit value to adjustment range (max = sample_rate).
102          * Otherwise if the signal_latency() is larger than the adjustment's max,
103          * LatencyGUI::finish() would set the adjustment's max value as custom-latency.
104          */
105         adjustment.set_value (std::min (sample_rate, _latent.signal_latency ()));
106
107         adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &LatencyGUI::finish));
108
109         bc.set_size_request (-1, 25);
110         bc.set_name (X_("ProcessorControlSlider"));
111
112         set_spacing (12);
113         pack_start (hbox1, true, true);
114         pack_start (hbox2, true, true);
115 }
116
117 void
118 LatencyGUI::finish ()
119 {
120         if (_ignore_change) {
121                 return;
122         }
123         samplepos_t new_value = (samplepos_t) adjustment.get_value();
124         _latent.set_user_latency (new_value);
125 }
126
127 void
128 LatencyGUI::reset ()
129 {
130         _latent.unset_user_latency ();
131         PBD::Unwinder<bool> uw (_ignore_change, true);
132         adjustment.set_value (std::min (sample_rate, _latent.signal_latency ()));
133 }
134
135 void
136 LatencyGUI::refresh ()
137 {
138         PBD::Unwinder<bool> uw (_ignore_change, true);
139         adjustment.set_value (std::min (sample_rate, _latent.effective_latency ()));
140 }
141
142 void
143 LatencyGUI::change_latency_from_button (int dir)
144 {
145         std::string unitstr = units_combo.get_active_text();
146         double shift = 0.0;
147
148         if (unitstr == unit_strings[0]) {
149                 shift = 1;
150         } else if (unitstr == unit_strings[1]) {
151                 shift = (sample_rate / 1000.0);
152         } else if (unitstr == unit_strings[2]) {
153                 shift = period_size;
154         } else {
155                 fatal << string_compose (_("programming error: %1 (%2)"), X_("illegal string in latency GUI units combo"), unitstr)
156                       << endmsg;
157                 abort(); /*NOTREACHED*/
158         }
159
160         if (dir > 0) {
161                 adjustment.set_value (adjustment.get_value() + shift);
162         } else {
163                 adjustment.set_value (adjustment.get_value() - shift);
164         }
165 }