Update GPL boilerplate and (C)
[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
37 #include "pbd/i18n.h"
38
39 using namespace PBD;
40 using namespace Gtk;
41 using namespace Gtkmm2ext;
42 using namespace ARDOUR;
43
44
45 static const gchar *_unit_strings[] = {
46         N_("sample"),
47         N_("msec"),
48         N_("period"),
49         0
50 };
51
52 std::vector<std::string> LatencyGUI::unit_strings;
53
54 std::string
55 LatencyBarController::get_label (double&)
56 {
57         double const nframes = _latency_gui->adjustment.get_value();
58         std::stringstream s;
59
60         if (nframes < (_latency_gui->sample_rate / 1000.0)) {
61                 const samplepos_t nf = (samplepos_t) rint (nframes);
62                 s << string_compose (P_("%1 sample", "%1 samples", nf), nf);
63         } else {
64                 s << std::fixed << std::setprecision (2) << (nframes / (_latency_gui->sample_rate / 1000.0)) << " ms";
65         }
66
67         return s.str ();
68 }
69
70 LatencyGUI::LatencyGUI (Latent& l, samplepos_t sr, samplepos_t psz)
71         : _latent (l)
72         , sample_rate (sr)
73         , period_size (psz)
74         , ignored (new PBD::IgnorableControllable())
75         , _ignore_change (false)
76         , adjustment (0, 0.0, sample_rate, 1.0, sample_rate / 1000.0f) /* max 1 second, step by samples, page by msecs */
77         , bc (adjustment, this)
78         , reset_button (_("Reset"))
79 {
80         Widget* w;
81
82         if (unit_strings.empty()) {
83                 unit_strings = I18N (_unit_strings);
84         }
85
86         set_popdown_strings (units_combo, unit_strings);
87         units_combo.set_active_text (unit_strings.front());
88
89         w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
90         w->show ();
91         plus_button.add (*w);
92         w = manage (new Image (Stock::REMOVE, ICON_SIZE_BUTTON));
93         w->show ();
94         minus_button.add (*w);
95
96         hbox1.pack_start (bc, true, true);
97
98         hbox2.set_homogeneous (false);
99         hbox2.set_spacing (12);
100         hbox2.pack_start (reset_button);
101         hbox2.pack_start (minus_button);
102         hbox2.pack_start (plus_button);
103         hbox2.pack_start (units_combo, true, true);
104
105         minus_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &LatencyGUI::change_latency_from_button), -1));
106         plus_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &LatencyGUI::change_latency_from_button), 1));
107         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &LatencyGUI::reset));
108
109         /* Limit value to adjustment range (max = sample_rate).
110          * Otherwise if the signal_latency() is larger than the adjustment's max,
111          * LatencyGUI::finish() would set the adjustment's max value as custom-latency.
112          */
113         adjustment.set_value (std::min (sample_rate, _latent.signal_latency ()));
114
115         adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &LatencyGUI::finish));
116
117         bc.set_size_request (-1, 25);
118         bc.set_name (X_("ProcessorControlSlider"));
119
120         set_spacing (12);
121         pack_start (hbox1, true, true);
122         pack_start (hbox2, true, true);
123 }
124
125 void
126 LatencyGUI::finish ()
127 {
128         if (_ignore_change) {
129                 return;
130         }
131         samplepos_t new_value = (samplepos_t) adjustment.get_value();
132         _latent.set_user_latency (new_value);
133 }
134
135 void
136 LatencyGUI::reset ()
137 {
138         _latent.unset_user_latency ();
139         PBD::Unwinder<bool> uw (_ignore_change, true);
140         adjustment.set_value (std::min (sample_rate, _latent.signal_latency ()));
141 }
142
143 void
144 LatencyGUI::refresh ()
145 {
146         PBD::Unwinder<bool> uw (_ignore_change, true);
147         adjustment.set_value (std::min (sample_rate, _latent.effective_latency ()));
148 }
149
150 void
151 LatencyGUI::change_latency_from_button (int dir)
152 {
153         std::string unitstr = units_combo.get_active_text();
154         double shift = 0.0;
155
156         if (unitstr == unit_strings[0]) {
157                 shift = 1;
158         } else if (unitstr == unit_strings[1]) {
159                 shift = (sample_rate / 1000.0);
160         } else if (unitstr == unit_strings[2]) {
161                 shift = period_size;
162         } else {
163                 fatal << string_compose (_("programming error: %1 (%2)"), X_("illegal string in latency GUI units combo"), unitstr)
164                       << endmsg;
165                 abort(); /*NOTREACHED*/
166         }
167
168         if (dir > 0) {
169                 adjustment.set_value (adjustment.get_value() + shift);
170         } else {
171                 adjustment.set_value (adjustment.get_value() - shift);
172         }
173 }
174
175 LatencyDialog::LatencyDialog (const std::string& title, Latent& l, samplepos_t sr, samplepos_t psz)
176         : ArdourDialog (title, false, true),
177           lwidget (l, sr, psz)
178 {
179         get_vbox()->pack_start (lwidget);
180         add_button (Stock::CLOSE, RESPONSE_CLOSE);
181
182         show_all ();
183         run ();
184 }