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>
29 #include <gtkmm/messagedialog.h>
31 #include "pbd/error.h"
32 #include "pbd/xml++.h"
34 #include <gtkmm/stock.h>
35 #include <gtkmm/notebook.h>
36 #include <gtkmm2ext/utils.h>
38 #include "ardour/audio_backend.h"
39 #include "ardour/audioengine.h"
40 #include "ardour/rc_configuration.h"
42 #include "pbd/convert.h"
43 #include "pbd/error.h"
45 #include "engine_dialog.h"
50 using namespace Gtkmm2ext;
54 EngineControl::EngineControl ()
55 : input_latency_adjustment (0, 0, 99999, 1)
56 , input_latency (input_latency_adjustment)
57 , output_latency_adjustment (0, 0, 99999, 1)
58 , output_latency (output_latency_adjustment)
59 , input_channels_adjustment (2, 0, 256, 1)
60 , input_channels (input_channels_adjustment)
61 , output_channels_adjustment (2, 0, 256, 1)
62 , output_channels (output_channels_adjustment)
63 , ports_adjustment (128, 8, 1024, 1, 16)
64 , ports_spinner (ports_adjustment)
65 , realtime_button (_("Realtime"))
73 using namespace Notebook_Helpers;
75 vector<string> strings;
79 /* basic parameters */
81 basic_packer.set_spacings (6);
85 vector<const ARDOUR::AudioBackendInfo*> backends = ARDOUR::AudioEngine::instance()->available_backends();
86 for (vector<const ARDOUR::AudioBackendInfo*>::const_iterator b = backends.begin(); b != backends.end(); ++b) {
87 strings.push_back ((*b)->name);
90 set_popdown_strings (backend_combo, strings);
91 backend_combo.set_active_text (strings.front());
93 backend_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::backend_changed));
96 driver_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::driver_changed));
99 strings.push_back (_("None"));
101 strings.push_back (_("coremidi"));
103 strings.push_back (_("seq"));
104 strings.push_back (_("raw"));
106 set_popdown_strings (midi_driver_combo, strings);
107 midi_driver_combo.set_active_text (strings.front ());
111 label = manage (left_aligned_label (_("Audio System:")));
112 basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
113 basic_packer.attach (backend_combo, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
116 label = manage (left_aligned_label (_("Driver:")));
117 basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
118 basic_packer.attach (driver_combo, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
121 label = manage (left_aligned_label (_("Device:")));
122 basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
123 basic_packer.attach (device_combo, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
126 label = manage (left_aligned_label (_("Sample rate:")));
127 basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
128 basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
131 sr_connection = sample_rate_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::sample_rate_changed));
133 label = manage (left_aligned_label (_("Buffer size:")));
134 basic_packer.attach (*label, 0, 1, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
135 basic_packer.attach (buffer_size_combo, 1, 2, row, row + 1, FILL|EXPAND, (AttachOptions) 0);
138 bs_connection = buffer_size_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::buffer_size_changed));
140 label = manage (left_aligned_label (_("Hardware input latency:")));
141 basic_packer.attach (*label, 0, 1, row, row+1, FILL|EXPAND, (AttachOptions) 0);
142 basic_packer.attach (input_latency, 1, 2, row, row+1, FILL|EXPAND, (AttachOptions) 0);
143 label = manage (left_aligned_label (_("samples")));
144 basic_packer.attach (*label, 2, 3, row, row+1, FILL|EXPAND, (AttachOptions) 0);
147 label = manage (left_aligned_label (_("Hardware output latency:")));
148 basic_packer.attach (*label, 0, 1, row, row+1, FILL|EXPAND, (AttachOptions) 0);
149 basic_packer.attach (output_latency, 1, 2, row, row+1, FILL|EXPAND, (AttachOptions) 0);
150 label = manage (left_aligned_label (_("samples")));
151 basic_packer.attach (*label, 2, 3, row, row+1, FILL|EXPAND, (AttachOptions) 0);
154 device_combo.set_size_request (250, -1);
155 input_device_combo.set_size_request (250, -1);
156 output_device_combo.set_size_request (250, -1);
158 device_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::device_changed));
160 basic_hbox.pack_start (basic_packer, false, false);
162 basic_packer.set_border_width (12);
163 midi_packer.set_border_width (12);
165 notebook.pages().push_back (TabElem (basic_hbox, _("Audio")));
166 notebook.pages().push_back (TabElem (midi_hbox, _("MIDI")));
167 notebook.set_border_width (12);
169 notebook.set_tab_pos (POS_RIGHT);
170 notebook.show_all ();
172 notebook.set_name ("SettingsNotebook");
174 set_border_width (12);
175 pack_start (notebook);
177 /* Pick up any existing audio setup configuration, if appropriate */
179 XMLNode* audio_setup = ARDOUR::Config->extra_xml ("AudioMIDISetup");
182 set_state (*audio_setup);
186 EngineControl::~EngineControl ()
192 EngineControl::backend_changed ()
194 string backend_name = backend_combo.get_active_text();
195 boost::shared_ptr<ARDOUR::AudioBackend> backend;
197 if (!(backend = ARDOUR::AudioEngine::instance()->set_backend (backend_name, "ardour", ""))) {
202 if (backend->requires_driver_selection()) {
203 vector<string> drivers = backend->enumerate_drivers();
204 driver_combo.set_sensitive (true);
205 set_popdown_strings (driver_combo, drivers);
206 driver_combo.set_active_text (drivers.front());
209 driver_combo.set_sensitive (false);
217 EngineControl::list_devices ()
219 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
222 /* now fill out devices, mark sample rates, buffer sizes insensitive */
224 vector<ARDOUR::AudioBackend::DeviceStatus> all_devices = backend->enumerate_devices ();
226 /* NOTE: Ardour currently does not display the "available" field of the
229 * Doing so would require a different GUI widget than the combo
230 * box/popdown that we currently use, since it has no way to list
231 * items that are not selectable. Something more like a popup menu,
232 * which could have unselectable items, would be appropriate.
235 vector<string> available_devices;
237 for (vector<ARDOUR::AudioBackend::DeviceStatus>::const_iterator i = all_devices.begin(); i != all_devices.end(); ++i) {
238 available_devices.push_back (i->name);
241 set_popdown_strings (device_combo, available_devices);
242 set_popdown_strings (input_device_combo, available_devices);
243 set_popdown_strings (output_device_combo, available_devices);
245 if (!available_devices.empty()) {
246 device_combo.set_active_text (available_devices.front());
247 input_device_combo.set_active_text (available_devices.front());
248 output_device_combo.set_active_text (available_devices.front());
255 EngineControl::driver_changed ()
257 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
260 backend->set_driver (driver_combo.get_active_text());
267 EngineControl::device_changed ()
269 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
271 string device_name = device_combo.get_active_text ();
274 /* don't allow programmatic change to sample_rate_combo to cause a
275 recursive call to this method.
278 sr_connection.block ();
282 vector<float> sr = backend->available_sample_rates (device_name);
283 for (vector<float>::const_iterator x = sr.begin(); x != sr.end(); ++x) {
285 if (fmod (*x, 1000.0f)) {
286 snprintf (buf, sizeof (buf), "%.1f kHz", (*x)/1000.0);
288 snprintf (buf, sizeof (buf), "%.0f kHz", (*x)/1000.0);
293 set_popdown_strings (sample_rate_combo, s);
294 sample_rate_combo.set_active_text (s.front());
296 reshow_buffer_sizes (true);
298 sr_connection.unblock ();
304 EngineControl::sample_rate_changed ()
306 /* reset the strings for buffer size to show the correct msec value
307 (reflecting the new sample rate
310 reshow_buffer_sizes (false);
316 EngineControl::buffer_size_changed ()
322 EngineControl::reshow_buffer_sizes (bool size_choice_changed)
324 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
326 string device_name = device_combo.get_active_text ();
328 uint32_t existing_size_choice = 0;
329 string new_target_string;
331 /* buffer sizes - convert from just samples to samples + msecs for
332 * the displayed string
335 bs_connection.block ();
337 if (!size_choice_changed) {
338 sscanf (buffer_size_combo.get_active_text().c_str(), "%" PRIu32, &existing_size_choice);
342 vector<uint32_t> bs = backend->available_buffer_sizes(device_name);
343 uint32_t rate = get_rate();
345 for (vector<uint32_t>::const_iterator x = bs.begin(); x != bs.end(); ++x) {
348 /* Translators: "samples" is ALWAYS plural here, so we do not
349 need singular form as well. Same for msecs.
351 snprintf (buf, sizeof (buf), _("%u samples (%.1f msecs)"), *x, (2 * (*x)) / (rate/1000.0));
354 /* if this is the size previously chosen, this is the string we
355 * will want to be active in the combo.
358 if (existing_size_choice == *x) {
359 new_target_string = buf;
364 set_popdown_strings (buffer_size_combo, s);
366 if (!new_target_string.empty()) {
367 buffer_size_combo.set_active_text (new_target_string);
369 buffer_size_combo.set_active_text (s.front());
372 bs_connection.unblock ();
376 EngineControl::audio_mode_changed ()
378 std::string str = audio_mode_combo.get_active_text();
380 if (str == _("Playback/recording on 1 device")) {
381 input_device_combo.set_sensitive (false);
382 output_device_combo.set_sensitive (false);
383 } else if (str == _("Playback/recording on 2 devices")) {
384 input_device_combo.set_sensitive (true);
385 output_device_combo.set_sensitive (true);
386 } else if (str == _("Playback only")) {
387 output_device_combo.set_sensitive (true);
388 input_device_combo.set_sensitive (false);
389 } else if (str == _("Recording only")) {
390 input_device_combo.set_sensitive (true);
391 output_device_combo.set_sensitive (false);
395 EngineControl::State*
396 EngineControl::get_matching_state (const string& backend,
397 const string& driver,
398 const string& device)
400 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
401 if ((*i).backend == backend &&
402 (*i).driver == driver &&
403 (*i).device == device) {
410 EngineControl::State*
411 EngineControl::get_current_state ()
413 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
416 return get_matching_state (backend_combo.get_active_text(),
417 (backend->requires_driver_selection() ? (std::string) driver_combo.get_active_text() : string()),
418 device_combo.get_active_text());
422 return get_matching_state (backend_combo.get_active_text(),
424 device_combo.get_active_text());
428 EngineControl::save_state ()
430 bool existing = true;
431 State* state = get_current_state ();
438 state->backend = backend_combo.get_active_text ();
439 state->driver = driver_combo.get_active_text ();
440 state->device = device_combo.get_active_text ();
441 state->buffer_size = buffer_size_combo.get_active_text ();
442 state->sample_rate = sample_rate_combo.get_active_text ();
445 states.push_back (*state);
450 EngineControl::maybe_set_state ()
452 State* state = get_current_state ();
455 sr_connection.block ();
456 bs_connection.block ();
457 sample_rate_combo.set_active_text (state->sample_rate);
458 /* need to reset possible strings for buffer size before we do
461 reshow_buffer_sizes (false);
462 buffer_size_combo.set_active_text (state->buffer_size);
463 bs_connection.unblock ();
464 sr_connection.unblock ();
469 EngineControl::get_state ()
471 XMLNode* root = new XMLNode ("AudioMIDISetup");
476 if (!states.empty()) {
477 XMLNode* state_nodes = new XMLNode ("EngineStates");
479 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
481 XMLNode* node = new XMLNode ("State");
483 node->add_property ("backend", (*i).backend);
484 node->add_property ("driver", (*i).driver);
485 node->add_property ("device", (*i).device);
486 node->add_property ("sample-rate", (*i).sample_rate);
487 node->add_property ("buffer-size", (*i).buffer_size);
488 node->add_property ("input-latency", (*i).input_latency);
489 node->add_property ("output-latency", (*i).output_latency);
490 node->add_property ("input-channels", (*i).input_channels);
491 node->add_property ("output-channels", (*i).output_channels);
492 node->add_property ("active", (*i).active ? "yes" : "no");
494 state_nodes->add_child_nocopy (*node);
497 root->add_child_nocopy (*state_nodes);
505 EngineControl::set_state (const XMLNode& root)
507 XMLNodeList clist, cclist;
508 XMLNodeConstIterator citer, cciter;
511 XMLProperty* prop = NULL;
513 if (root.name() != "AudioMIDISetup") {
517 clist = root.children();
521 for (citer = clist.begin(); citer != clist.end(); ++citer) {
525 if (child->name() != "EngineStates") {
529 cclist = child->children();
531 for (cciter = cclist.begin(); cciter != cclist.end(); ++cciter) {
534 grandchild = *cciter;
536 if (grandchild->name() != "State") {
540 if ((prop = grandchild->property ("backend")) == 0) {
543 state.backend = prop->value ();
545 if ((prop = grandchild->property ("driver")) == 0) {
548 state.driver = prop->value ();
550 if ((prop = grandchild->property ("device")) == 0) {
553 state.device = prop->value ();
555 if ((prop = grandchild->property ("sample-rate")) == 0) {
558 state.sample_rate = prop->value ();
560 if ((prop = grandchild->property ("buffer-size")) == 0) {
563 state.buffer_size = prop->value ();
565 if ((prop = grandchild->property ("input-latency")) == 0) {
568 state.input_latency = prop->value ();
570 if ((prop = grandchild->property ("output-latency")) == 0) {
573 state.output_latency = prop->value ();
575 if ((prop = grandchild->property ("input-channels")) == 0) {
578 state.input_channels = prop->value ();
580 if ((prop = grandchild->property ("output-channels")) == 0) {
583 state.output_channels = prop->value ();
585 if ((prop = grandchild->property ("active")) == 0) {
588 state.active = string_is_affirmative (prop->value ());
590 states.push_back (state);
594 /* now see if there was an active state and switch the setup to it */
596 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
598 sr_connection.block ();
599 bs_connection.block ();
600 backend_combo.set_active_text ((*i).backend);
601 driver_combo.set_active_text ((*i).driver);
602 device_combo.set_active_text ((*i).device);
603 sample_rate_combo.set_active_text ((*i).sample_rate);
604 buffer_size_combo.set_active_text ((*i).buffer_size);
605 sr_connection.unblock ();
606 bs_connection.unblock ();
613 EngineControl::setup_engine (bool start)
615 boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
618 /* grab the parameters from the GUI and apply them */
621 if (backend->requires_driver_selection()) {
622 if (backend->set_driver (get_driver())) {
627 if (backend->set_device_name (get_device_name())) {
631 if (backend->set_sample_rate (get_rate())) {
632 error << string_compose (_("Cannot set sample rate to %1"), get_rate()) << endmsg;
635 if (backend->set_buffer_size (get_buffer_size())) {
636 error << string_compose (_("Cannot set buffer size to %1"), get_buffer_size()) << endmsg;
639 if (backend->set_input_channels (get_input_channels())) {
640 error << string_compose (_("Cannot set input channels to %1"), get_input_channels()) << endmsg;
643 if (backend->set_output_channels (get_output_channels())) {
644 error << string_compose (_("Cannot set output channels to %1"), get_output_channels()) << endmsg;
647 if (backend->set_systemic_input_latency (get_input_latency())) {
648 error << string_compose (_("Cannot set input latency to %1"), get_input_latency()) << endmsg;
651 if (backend->set_systemic_output_latency (get_output_latency())) {
652 error << string_compose (_("Cannot set output latency to %1"), get_output_latency()) << endmsg;
656 /* we've used this dialog to configure the engine, which means
657 * that our state becomes relevant for saving (and thus
658 * implicitly, restoring.
663 /* get a pointer to the current state object, creating one if
667 State* state = get_current_state ();
671 state = get_current_state ();
677 for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
681 /* mark this one active (to be used next time the dialog is
685 state->active = true;
688 return ARDOUR::AudioEngine::instance()->start();
694 cerr << "exception thrown...\n";
700 EngineControl::get_rate () const
702 double r = atof (sample_rate_combo.get_active_text ());
703 /* the string may have been translated with an abbreviation for
704 * thousands, so use a crude heuristic to fix this.
713 EngineControl::get_buffer_size () const
715 string txt = buffer_size_combo.get_active_text ();
718 if (sscanf (txt.c_str(), "%d", &samples) != 1) {
726 EngineControl::get_input_channels() const
728 return (uint32_t) input_channels_adjustment.get_value();
732 EngineControl::get_output_channels() const
734 return (uint32_t) output_channels_adjustment.get_value();
738 EngineControl::get_input_latency() const
740 return (uint32_t) input_latency_adjustment.get_value();
744 EngineControl::get_output_latency() const
746 return (uint32_t) output_latency_adjustment.get_value();
750 EngineControl::get_driver () const
752 return driver_combo.get_active_text ();
756 EngineControl::get_device_name () const
758 return device_combo.get_active_text ();