clarify loudness analysis/normalization
[ardour.git] / gtk2_ardour / export_format_dialog.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "ardour/session.h"
22 #include "ardour/export_format_specification.h"
23
24 #include "export_format_dialog.h"
25 #include "gui_thread.h"
26 #include "tooltips.h"
27 #include "i18n.h"
28
29 using namespace ARDOUR;
30
31 ExportFormatDialog::ExportFormatDialog (FormatPtr format, bool new_dialog) :
32   ArdourDialog (new_dialog ? _("New Export Format Profile") : _("Edit Export Format Profile")),
33   format (format),
34   manager (format),
35   original_state (format->get_state()),
36
37   applying_changes_from_engine (0),
38
39   name_label (_("Label: "), Gtk::ALIGN_LEFT),
40   name_generated_part ("", Gtk::ALIGN_LEFT),
41
42   normalize_checkbox (_("Normalize:")),
43   normalize_peak_rb (_("Peak")),
44   normalize_loudness_rb (_("Loudness")),
45   normalize_dbfs_adjustment ( 0.00, -90.00, 0.00, 0.1, 0.2),
46   normalize_lufs_adjustment (-23.0, -90.00, 0.00, 0.1, 1.0),
47   normalize_dbtp_adjustment ( -1.0, -90.00, 0.00, 0.1, 0.2),
48
49   normalize_dbfs_label (_("dBFS"), Gtk::ALIGN_LEFT),
50   normalize_lufs_label (_("LUFS"), Gtk::ALIGN_LEFT),
51   normalize_dbtp_label (_("dBTP"), Gtk::ALIGN_LEFT),
52
53   silence_table (2, 4),
54   trim_start_checkbox (_("Trim silence at start")),
55   silence_start_checkbox (_("Add silence at start:")),
56   silence_start_clock ("silence_start", true, "", true, false, true),
57
58   trim_end_checkbox (_("Trim silence at end")),
59   silence_end_checkbox (_("Add silence at end:")),
60   silence_end_clock ("silence_end", true, "", true, false, true),
61
62   command_label(_("Command to run post-export\n(%f=full path & filename, %d=directory, %b=basename):"), Gtk::ALIGN_LEFT),
63
64   format_table (3, 4),
65   compatibility_label (_("Compatibility"), Gtk::ALIGN_LEFT),
66   quality_label (_("Quality"), Gtk::ALIGN_LEFT),
67   format_label (_("File format"), Gtk::ALIGN_LEFT),
68   sample_rate_label (_("Sample rate"), Gtk::ALIGN_LEFT),
69   src_quality_label (_("Sample rate conversion quality:"), Gtk::ALIGN_RIGHT),
70
71   encoding_options_label ("", Gtk::ALIGN_LEFT),
72
73   /* Changing encoding options from here on */
74
75   sample_format_label (_("Sample Format"), Gtk::ALIGN_LEFT),
76   dither_label (_("Dithering"), Gtk::ALIGN_LEFT),
77
78   with_cue (_("Create CUE file for disk-at-once CD/DVD creation")),
79   with_toc (_("Create TOC file for disk-at-once CD/DVD creation")),
80   with_mp4chaps (_("Create chapter mark file for MP4 chapter marks")),
81
82   tag_checkbox (_("Tag file with session's metadata"))
83 {
84
85         /* Pack containers in dialog */
86
87         get_vbox()->pack_start (silence_table, false, false, 6);
88         get_vbox()->pack_start (format_table, false, false, 6);
89         get_vbox()->pack_start (encoding_options_vbox, false, false, 0);
90         get_vbox()->pack_start (cue_toc_vbox, false, false, 0);
91         get_vbox()->pack_start (name_hbox, false, false, 6);
92
93         /* Name, new and remove */
94
95         name_hbox.pack_start (name_label, false, false, 0);
96         name_hbox.pack_start (name_entry, false, false, 0);
97         name_hbox.pack_start (name_generated_part, true, true, 0);
98         name_entry.set_width_chars(20);
99         update_description();
100         manager.DescriptionChanged.connect(
101                 *this, invalidator (*this),
102                 boost::bind (&ExportFormatDialog::update_description, this), gui_context());
103
104         /* Normalize */
105
106         Gtk::RadioButtonGroup normalize_group = normalize_loudness_rb.get_group();
107         normalize_peak_rb.set_group (normalize_group);
108
109         normalize_hbox.pack_start (normalize_checkbox, false, false, 2);
110         normalize_hbox.pack_start (normalize_peak_rb, false, false, 0);
111         normalize_hbox.pack_start (normalize_dbfs_spinbutton, false, false, 2);
112         normalize_hbox.pack_start (normalize_dbfs_label, false, false, 0);
113
114         normalize_hbox.pack_start (*Gtk::manage (new Gtk::Label ("")), false, false, 6); // separator
115
116         normalize_hbox.pack_start (normalize_loudness_rb, false, false, 0);
117         normalize_hbox.pack_start (normalize_lufs_spinbutton, false, false, 2);
118         normalize_hbox.pack_start (normalize_lufs_label, false, false, 0);
119         normalize_hbox.pack_start (*Gtk::manage (new Gtk::Label (_("\u2227"))), false, false, 4);
120         normalize_hbox.pack_start (normalize_dbtp_spinbutton, false, false, 2);
121         normalize_hbox.pack_start (normalize_dbtp_label, false, false, 0);
122
123         ARDOUR_UI_UTILS::set_tooltip (normalize_loudness_rb,
124                         _("Normalize loudness to not exceed given EBU-R128/RMS LUFS and given true-peak. EBU-R128 normalization is only available for mono and stereo targets."));
125
126         normalize_dbfs_spinbutton.configure (normalize_dbfs_adjustment, 0.1, 2);
127         normalize_lufs_spinbutton.configure (normalize_lufs_adjustment, 0.1, 2);
128         normalize_dbtp_spinbutton.configure (normalize_dbtp_adjustment, 0.1, 2);
129
130         /* Silence  */
131
132         silence_table.set_row_spacings (6);
133         silence_table.set_col_spacings (12);
134
135         silence_table.attach (normalize_hbox, 0, 3, 0, 1);
136
137         silence_table.attach (trim_start_checkbox, 0, 1, 1, 2);
138         silence_table.attach (silence_start_checkbox, 1, 2, 1, 2);
139         silence_table.attach (silence_start_clock, 2, 3, 1, 2);
140
141         silence_table.attach (trim_end_checkbox, 0, 1, 2, 3);
142         silence_table.attach (silence_end_checkbox, 1, 2, 2, 3);
143         silence_table.attach (silence_end_clock, 2, 3, 2, 3);
144
145         /* Post-export hook script */
146
147         get_vbox()->pack_start (command_label, false, false);
148         get_vbox()->pack_start (command_entry, false, false);
149
150         /* Format table */
151
152         init_format_table();
153
154         /* Encoding options */
155
156         init_encoding_option_widgets();
157
158         encoding_options_table.set_spacings (1);
159
160         encoding_options_vbox.pack_start (encoding_options_label, false, false, 0);
161         encoding_options_vbox.pack_start (encoding_options_table, false, false, 12);
162
163         Pango::AttrList bold;
164         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
165         bold.insert (b);
166         encoding_options_label.set_attributes (bold);
167
168         /* Buttons */
169
170         revert_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
171         revert_button->signal_clicked().connect (sigc::mem_fun(*this, &ExportFormatDialog::revert));
172         close_button = add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_APPLY);
173         close_button->set_sensitive (false);
174         close_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportFormatDialog::end_dialog));
175         manager.CompleteChanged.connect (*this, invalidator (*this), boost::bind (&Gtk::Button::set_sensitive, close_button, _1), gui_context());
176
177         with_cue.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_with_cue));
178         with_toc.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_with_toc));
179         with_mp4chaps.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_with_mp4chaps));
180         command_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_command));
181
182         cue_toc_vbox.pack_start (with_cue, false, false);
183         cue_toc_vbox.pack_start (with_toc, false, false);
184         cue_toc_vbox.pack_start (with_mp4chaps, false, false);
185
186         /* Load state before hooking up the rest of the signals */
187
188         load_state (format);
189
190         /* Name entry */
191
192         name_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_name));
193
194         /* Normalize, silence and src_quality signals */
195
196         trim_start_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_trim_start_selection));
197         trim_end_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_trim_end_selection));
198
199         normalize_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
200         normalize_peak_rb.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
201         normalize_loudness_rb.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
202         normalize_dbfs_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
203         normalize_lufs_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
204         normalize_dbtp_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
205
206         silence_start_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_start_selection));
207         silence_start_clock.ValueChanged.connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_start_selection));
208
209         silence_end_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_end_selection));
210         silence_end_clock.ValueChanged.connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_end_selection));
211
212         src_quality_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_src_quality_selection));
213
214         /* Format table signals */
215
216         Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(compatibility_view.get_column_cell_renderer (0));
217         toggle->signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_compatibility_selection));
218         compatibility_select_connection = compatibility_view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::prohibit_compatibility_selection));
219
220         quality_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_quality_selection));
221         format_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_format_selection));
222         sample_rate_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_sample_rate_selection));
223
224         /* Encoding option signals */
225
226         sample_format_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_sample_format_selection));
227         dither_type_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_dither_type_selection));
228
229         tag_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_tagging_selection));
230
231         /* Finalize */
232
233         show_all_children();
234         update_normalize_sensitivity ();
235 }
236
237 ExportFormatDialog::~ExportFormatDialog ()
238 {
239
240 }
241
242 void
243 ExportFormatDialog::revert ()
244 {
245         ++applying_changes_from_engine;
246
247         format->set_state (original_state);
248         load_state (format);
249
250         --applying_changes_from_engine;
251 }
252
253 void
254 ExportFormatDialog::set_session (ARDOUR::Session* s)
255 {
256         SessionHandlePtr::set_session (s);
257         silence_start_clock.set_session (s);
258         silence_end_clock.set_session (s);
259
260         if (!_session) {
261                 return;
262         }
263
264         update_clock (silence_start_clock, silence_start);
265         update_clock (silence_end_clock, silence_end);
266
267         /* Select native samplerate if no selection is yet made */
268
269         if (sample_rate_view.get_selection()->count_selected_rows() == 0) {
270                 Gtk::ListStore::Children::iterator it;
271                 for (it = sample_rate_list->children().begin(); it != sample_rate_list->children().end(); ++it) {
272                         if ((framecnt_t) (*it)->get_value (sample_rate_cols.ptr)->rate == _session->nominal_frame_rate()) {
273                                 sample_rate_view.get_selection()->select (it);
274                                 break;
275                         }
276                 }
277         }
278 }
279
280 void
281 ExportFormatDialog::load_state (FormatPtr spec)
282 {
283         name_entry.set_text (spec->name());
284
285         normalize_checkbox.set_active (spec->normalize());
286         normalize_peak_rb.set_active (!spec->normalize_loudness());
287         normalize_loudness_rb.set_active (spec->normalize_loudness());
288         normalize_dbfs_spinbutton.set_value (spec->normalize_dbfs());
289         normalize_lufs_spinbutton.set_value (spec->normalize_lufs());
290         normalize_dbtp_spinbutton.set_value (spec->normalize_dbtp());
291
292         trim_start_checkbox.set_active (spec->trim_beginning());
293         silence_start = spec->silence_beginning_time();
294         silence_start_checkbox.set_active (spec->silence_beginning_time().not_zero());
295
296         trim_end_checkbox.set_active (spec->trim_end());
297         silence_end = spec->silence_end_time();
298         silence_end_checkbox.set_active (spec->silence_end_time().not_zero());
299
300         with_cue.set_active (spec->with_cue());
301         with_toc.set_active (spec->with_toc());
302         with_mp4chaps.set_active (spec->with_mp4chaps());
303
304         for (Gtk::ListStore::Children::iterator it = src_quality_list->children().begin(); it != src_quality_list->children().end(); ++it) {
305                 if (it->get_value (src_quality_cols.id) == spec->src_quality()) {
306                         src_quality_combo.set_active (it);
307                         break;
308                 }
309         }
310
311         for (Gtk::ListStore::Children::iterator it = format_list->children().begin(); it != format_list->children().end(); ++it) {
312                 boost::shared_ptr<ARDOUR::ExportFormat> format_in_list = it->get_value (format_cols.ptr);
313                 if (format_in_list->get_format_id() == spec->format_id() &&
314                     // BWF has the same format id with wav, so we need to check this.
315                     format_in_list->has_broadcast_info() == spec->has_broadcast_info()) {
316
317                         format_in_list->set_selected (true);
318                         break;
319                 }
320         }
321
322         for (Gtk::ListStore::Children::iterator it = sample_rate_list->children().begin(); it != sample_rate_list->children().end(); ++it) {
323                 if (it->get_value (sample_rate_cols.ptr)->rate == spec->sample_rate()) {
324                         it->get_value (sample_rate_cols.ptr)->set_selected (true);
325                         break;
326                 }
327         }
328
329         if (spec->sample_format()) {
330                 for (Gtk::ListStore::Children::iterator it = sample_format_list->children().begin(); it != sample_format_list->children().end(); ++it) {
331                         if (it->get_value (sample_format_cols.ptr)->format == spec->sample_format()) {
332                                 it->get_value (sample_format_cols.ptr)->set_selected (true);
333                                 break;
334                         }
335                 }
336
337                 for (Gtk::ListStore::Children::iterator it = dither_type_list->children().begin(); it != dither_type_list->children().end(); ++it) {
338                         if (it->get_value (dither_type_cols.ptr)->type == spec->dither_type()) {
339                                 it->get_value (dither_type_cols.ptr)->set_selected (true);
340                                 break;
341                         }
342                 }
343         }
344
345         update_normalize_sensitivity ();
346         tag_checkbox.set_active (spec->tag());
347         command_entry.set_text (spec->command());
348 }
349
350 void
351 ExportFormatDialog::init_format_table ()
352 {
353
354         format_table.set_spacings (1);
355
356         format_table.attach (compatibility_label, 0, 1, 0, 1);
357         format_table.attach (quality_label, 1, 2, 0, 1);
358         format_table.attach (format_label, 2, 3, 0, 1);
359         format_table.attach (sample_rate_label, 3, 4, 0, 1);
360
361         format_table.attach (compatibility_view, 0, 1, 1, 2);
362         format_table.attach (quality_view, 1, 2, 1, 2);
363         format_table.attach (format_view, 2, 3, 1, 2);
364         format_table.attach (sample_rate_view, 3, 4, 1, 2);
365
366         format_table.attach (src_quality_label, 0, 3, 2, 3);
367         format_table.attach (src_quality_combo, 3, 4, 2, 3);
368
369         compatibility_view.set_headers_visible (false);
370         quality_view.set_headers_visible (false);
371         format_view.set_headers_visible (false);
372         sample_rate_view.set_headers_visible (false);
373
374         /*** Table entries ***/
375
376         Gtk::TreeModel::iterator iter;
377         Gtk::TreeModel::Row row;
378
379         /* Compatibilities */
380
381         compatibility_list = Gtk::ListStore::create (compatibility_cols);
382         compatibility_view.set_model (compatibility_list);
383
384         ExportFormatManager::CompatList const & compat_list = manager.get_compatibilities();
385
386         for (ExportFormatManager::CompatList::const_iterator it = compat_list.begin(); it != compat_list.end(); ++it) {
387                 iter = compatibility_list->append();
388                 row = *iter;
389
390                 row[compatibility_cols.ptr] = *it;
391                 row[compatibility_cols.selected] = false;
392                 row[compatibility_cols.label] = (*it)->name();
393
394                 WeakCompatPtr ptr (*it);
395                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_compatibility_selection, this, _1, ptr), gui_context());
396         }
397
398         compatibility_view.append_column_editable ("", compatibility_cols.selected);
399
400         Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
401         text_renderer->property_editable() = false;
402
403         Gtk::TreeView::Column* column = compatibility_view.get_column (0);
404         column->pack_start (*text_renderer);
405         column->add_attribute (text_renderer->property_text(), compatibility_cols.label);
406
407         /* Qualities */
408
409         quality_list = Gtk::ListStore::create (quality_cols);
410         quality_view.set_model (quality_list);
411
412         ExportFormatManager::QualityList const & qualities = manager.get_qualities ();
413
414         for (ExportFormatManager::QualityList::const_iterator it = qualities.begin(); it != qualities.end(); ++it) {
415                 iter = quality_list->append();
416                 row = *iter;
417
418                 row[quality_cols.ptr] = *it;
419                 row[quality_cols.color] = "white";
420                 row[quality_cols.label] = (*it)->name();
421
422                 WeakQualityPtr ptr (*it);
423                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_quality_selection, this, _1, ptr), gui_context());
424                 (*it)->CompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_quality_compatibility, this, _1, ptr), gui_context());
425         }
426
427         quality_view.append_column ("", quality_cols.label);
428
429         /* Formats */
430
431         format_list = Gtk::ListStore::create (format_cols);
432         format_view.set_model (format_list);
433
434         ExportFormatManager::FormatList const & formats = manager.get_formats ();
435
436         for (ExportFormatManager::FormatList::const_iterator it = formats.begin(); it != formats.end(); ++it) {
437                 iter = format_list->append();
438                 row = *iter;
439
440                 row[format_cols.ptr] = *it;
441                 row[format_cols.color] = "white";
442                 row[format_cols.label] = (*it)->name();
443
444                 WeakFormatPtr ptr (*it);
445                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_format_selection, this, _1, ptr), gui_context());
446                 (*it)->CompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_format_compatibility, this, _1, ptr), gui_context());
447
448                 /* Encoding options */
449
450                 boost::shared_ptr<HasSampleFormat> hsf;
451
452                 if ((hsf = boost::dynamic_pointer_cast<HasSampleFormat> (*it))) {
453                         hsf->SampleFormatSelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_format_selection, this, _1, _2), gui_context());
454                         hsf->SampleFormatCompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_format_compatibility, this, _1, _2), gui_context());
455
456                         hsf->DitherTypeSelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_dither_type_selection, this, _1, _2), gui_context());
457                         hsf->DitherTypeCompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_dither_type_compatibility, this, _1, _2), gui_context());
458                 }
459         }
460
461         format_view.append_column ("", format_cols.label);
462
463         /* Sample Rates */
464
465         sample_rate_list = Gtk::ListStore::create (sample_rate_cols);
466         sample_rate_view.set_model (sample_rate_list);
467
468         ExportFormatManager::SampleRateList const & rates = manager.get_sample_rates ();
469
470         for (ExportFormatManager::SampleRateList::const_iterator it = rates.begin(); it != rates.end(); ++it) {
471                 iter = sample_rate_list->append();
472                 row = *iter;
473
474                 row[sample_rate_cols.ptr] = *it;
475                 row[sample_rate_cols.color] = "white";
476                 row[sample_rate_cols.label] = (*it)->name();
477
478                 WeakSampleRatePtr ptr (*it);
479                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_rate_selection, this, _1, ptr), gui_context());
480                 (*it)->CompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_rate_compatibility, this, _1, ptr), gui_context());
481         }
482
483         sample_rate_view.append_column ("", sample_rate_cols.label);
484
485         /* Color rendering */
486
487         Gtk::TreeViewColumn * label_col;
488         Gtk::CellRendererText * renderer;
489
490         label_col = quality_view.get_column(0);
491         renderer = dynamic_cast<Gtk::CellRendererText*> (quality_view.get_column_cell_renderer (0));
492         label_col->add_attribute(renderer->property_foreground(), quality_cols.color);
493
494         label_col = format_view.get_column(0);
495         renderer = dynamic_cast<Gtk::CellRendererText*> (format_view.get_column_cell_renderer (0));
496         label_col->add_attribute(renderer->property_foreground(), format_cols.color);
497
498         label_col = sample_rate_view.get_column(0);
499         renderer = dynamic_cast<Gtk::CellRendererText*> (sample_rate_view.get_column_cell_renderer (0));
500         label_col->add_attribute(renderer->property_foreground(), sample_rate_cols.color);
501
502         /* SRC Qualities */
503
504         src_quality_list = Gtk::ListStore::create (src_quality_cols);
505         src_quality_combo.set_model (src_quality_list);
506
507         iter = src_quality_list->append();
508         row = *iter;
509         row[src_quality_cols.id] = ExportFormatBase::SRC_SincBest;
510         row[src_quality_cols.label] = _("Best (sinc)");
511
512         iter = src_quality_list->append();
513         row = *iter;
514         row[src_quality_cols.id] = ExportFormatBase::SRC_SincMedium;
515         row[src_quality_cols.label] = _("Medium (sinc)");
516
517         iter = src_quality_list->append();
518         row = *iter;
519         row[src_quality_cols.id] = ExportFormatBase::SRC_SincFast;
520         row[src_quality_cols.label] = _("Fast (sinc)");
521
522         iter = src_quality_list->append();
523         row = *iter;
524         row[src_quality_cols.id] = ExportFormatBase::SRC_Linear;
525         row[src_quality_cols.label] = _("Linear");
526
527         iter = src_quality_list->append();
528         row = *iter;
529         row[src_quality_cols.id] = ExportFormatBase::SRC_ZeroOrderHold;
530         row[src_quality_cols.label] = _("Zero order hold");
531
532         src_quality_combo.pack_start (src_quality_cols.label);
533         src_quality_combo.set_active (0);
534 }
535
536 void
537 ExportFormatDialog::init_encoding_option_widgets ()
538 {
539         Gtk::TreeViewColumn * label_col;
540         Gtk::CellRendererText * renderer;
541
542         sample_format_list = Gtk::ListStore::create (sample_format_cols);
543         sample_format_view.set_model (sample_format_list);
544         sample_format_view.set_headers_visible (false);
545         sample_format_view.append_column ("", sample_format_cols.label);
546         label_col = sample_format_view.get_column(0);
547         renderer = dynamic_cast<Gtk::CellRendererText*> (sample_format_view.get_column_cell_renderer (0));
548         label_col->add_attribute(renderer->property_foreground(), sample_format_cols.color);
549
550         dither_type_list = Gtk::ListStore::create (dither_type_cols);
551         dither_type_view.set_model (dither_type_list);
552         dither_type_view.set_headers_visible (false);
553         dither_type_view.append_column ("", dither_type_cols.label);
554         label_col = dither_type_view.get_column(0);
555         renderer = dynamic_cast<Gtk::CellRendererText*> (dither_type_view.get_column_cell_renderer (0));
556         label_col->add_attribute(renderer->property_foreground(), dither_type_cols.color);
557
558 }
559
560 void
561 ExportFormatDialog::update_compatibility_selection (std::string const & path)
562 {
563
564         Gtk::TreeModel::iterator iter = compatibility_view.get_model ()->get_iter (path);
565         ExportFormatCompatibilityPtr ptr = iter->get_value (compatibility_cols.ptr);
566         bool state = iter->get_value (compatibility_cols.selected);
567
568         iter->set_value (compatibility_cols.selected, state);
569         ptr->set_selected (state);
570
571 }
572
573 void
574 ExportFormatDialog::update_quality_selection ()
575 {
576         update_selection<QualityCols> (quality_list, quality_view, quality_cols);
577 }
578
579 void
580 ExportFormatDialog::update_format_selection ()
581 {
582         update_selection<FormatCols> (format_list, format_view, format_cols);
583 }
584
585 void
586 ExportFormatDialog::update_sample_rate_selection ()
587 {
588         update_selection<SampleRateCols> (sample_rate_list, sample_rate_view, sample_rate_cols);
589 }
590
591 void
592 ExportFormatDialog::update_sample_format_selection ()
593 {
594         update_selection<SampleFormatCols> (sample_format_list, sample_format_view, sample_format_cols);
595 }
596
597 void
598 ExportFormatDialog::update_dither_type_selection ()
599 {
600         update_selection<DitherTypeCols> (dither_type_list, dither_type_view, dither_type_cols);
601 }
602
603 template<typename ColsT>
604 void
605 ExportFormatDialog::update_selection (Glib::RefPtr<Gtk::ListStore> & list, Gtk::TreeView & view, ColsT & cols)
606 {
607         if (applying_changes_from_engine) {
608                 return;
609         }
610
611         Gtk::ListStore::Children::iterator it;
612         Glib::RefPtr<Gtk::TreeSelection> selection = view.get_selection();
613
614         for (it = list->children().begin(); it != list->children().end(); ++it) {
615                 bool selected = selection->is_selected (it);
616                 it->get_value (cols.ptr)->set_selected (selected);
617         }
618 }
619
620 void
621 ExportFormatDialog::change_compatibility_selection (bool select, WeakCompatPtr compat)
622 {
623         ++applying_changes_from_engine;
624
625         ExportFormatCompatibilityPtr ptr = compat.lock();
626
627         for (Gtk::ListStore::Children::iterator it = compatibility_list->children().begin(); it != compatibility_list->children().end(); ++it) {
628                 if (it->get_value (compatibility_cols.ptr) == ptr) {
629                         it->set_value (compatibility_cols.selected, select);
630                         break;
631                 }
632         }
633
634         --applying_changes_from_engine;
635 }
636
637 void
638 ExportFormatDialog::change_quality_selection (bool select, WeakQualityPtr quality)
639 {
640         change_selection<ExportFormatManager::QualityState, QualityCols> (select, quality, quality_list, quality_view, quality_cols);
641 }
642
643 void
644 ExportFormatDialog::change_format_selection (bool select, WeakFormatPtr format)
645 {
646         change_selection<ExportFormat, FormatCols> (select, format, format_list, format_view, format_cols);
647
648         ExportFormatPtr ptr = format.lock();
649
650         if (select && ptr) {
651                 change_encoding_options (ptr);
652         }
653 }
654
655 void
656 ExportFormatDialog::change_sample_rate_selection (bool select, WeakSampleRatePtr rate)
657 {
658         change_selection<ExportFormatManager::SampleRateState, SampleRateCols> (select, rate, sample_rate_list, sample_rate_view, sample_rate_cols);
659
660         if (select) {
661                 ExportFormatManager::SampleRatePtr ptr = rate.lock();
662                 if (ptr && _session) {
663                         src_quality_combo.set_sensitive ((uint32_t) ptr->rate != _session->frame_rate());
664                 }
665         }
666 }
667
668 void
669 ExportFormatDialog::change_sample_format_selection (bool select, WeakSampleFormatPtr format)
670 {
671         change_selection<HasSampleFormat::SampleFormatState, SampleFormatCols> (select, format, sample_format_list, sample_format_view, sample_format_cols);
672 }
673
674 void
675 ExportFormatDialog::change_dither_type_selection (bool select, WeakDitherTypePtr type)
676 {
677         change_selection<HasSampleFormat::DitherTypeState, DitherTypeCols> (select, type, dither_type_list, dither_type_view, dither_type_cols);
678 }
679
680 template<typename T, typename ColsT>
681 void
682 ExportFormatDialog::change_selection (bool select, boost::weak_ptr<T> w_ptr, Glib::RefPtr<Gtk::ListStore> & list, Gtk::TreeView & view, ColsT & cols)
683 {
684         ++applying_changes_from_engine;
685
686         boost::shared_ptr<T> ptr = w_ptr.lock();
687
688         Gtk::ListStore::Children::iterator it;
689         Glib::RefPtr<Gtk::TreeSelection> selection;
690
691         selection = view.get_selection();
692
693         if (!ptr) {
694                 selection->unselect_all();
695         } else {
696                 for (it = list->children().begin(); it != list->children().end(); ++it) {
697                         if (it->get_value (cols.ptr) == ptr) {
698                                 if (select) {
699                                         selection->select (it);
700                                 } else {
701                                         selection->unselect (it);
702                                 }
703                                 break;
704                         }
705                 }
706         }
707
708         --applying_changes_from_engine;
709 }
710
711 void
712 ExportFormatDialog::change_quality_compatibility (bool compatibility, WeakQualityPtr quality)
713 {
714         change_compatibility<ExportFormatManager::QualityState, QualityCols> (compatibility, quality, quality_list, quality_cols);
715 }
716
717 void
718 ExportFormatDialog::change_format_compatibility (bool compatibility, WeakFormatPtr format)
719 {
720         change_compatibility<ExportFormat, FormatCols> (compatibility, format, format_list, format_cols);
721 }
722
723 void
724 ExportFormatDialog::change_sample_rate_compatibility (bool compatibility, WeakSampleRatePtr rate)
725 {
726         change_compatibility<ExportFormatManager::SampleRateState, SampleRateCols> (compatibility, rate, sample_rate_list, sample_rate_cols);
727 }
728
729 void
730 ExportFormatDialog::change_sample_format_compatibility (bool compatibility, WeakSampleFormatPtr format)
731 {
732         change_compatibility<HasSampleFormat::SampleFormatState, SampleFormatCols> (compatibility, format, sample_format_list, sample_format_cols);
733 }
734
735 void
736 ExportFormatDialog::change_dither_type_compatibility (bool compatibility, WeakDitherTypePtr type)
737 {
738         change_compatibility<HasSampleFormat::DitherTypeState, DitherTypeCols> (compatibility, type, dither_type_list, dither_type_cols, "red");
739 }
740
741 template<typename T, typename ColsT>
742 void
743 ExportFormatDialog::change_compatibility (bool compatibility, boost::weak_ptr<T> w_ptr, Glib::RefPtr<Gtk::ListStore> & list, ColsT & cols,
744                                           std::string const & c_incompatible, std::string const & c_compatible)
745 {
746         boost::shared_ptr<T> ptr = w_ptr.lock();
747
748         Gtk::ListStore::Children::iterator it;
749         for (it = list->children().begin(); it != list->children().end(); ++it) {
750                 if (it->get_value (cols.ptr) == ptr) {
751                         it->set_value (cols.color, compatibility ? c_compatible : c_incompatible);
752                         break;
753                 }
754         }
755 }
756
757 void
758 ExportFormatDialog::update_with_cue ()
759 {
760         manager.select_with_cue (with_cue.get_active());
761 }
762
763 void
764 ExportFormatDialog::update_with_toc ()
765 {
766         manager.select_with_toc (with_toc.get_active());
767 }
768
769 void
770 ExportFormatDialog::update_with_mp4chaps ()
771 {
772         manager.select_with_mp4chaps (with_mp4chaps.get_active());
773 }
774
775 void
776 ExportFormatDialog::update_command ()
777 {
778         manager.set_command (command_entry.get_text());
779 }
780
781 void
782 ExportFormatDialog::update_description()
783 {
784         std::string text = ": " + format->description(false);
785         name_generated_part.set_text(text);
786 }
787
788 void
789 ExportFormatDialog::update_name ()
790 {
791         manager.set_name (name_entry.get_text());
792 }
793
794 void
795 ExportFormatDialog::update_trim_start_selection ()
796 {
797         manager.select_trim_beginning (trim_start_checkbox.get_active());
798 }
799
800 void
801 ExportFormatDialog::update_trim_end_selection ()
802 {
803         manager.select_trim_end (trim_end_checkbox.get_active());
804 }
805
806 void
807 ExportFormatDialog::update_normalize_sensitivity ()
808 {
809         bool en = normalize_checkbox.get_active();
810         bool loudness = normalize_loudness_rb.get_active();
811         normalize_dbfs_spinbutton.set_sensitive (!loudness && en);
812         normalize_lufs_spinbutton.set_sensitive (loudness && en);
813         normalize_dbtp_spinbutton.set_sensitive (loudness && en);
814 }
815
816 void
817 ExportFormatDialog::update_normalize_selection ()
818 {
819         manager.select_normalize (normalize_checkbox.get_active());
820         manager.select_normalize_loudness (normalize_loudness_rb.get_active());
821         manager.select_normalize_dbfs (normalize_dbfs_spinbutton.get_value ());
822         manager.select_normalize_lufs (normalize_lufs_spinbutton.get_value ());
823         manager.select_normalize_dbtp (normalize_dbtp_spinbutton.get_value ());
824         update_normalize_sensitivity ();
825 }
826
827 void
828 ExportFormatDialog::update_silence_start_selection ()
829 {
830         update_time (silence_start, silence_start_clock);
831         AnyTime zero;
832         zero.type = AnyTime::Timecode;
833         manager.select_silence_beginning (silence_start_checkbox.get_active() ? silence_start : zero);
834 }
835
836 void
837 ExportFormatDialog::update_silence_end_selection ()
838 {
839         update_time (silence_end, silence_end_clock);
840         AnyTime zero;
841         zero.type = AnyTime::Timecode;
842         manager.select_silence_end (silence_end_checkbox.get_active() ? silence_end : zero);
843 }
844
845 void
846 ExportFormatDialog::update_clock (AudioClock & clock, ARDOUR::AnyTime const & time)
847 {
848         // TODO position
849         clock.set (_session->convert_to_frames (time), true);
850
851         AudioClock::Mode mode(AudioClock::Timecode);
852
853         switch (time.type) {
854           case AnyTime::Timecode:
855                 mode = AudioClock::Timecode;
856                 break;
857           case AnyTime::BBT:
858                 mode = AudioClock::BBT;
859                 break;
860           case AnyTime::Frames:
861                 mode = AudioClock::Frames;
862                 break;
863           case AnyTime::Seconds:
864                 mode = AudioClock::MinSec;
865                 break;
866         }
867
868         clock.set_mode (mode);
869 }
870
871 void
872 ExportFormatDialog::update_time (AnyTime & time, AudioClock const & clock)
873 {
874         if (!_session) {
875                 return;
876         }
877
878         framecnt_t frames = clock.current_duration();
879
880         switch (clock.mode()) {
881           case AudioClock::Timecode:
882                 time.type = AnyTime::Timecode;
883                 _session->timecode_time (frames, time.timecode);
884                 break;
885           case AudioClock::BBT:
886                 time.type = AnyTime::BBT;
887                 _session->bbt_time (frames, time.bbt);
888                 break;
889           case AudioClock::MinSec:
890                 time.type = AnyTime::Seconds;
891                 time.seconds = (double) frames / _session->frame_rate();
892                 break;
893           case AudioClock::Frames:
894                 time.type = AnyTime::Frames;
895                 time.frames = frames;
896                 break;
897         }
898 }
899
900 void
901 ExportFormatDialog::update_src_quality_selection ()
902 {
903         Gtk::TreeModel::const_iterator iter = src_quality_combo.get_active();
904         ExportFormatBase::SRCQuality quality = iter->get_value (src_quality_cols.id);
905         manager.select_src_quality (quality);
906 }
907
908 void
909 ExportFormatDialog::update_tagging_selection ()
910 {
911         manager.select_tagging (tag_checkbox.get_active());
912 }
913
914 void
915 ExportFormatDialog::change_encoding_options (ExportFormatPtr ptr)
916 {
917         empty_encoding_option_table ();
918
919         boost::shared_ptr<ARDOUR::ExportFormatLinear> linear_ptr;
920         boost::shared_ptr<ARDOUR::ExportFormatOggVorbis> ogg_ptr;
921         boost::shared_ptr<ARDOUR::ExportFormatFLAC> flac_ptr;
922         boost::shared_ptr<ARDOUR::ExportFormatBWF> bwf_ptr;
923
924         if ((linear_ptr = boost::dynamic_pointer_cast<ExportFormatLinear> (ptr))) {
925                 show_linear_enconding_options (linear_ptr);
926         } else if ((ogg_ptr = boost::dynamic_pointer_cast<ExportFormatOggVorbis> (ptr))) {
927                 show_ogg_enconding_options (ogg_ptr);
928         } else if ((flac_ptr = boost::dynamic_pointer_cast<ExportFormatFLAC> (ptr))) {
929                 show_flac_enconding_options (flac_ptr);
930         } else if ((bwf_ptr = boost::dynamic_pointer_cast<ExportFormatBWF> (ptr))) {
931                 show_bwf_enconding_options (bwf_ptr);
932         } else {
933                 std::cout << "Unrecognized format!" << std::endl;
934         }
935 }
936
937 void
938 ExportFormatDialog::empty_encoding_option_table ()
939 {
940         encoding_options_table.foreach (sigc::bind (sigc::mem_fun (*this, &ExportFormatDialog::remove_widget), &encoding_options_table));
941 }
942
943 void
944 ExportFormatDialog::remove_widget (Gtk::Widget & to_remove, Gtk::Container * remove_from)
945 {
946         remove_from->remove (to_remove);
947 }
948
949 void
950 ExportFormatDialog::show_linear_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatLinear> ptr)
951 {
952         /* Set label and pack table */
953
954         encoding_options_label.set_label (_("Linear encoding options"));
955
956         encoding_options_table.resize (2, 2);
957         encoding_options_table.attach (sample_format_label, 0, 1, 0, 1);
958         encoding_options_table.attach (dither_label, 1, 2, 0, 1);
959         encoding_options_table.attach (sample_format_view, 0, 1, 1, 2);
960         encoding_options_table.attach (dither_type_view, 1, 2, 1, 2);
961
962         fill_sample_format_lists (boost::dynamic_pointer_cast<HasSampleFormat> (ptr));
963
964         show_all_children ();
965 }
966
967 void
968 ExportFormatDialog::show_ogg_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatOggVorbis> /*ptr*/)
969 {
970         encoding_options_label.set_label (_("Ogg Vorbis options"));
971
972         encoding_options_table.resize (1, 1);
973         encoding_options_table.attach (tag_checkbox, 0, 1, 0, 1);
974
975         show_all_children ();
976 }
977
978 void
979 ExportFormatDialog::show_flac_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatFLAC> ptr)
980 {
981         encoding_options_label.set_label (_("FLAC options"));
982
983         encoding_options_table.resize (3, 2);
984         encoding_options_table.attach (sample_format_label, 0, 1, 0, 1);
985         encoding_options_table.attach (dither_label, 1, 2, 0, 1);
986         encoding_options_table.attach (sample_format_view, 0, 1, 1, 2);
987         encoding_options_table.attach (dither_type_view, 1, 2, 1, 2);
988         encoding_options_table.attach (tag_checkbox, 0, 2, 2, 3);
989
990         fill_sample_format_lists (boost::dynamic_pointer_cast<HasSampleFormat> (ptr));
991
992         show_all_children ();
993 }
994
995 void
996 ExportFormatDialog::show_bwf_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatBWF> ptr)
997 {
998         encoding_options_label.set_label (_("Broadcast Wave options"));
999
1000         encoding_options_table.resize (2, 2);
1001         encoding_options_table.attach (sample_format_label, 0, 1, 0, 1);
1002         encoding_options_table.attach (dither_label, 1, 2, 0, 1);
1003         encoding_options_table.attach (sample_format_view, 0, 1, 1, 2);
1004         encoding_options_table.attach (dither_type_view, 1, 2, 1, 2);
1005
1006         fill_sample_format_lists (boost::dynamic_pointer_cast<HasSampleFormat> (ptr));
1007
1008         show_all_children ();
1009 }
1010
1011 void
1012 ExportFormatDialog::fill_sample_format_lists (boost::shared_ptr<ARDOUR::HasSampleFormat> ptr)
1013 {
1014         /* Fill lists */
1015
1016         Gtk::TreeModel::iterator iter;
1017         Gtk::TreeModel::Row row;
1018
1019         sample_format_list->clear ();
1020
1021         HasSampleFormat::SampleFormatList const & formats = ptr->get_sample_formats ();
1022
1023         for (HasSampleFormat::SampleFormatList::const_iterator it = formats.begin(); it != formats.end(); ++it) {
1024                 iter = sample_format_list->append();
1025                 row = *iter;
1026
1027                 row[sample_format_cols.ptr] = *it;
1028                 row[sample_format_cols.color] = (*it)->compatible() ? "white" : "red";
1029                 row[sample_format_cols.label] = (*it)->name();
1030
1031                 if ((*it)->selected()) {
1032                         sample_format_view.get_selection()->select (iter);
1033                 }
1034         }
1035
1036         dither_type_list->clear ();
1037
1038         HasSampleFormat::DitherTypeList const & types = ptr->get_dither_types ();
1039
1040         for (HasSampleFormat::DitherTypeList::const_iterator it = types.begin(); it != types.end(); ++it) {
1041                 iter = dither_type_list->append();
1042                 row = *iter;
1043
1044                 row[dither_type_cols.ptr] = *it;
1045                 row[dither_type_cols.color] = "white";
1046                 row[dither_type_cols.label] = (*it)->name();
1047
1048                 if ((*it)->selected()) {
1049                         dither_type_view.get_selection()->select (iter);
1050                 }
1051         }
1052 }
1053
1054 void
1055 ExportFormatDialog::end_dialog ()
1056 {
1057         hide_all ();
1058 }
1059
1060 void
1061 ExportFormatDialog::prohibit_compatibility_selection ()
1062 {
1063         compatibility_select_connection.block (true);
1064         compatibility_view.get_selection()->unselect_all ();
1065         compatibility_select_connection.block (false);
1066 }