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