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