2 Copyright (C) 2010 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.
26 #include <boost/scoped_ptr.hpp>
28 #include <gtkmm/messagedialog.h>
30 #include "pbd/error.h"
31 #include "pbd/xml++.h"
32 #include "pbd/unwind.h"
34 #include <gtkmm/alignment.h>
35 #include <gtkmm/stock.h>
36 #include <gtkmm/notebook.h>
37 #include <gtkmm2ext/utils.h>
39 #include "ardour/audio_backend.h"
40 #include "ardour/audioengine.h"
41 #include "ardour/mtdm.h"
42 #include "ardour/rc_configuration.h"
43 #include "ardour/types.h"
45 #include "pbd/convert.h"
46 #include "pbd/error.h"
48 #include "ardour_ui.h"
49 #include "engine_dialog.h"
50 #include "gui_thread.h"
55 using namespace Gtkmm2ext;
59 EngineControl::EngineControl ()
60 : ArdourDialog (_("Audio/MIDI Setup"))
62 , input_latency_adjustment (0, 0, 99999, 1)
63 , input_latency (input_latency_adjustment)
64 , output_latency_adjustment (0, 0, 99999, 1)
65 , output_latency (output_latency_adjustment)
66 , input_channels_adjustment (0, 0, 256, 1)
67 , input_channels (input_channels_adjustment)
68 , output_channels_adjustment (0, 0, 256, 1)
69 , output_channels (output_channels_adjustment)
70 , ports_adjustment (128, 8, 1024, 1, 16)
71 , ports_spinner (ports_adjustment)
72 , control_app_button (_("Device Control Panel"))
73 , lm_start_stop_label (_("Measure latency"))
74 , lm_use_button (_("Use results"))
76 , have_lm_results (false)
77 , midi_refresh_button (_("Refresh list"))
78 , aj_button (_("Start MIDI ALSA/JACK bridge"))
80 , _desired_sample_rate (0)
82 if (!ARDOUR::AudioEngine::instance()->setup_required()) {
83 _have_control = false;
88 set_name (X_("AudioMIDISetup"));
92 get_vbox()->set_border_width (12);
93 get_vbox()->pack_start (notebook);
95 control_app_button.signal_clicked().connect (mem_fun (*this, &EngineControl::control_app_button_clicked));
96 manage_control_app_sensitivity ();
98 cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
99 ok_button = add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
100 apply_button = add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_APPLY);
102 /* Pick up any existing audio setup configuration, if appropriate */
104 XMLNode* audio_setup = ARDOUR::Config->extra_xml ("AudioMIDISetup");
106 /* push a change as if we altered the backend */
110 set_state (*audio_setup);
115 EngineControl::on_response (int response_id)
117 ArdourDialog::on_response (response_id);
119 switch (response_id) {
121 push_state_to_backend (true);
124 push_state_to_backend (true);
127 case RESPONSE_DELETE_EVENT: {
129 ev.type = GDK_BUTTON_PRESS;
131 on_delete_event ((GdkEventAny*) &ev);
140 EngineControl::build_notebook ()
142 using namespace Notebook_Helpers;
144 vector<string> strings;
147 vector<const ARDOUR::AudioBackendInfo*> backends = ARDOUR::AudioEngine::instance()->available_backends();
148 for (vector<const ARDOUR::AudioBackendInfo*>::const_iterator b = backends.begin(); b != backends.end(); ++b) {
149 strings.push_back ((*b)->name);
152 set_popdown_strings (backend_combo, strings);
153 backend_combo.set_active_text (strings.front());
155 basic_packer.set_spacings (6);
156 basic_packer.set_border_width (12);
157 basic_packer.set_homogeneous (true);
161 AttachOptions xopt = AttachOptions (FILL|EXPAND);
163 label = manage (left_aligned_label (_("Audio System:")));
164 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
165 basic_packer.attach (backend_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
168 label = manage (left_aligned_label (_("Driver:")));
169 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
170 basic_packer.attach (driver_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
173 label = manage (left_aligned_label (_("Device:")));
174 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
175 basic_packer.attach (device_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
178 label = manage (left_aligned_label (_("Sample rate:")));
179 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
180 basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
184 label = manage (left_aligned_label (_("Buffer size:")));
185 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
186 basic_packer.attach (buffer_size_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
187 buffer_size_duration_label.set_alignment (0.0); /* left-align */
188 basic_packer.attach (buffer_size_duration_label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
191 input_channels.set_name ("InputChannels");
192 input_channels.set_flags(Gtk::CAN_FOCUS);
193 input_channels.set_digits(0);
194 input_channels.set_wrap(false);
195 output_channels.set_editable (true);
197 label = manage (left_aligned_label (_("Input Channels:")));
198 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
199 basic_packer.attach (input_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
202 output_channels.set_name ("OutputChannels");
203 output_channels.set_flags(Gtk::CAN_FOCUS);
204 output_channels.set_digits(0);
205 output_channels.set_wrap(false);
206 output_channels.set_editable (true);
208 label = manage (left_aligned_label (_("Output Channels:")));
209 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
210 basic_packer.attach (output_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
213 input_latency.set_name ("InputLatency");
214 input_latency.set_flags(Gtk::CAN_FOCUS);
215 input_latency.set_digits(0);
216 input_latency.set_wrap(false);
217 input_latency.set_editable (true);
219 label = manage (left_aligned_label (_("Hardware input latency:")));
220 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
221 basic_packer.attach (input_latency, 1, 2, row, row+1, xopt, (AttachOptions) 0);
222 label = manage (left_aligned_label (_("samples")));
223 basic_packer.attach (*label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
226 output_latency.set_name ("OutputLatency");
227 output_latency.set_flags(Gtk::CAN_FOCUS);
228 output_latency.set_digits(0);
229 output_latency.set_wrap(false);
230 output_latency.set_editable (true);
232 label = manage (left_aligned_label (_("Hardware output latency:")));
233 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
234 basic_packer.attach (output_latency, 1, 2, row, row+1, xopt, (AttachOptions) 0);
235 label = manage (left_aligned_label (_("samples")));
236 basic_packer.attach (*label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
239 basic_hbox.pack_start (basic_packer, false, false);
240 basic_vbox.pack_start (basic_hbox, false, false);
242 Gtk::HBox* hpacker = manage (new HBox);
243 hpacker->set_border_width (12);
244 hpacker->pack_start (control_app_button, false, false);
246 control_app_button.show();
247 basic_vbox.pack_start (*hpacker);
249 /* latency measurement tab */
251 lm_title.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Latency Measurement Tool")));
254 lm_table.set_row_spacings (12);
256 lm_table.attach (lm_title, 0, 2, row, row+1, xopt, (AttachOptions) 0);
259 Gtk::Label* preamble;
261 preamble = manage (new Label);
262 preamble->set_width_chars (60);
263 preamble->set_line_wrap (true);
264 preamble->set_markup (_("<span weight=\"bold\">Turn down the volume on your hardware to a very low level.</span>"));
266 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
269 preamble = manage (new Label);
270 preamble->set_width_chars (60);
271 preamble->set_line_wrap (true);
272 preamble->set_markup (_("Select two channels below and connect them using a cable or (less ideally) a speaker and microphone."));
274 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
277 label = manage (new Label (_("Output channel")));
278 lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
280 Gtk::Alignment* misc_align = manage (new Alignment (0.0, 0.5));
281 misc_align->add (lm_output_channel_combo);
282 lm_table.attach (*misc_align, 1, 2, row, row+1, xopt, (AttachOptions) 0);
285 label = manage (new Label (_("Input channel")));
286 lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
288 misc_align = manage (new Alignment (0.0, 0.5));
289 misc_align->add (lm_input_channel_combo);
290 lm_table.attach (*misc_align, 1, 2, row, row+1, FILL, (AttachOptions) 0);
293 xopt = AttachOptions(0);
295 lm_measure_button.add (lm_start_stop_label);
297 lm_measure_button.signal_toggled().connect (sigc::mem_fun (*this, &EngineControl::latency_button_toggled));
298 lm_use_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::use_latency_button_clicked));
299 lm_use_button.set_sensitive (false);
302 preamble = manage (new Label);
303 preamble->set_width_chars (60);
304 preamble->set_line_wrap (true);
305 preamble->set_markup (_("Once the channels are connected, click the \"Measure latency\" button."));
306 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
309 lm_table.attach (lm_measure_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
311 lm_table.attach (lm_results, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
315 preamble = manage (new Label);
316 preamble->set_width_chars (60);
317 preamble->set_line_wrap (true);
318 preamble->set_markup (_("When satisfied with the results, click the \"Use results\" button."));
319 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
322 lm_table.attach (lm_use_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
325 lm_results.set_markup ("<i>No measurement results yet</i>");
327 lm_vbox.set_border_width (12);
328 lm_vbox.pack_start (lm_table, false, false);
332 notebook.pages().push_back (TabElem (basic_vbox, _("Audio")));
333 notebook.pages().push_back (TabElem (midi_vbox, _("MIDI")));
334 notebook.pages().push_back (TabElem (lm_vbox, _("Latency")));
335 notebook.set_border_width (12);
337 notebook.set_tab_pos (POS_RIGHT);
338 notebook.show_all ();
340 notebook.set_name ("SettingsNotebook");
342 /* Connect to signals */
344 backend_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::backend_changed));
345 driver_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::driver_changed));
346 sample_rate_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::sample_rate_changed));
347 buffer_size_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::buffer_size_changed));
348 device_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::device_changed));
350 input_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
351 output_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
352 input_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
353 output_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
355 input_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &input_channels));
356 output_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &output_channels));
358 notebook.signal_switch_page().connect (sigc::mem_fun (*this, &EngineControl::on_switch_page));
361 EngineControl::~EngineControl ()
367 EngineControl::disable_latency_tab ()
369 vector<string> empty;
370 set_popdown_strings (lm_output_channel_combo, empty);
371 set_popdown_strings (lm_input_channel_combo, empty);
372 lm_measure_button.set_sensitive (false);
373 lm_use_button.set_sensitive (false);
377 EngineControl::enable_latency_tab ()
379 vector<string> outputs;
380 ARDOUR::AudioEngine::instance()->get_physical_outputs (ARDOUR::DataType::AUDIO, outputs);
381 set_popdown_strings (lm_output_channel_combo, outputs);
382 lm_output_channel_combo.set_active_text (outputs.front());
384 vector<string> inputs;
385 ARDOUR::AudioEngine::instance()->get_physical_inputs (ARDOUR::DataType::AUDIO, inputs);
386 set_popdown_strings (lm_input_channel_combo, inputs);
387 lm_input_channel_combo.set_active_text (inputs.front());
389 lm_measure_button.set_sensitive (true);
393 EngineControl::setup_midi_tab_for_backend ()
395 string backend = backend_combo.get_active_text ();
397 Gtkmm2ext::container_clear (midi_vbox);
399 midi_vbox.set_border_width (12);
400 midi_device_table.set_border_width (12);
402 if (backend == "JACK") {
403 setup_midi_tab_for_jack ();
406 midi_vbox.pack_start (midi_device_table, true, true);
407 midi_vbox.pack_start (midi_refresh_button, false, false);
408 midi_vbox.show_all ();
410 midi_refresh_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::refresh_midi_display));
414 EngineControl::setup_midi_tab_for_jack ()
416 midi_vbox.pack_start (aj_button, false, false);
420 EngineControl::refresh_midi_display ()
422 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
425 vector<string> midi_inputs;
426 vector<string> midi_outputs;
428 AttachOptions xopt = AttachOptions (FILL|EXPAND);
431 Gtkmm2ext::container_clear (midi_device_table);
433 backend->get_physical_inputs (ARDOUR::DataType::MIDI, midi_inputs);
434 backend->get_physical_outputs (ARDOUR::DataType::MIDI, midi_outputs);
436 midi_device_table.set_spacings (6);
437 midi_device_table.set_homogeneous (true);
438 midi_device_table.resize (midi_inputs.size() + midi_outputs.size() + 3, 1);
440 l = manage (new Label);
441 l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Inputs")));
442 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
443 l->set_alignment (0, 0.5);
447 for (vector<string>::iterator p = midi_inputs.begin(); p != midi_inputs.end(); ++p) {
448 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
449 l->set_alignment (0, 0.5);
450 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
455 row++; // extra row of spacing
457 l = manage (new Label);
458 l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Outputs")));
459 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
460 l->set_alignment (0, 0.5);
464 for (vector<string>::iterator p = midi_outputs.begin(); p != midi_outputs.end(); ++p) {
465 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
466 l->set_alignment (0, 0.5);
467 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
474 EngineControl::backend_changed ()
476 if (ignore_changes) {
480 string backend_name = backend_combo.get_active_text();
481 boost::shared_ptr<ARDOUR::AudioBackend> backend;
483 if (!(backend = ARDOUR::AudioEngine::instance()->set_backend (backend_name, "ardour", ""))) {
484 /* eh? setting the backend failed... how ? */
488 setup_midi_tab_for_backend ();
490 if (backend->requires_driver_selection()) {
491 vector<string> drivers = backend->enumerate_drivers();
492 driver_combo.set_sensitive (true);
493 set_popdown_strings (driver_combo, drivers);
494 driver_combo.set_active_text (drivers.front());
497 driver_combo.set_sensitive (false);
501 maybe_display_saved_state ();
505 EngineControl::print_channel_count (Gtk::SpinButton* sb)
507 uint32_t cnt = (uint32_t) sb->get_value();
509 sb->set_text (_("all available channels"));
512 snprintf (buf, sizeof (buf), "%d", cnt);
519 EngineControl::list_devices ()
521 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
524 /* now fill out devices, mark sample rates, buffer sizes insensitive */
526 vector<ARDOUR::AudioBackend::DeviceStatus> all_devices = backend->enumerate_devices ();
528 /* NOTE: Ardour currently does not display the "available" field of the
531 * Doing so would require a different GUI widget than the combo
532 * box/popdown that we currently use, since it has no way to list
533 * items that are not selectable. Something more like a popup menu,
534 * which could have unselectable items, would be appropriate.
537 vector<string> available_devices;
539 for (vector<ARDOUR::AudioBackend::DeviceStatus>::const_iterator i = all_devices.begin(); i != all_devices.end(); ++i) {
540 available_devices.push_back (i->name);
544 set_popdown_strings (device_combo, available_devices);
547 if (!available_devices.empty()) {
548 sample_rate_combo.set_sensitive (true);
549 buffer_size_combo.set_sensitive (true);
550 input_latency.set_sensitive (true);
551 output_latency.set_sensitive (true);
552 input_channels.set_sensitive (true);
553 output_channels.set_sensitive (true);
555 /* changing the text in the combo will trigger device_changed()
556 which should populate the parameter controls
559 device_combo.set_active_text (available_devices.front());
561 sample_rate_combo.set_sensitive (true);
562 buffer_size_combo.set_sensitive (true);
563 input_latency.set_sensitive (true);
564 output_latency.set_sensitive (true);
565 input_channels.set_sensitive (true);
566 output_channels.set_sensitive (true);
571 EngineControl::driver_changed ()
573 if (ignore_changes) {
577 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
580 backend->set_driver (driver_combo.get_active_text());
583 maybe_display_saved_state ();
587 EngineControl::device_changed ()
589 if (ignore_changes) {
593 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
595 string device_name = device_combo.get_active_text ();
598 /* don't allow programmatic change to sample_rate_combo to cause a
599 recursive call to this method.
608 vector<float> sr = backend->available_sample_rates (device_name);
609 for (vector<float>::const_iterator x = sr.begin(); x != sr.end(); ++x) {
610 s.push_back (rate_as_string (*x));
611 if (*x == _desired_sample_rate) {
617 set_popdown_strings (sample_rate_combo, s);
619 if (desired.empty()) {
620 sample_rate_combo.set_active_text (s.front());
622 sample_rate_combo.set_active_text (desired);
625 /* hmm ... how to tell the user about the fact that we have no
626 * available sample rates.
631 vector<uint32_t> bs = backend->available_buffer_sizes(device_name);
633 for (vector<uint32_t>::const_iterator x = bs.begin(); x != bs.end(); ++x) {
634 s.push_back (bufsize_as_string (*x));
638 set_popdown_strings (buffer_size_combo, s);
639 buffer_size_combo.set_active_text (s.front());
640 show_buffer_duration ();
642 /* hmm ... how to tell the user about the fact that we have no
643 * available buffer sizes.
647 manage_control_app_sensitivity ();
651 /* pick up any saved state for this device */
653 maybe_display_saved_state ();
655 /* and push it to the backend */
657 push_state_to_backend (false);
661 EngineControl::rate_as_string (float r)
664 if (fmod (r, 1000.0f)) {
665 snprintf (buf, sizeof (buf), "%.1f kHz", r/1000.0);
667 snprintf (buf, sizeof (buf), "%.0f kHz", r/1000.0);
673 EngineControl::bufsize_as_string (uint32_t sz)
675 /* Translators: "samples" is always plural here, so no
676 need for plural+singular forms.
679 snprintf (buf, sizeof (buf), _("%u samples"), sz);
684 EngineControl::sample_rate_changed ()
686 if (ignore_changes) {
690 /* reset the strings for buffer size to show the correct msec value
691 (reflecting the new sample rate).
694 show_buffer_duration ();
700 EngineControl::buffer_size_changed ()
702 if (ignore_changes) {
706 show_buffer_duration ();
711 EngineControl::show_buffer_duration ()
714 /* buffer sizes - convert from just samples to samples + msecs for
715 * the displayed string
718 string bs_text = buffer_size_combo.get_active_text ();
719 uint32_t samples = atoi (bs_text); /* will ignore trailing text */
720 uint32_t rate = get_rate();
722 /* Translators: "msecs" is ALWAYS plural here, so we do not
723 need singular form as well.
725 /* Developers: note the hard-coding of a double buffered model
726 in the (2 * samples) computation of latency. we always start
727 the audiobackend in this configuration.
730 snprintf (buf, sizeof (buf), _("(%.1f msecs)"), (2 * samples) / (rate/1000.0));
731 buffer_size_duration_label.set_text (buf);
735 EngineControl::parameter_changed ()
737 if (!ignore_changes) {
742 EngineControl::State*
743 EngineControl::get_matching_state (const string& backend,
744 const string& driver,
745 const string& device)
747 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
748 if ((*i).backend == backend &&
749 (*i).driver == driver &&
750 (*i).device == device) {
757 EngineControl::State*
758 EngineControl::get_saved_state_for_currently_displayed_backend_and_device ()
760 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
763 return get_matching_state (backend_combo.get_active_text(),
764 (backend->requires_driver_selection() ? (std::string) driver_combo.get_active_text() : string()),
765 device_combo.get_active_text());
769 return get_matching_state (backend_combo.get_active_text(),
771 device_combo.get_active_text());
774 EngineControl::State*
775 EngineControl::save_state ()
777 bool existing = true;
778 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
785 store_state (*state);
788 states.push_back (*state);
795 EngineControl::store_state (State& state)
797 state.backend = get_backend ();
798 state.driver = get_driver ();
799 state.device = get_device_name ();
800 state.sample_rate = get_rate ();
801 state.buffer_size = get_buffer_size ();
802 state.input_latency = get_input_latency ();
803 state.output_latency = get_output_latency ();
804 state.input_channels = get_input_channels ();
805 state.output_channels = get_output_channels ();
809 EngineControl::maybe_display_saved_state ()
811 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
815 if (!_desired_sample_rate) {
816 sample_rate_combo.set_active_text (rate_as_string (state->sample_rate));
818 buffer_size_combo.set_active_text (bufsize_as_string (state->buffer_size));
819 /* call this explicitly because we're ignoring changes to
820 the controls at this point.
822 show_buffer_duration ();
823 input_latency.set_value (state->input_latency);
824 output_latency.set_value (state->output_latency);
830 EngineControl::get_state ()
832 XMLNode* root = new XMLNode ("AudioMIDISetup");
835 if (!states.empty()) {
836 XMLNode* state_nodes = new XMLNode ("EngineStates");
838 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
840 XMLNode* node = new XMLNode ("State");
842 node->add_property ("backend", (*i).backend);
843 node->add_property ("driver", (*i).driver);
844 node->add_property ("device", (*i).device);
845 node->add_property ("sample-rate", (*i).sample_rate);
846 node->add_property ("buffer-size", (*i).buffer_size);
847 node->add_property ("input-latency", (*i).input_latency);
848 node->add_property ("output-latency", (*i).output_latency);
849 node->add_property ("input-channels", (*i).input_channels);
850 node->add_property ("output-channels", (*i).output_channels);
851 node->add_property ("active", (*i).active ? "yes" : "no");
853 state_nodes->add_child_nocopy (*node);
856 root->add_child_nocopy (*state_nodes);
863 EngineControl::set_state (const XMLNode& root)
865 XMLNodeList clist, cclist;
866 XMLNodeConstIterator citer, cciter;
869 XMLProperty* prop = NULL;
871 if (root.name() != "AudioMIDISetup") {
875 clist = root.children();
879 for (citer = clist.begin(); citer != clist.end(); ++citer) {
883 if (child->name() != "EngineStates") {
887 cclist = child->children();
889 for (cciter = cclist.begin(); cciter != cclist.end(); ++cciter) {
892 grandchild = *cciter;
894 if (grandchild->name() != "State") {
898 if ((prop = grandchild->property ("backend")) == 0) {
901 state.backend = prop->value ();
903 if ((prop = grandchild->property ("driver")) == 0) {
906 state.driver = prop->value ();
908 if ((prop = grandchild->property ("device")) == 0) {
911 state.device = prop->value ();
913 if ((prop = grandchild->property ("sample-rate")) == 0) {
916 state.sample_rate = atof (prop->value ());
918 if ((prop = grandchild->property ("buffer-size")) == 0) {
921 state.buffer_size = atoi (prop->value ());
923 if ((prop = grandchild->property ("input-latency")) == 0) {
926 state.input_latency = atoi (prop->value ());
928 if ((prop = grandchild->property ("output-latency")) == 0) {
931 state.output_latency = atoi (prop->value ());
933 if ((prop = grandchild->property ("input-channels")) == 0) {
936 state.input_channels = atoi (prop->value ());
938 if ((prop = grandchild->property ("output-channels")) == 0) {
941 state.output_channels = atoi (prop->value ());
943 if ((prop = grandchild->property ("active")) == 0) {
946 state.active = string_is_affirmative (prop->value ());
948 states.push_back (state);
952 /* now see if there was an active state and switch the setup to it */
954 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
957 backend_combo.set_active_text ((*i).backend);
958 driver_combo.set_active_text ((*i).driver);
959 device_combo.set_active_text ((*i).device);
960 sample_rate_combo.set_active_text (rate_as_string ((*i).sample_rate));
961 buffer_size_combo.set_active_text (bufsize_as_string ((*i).buffer_size));
962 input_latency.set_value ((*i).input_latency);
963 output_latency.set_value ((*i).output_latency);
972 EngineControl::push_state_to_backend (bool start)
974 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
980 /* figure out what is going to change */
982 bool restart_required = false;
983 bool was_running = ARDOUR::AudioEngine::instance()->running();
984 bool change_driver = false;
985 bool change_device = false;
986 bool change_rate = false;
987 bool change_bufsize = false;
988 bool change_latency = false;
989 bool change_channels = false;
991 uint32_t ochan = get_output_channels ();
992 uint32_t ichan = get_input_channels ();
996 /* we can control the backend */
998 if (backend->requires_driver_selection()) {
999 if (get_driver() != backend->driver_name()) {
1000 change_driver = true;
1004 if (get_device_name() != backend->device_name()) {
1005 change_device = true;
1008 if (get_rate() != backend->sample_rate()) {
1012 if (get_buffer_size() != backend->buffer_size()) {
1013 change_bufsize = true;
1016 /* zero-requested channels means "all available" */
1019 ichan = backend->input_channels();
1023 ochan = backend->output_channels();
1026 if (ichan != backend->input_channels()) {
1027 change_channels = true;
1030 if (ochan != backend->output_channels()) {
1031 change_channels = true;
1034 if (get_input_latency() != backend->systemic_input_latency() ||
1035 get_output_latency() != backend->systemic_output_latency()) {
1036 change_latency = true;
1041 /* we have no control over the backend, meaning that we can
1042 * only possibly change sample rate and buffer size.
1046 if (get_rate() != backend->sample_rate()) {
1047 change_bufsize = true;
1050 if (get_buffer_size() != backend->buffer_size()) {
1051 change_bufsize = true;
1055 if (!_have_control) {
1057 /* We do not have control over the backend, so the best we can
1058 * do is try to change the sample rate and/or bufsize and get
1062 if (change_rate && !backend->can_change_sample_rate_when_running()) {
1066 if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1071 backend->set_sample_rate (get_rate());
1074 if (change_bufsize) {
1075 backend->set_buffer_size (get_buffer_size());
1083 /* determine if we need to stop the backend before changing parameters */
1085 if (change_driver || change_device || change_channels || change_latency ||
1086 (change_rate && !backend->can_change_sample_rate_when_running()) ||
1087 (change_bufsize && !backend->can_change_buffer_size_when_running())) {
1088 restart_required = true;
1090 restart_required = false;
1095 if (!change_driver && !change_device && !change_channels && !change_latency) {
1096 /* no changes in any parameters that absolutely require a
1097 * restart, so check those that might be changeable without a
1101 if (change_rate && !backend->can_change_sample_rate_when_running()) {
1102 /* can't do this while running ... */
1103 restart_required = true;
1106 if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1107 /* can't do this while running ... */
1108 restart_required = true;
1114 if (restart_required) {
1115 if (ARDOUR_UI::instance()->disconnect_from_engine ()) {
1122 if (change_driver && backend->set_driver (get_driver())) {
1123 error << string_compose (_("Cannot set driver to %1"), get_driver()) << endmsg;
1126 if (change_device && backend->set_device_name (get_device_name())) {
1127 error << string_compose (_("Cannot set device name to %1"), get_device_name()) << endmsg;
1130 if (change_rate && backend->set_sample_rate (get_rate())) {
1131 error << string_compose (_("Cannot set sample rate to %1"), get_rate()) << endmsg;
1134 if (change_bufsize && backend->set_buffer_size (get_buffer_size())) {
1135 error << string_compose (_("Cannot set buffer size to %1"), get_buffer_size()) << endmsg;
1139 if (change_channels || get_input_channels() == 0 || get_output_channels() == 0) {
1140 if (backend->set_input_channels (get_input_channels())) {
1141 error << string_compose (_("Cannot set input channels to %1"), get_input_channels()) << endmsg;
1144 if (backend->set_output_channels (get_output_channels())) {
1145 error << string_compose (_("Cannot set output channels to %1"), get_output_channels()) << endmsg;
1149 if (change_latency) {
1150 if (backend->set_systemic_input_latency (get_input_latency())) {
1151 error << string_compose (_("Cannot set input latency to %1"), get_input_latency()) << endmsg;
1154 if (backend->set_systemic_output_latency (get_output_latency())) {
1155 error << string_compose (_("Cannot set output latency to %1"), get_output_latency()) << endmsg;
1160 if (start || (was_running && restart_required)) {
1161 if (ARDOUR_UI::instance()->reconnect_to_engine()) {
1172 EngineControl::post_push ()
1174 /* get a pointer to the current state object, creating one if
1178 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
1181 state = save_state ();
1187 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
1188 (*i).active = false;
1191 /* mark this one active (to be used next time the dialog is
1195 state->active = true;
1197 manage_control_app_sensitivity ();
1199 /* schedule a redisplay of MIDI ports */
1201 Glib::signal_timeout().connect (sigc::bind_return (sigc::mem_fun (*this, &EngineControl::refresh_midi_display), false), 1000);
1206 EngineControl::get_rate () const
1208 float r = atof (sample_rate_combo.get_active_text ());
1209 /* the string may have been translated with an abbreviation for
1210 * thousands, so use a crude heuristic to fix this.
1220 EngineControl::get_buffer_size () const
1222 string txt = buffer_size_combo.get_active_text ();
1225 if (sscanf (txt.c_str(), "%d", &samples) != 1) {
1233 EngineControl::get_input_channels() const
1235 return (uint32_t) input_channels_adjustment.get_value();
1239 EngineControl::get_output_channels() const
1241 return (uint32_t) output_channels_adjustment.get_value();
1245 EngineControl::get_input_latency() const
1247 return (uint32_t) input_latency_adjustment.get_value();
1251 EngineControl::get_output_latency() const
1253 return (uint32_t) output_latency_adjustment.get_value();
1257 EngineControl::get_backend () const
1259 return backend_combo.get_active_text ();
1263 EngineControl::get_driver () const
1265 return driver_combo.get_active_text ();
1269 EngineControl::get_device_name () const
1271 return device_combo.get_active_text ();
1275 EngineControl::control_app_button_clicked ()
1277 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1283 backend->launch_control_app ();
1287 EngineControl::manage_control_app_sensitivity ()
1289 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1295 string appname = backend->control_app_name();
1297 if (appname.empty()) {
1298 control_app_button.set_sensitive (false);
1300 control_app_button.set_sensitive (true);
1305 EngineControl::set_desired_sample_rate (uint32_t sr)
1307 _desired_sample_rate = sr;
1312 EngineControl::on_switch_page (GtkNotebookPage*, guint page_num)
1314 if (page_num == 0) {
1315 cancel_button->set_sensitive (true);
1316 ok_button->set_sensitive (true);
1317 apply_button->set_sensitive (true);
1319 cancel_button->set_sensitive (false);
1320 ok_button->set_sensitive (false);
1321 apply_button->set_sensitive (false);
1324 if (page_num == 1) {
1326 refresh_midi_display ();
1329 if (page_num == 2) {
1332 if (!ARDOUR::AudioEngine::instance()->running()) {
1334 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
1336 /* save any existing latency values */
1338 uint32_t il = (uint32_t) input_latency.get_value ();
1339 uint32_t ol = (uint32_t) input_latency.get_value ();
1341 /* reset to zero so that our new test instance of JACK
1342 will be clean of any existing latency measures.
1345 input_latency.set_value (0);
1346 output_latency.set_value (0);
1350 input_latency.set_value (il);
1351 output_latency.set_value (ol);
1355 if (ARDOUR::AudioEngine::instance()->prepare_for_latency_measurement()) {
1356 disable_latency_tab ();
1359 enable_latency_tab ();
1362 ARDOUR::AudioEngine::instance()->stop_latency_detection();
1366 /* latency measurement */
1369 EngineControl::check_latency_measurement ()
1371 MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1373 if (mtdm->resolve () < 0) {
1374 lm_results.set_markup (string_compose ("<span foreground=\"red\">%1</span>", _("No signal detected ")));
1378 if (mtdm->err () > 0.3) {
1384 ARDOUR::framecnt_t const sample_rate = ARDOUR::AudioEngine::instance()->sample_rate();
1386 if (sample_rate == 0) {
1387 lm_results.set_text (_("Disconnected from audio engine"));
1388 ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1392 uint32_t frames_total = mtdm->del();
1393 uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1395 snprintf (buf, sizeof (buf), "%u samples %10.3lf ms", extra, extra * 1000.0f/sample_rate);
1399 if (mtdm->err () > 0.2) {
1400 strcat (buf, " ??");
1405 strcat (buf, " (Inv)");
1410 lm_measure_button.set_active (false);
1411 lm_use_button.set_sensitive (true);
1412 strcat (buf, " (set)");
1413 have_lm_results = true;
1416 lm_results.set_text (buf);
1422 EngineControl::start_latency_detection ()
1424 ARDOUR::AudioEngine::instance()->set_latency_input_port (lm_input_channel_combo.get_active_text());
1425 ARDOUR::AudioEngine::instance()->set_latency_output_port (lm_output_channel_combo.get_active_text());
1426 ARDOUR::AudioEngine::instance()->start_latency_detection ();
1427 lm_results.set_text (_("Detecting ..."));
1428 latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &EngineControl::check_latency_measurement), 250);
1429 lm_start_stop_label.set_text (_("Cancel measurement"));
1430 have_lm_results = false;
1431 lm_input_channel_combo.set_sensitive (false);
1432 lm_output_channel_combo.set_sensitive (false);
1436 EngineControl::end_latency_detection ()
1438 ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1439 latency_timeout.disconnect ();
1440 lm_start_stop_label.set_text (_("Measure latency"));
1441 if (!have_lm_results) {
1442 lm_results.set_markup ("<i>No measurement results yet</i>");
1444 lm_input_channel_combo.set_sensitive (true);
1445 lm_output_channel_combo.set_sensitive (true);
1449 EngineControl::latency_button_toggled ()
1451 if (lm_measure_button.get_active ()) {
1452 start_latency_detection ();
1454 end_latency_detection ();
1459 EngineControl::use_latency_button_clicked ()
1461 MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1467 uint32_t frames_total = mtdm->del();
1468 uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1469 uint32_t one_way = extra/2;
1471 input_latency_adjustment.set_value (one_way);
1472 output_latency_adjustment.set_value (one_way);
1476 EngineControl::on_delete_event (GdkEventAny* ev)
1478 if (notebook.get_current_page() == 2) {
1479 /* currently on latency tab - be sure to clean up */
1480 end_latency_detection ();
1482 return ArdourDialog::on_delete_event (ev);