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 EngineControl::EngineControl ()
62 : ArdourDialog (_("Audio/MIDI Setup"))
64 , input_latency_adjustment (0, 0, 99999, 1)
65 , input_latency (input_latency_adjustment)
66 , output_latency_adjustment (0, 0, 99999, 1)
67 , output_latency (output_latency_adjustment)
68 , input_channels_adjustment (0, 0, 256, 1)
69 , input_channels (input_channels_adjustment)
70 , output_channels_adjustment (0, 0, 256, 1)
71 , output_channels (output_channels_adjustment)
72 , ports_adjustment (128, 8, 1024, 1, 16)
73 , ports_spinner (ports_adjustment)
74 , control_app_button (_("Device Control Panel"))
75 , lm_start_stop_label (_("Measure latency"))
76 , lm_use_button (_("Use results"))
78 , have_lm_results (false)
79 , midi_refresh_button (_("Refresh list"))
81 , _desired_sample_rate (0)
83 , started_at_least_once (false)
85 using namespace Notebook_Helpers;
86 vector<string> strings;
88 AttachOptions xopt = AttachOptions (FILL|EXPAND);
91 set_name (X_("AudioMIDISetup"));
93 /* the backend combo is the one thing that is ALWAYS visible
96 vector<const ARDOUR::AudioBackendInfo*> backends = ARDOUR::AudioEngine::instance()->available_backends();
98 if (backends.empty()) {
99 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));
101 throw failed_constructor ();
104 for (vector<const ARDOUR::AudioBackendInfo*>::const_iterator b = backends.begin(); b != backends.end(); ++b) {
105 strings.push_back ((*b)->name);
108 set_popdown_strings (backend_combo, strings);
109 backend_combo.set_active_text (strings.front());
110 backend_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::backend_changed));
112 /* setup basic packing characteristics for the table used on the main
113 * tab of the notebook
116 basic_packer.set_spacings (6);
117 basic_packer.set_border_width (12);
118 basic_packer.set_homogeneous (true);
122 basic_hbox.pack_start (basic_packer, false, false);
126 /* latency measurement tab */
128 lm_title.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Latency Measurement Tool")));
131 lm_table.set_row_spacings (12);
133 lm_table.attach (lm_title, 0, 2, row, row+1, xopt, (AttachOptions) 0);
136 Gtk::Label* preamble;
138 preamble = manage (new Label);
139 preamble->set_width_chars (60);
140 preamble->set_line_wrap (true);
141 preamble->set_markup (_("<span weight=\"bold\">Turn down the volume on your hardware to a very low level.</span>"));
143 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
146 preamble = manage (new Label);
147 preamble->set_width_chars (60);
148 preamble->set_line_wrap (true);
149 preamble->set_markup (_("Select two channels below and connect them using a cable or (less ideally) a speaker and microphone."));
151 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
154 label = manage (new Label (_("Output channel")));
155 lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
157 Gtk::Alignment* misc_align = manage (new Alignment (0.0, 0.5));
158 misc_align->add (lm_output_channel_combo);
159 lm_table.attach (*misc_align, 1, 2, row, row+1, xopt, (AttachOptions) 0);
162 label = manage (new Label (_("Input channel")));
163 lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
165 misc_align = manage (new Alignment (0.0, 0.5));
166 misc_align->add (lm_input_channel_combo);
167 lm_table.attach (*misc_align, 1, 2, row, row+1, FILL, (AttachOptions) 0);
170 xopt = AttachOptions(0);
172 lm_measure_button.add (lm_start_stop_label);
174 lm_measure_button.signal_toggled().connect (sigc::mem_fun (*this, &EngineControl::latency_button_toggled));
175 lm_use_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::use_latency_button_clicked));
176 lm_use_button.set_sensitive (false);
178 preamble = manage (new Label);
179 preamble->set_width_chars (60);
180 preamble->set_line_wrap (true);
181 preamble->set_markup (_("Once the channels are connected, click the \"Measure latency\" button."));
182 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
185 lm_table.attach (lm_measure_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
187 lm_table.attach (lm_results, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
191 preamble = manage (new Label);
192 preamble->set_width_chars (60);
193 preamble->set_line_wrap (true);
194 preamble->set_markup (_("When satisfied with the results, click the \"Use results\" button."));
195 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
198 lm_table.attach (lm_use_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
201 lm_results.set_markup ("<i>No measurement results yet</i>");
203 lm_vbox.set_border_width (12);
204 lm_vbox.pack_start (lm_table, false, false);
208 notebook.pages().push_back (TabElem (basic_vbox, _("Audio")));
209 // notebook.pages().push_back (TabElem (midi_vbox, _("MIDI")));
210 notebook.pages().push_back (TabElem (lm_vbox, _("Latency")));
211 notebook.set_border_width (12);
213 notebook.set_tab_pos (POS_RIGHT);
214 notebook.show_all ();
216 notebook.set_name ("SettingsNotebook");
218 /* packup the notebook */
220 get_vbox()->set_border_width (12);
221 get_vbox()->pack_start (notebook);
223 /* need a special function to print "all available channels" when the
224 * channel counts hit zero.
227 input_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &input_channels));
228 output_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &output_channels));
230 control_app_button.signal_clicked().connect (mem_fun (*this, &EngineControl::control_app_button_clicked));
231 manage_control_app_sensitivity ();
233 cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
234 ok_button = add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
235 apply_button = add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_APPLY);
237 /* Pick up any existing audio setup configuration, if appropriate */
239 XMLNode* audio_setup = ARDOUR::Config->extra_xml ("AudioMIDISetup");
241 ARDOUR::AudioEngine::instance()->Running.connect (running_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_running, this), gui_context());
242 ARDOUR::AudioEngine::instance()->Stopped.connect (stopped_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_stopped, this), gui_context());
243 ARDOUR::AudioEngine::instance()->Halted.connect (stopped_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_stopped, this), gui_context());
248 set_state (*audio_setup);
251 /* Connect to signals */
253 driver_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::driver_changed));
254 sample_rate_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::sample_rate_changed));
255 buffer_size_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::buffer_size_changed));
256 device_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::device_changed));
258 input_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
259 output_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
260 input_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
261 output_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
263 notebook.signal_switch_page().connect (sigc::mem_fun (*this, &EngineControl::on_switch_page));
269 EngineControl::on_response (int response_id)
271 ArdourDialog::on_response (response_id);
273 switch (response_id) {
275 push_state_to_backend (true);
278 push_state_to_backend (true);
281 case RESPONSE_DELETE_EVENT: {
283 ev.type = GDK_BUTTON_PRESS;
285 on_delete_event ((GdkEventAny*) &ev);
294 EngineControl::build_notebook ()
297 AttachOptions xopt = AttachOptions (FILL|EXPAND);
299 /* clear the table */
301 Gtkmm2ext::container_clear (basic_vbox);
302 Gtkmm2ext::container_clear (basic_packer);
304 label = manage (left_aligned_label (_("Audio System:")));
305 basic_packer.attach (*label, 0, 1, 0, 1, xopt, (AttachOptions) 0);
306 basic_packer.attach (backend_combo, 1, 2, 0, 1, xopt, (AttachOptions) 0);
309 build_full_control_notebook ();
311 build_no_control_notebook ();
314 basic_vbox.pack_start (basic_hbox, false, false);
317 Gtk::HBox* hpacker = manage (new HBox);
318 hpacker->set_border_width (12);
319 hpacker->pack_start (control_app_button, false, false);
321 control_app_button.show();
322 basic_vbox.pack_start (*hpacker);
325 basic_vbox.show_all ();
329 EngineControl::build_full_control_notebook ()
331 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
334 using namespace Notebook_Helpers;
336 vector<string> strings;
337 AttachOptions xopt = AttachOptions (FILL|EXPAND);
338 int row = 1; // row zero == backend combo
340 /* start packing it up */
342 if (backend->requires_driver_selection()) {
343 label = manage (left_aligned_label (_("Driver:")));
344 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
345 basic_packer.attach (driver_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
349 label = manage (left_aligned_label (_("Device:")));
350 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
351 basic_packer.attach (device_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
354 label = manage (left_aligned_label (_("Sample rate:")));
355 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
356 basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
360 label = manage (left_aligned_label (_("Buffer size:")));
361 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
362 basic_packer.attach (buffer_size_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
363 buffer_size_duration_label.set_alignment (0.0); /* left-align */
364 basic_packer.attach (buffer_size_duration_label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
367 input_channels.set_name ("InputChannels");
368 input_channels.set_flags(Gtk::CAN_FOCUS);
369 input_channels.set_digits(0);
370 input_channels.set_wrap(false);
371 output_channels.set_editable (true);
373 label = manage (left_aligned_label (_("Input Channels:")));
374 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
375 basic_packer.attach (input_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
378 output_channels.set_name ("OutputChannels");
379 output_channels.set_flags(Gtk::CAN_FOCUS);
380 output_channels.set_digits(0);
381 output_channels.set_wrap(false);
382 output_channels.set_editable (true);
384 label = manage (left_aligned_label (_("Output Channels:")));
385 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
386 basic_packer.attach (output_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
389 input_latency.set_name ("InputLatency");
390 input_latency.set_flags(Gtk::CAN_FOCUS);
391 input_latency.set_digits(0);
392 input_latency.set_wrap(false);
393 input_latency.set_editable (true);
395 label = manage (left_aligned_label (_("Hardware input latency:")));
396 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
397 basic_packer.attach (input_latency, 1, 2, row, row+1, xopt, (AttachOptions) 0);
398 label = manage (left_aligned_label (_("samples")));
399 basic_packer.attach (*label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
402 output_latency.set_name ("OutputLatency");
403 output_latency.set_flags(Gtk::CAN_FOCUS);
404 output_latency.set_digits(0);
405 output_latency.set_wrap(false);
406 output_latency.set_editable (true);
408 label = manage (left_aligned_label (_("Hardware output latency:")));
409 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
410 basic_packer.attach (output_latency, 1, 2, row, row+1, xopt, (AttachOptions) 0);
411 label = manage (left_aligned_label (_("samples")));
412 basic_packer.attach (*label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
415 label = manage (left_aligned_label (_("MIDI I/O using")));
416 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
417 basic_packer.attach (midi_option_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
422 EngineControl::build_no_control_notebook ()
424 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
427 using namespace Notebook_Helpers;
429 vector<string> strings;
430 AttachOptions xopt = AttachOptions (FILL|EXPAND);
431 int row = 1; // row zero == backend combo
432 const string msg = string_compose (_("The %1 audio backend was configured and started externally.\nThis limits your control over it."), backend->name());
434 label = manage (new Label);
435 label->set_markup (string_compose ("<span weight=\"bold\" foreground=\"red\">%1</span>", msg));
436 basic_packer.attach (*label, 0, 2, row, row + 1, xopt, (AttachOptions) 0);
439 if (backend->can_change_sample_rate_when_running()) {
440 label = manage (left_aligned_label (_("Sample rate:")));
441 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
442 basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
446 if (backend->can_change_buffer_size_when_running()) {
447 label = manage (left_aligned_label (_("Buffer size:")));
448 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
449 basic_packer.attach (buffer_size_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
450 buffer_size_duration_label.set_alignment (0.0); /* left-align */
451 basic_packer.attach (buffer_size_duration_label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
455 connect_disconnect_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::connect_disconnect_click));
457 basic_packer.attach (connect_disconnect_button, 0, 2, row, row+1, FILL, AttachOptions (0));
461 EngineControl::~EngineControl ()
467 EngineControl::disable_latency_tab ()
469 vector<string> empty;
470 set_popdown_strings (lm_output_channel_combo, empty);
471 set_popdown_strings (lm_input_channel_combo, empty);
472 lm_measure_button.set_sensitive (false);
473 lm_use_button.set_sensitive (false);
477 EngineControl::enable_latency_tab ()
479 vector<string> outputs;
480 ARDOUR::AudioEngine::instance()->get_physical_outputs (ARDOUR::DataType::AUDIO, outputs);
481 set_popdown_strings (lm_output_channel_combo, outputs);
482 lm_output_channel_combo.set_active_text (outputs.front());
484 vector<string> inputs;
485 ARDOUR::AudioEngine::instance()->get_physical_inputs (ARDOUR::DataType::AUDIO, inputs);
486 set_popdown_strings (lm_input_channel_combo, inputs);
487 lm_input_channel_combo.set_active_text (inputs.front());
489 lm_measure_button.set_sensitive (true);
493 EngineControl::setup_midi_tab_for_backend ()
495 string backend = backend_combo.get_active_text ();
497 Gtkmm2ext::container_clear (midi_vbox);
499 midi_vbox.set_border_width (12);
500 midi_device_table.set_border_width (12);
502 if (backend == "JACK") {
503 setup_midi_tab_for_jack ();
506 midi_vbox.pack_start (midi_device_table, true, true);
507 midi_vbox.pack_start (midi_refresh_button, false, false);
508 midi_vbox.show_all ();
510 midi_refresh_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::refresh_midi_display));
514 EngineControl::setup_midi_tab_for_jack ()
519 EngineControl::refresh_midi_display ()
521 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
524 vector<string> midi_inputs;
525 vector<string> midi_outputs;
527 AttachOptions xopt = AttachOptions (FILL|EXPAND);
530 Gtkmm2ext::container_clear (midi_device_table);
532 backend->get_physical_inputs (ARDOUR::DataType::MIDI, midi_inputs);
533 backend->get_physical_outputs (ARDOUR::DataType::MIDI, midi_outputs);
535 midi_device_table.set_spacings (6);
536 midi_device_table.set_homogeneous (true);
537 midi_device_table.resize (midi_inputs.size() + midi_outputs.size() + 3, 1);
539 l = manage (new Label);
540 l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Inputs")));
541 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
542 l->set_alignment (0, 0.5);
546 for (vector<string>::iterator p = midi_inputs.begin(); p != midi_inputs.end(); ++p) {
547 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
548 l->set_alignment (0, 0.5);
549 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
554 row++; // extra row of spacing
556 l = manage (new Label);
557 l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Outputs")));
558 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
559 l->set_alignment (0, 0.5);
563 for (vector<string>::iterator p = midi_outputs.begin(); p != midi_outputs.end(); ++p) {
564 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
565 l->set_alignment (0, 0.5);
566 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
573 EngineControl::update_sensitivity ()
578 EngineControl::backend_changed ()
580 if (ignore_changes) {
584 string backend_name = backend_combo.get_active_text();
585 boost::shared_ptr<ARDOUR::AudioBackend> backend;
587 if (!(backend = ARDOUR::AudioEngine::instance()->set_backend (backend_name, "ardour", ""))) {
588 /* eh? setting the backend failed... how ? */
592 _have_control = ARDOUR::AudioEngine::instance()->setup_required ();
595 setup_midi_tab_for_backend ();
597 if (backend->requires_driver_selection()) {
598 vector<string> drivers = backend->enumerate_drivers();
600 if (!drivers.empty()) {
602 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
603 set_popdown_strings (driver_combo, drivers);
604 driver_combo.set_active_text (drivers.front());
611 driver_combo.set_sensitive (false);
612 /* this will change the device text which will cause a call to
613 * device changed which will set up parameters
618 vector<string> midi_options = backend->enumerate_midi_options();
620 if (midi_options.size() == 1) {
621 /* only contains the "none" option */
622 midi_option_combo.set_sensitive (false);
625 set_popdown_strings (midi_option_combo, midi_options);
626 midi_option_combo.set_active_text (midi_options.front());
627 midi_option_combo.set_sensitive (true);
629 midi_option_combo.set_sensitive (false);
633 maybe_display_saved_state ();
637 EngineControl::print_channel_count (Gtk::SpinButton* sb)
639 uint32_t cnt = (uint32_t) sb->get_value();
641 sb->set_text (_("all available channels"));
644 snprintf (buf, sizeof (buf), "%d", cnt);
651 EngineControl::list_devices ()
653 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
656 /* now fill out devices, mark sample rates, buffer sizes insensitive */
658 vector<ARDOUR::AudioBackend::DeviceStatus> all_devices = backend->enumerate_devices ();
660 /* NOTE: Ardour currently does not display the "available" field of the
663 * Doing so would require a different GUI widget than the combo
664 * box/popdown that we currently use, since it has no way to list
665 * items that are not selectable. Something more like a popup menu,
666 * which could have unselectable items, would be appropriate.
669 vector<string> available_devices;
671 for (vector<ARDOUR::AudioBackend::DeviceStatus>::const_iterator i = all_devices.begin(); i != all_devices.end(); ++i) {
672 available_devices.push_back (i->name);
675 if (!available_devices.empty()) {
677 update_sensitivity ();
680 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
681 set_popdown_strings (device_combo, available_devices);
682 device_combo.set_active_text (available_devices.front());
687 ok_button->set_sensitive (true);
688 apply_button->set_sensitive (true);
691 sample_rate_combo.set_sensitive (false);
692 buffer_size_combo.set_sensitive (false);
693 input_latency.set_sensitive (false);
694 output_latency.set_sensitive (false);
695 input_channels.set_sensitive (false);
696 output_channels.set_sensitive (false);
697 ok_button->set_sensitive (false);
698 apply_button->set_sensitive (false);
703 EngineControl::driver_changed ()
705 if (ignore_changes) {
709 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
712 backend->set_driver (driver_combo.get_active_text());
715 maybe_display_saved_state ();
719 EngineControl::device_changed ()
721 if (ignore_changes) {
725 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
727 string device_name = device_combo.get_active_text ();
731 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
733 /* don't allow programmatic change to combos to cause a
734 recursive call to this method.
744 sr = backend->available_sample_rates (device_name);
747 sr.push_back (8000.0f);
748 sr.push_back (16000.0f);
749 sr.push_back (32000.0f);
750 sr.push_back (44100.0f);
751 sr.push_back (48000.0f);
752 sr.push_back (88200.0f);
753 sr.push_back (96000.0f);
754 sr.push_back (192000.0f);
755 sr.push_back (384000.0f);
758 for (vector<float>::const_iterator x = sr.begin(); x != sr.end(); ++x) {
759 s.push_back (rate_as_string (*x));
760 if (*x == _desired_sample_rate) {
766 sample_rate_combo.set_sensitive (true);
767 set_popdown_strings (sample_rate_combo, s);
769 if (desired.empty()) {
770 sample_rate_combo.set_active_text (s.front());
772 sample_rate_combo.set_active_text (desired);
776 sample_rate_combo.set_sensitive (false);
784 bs = backend->available_buffer_sizes(device_name);
785 } else if (backend->can_change_buffer_size_when_running()) {
799 for (vector<uint32_t>::const_iterator x = bs.begin(); x != bs.end(); ++x) {
800 s.push_back (bufsize_as_string (*x));
804 buffer_size_combo.set_sensitive (true);
805 set_popdown_strings (buffer_size_combo, s);
807 buffer_size_combo.set_active_text (s.front());
808 show_buffer_duration ();
810 buffer_size_combo.set_sensitive (false);
813 /* XXX theoretically need to set min + max channel counts here
816 manage_control_app_sensitivity ();
819 /* pick up any saved state for this device */
821 maybe_display_saved_state ();
823 /* and push it to the backend */
825 push_state_to_backend (false);
829 EngineControl::bufsize_as_string (uint32_t sz)
831 /* Translators: "samples" is always plural here, so no
832 need for plural+singular forms.
835 snprintf (buf, sizeof (buf), _("%u samples"), sz);
840 EngineControl::sample_rate_changed ()
842 if (ignore_changes) {
846 /* reset the strings for buffer size to show the correct msec value
847 (reflecting the new sample rate).
850 show_buffer_duration ();
856 EngineControl::buffer_size_changed ()
858 if (ignore_changes) {
862 show_buffer_duration ();
867 EngineControl::show_buffer_duration ()
870 /* buffer sizes - convert from just samples to samples + msecs for
871 * the displayed string
874 string bs_text = buffer_size_combo.get_active_text ();
875 uint32_t samples = atoi (bs_text); /* will ignore trailing text */
876 uint32_t rate = get_rate();
878 /* Translators: "msecs" is ALWAYS plural here, so we do not
879 need singular form as well.
881 /* Developers: note the hard-coding of a double buffered model
882 in the (2 * samples) computation of latency. we always start
883 the audiobackend in this configuration.
886 snprintf (buf, sizeof (buf), _("(%.1f msecs)"), (2 * samples) / (rate/1000.0));
887 buffer_size_duration_label.set_text (buf);
891 EngineControl::parameter_changed ()
893 if (!ignore_changes) {
898 EngineControl::State*
899 EngineControl::get_matching_state (const string& backend,
900 const string& driver,
901 const string& device)
903 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
904 if ((*i).backend == backend &&
905 (*i).driver == driver &&
906 (*i).device == device) {
913 EngineControl::State*
914 EngineControl::get_saved_state_for_currently_displayed_backend_and_device ()
916 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
919 return get_matching_state (backend_combo.get_active_text(),
920 (backend->requires_driver_selection() ? (std::string) driver_combo.get_active_text() : string()),
921 device_combo.get_active_text());
925 return get_matching_state (backend_combo.get_active_text(),
927 device_combo.get_active_text());
930 EngineControl::State*
931 EngineControl::save_state ()
933 if (!_have_control) {
937 bool existing = true;
938 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
945 store_state (*state);
948 states.push_back (*state);
955 EngineControl::store_state (State& state)
957 state.backend = get_backend ();
958 state.driver = get_driver ();
959 state.device = get_device_name ();
960 state.sample_rate = get_rate ();
961 state.buffer_size = get_buffer_size ();
962 state.input_latency = get_input_latency ();
963 state.output_latency = get_output_latency ();
964 state.input_channels = get_input_channels ();
965 state.output_channels = get_output_channels ();
969 EngineControl::maybe_display_saved_state ()
971 if (!_have_control) {
975 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
978 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
980 if (!_desired_sample_rate) {
981 sample_rate_combo.set_active_text (rate_as_string (state->sample_rate));
983 buffer_size_combo.set_active_text (bufsize_as_string (state->buffer_size));
984 /* call this explicitly because we're ignoring changes to
985 the controls at this point.
987 show_buffer_duration ();
988 input_latency.set_value (state->input_latency);
989 output_latency.set_value (state->output_latency);
991 if (!state->midi_option.empty()) {
992 midi_option_combo.set_active_text (state->midi_option);
998 EngineControl::get_state ()
1000 XMLNode* root = new XMLNode ("AudioMIDISetup");
1003 if (!states.empty()) {
1004 XMLNode* state_nodes = new XMLNode ("EngineStates");
1006 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
1008 XMLNode* node = new XMLNode ("State");
1010 node->add_property ("backend", (*i).backend);
1011 node->add_property ("driver", (*i).driver);
1012 node->add_property ("device", (*i).device);
1013 node->add_property ("sample-rate", (*i).sample_rate);
1014 node->add_property ("buffer-size", (*i).buffer_size);
1015 node->add_property ("input-latency", (*i).input_latency);
1016 node->add_property ("output-latency", (*i).output_latency);
1017 node->add_property ("input-channels", (*i).input_channels);
1018 node->add_property ("output-channels", (*i).output_channels);
1019 node->add_property ("active", (*i).active ? "yes" : "no");
1020 node->add_property ("midi-option", (*i).midi_option);
1022 state_nodes->add_child_nocopy (*node);
1025 root->add_child_nocopy (*state_nodes);
1032 EngineControl::set_state (const XMLNode& root)
1034 XMLNodeList clist, cclist;
1035 XMLNodeConstIterator citer, cciter;
1037 XMLNode* grandchild;
1038 XMLProperty* prop = NULL;
1040 if (root.name() != "AudioMIDISetup") {
1044 clist = root.children();
1048 for (citer = clist.begin(); citer != clist.end(); ++citer) {
1052 if (child->name() != "EngineStates") {
1056 cclist = child->children();
1058 for (cciter = cclist.begin(); cciter != cclist.end(); ++cciter) {
1061 grandchild = *cciter;
1063 if (grandchild->name() != "State") {
1067 if ((prop = grandchild->property ("backend")) == 0) {
1070 state.backend = prop->value ();
1072 if ((prop = grandchild->property ("driver")) == 0) {
1075 state.driver = prop->value ();
1077 if ((prop = grandchild->property ("device")) == 0) {
1080 state.device = prop->value ();
1082 if ((prop = grandchild->property ("sample-rate")) == 0) {
1085 state.sample_rate = atof (prop->value ());
1087 if ((prop = grandchild->property ("buffer-size")) == 0) {
1090 state.buffer_size = atoi (prop->value ());
1092 if ((prop = grandchild->property ("input-latency")) == 0) {
1095 state.input_latency = atoi (prop->value ());
1097 if ((prop = grandchild->property ("output-latency")) == 0) {
1100 state.output_latency = atoi (prop->value ());
1102 if ((prop = grandchild->property ("input-channels")) == 0) {
1105 state.input_channels = atoi (prop->value ());
1107 if ((prop = grandchild->property ("output-channels")) == 0) {
1110 state.output_channels = atoi (prop->value ());
1112 if ((prop = grandchild->property ("active")) == 0) {
1115 state.active = string_is_affirmative (prop->value ());
1117 if ((prop = grandchild->property ("midi-option")) == 0) {
1120 state.midi_option = prop->value ();
1122 states.push_back (state);
1126 /* now see if there was an active state and switch the setup to it */
1128 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
1132 backend_combo.set_active_text ((*i).backend);
1133 driver_combo.set_active_text ((*i).driver);
1134 device_combo.set_active_text ((*i).device);
1135 sample_rate_combo.set_active_text (rate_as_string ((*i).sample_rate));
1136 buffer_size_combo.set_active_text (bufsize_as_string ((*i).buffer_size));
1137 input_latency.set_value ((*i).input_latency);
1138 output_latency.set_value ((*i).output_latency);
1139 midi_option_combo.set_active_text ((*i).midi_option);
1148 EngineControl::push_state_to_backend (bool start)
1154 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1160 /* figure out what is going to change */
1162 bool restart_required = false;
1163 bool was_running = ARDOUR::AudioEngine::instance()->running();
1164 bool change_driver = false;
1165 bool change_device = false;
1166 bool change_rate = false;
1167 bool change_bufsize = false;
1168 bool change_latency = false;
1169 bool change_channels = false;
1170 bool change_midi = false;
1172 uint32_t ochan = get_output_channels ();
1173 uint32_t ichan = get_input_channels ();
1175 if (_have_control) {
1177 if (started_at_least_once) {
1179 /* we can control the backend */
1181 if (backend->requires_driver_selection()) {
1182 if (get_driver() != backend->driver_name()) {
1183 change_driver = true;
1187 if (get_device_name() != backend->device_name()) {
1188 change_device = true;
1191 if (get_rate() != backend->sample_rate()) {
1195 if (get_buffer_size() != backend->buffer_size()) {
1196 change_bufsize = true;
1199 /* zero-requested channels means "all available" */
1202 ichan = backend->input_channels();
1206 ochan = backend->output_channels();
1209 if (ichan != backend->input_channels()) {
1210 change_channels = true;
1213 if (ochan != backend->output_channels()) {
1214 change_channels = true;
1217 if (get_input_latency() != backend->systemic_input_latency() ||
1218 get_output_latency() != backend->systemic_output_latency()) {
1219 change_latency = true;
1222 /* backend never started, so we have to force a group
1225 change_driver = true;
1226 change_device = true;
1228 change_bufsize = true;
1229 change_channels = true;
1230 change_latency = true;
1235 /* we have no control over the backend, meaning that we can
1236 * only possibly change sample rate and buffer size.
1240 if (get_rate() != backend->sample_rate()) {
1241 change_bufsize = true;
1244 if (get_buffer_size() != backend->buffer_size()) {
1245 change_bufsize = true;
1249 if (!_have_control) {
1251 /* We do not have control over the backend, so the best we can
1252 * do is try to change the sample rate and/or bufsize and get
1256 if (change_rate && !backend->can_change_sample_rate_when_running()) {
1260 if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1265 backend->set_sample_rate (get_rate());
1268 if (change_bufsize) {
1269 backend->set_buffer_size (get_buffer_size());
1277 /* determine if we need to stop the backend before changing parameters */
1279 if (change_driver || change_device || change_channels || change_latency ||
1280 (change_rate && !backend->can_change_sample_rate_when_running()) ||
1281 (change_bufsize && !backend->can_change_buffer_size_when_running())) {
1282 restart_required = true;
1284 restart_required = false;
1289 if (!change_driver && !change_device && !change_channels && !change_latency) {
1290 /* no changes in any parameters that absolutely require a
1291 * restart, so check those that might be changeable without a
1295 if (change_rate && !backend->can_change_sample_rate_when_running()) {
1296 /* can't do this while running ... */
1297 restart_required = true;
1300 if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1301 /* can't do this while running ... */
1302 restart_required = true;
1308 if (restart_required) {
1309 if (ARDOUR_UI::instance()->disconnect_from_engine ()) {
1316 if (change_driver && backend->set_driver (get_driver())) {
1317 error << string_compose (_("Cannot set driver to %1"), get_driver()) << endmsg;
1320 if (change_device && backend->set_device_name (get_device_name())) {
1321 error << string_compose (_("Cannot set device name to %1"), get_device_name()) << endmsg;
1324 if (change_rate && backend->set_sample_rate (get_rate())) {
1325 error << string_compose (_("Cannot set sample rate to %1"), get_rate()) << endmsg;
1328 if (change_bufsize && backend->set_buffer_size (get_buffer_size())) {
1329 error << string_compose (_("Cannot set buffer size to %1"), get_buffer_size()) << endmsg;
1333 if (change_channels || get_input_channels() == 0 || get_output_channels() == 0) {
1334 if (backend->set_input_channels (get_input_channels())) {
1335 error << string_compose (_("Cannot set input channels to %1"), get_input_channels()) << endmsg;
1338 if (backend->set_output_channels (get_output_channels())) {
1339 error << string_compose (_("Cannot set output channels to %1"), get_output_channels()) << endmsg;
1343 if (change_latency) {
1344 if (backend->set_systemic_input_latency (get_input_latency())) {
1345 error << string_compose (_("Cannot set input latency to %1"), get_input_latency()) << endmsg;
1348 if (backend->set_systemic_output_latency (get_output_latency())) {
1349 error << string_compose (_("Cannot set output latency to %1"), get_output_latency()) << endmsg;
1354 backend->set_midi_option (midi_option_combo.get_active_text());
1356 if (start || (was_running && restart_required)) {
1357 if (ARDOUR_UI::instance()->reconnect_to_engine()) {
1368 EngineControl::post_push ()
1370 /* get a pointer to the current state object, creating one if
1374 if (_have_control) {
1375 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
1378 state = save_state ();
1384 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
1385 (*i).active = false;
1388 /* mark this one active (to be used next time the dialog is
1392 state->active = true;
1394 manage_control_app_sensitivity ();
1397 /* schedule a redisplay of MIDI ports */
1399 Glib::signal_timeout().connect (sigc::bind_return (sigc::mem_fun (*this, &EngineControl::refresh_midi_display), false), 1000);
1404 EngineControl::get_rate () const
1406 float r = atof (sample_rate_combo.get_active_text ());
1407 /* the string may have been translated with an abbreviation for
1408 * thousands, so use a crude heuristic to fix this.
1418 EngineControl::get_buffer_size () const
1420 string txt = buffer_size_combo.get_active_text ();
1423 if (sscanf (txt.c_str(), "%d", &samples) != 1) {
1431 EngineControl::get_input_channels() const
1433 return (uint32_t) input_channels_adjustment.get_value();
1437 EngineControl::get_output_channels() const
1439 return (uint32_t) output_channels_adjustment.get_value();
1443 EngineControl::get_input_latency() const
1445 return (uint32_t) input_latency_adjustment.get_value();
1449 EngineControl::get_output_latency() const
1451 return (uint32_t) output_latency_adjustment.get_value();
1455 EngineControl::get_backend () const
1457 return backend_combo.get_active_text ();
1461 EngineControl::get_driver () const
1463 return driver_combo.get_active_text ();
1467 EngineControl::get_device_name () const
1469 return device_combo.get_active_text ();
1473 EngineControl::control_app_button_clicked ()
1475 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1481 backend->launch_control_app ();
1485 EngineControl::manage_control_app_sensitivity ()
1487 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1493 string appname = backend->control_app_name();
1495 if (appname.empty()) {
1496 control_app_button.set_sensitive (false);
1498 control_app_button.set_sensitive (true);
1503 EngineControl::set_desired_sample_rate (uint32_t sr)
1505 _desired_sample_rate = sr;
1510 EngineControl::on_switch_page (GtkNotebookPage*, guint page_num)
1512 if (page_num == 0) {
1513 cancel_button->set_sensitive (true);
1514 ok_button->set_sensitive (true);
1515 apply_button->set_sensitive (true);
1517 cancel_button->set_sensitive (false);
1518 ok_button->set_sensitive (false);
1519 apply_button->set_sensitive (false);
1522 if (page_num == 1) {
1524 refresh_midi_display ();
1527 if (page_num == 2) {
1530 if (!ARDOUR::AudioEngine::instance()->running()) {
1532 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
1534 /* save any existing latency values */
1536 uint32_t il = (uint32_t) input_latency.get_value ();
1537 uint32_t ol = (uint32_t) input_latency.get_value ();
1539 /* reset to zero so that our new test instance of JACK
1540 will be clean of any existing latency measures.
1543 input_latency.set_value (0);
1544 output_latency.set_value (0);
1548 input_latency.set_value (il);
1549 output_latency.set_value (ol);
1553 if (ARDOUR::AudioEngine::instance()->prepare_for_latency_measurement()) {
1554 disable_latency_tab ();
1557 enable_latency_tab ();
1560 ARDOUR::AudioEngine::instance()->stop_latency_detection();
1564 /* latency measurement */
1567 EngineControl::check_latency_measurement ()
1569 MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1571 if (mtdm->resolve () < 0) {
1572 lm_results.set_markup (string_compose ("<span foreground=\"red\">%1</span>", _("No signal detected ")));
1576 if (mtdm->err () > 0.3) {
1582 ARDOUR::framecnt_t const sample_rate = ARDOUR::AudioEngine::instance()->sample_rate();
1584 if (sample_rate == 0) {
1585 lm_results.set_text (_("Disconnected from audio engine"));
1586 ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1590 uint32_t frames_total = mtdm->del();
1591 uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1593 snprintf (buf, sizeof (buf), "%u samples %10.3lf ms", extra, extra * 1000.0f/sample_rate);
1597 if (mtdm->err () > 0.2) {
1598 strcat (buf, " ??");
1603 strcat (buf, " (Inv)");
1608 lm_measure_button.set_active (false);
1609 lm_use_button.set_sensitive (true);
1610 strcat (buf, " (set)");
1611 have_lm_results = true;
1614 lm_results.set_text (buf);
1620 EngineControl::start_latency_detection ()
1622 ARDOUR::AudioEngine::instance()->set_latency_input_port (lm_input_channel_combo.get_active_text());
1623 ARDOUR::AudioEngine::instance()->set_latency_output_port (lm_output_channel_combo.get_active_text());
1624 ARDOUR::AudioEngine::instance()->start_latency_detection ();
1625 lm_results.set_text (_("Detecting ..."));
1626 latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &EngineControl::check_latency_measurement), 250);
1627 lm_start_stop_label.set_text (_("Cancel measurement"));
1628 have_lm_results = false;
1629 lm_input_channel_combo.set_sensitive (false);
1630 lm_output_channel_combo.set_sensitive (false);
1634 EngineControl::end_latency_detection ()
1636 ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1637 latency_timeout.disconnect ();
1638 lm_start_stop_label.set_text (_("Measure latency"));
1639 if (!have_lm_results) {
1640 lm_results.set_markup ("<i>No measurement results yet</i>");
1642 lm_input_channel_combo.set_sensitive (true);
1643 lm_output_channel_combo.set_sensitive (true);
1647 EngineControl::latency_button_toggled ()
1649 if (lm_measure_button.get_active ()) {
1650 start_latency_detection ();
1652 end_latency_detection ();
1657 EngineControl::use_latency_button_clicked ()
1659 MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1665 uint32_t frames_total = mtdm->del();
1666 uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1667 uint32_t one_way = extra/2;
1669 input_latency_adjustment.set_value (one_way);
1670 output_latency_adjustment.set_value (one_way);
1674 EngineControl::on_delete_event (GdkEventAny* ev)
1676 if (notebook.get_current_page() == 2) {
1677 /* currently on latency tab - be sure to clean up */
1678 end_latency_detection ();
1680 return ArdourDialog::on_delete_event (ev);
1684 EngineControl::engine_running ()
1686 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1689 buffer_size_combo.set_active_text (bufsize_as_string (backend->buffer_size()));
1690 sample_rate_combo.set_active_text (rate_as_string (backend->sample_rate()));
1692 buffer_size_combo.set_sensitive (true);
1693 sample_rate_combo.set_sensitive (true);
1695 connect_disconnect_button.set_label (string_compose (_("Disconnect from %1"), backend->name()));
1697 started_at_least_once = true;
1701 EngineControl::engine_stopped ()
1703 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1706 buffer_size_combo.set_sensitive (false);
1707 connect_disconnect_button.set_label (string_compose (_("Connect to %1"), backend->name()));
1709 sample_rate_combo.set_sensitive (true);
1710 buffer_size_combo.set_sensitive (true);
1714 EngineControl::connect_disconnect_click()
1716 if (ARDOUR::AudioEngine::instance()->running()) {
1717 ARDOUR_UI::instance()->disconnect_from_engine ();
1719 ARDOUR_UI::instance()->reconnect_to_engine ();