2 * Copyright (C) 2010-2011 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2010-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2011-2012 David Robillard <d@drobilla.net>
5 * Copyright (C) 2013-2017 Robin Gareus <robin@gareus.org>
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.
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.
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.
22 #include <glibmm/objectbase.h>
24 #include <gtkmm/messagedialog.h>
25 #include <gtkmm/stock.h>
27 #include "ardour/audioengine.h"
28 #include "ardour/mtdm.h"
29 #include "ardour/port_insert.h"
30 #include "ardour/session.h"
32 #include "gtkmm2ext/doi.h"
34 #include "port_insert_ui.h"
35 #include "gui_thread.h"
38 using namespace ARDOUR;
41 PortInsertUI::PortInsertUI (Gtk::Window* parent, ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
43 , latency_button (_("Measure Latency"))
44 , input_selector (parent, sess, pi->input())
45 , output_selector (parent, sess, pi->output())
47 latency_hbox.pack_start (latency_button, false, false);
48 latency_hbox.pack_start (latency_display, false, false);
49 latency_hbox.set_spacing (4);
51 output_selector.set_min_height_divisor (2);
52 input_selector.set_min_height_divisor (2);
54 notebook.append_page (output_selector, _("Send/Output"));
55 notebook.append_page (input_selector, _("Return/Input"));
57 notebook.set_current_page (0);
60 pack_start (notebook, true, true);
61 pack_start (latency_hbox, false, false);
63 update_latency_display ();
65 latency_button.signal_toggled().connect (mem_fun (*this, &PortInsertUI::latency_button_toggled));
66 latency_button.set_name (X_("MeasureLatencyButton"));
70 PortInsertUI::update_latency_display ()
72 samplecnt_t const sample_rate = AudioEngine::instance()->sample_rate();
73 if (sample_rate == 0) {
74 latency_display.set_text (_("Disconnected from audio engine"));
77 snprintf (buf, sizeof (buf), "%10.3lf samples %10.3lf ms",
78 (float)_pi->latency(), (float)_pi->latency() * 1000.0f/sample_rate);
79 latency_display.set_text(buf);
84 PortInsertUI::check_latency_measurement ()
86 MTDM* mtdm = _pi->mtdm ();
88 if (mtdm->resolve () < 0) {
89 latency_display.set_text (_("No signal detected"));
93 if (mtdm->err () > 0.3) {
99 samplecnt_t const sample_rate = AudioEngine::instance()->sample_rate();
101 if (sample_rate == 0) {
102 latency_display.set_text (_("Disconnected from audio engine"));
103 _pi->stop_latency_detection ();
107 snprintf (buf, sizeof (buf), "%10.3lf samples %10.3lf ms", mtdm->del (), mtdm->del () * 1000.0f/sample_rate);
111 if (mtdm->err () > 0.2) {
117 strcat (buf, " (Inv)");
122 _pi->set_measured_latency (rint (mtdm->del()));
123 latency_button.set_active (false);
124 strcat (buf, " (set)");
127 latency_display.set_text (buf);
133 PortInsertUI::latency_button_toggled ()
135 if (latency_button.get_active ()) {
137 _pi->start_latency_detection ();
138 latency_display.set_text (_("Detecting ..."));
139 latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &PortInsertUI::check_latency_measurement), 250);
142 _pi->stop_latency_detection ();
143 latency_timeout.disconnect ();
144 update_latency_display ();
149 PortInsertUI::redisplay ()
151 input_selector.setup_ports (input_selector.other());
152 output_selector.setup_ports (output_selector.other());
156 PortInsertUI::finished (IOSelector::Result r)
158 input_selector.Finished (r);
159 output_selector.Finished (r);
163 PortInsertWindow::PortInsertWindow (ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
164 : ArdourDialog ("port insert dialog"),
165 _portinsertui (this, sess, pi)
168 set_name ("IOSelectorWindow");
169 std::string title = _("Port Insert ");
173 get_vbox()->pack_start (_portinsertui);
175 Gtk::Button* cancel_but = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
176 Gtk::Button* ok_but = add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
178 cancel_but->signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::cancel));
179 ok_but->signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::accept));
181 signal_delete_event().connect (sigc::mem_fun (*this, &PortInsertWindow::wm_delete), false);
185 PortInsertWindow::wm_delete (GdkEventAny* /*event*/)
192 PortInsertWindow::on_map ()
194 _portinsertui.redisplay ();
200 PortInsertWindow::cancel ()
202 _portinsertui.finished (IOSelector::Cancelled);
207 PortInsertWindow::accept ()
209 _portinsertui.finished (IOSelector::Accepted);