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"
56 using namespace Gtkmm2ext;
60 EngineControl::EngineControl ()
61 : ArdourDialog (_("Audio/MIDI Setup"))
63 , input_latency_adjustment (0, 0, 99999, 1)
64 , input_latency (input_latency_adjustment)
65 , output_latency_adjustment (0, 0, 99999, 1)
66 , output_latency (output_latency_adjustment)
67 , input_channels_adjustment (0, 0, 256, 1)
68 , input_channels (input_channels_adjustment)
69 , output_channels_adjustment (0, 0, 256, 1)
70 , output_channels (output_channels_adjustment)
71 , ports_adjustment (128, 8, 1024, 1, 16)
72 , ports_spinner (ports_adjustment)
73 , control_app_button (_("Device Control Panel"))
74 , lm_start_stop_label (_("Measure latency"))
75 , lm_use_button (_("Use results"))
77 , have_lm_results (false)
78 , midi_refresh_button (_("Refresh list"))
79 , aj_button (_("Start MIDI ALSA/JACK bridge"))
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();
97 for (vector<const ARDOUR::AudioBackendInfo*>::const_iterator b = backends.begin(); b != backends.end(); ++b) {
98 strings.push_back ((*b)->name);
101 set_popdown_strings (backend_combo, strings);
102 backend_combo.set_active_text (strings.front());
103 backend_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::backend_changed));
105 /* setup basic packing characteristics for the table used on the main
106 * tab of the notebook
109 basic_packer.set_spacings (6);
110 basic_packer.set_border_width (12);
111 basic_packer.set_homogeneous (true);
115 basic_hbox.pack_start (basic_packer, false, false);
119 /* latency measurement tab */
121 lm_title.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Latency Measurement Tool")));
124 lm_table.set_row_spacings (12);
126 lm_table.attach (lm_title, 0, 2, row, row+1, xopt, (AttachOptions) 0);
129 Gtk::Label* preamble;
131 preamble = manage (new Label);
132 preamble->set_width_chars (60);
133 preamble->set_line_wrap (true);
134 preamble->set_markup (_("<span weight=\"bold\">Turn down the volume on your hardware to a very low level.</span>"));
136 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
139 preamble = manage (new Label);
140 preamble->set_width_chars (60);
141 preamble->set_line_wrap (true);
142 preamble->set_markup (_("Select two channels below and connect them using a cable or (less ideally) a speaker and microphone."));
144 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
147 label = manage (new Label (_("Output channel")));
148 lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
150 Gtk::Alignment* misc_align = manage (new Alignment (0.0, 0.5));
151 misc_align->add (lm_output_channel_combo);
152 lm_table.attach (*misc_align, 1, 2, row, row+1, xopt, (AttachOptions) 0);
155 label = manage (new Label (_("Input channel")));
156 lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
158 misc_align = manage (new Alignment (0.0, 0.5));
159 misc_align->add (lm_input_channel_combo);
160 lm_table.attach (*misc_align, 1, 2, row, row+1, FILL, (AttachOptions) 0);
163 xopt = AttachOptions(0);
165 lm_measure_button.add (lm_start_stop_label);
167 lm_measure_button.signal_toggled().connect (sigc::mem_fun (*this, &EngineControl::latency_button_toggled));
168 lm_use_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::use_latency_button_clicked));
169 lm_use_button.set_sensitive (false);
171 preamble = manage (new Label);
172 preamble->set_width_chars (60);
173 preamble->set_line_wrap (true);
174 preamble->set_markup (_("Once the channels are connected, click the \"Measure latency\" button."));
175 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
178 lm_table.attach (lm_measure_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
180 lm_table.attach (lm_results, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
184 preamble = manage (new Label);
185 preamble->set_width_chars (60);
186 preamble->set_line_wrap (true);
187 preamble->set_markup (_("When satisfied with the results, click the \"Use results\" button."));
188 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
191 lm_table.attach (lm_use_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
194 lm_results.set_markup ("<i>No measurement results yet</i>");
196 lm_vbox.set_border_width (12);
197 lm_vbox.pack_start (lm_table, false, false);
201 notebook.pages().push_back (TabElem (basic_vbox, _("Audio")));
202 notebook.pages().push_back (TabElem (midi_vbox, _("MIDI")));
203 notebook.pages().push_back (TabElem (lm_vbox, _("Latency")));
204 notebook.set_border_width (12);
206 notebook.set_tab_pos (POS_RIGHT);
207 notebook.show_all ();
209 notebook.set_name ("SettingsNotebook");
211 /* packup the notebook */
213 get_vbox()->set_border_width (12);
214 get_vbox()->pack_start (notebook);
216 /* need a special function to print "all available channels" when the
217 * channel counts hit zero.
220 input_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &input_channels));
221 output_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &output_channels));
223 control_app_button.signal_clicked().connect (mem_fun (*this, &EngineControl::control_app_button_clicked));
224 manage_control_app_sensitivity ();
226 cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
227 ok_button = add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
228 apply_button = add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_APPLY);
230 /* Pick up any existing audio setup configuration, if appropriate */
232 XMLNode* audio_setup = ARDOUR::Config->extra_xml ("AudioMIDISetup");
234 ARDOUR::AudioEngine::instance()->Running.connect (running_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_running, this), gui_context());
235 ARDOUR::AudioEngine::instance()->Stopped.connect (stopped_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_stopped, this), gui_context());
236 ARDOUR::AudioEngine::instance()->Halted.connect (stopped_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_stopped, this), gui_context());
241 set_state (*audio_setup);
244 /* Connect to signals */
246 driver_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::driver_changed));
247 sample_rate_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::sample_rate_changed));
248 buffer_size_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::buffer_size_changed));
249 device_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::device_changed));
251 input_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
252 output_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
253 input_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
254 output_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
256 notebook.signal_switch_page().connect (sigc::mem_fun (*this, &EngineControl::on_switch_page));
262 EngineControl::on_response (int response_id)
264 ArdourDialog::on_response (response_id);
266 switch (response_id) {
268 push_state_to_backend (true);
271 push_state_to_backend (true);
274 case RESPONSE_DELETE_EVENT: {
276 ev.type = GDK_BUTTON_PRESS;
278 on_delete_event ((GdkEventAny*) &ev);
287 EngineControl::build_notebook ()
290 AttachOptions xopt = AttachOptions (FILL|EXPAND);
292 /* clear the table */
294 Gtkmm2ext::container_clear (basic_vbox);
295 Gtkmm2ext::container_clear (basic_packer);
297 label = manage (left_aligned_label (_("Audio System:")));
298 basic_packer.attach (*label, 0, 1, 0, 1, xopt, (AttachOptions) 0);
299 basic_packer.attach (backend_combo, 1, 2, 0, 1, xopt, (AttachOptions) 0);
302 build_full_control_notebook ();
304 build_no_control_notebook ();
307 basic_vbox.pack_start (basic_hbox, false, false);
310 Gtk::HBox* hpacker = manage (new HBox);
311 hpacker->set_border_width (12);
312 hpacker->pack_start (control_app_button, false, false);
314 control_app_button.show();
315 basic_vbox.pack_start (*hpacker);
318 basic_vbox.show_all ();
322 EngineControl::build_full_control_notebook ()
324 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
327 using namespace Notebook_Helpers;
329 vector<string> strings;
330 AttachOptions xopt = AttachOptions (FILL|EXPAND);
331 int row = 1; // row zero == backend combo
333 /* start packing it up */
335 if (backend->requires_driver_selection()) {
336 label = manage (left_aligned_label (_("Driver:")));
337 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
338 basic_packer.attach (driver_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
342 label = manage (left_aligned_label (_("Device:")));
343 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
344 basic_packer.attach (device_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
347 label = manage (left_aligned_label (_("Sample rate:")));
348 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
349 basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
353 label = manage (left_aligned_label (_("Buffer size:")));
354 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
355 basic_packer.attach (buffer_size_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
356 buffer_size_duration_label.set_alignment (0.0); /* left-align */
357 basic_packer.attach (buffer_size_duration_label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
360 input_channels.set_name ("InputChannels");
361 input_channels.set_flags(Gtk::CAN_FOCUS);
362 input_channels.set_digits(0);
363 input_channels.set_wrap(false);
364 output_channels.set_editable (true);
366 label = manage (left_aligned_label (_("Input Channels:")));
367 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
368 basic_packer.attach (input_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
371 output_channels.set_name ("OutputChannels");
372 output_channels.set_flags(Gtk::CAN_FOCUS);
373 output_channels.set_digits(0);
374 output_channels.set_wrap(false);
375 output_channels.set_editable (true);
377 label = manage (left_aligned_label (_("Output Channels:")));
378 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
379 basic_packer.attach (output_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
382 input_latency.set_name ("InputLatency");
383 input_latency.set_flags(Gtk::CAN_FOCUS);
384 input_latency.set_digits(0);
385 input_latency.set_wrap(false);
386 input_latency.set_editable (true);
388 label = manage (left_aligned_label (_("Hardware input latency:")));
389 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
390 basic_packer.attach (input_latency, 1, 2, row, row+1, xopt, (AttachOptions) 0);
391 label = manage (left_aligned_label (_("samples")));
392 basic_packer.attach (*label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
395 output_latency.set_name ("OutputLatency");
396 output_latency.set_flags(Gtk::CAN_FOCUS);
397 output_latency.set_digits(0);
398 output_latency.set_wrap(false);
399 output_latency.set_editable (true);
401 label = manage (left_aligned_label (_("Hardware output latency:")));
402 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
403 basic_packer.attach (output_latency, 1, 2, row, row+1, xopt, (AttachOptions) 0);
404 label = manage (left_aligned_label (_("samples")));
405 basic_packer.attach (*label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
411 EngineControl::build_no_control_notebook ()
413 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
416 using namespace Notebook_Helpers;
418 vector<string> strings;
419 AttachOptions xopt = AttachOptions (FILL|EXPAND);
420 int row = 1; // row zero == backend combo
421 const string msg = string_compose (_("The %1 audio backend was configured and started externally.\nThis limits your control over it."), backend->name());
423 label = manage (new Label);
424 label->set_markup (string_compose ("<span weight=\"bold\" foreground=\"red\">%1</span>", msg));
425 basic_packer.attach (*label, 0, 2, row, row + 1, xopt, (AttachOptions) 0);
428 if (backend->can_change_sample_rate_when_running()) {
429 label = manage (left_aligned_label (_("Sample rate:")));
430 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
431 basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
435 if (backend->can_change_buffer_size_when_running()) {
436 label = manage (left_aligned_label (_("Buffer size:")));
437 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
438 basic_packer.attach (buffer_size_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
439 buffer_size_duration_label.set_alignment (0.0); /* left-align */
440 basic_packer.attach (buffer_size_duration_label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
444 connect_disconnect_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::connect_disconnect_click));
446 basic_packer.attach (connect_disconnect_button, 0, 2, row, row+1, FILL, AttachOptions (0));
450 EngineControl::~EngineControl ()
456 EngineControl::disable_latency_tab ()
458 vector<string> empty;
459 set_popdown_strings (lm_output_channel_combo, empty);
460 set_popdown_strings (lm_input_channel_combo, empty);
461 lm_measure_button.set_sensitive (false);
462 lm_use_button.set_sensitive (false);
466 EngineControl::enable_latency_tab ()
468 vector<string> outputs;
469 ARDOUR::AudioEngine::instance()->get_physical_outputs (ARDOUR::DataType::AUDIO, outputs);
470 set_popdown_strings (lm_output_channel_combo, outputs);
471 lm_output_channel_combo.set_active_text (outputs.front());
473 vector<string> inputs;
474 ARDOUR::AudioEngine::instance()->get_physical_inputs (ARDOUR::DataType::AUDIO, inputs);
475 set_popdown_strings (lm_input_channel_combo, inputs);
476 lm_input_channel_combo.set_active_text (inputs.front());
478 lm_measure_button.set_sensitive (true);
482 EngineControl::setup_midi_tab_for_backend ()
484 string backend = backend_combo.get_active_text ();
486 Gtkmm2ext::container_clear (midi_vbox);
488 midi_vbox.set_border_width (12);
489 midi_device_table.set_border_width (12);
491 if (backend == "JACK") {
492 setup_midi_tab_for_jack ();
495 midi_vbox.pack_start (midi_device_table, true, true);
496 midi_vbox.pack_start (midi_refresh_button, false, false);
497 midi_vbox.show_all ();
499 midi_refresh_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::refresh_midi_display));
503 EngineControl::setup_midi_tab_for_jack ()
505 midi_vbox.pack_start (aj_button, false, false);
509 EngineControl::refresh_midi_display ()
511 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
514 vector<string> midi_inputs;
515 vector<string> midi_outputs;
517 AttachOptions xopt = AttachOptions (FILL|EXPAND);
520 Gtkmm2ext::container_clear (midi_device_table);
522 backend->get_physical_inputs (ARDOUR::DataType::MIDI, midi_inputs);
523 backend->get_physical_outputs (ARDOUR::DataType::MIDI, midi_outputs);
525 midi_device_table.set_spacings (6);
526 midi_device_table.set_homogeneous (true);
527 midi_device_table.resize (midi_inputs.size() + midi_outputs.size() + 3, 1);
529 l = manage (new Label);
530 l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Inputs")));
531 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
532 l->set_alignment (0, 0.5);
536 for (vector<string>::iterator p = midi_inputs.begin(); p != midi_inputs.end(); ++p) {
537 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
538 l->set_alignment (0, 0.5);
539 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
544 row++; // extra row of spacing
546 l = manage (new Label);
547 l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Outputs")));
548 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
549 l->set_alignment (0, 0.5);
553 for (vector<string>::iterator p = midi_outputs.begin(); p != midi_outputs.end(); ++p) {
554 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
555 l->set_alignment (0, 0.5);
556 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
563 EngineControl::update_sensitivity ()
568 EngineControl::backend_changed ()
570 if (ignore_changes) {
574 string backend_name = backend_combo.get_active_text();
575 boost::shared_ptr<ARDOUR::AudioBackend> backend;
577 if (!(backend = ARDOUR::AudioEngine::instance()->set_backend (backend_name, "ardour", ""))) {
578 /* eh? setting the backend failed... how ? */
582 _have_control = ARDOUR::AudioEngine::instance()->setup_required ();
585 setup_midi_tab_for_backend ();
587 if (backend->requires_driver_selection()) {
588 vector<string> drivers = backend->enumerate_drivers();
590 if (!drivers.empty()) {
592 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
593 set_popdown_strings (driver_combo, drivers);
594 driver_combo.set_active_text (drivers.front());
601 driver_combo.set_sensitive (false);
602 /* this will change the device text which will cause a call to
603 * device changed which will set up parameters
608 maybe_display_saved_state ();
612 EngineControl::print_channel_count (Gtk::SpinButton* sb)
614 uint32_t cnt = (uint32_t) sb->get_value();
616 sb->set_text (_("all available channels"));
619 snprintf (buf, sizeof (buf), "%d", cnt);
626 EngineControl::list_devices ()
628 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
631 /* now fill out devices, mark sample rates, buffer sizes insensitive */
633 vector<ARDOUR::AudioBackend::DeviceStatus> all_devices = backend->enumerate_devices ();
635 /* NOTE: Ardour currently does not display the "available" field of the
638 * Doing so would require a different GUI widget than the combo
639 * box/popdown that we currently use, since it has no way to list
640 * items that are not selectable. Something more like a popup menu,
641 * which could have unselectable items, would be appropriate.
644 vector<string> available_devices;
646 for (vector<ARDOUR::AudioBackend::DeviceStatus>::const_iterator i = all_devices.begin(); i != all_devices.end(); ++i) {
647 available_devices.push_back (i->name);
650 if (!available_devices.empty()) {
652 update_sensitivity ();
655 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
656 set_popdown_strings (device_combo, available_devices);
657 device_combo.set_active_text (available_devices.front());
662 ok_button->set_sensitive (true);
663 apply_button->set_sensitive (true);
666 sample_rate_combo.set_sensitive (false);
667 buffer_size_combo.set_sensitive (false);
668 input_latency.set_sensitive (false);
669 output_latency.set_sensitive (false);
670 input_channels.set_sensitive (false);
671 output_channels.set_sensitive (false);
672 ok_button->set_sensitive (false);
673 apply_button->set_sensitive (false);
678 EngineControl::driver_changed ()
680 if (ignore_changes) {
684 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
687 backend->set_driver (driver_combo.get_active_text());
690 maybe_display_saved_state ();
694 EngineControl::device_changed ()
696 if (ignore_changes) {
700 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
702 string device_name = device_combo.get_active_text ();
706 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
708 /* don't allow programmatic change to combos to cause a
709 recursive call to this method.
719 sr = backend->available_sample_rates (device_name);
722 sr.push_back (8000.0f);
723 sr.push_back (16000.0f);
724 sr.push_back (32000.0f);
725 sr.push_back (44100.0f);
726 sr.push_back (48000.0f);
727 sr.push_back (88200.0f);
728 sr.push_back (96000.0f);
729 sr.push_back (192000.0f);
730 sr.push_back (384000.0f);
733 for (vector<float>::const_iterator x = sr.begin(); x != sr.end(); ++x) {
734 s.push_back (rate_as_string (*x));
735 if (*x == _desired_sample_rate) {
741 sample_rate_combo.set_sensitive (true);
742 set_popdown_strings (sample_rate_combo, s);
744 if (desired.empty()) {
745 sample_rate_combo.set_active_text (s.front());
747 sample_rate_combo.set_active_text (desired);
751 sample_rate_combo.set_sensitive (false);
759 bs = backend->available_buffer_sizes(device_name);
760 } else if (backend->can_change_buffer_size_when_running()) {
774 for (vector<uint32_t>::const_iterator x = bs.begin(); x != bs.end(); ++x) {
775 s.push_back (bufsize_as_string (*x));
779 buffer_size_combo.set_sensitive (true);
780 set_popdown_strings (buffer_size_combo, s);
782 buffer_size_combo.set_active_text (s.front());
783 show_buffer_duration ();
785 buffer_size_combo.set_sensitive (false);
788 /* XXX theoretically need to set min + max channel counts here
791 manage_control_app_sensitivity ();
794 /* pick up any saved state for this device */
796 maybe_display_saved_state ();
798 /* and push it to the backend */
800 push_state_to_backend (false);
804 EngineControl::bufsize_as_string (uint32_t sz)
806 /* Translators: "samples" is always plural here, so no
807 need for plural+singular forms.
810 snprintf (buf, sizeof (buf), _("%u samples"), sz);
815 EngineControl::sample_rate_changed ()
817 if (ignore_changes) {
821 /* reset the strings for buffer size to show the correct msec value
822 (reflecting the new sample rate).
825 show_buffer_duration ();
831 EngineControl::buffer_size_changed ()
833 if (ignore_changes) {
837 show_buffer_duration ();
842 EngineControl::show_buffer_duration ()
845 /* buffer sizes - convert from just samples to samples + msecs for
846 * the displayed string
849 string bs_text = buffer_size_combo.get_active_text ();
850 uint32_t samples = atoi (bs_text); /* will ignore trailing text */
851 uint32_t rate = get_rate();
853 /* Translators: "msecs" is ALWAYS plural here, so we do not
854 need singular form as well.
856 /* Developers: note the hard-coding of a double buffered model
857 in the (2 * samples) computation of latency. we always start
858 the audiobackend in this configuration.
861 snprintf (buf, sizeof (buf), _("(%.1f msecs)"), (2 * samples) / (rate/1000.0));
862 buffer_size_duration_label.set_text (buf);
866 EngineControl::parameter_changed ()
868 if (!ignore_changes) {
873 EngineControl::State*
874 EngineControl::get_matching_state (const string& backend,
875 const string& driver,
876 const string& device)
878 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
879 if ((*i).backend == backend &&
880 (*i).driver == driver &&
881 (*i).device == device) {
888 EngineControl::State*
889 EngineControl::get_saved_state_for_currently_displayed_backend_and_device ()
891 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
894 return get_matching_state (backend_combo.get_active_text(),
895 (backend->requires_driver_selection() ? (std::string) driver_combo.get_active_text() : string()),
896 device_combo.get_active_text());
900 return get_matching_state (backend_combo.get_active_text(),
902 device_combo.get_active_text());
905 EngineControl::State*
906 EngineControl::save_state ()
908 if (!_have_control) {
912 bool existing = true;
913 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
920 store_state (*state);
923 states.push_back (*state);
930 EngineControl::store_state (State& state)
932 state.backend = get_backend ();
933 state.driver = get_driver ();
934 state.device = get_device_name ();
935 state.sample_rate = get_rate ();
936 state.buffer_size = get_buffer_size ();
937 state.input_latency = get_input_latency ();
938 state.output_latency = get_output_latency ();
939 state.input_channels = get_input_channels ();
940 state.output_channels = get_output_channels ();
944 EngineControl::maybe_display_saved_state ()
946 if (!_have_control) {
950 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
953 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
955 if (!_desired_sample_rate) {
956 sample_rate_combo.set_active_text (rate_as_string (state->sample_rate));
958 buffer_size_combo.set_active_text (bufsize_as_string (state->buffer_size));
959 /* call this explicitly because we're ignoring changes to
960 the controls at this point.
962 show_buffer_duration ();
963 input_latency.set_value (state->input_latency);
964 output_latency.set_value (state->output_latency);
969 EngineControl::get_state ()
971 XMLNode* root = new XMLNode ("AudioMIDISetup");
974 if (!states.empty()) {
975 XMLNode* state_nodes = new XMLNode ("EngineStates");
977 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
979 XMLNode* node = new XMLNode ("State");
981 node->add_property ("backend", (*i).backend);
982 node->add_property ("driver", (*i).driver);
983 node->add_property ("device", (*i).device);
984 node->add_property ("sample-rate", (*i).sample_rate);
985 node->add_property ("buffer-size", (*i).buffer_size);
986 node->add_property ("input-latency", (*i).input_latency);
987 node->add_property ("output-latency", (*i).output_latency);
988 node->add_property ("input-channels", (*i).input_channels);
989 node->add_property ("output-channels", (*i).output_channels);
990 node->add_property ("active", (*i).active ? "yes" : "no");
992 state_nodes->add_child_nocopy (*node);
995 root->add_child_nocopy (*state_nodes);
1002 EngineControl::set_state (const XMLNode& root)
1004 XMLNodeList clist, cclist;
1005 XMLNodeConstIterator citer, cciter;
1007 XMLNode* grandchild;
1008 XMLProperty* prop = NULL;
1010 if (root.name() != "AudioMIDISetup") {
1014 clist = root.children();
1018 for (citer = clist.begin(); citer != clist.end(); ++citer) {
1022 if (child->name() != "EngineStates") {
1026 cclist = child->children();
1028 for (cciter = cclist.begin(); cciter != cclist.end(); ++cciter) {
1031 grandchild = *cciter;
1033 if (grandchild->name() != "State") {
1037 if ((prop = grandchild->property ("backend")) == 0) {
1040 state.backend = prop->value ();
1042 if ((prop = grandchild->property ("driver")) == 0) {
1045 state.driver = prop->value ();
1047 if ((prop = grandchild->property ("device")) == 0) {
1050 state.device = prop->value ();
1052 if ((prop = grandchild->property ("sample-rate")) == 0) {
1055 state.sample_rate = atof (prop->value ());
1057 if ((prop = grandchild->property ("buffer-size")) == 0) {
1060 state.buffer_size = atoi (prop->value ());
1062 if ((prop = grandchild->property ("input-latency")) == 0) {
1065 state.input_latency = atoi (prop->value ());
1067 if ((prop = grandchild->property ("output-latency")) == 0) {
1070 state.output_latency = atoi (prop->value ());
1072 if ((prop = grandchild->property ("input-channels")) == 0) {
1075 state.input_channels = atoi (prop->value ());
1077 if ((prop = grandchild->property ("output-channels")) == 0) {
1080 state.output_channels = atoi (prop->value ());
1082 if ((prop = grandchild->property ("active")) == 0) {
1085 state.active = string_is_affirmative (prop->value ());
1087 states.push_back (state);
1091 /* now see if there was an active state and switch the setup to it */
1093 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
1097 backend_combo.set_active_text ((*i).backend);
1098 driver_combo.set_active_text ((*i).driver);
1099 device_combo.set_active_text ((*i).device);
1100 sample_rate_combo.set_active_text (rate_as_string ((*i).sample_rate));
1101 buffer_size_combo.set_active_text (bufsize_as_string ((*i).buffer_size));
1102 input_latency.set_value ((*i).input_latency);
1103 output_latency.set_value ((*i).output_latency);
1112 EngineControl::push_state_to_backend (bool start)
1118 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1124 /* figure out what is going to change */
1126 bool restart_required = false;
1127 bool was_running = ARDOUR::AudioEngine::instance()->running();
1128 bool change_driver = false;
1129 bool change_device = false;
1130 bool change_rate = false;
1131 bool change_bufsize = false;
1132 bool change_latency = false;
1133 bool change_channels = false;
1135 uint32_t ochan = get_output_channels ();
1136 uint32_t ichan = get_input_channels ();
1138 if (_have_control) {
1140 if (started_at_least_once) {
1142 /* we can control the backend */
1144 if (backend->requires_driver_selection()) {
1145 if (get_driver() != backend->driver_name()) {
1146 change_driver = true;
1150 if (get_device_name() != backend->device_name()) {
1151 change_device = true;
1154 if (get_rate() != backend->sample_rate()) {
1158 if (get_buffer_size() != backend->buffer_size()) {
1159 change_bufsize = true;
1162 /* zero-requested channels means "all available" */
1165 ichan = backend->input_channels();
1169 ochan = backend->output_channels();
1172 if (ichan != backend->input_channels()) {
1173 change_channels = true;
1176 if (ochan != backend->output_channels()) {
1177 change_channels = true;
1180 if (get_input_latency() != backend->systemic_input_latency() ||
1181 get_output_latency() != backend->systemic_output_latency()) {
1182 change_latency = true;
1185 /* backend never started, so we have to force a group
1188 change_driver = true;
1189 change_device = true;
1191 change_bufsize = true;
1192 change_channels = true;
1193 change_latency = true;
1198 /* we have no control over the backend, meaning that we can
1199 * only possibly change sample rate and buffer size.
1203 if (get_rate() != backend->sample_rate()) {
1204 change_bufsize = true;
1207 if (get_buffer_size() != backend->buffer_size()) {
1208 change_bufsize = true;
1212 if (!_have_control) {
1214 /* We do not have control over the backend, so the best we can
1215 * do is try to change the sample rate and/or bufsize and get
1219 if (change_rate && !backend->can_change_sample_rate_when_running()) {
1223 if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1228 backend->set_sample_rate (get_rate());
1231 if (change_bufsize) {
1232 backend->set_buffer_size (get_buffer_size());
1240 /* determine if we need to stop the backend before changing parameters */
1242 if (change_driver || change_device || change_channels || change_latency ||
1243 (change_rate && !backend->can_change_sample_rate_when_running()) ||
1244 (change_bufsize && !backend->can_change_buffer_size_when_running())) {
1245 restart_required = true;
1247 restart_required = false;
1252 if (!change_driver && !change_device && !change_channels && !change_latency) {
1253 /* no changes in any parameters that absolutely require a
1254 * restart, so check those that might be changeable without a
1258 if (change_rate && !backend->can_change_sample_rate_when_running()) {
1259 /* can't do this while running ... */
1260 restart_required = true;
1263 if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1264 /* can't do this while running ... */
1265 restart_required = true;
1271 if (restart_required) {
1272 if (ARDOUR_UI::instance()->disconnect_from_engine ()) {
1279 if (change_driver && backend->set_driver (get_driver())) {
1280 error << string_compose (_("Cannot set driver to %1"), get_driver()) << endmsg;
1283 if (change_device && backend->set_device_name (get_device_name())) {
1284 error << string_compose (_("Cannot set device name to %1"), get_device_name()) << endmsg;
1287 if (change_rate && backend->set_sample_rate (get_rate())) {
1288 error << string_compose (_("Cannot set sample rate to %1"), get_rate()) << endmsg;
1291 if (change_bufsize && backend->set_buffer_size (get_buffer_size())) {
1292 error << string_compose (_("Cannot set buffer size to %1"), get_buffer_size()) << endmsg;
1296 if (change_channels || get_input_channels() == 0 || get_output_channels() == 0) {
1297 if (backend->set_input_channels (get_input_channels())) {
1298 error << string_compose (_("Cannot set input channels to %1"), get_input_channels()) << endmsg;
1301 if (backend->set_output_channels (get_output_channels())) {
1302 error << string_compose (_("Cannot set output channels to %1"), get_output_channels()) << endmsg;
1306 if (change_latency) {
1307 if (backend->set_systemic_input_latency (get_input_latency())) {
1308 error << string_compose (_("Cannot set input latency to %1"), get_input_latency()) << endmsg;
1311 if (backend->set_systemic_output_latency (get_output_latency())) {
1312 error << string_compose (_("Cannot set output latency to %1"), get_output_latency()) << endmsg;
1317 if (start || (was_running && restart_required)) {
1318 if (ARDOUR_UI::instance()->reconnect_to_engine()) {
1329 EngineControl::post_push ()
1331 /* get a pointer to the current state object, creating one if
1335 if (_have_control) {
1336 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
1339 state = save_state ();
1345 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
1346 (*i).active = false;
1349 /* mark this one active (to be used next time the dialog is
1353 state->active = true;
1355 manage_control_app_sensitivity ();
1358 /* schedule a redisplay of MIDI ports */
1360 Glib::signal_timeout().connect (sigc::bind_return (sigc::mem_fun (*this, &EngineControl::refresh_midi_display), false), 1000);
1365 EngineControl::get_rate () const
1367 float r = atof (sample_rate_combo.get_active_text ());
1368 /* the string may have been translated with an abbreviation for
1369 * thousands, so use a crude heuristic to fix this.
1379 EngineControl::get_buffer_size () const
1381 string txt = buffer_size_combo.get_active_text ();
1384 if (sscanf (txt.c_str(), "%d", &samples) != 1) {
1392 EngineControl::get_input_channels() const
1394 return (uint32_t) input_channels_adjustment.get_value();
1398 EngineControl::get_output_channels() const
1400 return (uint32_t) output_channels_adjustment.get_value();
1404 EngineControl::get_input_latency() const
1406 return (uint32_t) input_latency_adjustment.get_value();
1410 EngineControl::get_output_latency() const
1412 return (uint32_t) output_latency_adjustment.get_value();
1416 EngineControl::get_backend () const
1418 return backend_combo.get_active_text ();
1422 EngineControl::get_driver () const
1424 return driver_combo.get_active_text ();
1428 EngineControl::get_device_name () const
1430 return device_combo.get_active_text ();
1434 EngineControl::control_app_button_clicked ()
1436 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1442 backend->launch_control_app ();
1446 EngineControl::manage_control_app_sensitivity ()
1448 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1454 string appname = backend->control_app_name();
1456 if (appname.empty()) {
1457 control_app_button.set_sensitive (false);
1459 control_app_button.set_sensitive (true);
1464 EngineControl::set_desired_sample_rate (uint32_t sr)
1466 _desired_sample_rate = sr;
1471 EngineControl::on_switch_page (GtkNotebookPage*, guint page_num)
1473 if (page_num == 0) {
1474 cancel_button->set_sensitive (true);
1475 ok_button->set_sensitive (true);
1476 apply_button->set_sensitive (true);
1478 cancel_button->set_sensitive (false);
1479 ok_button->set_sensitive (false);
1480 apply_button->set_sensitive (false);
1483 if (page_num == 1) {
1485 refresh_midi_display ();
1488 if (page_num == 2) {
1491 if (!ARDOUR::AudioEngine::instance()->running()) {
1493 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
1495 /* save any existing latency values */
1497 uint32_t il = (uint32_t) input_latency.get_value ();
1498 uint32_t ol = (uint32_t) input_latency.get_value ();
1500 /* reset to zero so that our new test instance of JACK
1501 will be clean of any existing latency measures.
1504 input_latency.set_value (0);
1505 output_latency.set_value (0);
1509 input_latency.set_value (il);
1510 output_latency.set_value (ol);
1514 if (ARDOUR::AudioEngine::instance()->prepare_for_latency_measurement()) {
1515 disable_latency_tab ();
1518 enable_latency_tab ();
1521 ARDOUR::AudioEngine::instance()->stop_latency_detection();
1525 /* latency measurement */
1528 EngineControl::check_latency_measurement ()
1530 MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1532 if (mtdm->resolve () < 0) {
1533 lm_results.set_markup (string_compose ("<span foreground=\"red\">%1</span>", _("No signal detected ")));
1537 if (mtdm->err () > 0.3) {
1543 ARDOUR::framecnt_t const sample_rate = ARDOUR::AudioEngine::instance()->sample_rate();
1545 if (sample_rate == 0) {
1546 lm_results.set_text (_("Disconnected from audio engine"));
1547 ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1551 uint32_t frames_total = mtdm->del();
1552 uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1554 snprintf (buf, sizeof (buf), "%u samples %10.3lf ms", extra, extra * 1000.0f/sample_rate);
1558 if (mtdm->err () > 0.2) {
1559 strcat (buf, " ??");
1564 strcat (buf, " (Inv)");
1569 lm_measure_button.set_active (false);
1570 lm_use_button.set_sensitive (true);
1571 strcat (buf, " (set)");
1572 have_lm_results = true;
1575 lm_results.set_text (buf);
1581 EngineControl::start_latency_detection ()
1583 ARDOUR::AudioEngine::instance()->set_latency_input_port (lm_input_channel_combo.get_active_text());
1584 ARDOUR::AudioEngine::instance()->set_latency_output_port (lm_output_channel_combo.get_active_text());
1585 ARDOUR::AudioEngine::instance()->start_latency_detection ();
1586 lm_results.set_text (_("Detecting ..."));
1587 latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &EngineControl::check_latency_measurement), 250);
1588 lm_start_stop_label.set_text (_("Cancel measurement"));
1589 have_lm_results = false;
1590 lm_input_channel_combo.set_sensitive (false);
1591 lm_output_channel_combo.set_sensitive (false);
1595 EngineControl::end_latency_detection ()
1597 ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1598 latency_timeout.disconnect ();
1599 lm_start_stop_label.set_text (_("Measure latency"));
1600 if (!have_lm_results) {
1601 lm_results.set_markup ("<i>No measurement results yet</i>");
1603 lm_input_channel_combo.set_sensitive (true);
1604 lm_output_channel_combo.set_sensitive (true);
1608 EngineControl::latency_button_toggled ()
1610 if (lm_measure_button.get_active ()) {
1611 start_latency_detection ();
1613 end_latency_detection ();
1618 EngineControl::use_latency_button_clicked ()
1620 MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1626 uint32_t frames_total = mtdm->del();
1627 uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1628 uint32_t one_way = extra/2;
1630 input_latency_adjustment.set_value (one_way);
1631 output_latency_adjustment.set_value (one_way);
1635 EngineControl::on_delete_event (GdkEventAny* ev)
1637 if (notebook.get_current_page() == 2) {
1638 /* currently on latency tab - be sure to clean up */
1639 end_latency_detection ();
1641 return ArdourDialog::on_delete_event (ev);
1645 EngineControl::engine_running ()
1647 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1650 buffer_size_combo.set_active_text (bufsize_as_string (backend->buffer_size()));
1651 sample_rate_combo.set_active_text (rate_as_string (backend->sample_rate()));
1653 buffer_size_combo.set_sensitive (true);
1654 sample_rate_combo.set_sensitive (true);
1656 connect_disconnect_button.set_label (string_compose (_("Disconnect from %1"), backend->name()));
1658 started_at_least_once = true;
1662 EngineControl::engine_stopped ()
1664 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1667 buffer_size_combo.set_sensitive (false);
1668 connect_disconnect_button.set_label (string_compose (_("Connect to %1"), backend->name()));
1670 sample_rate_combo.set_sensitive (true);
1671 buffer_size_combo.set_sensitive (true);
1675 EngineControl::connect_disconnect_click()
1677 if (ARDOUR::AudioEngine::instance()->running()) {
1678 ARDOUR_UI::instance()->disconnect_from_engine ();
1680 ARDOUR_UI::instance()->reconnect_to_engine ();