Merge libs/ardour and gtk2_ardour with 2.0-ongoing R2837.
[ardour.git] / gtk2_ardour / export_dialog.cc
1 /*
2     Copyright (C) 1999-2005 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18
19 */
20
21 #include <unistd.h>
22 #include <utility>
23 #include <sys/stat.h>
24 #include <fstream>
25
26 #include <samplerate.h>
27 #include <pbd/convert.h>
28 #include <pbd/xml++.h>
29
30 #include <gtkmm2ext/utils.h>
31 #include <gtkmm2ext/window_title.h>
32
33 #include <ardour/export.h>
34 #include <ardour/session_directory.h>
35 #include <ardour/sndfile_helpers.h>
36 #include <ardour/audio_track.h>
37 #include <ardour/audioregion.h>
38 #include <ardour/audioengine.h>
39 #include <ardour/audiofilesource.h>
40 #include <ardour/gdither.h>
41 #include <ardour/utils.h>
42 #include <ardour/profile.h>
43
44 #include "export_dialog.h"
45 #include "ardour_ui.h"
46 #include "public_editor.h"
47 #include "keyboard.h"
48
49 #include "i18n.h"
50
51 #define FRAME_NAME "BaseFrame"
52
53 using namespace std;
54 using namespace ARDOUR;
55 using namespace PBD;
56 using namespace sigc;
57 using namespace Gtk;
58 using namespace Gtkmm2ext;
59
60 using PBD::internationalize;
61
62 static const gchar *sample_rates[] = {
63         N_("22.05kHz"),
64         N_("44.1kHz"),
65         N_("48kHz"),
66         N_("88.2kHz"),
67         N_("96kHz"),
68         N_("192kHz"),
69         0
70 };
71
72 static const gchar *src_quality[] = {
73         N_("best"),
74         N_("fastest"),
75         N_("linear"),
76         N_("better"),
77         N_("intermediate"),
78         0
79 };
80
81 static const gchar *dither_types[] = {
82         N_("None"),
83         N_("Rectangular"),
84         N_("Shaped Noise"),
85         N_("Triangular"),
86         0
87 };
88
89 static const gchar* channel_strings[] = {
90         N_("Stereo"), 
91         N_("Mono"), 
92         0
93 };
94
95 static const gchar* cue_file_types[] = {
96         N_("None"), 
97         N_("CUE"),
98         N_("TOC"),
99         0
100 };
101
102 ExportDialog::ExportDialog(PublicEditor& e)
103         : ArdourDialog ("export dialog"),
104           editor (e),
105           format_table (9, 2),
106           format_frame (_("Format")),
107           cue_file_label (_("CD Marker File Type"), 1.0, 0.5),
108           channel_count_label (_("Channels"), 1.0, 0.5),
109           header_format_label (_("File Type"), 1.0, 0.5),
110           bitdepth_format_label (_("Sample Format"), 1.0, 0.5),
111           endian_format_label (_("Sample Endianness"), 1.0, 0.5),
112           sample_rate_label (_("Sample Rate"), 1.0, 0.5),
113           src_quality_label (_("Conversion Quality"), 1.0, 0.5),
114           dither_type_label (_("Dither Type"), 1.0, 0.5),
115           cuefile_only_checkbox (_("Export CD Marker File Only")),
116           file_chooser (FILE_CHOOSER_ACTION_SAVE),
117           track_selector_button (_("Specific tracks ..."))
118 {
119         guint32 n;
120         guint32 len;
121         guint32 maxlen;
122
123         session = 0;
124         track_and_master_selection_allowed = true;
125         channel_count_selection_allowed = true;
126         export_cd_markers_allowed = true;
127         set_resizable (false);  
128
129         WindowTitle title(Glib::get_application_name());
130         title += _("Export");
131         
132         set_title (title.get_string());
133         set_wmclass (X_("ardour_export"), "Ardour");
134         set_name ("ExportWindow");
135         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
136
137         spec.running = false;
138
139         master_list = ListStore::create (exp_cols);
140         master_selector.set_model (master_list);
141
142         master_selector.set_name ("ExportTrackSelector");
143         master_selector.set_size_request (-1, 100);
144         master_selector.append_column(_("Output"), exp_cols.output);
145         master_selector.append_column_editable(_("Left"), exp_cols.left);
146         master_selector.append_column_editable(_("Right"), exp_cols.right);
147         master_selector.get_column(0)->set_min_width(100);
148         
149         master_selector.get_column(1)->set_min_width(40);
150         master_selector.get_column(1)->set_sizing(Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
151         master_selector.get_column(2)->set_min_width(40);
152         master_selector.get_column(2)->set_sizing(Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
153         master_selector.get_selection()->set_mode (Gtk::SELECTION_NONE);
154
155         track_list = ListStore::create (exp_cols);
156         track_selector.set_model (track_list);
157
158         track_selector.set_name ("ExportTrackSelector");
159         track_selector.set_size_request (-1, 130);
160         track_selector.append_column(_("Output"), exp_cols.output);
161         track_selector.append_column_editable(_("Left"), exp_cols.left);
162         track_selector.append_column_editable(_("Right"), exp_cols.right);
163
164         track_selector.get_column(0)->set_min_width(100);
165         track_selector.get_column(1)->set_min_width(40);
166         track_selector.get_column(1)->set_sizing(Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
167         track_selector.get_column(2)->set_min_width(40);
168         track_selector.get_column(2)->set_sizing(Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
169         track_selector.get_selection()->set_mode (Gtk::SELECTION_NONE);
170
171         progress_bar.set_name ("ExportProgress");
172
173         format_frame.add (format_table);
174         format_frame.set_name (FRAME_NAME);
175
176         track_scroll.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
177         master_scroll.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
178
179         get_vbox()->pack_start (file_frame, PACK_EXPAND_WIDGET);
180
181         hpacker.set_spacing (5);
182         hpacker.set_border_width (5);
183         hpacker.pack_start (format_frame, PACK_SHRINK );
184
185         master_scroll.add (master_selector);
186         track_scroll.add (track_selector);
187
188         master_scroll.set_size_request (220, 100);
189         track_scroll.set_size_request (220, 100);
190                 
191         /* we may hide some of these later */
192         track_vpacker.pack_start (master_scroll);
193         track_vpacker.pack_start (track_scroll);
194         track_vpacker.pack_start (track_selector_button, Gtk::PACK_EXPAND_PADDING);
195
196         hpacker.pack_start (track_vpacker);
197
198         get_vbox()->pack_start (hpacker, PACK_SHRINK);
199         
200         track_selector_button.set_name ("EditorGTKButton");
201         track_selector_button.signal_clicked().connect (mem_fun(*this, &ExportDialog::track_selector_button_click));
202
203         Gtk::FileFilter filter_wav;
204         filter_wav.set_name("Wav files");
205         filter_wav.add_mime_type("audio/wav");
206         file_chooser.add_filter(filter_wav);
207         
208         Gtk::FileFilter filter_aiff;
209         filter_aiff.set_name("Aiff files");
210         filter_aiff.add_mime_type("audio/aiff");
211         filter_aiff.add_pattern("*.aif");
212         filter_aiff.add_pattern("*.aiff");
213         file_chooser.add_filter(filter_aiff);
214         
215         Gtk::FileFilter filter_any;
216         filter_any.set_name("All files");
217         filter_any.add_pattern("*");
218         file_chooser.add_filter(filter_any);
219         file_chooser.set_no_show_all();
220
221         get_vbox()->pack_start (progress_bar, false, false, 5);
222         progress_bar.set_no_show_all();
223         progress_bar.hide();
224
225         file_hbox.set_spacing (5);
226         file_hbox.set_border_width (5);
227         file_hbox.pack_start (file_chooser, PACK_EXPAND_WIDGET);
228
229         file_frame.add (file_hbox);
230         file_frame.set_border_width (5);
231         file_frame.set_name (FRAME_NAME);
232
233         /* pop_strings needs to be created on the stack because set_popdown_strings()
234            takes a reference. 
235         */
236
237         vector<string> pop_strings = I18N(sample_rates);
238         Gtkmm2ext::set_popdown_strings (sample_rate_combo, pop_strings);
239         sample_rate_combo.set_active_text (pop_strings.front());
240         pop_strings = I18N(src_quality);
241         Gtkmm2ext::set_popdown_strings (src_quality_combo, pop_strings);
242         src_quality_combo.set_active_text (pop_strings.front());
243         pop_strings = I18N(dither_types);
244         Gtkmm2ext::set_popdown_strings (dither_type_combo, pop_strings);
245         dither_type_combo.set_active_text (pop_strings.front());
246         pop_strings = I18N(channel_strings);
247         Gtkmm2ext::set_popdown_strings (channel_count_combo, pop_strings);
248         channel_count_combo.set_active_text (pop_strings.front());
249         pop_strings = I18N((const char **) sndfile_header_formats_strings);
250         Gtkmm2ext::set_popdown_strings (header_format_combo, pop_strings);
251         header_format_combo.set_active_text (pop_strings.front());
252         pop_strings = I18N((const char **) sndfile_bitdepth_formats_strings);
253         Gtkmm2ext::set_popdown_strings (bitdepth_format_combo, pop_strings);
254         bitdepth_format_combo.set_active_text (pop_strings.front());
255         pop_strings = I18N((const char **) sndfile_endian_formats_strings);
256         Gtkmm2ext::set_popdown_strings (endian_format_combo, pop_strings);
257         endian_format_combo.set_active_text (pop_strings.front());
258         pop_strings = I18N(cue_file_types);
259         Gtkmm2ext::set_popdown_strings (cue_file_combo, pop_strings);
260         cue_file_combo.set_active_text (pop_strings.front());
261
262         /* this will re-sensitized as soon as a non RIFF/WAV
263            header format is chosen.
264         */
265
266         endian_format_combo.set_sensitive (false);
267
268         /* determine longest strings at runtime */
269
270         maxlen = 0;
271         const char *longest = X_("gl"); /* translators: one ascender, one descender */
272         string longest_str;
273
274         for (n = 0; n < SNDFILE_HEADER_FORMATS; ++n) {
275                 if ((len = strlen (sndfile_header_formats_strings[n])) > maxlen) {
276                         maxlen = len;
277                         longest = sndfile_header_formats_strings[n];
278                 }
279         }
280
281         for (n = 0; n < SNDFILE_BITDEPTH_FORMATS; ++n) {
282                 if ((len = strlen (sndfile_bitdepth_formats_strings[n])) > maxlen) {
283                         maxlen = len;
284                         longest = sndfile_bitdepth_formats_strings[n];
285                 }
286         }
287
288         for (n = 0; n < SNDFILE_ENDIAN_FORMATS; ++n) {
289                 if ((len = strlen (sndfile_endian_formats_strings[n])) > maxlen) {
290                         maxlen = len;
291                         longest = sndfile_endian_formats_strings[n];
292                 }
293         }
294
295         longest_str = longest;
296
297         /* force ascender + descender */
298
299         longest_str[0] = 'g';
300         longest_str[1] = 'l';
301
302         //Gtkmm2ext::set_size_request_to_display_given_text (header_format_combo, longest_str.c_str(), 5+FUDGE, 5);
303
304         // TRANSLATORS: "slereg" is "stereo" with ascender and descender substituted
305         //Gtkmm2ext::set_size_request_to_display_given_text (channel_count_combo, _("slereg"), 5+FUDGE, 5);
306
307 /*      header_format_combo.set_focus_on_click (true);
308         bitdepth_format_combo.set_focus_on_click (true);
309         endian_format_combo.set_focus_on_click (true);
310         channel_count_combo.set_focus_on_click (true);
311         src_quality_combo.set_focus_on_click (true);
312         dither_type_combo.set_focus_on_click (true);
313         sample_rate_combo.set_focus_on_click (true);
314         cue_file_combo.set_focus_on_click (true);
315 */
316         dither_type_label.set_name ("ExportFormatLabel");
317         sample_rate_label.set_name ("ExportFormatLabel");
318         src_quality_label.set_name ("ExportFormatLabel");
319         channel_count_label.set_name ("ExportFormatLabel");
320         header_format_label.set_name ("ExportFormatLabel");
321         bitdepth_format_label.set_name ("ExportFormatLabel");
322         endian_format_label.set_name ("ExportFormatLabel");
323         cue_file_label.set_name ("ExportFormatLabel");
324
325         header_format_combo.set_name ("ExportFormatDisplay");
326         bitdepth_format_combo.set_name ("ExportFormatDisplay");
327         endian_format_combo.set_name ("ExportFormatDisplay");
328         channel_count_combo.set_name ("ExportFormatDisplay");
329         dither_type_combo.set_name ("ExportFormatDisplay");
330         src_quality_combo.set_name ("ExportFormatDisplay");
331         sample_rate_combo.set_name ("ExportFormatDisplay");
332         cue_file_combo.set_name ("ExportFormatDisplay");
333
334         cuefile_only_checkbox.set_name ("ExportCheckbox");
335
336         format_table.set_homogeneous (true);
337         format_table.set_border_width (5);
338         format_table.set_col_spacings (5);
339         format_table.set_row_spacings (5);
340
341         int row = 0;
342
343         format_table.attach (channel_count_label, 0, 1, row, row+1);
344         format_table.attach (channel_count_combo, 1, 2, row, row+1);
345
346         row++;
347         
348         format_table.attach (header_format_label, 0, 1, 1, 2, FILL, FILL);
349         format_table.attach (header_format_combo, 1, 2, 1, 2, FILL, FILL);
350
351         format_table.attach (bitdepth_format_label, 0, 1, 2, 3, FILL, FILL);
352         format_table.attach (bitdepth_format_combo, 1, 2, 2, 3, FILL, FILL);
353
354         format_table.attach (bitdepth_format_label, 0, 1, row, row+1);
355         format_table.attach (bitdepth_format_combo, 1, 2, row, row+1);
356         
357         row++;
358         
359         if (!Profile->get_sae()) {
360                 format_table.attach (endian_format_label, 0, 1, row, row+1);
361                 format_table.attach (endian_format_combo, 1, 2, row, row+1);
362                 row++;
363         }
364
365         format_table.attach (sample_rate_label, 0, 1, row, row+1);
366         format_table.attach (sample_rate_combo, 1, 2, row, row+1);
367
368         row++;
369
370         if (!Profile->get_sae()) {
371                 format_table.attach (src_quality_label, 0, 1, row, row+1);
372                 format_table.attach (src_quality_combo, 1, 2, row, row+1);
373                 row++;
374         }
375
376         format_table.attach (dither_type_label, 0, 1, row, row+1);
377         format_table.attach (dither_type_combo, 1, 2, row, row+1);
378
379         row++;
380
381         if (!Profile->get_sae()) {
382                 format_table.attach (cue_file_label, 0, 1, row, row+1);
383                 format_table.attach (cue_file_combo, 1, 2, row, row+1);
384                 row++;
385         
386                 format_table.attach (cuefile_only_checkbox, 0, 2, row, row+1);
387         }
388
389         file_entry.set_name ("ExportFileDisplay");
390
391         signal_delete_event().connect (mem_fun(*this, &ExportDialog::window_closed));
392
393         cancel_button = add_button (Stock::CANCEL, RESPONSE_CANCEL);
394         cancel_button->signal_clicked().connect (mem_fun(*this, &ExportDialog::end_dialog));
395         ok_button = add_button (_("Export"), RESPONSE_ACCEPT);
396         ok_button->signal_clicked().connect (mem_fun(*this, &ExportDialog::do_export));
397         channel_count_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::channels_chosen));
398         bitdepth_format_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::bitdepth_chosen));
399         header_format_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::header_chosen));
400         sample_rate_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::sample_rate_chosen));
401         cue_file_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::cue_file_type_chosen));
402
403         file_chooser.signal_update_preview().connect (mem_fun(*this, &ExportDialog::file_chooser_selection_changed));
404
405 }
406
407 ExportDialog::~ExportDialog()
408 {
409 }
410
411 void
412 ExportDialog::do_not_allow_track_and_master_selection()
413 {
414         track_and_master_selection_allowed = false;
415 }
416
417 void
418 ExportDialog::do_not_allow_channel_count_selection()
419 {
420         channel_count_selection_allowed = false;
421 }
422
423 void
424 ExportDialog::do_not_allow_export_cd_markers()
425 {
426         export_cd_markers_allowed = false;
427 }
428
429 void
430 ExportDialog::connect_to_session (Session *s)
431 {
432         session = s;
433         session->GoingAway.connect (mem_fun(*this, &Window::hide_all));
434
435         switch (session->frame_rate()) {
436         case 22050:
437                 sample_rate_combo.set_active_text (_("22.05kHz"));
438                 break;
439         case 44100:
440                 sample_rate_combo.set_active_text (_("44.1kHz"));
441                 break;
442         case 48000:
443                 sample_rate_combo.set_active_text (_("48kHz"));
444                 break;
445         case 88200:
446                 sample_rate_combo.set_active_text (_("88.2kHz"));
447                 break;
448         case 96000:
449                 sample_rate_combo.set_active_text (_("96kHz"));
450                 break;
451         case 192000:
452                 sample_rate_combo.set_active_text (_("192kHz"));
453                 break;
454         default:
455                 sample_rate_combo.set_active_text (_("44.1kHz"));
456                 break;
457         }
458
459         src_quality_combo.set_sensitive (false);
460
461         set_state();
462 }
463
464 void
465 ExportDialog::set_state()
466 {
467         XMLNode* node = session->instant_xml(X_("ExportDialog"));
468         XMLProperty* prop;
469         bool fc_location_requested = false;
470         if (node) {
471
472                 if ((prop = node->property (X_("sample_rate"))) != 0) {
473                         sample_rate_combo.set_active_text(prop->value());
474                 }
475                 if ((prop = node->property (X_("src_quality"))) != 0) {
476                         src_quality_combo.set_active_text(prop->value());
477                 }
478                 if ((prop = node->property (X_("dither_type"))) != 0) {
479                         dither_type_combo.set_active_text(prop->value());
480                 }
481                 if ((prop = node->property (X_("channel_count"))) != 0) {
482                         channel_count_combo.set_active_text(prop->value());
483                 }
484                 if ((prop = node->property (X_("header_format"))) != 0) {
485                         header_format_combo.set_active_text(prop->value());
486                 }
487                 if ((prop = node->property (X_("bitdepth_format"))) != 0) {
488                         bitdepth_format_combo.set_active_text(prop->value());
489                 }
490                 if ((prop = node->property (X_("endian_format"))) != 0) {
491                         endian_format_combo.set_active_text(prop->value());
492                 }
493                 if ((prop = node->property (X_("filename"))) != 0) {
494                         file_chooser.set_filename(prop->value());
495                         fc_location_requested = true;
496                         file_chooser.set_current_folder(Glib::path_get_basename(prop->value()));
497                 }
498                   
499                 if ((prop = node->property (X_("cue_file_type"))) != 0) {
500                         cue_file_combo.set_active_text(prop->value());
501                 }
502         }
503
504         if (!fc_location_requested) {
505
506                 /*
507                   If the filename hasn't been set before, use the
508                   current session's export directory as a default
509                   location for the export.  
510                 */
511
512                 file_chooser.set_current_folder (session->session_directory().export_path().to_string());
513                 file_chooser.set_current_name (_("export.wav"));
514         }
515
516         header_chosen ();
517         bitdepth_chosen();
518         channels_chosen();
519         sample_rate_chosen();
520
521         if (session->master_out()) {
522                 track_scroll.hide ();
523         } else {
524                 master_scroll.hide ();
525                 track_selector_button.hide ();
526         }
527
528         if (!node) {
529                 return;
530         }
531
532         if (session->master_out()) {
533                 XMLNode* master = find_named_node(*node, (X_("Master")));
534                 int nchns;
535
536                 if (!master) {
537                         
538                         /* default is to use all */
539                         if (channel_count_combo.get_active_text() == _("Mono")) {
540                                 nchns = 1;
541                         } else {
542                                 nchns = 2;
543                         }
544
545                         TreeModel::Children rows = master_selector.get_model()->children();
546                         for (uint32_t r = 0; r < session->master_out()->n_outputs().n_audio(); ++r) {
547                                 if (nchns == 2) {
548                                         if (r % 2) {
549                                                 rows[r][exp_cols.right] = true;
550                                         } else {
551                                                 rows[r][exp_cols.left] = true;
552                                         }
553                                 } else {
554                                         rows[r][exp_cols.left] = true;
555                                 }
556                         }
557
558                 } else {
559                         /* XXX use XML state */
560                 }
561         }
562
563         XMLNode* tracks = find_named_node(*node, (X_("Tracks")));
564         if (!tracks) {
565                 return;
566         }
567         
568         XMLNodeList track_list = tracks->children(X_("Track"));
569         TreeModel::Children rows = track_selector.get_model()->children();
570         TreeModel::Children::iterator ri = rows.begin();
571         TreeModel::Row row;
572
573         for (XMLNodeIterator it = track_list.begin(); it != track_list.end(); ++it, ++ri) {
574                 if (ri == rows.end()){
575                         break;
576                 }
577
578                 XMLNode* track = *it;
579                 row = *ri;
580
581                 if ((prop = track->property(X_("channel1"))) != 0) {
582                         if (prop->value() == X_("on")) {
583                                 row[exp_cols.left] = true;
584                         } else {
585                                 row[exp_cols.left] = false;
586                         }
587                 }
588
589                 if ((prop = track->property(X_("channel2"))) != 0) {
590                         if (prop->value() == X_("on")) {
591                                 row[exp_cols.right] = true;
592                         } else {
593                                 row[exp_cols.right] = false;
594                         }
595                 }
596         }
597 }
598
599 void
600 ExportDialog::save_state()
601 {
602         if (!session) {
603                 return;
604         }
605
606         XMLNode* node = new XMLNode(X_("ExportDialog"));
607
608         node->add_property(X_("sample_rate"), sample_rate_combo.get_active_text());
609         node->add_property(X_("src_quality"), src_quality_combo.get_active_text());
610         node->add_property(X_("dither_type"), dither_type_combo.get_active_text());
611         node->add_property(X_("channel_count"), channel_count_combo.get_active_text());
612         node->add_property(X_("header_format"), header_format_combo.get_active_text());
613         node->add_property(X_("bitdepth_format"), bitdepth_format_combo.get_active_text());
614         node->add_property(X_("endian_format"), endian_format_combo.get_active_text());
615         node->add_property(X_("filename"), file_chooser.get_filename());
616         node->add_property(X_("cue_file_type"), cue_file_combo.get_active_text());
617
618         XMLNode* tracks = new XMLNode(X_("Tracks"));
619
620         TreeModel::Children rows = track_selector.get_model()->children();
621         TreeModel::Row row;
622         for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri) {
623                 XMLNode* track = new XMLNode(X_("Track"));
624
625                 row = *ri;
626                 track->add_property(X_("channel1"), row[exp_cols.left] ? X_("on") : X_("off"));
627                 track->add_property(X_("channel2"), row[exp_cols.right] ? X_("on") : X_("off"));
628
629                 tracks->add_child_nocopy(*track);
630         }
631         node->add_child_nocopy(*tracks);
632         
633         session->add_instant_xml(*node);
634 }
635
636 void
637 ExportDialog::set_range (nframes_t start, nframes_t end)
638 {
639         spec.start_frame = start;
640         spec.end_frame = end;
641 }
642
643 gint
644 ExportDialog::progress_timeout ()
645 {
646         progress_bar.set_fraction (spec.progress);
647         return TRUE;
648 }
649
650 void
651 frames_to_cd_frames_string (char* buf, nframes_t when, nframes_t fr)
652 {
653
654   long unsigned int remainder;
655   int mins, secs, frames;
656
657         mins = when / (60 * fr);
658         remainder = when - (mins * 60 * fr);
659         secs = remainder / fr;
660         remainder -= secs * fr;
661         frames = remainder / (fr / 75);
662         sprintf (buf, " %02d:%02d:%02d", mins, secs, frames);
663
664 }
665
666 struct LocationSortByStart {
667     bool operator() (Location *a, Location *b) {
668             return a->start() < b->start();
669     }
670 };
671
672 void
673 ExportDialog::export_toc_file (Locations::LocationList& locations, const string& path)
674 {
675         if(!export_cd_markers_allowed){
676                 return;
677         }
678         
679         string filepath = path + ".toc";
680         ofstream out (filepath.c_str());
681         long unsigned int last_end_time = spec.start_frame, last_start_time = spec.start_frame;
682         gchar buf[18];
683
684         if (!out) {
685                 error << string_compose(_("Editor: cannot open \"%1\" as export file for CD toc file"), filepath) << endmsg;
686                 return;
687         }
688         out << "CD_DA" << endl;
689         out << "CD_TEXT {" << endl << "  LANGUAGE_MAP {" << endl << "    0 : EN" << endl << "  }" << endl;
690         out << "  LANGUAGE 0 {" << endl << "    TITLE \"" << session->name() << "\"" << endl << "  }" << endl << "}" << endl;
691
692         Locations::LocationList::iterator i;
693         Locations::LocationList temp;
694
695         for (i = locations.begin(); i != locations.end(); ++i) {
696                 if ((*i)->start() >= spec.start_frame && (*i)->end() <= spec.end_frame && (*i)->is_cd_marker() && !(*i)->is_end()) {
697                         temp.push_back (*i);
698                 }
699         }
700
701         if (temp.size() > 0) {
702                 LocationSortByStart cmp;
703                 temp.sort (cmp);
704                 Location * curr_range = 0;
705                 Locations::LocationList::iterator nexti;
706
707                 for (i = temp.begin(); i != temp.end(); ++i) {
708
709                         if ((*i)->start() >= last_end_time)
710                         {
711                                 /* this is a track, defined by a cd range marker or a cd location marker outside of a cd range */
712                                 out << endl << "TRACK AUDIO" << endl;
713                                 
714                                 if ((*i)->cd_info.find("scms") != (*i)->cd_info.end())  {
715                                         out << "NO ";
716                                 }
717                                 out << "COPY" << endl;
718                                 
719                                 if ((*i)->cd_info.find("preemph") != (*i)->cd_info.end())  {
720                                         out << "PRE_EMPHASIS" << endl;
721                                 } else {
722                                         out << "NO PRE_EMPHASIS" << endl;
723                                 }
724                                 
725                                 if ((*i)->cd_info.find("isrc") != (*i)->cd_info.end())  {
726                                         out << "ISRC \"" << (*i)->cd_info["isrc"] << "\"" << endl;
727                                 }
728                                 
729                                 out << "CD_TEXT {" << endl << "  LANGUAGE 0 {" << endl << "     TITLE \"" << (*i)->name() << "\"" << endl;
730                                 if ((*i)->cd_info.find("performer") != (*i)->cd_info.end()) {
731                                         out << "     PERFORMER \"" << (*i)->cd_info["performer"]  << "\"" << endl;
732                                 }
733                                 if ((*i)->cd_info.find("string_composer") != (*i)->cd_info.end()) {
734                                         out  << "     COMPOSER \"" << (*i)->cd_info["string_composer"] << "\"" << endl;
735                                 }
736                                 
737                                 if ((*i)->cd_info.find("isrc") != (*i)->cd_info.end()) {                          
738                                         out  << "     ISRC \"";
739                                         out << (*i)->cd_info["isrc"].substr(0,2) << "-";
740                                         out << (*i)->cd_info["isrc"].substr(2,3) << "-";
741                                         out << (*i)->cd_info["isrc"].substr(5,2) << "-";
742                                         out << (*i)->cd_info["isrc"].substr(7,5) << "\"" << endl;
743                                 }
744                                 
745                                 out << "  }" << endl << "}" << endl;
746                                 
747                                 frames_to_cd_frames_string (buf, last_end_time - spec.start_frame, session->frame_rate());
748                                 out << "FILE \"" << path << "\" " << buf;
749                                 
750                                 if ((*i)->is_mark()) {
751                                         // a mark track location needs to look ahead to the next marker's start to determine length
752                                         nexti = i;
753                                         ++nexti;
754                                         if (nexti != temp.end()) {
755                                                 frames_to_cd_frames_string (buf, (*nexti)->start() - last_end_time, session->frame_rate());
756                                                 out << buf << endl;
757                                                 
758                                                 frames_to_cd_frames_string (buf, (*i)->start() - last_end_time, session->frame_rate());
759                                                 out << "START" << buf << endl;
760                                                 
761                                                 last_start_time = (*i)->start();
762                                                 last_end_time = (*nexti)->start();
763                                         }
764                                         else {
765                                                 // this was the last marker, use session end
766                                                 frames_to_cd_frames_string (buf, spec.end_frame - last_end_time, session->frame_rate());
767                                                 out << buf << endl;
768                                                 
769                                                 frames_to_cd_frames_string (buf, (*i)->start() - last_end_time, session->frame_rate());
770                                                 out << "START" << buf << endl;
771                                                 
772                                                 last_start_time = (*i)->start();
773                                                 last_end_time = spec.end_frame;
774                                         }
775
776                                         curr_range = 0;
777                                 }
778                                 else {
779                                         // range
780                                         frames_to_cd_frames_string (buf, (*i)->end() - last_end_time, session->frame_rate());
781                                         out << buf << endl;
782                                         
783                                         frames_to_cd_frames_string (buf, (*i)->start() - last_end_time, session->frame_rate());
784                                         out << "START" << buf << endl;
785                                         
786                                         last_start_time = (*i)->start();
787                                         last_end_time = (*i)->end();
788
789                                         curr_range = (*i);
790                                 }
791                                 
792                         }
793                         else if ((*i)->is_mark()) 
794                         {
795                                 /* this is an index within a track */
796                                 
797                                 frames_to_cd_frames_string (buf, (*i)->start() - last_start_time, session->frame_rate());
798                                 out << "INDEX" << buf << endl;
799                         }
800                 }
801         }
802         
803 }
804
805 void
806 ExportDialog::export_cue_file (Locations::LocationList& locations, const string& path)
807 {
808         if(!export_cd_markers_allowed){
809                 return;
810         }
811         
812     string filepath = path + ".cue";
813         ofstream out (filepath.c_str());
814         gchar buf[18];
815         long unsigned int last_track_end = spec.start_frame;
816         int numtracks = 0, tracknum = 0, indexnum = 0;
817
818         if (!out) {
819                 error << string_compose(_("Editor: cannot open \"%1\" as export file for CD cue file"), filepath) << endmsg;
820                 return;
821         }
822
823         Locations::LocationList::iterator i;
824         Locations::LocationList temp;
825
826         for (i = locations.begin(); i != locations.end(); ++i) {
827                 if ((*i)->start() >= spec.start_frame && (*i)->end() <= spec.end_frame && (*i)->is_cd_marker() && !(*i)->is_end()) {
828                         temp.push_back (*i);
829                         if (!(*i)->is_mark()) {
830                                 numtracks++;
831                         }
832                 }
833         }
834         
835         out << "REM Cue file generated by Ardour" << endl;
836         out << "TITLE \"" << session->name() << "\"" << endl;
837
838         if ((header_format_combo.get_active_text() == N_("WAV"))) {
839                   out << "FILE " << path  << " WAVE" << endl;
840         } else {
841                   out << "FILE " << path  << ' ' << (header_format_combo.get_active_text()) << endl;
842         }
843
844         if (false && numtracks == 0) {
845                     /* the user has supplied no track markers.
846                        the entire export is treated as one track. 
847                     */
848
849                   numtracks++;
850                   tracknum++;
851                   indexnum = 0;
852                   out << endl << "TRACK " << tracknum << " AUDIO" << endl;
853                   out << "FLAGS " ;
854                   
855                   out << "DCP " << endl;                   
856                   
857                   /* use the session name*/
858                   
859                   if (session->name() != "") {
860                     out << "TITLE \"" << session->name() << "\"" << endl;
861                   }           
862                   
863                   /* no pregap in this case */
864
865                   out << "INDEX 00 00:00:00" << endl;
866                   indexnum++;
867                   out << "INDEX 01 00:00:00" << endl;
868                   indexnum++;
869                   last_track_end = spec.end_frame;
870         }
871
872         if (temp.size()) {
873                 LocationSortByStart cmp;
874                 temp.sort (cmp);
875                 Location * curr_range = 0;
876                 Locations::LocationList::iterator nexti;
877
878                 for ( i = temp.begin(); i != temp.end(); ++i) {
879
880                         if ((*i)->start() >= last_track_end)
881                         {
882                                 /* this is a track and it doesn't start inside another one*/
883                                 
884                                 tracknum++;
885                                 indexnum = 0;
886                                 out << endl << "TRACK " << tracknum << " AUDIO" << endl;
887                                 out << "FLAGS " ;
888                                 
889                                 if ((*i)->cd_info.find("scms") != (*i)->cd_info.end())  {
890                                         out << "SCMS ";
891                                 } else {
892                                         out << "DCP ";
893                                 }
894                                 
895                                 if ((*i)->cd_info.find("preemph") != (*i)->cd_info.end())  {
896                                         out << "PRE";
897                                 }
898                                 out << endl;
899                                 
900                                 if ((*i)->cd_info.find("isrc") != (*i)->cd_info.end())  {
901                                         out << "ISRC " << (*i)->cd_info["isrc"] << endl;
902                                         
903                                 }
904                                 if ((*i)->name() != "") {
905                                         out << "TITLE \"" << (*i)->name() << "\"" << endl;
906                                 }             
907                                 
908                                 if ((*i)->cd_info.find("performer") != (*i)->cd_info.end()) {
909                                         out << "PERFORMER \"" <<  (*i)->cd_info["performer"] << "\"" << endl;
910                                 }
911                                 
912                                 if ((*i)->cd_info.find("string_composer") != (*i)->cd_info.end()) {
913                                         out << "SONGWRITER \"" << (*i)->cd_info["string_composer"]  << "\"" << endl;
914                                 }
915                                 snprintf (buf, sizeof(buf), "INDEX %02d", indexnum);
916                                 out << buf;
917                                 frames_to_cd_frames_string (buf, last_track_end - spec.start_frame, session->frame_rate());
918                                 out << buf << endl;
919                                 indexnum++;
920
921                                 if ((*i)->is_mark()) {
922                                         // need to find the next start to define the end
923                                         nexti = i;
924                                         ++nexti;
925                                         if (nexti != temp.end()) {
926                                                 last_track_end = (*nexti)->start();
927                                         }
928                                         else {
929                                                 last_track_end = spec.end_frame;
930                                         }
931                                         curr_range = 0;
932                                 }
933                                 else {
934                                         last_track_end = (*i)->end();
935                                         curr_range = (*i);
936                                 }
937                         } 
938         
939                         if ((tracknum > 0) && ((*i)->start() < last_track_end)) {
940                                 /*this is an index and it lies within a track*/
941                                 snprintf (buf, sizeof(buf), "INDEX %02d", indexnum);
942                                 out << buf;
943                                 frames_to_cd_frames_string (buf,(*i)->start() - spec.start_frame, session->frame_rate());
944                                 out << buf << endl;
945                                 indexnum++;
946                         }
947                 }
948         }
949         
950 }
951         
952 void
953 ExportDialog::do_export_cd_markers (const string& path,const string& cuefile_type)
954 {
955         if (cuefile_type == _("TOC")) {
956                 session->locations()->apply (*this, &ExportDialog::export_toc_file, path);      
957         } else {
958                 session->locations()->apply (*this, &ExportDialog::export_cue_file, path);
959         }
960 }
961
962
963 void
964 ExportDialog::do_export ()
965 {
966         string filepath = file_chooser.get_filename();
967
968         if (!ARDOUR_UI::instance()->the_engine().connected()) {
969                 MessageDialog msg (*this, 
970                                    _("Not connected to audioengine"),
971                                    true,
972                                    MESSAGE_ERROR,
973                                    BUTTONS_OK);
974                 msg.set_secondary_text (_("Ardour cannot export audio when disconnected"));
975                 msg.present ();
976                 msg.run ();
977                 return;
978         }
979                 
980         if(!is_filepath_valid(filepath)){
981                 return;
982         }
983
984         if (!Profile->get_sae() && export_cd_markers_allowed) {
985                 if (cue_file_combo.get_active_text () != _("None")) {
986                         do_export_cd_markers (file_chooser.get_filename(), cue_file_combo.get_active_text ());
987                 }
988
989                 if (cuefile_only_checkbox.get_active()) {
990                         end_dialog ();
991                         return;
992                 }
993         }
994
995         ok_button->set_sensitive(false);
996         save_state();
997
998         set_modal (true);
999         
1000         // read user input into spec
1001         initSpec(filepath);
1002         
1003         progress_bar.show();
1004         progress_connection = Glib::signal_timeout().connect (mem_fun(*this, &ExportDialog::progress_timeout), 100);
1005         cancel_label.set_text (_("Stop Export"));
1006
1007         export_audio_data();
1008         
1009         progress_connection.disconnect ();
1010         session->engine().freewheel (false);
1011         progress_bar.hide();
1012         end_dialog ();
1013 }
1014         
1015 void
1016 ExportDialog::end_dialog ()
1017 {
1018         if (spec.running) {
1019                 spec.stop = true;
1020
1021                 while (spec.running) {
1022                         if (gtk_events_pending()) {
1023                                 gtk_main_iteration ();
1024                         } else {
1025                                 usleep (10000);
1026                         }
1027                 }
1028         }
1029
1030         hide ();
1031
1032         session->finalize_audio_export ();
1033
1034         set_modal (false);
1035         ok_button->set_sensitive(true);
1036 }
1037
1038 void
1039 ExportDialog::start_export ()
1040 {
1041         if (session == 0) {
1042                 return;
1043         }
1044
1045         /* If the filename hasn't been set before, use the
1046            current session's export directory as a default
1047            location for the export.  
1048         */
1049         
1050         if (file_entry.get_text().length() == 0) {
1051
1052                 sys::path export_file_path = session->session_directory().export_path();
1053
1054                 if (!wants_dir()) {
1055                         export_file_path /= "export.wav";
1056                 }
1057                 
1058                 file_entry.set_text (export_file_path.to_string());
1059         }
1060
1061         progress_bar.set_fraction (0);
1062         progress_bar.hide();
1063         progress_bar.set_no_show_all();
1064         cancel_label.set_text (_("Cancel"));
1065
1066         show_all();
1067
1068         if (track_and_master_selection_allowed) {
1069                 track_vpacker.show();
1070         } else {
1071                 track_vpacker.hide();
1072         }
1073
1074         file_chooser.show();
1075
1076         if (channel_count_selection_allowed) {
1077                 channel_count_combo.show();
1078                 channel_count_label.show();
1079         } else {
1080                 channel_count_combo.hide();
1081                 channel_count_label.hide();
1082         }
1083
1084         if (export_cd_markers_allowed) {
1085                 cue_file_label.show();
1086                 cue_file_combo.show();
1087                 cuefile_only_checkbox.show();
1088         } else {
1089                 cue_file_label.hide();
1090                 cue_file_combo.hide();
1091                 cuefile_only_checkbox.hide();
1092         }
1093
1094         if (session->master_out()) {
1095                 track_scroll.hide ();
1096         } else {
1097                 master_scroll.hide ();
1098                 track_selector_button.hide ();
1099         }
1100
1101         track_and_master_selection_allowed = true;
1102         channel_count_selection_allowed =  true;
1103         export_cd_markers_allowed = true;
1104 }
1105
1106 void
1107 ExportDialog::header_chosen ()
1108 {
1109         if (sndfile_header_format_from_string (header_format_combo.get_active_text ()) == SF_FORMAT_WAV) {
1110                 endian_format_combo.set_active_text (N_("Little-endian (Intel)"));
1111                 endian_format_combo.set_sensitive (false);
1112         } else {
1113                 endian_format_combo.set_sensitive (true);
1114                 endian_format_combo.set_active_text (N_("Big-endian (Mac)"));
1115         }
1116 }
1117
1118 void
1119 ExportDialog::bitdepth_chosen ()
1120 {
1121         int format = sndfile_bitdepth_format_from_string (bitdepth_format_combo.get_active_text ());    
1122         switch (format) {
1123         case SF_FORMAT_PCM_24:
1124         case SF_FORMAT_PCM_32:
1125         case SF_FORMAT_FLOAT:
1126                 dither_type_combo.set_sensitive (false);
1127                 break;
1128
1129         default:
1130                 dither_type_combo.set_sensitive (true);
1131                 break;
1132         }
1133 }
1134
1135 void
1136 ExportDialog::cue_file_type_chosen ()
1137 {
1138         if (cue_file_combo.get_active_text () != "None") {
1139                 cuefile_only_checkbox.set_sensitive (true);
1140         } else {
1141                 cuefile_only_checkbox.set_active (false);
1142                 cuefile_only_checkbox.set_sensitive (false);
1143         }
1144 }
1145
1146 void
1147 ExportDialog::file_chooser_selection_changed ()
1148 {
1149
1150   /*
1151     if the user selects an existing file from the 'browse for other folders' tab,
1152     change the format settings to match the file.
1153   */
1154         if (file_chooser.get_filename().length() == 0) {
1155                 return;
1156         }
1157         if (Glib::file_test(file_chooser.get_preview_filename(),Glib::FILE_TEST_IS_DIR)){
1158                 file_chooser.set_current_name (_(""));
1159                 return;
1160         }
1161         if (!Glib::file_test(file_chooser.get_preview_filename(),Glib::FILE_TEST_EXISTS)) {
1162                 return;
1163         }
1164
1165         SoundFileInfo finfo;
1166         string error_msg, format_str;
1167         
1168         if (!AudioFileSource::get_soundfile_info (file_chooser.get_preview_filename(), finfo, error_msg)) {
1169                 error << string_compose(_("Export: cannot open file \"%1\"."), error_msg ) << endmsg;
1170                 return;
1171         }
1172
1173         if (finfo.samplerate == 22050) {
1174                 sample_rate_combo.set_active_text (N_("22.05kHz"));
1175         } else if (finfo.samplerate == 44100) {
1176                 sample_rate_combo.set_active_text (N_("44.1kHz"));
1177         } else if (finfo.samplerate == 48000) {
1178                 sample_rate_combo.set_active_text (N_("48kHz"));
1179         } else if (finfo.samplerate == 88200) {
1180                 sample_rate_combo.set_active_text (N_("88.2kHz"));
1181         } else if (finfo.samplerate == 96000) {
1182                 sample_rate_combo.set_active_text (N_("96kHz"));
1183         } else if (finfo.samplerate == 192000) {
1184                 sample_rate_combo.set_active_text (N_("192kHz"));
1185         } 
1186
1187         if (finfo.channels == 1) {
1188                 channel_count_combo.set_active_text(N_("Mono"));
1189         } else {
1190                 channel_count_combo.set_active_text(N_("Stereo"));
1191         }
1192
1193         string::size_type pos;
1194
1195         pos = finfo.format_name.find_first_of (" ");
1196         format_str = finfo.format_name.substr(pos + 1, 255);
1197         pos = format_str.find_first_of (" ");
1198         header_format_combo.set_active_text(format_str.substr(0, pos));
1199
1200         format_str = finfo.format_name;
1201         pos = format_str.find_first_of (",");
1202         bitdepth_format_combo.set_active_text(format_str.substr(pos + 2, 255));
1203 }
1204
1205 void
1206 ExportDialog::sample_rate_chosen ()
1207 {
1208         string sr_str = sample_rate_combo.get_active_text();
1209         nframes_t rate;
1210
1211         if (sr_str == N_("22.05kHz")) {
1212                 rate = 22050;
1213         } else if (sr_str == _("44.1kHz")) {
1214                 rate = 44100;
1215         } else if (sr_str == _("48kHz")) {
1216                 rate = 48000;
1217         } else if (sr_str == _("88.2kHz")) {
1218                 rate = 88200;
1219         } else if (sr_str == _("96kHz")) {
1220                 rate = 96000;
1221         } else if (sr_str == _("192kHz")) {
1222                 rate = 192000;
1223         } else {
1224                 rate = session->frame_rate();
1225         }
1226                 
1227         if (rate != session->frame_rate()) {
1228                 src_quality_combo.set_sensitive (true);
1229         } else {
1230                 src_quality_combo.set_sensitive (false);
1231         }
1232 }
1233
1234 void
1235 ExportDialog::channels_chosen ()
1236 {
1237         bool mono;
1238
1239         mono = (channel_count_combo.get_active_text() == _("Mono"));
1240
1241         if (mono) {
1242                 track_selector.get_column(2)->set_visible(false);
1243                 track_selector.get_column(1)->set_title(_("Export"));
1244
1245                 if (session->master_out()) {
1246                         master_selector.get_column(2)->set_visible(false);
1247                         master_selector.get_column(1)->set_title(_("Export"));
1248                 }
1249
1250         } else {
1251                 track_selector.get_column(2)->set_visible(true);
1252                 track_selector.get_column(1)->set_title(_("Left"));
1253
1254                 if (session->master_out()) {
1255                         master_selector.get_column(2)->set_visible(true);
1256                         master_selector.get_column(1)->set_title(_("Left"));
1257                 }
1258         }
1259
1260         fill_lists();
1261 }
1262
1263 void
1264 ExportDialog::fill_lists ()
1265 {
1266         track_list->clear();
1267         master_list->clear();
1268         
1269         boost::shared_ptr<Session::RouteList> routes = session->get_routes ();
1270
1271         for (Session::RouteList::iterator ri = routes->begin(); ri != routes->end(); ++ri) {
1272                 
1273                 boost::shared_ptr<Route> route = (*ri);
1274                 
1275                 if (route->is_hidden()) {
1276                         continue;
1277                 }
1278
1279                 for (uint32_t i=0; i < route->n_outputs().n_audio(); ++i) {
1280                         string name;
1281                         if (route->n_outputs().n_audio() == 1) {
1282                                 name = route->name();
1283                         } else {
1284                                 name = string_compose("%1: out-%2", route->name(), i+1);
1285                         }
1286
1287                         if (route == session->master_out()) {
1288                                 TreeModel::iterator iter = master_list->append();
1289                                 TreeModel::Row row = *iter;
1290                                 row[exp_cols.output] = name;
1291                                 row[exp_cols.left] = false;
1292                                 row[exp_cols.right] = false;
1293                                 row[exp_cols.port] = route->output (i);
1294                         } else {
1295                                 TreeModel::iterator iter = track_list->append();
1296                                 TreeModel::Row row = *iter;
1297                                 row[exp_cols.output] = name;
1298                                 row[exp_cols.left] = false;
1299                                 row[exp_cols.right] = false;
1300                                 row[exp_cols.port] = route->output (i);
1301                         }
1302                 }
1303         }
1304 }
1305
1306
1307 bool
1308 ExportDialog::is_filepath_valid(string &filepath)
1309 {
1310         // sanity check file name first
1311
1312         struct stat statbuf;
1313   
1314         if (filepath.empty()) {
1315                 string txt = _("Please enter a valid filename.");
1316                 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
1317                 msg.run();
1318                 return false;
1319         }
1320         
1321         // check if file exists already and warn
1322
1323         if (stat (filepath.c_str(), &statbuf) == 0) {
1324                 if (S_ISDIR (statbuf.st_mode)) {
1325                         string txt = _("Please specify a complete filename for the audio file.");
1326                         MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
1327                         msg.run();
1328                         return false;
1329                 }
1330                 else {
1331                         string txt = _("File ") + filepath + _(" already exists, do you want to overwrite it?");
1332                         MessageDialog msg (*this, txt, false, MESSAGE_QUESTION, BUTTONS_YES_NO, true);
1333                         if ((ResponseType) msg.run() == Gtk::RESPONSE_NO) {
1334                                 return false;
1335                         }
1336                 }
1337         }
1338         
1339         // directory needs to exist and be writable
1340
1341         string dirpath = Glib::path_get_dirname (filepath);
1342         if (::access (dirpath.c_str(), W_OK) != 0) {
1343                 string txt = _("Cannot write file in: ") + dirpath;
1344                 MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
1345                 msg.run();
1346                 return false;
1347         }
1348         
1349         return true;
1350 }
1351
1352 void
1353 ExportDialog::initSpec(string &filepath)
1354 {
1355         spec.path = filepath;
1356         spec.progress = 0;
1357         spec.running = true;
1358         spec.stop = false;
1359         spec.port_map.clear();
1360         
1361         if (channel_count_combo.get_active_text() == _("Mono")) {
1362                 spec.channels = 1;
1363         } else {
1364                 spec.channels = 2;
1365         }
1366
1367         spec.format = 0;
1368
1369         spec.format |= sndfile_header_format_from_string (header_format_combo.get_active_text ());
1370
1371         if (!Profile->get_sae()) {
1372                 if ((spec.format & SF_FORMAT_WAV) == 0) {
1373                         /* RIFF/WAV specifies endianess */
1374                         spec.format |= sndfile_endian_format_from_string (endian_format_combo.get_active_text ());
1375                 }
1376         }
1377
1378         spec.format |= sndfile_bitdepth_format_from_string (bitdepth_format_combo.get_active_text ());
1379
1380         string sr_str = sample_rate_combo.get_active_text();
1381         if (sr_str == N_("22.05kHz")) {
1382                 spec.sample_rate = 22050;
1383         } else if (sr_str == _("44.1kHz")) {
1384                 spec.sample_rate = 44100;
1385         } else if (sr_str == _("48kHz")) {
1386                 spec.sample_rate = 48000;
1387         } else if (sr_str == _("88.2kHz")) {
1388                 spec.sample_rate = 88200;
1389         } else if (sr_str == _("96kHz")) {
1390                 spec.sample_rate = 96000;
1391         } else if (sr_str == _("192kHz")) {
1392                 spec.sample_rate = 192000;
1393         } else {
1394                 spec.sample_rate = session->frame_rate();
1395         }
1396         
1397         if (Profile->get_sae()) {
1398                 spec.src_quality = SRC_SINC_BEST_QUALITY;
1399         } else {
1400                 string src_str = src_quality_combo.get_active_text();
1401                 if (src_str == _("fastest")) {
1402                         spec.src_quality = SRC_ZERO_ORDER_HOLD;
1403                 } else if (src_str == _("linear")) {
1404                         spec.src_quality = SRC_LINEAR;
1405                 } else if (src_str == _("better")) {
1406                         spec.src_quality = SRC_SINC_FASTEST;
1407                 } else if (src_str == _("intermediate")) {
1408                         spec.src_quality = SRC_SINC_MEDIUM_QUALITY;
1409                 } else {
1410                         spec.src_quality = SRC_SINC_BEST_QUALITY;
1411                 }
1412         }
1413
1414         string dither_str = dither_type_combo.get_active_text();
1415         if (dither_str == _("None")) {
1416                 spec.dither_type = GDitherNone;
1417         } else if (dither_str == _("Rectangular")) {
1418                 spec.dither_type = GDitherRect;
1419         } else if (dither_str == _("Triangular")) {
1420                 spec.dither_type = GDitherTri;
1421         } else {
1422                 spec.dither_type = GDitherShaped;
1423         } 
1424
1425         write_track_and_master_selection_to_spec();
1426 }
1427
1428
1429 void
1430 ExportDialog::write_track_and_master_selection_to_spec()
1431 {
1432         if(!track_and_master_selection_allowed){
1433                 return;
1434         }
1435
1436         uint32_t chan=0;
1437         Port *last_port = 0;
1438                 
1439         TreeModel::Children rows = master_selector.get_model()->children();
1440         TreeModel::Children::iterator ri;
1441         TreeModel::Row row;
1442         for (ri = rows.begin(); ri != rows.end(); ++ri) {
1443                 row = *ri;
1444                 Port* port = row[exp_cols.port];
1445                 
1446                 if (last_port != port) {
1447                         chan = 0;
1448                 }
1449                 
1450                 if (row[exp_cols.left]) {
1451                         spec.port_map[0].push_back (std::pair<Port*,uint32_t>(port, chan));
1452                 } 
1453                 
1454                 if (spec.channels == 2) {
1455                         if (row[exp_cols.right]) {
1456                                 spec.port_map[1].push_back (std::pair<Port*,uint32_t>(port, chan));
1457                         }
1458                 }
1459         }
1460         
1461         chan = 0;
1462         rows = track_selector.get_model()->children();
1463
1464         for (ri = rows.begin(); ri != rows.end(); ++ri) {
1465                 row = *ri;
1466                 
1467                 Port* port = row[exp_cols.port];
1468                 
1469                 if (last_port != port) {
1470                         chan = 0;
1471                 }
1472                 
1473                 if (row[exp_cols.left]) {
1474                         spec.port_map[0].push_back (std::pair<Port*,uint32_t>(port, chan));
1475                 } 
1476                 
1477                 if (spec.channels == 2) {
1478                         if (row[exp_cols.right]) {
1479                                 spec.port_map[1].push_back (std::pair<Port*,uint32_t>(port, chan));
1480                         }
1481                         
1482                 }
1483                 
1484                 last_port = port;
1485                 ++chan;
1486         }
1487 }
1488
1489
1490 gint
1491 ExportDialog::window_closed (GdkEventAny *ignored)
1492 {
1493         end_dialog ();
1494         return TRUE;
1495 }
1496
1497 void
1498 ExportDialog::track_selector_button_click ()
1499 {
1500         if (track_scroll.is_visible ()) {
1501                 track_scroll.hide ();
1502         } else {
1503                 track_scroll.show_all ();
1504         }
1505 }