2 Copyright (C) 2008 Paul Davis
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.
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.
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.
21 #include "ardour/export_format_manager.h"
23 #include "ardour/types.h"
24 #include "ardour/export_format_specification.h"
25 #include "ardour/export_format_compatibility.h"
34 ExportFormatManager::ExportFormatManager (ExportFormatSpecPtr specification) :
35 pending_selection_change (false),
36 universal_set (new ExportFormatBase ())
38 current_selection = specification;
40 init_compatibilities ();
46 ExportFormatManager::~ExportFormatManager ()
52 ExportFormatManager::init_compatibilities ()
54 ExportFormatCompatibilityPtr c_ptr;
56 c_ptr.reset (new ExportFormatCompatibility (_("CD")));
57 c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
58 c_ptr->add_format_id (ExportFormatBase::F_WAV);
59 c_ptr->add_format_id (ExportFormatBase::F_AIFF);
60 c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
61 c_ptr->add_sample_format (ExportFormatBase::SF_16);
62 c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
63 add_compatibility (c_ptr);
65 c_ptr.reset (new ExportFormatCompatibility (_("DVD-A")));
66 c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
67 c_ptr->add_sample_rate (ExportFormatBase::SR_48);
68 c_ptr->add_sample_rate (ExportFormatBase::SR_88_2);
69 c_ptr->add_sample_rate (ExportFormatBase::SR_96);
70 c_ptr->add_sample_rate (ExportFormatBase::SR_192);
71 c_ptr->add_format_id (ExportFormatBase::F_WAV);
72 c_ptr->add_format_id (ExportFormatBase::F_AIFF);
73 c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
74 c_ptr->add_sample_format (ExportFormatBase::SF_16);
75 c_ptr->add_sample_format (ExportFormatBase::SF_24);
76 c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
77 add_compatibility (c_ptr);
79 c_ptr.reset (new ExportFormatCompatibility (_("iPod")));
80 c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
81 c_ptr->add_sample_rate (ExportFormatBase::SR_48);
82 c_ptr->add_format_id (ExportFormatBase::F_WAV);
83 c_ptr->add_format_id (ExportFormatBase::F_AIFF);
84 c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
85 c_ptr->add_sample_format (ExportFormatBase::SF_16);
86 c_ptr->add_sample_format (ExportFormatBase::SF_24);
87 c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
88 add_compatibility (c_ptr);
90 c_ptr.reset (new ExportFormatCompatibility (_("Something else")));
91 c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
92 c_ptr->add_sample_rate (ExportFormatBase::SR_48);
93 c_ptr->add_format_id (ExportFormatBase::F_WAV);
94 c_ptr->add_format_id (ExportFormatBase::F_AIFF);
95 c_ptr->add_format_id (ExportFormatBase::F_AU);
96 c_ptr->add_format_id (ExportFormatBase::F_FLAC);
97 c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
98 c_ptr->add_quality (ExportFormatBase::Q_LosslessCompression);
99 c_ptr->add_sample_format (ExportFormatBase::SF_16);
100 c_ptr->add_sample_format (ExportFormatBase::SF_24);
101 c_ptr->add_sample_format (ExportFormatBase::SF_32);
102 c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
103 add_compatibility (c_ptr);
107 ExportFormatManager::init_qualities ()
109 add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_Any, _("Any"))));
110 add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_LosslessLinear, _("Lossless (linear PCM)"))));
111 add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_LossyCompression, _("Lossy compression"))));
112 add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_LosslessCompression, _("Lossless compression"))));
116 ExportFormatManager::init_formats ()
118 ExportFormatPtr f_ptr;
119 ExportFormatLinear * fl_ptr;
121 f_ptr.reset (fl_ptr = new ExportFormatLinear ("AIFF", ExportFormatBase::F_AIFF));
122 fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
123 fl_ptr->add_sample_format (ExportFormatBase::SF_8);
124 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
125 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
126 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
127 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
128 fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
129 fl_ptr->add_endianness (ExportFormatBase::E_Big);
130 fl_ptr->set_default_sample_format (ExportFormatBase::SF_16);
131 fl_ptr->set_extension ("aiff");
134 f_ptr.reset (fl_ptr = new ExportFormatLinear ("AU", ExportFormatBase::F_AU));
135 fl_ptr->add_sample_format (ExportFormatBase::SF_8);
136 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
137 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
138 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
139 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
140 fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
141 fl_ptr->set_default_sample_format (ExportFormatBase::SF_16);
142 fl_ptr->set_extension ("au");
145 f_ptr.reset (new ExportFormatBWF ());
148 f_ptr.reset (fl_ptr = new ExportFormatLinear ("IRCAM", ExportFormatBase::F_IRCAM));
149 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
150 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
151 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
152 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
153 fl_ptr->set_default_sample_format (ExportFormatBase::SF_24);
154 fl_ptr->set_extension ("sf");
157 f_ptr.reset (fl_ptr = new ExportFormatLinear ("WAV", ExportFormatBase::F_WAV));
158 fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
159 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
160 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
161 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
162 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
163 fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
164 fl_ptr->add_endianness (ExportFormatBase::E_Little);
165 fl_ptr->set_default_sample_format (ExportFormatBase::SF_16);
166 fl_ptr->set_extension ("wav");
169 f_ptr.reset (fl_ptr = new ExportFormatLinear ("W64", ExportFormatBase::F_W64));
170 fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
171 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
172 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
173 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
174 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
175 fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
176 fl_ptr->set_default_sample_format (ExportFormatBase::SF_Double);
177 fl_ptr->set_extension ("w64");
180 f_ptr.reset (fl_ptr = new ExportFormatLinear ("RAW", ExportFormatBase::F_RAW));
181 fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
182 fl_ptr->add_sample_format (ExportFormatBase::SF_8);
183 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
184 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
185 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
186 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
187 fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
188 fl_ptr->set_default_sample_format (ExportFormatBase::SF_Float);
189 fl_ptr->set_extension ("raw");
193 f_ptr.reset (new ExportFormatOggVorbis ());
195 } catch (ExportFormatIncompatible & e) {}
198 f_ptr.reset (new ExportFormatFLAC ());
200 } catch (ExportFormatIncompatible & e) {}
204 ExportFormatManager::init_sample_rates ()
206 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_Session, _("Session rate"))));
207 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_22_05, "22,05 kHz")));
208 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_44_1, "44,1 kHz")));
209 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_48, "48 kHz")));
210 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_88_2, "88,2 kHz")));
211 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_96, "96 kHz")));
212 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_192, "192 kHz")));
216 ExportFormatManager::add_compatibility (ExportFormatCompatibilityPtr ptr)
218 compatibilities.push_back (ptr);
219 ptr->SelectChanged.connect_same_thread (*this,
220 boost::bind (&ExportFormatManager::change_compatibility_selection,
221 this, _1, WeakExportFormatCompatibilityPtr (ptr)));
225 ExportFormatManager::add_quality (QualityPtr ptr)
227 ptr->SelectChanged.connect_same_thread (*this, boost::bind (&ExportFormatManager::change_quality_selection, this, _1, WeakQualityPtr (ptr)));
228 qualities.push_back (ptr);
232 ExportFormatManager::add_format (ExportFormatPtr ptr)
234 formats.push_back (ptr);
235 ptr->SelectChanged.connect_same_thread (*this, boost::bind (&ExportFormatManager::change_format_selection, this, _1, WeakExportFormatPtr (ptr)));
236 universal_set = universal_set->get_union (*ptr);
238 /* Encoding options */
240 boost::shared_ptr<HasSampleFormat> hsf;
242 if (hsf = boost::dynamic_pointer_cast<HasSampleFormat> (ptr)) {
243 hsf->SampleFormatSelectChanged.connect_same_thread (*this, boost::bind (&ExportFormatManager::change_sample_format_selection, this, _1, _2));
244 hsf->DitherTypeSelectChanged.connect_same_thread (*this, boost::bind (&ExportFormatManager::change_dither_type_selection, this, _1, _2));
249 ExportFormatManager::add_sample_rate (SampleRatePtr ptr)
251 ptr->SelectChanged.connect_same_thread (*this, boost::bind (&ExportFormatManager::change_sample_rate_selection, this, _1, WeakSampleRatePtr (ptr)));
252 sample_rates.push_back (ptr);
256 ExportFormatManager::set_name (string name)
258 current_selection->set_name (name);
262 ExportFormatManager::select_src_quality (ExportFormatBase::SRCQuality value)
264 current_selection->set_src_quality (value);
268 ExportFormatManager::select_trim_beginning (bool value)
270 current_selection->set_trim_beginning (value);
274 ExportFormatManager::select_silence_beginning (AnyTime const & time)
276 current_selection->set_silence_beginning (time);
280 ExportFormatManager::select_trim_end (bool value)
282 current_selection->set_trim_end (value);
286 ExportFormatManager::select_silence_end (AnyTime const & time)
288 current_selection->set_silence_end (time);
292 ExportFormatManager::select_normalize (bool value)
294 current_selection->set_normalize (value);
298 ExportFormatManager::select_normalize_target (float value)
300 current_selection->set_normalize_target (value);
304 ExportFormatManager::select_tagging (bool tag)
306 current_selection->set_tag (tag);
310 ExportFormatManager::change_compatibility_selection (bool select, WeakExportFormatCompatibilityPtr const & compat)
312 bool do_selection_changed = !pending_selection_change;
313 if (!pending_selection_change) {
314 pending_selection_change = true;
317 ExportFormatCompatibilityPtr ptr = compat.lock();
320 select_compatibility (ptr);
323 if (do_selection_changed) {
324 selection_changed ();
329 ExportFormatManager::change_quality_selection (bool select, WeakQualityPtr const & quality)
331 QualityPtr ptr = quality.lock ();
338 select_quality (ptr);
339 } else if (ptr->quality == current_selection->quality()) {
341 select_quality (ptr);
346 ExportFormatManager::change_format_selection (bool select, WeakExportFormatPtr const & format)
348 ExportFormatPtr ptr = format.lock();
356 } else if (ptr->get_format_id() == current_selection->format_id()) {
363 ExportFormatManager::change_sample_rate_selection (bool select, WeakSampleRatePtr const & rate)
365 SampleRatePtr ptr = rate.lock();
372 select_sample_rate (ptr);
373 } else if (ptr->rate == current_selection->sample_rate()) {
375 select_sample_rate (ptr);
380 ExportFormatManager::change_sample_format_selection (bool select, WeakSampleFormatPtr const & format)
382 SampleFormatPtr ptr = format.lock();
389 select_sample_format (ptr);
390 } else if (ptr->format == current_selection->sample_format()) {
392 select_sample_format (ptr);
397 ExportFormatManager::change_dither_type_selection (bool select, WeakDitherTypePtr const & type)
399 DitherTypePtr ptr = type.lock();
406 select_dither_type (ptr);
407 } else if (ptr->type == current_selection->dither_type()) {
409 select_dither_type (ptr);
414 ExportFormatManager::select_compatibility (WeakExportFormatCompatibilityPtr const & /*compat*/)
416 /* Calculate compatibility intersection for the selection */
418 ExportFormatBasePtr compat_intersect = get_compatibility_intersection ();
420 /* Unselect incompatible items */
422 boost::shared_ptr<ExportFormatBase> select_intersect;
424 select_intersect = compat_intersect->get_intersection (*current_selection);
425 if (select_intersect->qualities_empty()) {
426 select_quality (QualityPtr());
429 select_intersect = compat_intersect->get_intersection (*current_selection);
430 if (select_intersect->formats_empty()) {
431 select_format (ExportFormatPtr());
434 select_intersect = compat_intersect->get_intersection (*current_selection);
435 if (select_intersect->sample_rates_empty()) {
436 select_sample_rate (SampleRatePtr());
439 select_intersect = compat_intersect->get_intersection (*current_selection);
440 if (select_intersect->sample_formats_empty()) {
441 select_sample_format (SampleFormatPtr());
446 ExportFormatManager::select_quality (QualityPtr const & quality)
448 bool do_selection_changed = !pending_selection_change;
449 if (!pending_selection_change) {
450 pending_selection_change = true;
454 current_selection->set_quality (quality->quality);
456 /* Deselect format if it is incompatible */
458 ExportFormatPtr format = get_selected_format();
459 if (format && !format->has_quality (quality->quality)) {
460 format->set_selected (false);
464 current_selection->set_quality (ExportFormatBase::Q_None);
466 QualityPtr current_quality = get_selected_quality();
467 if (current_quality) {
468 current_quality->set_selected (false);
472 * A quality is never explicitly deselected without also deselecting the format
473 * so we don't need to deselect the format here.
474 * doing so causes extra complications
478 if (do_selection_changed) {
479 selection_changed ();
484 ExportFormatManager::select_format (ExportFormatPtr const & format)
486 bool do_selection_changed = !pending_selection_change;
487 if (!pending_selection_change) {
488 pending_selection_change = true;
491 current_selection->set_format (format);
495 /* Slect right quality for format */
497 ExportFormatBase::Quality quality = format->get_quality();
498 for (QualityList::iterator it = qualities.begin (); it != qualities.end (); ++it) {
499 if ((*it)->quality == quality) {
500 (*it)->set_selected (true);
502 (*it)->set_selected (false);
506 /* Handle sample formats */
508 ExportFormatBase::SampleFormat format_to_select;
509 if (format->sample_format_is_compatible (current_selection->sample_format())) {
510 format_to_select = current_selection->sample_format();
512 format_to_select = format->default_sample_format();
515 boost::shared_ptr<HasSampleFormat> hsf;
516 if ((hsf = boost::dynamic_pointer_cast<HasSampleFormat> (format))) {
517 SampleFormatList sample_formats = hsf->get_sample_formats();
518 for (SampleFormatList::iterator it = sample_formats.begin (); it != sample_formats.end (); ++it) {
519 if ((*it)->format == format_to_select) {
520 (*it)->set_selected (true);
522 (*it)->set_selected (false);
527 current_selection->set_sample_format (format_to_select);
530 ExportFormatPtr current_format = get_selected_format ();
531 if (current_format) {
532 current_format->set_selected (false);
536 if (do_selection_changed) {
537 selection_changed ();
542 ExportFormatManager::select_sample_rate (SampleRatePtr const & rate)
545 bool do_selection_changed = !pending_selection_change;
546 if (!pending_selection_change) {
547 pending_selection_change = true;
551 current_selection->set_sample_rate (rate->rate);
553 current_selection->set_sample_rate (ExportFormatBase::SR_None);
555 SampleRatePtr current_rate = get_selected_sample_rate();
557 current_rate->set_selected (false);
561 if (do_selection_changed) {
562 selection_changed ();
567 ExportFormatManager::select_sample_format (SampleFormatPtr const & format)
570 bool do_selection_changed = !pending_selection_change;
571 if (!pending_selection_change) {
572 pending_selection_change = true;
576 current_selection->set_sample_format (format->format);
578 current_selection->set_sample_format (ExportFormatBase::SF_None);
580 SampleFormatPtr current_format = get_selected_sample_format();
581 if (current_format) {
582 current_format->set_selected (false);
586 if (do_selection_changed) {
587 selection_changed ();
592 ExportFormatManager::select_dither_type (DitherTypePtr const & type)
595 bool do_selection_changed = !pending_selection_change;
596 if (!pending_selection_change) {
597 pending_selection_change = true;
601 current_selection->set_dither_type (type->type);
603 current_selection->set_dither_type (ExportFormatBase::D_None);
606 if (do_selection_changed) {
607 selection_changed ();
612 ExportFormatManager::selection_changed ()
614 /* Get a list of incompatible compatibility selections */
616 CompatList incompatibles;
617 for (CompatList::iterator it = compatibilities.begin(); it != compatibilities.end(); ++it) {
618 if (!current_selection->is_compatible_with (**it)) {
619 incompatibles.push_back (*it);
625 for (CompatList::iterator it = incompatibles.begin(); it != incompatibles.end(); ++it) {
626 (*it)->set_selected (false);
629 /* Mark compatibility for everything necessary */
631 std::set<ExportFormatBase::Quality> compatible_qualities;
632 ExportFormatBasePtr compat_intersect = get_compatibility_intersection ();
633 ExportFormatCompatibility global_compat (*compat_intersect);
635 for (FormatList::iterator it = formats.begin(); it != formats.end(); ++it) {
636 if ((*it)->set_compatibility_state (global_compat)) {
637 compatible_qualities.insert ((*it)->get_quality());
641 bool any_quality_compatible = true;
642 for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
643 if (compatible_qualities.find((*it)->quality) != compatible_qualities.end()) {
644 (*it)->set_compatible (true);
647 (*it)->set_compatible (false);
649 if ((*it)->quality != ExportFormatBase::Q_Any) {
650 any_quality_compatible = false;
655 if (any_quality_compatible) {
656 for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
657 if ((*it)->quality == ExportFormatBase::Q_Any) {
658 (*it)->set_compatible (true);
664 for (SampleRateList::iterator it = sample_rates.begin(); it != sample_rates.end(); ++it) {
665 if (compat_intersect->has_sample_rate ((*it)->rate)) {
666 (*it)->set_compatible (true);
668 (*it)->set_compatible (false);
672 boost::shared_ptr<HasSampleFormat> hsf;
673 if (hsf = boost::dynamic_pointer_cast<HasSampleFormat> (get_selected_format())) {
675 SampleFormatList sf_list = hsf->get_sample_formats();
676 for (SampleFormatList::iterator it = sf_list.begin(); it != sf_list.end(); ++it) {
677 if (compat_intersect->has_sample_format ((*it)->format)) {
678 (*it)->set_compatible (true);
680 (*it)->set_compatible (false);
686 /* Signal completeness */
688 CompleteChanged (current_selection->is_complete());
690 /* Reset pending state */
692 pending_selection_change = false;
695 ExportFormatManager::QualityPtr
696 ExportFormatManager::get_selected_quality ()
698 for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
699 if ((*it)->selected()) {
708 ExportFormatManager::get_selected_format ()
710 ExportFormatPtr format;
712 for (FormatList::iterator it = formats.begin(); it != formats.end(); ++it) {
713 if ((*it)->selected()) {
721 ExportFormatManager::SampleRatePtr
722 ExportFormatManager::get_selected_sample_rate ()
724 for (SampleRateList::iterator it = sample_rates.begin(); it != sample_rates.end(); ++it) {
725 if ((*it)->selected()) {
730 return SampleRatePtr();
733 ExportFormatManager::SampleFormatPtr
734 ExportFormatManager::get_selected_sample_format ()
736 boost::shared_ptr<HasSampleFormat> hsf;
738 if (hsf = boost::dynamic_pointer_cast<HasSampleFormat> (get_selected_format())) {
739 return hsf->get_selected_sample_format ();
741 return SampleFormatPtr ();
747 ExportFormatManager::get_compatibility_intersection ()
749 ExportFormatBasePtr compat_intersect = universal_set;
751 for (CompatList::iterator it = compatibilities.begin(); it != compatibilities.end(); ++it) {
752 if ((*it)->selected ()) {
753 compat_intersect = compat_intersect->get_intersection (**it);
757 return compat_intersect;
760 }; // namespace ARDOUR