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