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"
33 #include "pbd/failed_constructor.h"
35 #include <gtkmm/alignment.h>
36 #include <gtkmm/stock.h>
37 #include <gtkmm/notebook.h>
38 #include <gtkmm2ext/utils.h>
40 #include "ardour/audio_backend.h"
41 #include "ardour/audioengine.h"
42 #include "ardour/mtdm.h"
43 #include "ardour/rc_configuration.h"
44 #include "ardour/types.h"
46 #include "pbd/convert.h"
47 #include "pbd/error.h"
49 #include "ardour_ui.h"
50 #include "engine_dialog.h"
51 #include "gui_thread.h"
57 using namespace Gtkmm2ext;
61 static const unsigned int midi_tab = -1; /* not currently in use */
62 static const unsigned int latency_tab = 1; /* zero-based, page zero is the main setup page */
64 EngineControl::EngineControl ()
65 : ArdourDialog (_("Audio/MIDI Setup"))
67 , input_latency_adjustment (0, 0, 99999, 1)
68 , input_latency (input_latency_adjustment)
69 , output_latency_adjustment (0, 0, 99999, 1)
70 , output_latency (output_latency_adjustment)
71 , input_channels_adjustment (0, 0, 256, 1)
72 , input_channels (input_channels_adjustment)
73 , output_channels_adjustment (0, 0, 256, 1)
74 , output_channels (output_channels_adjustment)
75 , ports_adjustment (128, 8, 1024, 1, 16)
76 , ports_spinner (ports_adjustment)
77 , control_app_button (_("Device Control Panel"))
78 , lm_start_stop_label (_("Measure latency"))
79 , lm_use_button (_("Use results"))
81 , have_lm_results (false)
82 , midi_refresh_button (_("Refresh list"))
84 , _desired_sample_rate (0)
86 , started_at_least_once (false)
88 using namespace Notebook_Helpers;
89 vector<string> strings;
91 AttachOptions xopt = AttachOptions (FILL|EXPAND);
94 set_name (X_("AudioMIDISetup"));
96 /* the backend combo is the one thing that is ALWAYS visible
99 vector<const ARDOUR::AudioBackendInfo*> backends = ARDOUR::AudioEngine::instance()->available_backends();
101 if (backends.empty()) {
102 MessageDialog msg (string_compose (_("No audio/MIDI backends detected. %1 cannot run\n\n(This is a build/packaging/system error. It should never happen.)"), PROGRAM_NAME));
104 throw failed_constructor ();
107 for (vector<const ARDOUR::AudioBackendInfo*>::const_iterator b = backends.begin(); b != backends.end(); ++b) {
108 strings.push_back ((*b)->name);
111 set_popdown_strings (backend_combo, strings);
112 backend_combo.set_active_text (strings.front());
113 backend_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::backend_changed));
115 /* setup basic packing characteristics for the table used on the main
116 * tab of the notebook
119 basic_packer.set_spacings (6);
120 basic_packer.set_border_width (12);
121 basic_packer.set_homogeneous (true);
125 basic_hbox.pack_start (basic_packer, false, false);
129 /* latency measurement tab */
131 lm_title.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Latency Measurement Tool")));
134 lm_table.set_row_spacings (12);
136 lm_table.attach (lm_title, 0, 2, row, row+1, xopt, (AttachOptions) 0);
139 Gtk::Label* preamble;
141 preamble = manage (new Label);
142 preamble->set_width_chars (60);
143 preamble->set_line_wrap (true);
144 preamble->set_markup (_("<span weight=\"bold\">Turn down the volume on your hardware to a very low level.</span>"));
146 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
149 preamble = manage (new Label);
150 preamble->set_width_chars (60);
151 preamble->set_line_wrap (true);
152 preamble->set_markup (_("Select two channels below and connect them using a cable or (less ideally) a speaker and microphone."));
154 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
157 label = manage (new Label (_("Output channel")));
158 lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
160 Gtk::Alignment* misc_align = manage (new Alignment (0.0, 0.5));
161 misc_align->add (lm_output_channel_combo);
162 lm_table.attach (*misc_align, 1, 2, row, row+1, xopt, (AttachOptions) 0);
165 label = manage (new Label (_("Input channel")));
166 lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
168 misc_align = manage (new Alignment (0.0, 0.5));
169 misc_align->add (lm_input_channel_combo);
170 lm_table.attach (*misc_align, 1, 2, row, row+1, FILL, (AttachOptions) 0);
173 xopt = AttachOptions(0);
175 lm_measure_button.add (lm_start_stop_label);
177 lm_measure_button.signal_toggled().connect (sigc::mem_fun (*this, &EngineControl::latency_button_toggled));
178 lm_use_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::use_latency_button_clicked));
179 lm_use_button.set_sensitive (false);
181 preamble = manage (new Label);
182 preamble->set_width_chars (60);
183 preamble->set_line_wrap (true);
184 preamble->set_markup (_("Once the channels are connected, click the \"Measure latency\" button."));
185 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
188 lm_table.attach (lm_measure_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
190 lm_table.attach (lm_results, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
194 preamble = manage (new Label);
195 preamble->set_width_chars (60);
196 preamble->set_line_wrap (true);
197 preamble->set_markup (_("When satisfied with the results, click the \"Use results\" button."));
198 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
201 lm_table.attach (lm_use_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
204 lm_results.set_markup ("<i>No measurement results yet</i>");
206 lm_vbox.set_border_width (12);
207 lm_vbox.pack_start (lm_table, false, false);
211 notebook.pages().push_back (TabElem (basic_vbox, _("Audio")));
212 // notebook.pages().push_back (TabElem (midi_vbox, _("MIDI")));
213 notebook.pages().push_back (TabElem (lm_vbox, _("Latency")));
214 notebook.set_border_width (12);
216 notebook.set_tab_pos (POS_RIGHT);
217 notebook.show_all ();
219 notebook.set_name ("SettingsNotebook");
221 /* packup the notebook */
223 get_vbox()->set_border_width (12);
224 get_vbox()->pack_start (notebook);
226 /* need a special function to print "all available channels" when the
227 * channel counts hit zero.
230 input_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &input_channels));
231 output_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &output_channels));
233 control_app_button.signal_clicked().connect (mem_fun (*this, &EngineControl::control_app_button_clicked));
234 manage_control_app_sensitivity ();
236 cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
237 ok_button = add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
238 apply_button = add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_APPLY);
240 /* Pick up any existing audio setup configuration, if appropriate */
242 XMLNode* audio_setup = ARDOUR::Config->extra_xml ("AudioMIDISetup");
244 ARDOUR::AudioEngine::instance()->Running.connect (running_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_running, this), gui_context());
245 ARDOUR::AudioEngine::instance()->Stopped.connect (stopped_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_stopped, this), gui_context());
246 ARDOUR::AudioEngine::instance()->Halted.connect (stopped_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_stopped, this), gui_context());
251 set_state (*audio_setup);
254 /* Connect to signals */
256 driver_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::driver_changed));
257 sample_rate_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::sample_rate_changed));
258 buffer_size_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::buffer_size_changed));
259 device_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::device_changed));
260 midi_option_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::midi_option_changed));
262 input_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
263 output_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
264 input_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
265 output_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
267 notebook.signal_switch_page().connect (sigc::mem_fun (*this, &EngineControl::on_switch_page));
273 EngineControl::on_response (int response_id)
275 ArdourDialog::on_response (response_id);
277 switch (response_id) {
279 push_state_to_backend (true);
282 push_state_to_backend (true);
285 case RESPONSE_DELETE_EVENT: {
287 ev.type = GDK_BUTTON_PRESS;
289 on_delete_event ((GdkEventAny*) &ev);
298 EngineControl::build_notebook ()
301 AttachOptions xopt = AttachOptions (FILL|EXPAND);
303 /* clear the table */
305 Gtkmm2ext::container_clear (basic_vbox);
306 Gtkmm2ext::container_clear (basic_packer);
308 label = manage (left_aligned_label (_("Audio System:")));
309 basic_packer.attach (*label, 0, 1, 0, 1, xopt, (AttachOptions) 0);
310 basic_packer.attach (backend_combo, 1, 2, 0, 1, xopt, (AttachOptions) 0);
313 build_full_control_notebook ();
315 build_no_control_notebook ();
318 basic_vbox.pack_start (basic_hbox, false, false);
321 Gtk::HBox* hpacker = manage (new HBox);
322 hpacker->set_border_width (12);
323 hpacker->pack_start (control_app_button, false, false);
325 control_app_button.show();
326 basic_vbox.pack_start (*hpacker);
329 basic_vbox.show_all ();
333 EngineControl::build_full_control_notebook ()
335 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
338 using namespace Notebook_Helpers;
340 vector<string> strings;
341 AttachOptions xopt = AttachOptions (FILL|EXPAND);
342 int row = 1; // row zero == backend combo
344 /* start packing it up */
346 if (backend->requires_driver_selection()) {
347 label = manage (left_aligned_label (_("Driver:")));
348 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
349 basic_packer.attach (driver_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
353 label = manage (left_aligned_label (_("Device:")));
354 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
355 basic_packer.attach (device_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
358 label = manage (left_aligned_label (_("Sample rate:")));
359 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
360 basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
364 label = manage (left_aligned_label (_("Buffer size:")));
365 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
366 basic_packer.attach (buffer_size_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
367 buffer_size_duration_label.set_alignment (0.0); /* left-align */
368 basic_packer.attach (buffer_size_duration_label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
371 input_channels.set_name ("InputChannels");
372 input_channels.set_flags(Gtk::CAN_FOCUS);
373 input_channels.set_digits(0);
374 input_channels.set_wrap(false);
375 output_channels.set_editable (true);
377 label = manage (left_aligned_label (_("Input Channels:")));
378 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
379 basic_packer.attach (input_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
382 output_channels.set_name ("OutputChannels");
383 output_channels.set_flags(Gtk::CAN_FOCUS);
384 output_channels.set_digits(0);
385 output_channels.set_wrap(false);
386 output_channels.set_editable (true);
388 label = manage (left_aligned_label (_("Output Channels:")));
389 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
390 basic_packer.attach (output_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
393 input_latency.set_name ("InputLatency");
394 input_latency.set_flags(Gtk::CAN_FOCUS);
395 input_latency.set_digits(0);
396 input_latency.set_wrap(false);
397 input_latency.set_editable (true);
399 label = manage (left_aligned_label (_("Hardware input latency:")));
400 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
401 basic_packer.attach (input_latency, 1, 2, row, row+1, xopt, (AttachOptions) 0);
402 label = manage (left_aligned_label (_("samples")));
403 basic_packer.attach (*label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
406 output_latency.set_name ("OutputLatency");
407 output_latency.set_flags(Gtk::CAN_FOCUS);
408 output_latency.set_digits(0);
409 output_latency.set_wrap(false);
410 output_latency.set_editable (true);
412 label = manage (left_aligned_label (_("Hardware output latency:")));
413 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
414 basic_packer.attach (output_latency, 1, 2, row, row+1, xopt, (AttachOptions) 0);
415 label = manage (left_aligned_label (_("samples")));
416 basic_packer.attach (*label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
419 label = manage (left_aligned_label (_("MIDI I/O using")));
420 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
421 basic_packer.attach (midi_option_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
426 EngineControl::build_no_control_notebook ()
428 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
431 using namespace Notebook_Helpers;
433 vector<string> strings;
434 AttachOptions xopt = AttachOptions (FILL|EXPAND);
435 int row = 1; // row zero == backend combo
436 const string msg = string_compose (_("The %1 audio backend was configured and started externally.\nThis limits your control over it."), backend->name());
438 label = manage (new Label);
439 label->set_markup (string_compose ("<span weight=\"bold\" foreground=\"red\">%1</span>", msg));
440 basic_packer.attach (*label, 0, 2, row, row + 1, xopt, (AttachOptions) 0);
443 if (backend->can_change_sample_rate_when_running()) {
444 label = manage (left_aligned_label (_("Sample rate:")));
445 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
446 basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
450 if (backend->can_change_buffer_size_when_running()) {
451 label = manage (left_aligned_label (_("Buffer size:")));
452 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
453 basic_packer.attach (buffer_size_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
454 buffer_size_duration_label.set_alignment (0.0); /* left-align */
455 basic_packer.attach (buffer_size_duration_label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
459 connect_disconnect_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::connect_disconnect_click));
461 basic_packer.attach (connect_disconnect_button, 0, 2, row, row+1, FILL, AttachOptions (0));
465 EngineControl::~EngineControl ()
471 EngineControl::disable_latency_tab ()
473 vector<string> empty;
474 set_popdown_strings (lm_output_channel_combo, empty);
475 set_popdown_strings (lm_input_channel_combo, empty);
476 lm_measure_button.set_sensitive (false);
477 lm_use_button.set_sensitive (false);
481 EngineControl::enable_latency_tab ()
483 vector<string> outputs;
484 ARDOUR::AudioEngine::instance()->get_physical_outputs (ARDOUR::DataType::AUDIO, outputs);
485 set_popdown_strings (lm_output_channel_combo, outputs);
486 lm_output_channel_combo.set_active_text (outputs.front());
488 vector<string> inputs;
489 ARDOUR::AudioEngine::instance()->get_physical_inputs (ARDOUR::DataType::AUDIO, inputs);
490 set_popdown_strings (lm_input_channel_combo, inputs);
491 lm_input_channel_combo.set_active_text (inputs.front());
493 lm_measure_button.set_sensitive (true);
497 EngineControl::setup_midi_tab_for_backend ()
499 string backend = backend_combo.get_active_text ();
501 Gtkmm2ext::container_clear (midi_vbox);
503 midi_vbox.set_border_width (12);
504 midi_device_table.set_border_width (12);
506 if (backend == "JACK") {
507 setup_midi_tab_for_jack ();
510 midi_vbox.pack_start (midi_device_table, true, true);
511 midi_vbox.pack_start (midi_refresh_button, false, false);
512 midi_vbox.show_all ();
514 midi_refresh_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::refresh_midi_display));
518 EngineControl::setup_midi_tab_for_jack ()
523 EngineControl::refresh_midi_display ()
525 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
528 vector<string> midi_inputs;
529 vector<string> midi_outputs;
531 AttachOptions xopt = AttachOptions (FILL|EXPAND);
534 Gtkmm2ext::container_clear (midi_device_table);
536 backend->get_physical_inputs (ARDOUR::DataType::MIDI, midi_inputs);
537 backend->get_physical_outputs (ARDOUR::DataType::MIDI, midi_outputs);
539 midi_device_table.set_spacings (6);
540 midi_device_table.set_homogeneous (true);
541 midi_device_table.resize (midi_inputs.size() + midi_outputs.size() + 3, 1);
543 l = manage (new Label);
544 l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Inputs")));
545 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
546 l->set_alignment (0, 0.5);
550 for (vector<string>::iterator p = midi_inputs.begin(); p != midi_inputs.end(); ++p) {
551 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
552 l->set_alignment (0, 0.5);
553 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
558 row++; // extra row of spacing
560 l = manage (new Label);
561 l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Outputs")));
562 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
563 l->set_alignment (0, 0.5);
567 for (vector<string>::iterator p = midi_outputs.begin(); p != midi_outputs.end(); ++p) {
568 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
569 l->set_alignment (0, 0.5);
570 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
577 EngineControl::update_sensitivity ()
582 EngineControl::backend_changed ()
584 if (ignore_changes) {
588 string backend_name = backend_combo.get_active_text();
589 boost::shared_ptr<ARDOUR::AudioBackend> backend;
591 if (!(backend = ARDOUR::AudioEngine::instance()->set_backend (backend_name, "ardour", ""))) {
592 /* eh? setting the backend failed... how ? */
596 _have_control = ARDOUR::AudioEngine::instance()->setup_required ();
599 setup_midi_tab_for_backend ();
601 if (backend->requires_driver_selection()) {
602 vector<string> drivers = backend->enumerate_drivers();
604 if (!drivers.empty()) {
606 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
607 set_popdown_strings (driver_combo, drivers);
608 driver_combo.set_active_text (drivers.front());
615 driver_combo.set_sensitive (false);
616 /* this will change the device text which will cause a call to
617 * device changed which will set up parameters
622 vector<string> midi_options = backend->enumerate_midi_options();
624 if (midi_options.size() == 1) {
625 /* only contains the "none" option */
626 midi_option_combo.set_sensitive (false);
629 set_popdown_strings (midi_option_combo, midi_options);
630 midi_option_combo.set_active_text (midi_options.front());
631 midi_option_combo.set_sensitive (true);
633 midi_option_combo.set_sensitive (false);
637 maybe_display_saved_state ();
641 EngineControl::print_channel_count (Gtk::SpinButton* sb)
643 uint32_t cnt = (uint32_t) sb->get_value();
645 sb->set_text (_("all available channels"));
648 snprintf (buf, sizeof (buf), "%d", cnt);
655 EngineControl::list_devices ()
657 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
660 /* now fill out devices, mark sample rates, buffer sizes insensitive */
662 vector<ARDOUR::AudioBackend::DeviceStatus> all_devices = backend->enumerate_devices ();
664 /* NOTE: Ardour currently does not display the "available" field of the
667 * Doing so would require a different GUI widget than the combo
668 * box/popdown that we currently use, since it has no way to list
669 * items that are not selectable. Something more like a popup menu,
670 * which could have unselectable items, would be appropriate.
673 vector<string> available_devices;
675 for (vector<ARDOUR::AudioBackend::DeviceStatus>::const_iterator i = all_devices.begin(); i != all_devices.end(); ++i) {
676 available_devices.push_back (i->name);
679 if (!available_devices.empty()) {
681 update_sensitivity ();
684 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
685 set_popdown_strings (device_combo, available_devices);
686 device_combo.set_active_text (available_devices.front());
691 ok_button->set_sensitive (true);
692 apply_button->set_sensitive (true);
695 sample_rate_combo.set_sensitive (false);
696 buffer_size_combo.set_sensitive (false);
697 input_latency.set_sensitive (false);
698 output_latency.set_sensitive (false);
699 input_channels.set_sensitive (false);
700 output_channels.set_sensitive (false);
701 ok_button->set_sensitive (false);
702 apply_button->set_sensitive (false);
707 EngineControl::driver_changed ()
709 if (ignore_changes) {
713 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
716 backend->set_driver (driver_combo.get_active_text());
719 maybe_display_saved_state ();
723 EngineControl::device_changed ()
725 if (ignore_changes) {
729 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
731 string device_name = device_combo.get_active_text ();
735 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
737 /* don't allow programmatic change to combos to cause a
738 recursive call to this method.
748 sr = backend->available_sample_rates (device_name);
751 sr.push_back (8000.0f);
752 sr.push_back (16000.0f);
753 sr.push_back (32000.0f);
754 sr.push_back (44100.0f);
755 sr.push_back (48000.0f);
756 sr.push_back (88200.0f);
757 sr.push_back (96000.0f);
758 sr.push_back (192000.0f);
759 sr.push_back (384000.0f);
762 for (vector<float>::const_iterator x = sr.begin(); x != sr.end(); ++x) {
763 s.push_back (rate_as_string (*x));
764 if (*x == _desired_sample_rate) {
770 sample_rate_combo.set_sensitive (true);
771 set_popdown_strings (sample_rate_combo, s);
773 if (desired.empty()) {
774 sample_rate_combo.set_active_text (s.front());
776 sample_rate_combo.set_active_text (desired);
780 sample_rate_combo.set_sensitive (false);
788 bs = backend->available_buffer_sizes(device_name);
789 } else if (backend->can_change_buffer_size_when_running()) {
803 for (vector<uint32_t>::const_iterator x = bs.begin(); x != bs.end(); ++x) {
804 s.push_back (bufsize_as_string (*x));
808 buffer_size_combo.set_sensitive (true);
809 set_popdown_strings (buffer_size_combo, s);
811 buffer_size_combo.set_active_text (s.front());
812 show_buffer_duration ();
814 buffer_size_combo.set_sensitive (false);
817 /* XXX theoretically need to set min + max channel counts here
820 manage_control_app_sensitivity ();
823 /* pick up any saved state for this device */
825 maybe_display_saved_state ();
827 /* and push it to the backend */
829 push_state_to_backend (false);
833 EngineControl::bufsize_as_string (uint32_t sz)
835 /* Translators: "samples" is always plural here, so no
836 need for plural+singular forms.
839 snprintf (buf, sizeof (buf), _("%u samples"), sz);
844 EngineControl::sample_rate_changed ()
846 if (ignore_changes) {
850 /* reset the strings for buffer size to show the correct msec value
851 (reflecting the new sample rate).
854 show_buffer_duration ();
860 EngineControl::buffer_size_changed ()
862 if (ignore_changes) {
866 show_buffer_duration ();
871 EngineControl::show_buffer_duration ()
874 /* buffer sizes - convert from just samples to samples + msecs for
875 * the displayed string
878 string bs_text = buffer_size_combo.get_active_text ();
879 uint32_t samples = atoi (bs_text); /* will ignore trailing text */
880 uint32_t rate = get_rate();
882 /* Translators: "msecs" is ALWAYS plural here, so we do not
883 need singular form as well.
885 /* Developers: note the hard-coding of a double buffered model
886 in the (2 * samples) computation of latency. we always start
887 the audiobackend in this configuration.
890 snprintf (buf, sizeof (buf), _("(%.1f msecs)"), (2 * samples) / (rate/1000.0));
891 buffer_size_duration_label.set_text (buf);
895 EngineControl::midi_option_changed ()
897 if (!ignore_changes) {
903 EngineControl::parameter_changed ()
905 if (!ignore_changes) {
910 EngineControl::State*
911 EngineControl::get_matching_state (const string& backend,
912 const string& driver,
913 const string& device)
915 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
916 if ((*i).backend == backend &&
917 (*i).driver == driver &&
918 (*i).device == device) {
925 EngineControl::State*
926 EngineControl::get_saved_state_for_currently_displayed_backend_and_device ()
928 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
931 return get_matching_state (backend_combo.get_active_text(),
932 (backend->requires_driver_selection() ? (std::string) driver_combo.get_active_text() : string()),
933 device_combo.get_active_text());
937 return get_matching_state (backend_combo.get_active_text(),
939 device_combo.get_active_text());
942 EngineControl::State*
943 EngineControl::save_state ()
945 if (!_have_control) {
949 bool existing = true;
950 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
957 store_state (*state);
960 states.push_back (*state);
967 EngineControl::store_state (State& state)
969 state.backend = get_backend ();
970 state.driver = get_driver ();
971 state.device = get_device_name ();
972 state.sample_rate = get_rate ();
973 state.buffer_size = get_buffer_size ();
974 state.input_latency = get_input_latency ();
975 state.output_latency = get_output_latency ();
976 state.input_channels = get_input_channels ();
977 state.output_channels = get_output_channels ();
978 state.midi_option = get_midi_option ();
982 EngineControl::maybe_display_saved_state ()
984 if (!_have_control) {
988 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
991 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
993 if (!_desired_sample_rate) {
994 sample_rate_combo.set_active_text (rate_as_string (state->sample_rate));
996 buffer_size_combo.set_active_text (bufsize_as_string (state->buffer_size));
997 /* call this explicitly because we're ignoring changes to
998 the controls at this point.
1000 show_buffer_duration ();
1001 input_latency.set_value (state->input_latency);
1002 output_latency.set_value (state->output_latency);
1004 if (!state->midi_option.empty()) {
1005 midi_option_combo.set_active_text (state->midi_option);
1011 EngineControl::get_state ()
1013 XMLNode* root = new XMLNode ("AudioMIDISetup");
1016 if (!states.empty()) {
1017 XMLNode* state_nodes = new XMLNode ("EngineStates");
1019 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
1021 XMLNode* node = new XMLNode ("State");
1023 node->add_property ("backend", (*i).backend);
1024 node->add_property ("driver", (*i).driver);
1025 node->add_property ("device", (*i).device);
1026 node->add_property ("sample-rate", (*i).sample_rate);
1027 node->add_property ("buffer-size", (*i).buffer_size);
1028 node->add_property ("input-latency", (*i).input_latency);
1029 node->add_property ("output-latency", (*i).output_latency);
1030 node->add_property ("input-channels", (*i).input_channels);
1031 node->add_property ("output-channels", (*i).output_channels);
1032 node->add_property ("active", (*i).active ? "yes" : "no");
1033 node->add_property ("midi-option", (*i).midi_option);
1035 state_nodes->add_child_nocopy (*node);
1038 root->add_child_nocopy (*state_nodes);
1045 EngineControl::set_state (const XMLNode& root)
1047 XMLNodeList clist, cclist;
1048 XMLNodeConstIterator citer, cciter;
1050 XMLNode* grandchild;
1051 XMLProperty* prop = NULL;
1053 if (root.name() != "AudioMIDISetup") {
1057 clist = root.children();
1061 for (citer = clist.begin(); citer != clist.end(); ++citer) {
1065 if (child->name() != "EngineStates") {
1069 cclist = child->children();
1071 for (cciter = cclist.begin(); cciter != cclist.end(); ++cciter) {
1074 grandchild = *cciter;
1076 if (grandchild->name() != "State") {
1080 if ((prop = grandchild->property ("backend")) == 0) {
1083 state.backend = prop->value ();
1085 if ((prop = grandchild->property ("driver")) == 0) {
1088 state.driver = prop->value ();
1090 if ((prop = grandchild->property ("device")) == 0) {
1093 state.device = prop->value ();
1095 if ((prop = grandchild->property ("sample-rate")) == 0) {
1098 state.sample_rate = atof (prop->value ());
1100 if ((prop = grandchild->property ("buffer-size")) == 0) {
1103 state.buffer_size = atoi (prop->value ());
1105 if ((prop = grandchild->property ("input-latency")) == 0) {
1108 state.input_latency = atoi (prop->value ());
1110 if ((prop = grandchild->property ("output-latency")) == 0) {
1113 state.output_latency = atoi (prop->value ());
1115 if ((prop = grandchild->property ("input-channels")) == 0) {
1118 state.input_channels = atoi (prop->value ());
1120 if ((prop = grandchild->property ("output-channels")) == 0) {
1123 state.output_channels = atoi (prop->value ());
1125 if ((prop = grandchild->property ("active")) == 0) {
1128 state.active = string_is_affirmative (prop->value ());
1130 if ((prop = grandchild->property ("midi-option")) == 0) {
1133 state.midi_option = prop->value ();
1135 states.push_back (state);
1139 /* now see if there was an active state and switch the setup to it */
1141 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
1145 backend_combo.set_active_text ((*i).backend);
1146 driver_combo.set_active_text ((*i).driver);
1147 device_combo.set_active_text ((*i).device);
1148 sample_rate_combo.set_active_text (rate_as_string ((*i).sample_rate));
1149 buffer_size_combo.set_active_text (bufsize_as_string ((*i).buffer_size));
1150 input_latency.set_value ((*i).input_latency);
1151 output_latency.set_value ((*i).output_latency);
1152 midi_option_combo.set_active_text ((*i).midi_option);
1161 EngineControl::push_state_to_backend (bool start)
1167 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1173 /* figure out what is going to change */
1175 bool restart_required = false;
1176 bool was_running = ARDOUR::AudioEngine::instance()->running();
1177 bool change_driver = false;
1178 bool change_device = false;
1179 bool change_rate = false;
1180 bool change_bufsize = false;
1181 bool change_latency = false;
1182 bool change_channels = false;
1183 bool change_midi = false;
1185 uint32_t ochan = get_output_channels ();
1186 uint32_t ichan = get_input_channels ();
1188 if (_have_control) {
1190 if (started_at_least_once) {
1192 /* we can control the backend */
1194 if (backend->requires_driver_selection()) {
1195 if (get_driver() != backend->driver_name()) {
1196 change_driver = true;
1200 if (get_device_name() != backend->device_name()) {
1201 change_device = true;
1204 if (get_rate() != backend->sample_rate()) {
1208 if (get_buffer_size() != backend->buffer_size()) {
1209 change_bufsize = true;
1212 if (get_midi_option() != backend->midi_option()) {
1216 /* zero-requested channels means "all available" */
1219 ichan = backend->input_channels();
1223 ochan = backend->output_channels();
1226 if (ichan != backend->input_channels()) {
1227 change_channels = true;
1230 if (ochan != backend->output_channels()) {
1231 change_channels = true;
1234 if (get_input_latency() != backend->systemic_input_latency() ||
1235 get_output_latency() != backend->systemic_output_latency()) {
1236 change_latency = true;
1239 /* backend never started, so we have to force a group
1242 change_driver = true;
1243 change_device = true;
1245 change_bufsize = true;
1246 change_channels = true;
1247 change_latency = true;
1253 /* we have no control over the backend, meaning that we can
1254 * only possibly change sample rate and buffer size.
1258 if (get_rate() != backend->sample_rate()) {
1259 change_bufsize = true;
1262 if (get_buffer_size() != backend->buffer_size()) {
1263 change_bufsize = true;
1267 if (!_have_control) {
1269 /* We do not have control over the backend, so the best we can
1270 * do is try to change the sample rate and/or bufsize and get
1274 if (change_rate && !backend->can_change_sample_rate_when_running()) {
1278 if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1283 backend->set_sample_rate (get_rate());
1286 if (change_bufsize) {
1287 backend->set_buffer_size (get_buffer_size());
1295 /* determine if we need to stop the backend before changing parameters */
1297 if (change_driver || change_device || change_channels || change_latency ||
1298 (change_rate && !backend->can_change_sample_rate_when_running()) ||
1300 (change_bufsize && !backend->can_change_buffer_size_when_running())) {
1301 restart_required = true;
1303 restart_required = false;
1308 if (!change_driver && !change_device && !change_channels && !change_latency && !change_midi) {
1309 /* no changes in any parameters that absolutely require a
1310 * restart, so check those that might be changeable without a
1314 if (change_rate && !backend->can_change_sample_rate_when_running()) {
1315 /* can't do this while running ... */
1316 restart_required = true;
1319 if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1320 /* can't do this while running ... */
1321 restart_required = true;
1327 if (restart_required) {
1328 if (ARDOUR_UI::instance()->disconnect_from_engine ()) {
1335 if (change_driver && backend->set_driver (get_driver())) {
1336 error << string_compose (_("Cannot set driver to %1"), get_driver()) << endmsg;
1339 if (change_device && backend->set_device_name (get_device_name())) {
1340 error << string_compose (_("Cannot set device name to %1"), get_device_name()) << endmsg;
1343 if (change_rate && backend->set_sample_rate (get_rate())) {
1344 error << string_compose (_("Cannot set sample rate to %1"), get_rate()) << endmsg;
1347 if (change_bufsize && backend->set_buffer_size (get_buffer_size())) {
1348 error << string_compose (_("Cannot set buffer size to %1"), get_buffer_size()) << endmsg;
1352 if (change_channels || get_input_channels() == 0 || get_output_channels() == 0) {
1353 if (backend->set_input_channels (get_input_channels())) {
1354 error << string_compose (_("Cannot set input channels to %1"), get_input_channels()) << endmsg;
1357 if (backend->set_output_channels (get_output_channels())) {
1358 error << string_compose (_("Cannot set output channels to %1"), get_output_channels()) << endmsg;
1362 if (change_latency) {
1363 if (backend->set_systemic_input_latency (get_input_latency())) {
1364 error << string_compose (_("Cannot set input latency to %1"), get_input_latency()) << endmsg;
1367 if (backend->set_systemic_output_latency (get_output_latency())) {
1368 error << string_compose (_("Cannot set output latency to %1"), get_output_latency()) << endmsg;
1374 backend->set_midi_option (get_midi_option());
1377 if (start || (was_running && restart_required)) {
1378 if (ARDOUR_UI::instance()->reconnect_to_engine()) {
1389 EngineControl::post_push ()
1391 /* get a pointer to the current state object, creating one if
1395 if (_have_control) {
1396 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
1399 state = save_state ();
1405 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
1406 (*i).active = false;
1409 /* mark this one active (to be used next time the dialog is
1413 state->active = true;
1415 manage_control_app_sensitivity ();
1418 /* schedule a redisplay of MIDI ports */
1420 Glib::signal_timeout().connect (sigc::bind_return (sigc::mem_fun (*this, &EngineControl::refresh_midi_display), false), 1000);
1425 EngineControl::get_rate () const
1427 float r = atof (sample_rate_combo.get_active_text ());
1428 /* the string may have been translated with an abbreviation for
1429 * thousands, so use a crude heuristic to fix this.
1439 EngineControl::get_buffer_size () const
1441 string txt = buffer_size_combo.get_active_text ();
1444 if (sscanf (txt.c_str(), "%d", &samples) != 1) {
1452 EngineControl::get_midi_option () const
1454 return midi_option_combo.get_active_text();
1458 EngineControl::get_input_channels() const
1460 return (uint32_t) input_channels_adjustment.get_value();
1464 EngineControl::get_output_channels() const
1466 return (uint32_t) output_channels_adjustment.get_value();
1470 EngineControl::get_input_latency() const
1472 return (uint32_t) input_latency_adjustment.get_value();
1476 EngineControl::get_output_latency() const
1478 return (uint32_t) output_latency_adjustment.get_value();
1482 EngineControl::get_backend () const
1484 return backend_combo.get_active_text ();
1488 EngineControl::get_driver () const
1490 return driver_combo.get_active_text ();
1494 EngineControl::get_device_name () const
1496 return device_combo.get_active_text ();
1500 EngineControl::control_app_button_clicked ()
1502 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1508 backend->launch_control_app ();
1512 EngineControl::manage_control_app_sensitivity ()
1514 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1520 string appname = backend->control_app_name();
1522 if (appname.empty()) {
1523 control_app_button.set_sensitive (false);
1525 control_app_button.set_sensitive (true);
1530 EngineControl::set_desired_sample_rate (uint32_t sr)
1532 _desired_sample_rate = sr;
1537 EngineControl::on_switch_page (GtkNotebookPage*, guint page_num)
1539 if (page_num == 0) {
1540 cancel_button->set_sensitive (true);
1541 ok_button->set_sensitive (true);
1542 apply_button->set_sensitive (true);
1544 cancel_button->set_sensitive (false);
1545 ok_button->set_sensitive (false);
1546 apply_button->set_sensitive (false);
1549 if (page_num == midi_tab) {
1551 refresh_midi_display ();
1554 if (page_num == latency_tab) {
1557 if (!ARDOUR::AudioEngine::instance()->running()) {
1559 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
1561 /* save any existing latency values */
1563 uint32_t il = (uint32_t) input_latency.get_value ();
1564 uint32_t ol = (uint32_t) input_latency.get_value ();
1566 /* reset to zero so that our new test instance of JACK
1567 will be clean of any existing latency measures.
1570 input_latency.set_value (0);
1571 output_latency.set_value (0);
1575 input_latency.set_value (il);
1576 output_latency.set_value (ol);
1580 if (ARDOUR::AudioEngine::instance()->prepare_for_latency_measurement()) {
1581 disable_latency_tab ();
1584 enable_latency_tab ();
1587 ARDOUR::AudioEngine::instance()->stop_latency_detection();
1591 /* latency measurement */
1594 EngineControl::check_latency_measurement ()
1596 MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1598 if (mtdm->resolve () < 0) {
1599 lm_results.set_markup (string_compose ("<span foreground=\"red\">%1</span>", _("No signal detected ")));
1603 if (mtdm->err () > 0.3) {
1609 ARDOUR::framecnt_t const sample_rate = ARDOUR::AudioEngine::instance()->sample_rate();
1611 if (sample_rate == 0) {
1612 lm_results.set_text (_("Disconnected from audio engine"));
1613 ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1617 uint32_t frames_total = mtdm->del();
1618 uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1620 snprintf (buf, sizeof (buf), "%u samples %10.3lf ms", extra, extra * 1000.0f/sample_rate);
1624 if (mtdm->err () > 0.2) {
1625 strcat (buf, " ??");
1630 strcat (buf, " (Inv)");
1635 lm_measure_button.set_active (false);
1636 lm_use_button.set_sensitive (true);
1637 strcat (buf, " (set)");
1638 have_lm_results = true;
1641 lm_results.set_text (buf);
1647 EngineControl::start_latency_detection ()
1649 ARDOUR::AudioEngine::instance()->set_latency_input_port (lm_input_channel_combo.get_active_text());
1650 ARDOUR::AudioEngine::instance()->set_latency_output_port (lm_output_channel_combo.get_active_text());
1651 ARDOUR::AudioEngine::instance()->start_latency_detection ();
1652 lm_results.set_text (_("Detecting ..."));
1653 latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &EngineControl::check_latency_measurement), 250);
1654 lm_start_stop_label.set_text (_("Cancel measurement"));
1655 have_lm_results = false;
1656 lm_input_channel_combo.set_sensitive (false);
1657 lm_output_channel_combo.set_sensitive (false);
1661 EngineControl::end_latency_detection ()
1663 ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1664 latency_timeout.disconnect ();
1665 lm_start_stop_label.set_text (_("Measure latency"));
1666 if (!have_lm_results) {
1667 lm_results.set_markup ("<i>No measurement results yet</i>");
1669 lm_input_channel_combo.set_sensitive (true);
1670 lm_output_channel_combo.set_sensitive (true);
1674 EngineControl::latency_button_toggled ()
1676 if (lm_measure_button.get_active ()) {
1677 start_latency_detection ();
1679 end_latency_detection ();
1684 EngineControl::use_latency_button_clicked ()
1686 MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1692 uint32_t frames_total = mtdm->del();
1693 uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1694 uint32_t one_way = extra/2;
1696 input_latency_adjustment.set_value (one_way);
1697 output_latency_adjustment.set_value (one_way);
1701 EngineControl::on_delete_event (GdkEventAny* ev)
1703 if (notebook.get_current_page() == 2) {
1704 /* currently on latency tab - be sure to clean up */
1705 end_latency_detection ();
1707 return ArdourDialog::on_delete_event (ev);
1711 EngineControl::engine_running ()
1713 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1716 buffer_size_combo.set_active_text (bufsize_as_string (backend->buffer_size()));
1717 sample_rate_combo.set_active_text (rate_as_string (backend->sample_rate()));
1719 buffer_size_combo.set_sensitive (true);
1720 sample_rate_combo.set_sensitive (true);
1722 connect_disconnect_button.set_label (string_compose (_("Disconnect from %1"), backend->name()));
1724 started_at_least_once = true;
1728 EngineControl::engine_stopped ()
1730 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1733 buffer_size_combo.set_sensitive (false);
1734 connect_disconnect_button.set_label (string_compose (_("Connect to %1"), backend->name()));
1736 sample_rate_combo.set_sensitive (true);
1737 buffer_size_combo.set_sensitive (true);
1741 EngineControl::connect_disconnect_click()
1743 if (ARDOUR::AudioEngine::instance()->running()) {
1744 ARDOUR_UI::instance()->disconnect_from_engine ();
1746 ARDOUR_UI::instance()->reconnect_to_engine ();