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