take a rather different approach toggling editor+mixer
[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=file path, %d=directory, %b=basename, see tooltip for more):"), 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 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."));
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         ARDOUR_UI_UTILS::set_tooltip (command_entry,
151                         _(
152                                 "%a Artist name\n"
153                                 "%b File's base-name\n"
154                                 "%d File's directory\n"
155                                 "%f File's full absolute path\n"
156                                 "%n Session name\n"
157                                 "%t Title\n"
158                                 "%A Album\n"
159                                 "%C Comment\n"
160                                 "%E Engineer\n"
161                                 "%G Genre\n"
162                                 "%N Timespan name\n"
163                                 "%P Producer\n"
164                                 "%T Track number\n"
165                                 "%Y Year"
166                          ));
167
168         /* Format table */
169
170         init_format_table();
171
172         /* Encoding options */
173
174         init_encoding_option_widgets();
175
176         encoding_options_table.set_spacings (1);
177
178         encoding_options_vbox.pack_start (encoding_options_label, false, false, 0);
179         encoding_options_vbox.pack_start (encoding_options_table, false, false, 12);
180
181         Pango::AttrList bold;
182         Pango::Attribute b = Pango::Attribute::create_attr_weight (Pango::WEIGHT_BOLD);
183         bold.insert (b);
184         encoding_options_label.set_attributes (bold);
185
186         /* Buttons */
187
188         revert_button = add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
189         revert_button->signal_clicked().connect (sigc::mem_fun(*this, &ExportFormatDialog::revert));
190         close_button = add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_APPLY);
191         close_button->set_sensitive (false);
192         close_button->signal_clicked().connect (sigc::mem_fun (*this, &ExportFormatDialog::end_dialog));
193         manager.CompleteChanged.connect (*this, invalidator (*this), boost::bind (&Gtk::Button::set_sensitive, close_button, _1), gui_context());
194
195         with_cue.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_with_cue));
196         with_toc.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_with_toc));
197         with_mp4chaps.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_with_mp4chaps));
198         command_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_command));
199
200         cue_toc_vbox.pack_start (with_cue, false, false);
201         cue_toc_vbox.pack_start (with_toc, false, false);
202         cue_toc_vbox.pack_start (with_mp4chaps, false, false);
203
204         /* Load state before hooking up the rest of the signals */
205
206         load_state (format);
207
208         /* Name entry */
209
210         name_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_name));
211
212         /* Normalize, silence and src_quality signals */
213
214         trim_start_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_trim_start_selection));
215         trim_end_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_trim_end_selection));
216
217         normalize_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
218         normalize_peak_rb.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
219         normalize_loudness_rb.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
220         normalize_dbfs_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
221         normalize_lufs_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
222         normalize_dbtp_spinbutton.signal_value_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_normalize_selection));
223
224         silence_start_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_start_selection));
225         silence_start_clock.ValueChanged.connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_start_selection));
226
227         silence_end_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_end_selection));
228         silence_end_clock.ValueChanged.connect (sigc::mem_fun (*this, &ExportFormatDialog::update_silence_end_selection));
229
230         src_quality_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_src_quality_selection));
231
232         /* Format table signals */
233
234         Gtk::CellRendererToggle *toggle = dynamic_cast<Gtk::CellRendererToggle *>(compatibility_view.get_column_cell_renderer (0));
235         toggle->signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_compatibility_selection));
236         compatibility_select_connection = compatibility_view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &ExportFormatDialog::prohibit_compatibility_selection));
237
238         quality_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_quality_selection));
239         format_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_format_selection));
240         sample_rate_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_sample_rate_selection));
241
242         /* Encoding option signals */
243
244         sample_format_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_sample_format_selection));
245         dither_type_view.get_selection()->signal_changed().connect (sigc::mem_fun(*this, &ExportFormatDialog::update_dither_type_selection));
246
247         tag_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportFormatDialog::update_tagging_selection));
248
249         /* Finalize */
250
251         show_all_children();
252         update_normalize_sensitivity ();
253 }
254
255 ExportFormatDialog::~ExportFormatDialog ()
256 {
257
258 }
259
260 void
261 ExportFormatDialog::revert ()
262 {
263         ++applying_changes_from_engine;
264
265         format->set_state (original_state);
266         load_state (format);
267
268         --applying_changes_from_engine;
269 }
270
271 void
272 ExportFormatDialog::set_session (ARDOUR::Session* s)
273 {
274         SessionHandlePtr::set_session (s);
275         silence_start_clock.set_session (s);
276         silence_end_clock.set_session (s);
277
278         if (!_session) {
279                 return;
280         }
281
282         update_clock (silence_start_clock, silence_start);
283         update_clock (silence_end_clock, silence_end);
284
285         /* Select native samplerate if no selection is yet made */
286
287         if (sample_rate_view.get_selection()->count_selected_rows() == 0) {
288                 Gtk::ListStore::Children::iterator it;
289                 for (it = sample_rate_list->children().begin(); it != sample_rate_list->children().end(); ++it) {
290                         if ((framecnt_t) (*it)->get_value (sample_rate_cols.ptr)->rate == _session->nominal_frame_rate()) {
291                                 sample_rate_view.get_selection()->select (it);
292                                 break;
293                         }
294                 }
295         }
296 }
297
298 void
299 ExportFormatDialog::load_state (FormatPtr spec)
300 {
301         name_entry.set_text (spec->name());
302
303         normalize_checkbox.set_active (spec->normalize());
304         normalize_peak_rb.set_active (!spec->normalize_loudness());
305         normalize_loudness_rb.set_active (spec->normalize_loudness());
306         normalize_dbfs_spinbutton.set_value (spec->normalize_dbfs());
307         normalize_lufs_spinbutton.set_value (spec->normalize_lufs());
308         normalize_dbtp_spinbutton.set_value (spec->normalize_dbtp());
309
310         trim_start_checkbox.set_active (spec->trim_beginning());
311         silence_start = spec->silence_beginning_time();
312         silence_start_checkbox.set_active (spec->silence_beginning_time().not_zero());
313
314         trim_end_checkbox.set_active (spec->trim_end());
315         silence_end = spec->silence_end_time();
316         silence_end_checkbox.set_active (spec->silence_end_time().not_zero());
317
318         with_cue.set_active (spec->with_cue());
319         with_toc.set_active (spec->with_toc());
320         with_mp4chaps.set_active (spec->with_mp4chaps());
321
322         for (Gtk::ListStore::Children::iterator it = src_quality_list->children().begin(); it != src_quality_list->children().end(); ++it) {
323                 if (it->get_value (src_quality_cols.id) == spec->src_quality()) {
324                         src_quality_combo.set_active (it);
325                         break;
326                 }
327         }
328
329         for (Gtk::ListStore::Children::iterator it = format_list->children().begin(); it != format_list->children().end(); ++it) {
330                 boost::shared_ptr<ARDOUR::ExportFormat> format_in_list = it->get_value (format_cols.ptr);
331                 if (format_in_list->get_format_id() == spec->format_id() &&
332                     // BWF has the same format id with wav, so we need to check this.
333                     format_in_list->has_broadcast_info() == spec->has_broadcast_info()) {
334
335                         format_in_list->set_selected (true);
336                         break;
337                 }
338         }
339
340         for (Gtk::ListStore::Children::iterator it = sample_rate_list->children().begin(); it != sample_rate_list->children().end(); ++it) {
341                 if (it->get_value (sample_rate_cols.ptr)->rate == spec->sample_rate()) {
342                         it->get_value (sample_rate_cols.ptr)->set_selected (true);
343                         break;
344                 }
345         }
346
347         if (spec->sample_format()) {
348                 for (Gtk::ListStore::Children::iterator it = sample_format_list->children().begin(); it != sample_format_list->children().end(); ++it) {
349                         if (it->get_value (sample_format_cols.ptr)->format == spec->sample_format()) {
350                                 it->get_value (sample_format_cols.ptr)->set_selected (true);
351                                 break;
352                         }
353                 }
354
355                 for (Gtk::ListStore::Children::iterator it = dither_type_list->children().begin(); it != dither_type_list->children().end(); ++it) {
356                         if (it->get_value (dither_type_cols.ptr)->type == spec->dither_type()) {
357                                 it->get_value (dither_type_cols.ptr)->set_selected (true);
358                                 break;
359                         }
360                 }
361         }
362
363         update_normalize_sensitivity ();
364         tag_checkbox.set_active (spec->tag());
365         command_entry.set_text (spec->command());
366 }
367
368 void
369 ExportFormatDialog::init_format_table ()
370 {
371
372         format_table.set_spacings (1);
373
374         format_table.attach (compatibility_label, 0, 1, 0, 1);
375         format_table.attach (quality_label, 1, 2, 0, 1);
376         format_table.attach (format_label, 2, 3, 0, 1);
377         format_table.attach (sample_rate_label, 3, 4, 0, 1);
378
379         format_table.attach (compatibility_view, 0, 1, 1, 2);
380         format_table.attach (quality_view, 1, 2, 1, 2);
381         format_table.attach (format_view, 2, 3, 1, 2);
382         format_table.attach (sample_rate_view, 3, 4, 1, 2);
383
384         format_table.attach (src_quality_label, 0, 3, 2, 3);
385         format_table.attach (src_quality_combo, 3, 4, 2, 3);
386
387         compatibility_view.set_headers_visible (false);
388         quality_view.set_headers_visible (false);
389         format_view.set_headers_visible (false);
390         sample_rate_view.set_headers_visible (false);
391
392         /*** Table entries ***/
393
394         Gtk::TreeModel::iterator iter;
395         Gtk::TreeModel::Row row;
396
397         /* Compatibilities */
398
399         compatibility_list = Gtk::ListStore::create (compatibility_cols);
400         compatibility_view.set_model (compatibility_list);
401
402         ExportFormatManager::CompatList const & compat_list = manager.get_compatibilities();
403
404         for (ExportFormatManager::CompatList::const_iterator it = compat_list.begin(); it != compat_list.end(); ++it) {
405                 iter = compatibility_list->append();
406                 row = *iter;
407
408                 row[compatibility_cols.ptr] = *it;
409                 row[compatibility_cols.selected] = false;
410                 row[compatibility_cols.label] = (*it)->name();
411
412                 WeakCompatPtr ptr (*it);
413                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_compatibility_selection, this, _1, ptr), gui_context());
414         }
415
416         compatibility_view.append_column_editable ("", compatibility_cols.selected);
417
418         Gtk::CellRendererText* text_renderer = Gtk::manage (new Gtk::CellRendererText);
419         text_renderer->property_editable() = false;
420
421         Gtk::TreeView::Column* column = compatibility_view.get_column (0);
422         column->pack_start (*text_renderer);
423         column->add_attribute (text_renderer->property_text(), compatibility_cols.label);
424
425         /* Qualities */
426
427         quality_list = Gtk::ListStore::create (quality_cols);
428         quality_view.set_model (quality_list);
429
430         ExportFormatManager::QualityList const & qualities = manager.get_qualities ();
431
432         for (ExportFormatManager::QualityList::const_iterator it = qualities.begin(); it != qualities.end(); ++it) {
433                 iter = quality_list->append();
434                 row = *iter;
435
436                 row[quality_cols.ptr] = *it;
437                 row[quality_cols.color] = "white";
438                 row[quality_cols.label] = (*it)->name();
439
440                 WeakQualityPtr ptr (*it);
441                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_quality_selection, this, _1, ptr), gui_context());
442                 (*it)->CompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_quality_compatibility, this, _1, ptr), gui_context());
443         }
444
445         quality_view.append_column ("", quality_cols.label);
446
447         /* Formats */
448
449         format_list = Gtk::ListStore::create (format_cols);
450         format_view.set_model (format_list);
451
452         ExportFormatManager::FormatList const & formats = manager.get_formats ();
453
454         for (ExportFormatManager::FormatList::const_iterator it = formats.begin(); it != formats.end(); ++it) {
455                 iter = format_list->append();
456                 row = *iter;
457
458                 row[format_cols.ptr] = *it;
459                 row[format_cols.color] = "white";
460                 row[format_cols.label] = (*it)->name();
461
462                 WeakFormatPtr ptr (*it);
463                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_format_selection, this, _1, ptr), gui_context());
464                 (*it)->CompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_format_compatibility, this, _1, ptr), gui_context());
465
466                 /* Encoding options */
467
468                 boost::shared_ptr<HasSampleFormat> hsf;
469
470                 if ((hsf = boost::dynamic_pointer_cast<HasSampleFormat> (*it))) {
471                         hsf->SampleFormatSelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_format_selection, this, _1, _2), gui_context());
472                         hsf->SampleFormatCompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_format_compatibility, this, _1, _2), gui_context());
473
474                         hsf->DitherTypeSelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_dither_type_selection, this, _1, _2), gui_context());
475                         hsf->DitherTypeCompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_dither_type_compatibility, this, _1, _2), gui_context());
476                 }
477         }
478
479         format_view.append_column ("", format_cols.label);
480
481         /* Sample Rates */
482
483         sample_rate_list = Gtk::ListStore::create (sample_rate_cols);
484         sample_rate_view.set_model (sample_rate_list);
485
486         ExportFormatManager::SampleRateList const & rates = manager.get_sample_rates ();
487
488         for (ExportFormatManager::SampleRateList::const_iterator it = rates.begin(); it != rates.end(); ++it) {
489                 iter = sample_rate_list->append();
490                 row = *iter;
491
492                 row[sample_rate_cols.ptr] = *it;
493                 row[sample_rate_cols.color] = "white";
494                 row[sample_rate_cols.label] = (*it)->name();
495
496                 WeakSampleRatePtr ptr (*it);
497                 (*it)->SelectChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_rate_selection, this, _1, ptr), gui_context());
498                 (*it)->CompatibleChanged.connect (*this, invalidator (*this), boost::bind (&ExportFormatDialog::change_sample_rate_compatibility, this, _1, ptr), gui_context());
499         }
500
501         sample_rate_view.append_column ("", sample_rate_cols.label);
502
503         /* Color rendering */
504
505         Gtk::TreeViewColumn * label_col;
506         Gtk::CellRendererText * renderer;
507
508         label_col = quality_view.get_column(0);
509         renderer = dynamic_cast<Gtk::CellRendererText*> (quality_view.get_column_cell_renderer (0));
510         label_col->add_attribute(renderer->property_foreground(), quality_cols.color);
511
512         label_col = format_view.get_column(0);
513         renderer = dynamic_cast<Gtk::CellRendererText*> (format_view.get_column_cell_renderer (0));
514         label_col->add_attribute(renderer->property_foreground(), format_cols.color);
515
516         label_col = sample_rate_view.get_column(0);
517         renderer = dynamic_cast<Gtk::CellRendererText*> (sample_rate_view.get_column_cell_renderer (0));
518         label_col->add_attribute(renderer->property_foreground(), sample_rate_cols.color);
519
520         /* SRC Qualities */
521
522         src_quality_list = Gtk::ListStore::create (src_quality_cols);
523         src_quality_combo.set_model (src_quality_list);
524
525         iter = src_quality_list->append();
526         row = *iter;
527         row[src_quality_cols.id] = ExportFormatBase::SRC_SincBest;
528         row[src_quality_cols.label] = _("Best (sinc)");
529
530         iter = src_quality_list->append();
531         row = *iter;
532         row[src_quality_cols.id] = ExportFormatBase::SRC_SincMedium;
533         row[src_quality_cols.label] = _("Medium (sinc)");
534
535         iter = src_quality_list->append();
536         row = *iter;
537         row[src_quality_cols.id] = ExportFormatBase::SRC_SincFast;
538         row[src_quality_cols.label] = _("Fast (sinc)");
539
540         iter = src_quality_list->append();
541         row = *iter;
542         row[src_quality_cols.id] = ExportFormatBase::SRC_Linear;
543         row[src_quality_cols.label] = _("Linear");
544
545         iter = src_quality_list->append();
546         row = *iter;
547         row[src_quality_cols.id] = ExportFormatBase::SRC_ZeroOrderHold;
548         row[src_quality_cols.label] = _("Zero order hold");
549
550         src_quality_combo.pack_start (src_quality_cols.label);
551         src_quality_combo.set_active (0);
552 }
553
554 void
555 ExportFormatDialog::init_encoding_option_widgets ()
556 {
557         Gtk::TreeViewColumn * label_col;
558         Gtk::CellRendererText * renderer;
559
560         sample_format_list = Gtk::ListStore::create (sample_format_cols);
561         sample_format_view.set_model (sample_format_list);
562         sample_format_view.set_headers_visible (false);
563         sample_format_view.append_column ("", sample_format_cols.label);
564         label_col = sample_format_view.get_column(0);
565         renderer = dynamic_cast<Gtk::CellRendererText*> (sample_format_view.get_column_cell_renderer (0));
566         label_col->add_attribute(renderer->property_foreground(), sample_format_cols.color);
567
568         dither_type_list = Gtk::ListStore::create (dither_type_cols);
569         dither_type_view.set_model (dither_type_list);
570         dither_type_view.set_headers_visible (false);
571         dither_type_view.append_column ("", dither_type_cols.label);
572         label_col = dither_type_view.get_column(0);
573         renderer = dynamic_cast<Gtk::CellRendererText*> (dither_type_view.get_column_cell_renderer (0));
574         label_col->add_attribute(renderer->property_foreground(), dither_type_cols.color);
575
576 }
577
578 void
579 ExportFormatDialog::update_compatibility_selection (std::string const & path)
580 {
581
582         Gtk::TreeModel::iterator iter = compatibility_view.get_model ()->get_iter (path);
583         ExportFormatCompatibilityPtr ptr = iter->get_value (compatibility_cols.ptr);
584         bool state = iter->get_value (compatibility_cols.selected);
585
586         iter->set_value (compatibility_cols.selected, state);
587         ptr->set_selected (state);
588
589 }
590
591 void
592 ExportFormatDialog::update_quality_selection ()
593 {
594         update_selection<QualityCols> (quality_list, quality_view, quality_cols);
595 }
596
597 void
598 ExportFormatDialog::update_format_selection ()
599 {
600         update_selection<FormatCols> (format_list, format_view, format_cols);
601 }
602
603 void
604 ExportFormatDialog::update_sample_rate_selection ()
605 {
606         update_selection<SampleRateCols> (sample_rate_list, sample_rate_view, sample_rate_cols);
607 }
608
609 void
610 ExportFormatDialog::update_sample_format_selection ()
611 {
612         update_selection<SampleFormatCols> (sample_format_list, sample_format_view, sample_format_cols);
613 }
614
615 void
616 ExportFormatDialog::update_dither_type_selection ()
617 {
618         update_selection<DitherTypeCols> (dither_type_list, dither_type_view, dither_type_cols);
619 }
620
621 template<typename ColsT>
622 void
623 ExportFormatDialog::update_selection (Glib::RefPtr<Gtk::ListStore> & list, Gtk::TreeView & view, ColsT & cols)
624 {
625         if (applying_changes_from_engine) {
626                 return;
627         }
628
629         Gtk::ListStore::Children::iterator it;
630         Glib::RefPtr<Gtk::TreeSelection> selection = view.get_selection();
631
632         for (it = list->children().begin(); it != list->children().end(); ++it) {
633                 bool selected = selection->is_selected (it);
634                 it->get_value (cols.ptr)->set_selected (selected);
635         }
636 }
637
638 void
639 ExportFormatDialog::change_compatibility_selection (bool select, WeakCompatPtr compat)
640 {
641         ++applying_changes_from_engine;
642
643         ExportFormatCompatibilityPtr ptr = compat.lock();
644
645         for (Gtk::ListStore::Children::iterator it = compatibility_list->children().begin(); it != compatibility_list->children().end(); ++it) {
646                 if (it->get_value (compatibility_cols.ptr) == ptr) {
647                         it->set_value (compatibility_cols.selected, select);
648                         break;
649                 }
650         }
651
652         --applying_changes_from_engine;
653 }
654
655 void
656 ExportFormatDialog::change_quality_selection (bool select, WeakQualityPtr quality)
657 {
658         change_selection<ExportFormatManager::QualityState, QualityCols> (select, quality, quality_list, quality_view, quality_cols);
659 }
660
661 void
662 ExportFormatDialog::change_format_selection (bool select, WeakFormatPtr format)
663 {
664         change_selection<ExportFormat, FormatCols> (select, format, format_list, format_view, format_cols);
665
666         ExportFormatPtr ptr = format.lock();
667
668         if (select && ptr) {
669                 change_encoding_options (ptr);
670         }
671 }
672
673 void
674 ExportFormatDialog::change_sample_rate_selection (bool select, WeakSampleRatePtr rate)
675 {
676         change_selection<ExportFormatManager::SampleRateState, SampleRateCols> (select, rate, sample_rate_list, sample_rate_view, sample_rate_cols);
677
678         if (select) {
679                 ExportFormatManager::SampleRatePtr ptr = rate.lock();
680                 if (ptr && _session) {
681                         src_quality_combo.set_sensitive ((uint32_t) ptr->rate != _session->frame_rate());
682                 }
683         }
684 }
685
686 void
687 ExportFormatDialog::change_sample_format_selection (bool select, WeakSampleFormatPtr format)
688 {
689         change_selection<HasSampleFormat::SampleFormatState, SampleFormatCols> (select, format, sample_format_list, sample_format_view, sample_format_cols);
690 }
691
692 void
693 ExportFormatDialog::change_dither_type_selection (bool select, WeakDitherTypePtr type)
694 {
695         change_selection<HasSampleFormat::DitherTypeState, DitherTypeCols> (select, type, dither_type_list, dither_type_view, dither_type_cols);
696 }
697
698 template<typename T, typename ColsT>
699 void
700 ExportFormatDialog::change_selection (bool select, boost::weak_ptr<T> w_ptr, Glib::RefPtr<Gtk::ListStore> & list, Gtk::TreeView & view, ColsT & cols)
701 {
702         ++applying_changes_from_engine;
703
704         boost::shared_ptr<T> ptr = w_ptr.lock();
705
706         Gtk::ListStore::Children::iterator it;
707         Glib::RefPtr<Gtk::TreeSelection> selection;
708
709         selection = view.get_selection();
710
711         if (!ptr) {
712                 selection->unselect_all();
713         } else {
714                 for (it = list->children().begin(); it != list->children().end(); ++it) {
715                         if (it->get_value (cols.ptr) == ptr) {
716                                 if (select) {
717                                         selection->select (it);
718                                 } else {
719                                         selection->unselect (it);
720                                 }
721                                 break;
722                         }
723                 }
724         }
725
726         --applying_changes_from_engine;
727 }
728
729 void
730 ExportFormatDialog::change_quality_compatibility (bool compatibility, WeakQualityPtr quality)
731 {
732         change_compatibility<ExportFormatManager::QualityState, QualityCols> (compatibility, quality, quality_list, quality_cols);
733 }
734
735 void
736 ExportFormatDialog::change_format_compatibility (bool compatibility, WeakFormatPtr format)
737 {
738         change_compatibility<ExportFormat, FormatCols> (compatibility, format, format_list, format_cols);
739 }
740
741 void
742 ExportFormatDialog::change_sample_rate_compatibility (bool compatibility, WeakSampleRatePtr rate)
743 {
744         change_compatibility<ExportFormatManager::SampleRateState, SampleRateCols> (compatibility, rate, sample_rate_list, sample_rate_cols);
745 }
746
747 void
748 ExportFormatDialog::change_sample_format_compatibility (bool compatibility, WeakSampleFormatPtr format)
749 {
750         change_compatibility<HasSampleFormat::SampleFormatState, SampleFormatCols> (compatibility, format, sample_format_list, sample_format_cols);
751 }
752
753 void
754 ExportFormatDialog::change_dither_type_compatibility (bool compatibility, WeakDitherTypePtr type)
755 {
756         change_compatibility<HasSampleFormat::DitherTypeState, DitherTypeCols> (compatibility, type, dither_type_list, dither_type_cols, "red");
757 }
758
759 template<typename T, typename ColsT>
760 void
761 ExportFormatDialog::change_compatibility (bool compatibility, boost::weak_ptr<T> w_ptr, Glib::RefPtr<Gtk::ListStore> & list, ColsT & cols,
762                                           std::string const & c_incompatible, std::string const & c_compatible)
763 {
764         boost::shared_ptr<T> ptr = w_ptr.lock();
765
766         Gtk::ListStore::Children::iterator it;
767         for (it = list->children().begin(); it != list->children().end(); ++it) {
768                 if (it->get_value (cols.ptr) == ptr) {
769                         it->set_value (cols.color, compatibility ? c_compatible : c_incompatible);
770                         break;
771                 }
772         }
773 }
774
775 void
776 ExportFormatDialog::update_with_cue ()
777 {
778         manager.select_with_cue (with_cue.get_active());
779 }
780
781 void
782 ExportFormatDialog::update_with_toc ()
783 {
784         manager.select_with_toc (with_toc.get_active());
785 }
786
787 void
788 ExportFormatDialog::update_with_mp4chaps ()
789 {
790         manager.select_with_mp4chaps (with_mp4chaps.get_active());
791 }
792
793 void
794 ExportFormatDialog::update_command ()
795 {
796         manager.set_command (command_entry.get_text());
797 }
798
799 void
800 ExportFormatDialog::update_description()
801 {
802         std::string text = ": " + format->description(false);
803         name_generated_part.set_text(text);
804 }
805
806 void
807 ExportFormatDialog::update_name ()
808 {
809         manager.set_name (name_entry.get_text());
810 }
811
812 void
813 ExportFormatDialog::update_trim_start_selection ()
814 {
815         manager.select_trim_beginning (trim_start_checkbox.get_active());
816 }
817
818 void
819 ExportFormatDialog::update_trim_end_selection ()
820 {
821         manager.select_trim_end (trim_end_checkbox.get_active());
822 }
823
824 void
825 ExportFormatDialog::update_normalize_sensitivity ()
826 {
827         bool en = normalize_checkbox.get_active();
828         bool loudness = normalize_loudness_rb.get_active();
829         normalize_dbfs_spinbutton.set_sensitive (!loudness && en);
830         normalize_lufs_spinbutton.set_sensitive (loudness && en);
831         normalize_dbtp_spinbutton.set_sensitive (loudness && en);
832 }
833
834 void
835 ExportFormatDialog::update_normalize_selection ()
836 {
837         manager.select_normalize (normalize_checkbox.get_active());
838         manager.select_normalize_loudness (normalize_loudness_rb.get_active());
839         manager.select_normalize_dbfs (normalize_dbfs_spinbutton.get_value ());
840         manager.select_normalize_lufs (normalize_lufs_spinbutton.get_value ());
841         manager.select_normalize_dbtp (normalize_dbtp_spinbutton.get_value ());
842         update_normalize_sensitivity ();
843 }
844
845 void
846 ExportFormatDialog::update_silence_start_selection ()
847 {
848         update_time (silence_start, silence_start_clock);
849         AnyTime zero;
850         zero.type = AnyTime::Timecode;
851         manager.select_silence_beginning (silence_start_checkbox.get_active() ? silence_start : zero);
852 }
853
854 void
855 ExportFormatDialog::update_silence_end_selection ()
856 {
857         update_time (silence_end, silence_end_clock);
858         AnyTime zero;
859         zero.type = AnyTime::Timecode;
860         manager.select_silence_end (silence_end_checkbox.get_active() ? silence_end : zero);
861 }
862
863 void
864 ExportFormatDialog::update_clock (AudioClock & clock, ARDOUR::AnyTime const & time)
865 {
866         // TODO position
867         clock.set (_session->convert_to_frames (time), true);
868
869         AudioClock::Mode mode(AudioClock::Timecode);
870
871         switch (time.type) {
872           case AnyTime::Timecode:
873                 mode = AudioClock::Timecode;
874                 break;
875           case AnyTime::BBT:
876                 mode = AudioClock::BBT;
877                 break;
878           case AnyTime::Frames:
879                 mode = AudioClock::Frames;
880                 break;
881           case AnyTime::Seconds:
882                 mode = AudioClock::MinSec;
883                 break;
884         }
885
886         clock.set_mode (mode);
887 }
888
889 void
890 ExportFormatDialog::update_time (AnyTime & time, AudioClock const & clock)
891 {
892         if (!_session) {
893                 return;
894         }
895
896         framecnt_t frames = clock.current_duration();
897
898         switch (clock.mode()) {
899           case AudioClock::Timecode:
900                 time.type = AnyTime::Timecode;
901                 _session->timecode_time (frames, time.timecode);
902                 break;
903           case AudioClock::BBT:
904                 time.type = AnyTime::BBT;
905                 _session->bbt_time (frames, time.bbt);
906                 break;
907           case AudioClock::MinSec:
908                 time.type = AnyTime::Seconds;
909                 time.seconds = (double) frames / _session->frame_rate();
910                 break;
911           case AudioClock::Frames:
912                 time.type = AnyTime::Frames;
913                 time.frames = frames;
914                 break;
915         }
916 }
917
918 void
919 ExportFormatDialog::update_src_quality_selection ()
920 {
921         Gtk::TreeModel::const_iterator iter = src_quality_combo.get_active();
922         ExportFormatBase::SRCQuality quality = iter->get_value (src_quality_cols.id);
923         manager.select_src_quality (quality);
924 }
925
926 void
927 ExportFormatDialog::update_tagging_selection ()
928 {
929         manager.select_tagging (tag_checkbox.get_active());
930 }
931
932 void
933 ExportFormatDialog::change_encoding_options (ExportFormatPtr ptr)
934 {
935         empty_encoding_option_table ();
936
937         boost::shared_ptr<ARDOUR::ExportFormatLinear> linear_ptr;
938         boost::shared_ptr<ARDOUR::ExportFormatOggVorbis> ogg_ptr;
939         boost::shared_ptr<ARDOUR::ExportFormatFLAC> flac_ptr;
940         boost::shared_ptr<ARDOUR::ExportFormatBWF> bwf_ptr;
941
942         if ((linear_ptr = boost::dynamic_pointer_cast<ExportFormatLinear> (ptr))) {
943                 show_linear_enconding_options (linear_ptr);
944         } else if ((ogg_ptr = boost::dynamic_pointer_cast<ExportFormatOggVorbis> (ptr))) {
945                 show_ogg_enconding_options (ogg_ptr);
946         } else if ((flac_ptr = boost::dynamic_pointer_cast<ExportFormatFLAC> (ptr))) {
947                 show_flac_enconding_options (flac_ptr);
948         } else if ((bwf_ptr = boost::dynamic_pointer_cast<ExportFormatBWF> (ptr))) {
949                 show_bwf_enconding_options (bwf_ptr);
950         } else {
951                 std::cout << "Unrecognized format!" << std::endl;
952         }
953 }
954
955 void
956 ExportFormatDialog::empty_encoding_option_table ()
957 {
958         encoding_options_table.foreach (sigc::bind (sigc::mem_fun (*this, &ExportFormatDialog::remove_widget), &encoding_options_table));
959 }
960
961 void
962 ExportFormatDialog::remove_widget (Gtk::Widget & to_remove, Gtk::Container * remove_from)
963 {
964         remove_from->remove (to_remove);
965 }
966
967 void
968 ExportFormatDialog::show_linear_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatLinear> ptr)
969 {
970         /* Set label and pack table */
971
972         encoding_options_label.set_label (_("Linear encoding options"));
973
974         encoding_options_table.resize (2, 2);
975         encoding_options_table.attach (sample_format_label, 0, 1, 0, 1);
976         encoding_options_table.attach (dither_label, 1, 2, 0, 1);
977         encoding_options_table.attach (sample_format_view, 0, 1, 1, 2);
978         encoding_options_table.attach (dither_type_view, 1, 2, 1, 2);
979
980         fill_sample_format_lists (boost::dynamic_pointer_cast<HasSampleFormat> (ptr));
981
982         show_all_children ();
983 }
984
985 void
986 ExportFormatDialog::show_ogg_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatOggVorbis> /*ptr*/)
987 {
988         encoding_options_label.set_label (_("Ogg Vorbis options"));
989
990         encoding_options_table.resize (1, 1);
991         encoding_options_table.attach (tag_checkbox, 0, 1, 0, 1);
992
993         show_all_children ();
994 }
995
996 void
997 ExportFormatDialog::show_flac_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatFLAC> ptr)
998 {
999         encoding_options_label.set_label (_("FLAC options"));
1000
1001         encoding_options_table.resize (3, 2);
1002         encoding_options_table.attach (sample_format_label, 0, 1, 0, 1);
1003         encoding_options_table.attach (dither_label, 1, 2, 0, 1);
1004         encoding_options_table.attach (sample_format_view, 0, 1, 1, 2);
1005         encoding_options_table.attach (dither_type_view, 1, 2, 1, 2);
1006         encoding_options_table.attach (tag_checkbox, 0, 2, 2, 3);
1007
1008         fill_sample_format_lists (boost::dynamic_pointer_cast<HasSampleFormat> (ptr));
1009
1010         show_all_children ();
1011 }
1012
1013 void
1014 ExportFormatDialog::show_bwf_enconding_options (boost::shared_ptr<ARDOUR::ExportFormatBWF> ptr)
1015 {
1016         encoding_options_label.set_label (_("Broadcast Wave options"));
1017
1018         encoding_options_table.resize (2, 2);
1019         encoding_options_table.attach (sample_format_label, 0, 1, 0, 1);
1020         encoding_options_table.attach (dither_label, 1, 2, 0, 1);
1021         encoding_options_table.attach (sample_format_view, 0, 1, 1, 2);
1022         encoding_options_table.attach (dither_type_view, 1, 2, 1, 2);
1023
1024         fill_sample_format_lists (boost::dynamic_pointer_cast<HasSampleFormat> (ptr));
1025
1026         show_all_children ();
1027 }
1028
1029 void
1030 ExportFormatDialog::fill_sample_format_lists (boost::shared_ptr<ARDOUR::HasSampleFormat> ptr)
1031 {
1032         /* Fill lists */
1033
1034         Gtk::TreeModel::iterator iter;
1035         Gtk::TreeModel::Row row;
1036
1037         sample_format_list->clear ();
1038
1039         HasSampleFormat::SampleFormatList const & formats = ptr->get_sample_formats ();
1040
1041         for (HasSampleFormat::SampleFormatList::const_iterator it = formats.begin(); it != formats.end(); ++it) {
1042                 iter = sample_format_list->append();
1043                 row = *iter;
1044
1045                 row[sample_format_cols.ptr] = *it;
1046                 row[sample_format_cols.color] = (*it)->compatible() ? "white" : "red";
1047                 row[sample_format_cols.label] = (*it)->name();
1048
1049                 if ((*it)->selected()) {
1050                         sample_format_view.get_selection()->select (iter);
1051                 }
1052         }
1053
1054         dither_type_list->clear ();
1055
1056         HasSampleFormat::DitherTypeList const & types = ptr->get_dither_types ();
1057
1058         for (HasSampleFormat::DitherTypeList::const_iterator it = types.begin(); it != types.end(); ++it) {
1059                 iter = dither_type_list->append();
1060                 row = *iter;
1061
1062                 row[dither_type_cols.ptr] = *it;
1063                 row[dither_type_cols.color] = "white";
1064                 row[dither_type_cols.label] = (*it)->name();
1065
1066                 if ((*it)->selected()) {
1067                         dither_type_view.get_selection()->select (iter);
1068                 }
1069         }
1070 }
1071
1072 void
1073 ExportFormatDialog::end_dialog ()
1074 {
1075         hide_all ();
1076 }
1077
1078 void
1079 ExportFormatDialog::prohibit_compatibility_selection ()
1080 {
1081         compatibility_select_connection.block (true);
1082         compatibility_view.get_selection()->unselect_all ();
1083         compatibility_select_connection.block (false);
1084 }