2 Copyright (C) 2002-2007 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 <gtkmm/messagedialog.h>
21 #include <glibmm/objectbase.h>
23 #include <gtkmm2ext/doi.h>
25 #include "ardour/port_insert.h"
26 #include "ardour/session.h"
27 #include "ardour/io.h"
28 #include "ardour/audioengine.h"
29 #include "ardour/track.h"
30 #include "ardour/audio_track.h"
31 #include "ardour/midi_track.h"
32 #include "ardour/mtdm.h"
33 #include "ardour/data_type.h"
34 #include "ardour/port.h"
35 #include "ardour/bundle.h"
37 #include "port_insert_ui.h"
39 #include "gui_thread.h"
42 using namespace ARDOUR;
45 PortInsertUI::PortInsertUI (Gtk::Window* parent, ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
47 , latency_button (_("Measure Latency"))
48 , input_selector (parent, sess, pi->input())
49 , output_selector (parent, sess, pi->output())
51 latency_hbox.pack_start (latency_button, false, false);
52 latency_hbox.pack_start (latency_display, false, false);
53 latency_hbox.set_spacing (4);
55 output_selector.set_min_height_divisor (2);
56 input_selector.set_min_height_divisor (2);
58 notebook.append_page (output_selector, _("Send/Output"));
59 notebook.append_page (input_selector, _("Return/Input"));
61 notebook.set_current_page (0);
64 pack_start (notebook, true, true);
65 pack_start (latency_hbox, false, false);
67 update_latency_display ();
69 latency_button.signal_toggled().connect (mem_fun (*this, &PortInsertUI::latency_button_toggled));
70 latency_button.set_name (X_("MeasureLatencyButton"));
74 PortInsertUI::update_latency_display ()
76 framecnt_t const sample_rate = input_selector.session()->engine().frame_rate();
77 if (sample_rate == 0) {
78 latency_display.set_text (_("Disconnected from audio engine"));
81 snprintf (buf, sizeof (buf), "%10.3lf frames %10.3lf ms",
82 (float)_pi->latency(), (float)_pi->latency() * 1000.0f/sample_rate);
83 latency_display.set_text(buf);
88 PortInsertUI::check_latency_measurement ()
90 MTDM* mtdm = _pi->mtdm ();
92 if (mtdm->resolve () < 0) {
93 latency_display.set_text (_("No signal detected"));
97 if (mtdm->err () > 0.3) {
103 framecnt_t const sample_rate = AudioEngine::instance()->frame_rate();
105 if (sample_rate == 0) {
106 latency_display.set_text (_("Disconnected from audio engine"));
107 _pi->stop_latency_detection ();
111 snprintf (buf, sizeof (buf), "%10.3lf frames %10.3lf ms", mtdm->del (), mtdm->del () * 1000.0f/sample_rate);
115 if (mtdm->err () > 0.2) {
121 strcat (buf, " (Inv)");
126 _pi->set_measured_latency (rint (mtdm->del()));
127 latency_button.set_active (false);
128 strcat (buf, " (set)");
131 latency_display.set_text (buf);
137 PortInsertUI::latency_button_toggled ()
139 if (latency_button.get_active ()) {
141 _pi->start_latency_detection ();
142 latency_display.set_text (_("Detecting ..."));
143 latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &PortInsertUI::check_latency_measurement), 250);
146 _pi->stop_latency_detection ();
147 latency_timeout.disconnect ();
148 update_latency_display ();
153 PortInsertUI::redisplay ()
155 input_selector.setup_ports (input_selector.other());
156 output_selector.setup_ports (output_selector.other());
160 PortInsertUI::finished (IOSelector::Result r)
162 input_selector.Finished (r);
163 output_selector.Finished (r);
167 PortInsertWindow::PortInsertWindow (ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
168 : ArdourDialog ("port insert dialog"),
169 _portinsertui (this, sess, pi)
172 set_name ("IOSelectorWindow");
173 string title = _("Port Insert ");
177 get_vbox()->pack_start (_portinsertui);
179 Gtk::Button* cancel_but = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
180 Gtk::Button* ok_but = add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
182 cancel_but->signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::cancel));
183 ok_but->signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::accept));
185 signal_delete_event().connect (sigc::mem_fun (*this, &PortInsertWindow::wm_delete), false);
187 pi->DropReferences.connect (going_away_connection, invalidator (*this), boost::bind (&PortInsertWindow::plugin_going_away, this), gui_context());
191 PortInsertWindow::wm_delete (GdkEventAny* /*event*/)
198 PortInsertWindow::plugin_going_away ()
200 ENSURE_GUI_THREAD (*this, &PortInsertWindow::plugin_going_away)
202 going_away_connection.disconnect ();
203 delete_when_idle (this);
207 PortInsertWindow::on_map ()
209 _portinsertui.redisplay ();
215 PortInsertWindow::cancel ()
217 _portinsertui.finished (IOSelector::Cancelled);
222 PortInsertWindow::accept ()
224 _portinsertui.finished (IOSelector::Accepted);