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 "io_selector.h"
39 #include "gui_thread.h"
42 using namespace ARDOUR;
45 IOSelector::IOSelector (Gtk::Window* p, ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io)
46 : PortMatrix (p, session, io->default_type())
49 /* signal flow from 0 to 1 */
51 _find_inputs_for_io_outputs = (_io->direction() == IO::Output);
53 if (_find_inputs_for_io_outputs) {
61 _port_group.reset (new PortGroup (io->name()));
62 _ports[_ours].add_group (_port_group);
69 IOSelector::setup_ports (int dim)
75 _ports[dim].suspend_signals ();
79 _ports[_other].gather (_session, _find_inputs_for_io_outputs, false);
83 _port_group->clear ();
84 _port_group->add_bundle (_io->bundle (), _io);
87 _ports[dim].resume_signals ();
91 IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
93 ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
94 ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
96 for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
97 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
99 Port* f = _session->engine().get_port_by_name (*i);
105 _io->connect (f, *j, 0);
107 _io->disconnect (f, *j, 0);
113 PortMatrixNode::State
114 IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
116 ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
117 ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
119 if (our_ports.empty() || other_ports.empty()) {
120 /* we're looking at a bundle with no parts associated with this channel,
121 so nothing to connect */
122 return PortMatrixNode::NOT_ASSOCIATED;
125 for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
126 for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
128 Port* f = _session->engine().get_port_by_name (*i);
130 /* since we are talking about an IO, our ports should all have an associated Port *,
131 so the above call should never fail */
134 if (!f->connected_to (*j)) {
135 /* if any one thing is not connected, all bets are off */
136 return PortMatrixNode::NOT_ASSOCIATED;
141 return PortMatrixNode::ASSOCIATED;
145 IOSelector::n_io_ports () const
147 if (!_find_inputs_for_io_outputs) {
148 return _io->n_ports().get (_io->default_type());
150 return _io->n_ports().get (_io->default_type());
155 IOSelector::list_is_global (int dim) const
157 return (dim == _other);
160 IOSelectorWindow::IOSelectorWindow (ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io, bool /*can_cancel*/)
161 : _selector (this, session, io)
163 set_name ("IOSelectorWindow2");
164 set_title (_("I/O selector"));
166 Gtk::Alignment* alignment = manage(new Gtk::Alignment(0.5, 0.5, 0.0, 0.0));
167 alignment->add (_selector);
170 set_position (Gtk::WIN_POS_MOUSE);
172 io_name_changed (this);
176 signal_delete_event().connect (sigc::mem_fun (*this, &IOSelectorWindow::wm_delete));
180 IOSelectorWindow::wm_delete (GdkEventAny* /*event*/)
182 _selector.Finished (IOSelector::Accepted);
189 IOSelectorWindow::on_map ()
191 _selector.setup_all_ports ();
196 IOSelectorWindow::on_show ()
198 Gtk::Window::on_show ();
199 pair<uint32_t, uint32_t> const pm_max = _selector.max_size ();
200 resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
204 IOSelectorWindow::io_name_changed (void* src)
206 ENSURE_GUI_THREAD (*this, &IOSelectorWindow::io_name_changed, src)
210 if (!_selector.find_inputs_for_io_outputs()) {
211 title = string_compose(_("%1 input"), _selector.io()->name());
213 title = string_compose(_("%1 output"), _selector.io()->name());
219 PortInsertUI::PortInsertUI (Gtk::Window* parent, ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
221 , latency_button (_("Measure Latency"))
222 , input_selector (parent, sess, pi->input())
223 , output_selector (parent, sess, pi->output())
225 latency_hbox.pack_start (latency_button, false, false);
226 latency_hbox.pack_start (latency_display, false, false);
227 latency_frame.add (latency_hbox);
229 output_selector.set_min_height_divisor (2);
230 input_selector.set_min_height_divisor (2);
232 pack_start (latency_frame);
233 pack_start (output_selector, true, true);
234 pack_start (input_selector, true, true);
236 latency_button.signal_toggled().connect (mem_fun (*this, &PortInsertUI::latency_button_toggled));
240 PortInsertUI::check_latency_measurement ()
242 MTDM* mtdm = _pi->mtdm ();
244 if (mtdm->resolve () < 0) {
245 latency_display.set_text (_("No signal detected"));
249 if (mtdm->err () > 0.3) {
255 nframes_t sample_rate = AudioEngine::instance()->frame_rate();
257 if (sample_rate == 0) {
258 latency_display.set_text (_("Disconnected from audio engine"));
259 _pi->stop_latency_detection ();
263 snprintf (buf, sizeof (buf), "%10.3lf frames %10.3lf ms", mtdm->del (), mtdm->del () * 1000.0f/sample_rate);
267 if (mtdm->err () > 0.2) {
273 strcat (buf, " (Inv)");
278 _pi->set_measured_latency ((nframes_t) rint (mtdm->del()));
279 strcat (buf, " (set)");
282 latency_display.set_text (buf);
287 PortInsertUI::latency_button_toggled ()
289 if (latency_button.get_active ()) {
291 _pi->start_latency_detection ();
292 latency_display.set_text (_("Detecting ..."));
293 latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &PortInsertUI::check_latency_measurement), 250);
296 _pi->stop_latency_detection ();
297 latency_timeout.disconnect ();
303 PortInsertUI::redisplay ()
305 input_selector.setup_ports (input_selector.other());
306 output_selector.setup_ports (output_selector.other());
310 PortInsertUI::finished (IOSelector::Result r)
312 input_selector.Finished (r);
313 output_selector.Finished (r);
317 PortInsertWindow::PortInsertWindow (ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi, bool can_cancel)
318 : ArdourDialog ("port insert dialog"),
319 _portinsertui (this, sess, pi),
320 ok_button (can_cancel ? _("OK"): _("Close")),
321 cancel_button (_("Cancel"))
324 set_name ("IOSelectorWindow");
325 string title = _("ardour: ");
329 ok_button.set_name ("IOSelectorButton");
331 ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
333 cancel_button.set_name ("IOSelectorButton");
336 cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
337 get_action_area()->pack_start (cancel_button, false, false);
339 cancel_button.hide();
341 get_action_area()->pack_start (ok_button, false, false);
343 get_vbox()->pack_start (_portinsertui);
345 ok_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::accept));
346 cancel_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::cancel));
348 signal_delete_event().connect (sigc::mem_fun (*this, &PortInsertWindow::wm_delete), false);
350 pi->DropReferences.connect (going_away_connection, invalidator (*this), boost::bind (&PortInsertWindow::plugin_going_away, this), gui_context());
354 PortInsertWindow::wm_delete (GdkEventAny* /*event*/)
361 PortInsertWindow::plugin_going_away ()
363 ENSURE_GUI_THREAD (*this, &PortInsertWindow::plugin_going_away)
365 going_away_connection.disconnect ();
366 delete_when_idle (this);
370 PortInsertWindow::on_map ()
372 _portinsertui.redisplay ();
378 PortInsertWindow::cancel ()
380 _portinsertui.finished (IOSelector::Cancelled);
385 PortInsertWindow::accept ()
387 _portinsertui.finished (IOSelector::Accepted);