From 7c8867068027c9f815606cce8136528269734053 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Thu, 12 Sep 2013 16:28:51 -0400 Subject: [PATCH] lots of changes to auto-start (and stop) the backend for latency measurements, and continuing work on the session construction/engine configuration flow --- gtk2_ardour/ardour_ui.cc | 25 +++---- gtk2_ardour/engine_dialog.cc | 116 +++++++++++++++++++++++-------- gtk2_ardour/engine_dialog.h | 4 +- libs/ardour/ardour/audioengine.h | 2 + libs/ardour/audioengine.cc | 53 ++++++++++++-- libs/ardour/session.cc | 17 ++++- 6 files changed, 164 insertions(+), 53 deletions(-) diff --git a/gtk2_ardour/ardour_ui.cc b/gtk2_ardour/ardour_ui.cc index 4992e0a14b..8bdc694742 100644 --- a/gtk2_ardour/ardour_ui.cc +++ b/gtk2_ardour/ardour_ui.cc @@ -384,25 +384,8 @@ ARDOUR_UI::create_global_port_matrix (ARDOUR::DataType type) void ARDOUR_UI::attach_to_engine () { - AudioEngine::instance()->Stopped.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::engine_stopped, this), gui_context()); AudioEngine::instance()->Running.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::engine_running, this), gui_context()); - AudioEngine::instance()->SampleRateChanged.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::update_sample_rate, this, _1), gui_context()); - - AudioEngine::instance()->Halted.connect_same_thread (halt_connection, boost::bind (&ARDOUR_UI::engine_halted, this, _1, false)); - ARDOUR::Port::set_connecting_blocked (ARDOUR_COMMAND_LINE::no_connect_ports); - - /* if there is only one audio/midi backend, and it does not require setup, get our use of it underway - * right here (we need to know the client name and potential session ID - * to do this, which is why this is here, rather than in, say, - * ARDOUR::init(). - */ - - if (!AudioEngine::instance()->setup_required()) { - const AudioBackendInfo* backend = AudioEngine::instance()->available_backends().front(); - AudioEngine::instance()->set_backend (backend->name, ARDOUR_COMMAND_LINE::backend_client_name, ARDOUR_COMMAND_LINE::backend_session_uuid); - AudioEngine::instance()->start (); - } } void @@ -524,6 +507,12 @@ ARDOUR_UI::post_engine () */ ARDOUR::init_post_engine (); + + /* connect to important signals */ + + AudioEngine::instance()->Stopped.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::engine_stopped, this), gui_context()); + AudioEngine::instance()->SampleRateChanged.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::update_sample_rate, this, _1), gui_context()); + AudioEngine::instance()->Halted.connect_same_thread (halt_connection, boost::bind (&ARDOUR_UI::engine_halted, this, _1, false)); _tooltips.enable(); @@ -4161,6 +4150,8 @@ ARDOUR_UI::launch_audio_midi_setup () int ARDOUR_UI::do_audio_midi_setup (uint32_t desired_sample_rate) { + cerr << "DO-AMS\n"; + launch_audio_midi_setup (); _audio_midi_setup->set_desired_sample_rate (desired_sample_rate); diff --git a/gtk2_ardour/engine_dialog.cc b/gtk2_ardour/engine_dialog.cc index 923024bdc2..d724f4f4d3 100644 --- a/gtk2_ardour/engine_dialog.cc +++ b/gtk2_ardour/engine_dialog.cc @@ -29,6 +29,7 @@ #include "pbd/error.h" #include "pbd/xml++.h" +#include "pbd/unwind.h" #include #include @@ -95,15 +96,6 @@ EngineControl::EngineControl () if (audio_setup) { set_state (*audio_setup); } - - ARDOUR::AudioEngine::instance()->Stopped.connect (*this, MISSING_INVALIDATOR, boost::bind (&EngineControl::disable_latency_tab, this), gui_context()); - - if (!ARDOUR::AudioEngine::instance()->connected()) { - ARDOUR::AudioEngine::instance()->Running.connect (*this, MISSING_INVALIDATOR, boost::bind (&EngineControl::enable_latency_tab, this), gui_context()); - disable_latency_tab (); - } else { - enable_latency_tab (); - } } void @@ -281,9 +273,18 @@ and microphone.\n\n\ driver_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::driver_changed)); sample_rate_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::sample_rate_changed)); buffer_size_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::buffer_size_changed)); + device_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::device_changed)); + + input_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed)); + output_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed)); + input_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed)); + output_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed)); + + input_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &input_channels)); output_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &output_channels)); - device_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::device_changed)); + + notebook.signal_switch_page().connect (sigc::mem_fun (*this, &EngineControl::on_switch_page)); } EngineControl::~EngineControl () @@ -297,6 +298,8 @@ EngineControl::disable_latency_tab () vector empty; set_popdown_strings (lm_output_channel_combo, empty); set_popdown_strings (lm_input_channel_combo, empty); + lm_measure_button.set_sensitive (false); + lm_use_button.set_sensitive (false); } void @@ -311,6 +314,9 @@ EngineControl::enable_latency_tab () ARDOUR::AudioEngine::instance()->get_physical_inputs (ARDOUR::DataType::AUDIO, inputs); set_popdown_strings (lm_input_channel_combo, inputs); lm_input_channel_combo.set_active_text (inputs.front()); + + lm_measure_button.set_sensitive (true); + lm_use_button.set_sensitive (true); } void @@ -384,10 +390,26 @@ EngineControl::list_devices () set_popdown_strings (device_combo, available_devices); if (!available_devices.empty()) { + sample_rate_combo.set_sensitive (true); + buffer_size_combo.set_sensitive (true); + input_latency.set_sensitive (true); + output_latency.set_sensitive (true); + input_channels.set_sensitive (true); + output_channels.set_sensitive (true); + + /* changing the text in the combo will trigger device_changed() + which should populate the parameter controls + */ + device_combo.set_active_text (available_devices.front()); + } else { + sample_rate_combo.set_sensitive (true); + buffer_size_combo.set_sensitive (true); + input_latency.set_sensitive (true); + output_latency.set_sensitive (true); + input_channels.set_sensitive (true); + output_channels.set_sensitive (true); } - - device_changed (); } void @@ -468,7 +490,13 @@ EngineControl::device_changed () ignore_changes--; + /* pick up any saved state for this device */ + maybe_display_saved_state (); + + /* and push it to the backend */ + + push_state_to_backend (false); } void @@ -483,6 +511,7 @@ EngineControl::sample_rate_changed () */ show_buffer_duration (); + push_state_to_backend (false); save_state (); } @@ -495,6 +524,7 @@ EngineControl::buffer_size_changed () } show_buffer_duration (); + push_state_to_backend (false); save_state (); } @@ -522,6 +552,14 @@ EngineControl::show_buffer_duration () buffer_size_duration_label.set_text (buf); } +void +EngineControl::parameter_changed () +{ + if (!ignore_changes) { + save_state (); + } +} + EngineControl::State* EngineControl::get_matching_state (const string& backend, const string& driver, @@ -927,23 +965,47 @@ EngineControl::set_desired_sample_rate (uint32_t sr) device_changed (); } -/* latency measurement */ - void -EngineControl::update_latency_display () +EngineControl::on_switch_page (GtkNotebookPage*, guint page_num) { - ARDOUR::framecnt_t const sample_rate = ARDOUR::AudioEngine::instance()->sample_rate(); - if (sample_rate == 0) { - lm_results.set_text (_("Disconnected from audio engine")); - } else { - char buf[64]; - //snprintf (buf, sizeof (buf), "%10.3lf frames %10.3lf ms", - //(float)_pi->latency(), (float)_pi->latency() * 1000.0f/sample_rate); - strcpy (buf, "got something"); - lm_results.set_text(buf); - } + if (page_num == 2) { + /* latency tab */ + + if (!ARDOUR::AudioEngine::instance()->running()) { + + PBD::Unwinder protect_ignore_changes (ignore_changes, ignore_changes + 1); + + /* save any existing latency values */ + + uint32_t il = (uint32_t) input_latency.get_value (); + uint32_t ol = (uint32_t) input_latency.get_value (); + + /* reset to zero so that our new test instance of JACK + will be clean of any existing latency measures. + */ + + input_latency.set_value (0); + output_latency.set_value (0); + + push_state_to_backend (false); + + /* reset control */ + + input_latency.set_value (il); + output_latency.set_value (ol); + } + + if (ARDOUR::AudioEngine::instance()->prepare_for_latency_measurement()) { + disable_latency_tab (); + } + enable_latency_tab (); + } else { + ARDOUR::AudioEngine::instance()->stop_latency_detection(); + } } +/* latency measurement */ + bool EngineControl::check_latency_measurement () { @@ -975,6 +1037,7 @@ EngineControl::check_latency_measurement () } uint32_t frames_total = mtdm->del(); + cerr << "total = " << frames_total << " delay = " << ARDOUR::AudioEngine::instance()->latency_signal_delay() << endl; uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay(); snprintf (buf, sizeof (buf), "%u samples %10.3lf ms", extra, extra * 1000.0f/sample_rate); @@ -992,7 +1055,6 @@ EngineControl::check_latency_measurement () } if (solid) { - // _pi->set_measured_latency (rint (mtdm->del())); lm_measure_button.set_active (false); lm_use_button.set_sensitive (true); strcat (buf, " (set)"); @@ -1010,7 +1072,6 @@ EngineControl::latency_button_toggled () ARDOUR::AudioEngine::instance()->set_latency_input_port (lm_input_channel_combo.get_active_text()); ARDOUR::AudioEngine::instance()->set_latency_output_port (lm_output_channel_combo.get_active_text()); - cerr << "latency detection on " << lm_input_channel_combo.get_active_text() << " => " << lm_output_channel_combo.get_active_text() << endl; ARDOUR::AudioEngine::instance()->start_latency_detection (); lm_results.set_text (_("Detecting ...")); latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &EngineControl::check_latency_measurement), 250); @@ -1018,7 +1079,6 @@ EngineControl::latency_button_toggled () } else { ARDOUR::AudioEngine::instance()->stop_latency_detection (); latency_timeout.disconnect (); - update_latency_display (); } } diff --git a/gtk2_ardour/engine_dialog.h b/gtk2_ardour/engine_dialog.h index 88e5094948..98ddf2c1b3 100644 --- a/gtk2_ardour/engine_dialog.h +++ b/gtk2_ardour/engine_dialog.h @@ -118,6 +118,7 @@ class EngineControl : public ArdourDialog, public PBD::ScopedConnectionList { void backend_changed (); void sample_rate_changed (); void buffer_size_changed (); + void parameter_changed (); uint32_t get_rate() const; uint32_t get_buffer_size() const; @@ -172,10 +173,11 @@ class EngineControl : public ArdourDialog, public PBD::ScopedConnectionList { /* latency measurement */ void latency_button_toggled (); bool check_latency_measurement (); - void update_latency_display (); sigc::connection latency_timeout; void enable_latency_tab (); void disable_latency_tab (); + + void on_switch_page (GtkNotebookPage*, guint page_num); }; #endif /* __gtk2_ardour_engine_dialog_h__ */ diff --git a/libs/ardour/ardour/audioengine.h b/libs/ardour/ardour/audioengine.h index 21206da8fc..5762e709b3 100644 --- a/libs/ardour/ardour/audioengine.h +++ b/libs/ardour/ardour/audioengine.h @@ -188,6 +188,7 @@ public: /* latency measurement */ MTDM* mtdm(); + int prepare_for_latency_measurement (); void start_latency_detection (); void stop_latency_detection (); void set_latency_input_port (const std::string&); @@ -225,6 +226,7 @@ public: std::string _latency_input_name; std::string _latency_output_name; framecnt_t _latency_signal_latency; + bool _started_for_latency; void meter_thread (); void start_metering_thread (); diff --git a/libs/ardour/audioengine.cc b/libs/ardour/audioengine.cc index ba5b5ad525..819a166866 100644 --- a/libs/ardour/audioengine.cc +++ b/libs/ardour/audioengine.cc @@ -80,6 +80,7 @@ AudioEngine::AudioEngine () , _latency_output_port (0) , _latency_flush_frames (0) , _latency_signal_latency (0) + , _started_for_latency (false) { g_atomic_int_set (&m_meter_exit, 0); discover_backends (); @@ -202,6 +203,13 @@ AudioEngine::process_callback (pframes_t nframes) bool return_after_remove_check = false; if (_measuring_latency && _mtdm) { + /* run a normal cycle from the perspective of the PortManager + so that we get silence on all registered ports. + + we overwrite the silence on the two ports used for latency + measurement. + */ + PortManager::cycle_start (nframes); PortManager::silence (nframes); @@ -609,7 +617,9 @@ AudioEngine::start () start_metering_thread (); - Running(); /* EMIT SIGNAL */ + if (!_started_for_latency) { + Running(); /* EMIT SIGNAL */ + } return 0; } @@ -632,6 +642,7 @@ AudioEngine::stop () _measuring_latency = false; _latency_output_port = 0; _latency_input_port = 0; + _started_for_latency = false; stop_metering_thread (); Port::PortDrop (); @@ -982,6 +993,13 @@ AudioEngine::halted_callback (const char* why) bool AudioEngine::setup_required () const { + /* If there is only a single backend and it claims to be configured + * already there is no setup to be done. + * + * Primarily for a case where there is only a JACK backend and + * JACK is already running. + */ + if (_backends.size() == 1 && _backends.begin()->second->already_configured()) { return false; } @@ -995,9 +1013,28 @@ AudioEngine::mtdm() return _mtdm; } +int +AudioEngine::prepare_for_latency_measurement () +{ + if (!running()) { + _started_for_latency = true; + + if (start()) { + _started_for_latency = false; + return -1; + } + } + + return 0; +} + void AudioEngine::start_latency_detection () { + if (prepare_for_latency_measurement ()) { + return; + } + PortEngine& pe (port_engine()); delete _mtdm; @@ -1026,26 +1063,32 @@ AudioEngine::start_latency_detection () _latency_signal_latency = 0; lr = pe.get_latency_range (_latency_input_port, false); _latency_signal_latency = lr.max; + cerr << "Input port lm = " << lr.max; lr = pe.get_latency_range (_latency_output_port, true); _latency_signal_latency += lr.max; + cerr << " output port lm = " << lr.max << endl; - cerr << "latency signal pathway = " << _latency_signal_latency << endl; - /* all created and connected, lets go */ _mtdm = new MTDM (sample_rate()); _measuring_latency = true; _latency_flush_frames = samples_per_cycle(); - } void AudioEngine::stop_latency_detection () { + cerr << "Stop LD\n"; + + _measuring_latency = false; + port_engine().unregister_port (_latency_output_port); port_engine().unregister_port (_latency_input_port); - _measuring_latency = false; + + if (_started_for_latency) { + stop (); + } } void diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc index 443738f777..94882c13b3 100644 --- a/libs/ardour/session.cc +++ b/libs/ardour/session.cc @@ -289,13 +289,26 @@ Session::Session (AudioEngine &eng, /* we need the audioengine to be up and usable to make much more * progress with construction, so lets get that started if it isn't already. */ - - if (_engine.current_backend() == 0 || _engine.setup_required()) { + + if (_engine.current_backend() == 0) { + /* backend is unknown ... */ boost::optional r = AudioEngineSetupRequired (sr); if (r.get_value_or (-1) != 0) { destroy (); throw failed_constructor(); } + } else if (_engine.setup_required()) { + /* backend is known, but setup is needed */ + boost::optional r = AudioEngineSetupRequired (sr); + if (r.get_value_or (-1) != 0) { + destroy (); + throw failed_constructor(); + } + } else if (!_engine.running()) { + if (_engine.start()) { + destroy (); + throw failed_constructor (); + } } /* at this point the engine should be connected (i.e. interacting