2 Copyright (C) 2010 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <boost/scoped_ptr.hpp>
28 #include <gtkmm/messagedialog.h>
30 #include "pbd/error.h"
31 #include "pbd/xml++.h"
32 #include "pbd/unwind.h"
34 #include <gtkmm/alignment.h>
35 #include <gtkmm/stock.h>
36 #include <gtkmm/notebook.h>
37 #include <gtkmm2ext/utils.h>
39 #include "ardour/audio_backend.h"
40 #include "ardour/audioengine.h"
41 #include "ardour/mtdm.h"
42 #include "ardour/rc_configuration.h"
43 #include "ardour/types.h"
45 #include "pbd/convert.h"
46 #include "pbd/error.h"
48 #include "ardour_ui.h"
49 #include "engine_dialog.h"
50 #include "gui_thread.h"
55 using namespace Gtkmm2ext;
59 EngineControl::EngineControl ()
60 : ArdourDialog (_("Audio/MIDI Setup"))
62 , input_latency_adjustment (0, 0, 99999, 1)
63 , input_latency (input_latency_adjustment)
64 , output_latency_adjustment (0, 0, 99999, 1)
65 , output_latency (output_latency_adjustment)
66 , input_channels_adjustment (0, 0, 256, 1)
67 , input_channels (input_channels_adjustment)
68 , output_channels_adjustment (0, 0, 256, 1)
69 , output_channels (output_channels_adjustment)
70 , ports_adjustment (128, 8, 1024, 1, 16)
71 , ports_spinner (ports_adjustment)
72 , control_app_button (_("Device Control Panel"))
73 , lm_start_stop_label (_("Measure latency"))
74 , lm_use_button (_("Use results"))
76 , have_lm_results (false)
77 , midi_refresh_button (_("Refresh list"))
78 , aj_button (_("Start MIDI ALSA/JACK bridge"))
80 , _desired_sample_rate (0)
83 using namespace Notebook_Helpers;
84 vector<string> strings;
86 AttachOptions xopt = AttachOptions (FILL|EXPAND);
89 set_name (X_("AudioMIDISetup"));
91 /* the backend combo is the one thing that is ALWAYS visible
94 vector<const ARDOUR::AudioBackendInfo*> backends = ARDOUR::AudioEngine::instance()->available_backends();
95 for (vector<const ARDOUR::AudioBackendInfo*>::const_iterator b = backends.begin(); b != backends.end(); ++b) {
96 strings.push_back ((*b)->name);
99 set_popdown_strings (backend_combo, strings);
100 backend_combo.set_active_text (strings.front());
101 backend_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::backend_changed));
103 /* setup basic packing characteristics for the table used on the main
104 * tab of the notebook
107 basic_packer.set_spacings (6);
108 basic_packer.set_border_width (12);
109 basic_packer.set_homogeneous (true);
113 basic_hbox.pack_start (basic_packer, false, false);
117 /* latency measurement tab */
119 lm_title.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Latency Measurement Tool")));
122 lm_table.set_row_spacings (12);
124 lm_table.attach (lm_title, 0, 2, row, row+1, xopt, (AttachOptions) 0);
127 Gtk::Label* preamble;
129 preamble = manage (new Label);
130 preamble->set_width_chars (60);
131 preamble->set_line_wrap (true);
132 preamble->set_markup (_("<span weight=\"bold\">Turn down the volume on your hardware to a very low level.</span>"));
134 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
137 preamble = manage (new Label);
138 preamble->set_width_chars (60);
139 preamble->set_line_wrap (true);
140 preamble->set_markup (_("Select two channels below and connect them using a cable or (less ideally) a speaker and microphone."));
142 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
145 label = manage (new Label (_("Output channel")));
146 lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
148 Gtk::Alignment* misc_align = manage (new Alignment (0.0, 0.5));
149 misc_align->add (lm_output_channel_combo);
150 lm_table.attach (*misc_align, 1, 2, row, row+1, xopt, (AttachOptions) 0);
153 label = manage (new Label (_("Input channel")));
154 lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
156 misc_align = manage (new Alignment (0.0, 0.5));
157 misc_align->add (lm_input_channel_combo);
158 lm_table.attach (*misc_align, 1, 2, row, row+1, FILL, (AttachOptions) 0);
161 xopt = AttachOptions(0);
163 lm_measure_button.add (lm_start_stop_label);
165 lm_measure_button.signal_toggled().connect (sigc::mem_fun (*this, &EngineControl::latency_button_toggled));
166 lm_use_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::use_latency_button_clicked));
167 lm_use_button.set_sensitive (false);
169 preamble = manage (new Label);
170 preamble->set_width_chars (60);
171 preamble->set_line_wrap (true);
172 preamble->set_markup (_("Once the channels are connected, click the \"Measure latency\" button."));
173 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
176 lm_table.attach (lm_measure_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
178 lm_table.attach (lm_results, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
182 preamble = manage (new Label);
183 preamble->set_width_chars (60);
184 preamble->set_line_wrap (true);
185 preamble->set_markup (_("When satisfied with the results, click the \"Use results\" button."));
186 lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
189 lm_table.attach (lm_use_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
192 lm_results.set_markup ("<i>No measurement results yet</i>");
194 lm_vbox.set_border_width (12);
195 lm_vbox.pack_start (lm_table, false, false);
199 notebook.pages().push_back (TabElem (basic_vbox, _("Audio")));
200 notebook.pages().push_back (TabElem (midi_vbox, _("MIDI")));
201 notebook.pages().push_back (TabElem (lm_vbox, _("Latency")));
202 notebook.set_border_width (12);
204 notebook.set_tab_pos (POS_RIGHT);
205 notebook.show_all ();
207 notebook.set_name ("SettingsNotebook");
209 /* packup the notebook */
211 get_vbox()->set_border_width (12);
212 get_vbox()->pack_start (notebook);
214 /* need a special function to print "all available channels" when the
215 * channel counts hit zero.
218 input_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &input_channels));
219 output_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &output_channels));
221 control_app_button.signal_clicked().connect (mem_fun (*this, &EngineControl::control_app_button_clicked));
222 manage_control_app_sensitivity ();
224 cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
225 ok_button = add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
226 apply_button = add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_APPLY);
228 /* Pick up any existing audio setup configuration, if appropriate */
230 XMLNode* audio_setup = ARDOUR::Config->extra_xml ("AudioMIDISetup");
232 ARDOUR::AudioEngine::instance()->Running.connect (running_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_running, this), gui_context());
233 ARDOUR::AudioEngine::instance()->Stopped.connect (stopped_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_stopped, this), gui_context());
234 ARDOUR::AudioEngine::instance()->Halted.connect (stopped_connection, MISSING_INVALIDATOR, boost::bind (&EngineControl::engine_stopped, this), gui_context());
239 set_state (*audio_setup);
242 /* Connect to signals */
244 driver_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::driver_changed));
245 sample_rate_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::sample_rate_changed));
246 buffer_size_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::buffer_size_changed));
247 device_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::device_changed));
249 input_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
250 output_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
251 input_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
252 output_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
254 notebook.signal_switch_page().connect (sigc::mem_fun (*this, &EngineControl::on_switch_page));
260 EngineControl::on_response (int response_id)
262 ArdourDialog::on_response (response_id);
264 switch (response_id) {
266 push_state_to_backend (true);
269 push_state_to_backend (true);
272 case RESPONSE_DELETE_EVENT: {
274 ev.type = GDK_BUTTON_PRESS;
276 on_delete_event ((GdkEventAny*) &ev);
285 EngineControl::build_notebook ()
288 AttachOptions xopt = AttachOptions (FILL|EXPAND);
290 /* clear the table */
292 Gtkmm2ext::container_clear (basic_vbox);
293 Gtkmm2ext::container_clear (basic_packer);
295 label = manage (left_aligned_label (_("Audio System:")));
296 basic_packer.attach (*label, 0, 1, 0, 1, xopt, (AttachOptions) 0);
297 basic_packer.attach (backend_combo, 1, 2, 0, 1, xopt, (AttachOptions) 0);
300 build_full_control_notebook ();
302 build_no_control_notebook ();
305 basic_vbox.pack_start (basic_hbox, false, false);
308 Gtk::HBox* hpacker = manage (new HBox);
309 hpacker->set_border_width (12);
310 hpacker->pack_start (control_app_button, false, false);
312 control_app_button.show();
313 basic_vbox.pack_start (*hpacker);
316 basic_vbox.show_all ();
320 EngineControl::build_full_control_notebook ()
322 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
325 using namespace Notebook_Helpers;
327 vector<string> strings;
328 AttachOptions xopt = AttachOptions (FILL|EXPAND);
329 int row = 1; // row zero == backend combo
331 /* start packing it up */
333 if (backend->requires_driver_selection()) {
334 label = manage (left_aligned_label (_("Driver:")));
335 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
336 basic_packer.attach (driver_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
340 label = manage (left_aligned_label (_("Device:")));
341 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
342 basic_packer.attach (device_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
345 label = manage (left_aligned_label (_("Sample rate:")));
346 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
347 basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
351 label = manage (left_aligned_label (_("Buffer size:")));
352 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
353 basic_packer.attach (buffer_size_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
354 buffer_size_duration_label.set_alignment (0.0); /* left-align */
355 basic_packer.attach (buffer_size_duration_label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
358 input_channels.set_name ("InputChannels");
359 input_channels.set_flags(Gtk::CAN_FOCUS);
360 input_channels.set_digits(0);
361 input_channels.set_wrap(false);
362 output_channels.set_editable (true);
364 label = manage (left_aligned_label (_("Input Channels:")));
365 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
366 basic_packer.attach (input_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
369 output_channels.set_name ("OutputChannels");
370 output_channels.set_flags(Gtk::CAN_FOCUS);
371 output_channels.set_digits(0);
372 output_channels.set_wrap(false);
373 output_channels.set_editable (true);
375 label = manage (left_aligned_label (_("Output Channels:")));
376 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
377 basic_packer.attach (output_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
380 input_latency.set_name ("InputLatency");
381 input_latency.set_flags(Gtk::CAN_FOCUS);
382 input_latency.set_digits(0);
383 input_latency.set_wrap(false);
384 input_latency.set_editable (true);
386 label = manage (left_aligned_label (_("Hardware input latency:")));
387 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
388 basic_packer.attach (input_latency, 1, 2, row, row+1, xopt, (AttachOptions) 0);
389 label = manage (left_aligned_label (_("samples")));
390 basic_packer.attach (*label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
393 output_latency.set_name ("OutputLatency");
394 output_latency.set_flags(Gtk::CAN_FOCUS);
395 output_latency.set_digits(0);
396 output_latency.set_wrap(false);
397 output_latency.set_editable (true);
399 label = manage (left_aligned_label (_("Hardware output latency:")));
400 basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
401 basic_packer.attach (output_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);
409 EngineControl::build_no_control_notebook ()
411 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
414 using namespace Notebook_Helpers;
416 vector<string> strings;
417 AttachOptions xopt = AttachOptions (FILL|EXPAND);
418 int row = 1; // row zero == backend combo
419 const string msg = string_compose (_("The %1 audio backend was configured and started externally.\nThis limits your control over it."), backend->name());
421 label = manage (new Label);
422 label->set_markup (string_compose ("<span weight=\"bold\" foreground=\"red\">%1</span>", msg));
423 basic_packer.attach (*label, 0, 2, row, row + 1, xopt, (AttachOptions) 0);
426 if (backend->can_change_sample_rate_when_running()) {
427 label = manage (left_aligned_label (_("Sample rate:")));
428 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
429 basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
433 if (backend->can_change_buffer_size_when_running()) {
434 label = manage (left_aligned_label (_("Buffer size:")));
435 basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
436 basic_packer.attach (buffer_size_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
437 buffer_size_duration_label.set_alignment (0.0); /* left-align */
438 basic_packer.attach (buffer_size_duration_label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
442 connect_disconnect_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::connect_disconnect_click));
444 basic_packer.attach (connect_disconnect_button, 0, 2, row, row+1, FILL, AttachOptions (0));
448 EngineControl::~EngineControl ()
454 EngineControl::disable_latency_tab ()
456 vector<string> empty;
457 set_popdown_strings (lm_output_channel_combo, empty);
458 set_popdown_strings (lm_input_channel_combo, empty);
459 lm_measure_button.set_sensitive (false);
460 lm_use_button.set_sensitive (false);
464 EngineControl::enable_latency_tab ()
466 vector<string> outputs;
467 ARDOUR::AudioEngine::instance()->get_physical_outputs (ARDOUR::DataType::AUDIO, outputs);
468 set_popdown_strings (lm_output_channel_combo, outputs);
469 lm_output_channel_combo.set_active_text (outputs.front());
471 vector<string> inputs;
472 ARDOUR::AudioEngine::instance()->get_physical_inputs (ARDOUR::DataType::AUDIO, inputs);
473 set_popdown_strings (lm_input_channel_combo, inputs);
474 lm_input_channel_combo.set_active_text (inputs.front());
476 lm_measure_button.set_sensitive (true);
480 EngineControl::setup_midi_tab_for_backend ()
482 string backend = backend_combo.get_active_text ();
484 Gtkmm2ext::container_clear (midi_vbox);
486 midi_vbox.set_border_width (12);
487 midi_device_table.set_border_width (12);
489 if (backend == "JACK") {
490 setup_midi_tab_for_jack ();
493 midi_vbox.pack_start (midi_device_table, true, true);
494 midi_vbox.pack_start (midi_refresh_button, false, false);
495 midi_vbox.show_all ();
497 midi_refresh_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::refresh_midi_display));
501 EngineControl::setup_midi_tab_for_jack ()
503 midi_vbox.pack_start (aj_button, false, false);
507 EngineControl::refresh_midi_display ()
509 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
512 vector<string> midi_inputs;
513 vector<string> midi_outputs;
515 AttachOptions xopt = AttachOptions (FILL|EXPAND);
518 Gtkmm2ext::container_clear (midi_device_table);
520 backend->get_physical_inputs (ARDOUR::DataType::MIDI, midi_inputs);
521 backend->get_physical_outputs (ARDOUR::DataType::MIDI, midi_outputs);
523 midi_device_table.set_spacings (6);
524 midi_device_table.set_homogeneous (true);
525 midi_device_table.resize (midi_inputs.size() + midi_outputs.size() + 3, 1);
527 l = manage (new Label);
528 l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Inputs")));
529 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
530 l->set_alignment (0, 0.5);
534 for (vector<string>::iterator p = midi_inputs.begin(); p != midi_inputs.end(); ++p) {
535 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
536 l->set_alignment (0, 0.5);
537 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
542 row++; // extra row of spacing
544 l = manage (new Label);
545 l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Outputs")));
546 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
547 l->set_alignment (0, 0.5);
551 for (vector<string>::iterator p = midi_outputs.begin(); p != midi_outputs.end(); ++p) {
552 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
553 l->set_alignment (0, 0.5);
554 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
561 EngineControl::update_sensitivity ()
566 EngineControl::backend_changed ()
568 if (ignore_changes) {
572 string backend_name = backend_combo.get_active_text();
573 boost::shared_ptr<ARDOUR::AudioBackend> backend;
575 if (!(backend = ARDOUR::AudioEngine::instance()->set_backend (backend_name, "ardour", ""))) {
576 /* eh? setting the backend failed... how ? */
580 _have_control = ARDOUR::AudioEngine::instance()->setup_required ();
583 setup_midi_tab_for_backend ();
585 if (backend->requires_driver_selection()) {
586 vector<string> drivers = backend->enumerate_drivers();
588 if (!drivers.empty()) {
590 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
591 set_popdown_strings (driver_combo, drivers);
592 driver_combo.set_active_text (drivers.front());
599 driver_combo.set_sensitive (false);
600 /* this will change the device text which will cause a call to
601 * device changed which will set up parameters
606 maybe_display_saved_state ();
610 EngineControl::print_channel_count (Gtk::SpinButton* sb)
612 uint32_t cnt = (uint32_t) sb->get_value();
614 sb->set_text (_("all available channels"));
617 snprintf (buf, sizeof (buf), "%d", cnt);
624 EngineControl::list_devices ()
626 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
629 /* now fill out devices, mark sample rates, buffer sizes insensitive */
631 vector<ARDOUR::AudioBackend::DeviceStatus> all_devices = backend->enumerate_devices ();
633 /* NOTE: Ardour currently does not display the "available" field of the
636 * Doing so would require a different GUI widget than the combo
637 * box/popdown that we currently use, since it has no way to list
638 * items that are not selectable. Something more like a popup menu,
639 * which could have unselectable items, would be appropriate.
642 vector<string> available_devices;
644 for (vector<ARDOUR::AudioBackend::DeviceStatus>::const_iterator i = all_devices.begin(); i != all_devices.end(); ++i) {
645 available_devices.push_back (i->name);
648 if (!available_devices.empty()) {
650 update_sensitivity ();
653 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
654 set_popdown_strings (device_combo, available_devices);
655 device_combo.set_active_text (available_devices.front());
660 ok_button->set_sensitive (true);
661 apply_button->set_sensitive (true);
664 sample_rate_combo.set_sensitive (false);
665 buffer_size_combo.set_sensitive (false);
666 input_latency.set_sensitive (false);
667 output_latency.set_sensitive (false);
668 input_channels.set_sensitive (false);
669 output_channels.set_sensitive (false);
670 ok_button->set_sensitive (false);
671 apply_button->set_sensitive (false);
676 EngineControl::driver_changed ()
678 if (ignore_changes) {
682 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
685 backend->set_driver (driver_combo.get_active_text());
688 maybe_display_saved_state ();
692 EngineControl::device_changed ()
694 if (ignore_changes) {
698 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
700 string device_name = device_combo.get_active_text ();
704 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
706 /* don't allow programmatic change to combos to cause a
707 recursive call to this method.
717 sr = backend->available_sample_rates (device_name);
720 sr.push_back (8000.0f);
721 sr.push_back (16000.0f);
722 sr.push_back (32000.0f);
723 sr.push_back (44100.0f);
724 sr.push_back (48000.0f);
725 sr.push_back (88200.0f);
726 sr.push_back (96000.0f);
727 sr.push_back (192000.0f);
728 sr.push_back (384000.0f);
731 for (vector<float>::const_iterator x = sr.begin(); x != sr.end(); ++x) {
732 s.push_back (rate_as_string (*x));
733 if (*x == _desired_sample_rate) {
739 sample_rate_combo.set_sensitive (true);
740 set_popdown_strings (sample_rate_combo, s);
742 if (desired.empty()) {
743 sample_rate_combo.set_active_text (s.front());
745 sample_rate_combo.set_active_text (desired);
749 sample_rate_combo.set_sensitive (false);
757 bs = backend->available_buffer_sizes(device_name);
758 } else if (backend->can_change_buffer_size_when_running()) {
772 for (vector<uint32_t>::const_iterator x = bs.begin(); x != bs.end(); ++x) {
773 s.push_back (bufsize_as_string (*x));
777 buffer_size_combo.set_sensitive (true);
778 set_popdown_strings (buffer_size_combo, s);
780 buffer_size_combo.set_active_text (s.front());
781 show_buffer_duration ();
783 buffer_size_combo.set_sensitive (false);
786 /* XXX theoretically need to set min + max channel counts here
789 manage_control_app_sensitivity ();
792 /* pick up any saved state for this device */
794 maybe_display_saved_state ();
796 /* and push it to the backend */
798 push_state_to_backend (false);
802 EngineControl::rate_as_string (float r)
805 if (fmod (r, 1000.0f)) {
806 snprintf (buf, sizeof (buf), "%.1f kHz", r/1000.0);
808 snprintf (buf, sizeof (buf), "%.0f kHz", r/1000.0);
814 EngineControl::bufsize_as_string (uint32_t sz)
816 /* Translators: "samples" is always plural here, so no
817 need for plural+singular forms.
820 snprintf (buf, sizeof (buf), _("%u samples"), sz);
825 EngineControl::sample_rate_changed ()
827 if (ignore_changes) {
831 /* reset the strings for buffer size to show the correct msec value
832 (reflecting the new sample rate).
835 show_buffer_duration ();
841 EngineControl::buffer_size_changed ()
843 if (ignore_changes) {
847 show_buffer_duration ();
852 EngineControl::show_buffer_duration ()
855 /* buffer sizes - convert from just samples to samples + msecs for
856 * the displayed string
859 string bs_text = buffer_size_combo.get_active_text ();
860 uint32_t samples = atoi (bs_text); /* will ignore trailing text */
861 uint32_t rate = get_rate();
863 /* Translators: "msecs" is ALWAYS plural here, so we do not
864 need singular form as well.
866 /* Developers: note the hard-coding of a double buffered model
867 in the (2 * samples) computation of latency. we always start
868 the audiobackend in this configuration.
871 snprintf (buf, sizeof (buf), _("(%.1f msecs)"), (2 * samples) / (rate/1000.0));
872 buffer_size_duration_label.set_text (buf);
876 EngineControl::parameter_changed ()
878 if (!ignore_changes) {
883 EngineControl::State*
884 EngineControl::get_matching_state (const string& backend,
885 const string& driver,
886 const string& device)
888 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
889 if ((*i).backend == backend &&
890 (*i).driver == driver &&
891 (*i).device == device) {
898 EngineControl::State*
899 EngineControl::get_saved_state_for_currently_displayed_backend_and_device ()
901 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
904 return get_matching_state (backend_combo.get_active_text(),
905 (backend->requires_driver_selection() ? (std::string) driver_combo.get_active_text() : string()),
906 device_combo.get_active_text());
910 return get_matching_state (backend_combo.get_active_text(),
912 device_combo.get_active_text());
915 EngineControl::State*
916 EngineControl::save_state ()
918 if (!_have_control) {
922 bool existing = true;
923 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
930 store_state (*state);
933 states.push_back (*state);
940 EngineControl::store_state (State& state)
942 state.backend = get_backend ();
943 state.driver = get_driver ();
944 state.device = get_device_name ();
945 state.sample_rate = get_rate ();
946 state.buffer_size = get_buffer_size ();
947 state.input_latency = get_input_latency ();
948 state.output_latency = get_output_latency ();
949 state.input_channels = get_input_channels ();
950 state.output_channels = get_output_channels ();
954 EngineControl::maybe_display_saved_state ()
956 if (!_have_control) {
960 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
963 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
965 if (!_desired_sample_rate) {
966 sample_rate_combo.set_active_text (rate_as_string (state->sample_rate));
968 buffer_size_combo.set_active_text (bufsize_as_string (state->buffer_size));
969 /* call this explicitly because we're ignoring changes to
970 the controls at this point.
972 show_buffer_duration ();
973 input_latency.set_value (state->input_latency);
974 output_latency.set_value (state->output_latency);
979 EngineControl::get_state ()
981 XMLNode* root = new XMLNode ("AudioMIDISetup");
984 if (!states.empty()) {
985 XMLNode* state_nodes = new XMLNode ("EngineStates");
987 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
989 XMLNode* node = new XMLNode ("State");
991 node->add_property ("backend", (*i).backend);
992 node->add_property ("driver", (*i).driver);
993 node->add_property ("device", (*i).device);
994 node->add_property ("sample-rate", (*i).sample_rate);
995 node->add_property ("buffer-size", (*i).buffer_size);
996 node->add_property ("input-latency", (*i).input_latency);
997 node->add_property ("output-latency", (*i).output_latency);
998 node->add_property ("input-channels", (*i).input_channels);
999 node->add_property ("output-channels", (*i).output_channels);
1000 node->add_property ("active", (*i).active ? "yes" : "no");
1002 state_nodes->add_child_nocopy (*node);
1005 root->add_child_nocopy (*state_nodes);
1012 EngineControl::set_state (const XMLNode& root)
1014 XMLNodeList clist, cclist;
1015 XMLNodeConstIterator citer, cciter;
1017 XMLNode* grandchild;
1018 XMLProperty* prop = NULL;
1020 if (root.name() != "AudioMIDISetup") {
1024 clist = root.children();
1028 for (citer = clist.begin(); citer != clist.end(); ++citer) {
1032 if (child->name() != "EngineStates") {
1036 cclist = child->children();
1038 for (cciter = cclist.begin(); cciter != cclist.end(); ++cciter) {
1041 grandchild = *cciter;
1043 if (grandchild->name() != "State") {
1047 if ((prop = grandchild->property ("backend")) == 0) {
1050 state.backend = prop->value ();
1052 if ((prop = grandchild->property ("driver")) == 0) {
1055 state.driver = prop->value ();
1057 if ((prop = grandchild->property ("device")) == 0) {
1060 state.device = prop->value ();
1062 if ((prop = grandchild->property ("sample-rate")) == 0) {
1065 state.sample_rate = atof (prop->value ());
1067 if ((prop = grandchild->property ("buffer-size")) == 0) {
1070 state.buffer_size = atoi (prop->value ());
1072 if ((prop = grandchild->property ("input-latency")) == 0) {
1075 state.input_latency = atoi (prop->value ());
1077 if ((prop = grandchild->property ("output-latency")) == 0) {
1080 state.output_latency = atoi (prop->value ());
1082 if ((prop = grandchild->property ("input-channels")) == 0) {
1085 state.input_channels = atoi (prop->value ());
1087 if ((prop = grandchild->property ("output-channels")) == 0) {
1090 state.output_channels = atoi (prop->value ());
1092 if ((prop = grandchild->property ("active")) == 0) {
1095 state.active = string_is_affirmative (prop->value ());
1097 states.push_back (state);
1101 /* now see if there was an active state and switch the setup to it */
1103 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
1107 backend_combo.set_active_text ((*i).backend);
1108 driver_combo.set_active_text ((*i).driver);
1109 device_combo.set_active_text ((*i).device);
1110 sample_rate_combo.set_active_text (rate_as_string ((*i).sample_rate));
1111 buffer_size_combo.set_active_text (bufsize_as_string ((*i).buffer_size));
1112 input_latency.set_value ((*i).input_latency);
1113 output_latency.set_value ((*i).output_latency);
1122 EngineControl::push_state_to_backend (bool start)
1128 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1134 /* figure out what is going to change */
1136 bool restart_required = false;
1137 bool was_running = ARDOUR::AudioEngine::instance()->running();
1138 bool change_driver = false;
1139 bool change_device = false;
1140 bool change_rate = false;
1141 bool change_bufsize = false;
1142 bool change_latency = false;
1143 bool change_channels = false;
1145 uint32_t ochan = get_output_channels ();
1146 uint32_t ichan = get_input_channels ();
1148 if (_have_control) {
1150 if (started_at_least_once) {
1152 /* we can control the backend */
1154 if (backend->requires_driver_selection()) {
1155 if (get_driver() != backend->driver_name()) {
1156 change_driver = true;
1160 if (get_device_name() != backend->device_name()) {
1161 change_device = true;
1164 if (get_rate() != backend->sample_rate()) {
1168 if (get_buffer_size() != backend->buffer_size()) {
1169 change_bufsize = true;
1172 /* zero-requested channels means "all available" */
1175 ichan = backend->input_channels();
1179 ochan = backend->output_channels();
1182 if (ichan != backend->input_channels()) {
1183 change_channels = true;
1186 if (ochan != backend->output_channels()) {
1187 change_channels = true;
1190 if (get_input_latency() != backend->systemic_input_latency() ||
1191 get_output_latency() != backend->systemic_output_latency()) {
1192 change_latency = true;
1195 /* backend never started, so we have to force a group
1198 change_driver = true;
1199 change_device = true;
1201 change_bufsize = true;
1202 change_channels = true;
1203 change_latency = true;
1208 /* we have no control over the backend, meaning that we can
1209 * only possibly change sample rate and buffer size.
1213 if (get_rate() != backend->sample_rate()) {
1214 change_bufsize = true;
1217 if (get_buffer_size() != backend->buffer_size()) {
1218 change_bufsize = true;
1222 if (!_have_control) {
1224 /* We do not have control over the backend, so the best we can
1225 * do is try to change the sample rate and/or bufsize and get
1229 if (change_rate && !backend->can_change_sample_rate_when_running()) {
1233 if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1238 backend->set_sample_rate (get_rate());
1241 if (change_bufsize) {
1242 backend->set_buffer_size (get_buffer_size());
1250 /* determine if we need to stop the backend before changing parameters */
1252 if (change_driver || change_device || change_channels || change_latency ||
1253 (change_rate && !backend->can_change_sample_rate_when_running()) ||
1254 (change_bufsize && !backend->can_change_buffer_size_when_running())) {
1255 restart_required = true;
1257 restart_required = false;
1262 if (!change_driver && !change_device && !change_channels && !change_latency) {
1263 /* no changes in any parameters that absolutely require a
1264 * restart, so check those that might be changeable without a
1268 if (change_rate && !backend->can_change_sample_rate_when_running()) {
1269 /* can't do this while running ... */
1270 restart_required = true;
1273 if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1274 /* can't do this while running ... */
1275 restart_required = true;
1281 if (restart_required) {
1282 if (ARDOUR_UI::instance()->disconnect_from_engine ()) {
1289 if (change_driver && backend->set_driver (get_driver())) {
1290 error << string_compose (_("Cannot set driver to %1"), get_driver()) << endmsg;
1293 if (change_device && backend->set_device_name (get_device_name())) {
1294 error << string_compose (_("Cannot set device name to %1"), get_device_name()) << endmsg;
1297 if (change_rate && backend->set_sample_rate (get_rate())) {
1298 error << string_compose (_("Cannot set sample rate to %1"), get_rate()) << endmsg;
1301 if (change_bufsize && backend->set_buffer_size (get_buffer_size())) {
1302 error << string_compose (_("Cannot set buffer size to %1"), get_buffer_size()) << endmsg;
1306 if (change_channels || get_input_channels() == 0 || get_output_channels() == 0) {
1307 if (backend->set_input_channels (get_input_channels())) {
1308 error << string_compose (_("Cannot set input channels to %1"), get_input_channels()) << endmsg;
1311 if (backend->set_output_channels (get_output_channels())) {
1312 error << string_compose (_("Cannot set output channels to %1"), get_output_channels()) << endmsg;
1316 if (change_latency) {
1317 if (backend->set_systemic_input_latency (get_input_latency())) {
1318 error << string_compose (_("Cannot set input latency to %1"), get_input_latency()) << endmsg;
1321 if (backend->set_systemic_output_latency (get_output_latency())) {
1322 error << string_compose (_("Cannot set output latency to %1"), get_output_latency()) << endmsg;
1327 if (start || (was_running && restart_required)) {
1328 if (ARDOUR_UI::instance()->reconnect_to_engine()) {
1339 EngineControl::post_push ()
1341 /* get a pointer to the current state object, creating one if
1345 if (_have_control) {
1346 State* state = get_saved_state_for_currently_displayed_backend_and_device ();
1349 state = save_state ();
1355 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
1356 (*i).active = false;
1359 /* mark this one active (to be used next time the dialog is
1363 state->active = true;
1365 manage_control_app_sensitivity ();
1368 /* schedule a redisplay of MIDI ports */
1370 Glib::signal_timeout().connect (sigc::bind_return (sigc::mem_fun (*this, &EngineControl::refresh_midi_display), false), 1000);
1375 EngineControl::get_rate () const
1377 float r = atof (sample_rate_combo.get_active_text ());
1378 /* the string may have been translated with an abbreviation for
1379 * thousands, so use a crude heuristic to fix this.
1389 EngineControl::get_buffer_size () const
1391 string txt = buffer_size_combo.get_active_text ();
1394 if (sscanf (txt.c_str(), "%d", &samples) != 1) {
1402 EngineControl::get_input_channels() const
1404 return (uint32_t) input_channels_adjustment.get_value();
1408 EngineControl::get_output_channels() const
1410 return (uint32_t) output_channels_adjustment.get_value();
1414 EngineControl::get_input_latency() const
1416 return (uint32_t) input_latency_adjustment.get_value();
1420 EngineControl::get_output_latency() const
1422 return (uint32_t) output_latency_adjustment.get_value();
1426 EngineControl::get_backend () const
1428 return backend_combo.get_active_text ();
1432 EngineControl::get_driver () const
1434 return driver_combo.get_active_text ();
1438 EngineControl::get_device_name () const
1440 return device_combo.get_active_text ();
1444 EngineControl::control_app_button_clicked ()
1446 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1452 backend->launch_control_app ();
1456 EngineControl::manage_control_app_sensitivity ()
1458 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1464 string appname = backend->control_app_name();
1466 if (appname.empty()) {
1467 control_app_button.set_sensitive (false);
1469 control_app_button.set_sensitive (true);
1474 EngineControl::set_desired_sample_rate (uint32_t sr)
1476 _desired_sample_rate = sr;
1481 EngineControl::on_switch_page (GtkNotebookPage*, guint page_num)
1483 if (page_num == 0) {
1484 cancel_button->set_sensitive (true);
1485 ok_button->set_sensitive (true);
1486 apply_button->set_sensitive (true);
1488 cancel_button->set_sensitive (false);
1489 ok_button->set_sensitive (false);
1490 apply_button->set_sensitive (false);
1493 if (page_num == 1) {
1495 refresh_midi_display ();
1498 if (page_num == 2) {
1501 if (!ARDOUR::AudioEngine::instance()->running()) {
1503 PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
1505 /* save any existing latency values */
1507 uint32_t il = (uint32_t) input_latency.get_value ();
1508 uint32_t ol = (uint32_t) input_latency.get_value ();
1510 /* reset to zero so that our new test instance of JACK
1511 will be clean of any existing latency measures.
1514 input_latency.set_value (0);
1515 output_latency.set_value (0);
1519 input_latency.set_value (il);
1520 output_latency.set_value (ol);
1524 if (ARDOUR::AudioEngine::instance()->prepare_for_latency_measurement()) {
1525 disable_latency_tab ();
1528 enable_latency_tab ();
1531 ARDOUR::AudioEngine::instance()->stop_latency_detection();
1535 /* latency measurement */
1538 EngineControl::check_latency_measurement ()
1540 MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1542 if (mtdm->resolve () < 0) {
1543 lm_results.set_markup (string_compose ("<span foreground=\"red\">%1</span>", _("No signal detected ")));
1547 if (mtdm->err () > 0.3) {
1553 ARDOUR::framecnt_t const sample_rate = ARDOUR::AudioEngine::instance()->sample_rate();
1555 if (sample_rate == 0) {
1556 lm_results.set_text (_("Disconnected from audio engine"));
1557 ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1561 uint32_t frames_total = mtdm->del();
1562 uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1564 snprintf (buf, sizeof (buf), "%u samples %10.3lf ms", extra, extra * 1000.0f/sample_rate);
1568 if (mtdm->err () > 0.2) {
1569 strcat (buf, " ??");
1574 strcat (buf, " (Inv)");
1579 lm_measure_button.set_active (false);
1580 lm_use_button.set_sensitive (true);
1581 strcat (buf, " (set)");
1582 have_lm_results = true;
1585 lm_results.set_text (buf);
1591 EngineControl::start_latency_detection ()
1593 ARDOUR::AudioEngine::instance()->set_latency_input_port (lm_input_channel_combo.get_active_text());
1594 ARDOUR::AudioEngine::instance()->set_latency_output_port (lm_output_channel_combo.get_active_text());
1595 ARDOUR::AudioEngine::instance()->start_latency_detection ();
1596 lm_results.set_text (_("Detecting ..."));
1597 latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &EngineControl::check_latency_measurement), 250);
1598 lm_start_stop_label.set_text (_("Cancel measurement"));
1599 have_lm_results = false;
1600 lm_input_channel_combo.set_sensitive (false);
1601 lm_output_channel_combo.set_sensitive (false);
1605 EngineControl::end_latency_detection ()
1607 ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1608 latency_timeout.disconnect ();
1609 lm_start_stop_label.set_text (_("Measure latency"));
1610 if (!have_lm_results) {
1611 lm_results.set_markup ("<i>No measurement results yet</i>");
1613 lm_input_channel_combo.set_sensitive (true);
1614 lm_output_channel_combo.set_sensitive (true);
1618 EngineControl::latency_button_toggled ()
1620 if (lm_measure_button.get_active ()) {
1621 start_latency_detection ();
1623 end_latency_detection ();
1628 EngineControl::use_latency_button_clicked ()
1630 MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1636 uint32_t frames_total = mtdm->del();
1637 uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1638 uint32_t one_way = extra/2;
1640 input_latency_adjustment.set_value (one_way);
1641 output_latency_adjustment.set_value (one_way);
1645 EngineControl::on_delete_event (GdkEventAny* ev)
1647 if (notebook.get_current_page() == 2) {
1648 /* currently on latency tab - be sure to clean up */
1649 end_latency_detection ();
1651 return ArdourDialog::on_delete_event (ev);
1655 EngineControl::engine_running ()
1657 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1660 buffer_size_combo.set_active_text (bufsize_as_string (backend->buffer_size()));
1661 sample_rate_combo.set_active_text (rate_as_string (backend->sample_rate()));
1663 if (backend->can_change_buffer_size_when_running()) {
1664 buffer_size_combo.set_sensitive (true);
1666 buffer_size_combo.set_sensitive (false);
1669 if (backend->can_change_sample_rate_when_running()) {
1670 sample_rate_combo.set_sensitive (true);
1672 sample_rate_combo.set_sensitive (false);
1675 connect_disconnect_button.set_label (string_compose (_("Disconnect from %1"), backend->name()));
1677 started_at_least_once = true;
1681 EngineControl::engine_stopped ()
1683 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1686 buffer_size_combo.set_sensitive (false);
1687 connect_disconnect_button.set_label (string_compose (_("Connect to %1"), backend->name()));
1689 sample_rate_combo.set_sensitive (true);
1690 buffer_size_combo.set_sensitive (true);
1694 EngineControl::connect_disconnect_click()
1696 if (ARDOUR::AudioEngine::instance()->running()) {
1697 ARDOUR_UI::instance()->disconnect_from_engine ();
1699 ARDOUR_UI::instance()->reconnect_to_engine ();