Attempt #46 to understand MIDI data locking.
[ardour2.git] / gtk2_ardour / latency_gui.cc
blob8d2dd3f1c6037e6a8f18c357b6f1283c2fbd43f3
1 /*
2 Copyright (C) 2009 Paul Davis
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.
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.
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.
20 #include <inttypes.h>
22 #include <iomanip>
23 #include "ardour/latent.h"
24 #include "pbd/convert.h"
25 #include <gtkmm2ext/utils.h>
27 #include "latency_gui.h"
29 #include "i18n.h"
31 using namespace PBD;
32 using namespace Gtk;
33 using namespace Gtkmm2ext;
34 using namespace ARDOUR;
37 static const gchar *_unit_strings[] = {
38 N_("sample"),
39 N_("msec"),
40 N_("period"),
44 std::vector<std::string> LatencyGUI::unit_strings;
46 std::string
47 LatencyBarController::get_label (double&)
49 double const nframes = _latency_gui->adjustment.get_value();
50 std::stringstream s;
52 if (nframes < (_latency_gui->sample_rate / 1000.0)) {
53 s << ((framepos_t) rint (nframes)) << " samples";
54 } else {
55 s << std::fixed << std::setprecision (2) << (nframes / (_latency_gui->sample_rate / 1000.0)) << " msecs";
58 return s.str ();
61 LatencyGUI::LatencyGUI (Latent& l, framepos_t sr, framepos_t psz)
62 : _latent (l),
63 initial_value (_latent.signal_latency()),
64 sample_rate (sr),
65 period_size (psz),
66 ignored (new PBD::IgnorableControllable()),
67 /* max 1 second, step by frames, page by msecs */
68 adjustment (initial_value, 0.0, sample_rate, 1.0, sample_rate / 1000.0f),
69 bc (adjustment, this),
70 reset_button (_("Reset"))
72 Widget* w;
74 if (unit_strings.empty()) {
75 unit_strings = I18N (_unit_strings);
78 set_popdown_strings (units_combo, unit_strings);
79 units_combo.set_active_text (unit_strings.front());
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);
88 hbox1.pack_start (bc, true, true);
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);
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));
101 adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &LatencyGUI::finish));
103 bc.set_size_request (-1, 25);
104 bc.set_style (BarController::LeftToRight);
105 bc.set_use_parent (true);
106 bc.set_name (X_("PluginSlider"));
108 set_spacing (12);
109 pack_start (hbox1, true, true);
110 pack_start (hbox2, true, true);
113 void
114 LatencyGUI::finish ()
116 framepos_t new_value = (framepos_t) adjustment.get_value();
117 if (new_value != initial_value) {
118 _latent.set_user_latency (new_value);
122 void
123 LatencyGUI::reset ()
125 _latent.set_user_latency (0);
126 adjustment.set_value (initial_value);
129 void
130 LatencyGUI::refresh ()
132 initial_value = _latent.signal_latency();
133 adjustment.set_value (initial_value);
136 void
137 LatencyGUI::change_latency_from_button (int dir)
139 std::string unitstr = units_combo.get_active_text();
140 double shift = 0.0;
142 if (unitstr == unit_strings[0]) {
143 shift = 1;
144 } else if (unitstr == unit_strings[1]) {
145 shift = (sample_rate / 1000.0);
146 } else if (unitstr == unit_strings[2]) {
147 shift = period_size;
148 } else {
149 fatal << string_compose (_("programming error: %1 (%2)"), X_("illegal string in latency GUI units combo"), unitstr)
150 << endmsg;
151 /*NOTREACHED*/
154 if (dir > 0) {
155 adjustment.set_value (adjustment.get_value() + shift);
156 } else {
157 adjustment.set_value (adjustment.get_value() - shift);
161 LatencyDialog::LatencyDialog (const std::string& title, Latent& l, framepos_t sr, framepos_t psz)
162 : ArdourDialog (title, false, true),
163 lwidget (l, sr, psz)
166 get_vbox()->pack_start (lwidget);
167 add_button (Stock::CANCEL, RESPONSE_CANCEL);
168 add_button (Stock::APPLY, RESPONSE_REJECT);
169 add_button (Stock::OK, RESPONSE_ACCEPT);
171 show_all ();
173 while (true) {
174 int ret = run ();
176 switch (ret) {
177 case RESPONSE_ACCEPT:
178 return;
179 break;
181 case RESPONSE_REJECT:
182 lwidget.finish ();
183 break;
184 default:
185 return;