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