fix merge conflicts with master
[ardour.git] / gtk2_ardour / port_insert_ui.cc
1 /*
2     Copyright (C) 2002-2007 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <gtkmm/messagedialog.h>
21 #include <glibmm/objectbase.h>
22
23 #include <gtkmm2ext/doi.h>
24
25 #include "ardour/audioengine.h"
26 #include "ardour/mtdm.h"
27 #include "ardour/port_insert.h"
28 #include "ardour/session.h"
29
30 #include "port_insert_ui.h"
31 #include "utils.h"
32 #include "gui_thread.h"
33 #include "i18n.h"
34
35 using namespace ARDOUR;
36 using namespace Gtk;
37
38 PortInsertUI::PortInsertUI (Gtk::Window* parent, ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
39         : _pi (pi)
40         , latency_button (_("Measure Latency"))
41         , input_selector (parent, sess, pi->input())
42         , output_selector (parent, sess, pi->output())
43 {
44         latency_hbox.pack_start (latency_button, false, false);
45         latency_hbox.pack_start (latency_display, false, false);
46         latency_hbox.set_spacing (4);
47
48         output_selector.set_min_height_divisor (2);
49         input_selector.set_min_height_divisor (2);
50
51         notebook.append_page (output_selector, _("Send/Output"));
52         notebook.append_page (input_selector, _("Return/Input"));
53
54         notebook.set_current_page (0);
55
56         set_spacing (12);
57         pack_start (notebook, true, true);
58         pack_start (latency_hbox, false, false);
59
60         update_latency_display ();
61
62         latency_button.signal_toggled().connect (mem_fun (*this, &PortInsertUI::latency_button_toggled));
63         latency_button.set_name (X_("MeasureLatencyButton"));
64 }
65
66 void
67 PortInsertUI::update_latency_display ()
68 {
69         framecnt_t const sample_rate = AudioEngine::instance()->sample_rate();
70         if (sample_rate == 0) {
71                 latency_display.set_text (_("Disconnected from audio engine"));
72         } else {
73                 char buf[64];
74                 snprintf (buf, sizeof (buf), "%10.3lf frames %10.3lf ms",
75                           (float)_pi->latency(), (float)_pi->latency() * 1000.0f/sample_rate);
76                 latency_display.set_text(buf);
77         }
78 }
79
80 bool
81 PortInsertUI::check_latency_measurement ()
82 {
83         MTDM* mtdm = _pi->mtdm ();
84
85         if (mtdm->resolve () < 0) {
86                 latency_display.set_text (_("No signal detected"));
87                 return true;
88         }
89
90         if (mtdm->err () > 0.3) {
91                 mtdm->invert ();
92                 mtdm->resolve ();
93         }
94
95         char buf[128];
96         framecnt_t const sample_rate = AudioEngine::instance()->sample_rate();
97
98         if (sample_rate == 0) {
99                 latency_display.set_text (_("Disconnected from audio engine"));
100                 _pi->stop_latency_detection ();
101                 return false;
102         }
103
104         snprintf (buf, sizeof (buf), "%10.3lf frames %10.3lf ms", mtdm->del (), mtdm->del () * 1000.0f/sample_rate);
105
106         bool solid = true;
107
108         if (mtdm->err () > 0.2) {
109                 strcat (buf, " ??");
110                 solid = false;
111         }
112
113         if (mtdm->inv ()) {
114                 strcat (buf, " (Inv)");
115                 solid = false;
116         }
117
118         if (solid) {
119                 _pi->set_measured_latency (rint (mtdm->del()));
120                 latency_button.set_active (false);
121                 strcat (buf, " (set)");
122         }
123
124         latency_display.set_text (buf);
125
126         return true;
127 }
128
129 void
130 PortInsertUI::latency_button_toggled ()
131 {
132         if (latency_button.get_active ()) {
133
134                 _pi->start_latency_detection ();
135                 latency_display.set_text (_("Detecting ..."));
136                 latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &PortInsertUI::check_latency_measurement), 250);
137
138         } else {
139                 _pi->stop_latency_detection ();
140                 latency_timeout.disconnect ();
141                 update_latency_display ();
142         }
143 }
144
145 void
146 PortInsertUI::redisplay ()
147 {
148         input_selector.setup_ports (input_selector.other());
149         output_selector.setup_ports (output_selector.other());
150 }
151
152 void
153 PortInsertUI::finished (IOSelector::Result r)
154 {
155         input_selector.Finished (r);
156         output_selector.Finished (r);
157 }
158
159
160 PortInsertWindow::PortInsertWindow (ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
161         : ArdourDialog ("port insert dialog"),
162           _portinsertui (this, sess, pi)
163 {
164
165         set_name ("IOSelectorWindow");
166         std::string title = _("Port Insert ");
167         title += pi->name();
168         set_title (title);
169
170         get_vbox()->pack_start (_portinsertui);
171
172         Gtk::Button* cancel_but = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
173         Gtk::Button* ok_but = add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
174
175         cancel_but->signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::cancel));
176         ok_but->signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::accept));
177
178         signal_delete_event().connect (sigc::mem_fun (*this, &PortInsertWindow::wm_delete), false);
179
180         pi->DropReferences.connect (going_away_connection, invalidator (*this), boost::bind (&PortInsertWindow::plugin_going_away, this), gui_context());
181 }
182
183 bool
184 PortInsertWindow::wm_delete (GdkEventAny* /*event*/)
185 {
186         accept ();
187         return false;
188 }
189
190 void
191 PortInsertWindow::plugin_going_away ()
192 {
193         ENSURE_GUI_THREAD (*this, &PortInsertWindow::plugin_going_away)
194
195         going_away_connection.disconnect ();
196         delete_when_idle (this);
197 }
198
199 void
200 PortInsertWindow::on_map ()
201 {
202         _portinsertui.redisplay ();
203         Window::on_map ();
204 }
205
206
207 void
208 PortInsertWindow::cancel ()
209 {
210         _portinsertui.finished (IOSelector::Cancelled);
211         hide ();
212 }
213
214 void
215 PortInsertWindow::accept ()
216 {
217         _portinsertui.finished (IOSelector::Accepted);
218         hide ();
219 }
220