X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=gtk2_ardour%2Fio_selector.cc;h=9399a6aaa7c5000c166b0e0219ba970c7baaf218;hb=d936bdb3a8e55d26a238376d4b99f56ebeb582b1;hp=cecea984709ca703d8c50e447840649ee7c9af1c;hpb=8ab17e96312f1a61c014c50687e15430d5ae786b;p=ardour.git diff --git a/gtk2_ardour/io_selector.cc b/gtk2_ardour/io_selector.cc index cecea98470..9399a6aaa7 100644 --- a/gtk2_ardour/io_selector.cc +++ b/gtk2_ardour/io_selector.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2002-2007 Paul Davis + Copyright (C) 2002-2007 Paul Davis This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,286 +17,346 @@ */ +#include #include + #include -#include + +#include "ardour/port_insert.h" #include "ardour/session.h" #include "ardour/io.h" #include "ardour/audioengine.h" #include "ardour/track.h" #include "ardour/audio_track.h" #include "ardour/midi_track.h" +#include "ardour/mtdm.h" #include "ardour/data_type.h" +#include "ardour/port.h" +#include "ardour/bundle.h" + #include "io_selector.h" #include "utils.h" #include "gui_thread.h" #include "i18n.h" -IOSelector::IOSelector (ARDOUR::Session& session, boost::shared_ptr io, bool offer_inputs) - : PortMatrix ( - session, io->default_type(), offer_inputs, - PortGroupList::Mask (PortGroupList::BUSS | PortGroupList::SYSTEM | PortGroupList::OTHER) - ), - _io (io) +using namespace ARDOUR; +using namespace Gtk; + +IOSelector::IOSelector (Gtk::Window* p, ARDOUR::Session* session, boost::shared_ptr io) + : PortMatrix (p, session, DataType::NIL) + , _io (io) { - /* Listen for ports changing on the IO */ - if (!offer_inputs) { - _io->input_changed.connect (mem_fun(*this, &IOSelector::ports_changed)); + setup_type (); + + /* signal flow from 0 to 1 */ + + _find_inputs_for_io_outputs = (_io->direction() == IO::Output); + + if (_find_inputs_for_io_outputs) { + _other = 1; + _ours = 0; } else { - _io->output_changed.connect (mem_fun(*this, &IOSelector::ports_changed)); + _other = 0; + _ours = 1; } + + _port_group.reset (new PortGroup (io->name())); + _ports[_ours].add_group (_port_group); + + io->changed.connect (_io_connection, invalidator (*this), boost::bind (&IOSelector::io_changed_proxy, this), gui_context ()); + + setup_all_ports (); + init (); } +void +IOSelector::setup_type () +{ + /* set type according to what's in the IO */ + + int N = 0; + DataType type_with_ports = DataType::NIL; + for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) { + if (_io->ports().num_ports (*i)) { + type_with_ports = *i; + ++N; + } + } + + if (N <= 1) { + set_type (type_with_ports); + } else { + set_type (DataType::NIL); + } +} void -IOSelector::ports_changed (ARDOUR::IOChange change, void *src) +IOSelector::io_changed_proxy () { - ENSURE_GUI_THREAD (bind (mem_fun (*this, &IOSelector::ports_changed), change, src)); + /* The IO's changed signal is emitted from code that holds its route's processor lock, + so we can't call setup_all_ports (which results in a call to Route::foreach_processor) + without a deadlock unless we break things up with this idle handler. + */ + + Glib::signal_idle().connect_once (sigc::mem_fun (*this, &IOSelector::io_changed)); +} - redisplay (); +void +IOSelector::io_changed () +{ + setup_type (); + setup_all_ports (); } +void +IOSelector::setup_ports (int dim) +{ + if (!_session) { + return; + } + + _ports[dim].suspend_signals (); + if (dim == _other) { + + _ports[_other].gather (_session, type(), _find_inputs_for_io_outputs, false); + + } else { + + _port_group->clear (); + _port_group->add_bundle (_io->bundle (), _io); + } + + _ports[dim].resume_signals (); +} void -IOSelector::set_state (int r, std::string const & p, bool s) +IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s) { - if (s) { - if (!_offer_inputs) { - _io->connect_input (_io->input(r), p, 0); - } else { - _io->connect_output (_io->output(r), p, 0); - } - } else { - if (!_offer_inputs) { - _io->disconnect_input (_io->input(r), p, 0); - } else { - _io->disconnect_output (_io->output(r), p, 0); - } - } + ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel); + ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel); + + for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) { + for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) { + + Port* f = _session->engine().get_port_by_name (*i); + if (!f) { + return; + } + + if (s) { + _io->connect (f, *j, 0); + } else { + _io->disconnect (f, *j, 0); + } + } + } } -bool -IOSelector::get_state (int r, std::string const & p) const +PortMatrixNode::State +IOSelector::get_state (ARDOUR::BundleChannel c[2]) const { - vector connections; + ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel); + ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel); - if (_offer_inputs) { - _io->output(r)->get_connections (connections); - } else { - _io->input(r)->get_connections (connections); + if (our_ports.empty() || other_ports.empty()) { + /* we're looking at a bundle with no parts associated with this channel, + so nothing to connect */ + return PortMatrixNode::NOT_ASSOCIATED; } - int k = 0; + for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) { + for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) { - for (vector::iterator i = connections.begin(); i != connections.end(); ++i) { + Port* f = _session->engine().get_port_by_name (*i); - if ((*i)== p) { - return true; + /* since we are talking about an IO, our ports should all have an associated Port *, + so the above call should never fail */ + assert (f); + + if (!f->connected_to (*j)) { + /* if any one thing is not connected, all bets are off */ + return PortMatrixNode::NOT_ASSOCIATED; + } } - - ++k; } - return false; + return PortMatrixNode::ASSOCIATED; } uint32_t -IOSelector::n_rows () const +IOSelector::n_io_ports () const { - if (!_offer_inputs) { - return _io->inputs().num_ports (_io->default_type()); + if (!_find_inputs_for_io_outputs) { + return _io->n_ports().get (_io->default_type()); } else { - return _io->outputs().num_ports (_io->default_type()); + return _io->n_ports().get (_io->default_type()); } } -uint32_t -IOSelector::maximum_rows () const +bool +IOSelector::list_is_global (int dim) const { - if (!_offer_inputs) { - return _io->input_maximum ().get (_io->default_type()); - } else { - return _io->output_maximum ().get (_io->default_type()); - } + return (dim == _other); } - -uint32_t -IOSelector::minimum_rows () const +string +IOSelector::disassociation_verb () const { - if (!_offer_inputs) { - return _io->input_minimum ().get (_io->default_type()); - } else { - return _io->output_minimum ().get (_io->default_type()); - } + return _("Disconnect"); } -std::string -IOSelector::row_name (int r) const +string +IOSelector::channel_noun () const { - if (!_offer_inputs) { - return _io->input(r)->name(); - } else { - return _io->output(r)->name(); - } - + return _("port"); } -void -IOSelector::add_row () +IOSelectorWindow::IOSelectorWindow (ARDOUR::Session* session, boost::shared_ptr io, bool /*can_cancel*/) + : _selector (this, session, io) { - // The IO selector only works for single typed IOs - const ARDOUR::DataType t = _io->default_type (); + set_name ("IOSelectorWindow2"); + set_title (_("I/O selector")); - if (!_offer_inputs) { + add (_selector); - try { - _io->add_input_port ("", this); - } + set_position (Gtk::WIN_POS_MOUSE); - catch (ARDOUR::AudioEngine::PortRegistrationFailure& err) { - Gtk::MessageDialog msg (0, _("There are no more JACK ports available.")); - msg.run (); - } + io_name_changed (this); - } else { + show_all (); - try { - _io->add_output_port ("", this); - } + signal_delete_event().connect (sigc::mem_fun (*this, &IOSelectorWindow::wm_delete)); +} - catch (ARDOUR::AudioEngine::PortRegistrationFailure& err) { - Gtk::MessageDialog msg (0, _("There are no more JACK ports available.")); - msg.run (); - } - } +bool +IOSelectorWindow::wm_delete (GdkEventAny* /*event*/) +{ + _selector.Finished (IOSelector::Accepted); + hide (); + return true; } void -IOSelector::remove_row (int r) +IOSelectorWindow::on_map () { - // The IO selector only works for single typed IOs - const ARDOUR::DataType t = _io->default_type (); - - if (!_offer_inputs) { - _io->remove_input_port (_io->input (r), this); - } else { - _io->remove_output_port (_io->output (r), this); - } + _selector.setup_all_ports (); + Window::on_map (); } -std::string -IOSelector::row_descriptor () const +void +IOSelectorWindow::on_show () { - return _("port"); + Gtk::Window::on_show (); + pair const pm_max = _selector.max_size (); + resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second); } - - -IOSelectorWindow::IOSelectorWindow ( - ARDOUR::Session& session, boost::shared_ptr io, bool for_input, bool can_cancel - ) - : ArdourDialog ("I/O selector"), - _selector (session, io, !for_input), - ok_button (can_cancel ? _("OK"): _("Close")), - cancel_button (_("Cancel")), - rescan_button (_("Rescan")) - +void +IOSelectorWindow::io_name_changed (void* src) { - add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK); - set_name ("IOSelectorWindow2"); + ENSURE_GUI_THREAD (*this, &IOSelectorWindow::io_name_changed, src) string title; - if (for_input) { - title = string_compose(_("%1 input"), io->name()); + + if (!_selector.find_inputs_for_io_outputs()) { + title = string_compose(_("%1 input"), _selector.io()->name()); } else { - title = string_compose(_("%1 output"), io->name()); + title = string_compose(_("%1 output"), _selector.io()->name()); } - ok_button.set_name ("IOSelectorButton"); - if (!can_cancel) { - ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON))); - } - cancel_button.set_name ("IOSelectorButton"); - rescan_button.set_name ("IOSelectorButton"); - rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON))); + set_title (title); +} - get_action_area()->pack_start (rescan_button, false, false); +PortInsertUI::PortInsertUI (Gtk::Window* parent, ARDOUR::Session* sess, boost::shared_ptr pi) + : _pi (pi) + , latency_button (_("Measure Latency")) + , input_selector (parent, sess, pi->input()) + , output_selector (parent, sess, pi->output()) +{ + latency_hbox.pack_start (latency_button, false, false); + latency_hbox.pack_start (latency_display, false, false); + latency_frame.add (latency_hbox); + + output_selector.set_min_height_divisor (2); + input_selector.set_min_height_divisor (2); + + pack_start (latency_frame); + pack_start (output_selector, true, true); + pack_start (input_selector, true, true); + + latency_button.signal_toggled().connect (mem_fun (*this, &PortInsertUI::latency_button_toggled)); +} - if (can_cancel) { - cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON))); - get_action_area()->pack_start (cancel_button, false, false); - } else { - cancel_button.hide(); - } - - get_action_area()->pack_start (ok_button, false, false); +bool +PortInsertUI::check_latency_measurement () +{ + MTDM* mtdm = _pi->mtdm (); - get_vbox()->set_spacing (8); - get_vbox()->pack_start (_selector); + if (mtdm->resolve () < 0) { + latency_display.set_text (_("No signal detected")); + return true; + } - ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept)); - cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel)); - rescan_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::rescan)); + if (mtdm->err () > 0.3) { + mtdm->invert (); + mtdm->resolve (); + } - set_title (title); - set_position (Gtk::WIN_POS_MOUSE); + char buf[64]; + nframes_t sample_rate = AudioEngine::instance()->frame_rate(); - show_all (); + if (sample_rate == 0) { + latency_display.set_text (_("Disconnected from audio engine")); + _pi->stop_latency_detection (); + return false; + } - signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast (this))); -} + snprintf (buf, sizeof (buf), "%10.3lf frames %10.3lf ms", mtdm->del (), mtdm->del () * 1000.0f/sample_rate); -IOSelectorWindow::~IOSelectorWindow() -{ - -} + bool solid = true; -void -IOSelectorWindow::rescan () -{ - _selector.redisplay (); -} + if (mtdm->err () > 0.2) { + strcat (buf, " ??"); + solid = false; + } -void -IOSelectorWindow::cancel () -{ - _selector.Finished (IOSelector::Cancelled); - hide (); -} + if (mtdm->inv ()) { + strcat (buf, " (Inv)"); + solid = false; + } -void -IOSelectorWindow::accept () -{ - _selector.Finished (IOSelector::Accepted); - hide (); + if (solid) { + _pi->set_measured_latency ((nframes_t) rint (mtdm->del())); + strcat (buf, " (set)"); + } + + latency_display.set_text (buf); + return true; } void -IOSelectorWindow::on_map () +PortInsertUI::latency_button_toggled () { - _selector.redisplay (); - Window::on_map (); -} + if (latency_button.get_active ()) { + _pi->start_latency_detection (); + latency_display.set_text (_("Detecting ...")); + latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &PortInsertUI::check_latency_measurement), 250); -PortInsertUI::PortInsertUI (ARDOUR::Session& sess, boost::shared_ptr pi) - : input_selector (sess, pi->io(), true), - output_selector (sess, pi->io(), false) -{ - hbox.pack_start (output_selector, true, true); - hbox.pack_start (input_selector, true, true); - - pack_start (hbox); + } else { + _pi->stop_latency_detection (); + latency_timeout.disconnect (); + } } + void PortInsertUI::redisplay () { - input_selector.redisplay(); - output_selector.redisplay(); + input_selector.setup_ports (input_selector.other()); + output_selector.setup_ports (output_selector.other()); } void @@ -307,28 +367,24 @@ PortInsertUI::finished (IOSelector::Result r) } -PortInsertWindow::PortInsertWindow (ARDOUR::Session& sess, boost::shared_ptr pi, bool can_cancel) +PortInsertWindow::PortInsertWindow (ARDOUR::Session* sess, boost::shared_ptr pi, bool can_cancel) : ArdourDialog ("port insert dialog"), - _portinsertui (sess, pi), + _portinsertui (this, sess, pi), ok_button (can_cancel ? _("OK"): _("Close")), - cancel_button (_("Cancel")), - rescan_button (_("Rescan")) + cancel_button (_("Cancel")) { set_name ("IOSelectorWindow"); - string title = _("ardour: "); + string title = _("Port Insert "); title += pi->name(); set_title (title); - + ok_button.set_name ("IOSelectorButton"); if (!can_cancel) { ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON))); } cancel_button.set_name ("IOSelectorButton"); - rescan_button.set_name ("IOSelectorButton"); - rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON))); - get_action_area()->pack_start (rescan_button, false, false); if (can_cancel) { cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON))); get_action_area()->pack_start (cancel_button, false, false); @@ -339,20 +395,26 @@ PortInsertWindow::PortInsertWindow (ARDOUR::Session& sess, boost::shared_ptrpack_start (_portinsertui); - ok_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::accept)); - cancel_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::cancel)); - rescan_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::rescan)); + ok_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::accept)); + cancel_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::cancel)); - signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast (this))); + signal_delete_event().connect (sigc::mem_fun (*this, &PortInsertWindow::wm_delete), false); - going_away_connection = pi->GoingAway.connect (mem_fun (*this, &PortInsertWindow::plugin_going_away)); + pi->DropReferences.connect (going_away_connection, invalidator (*this), boost::bind (&PortInsertWindow::plugin_going_away, this), gui_context()); +} + +bool +PortInsertWindow::wm_delete (GdkEventAny* /*event*/) +{ + accept (); + return true; } void PortInsertWindow::plugin_going_away () { - ENSURE_GUI_THREAD (mem_fun (*this, &PortInsertWindow::plugin_going_away)); - + ENSURE_GUI_THREAD (*this, &PortInsertWindow::plugin_going_away) + going_away_connection.disconnect (); delete_when_idle (this); } @@ -365,12 +427,6 @@ PortInsertWindow::on_map () } -void -PortInsertWindow::rescan () -{ - _portinsertui.redisplay (); -} - void PortInsertWindow::cancel () { @@ -384,3 +440,4 @@ PortInsertWindow::accept () _portinsertui.finished (IOSelector::Accepted); hide (); } +