65fa7227a78eb3fe717ed98946ced7704cbdf441
[ardour.git] / gtk2_ardour / engine_dialog.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <exception>
21 #include <vector>
22 #include <cmath>
23 #include <fstream>
24 #include <map>
25
26 #include <boost/scoped_ptr.hpp>
27
28 #include <gtkmm/messagedialog.h>
29
30 #include "pbd/error.h"
31 #include "pbd/xml++.h"
32 #include "pbd/unwind.h"
33
34 #include <gtkmm/alignment.h>
35 #include <gtkmm/stock.h>
36 #include <gtkmm/notebook.h>
37 #include <gtkmm2ext/utils.h>
38
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"
44
45 #include "pbd/convert.h"
46 #include "pbd/error.h"
47
48 #include "ardour_ui.h"
49 #include "engine_dialog.h"
50 #include "gui_thread.h"
51 #include "i18n.h"
52
53 using namespace std;
54 using namespace Gtk;
55 using namespace Gtkmm2ext;
56 using namespace PBD;
57 using namespace Glib;
58
59 EngineControl::EngineControl ()
60         : ArdourDialog (_("Audio/MIDI Setup"))
61         , basic_packer (9, 3)
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"))
75         , lm_table (5, 2)
76         , have_lm_results (false)
77         , midi_refresh_button (_("Refresh list"))
78         , aj_button (_("Start MIDI ALSA/JACK bridge"))
79         , ignore_changes (0)
80         , _desired_sample_rate (0)
81 {
82         if (!ARDOUR::AudioEngine::instance()->setup_required()) {
83                 _have_control = false;
84         } else {
85                 _have_control = true;
86         }
87
88         set_name (X_("AudioMIDISetup"));
89
90         build_notebook ();
91
92         get_vbox()->set_border_width (12);
93         get_vbox()->pack_start (notebook);
94
95         control_app_button.signal_clicked().connect (mem_fun (*this, &EngineControl::control_app_button_clicked));
96         manage_control_app_sensitivity ();
97
98         cancel_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
99         ok_button = add_button (Gtk::Stock::OK, Gtk::RESPONSE_OK);
100         apply_button = add_button (Gtk::Stock::APPLY, Gtk::RESPONSE_APPLY);
101
102         /* Pick up any existing audio setup configuration, if appropriate */
103
104         XMLNode* audio_setup = ARDOUR::Config->extra_xml ("AudioMIDISetup");
105         
106         /* push a change as if we altered the backend */
107         backend_changed ();
108
109         if (audio_setup) {
110                 set_state (*audio_setup);
111         } 
112 }
113
114 void
115 EngineControl::on_response (int response_id)
116 {
117         ArdourDialog::on_response (response_id);
118
119         switch (response_id) {
120         case RESPONSE_APPLY:
121                 push_state_to_backend (true);
122                 break;
123         case RESPONSE_OK:
124                 push_state_to_backend (true);
125                 hide ();
126                 break;
127         case RESPONSE_DELETE_EVENT: {
128                 GdkEventButton ev;
129                 ev.type = GDK_BUTTON_PRESS;
130                 ev.button = 1;
131                 on_delete_event ((GdkEventAny*) &ev);
132                 break;
133         }
134         default:
135                 hide ();
136         }
137 }
138
139 void
140 EngineControl::build_notebook ()
141 {
142         using namespace Notebook_Helpers;
143         Label* label;
144         vector<string> strings;
145         int row = 0;
146
147         vector<const ARDOUR::AudioBackendInfo*> backends = ARDOUR::AudioEngine::instance()->available_backends();
148         for (vector<const ARDOUR::AudioBackendInfo*>::const_iterator b = backends.begin(); b != backends.end(); ++b) {
149                 strings.push_back ((*b)->name);
150         }
151
152         set_popdown_strings (backend_combo, strings);
153         backend_combo.set_active_text (strings.front());
154
155         basic_packer.set_spacings (6);
156         basic_packer.set_border_width (12);
157         basic_packer.set_homogeneous (true);
158
159         row = 0;
160
161         AttachOptions xopt = AttachOptions (FILL|EXPAND);
162
163         label = manage (left_aligned_label (_("Audio System:")));
164         basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
165         basic_packer.attach (backend_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
166         row++;
167
168         label = manage (left_aligned_label (_("Driver:")));
169         basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
170         basic_packer.attach (driver_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
171         row++;
172
173         label = manage (left_aligned_label (_("Device:")));
174         basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
175         basic_packer.attach (device_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
176         row++;
177
178         label = manage (left_aligned_label (_("Sample rate:")));
179         basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
180         basic_packer.attach (sample_rate_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
181         row++;
182
183
184         label = manage (left_aligned_label (_("Buffer size:")));
185         basic_packer.attach (*label, 0, 1, row, row + 1, xopt, (AttachOptions) 0);
186         basic_packer.attach (buffer_size_combo, 1, 2, row, row + 1, xopt, (AttachOptions) 0);
187         buffer_size_duration_label.set_alignment (0.0); /* left-align */
188         basic_packer.attach (buffer_size_duration_label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
189         row++;
190
191         input_channels.set_name ("InputChannels");
192         input_channels.set_flags(Gtk::CAN_FOCUS);
193         input_channels.set_digits(0);
194         input_channels.set_wrap(false);
195         output_channels.set_editable (true);
196
197         label = manage (left_aligned_label (_("Input Channels:")));
198         basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
199         basic_packer.attach (input_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
200         ++row;
201
202         output_channels.set_name ("OutputChannels");
203         output_channels.set_flags(Gtk::CAN_FOCUS);
204         output_channels.set_digits(0);
205         output_channels.set_wrap(false);
206         output_channels.set_editable (true);
207
208         label = manage (left_aligned_label (_("Output Channels:")));
209         basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
210         basic_packer.attach (output_channels, 1, 2, row, row+1, xopt, (AttachOptions) 0);
211         ++row;
212
213         input_latency.set_name ("InputLatency");
214         input_latency.set_flags(Gtk::CAN_FOCUS);
215         input_latency.set_digits(0);
216         input_latency.set_wrap(false);
217         input_latency.set_editable (true);
218
219         label = manage (left_aligned_label (_("Hardware input latency:")));
220         basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
221         basic_packer.attach (input_latency, 1, 2, row, row+1, xopt, (AttachOptions) 0);
222         label = manage (left_aligned_label (_("samples")));
223         basic_packer.attach (*label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
224         ++row;
225
226         output_latency.set_name ("OutputLatency");
227         output_latency.set_flags(Gtk::CAN_FOCUS);
228         output_latency.set_digits(0);
229         output_latency.set_wrap(false);
230         output_latency.set_editable (true);
231
232         label = manage (left_aligned_label (_("Hardware output latency:")));
233         basic_packer.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
234         basic_packer.attach (output_latency, 1, 2, row, row+1, xopt, (AttachOptions) 0);
235         label = manage (left_aligned_label (_("samples")));
236         basic_packer.attach (*label, 2, 3, row, row+1, xopt, (AttachOptions) 0);
237         ++row;
238
239         basic_hbox.pack_start (basic_packer, false, false);
240         basic_vbox.pack_start (basic_hbox, false, false);
241
242         Gtk::HBox* hpacker = manage (new HBox);
243         hpacker->set_border_width (12);
244         hpacker->pack_start (control_app_button, false, false);
245         hpacker->show ();
246         control_app_button.show();
247         basic_vbox.pack_start (*hpacker);
248
249         /* latency measurement tab */
250         
251         lm_title.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Latency Measurement Tool")));
252         
253         row = 0;
254         lm_table.set_row_spacings (12);
255
256         lm_table.attach (lm_title, 0, 2, row, row+1, xopt, (AttachOptions) 0);
257         row++;
258
259         Gtk::Label* preamble;
260
261         preamble = manage (new Label);
262         preamble->set_width_chars (60);
263         preamble->set_line_wrap (true);
264         preamble->set_markup (_("<span weight=\"bold\">Turn down the volume on your hardware to a very low level.</span>"));
265
266         lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
267         row++;
268
269         preamble = manage (new Label);
270         preamble->set_width_chars (60);
271         preamble->set_line_wrap (true);
272         preamble->set_markup (_("Select two channels below and connect them using a cable or (less ideally) a speaker and microphone."));
273
274         lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
275         row++;
276
277         label = manage (new Label (_("Output channel")));
278         lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
279
280         Gtk::Alignment* misc_align = manage (new Alignment (0.0, 0.5));
281         misc_align->add (lm_output_channel_combo);
282         lm_table.attach (*misc_align, 1, 2, row, row+1, xopt, (AttachOptions) 0);
283         ++row;
284
285         label = manage (new Label (_("Input channel")));
286         lm_table.attach (*label, 0, 1, row, row+1, xopt, (AttachOptions) 0);
287
288         misc_align = manage (new Alignment (0.0, 0.5));
289         misc_align->add (lm_input_channel_combo);
290         lm_table.attach (*misc_align, 1, 2, row, row+1, FILL, (AttachOptions) 0);
291         ++row;
292
293         xopt = AttachOptions(0);
294
295         lm_measure_button.add (lm_start_stop_label);
296         
297         lm_measure_button.signal_toggled().connect (sigc::mem_fun (*this, &EngineControl::latency_button_toggled));
298         lm_use_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::use_latency_button_clicked));
299         lm_use_button.set_sensitive (false);
300                 
301
302         preamble = manage (new Label);
303         preamble->set_width_chars (60);
304         preamble->set_line_wrap (true);
305         preamble->set_markup (_("Once the channels are connected, click the \"Measure latency\" button."));
306         lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
307         row++;
308
309         lm_table.attach (lm_measure_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
310         ++row;
311         lm_table.attach (lm_results, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
312         ++row;
313
314
315         preamble = manage (new Label);
316         preamble->set_width_chars (60);
317         preamble->set_line_wrap (true);
318         preamble->set_markup (_("When satisfied with the results, click the \"Use results\" button."));
319         lm_table.attach (*preamble, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), (AttachOptions) 0);
320         row++;
321
322         lm_table.attach (lm_use_button, 0, 2, row, row+1, xopt, (AttachOptions) 0);
323         ++row;
324
325         lm_results.set_markup ("<i>No measurement results yet</i>");
326
327         lm_vbox.set_border_width (12);
328         lm_vbox.pack_start (lm_table, false, false);
329
330         /* pack it all up */
331
332         notebook.pages().push_back (TabElem (basic_vbox, _("Audio")));
333         notebook.pages().push_back (TabElem (midi_vbox, _("MIDI")));
334         notebook.pages().push_back (TabElem (lm_vbox, _("Latency")));
335         notebook.set_border_width (12);
336
337         notebook.set_tab_pos (POS_RIGHT);
338         notebook.show_all ();
339
340         notebook.set_name ("SettingsNotebook");
341
342         /* Connect to signals */
343
344         backend_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::backend_changed));
345         driver_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::driver_changed));
346         sample_rate_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::sample_rate_changed));
347         buffer_size_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::buffer_size_changed));
348         device_combo.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::device_changed));
349
350         input_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
351         output_latency.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
352         input_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
353         output_channels.signal_changed().connect (sigc::mem_fun (*this, &EngineControl::parameter_changed));
354
355         input_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &input_channels));
356         output_channels.signal_output().connect (sigc::bind (sigc::ptr_fun (&EngineControl::print_channel_count), &output_channels));
357
358         notebook.signal_switch_page().connect (sigc::mem_fun (*this, &EngineControl::on_switch_page));
359 }
360
361 EngineControl::~EngineControl ()
362 {
363
364 }
365
366 void
367 EngineControl::disable_latency_tab ()
368 {
369         vector<string> empty;
370         set_popdown_strings (lm_output_channel_combo, empty);
371         set_popdown_strings (lm_input_channel_combo, empty);
372         lm_measure_button.set_sensitive (false);
373         lm_use_button.set_sensitive (false);
374 }
375
376 void
377 EngineControl::enable_latency_tab ()
378 {
379         vector<string> outputs;
380         ARDOUR::AudioEngine::instance()->get_physical_outputs (ARDOUR::DataType::AUDIO, outputs);
381         set_popdown_strings (lm_output_channel_combo, outputs);
382         lm_output_channel_combo.set_active_text (outputs.front());
383
384         vector<string> inputs;
385         ARDOUR::AudioEngine::instance()->get_physical_inputs (ARDOUR::DataType::AUDIO, inputs);
386         set_popdown_strings (lm_input_channel_combo, inputs);
387         lm_input_channel_combo.set_active_text (inputs.front());
388
389         lm_measure_button.set_sensitive (true);
390 }
391
392 void
393 EngineControl::setup_midi_tab_for_backend ()
394 {
395         string backend = backend_combo.get_active_text ();
396
397         Gtkmm2ext::container_clear (midi_vbox);
398
399         midi_vbox.set_border_width (12);
400         midi_device_table.set_border_width (12);
401
402         if (backend == "JACK") {
403                 setup_midi_tab_for_jack ();
404         }
405
406         midi_vbox.pack_start (midi_device_table, true, true);
407         midi_vbox.pack_start (midi_refresh_button, false, false);
408         midi_vbox.show_all ();
409
410         midi_refresh_button.signal_clicked().connect (sigc::mem_fun (*this, &EngineControl::refresh_midi_display));
411 }
412
413 void
414 EngineControl::setup_midi_tab_for_jack ()
415 {
416         midi_vbox.pack_start (aj_button, false, false);
417 }       
418
419 void
420 EngineControl::refresh_midi_display ()
421 {
422         boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
423         assert (backend);
424
425         vector<string> midi_inputs;
426         vector<string> midi_outputs;
427         int row  = 0;
428         AttachOptions xopt = AttachOptions (FILL|EXPAND);
429         Gtk::Label* l;
430
431         Gtkmm2ext::container_clear (midi_device_table);
432
433         backend->get_physical_inputs (ARDOUR::DataType::MIDI, midi_inputs);
434         backend->get_physical_outputs (ARDOUR::DataType::MIDI, midi_outputs);
435
436         midi_device_table.set_spacings (6);
437         midi_device_table.set_homogeneous (true);
438         midi_device_table.resize (midi_inputs.size() + midi_outputs.size() + 3, 1);
439
440         l = manage (new Label);
441         l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Inputs")));
442         midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
443         l->set_alignment (0, 0.5);
444         row++;
445         l->show ();
446         
447         for (vector<string>::iterator p = midi_inputs.begin(); p != midi_inputs.end(); ++p) {
448                 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
449                 l->set_alignment (0, 0.5);
450                 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
451                 l->show ();
452                 row++;
453         }
454
455         row++; // extra row of spacing
456
457         l = manage (new Label);
458         l->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("MIDI Outputs")));
459         midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
460         l->set_alignment (0, 0.5);
461         row++;
462         l->show ();
463
464         for (vector<string>::iterator p = midi_outputs.begin(); p != midi_outputs.end(); ++p) {
465                 l = manage (new Label ((*p).substr ((*p).find_last_of (':') + 1)));
466                 l->set_alignment (0, 0.5);
467                 midi_device_table.attach (*l, 0, 1, row, row + 1, xopt, AttachOptions (0));
468                 l->show ();
469                 row++;
470         }
471 }
472
473 void
474 EngineControl::backend_changed ()
475 {
476         if (ignore_changes) {
477                 return;
478         }
479
480         string backend_name = backend_combo.get_active_text();
481         boost::shared_ptr<ARDOUR::AudioBackend> backend;
482
483         if (!(backend = ARDOUR::AudioEngine::instance()->set_backend (backend_name, "ardour", ""))) {
484                 /* eh? setting the backend failed... how ? */
485                 return;
486         }
487
488         setup_midi_tab_for_backend ();
489
490         if (backend->requires_driver_selection()) {
491                 vector<string> drivers = backend->enumerate_drivers();
492                 driver_combo.set_sensitive (true);
493                 set_popdown_strings (driver_combo, drivers);
494                 driver_combo.set_active_text (drivers.front());
495                 driver_changed ();
496         } else {
497                 driver_combo.set_sensitive (false);
498                 list_devices ();
499         }
500         
501         maybe_display_saved_state ();
502 }
503
504 bool
505 EngineControl::print_channel_count (Gtk::SpinButton* sb)
506 {
507         uint32_t cnt = (uint32_t) sb->get_value();
508         if (cnt == 0) {
509                 sb->set_text (_("all available channels"));
510         } else {
511                 char buf[32];
512                 snprintf (buf, sizeof (buf), "%d", cnt);
513                 sb->set_text (buf);
514         }
515         return true;
516 }
517
518 void
519 EngineControl::list_devices ()
520 {
521         boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
522         assert (backend);
523
524         /* now fill out devices, mark sample rates, buffer sizes insensitive */
525             
526         vector<ARDOUR::AudioBackend::DeviceStatus> all_devices = backend->enumerate_devices ();
527         
528         /* NOTE: Ardour currently does not display the "available" field of the
529          * returned devices.
530          *
531          * Doing so would require a different GUI widget than the combo
532          * box/popdown that we currently use, since it has no way to list
533          * items that are not selectable. Something more like a popup menu,
534          * which could have unselectable items, would be appropriate.
535          */
536
537         vector<string> available_devices;
538
539         for (vector<ARDOUR::AudioBackend::DeviceStatus>::const_iterator i = all_devices.begin(); i != all_devices.end(); ++i) {
540                 available_devices.push_back (i->name);
541         }
542
543         ignore_changes++;
544         set_popdown_strings (device_combo, available_devices);
545         ignore_changes--;
546
547         if (!available_devices.empty()) {
548                 sample_rate_combo.set_sensitive (true);
549                 buffer_size_combo.set_sensitive (true);
550                 input_latency.set_sensitive (true);
551                 output_latency.set_sensitive (true);
552                 input_channels.set_sensitive (true);
553                 output_channels.set_sensitive (true);
554                                                 
555                 /* changing the text in the combo will trigger device_changed()
556                    which should populate the parameter controls
557                 */
558                 
559                 device_combo.set_active_text (available_devices.front());
560         } else {
561                 sample_rate_combo.set_sensitive (true);
562                 buffer_size_combo.set_sensitive (true);
563                 input_latency.set_sensitive (true);
564                 output_latency.set_sensitive (true);
565                 input_channels.set_sensitive (true);
566                 output_channels.set_sensitive (true);
567         }
568 }
569         
570 void
571 EngineControl::driver_changed ()
572 {
573         if (ignore_changes) {
574                 return;
575         }
576
577         boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
578         assert (backend);
579
580         backend->set_driver (driver_combo.get_active_text());
581         list_devices ();
582
583         maybe_display_saved_state ();
584 }
585
586 void
587 EngineControl::device_changed ()
588 {
589         if (ignore_changes) {
590                 return;
591         }
592
593         boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
594         assert (backend);
595         string device_name = device_combo.get_active_text ();
596         vector<string> s;
597
598         /* don't allow programmatic change to sample_rate_combo to cause a
599            recursive call to this method.
600         */
601            
602         ignore_changes++;
603
604         /* sample rates */
605         
606         string desired;
607
608         vector<float> sr = backend->available_sample_rates (device_name);
609         for (vector<float>::const_iterator x = sr.begin(); x != sr.end(); ++x) {
610                 s.push_back (rate_as_string (*x));
611                 if (*x == _desired_sample_rate) {
612                         desired = s.back();
613                 }
614         }
615
616         if (!s.empty()) {
617                 set_popdown_strings (sample_rate_combo, s);
618         
619                 if (desired.empty()) {
620                         sample_rate_combo.set_active_text (s.front());
621                 } else {
622                         sample_rate_combo.set_active_text (desired);
623                 }
624         } else {
625                 /* hmm ... how to tell the user about the fact that we have no
626                  * available sample rates.
627                  */
628         }
629                  
630
631         vector<uint32_t> bs = backend->available_buffer_sizes(device_name);
632         s.clear ();
633         for (vector<uint32_t>::const_iterator x = bs.begin(); x != bs.end(); ++x) {
634                 s.push_back (bufsize_as_string (*x));
635         }
636
637         if (!s.empty()) {
638                 set_popdown_strings (buffer_size_combo, s);
639                 buffer_size_combo.set_active_text (s.front());
640                 show_buffer_duration ();
641         } else {
642                 /* hmm ... how to tell the user about the fact that we have no
643                  * available buffer sizes.
644                  */
645         }
646
647         manage_control_app_sensitivity ();
648
649         ignore_changes--;
650
651         /* pick up any saved state for this device */
652
653         maybe_display_saved_state ();
654
655         /* and push it to the backend */
656
657         push_state_to_backend (false);
658 }       
659
660 string
661 EngineControl::rate_as_string (float r)
662 {
663         char buf[32];
664         if (fmod (r, 1000.0f)) {
665                 snprintf (buf, sizeof (buf), "%.1f kHz", r/1000.0);
666         } else {
667                 snprintf (buf, sizeof (buf), "%.0f kHz", r/1000.0);
668         }
669         return buf;
670 }
671
672 string
673 EngineControl::bufsize_as_string (uint32_t sz)
674 {
675         /* Translators: "samples" is always plural here, so no
676            need for plural+singular forms.
677         */
678         char buf[32];
679         snprintf (buf, sizeof (buf), _("%u samples"), sz);
680         return buf;
681 }
682
683 void 
684 EngineControl::sample_rate_changed ()
685 {
686         if (ignore_changes) {
687                 return;
688         }
689
690         /* reset the strings for buffer size to show the correct msec value
691            (reflecting the new sample rate).
692         */
693
694         show_buffer_duration ();
695         save_state ();
696
697 }
698
699 void 
700 EngineControl::buffer_size_changed ()
701 {
702         if (ignore_changes) {
703                 return;
704         }
705
706         show_buffer_duration ();
707         save_state ();
708 }
709
710 void
711 EngineControl::show_buffer_duration ()
712 {
713
714         /* buffer sizes  - convert from just samples to samples + msecs for
715          * the displayed string
716          */
717
718         string bs_text = buffer_size_combo.get_active_text ();
719         uint32_t samples = atoi (bs_text); /* will ignore trailing text */
720         uint32_t rate = get_rate();
721
722         /* Translators: "msecs" is ALWAYS plural here, so we do not
723            need singular form as well.
724         */
725         /* Developers: note the hard-coding of a double buffered model
726            in the (2 * samples) computation of latency. we always start
727            the audiobackend in this configuration.
728         */
729         char buf[32];
730         snprintf (buf, sizeof (buf), _("(%.1f msecs)"), (2 * samples) / (rate/1000.0));
731         buffer_size_duration_label.set_text (buf);
732 }
733
734 void
735 EngineControl::parameter_changed ()
736 {
737         if (!ignore_changes) {
738                 save_state ();
739         }
740 }
741
742 EngineControl::State*
743 EngineControl::get_matching_state (const string& backend,
744                                    const string& driver,
745                                    const string& device)
746 {
747         for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
748                 if ((*i).backend == backend &&
749                     (*i).driver == driver &&
750                     (*i).device == device) {
751                         return &(*i);
752                 }
753         }
754         return 0;
755 }
756
757 EngineControl::State*
758 EngineControl::get_saved_state_for_currently_displayed_backend_and_device ()
759 {
760         boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
761
762         if (backend) {
763                 return get_matching_state (backend_combo.get_active_text(),
764                                            (backend->requires_driver_selection() ? (std::string) driver_combo.get_active_text() : string()),
765                                            device_combo.get_active_text());
766         }
767
768
769         return get_matching_state (backend_combo.get_active_text(),
770                                    string(),
771                                    device_combo.get_active_text());
772 }
773
774 EngineControl::State*
775 EngineControl::save_state ()
776 {
777         bool existing = true;
778         State* state = get_saved_state_for_currently_displayed_backend_and_device ();
779
780         if (!state) {
781                 existing = false;
782                 state = new State;
783         }
784         
785         store_state (*state);
786
787         if (!existing) {
788                 states.push_back (*state);
789         }
790
791         return state;
792 }
793
794 void
795 EngineControl::store_state (State& state)
796 {
797         state.backend = get_backend ();
798         state.driver = get_driver ();
799         state.device = get_device_name ();
800         state.sample_rate = get_rate ();
801         state.buffer_size = get_buffer_size ();
802         state.input_latency = get_input_latency ();
803         state.output_latency = get_output_latency ();
804         state.input_channels = get_input_channels ();
805         state.output_channels = get_output_channels ();
806 }
807
808 void
809 EngineControl::maybe_display_saved_state ()
810 {
811         State* state = get_saved_state_for_currently_displayed_backend_and_device ();
812
813         if (state) {
814                 ignore_changes++;
815                 if (!_desired_sample_rate) {
816                         sample_rate_combo.set_active_text (rate_as_string (state->sample_rate));
817                 }
818                 buffer_size_combo.set_active_text (bufsize_as_string (state->buffer_size));
819                 /* call this explicitly because we're ignoring changes to
820                    the controls at this point.
821                 */
822                 show_buffer_duration ();
823                 input_latency.set_value (state->input_latency);
824                 output_latency.set_value (state->output_latency);
825                 ignore_changes--;
826         }
827 }
828         
829 XMLNode&
830 EngineControl::get_state ()
831 {
832         XMLNode* root = new XMLNode ("AudioMIDISetup");
833         std::string path;
834
835         if (!states.empty()) {
836                 XMLNode* state_nodes = new XMLNode ("EngineStates");
837                 
838                 for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
839                         
840                         XMLNode* node = new XMLNode ("State");
841                         
842                         node->add_property ("backend", (*i).backend);
843                         node->add_property ("driver", (*i).driver);
844                         node->add_property ("device", (*i).device);
845                         node->add_property ("sample-rate", (*i).sample_rate);
846                         node->add_property ("buffer-size", (*i).buffer_size);
847                         node->add_property ("input-latency", (*i).input_latency);
848                         node->add_property ("output-latency", (*i).output_latency);
849                         node->add_property ("input-channels", (*i).input_channels);
850                         node->add_property ("output-channels", (*i).output_channels);
851                         node->add_property ("active", (*i).active ? "yes" : "no");
852                         
853                         state_nodes->add_child_nocopy (*node);
854                 }
855                 
856                 root->add_child_nocopy (*state_nodes);
857         }
858
859         return *root;
860 }
861
862 void
863 EngineControl::set_state (const XMLNode& root)
864 {
865         XMLNodeList          clist, cclist;
866         XMLNodeConstIterator citer, cciter;
867         XMLNode* child;
868         XMLNode* grandchild;
869         XMLProperty* prop = NULL;
870
871         if (root.name() != "AudioMIDISetup") {
872                 return;
873         }
874
875         clist = root.children();
876
877         states.clear ();
878
879         for (citer = clist.begin(); citer != clist.end(); ++citer) {
880
881                 child = *citer;
882                 
883                 if (child->name() != "EngineStates") {
884                         continue;
885                 }
886
887                 cclist = child->children();
888
889                 for (cciter = cclist.begin(); cciter != cclist.end(); ++cciter) {
890                         State state;
891                         
892                         grandchild = *cciter;
893
894                         if (grandchild->name() != "State") {
895                                 continue;
896                         }
897                         
898                         if ((prop = grandchild->property ("backend")) == 0) {
899                                 continue;
900                         }
901                         state.backend = prop->value ();
902                         
903                         if ((prop = grandchild->property ("driver")) == 0) {
904                                 continue;
905                         }
906                         state.driver = prop->value ();
907                         
908                         if ((prop = grandchild->property ("device")) == 0) {
909                                 continue;
910                         }
911                         state.device = prop->value ();
912                         
913                         if ((prop = grandchild->property ("sample-rate")) == 0) {
914                                 continue;
915                         }
916                         state.sample_rate = atof (prop->value ());
917                         
918                         if ((prop = grandchild->property ("buffer-size")) == 0) {
919                                 continue;
920                         }
921                         state.buffer_size = atoi (prop->value ());
922                         
923                         if ((prop = grandchild->property ("input-latency")) == 0) {
924                                 continue;
925                         }
926                         state.input_latency = atoi (prop->value ());
927                         
928                         if ((prop = grandchild->property ("output-latency")) == 0) {
929                                 continue;
930                         }
931                         state.output_latency = atoi (prop->value ());
932                         
933                         if ((prop = grandchild->property ("input-channels")) == 0) {
934                                 continue;
935                         }
936                         state.input_channels = atoi (prop->value ());
937                         
938                         if ((prop = grandchild->property ("output-channels")) == 0) {
939                                 continue;
940                         }
941                         state.output_channels = atoi (prop->value ());
942
943                         if ((prop = grandchild->property ("active")) == 0) {
944                                 continue;
945                         }
946                         state.active = string_is_affirmative (prop->value ());
947                         
948                         states.push_back (state);
949                 }
950         }
951
952         /* now see if there was an active state and switch the setup to it */
953         
954         for (StateList::const_iterator i = states.begin(); i != states.end(); ++i) {
955                 if ((*i).active) {
956                         ignore_changes++;
957                         backend_combo.set_active_text ((*i).backend);
958                         driver_combo.set_active_text ((*i).driver);
959                         device_combo.set_active_text ((*i).device);
960                         sample_rate_combo.set_active_text (rate_as_string ((*i).sample_rate));
961                         buffer_size_combo.set_active_text (bufsize_as_string ((*i).buffer_size));
962                         input_latency.set_value ((*i).input_latency);
963                         output_latency.set_value ((*i).output_latency);
964                         ignore_changes--;
965                         break;
966                 }
967         }
968 }
969
970
971 int
972 EngineControl::push_state_to_backend (bool start)
973 {
974         boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
975
976         if (!backend) {
977                 return 0;
978         }
979         
980         /* figure out what is going to change */
981
982         bool restart_required = false;
983         bool was_running = ARDOUR::AudioEngine::instance()->running();
984         bool change_driver = false;
985         bool change_device = false;
986         bool change_rate = false;
987         bool change_bufsize = false;
988         bool change_latency = false;
989         bool change_channels = false;
990
991         uint32_t ochan = get_output_channels ();
992         uint32_t ichan = get_input_channels ();
993
994         if (_have_control) {
995                 
996                 /* we can control the backend */
997
998                 if (backend->requires_driver_selection()) {
999                         if (get_driver() != backend->driver_name()) {
1000                                 change_driver = true;
1001                         }
1002                 }
1003
1004                 if (get_device_name() != backend->device_name()) {
1005                         change_device = true;
1006                 }
1007
1008                 if (get_rate() != backend->sample_rate()) {
1009                         change_rate = true;
1010                 }
1011
1012                 if (get_buffer_size() != backend->buffer_size()) {
1013                         change_bufsize = true;
1014                 }
1015
1016                 /* zero-requested channels means "all available" */
1017
1018                 if (ichan == 0) {
1019                         ichan = backend->input_channels();
1020                 }
1021
1022                 if (ochan == 0) {
1023                         ochan = backend->output_channels();
1024                 }
1025
1026                 if (ichan != backend->input_channels()) {
1027                         change_channels = true;
1028                 }
1029
1030                 if (ochan != backend->output_channels()) {
1031                         change_channels = true;
1032                 }
1033
1034                 if (get_input_latency() != backend->systemic_input_latency() ||
1035                     get_output_latency() != backend->systemic_output_latency()) {
1036                         change_latency = true;
1037                 }
1038
1039         } else {
1040
1041                 /* we have no control over the backend, meaning that we can
1042                  * only possibly change sample rate and buffer size.
1043                  */
1044
1045
1046                 if (get_rate() != backend->sample_rate()) {
1047                         change_bufsize = true;
1048                 }
1049
1050                 if (get_buffer_size() != backend->buffer_size()) {
1051                         change_bufsize = true;
1052                 }
1053         }
1054
1055         if (!_have_control) {
1056
1057                 /* We do not have control over the backend, so the best we can
1058                  * do is try to change the sample rate and/or bufsize and get
1059                  * out of here.
1060                  */
1061
1062                 if (change_rate && !backend->can_change_sample_rate_when_running()) {
1063                         return 1;
1064                 }
1065
1066                 if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1067                         return 1;
1068                 }
1069                 
1070                 if (change_rate) {
1071                         backend->set_sample_rate (get_rate());
1072                 }
1073                 
1074                 if (change_bufsize) {
1075                         backend->set_buffer_size (get_buffer_size());
1076                 }
1077
1078                 post_push ();
1079
1080                 return 0;
1081         } 
1082
1083         /* determine if we need to stop the backend before changing parameters */
1084
1085         if (change_driver || change_device || change_channels || change_latency ||
1086             (change_rate && !backend->can_change_sample_rate_when_running()) ||
1087             (change_bufsize && !backend->can_change_buffer_size_when_running())) {
1088                 restart_required = true;
1089         } else {
1090                 restart_required = false;
1091         }
1092
1093         if (was_running) {
1094
1095                 if (!change_driver && !change_device && !change_channels && !change_latency) {
1096                         /* no changes in any parameters that absolutely require a
1097                          * restart, so check those that might be changeable without a
1098                          * restart
1099                          */
1100                         
1101                         if (change_rate && !backend->can_change_sample_rate_when_running()) {
1102                                 /* can't do this while running ... */
1103                                 restart_required = true;
1104                         }
1105
1106                         if (change_bufsize && !backend->can_change_buffer_size_when_running()) {
1107                                 /* can't do this while running ... */
1108                                 restart_required = true;
1109                         }
1110                 }
1111         }
1112
1113         if (was_running) {
1114                 if (restart_required) {
1115                         if (ARDOUR_UI::instance()->disconnect_from_engine ()) {
1116                                 return -1;
1117                         }
1118                 }
1119         }
1120                 
1121
1122         if (change_driver && backend->set_driver (get_driver())) {
1123                 error << string_compose (_("Cannot set driver to %1"), get_driver()) << endmsg;
1124                 return -1;
1125         }
1126         if (change_device && backend->set_device_name (get_device_name())) {
1127                 error << string_compose (_("Cannot set device name to %1"), get_device_name()) << endmsg;
1128                 return -1;
1129         }
1130         if (change_rate && backend->set_sample_rate (get_rate())) {
1131                 error << string_compose (_("Cannot set sample rate to %1"), get_rate()) << endmsg;
1132                 return -1;
1133         }
1134         if (change_bufsize && backend->set_buffer_size (get_buffer_size())) {
1135                 error << string_compose (_("Cannot set buffer size to %1"), get_buffer_size()) << endmsg;
1136                 return -1;
1137         }
1138
1139         if (change_channels || get_input_channels() == 0 || get_output_channels() == 0) {
1140                 if (backend->set_input_channels (get_input_channels())) {
1141                         error << string_compose (_("Cannot set input channels to %1"), get_input_channels()) << endmsg;
1142                         return -1;
1143                 }
1144                 if (backend->set_output_channels (get_output_channels())) {
1145                         error << string_compose (_("Cannot set output channels to %1"), get_output_channels()) << endmsg;
1146                         return -1;
1147                 }
1148         }
1149         if (change_latency) {
1150                 if (backend->set_systemic_input_latency (get_input_latency())) {
1151                         error << string_compose (_("Cannot set input latency to %1"), get_input_latency()) << endmsg;
1152                         return -1;
1153                 }
1154                 if (backend->set_systemic_output_latency (get_output_latency())) {
1155                         error << string_compose (_("Cannot set output latency to %1"), get_output_latency()) << endmsg;
1156                         return -1;
1157                 }
1158         }
1159                         
1160         if (start || (was_running && restart_required)) {
1161                 if (ARDOUR_UI::instance()->reconnect_to_engine()) {
1162                         return -1;
1163                 }
1164         }
1165         
1166         post_push ();
1167
1168         return 0;
1169 }
1170
1171 void
1172 EngineControl::post_push ()
1173 {
1174         /* get a pointer to the current state object, creating one if
1175          * necessary
1176          */
1177         
1178         State* state = get_saved_state_for_currently_displayed_backend_and_device ();
1179         
1180         if (!state) {
1181                 state = save_state ();
1182                 assert (state);
1183         }
1184         
1185         /* all off */
1186         
1187         for (StateList::iterator i = states.begin(); i != states.end(); ++i) {
1188                 (*i).active = false;
1189         }
1190         
1191         /* mark this one active (to be used next time the dialog is
1192          * shown)
1193          */
1194         
1195         state->active = true;
1196
1197         manage_control_app_sensitivity ();
1198
1199         /* schedule a redisplay of MIDI ports */
1200         
1201         Glib::signal_timeout().connect (sigc::bind_return (sigc::mem_fun (*this, &EngineControl::refresh_midi_display), false), 1000);
1202 }
1203
1204
1205 float
1206 EngineControl::get_rate () const
1207 {
1208         float r = atof (sample_rate_combo.get_active_text ());
1209         /* the string may have been translated with an abbreviation for
1210          * thousands, so use a crude heuristic to fix this.
1211          */
1212         if (r < 1000.0) {
1213                 r *= 1000.0;
1214         }
1215         return r;
1216 }
1217         
1218
1219 uint32_t
1220 EngineControl::get_buffer_size () const
1221 {
1222         string txt = buffer_size_combo.get_active_text ();
1223         uint32_t samples;
1224
1225         if (sscanf (txt.c_str(), "%d", &samples) != 1) {
1226                 throw exception ();
1227         }
1228
1229         return samples;
1230 }
1231
1232 uint32_t
1233 EngineControl::get_input_channels() const
1234 {
1235         return (uint32_t) input_channels_adjustment.get_value();
1236 }
1237
1238 uint32_t
1239 EngineControl::get_output_channels() const
1240 {
1241         return (uint32_t) output_channels_adjustment.get_value();
1242 }
1243
1244 uint32_t
1245 EngineControl::get_input_latency() const
1246 {
1247         return (uint32_t) input_latency_adjustment.get_value();
1248 }
1249
1250 uint32_t
1251 EngineControl::get_output_latency() const
1252 {
1253         return (uint32_t) output_latency_adjustment.get_value();
1254 }
1255
1256 string
1257 EngineControl::get_backend () const
1258 {
1259         return backend_combo.get_active_text ();
1260 }
1261
1262 string
1263 EngineControl::get_driver () const
1264 {
1265         return driver_combo.get_active_text ();
1266 }
1267
1268 string
1269 EngineControl::get_device_name () const
1270 {
1271         return device_combo.get_active_text ();
1272 }
1273
1274 void
1275 EngineControl::control_app_button_clicked ()
1276 {
1277         boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1278         
1279         if (!backend) {
1280                 return;
1281         }
1282         
1283         backend->launch_control_app ();
1284 }
1285
1286 void
1287 EngineControl::manage_control_app_sensitivity ()
1288 {
1289         boost::shared_ptr<ARDOUR::AudioBackend> backend = ARDOUR::AudioEngine::instance()->current_backend();
1290         
1291         if (!backend) {
1292                 return;
1293         }
1294         
1295         string appname = backend->control_app_name();
1296
1297         if (appname.empty()) {
1298                 control_app_button.set_sensitive (false);
1299         } else {
1300                 control_app_button.set_sensitive (true);
1301         }
1302 }
1303
1304 void
1305 EngineControl::set_desired_sample_rate (uint32_t sr)
1306 {
1307         _desired_sample_rate = sr;
1308         device_changed ();
1309 }
1310
1311 void
1312 EngineControl::on_switch_page (GtkNotebookPage*, guint page_num)
1313 {
1314         if (page_num == 0) {
1315                 cancel_button->set_sensitive (true);
1316                 ok_button->set_sensitive (true);
1317                 apply_button->set_sensitive (true);
1318         } else {
1319                 cancel_button->set_sensitive (false);
1320                 ok_button->set_sensitive (false);
1321                 apply_button->set_sensitive (false);
1322         }
1323
1324         if (page_num == 1) {
1325                 /* MIDI tab */
1326                 refresh_midi_display ();
1327         }
1328
1329         if (page_num == 2) {
1330                 /* latency tab */
1331
1332                 if (!ARDOUR::AudioEngine::instance()->running()) {
1333                         
1334                         PBD::Unwinder<uint32_t> protect_ignore_changes (ignore_changes, ignore_changes + 1);
1335                         
1336                         /* save any existing latency values */
1337                         
1338                         uint32_t il = (uint32_t) input_latency.get_value ();
1339                         uint32_t ol = (uint32_t) input_latency.get_value ();
1340
1341                         /* reset to zero so that our new test instance of JACK
1342                            will be clean of any existing latency measures.
1343                         */
1344                         
1345                         input_latency.set_value (0);
1346                         output_latency.set_value (0);
1347                         
1348                         /* reset control */
1349
1350                         input_latency.set_value (il);
1351                         output_latency.set_value (ol);
1352
1353                 } 
1354
1355                 if (ARDOUR::AudioEngine::instance()->prepare_for_latency_measurement()) {
1356                         disable_latency_tab ();
1357                 }
1358
1359                 enable_latency_tab ();
1360
1361         } else {
1362                 ARDOUR::AudioEngine::instance()->stop_latency_detection();
1363         }
1364 }
1365
1366 /* latency measurement */
1367
1368 bool
1369 EngineControl::check_latency_measurement ()
1370 {
1371         MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1372
1373         if (mtdm->resolve () < 0) {
1374                 lm_results.set_markup (string_compose ("<span foreground=\"red\">%1</span>", _("No signal detected ")));
1375                 return true;
1376         }
1377
1378         if (mtdm->err () > 0.3) {
1379                 mtdm->invert ();
1380                 mtdm->resolve ();
1381         }
1382
1383         char buf[128];
1384         ARDOUR::framecnt_t const sample_rate = ARDOUR::AudioEngine::instance()->sample_rate();
1385
1386         if (sample_rate == 0) {
1387                 lm_results.set_text (_("Disconnected from audio engine"));
1388                 ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1389                 return false;
1390         }
1391
1392         uint32_t frames_total = mtdm->del();
1393         uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1394
1395         snprintf (buf, sizeof (buf), "%u samples %10.3lf ms", extra, extra * 1000.0f/sample_rate);
1396
1397         bool solid = true;
1398
1399         if (mtdm->err () > 0.2) {
1400                 strcat (buf, " ??");
1401                 solid = false;
1402         }
1403
1404         if (mtdm->inv ()) {
1405                 strcat (buf, " (Inv)");
1406                 solid = false;
1407         }
1408
1409         if (solid) {
1410                 lm_measure_button.set_active (false);
1411                 lm_use_button.set_sensitive (true);
1412                 strcat (buf, " (set)");
1413                 have_lm_results = true;
1414         }
1415         
1416         lm_results.set_text (buf);
1417
1418         return true;
1419 }
1420
1421 void
1422 EngineControl::start_latency_detection ()
1423 {
1424         ARDOUR::AudioEngine::instance()->set_latency_input_port (lm_input_channel_combo.get_active_text());
1425         ARDOUR::AudioEngine::instance()->set_latency_output_port (lm_output_channel_combo.get_active_text());
1426         ARDOUR::AudioEngine::instance()->start_latency_detection ();
1427         lm_results.set_text (_("Detecting ..."));
1428         latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &EngineControl::check_latency_measurement), 250);
1429         lm_start_stop_label.set_text (_("Cancel measurement"));
1430         have_lm_results = false;
1431         lm_input_channel_combo.set_sensitive (false);
1432         lm_output_channel_combo.set_sensitive (false);
1433 }
1434
1435 void
1436 EngineControl::end_latency_detection ()
1437 {
1438         ARDOUR::AudioEngine::instance()->stop_latency_detection ();
1439         latency_timeout.disconnect ();
1440         lm_start_stop_label.set_text (_("Measure latency"));
1441         if (!have_lm_results) {
1442                 lm_results.set_markup ("<i>No measurement results yet</i>");
1443         }
1444         lm_input_channel_combo.set_sensitive (true);
1445         lm_output_channel_combo.set_sensitive (true);
1446 }
1447
1448 void
1449 EngineControl::latency_button_toggled ()
1450 {
1451         if (lm_measure_button.get_active ()) {
1452                 start_latency_detection ();
1453         } else {
1454                 end_latency_detection ();
1455         }
1456 }
1457
1458 void
1459 EngineControl::use_latency_button_clicked ()
1460 {
1461         MTDM* mtdm = ARDOUR::AudioEngine::instance()->mtdm ();
1462
1463         if (!mtdm) {
1464                 return;
1465         }
1466
1467         uint32_t frames_total = mtdm->del();
1468         uint32_t extra = frames_total - ARDOUR::AudioEngine::instance()->latency_signal_delay();
1469         uint32_t one_way = extra/2;
1470
1471         input_latency_adjustment.set_value (one_way);
1472         output_latency_adjustment.set_value (one_way);
1473 }
1474
1475 bool
1476 EngineControl::on_delete_event (GdkEventAny* ev)
1477 {
1478         if (notebook.get_current_page() == 2) {
1479                 /* currently on latency tab - be sure to clean up */
1480                 end_latency_detection ();
1481         }
1482         return ArdourDialog::on_delete_event (ev);
1483 }
1484