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>
32 ExportFormatManager::ExportFormatManager (SpecPtr specification) :
33 pending_selection_change (false),
34 universal_set (new ExportFormatBase ())
36 current_selection = specification;
38 init_compatibilities ();
44 ExportFormatManager::~ExportFormatManager ()
50 ExportFormatManager::init_compatibilities ()
54 c_ptr.reset (new ExportFormatCompatibility (_("CD")));
55 c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
56 c_ptr->add_format_id (ExportFormatBase::F_WAV);
57 c_ptr->add_format_id (ExportFormatBase::F_AIFF);
58 c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
59 c_ptr->add_sample_format (ExportFormatBase::SF_16);
60 c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
61 add_compatibility (c_ptr);
63 c_ptr.reset (new ExportFormatCompatibility (_("DVD-A")));
64 c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
65 c_ptr->add_sample_rate (ExportFormatBase::SR_48);
66 c_ptr->add_sample_rate (ExportFormatBase::SR_88_2);
67 c_ptr->add_sample_rate (ExportFormatBase::SR_96);
68 c_ptr->add_sample_rate (ExportFormatBase::SR_192);
69 c_ptr->add_format_id (ExportFormatBase::F_WAV);
70 c_ptr->add_format_id (ExportFormatBase::F_AIFF);
71 c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
72 c_ptr->add_sample_format (ExportFormatBase::SF_16);
73 c_ptr->add_sample_format (ExportFormatBase::SF_24);
74 c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
75 add_compatibility (c_ptr);
77 c_ptr.reset (new ExportFormatCompatibility (_("iPod")));
78 c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
79 c_ptr->add_sample_rate (ExportFormatBase::SR_48);
80 c_ptr->add_format_id (ExportFormatBase::F_WAV);
81 c_ptr->add_format_id (ExportFormatBase::F_AIFF);
82 c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
83 c_ptr->add_sample_format (ExportFormatBase::SF_16);
84 c_ptr->add_sample_format (ExportFormatBase::SF_24);
85 c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
86 add_compatibility (c_ptr);
88 c_ptr.reset (new ExportFormatCompatibility (_("Something else")));
89 c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
90 c_ptr->add_sample_rate (ExportFormatBase::SR_48);
91 c_ptr->add_format_id (ExportFormatBase::F_WAV);
92 c_ptr->add_format_id (ExportFormatBase::F_AIFF);
93 c_ptr->add_format_id (ExportFormatBase::F_AU);
94 c_ptr->add_format_id (ExportFormatBase::F_FLAC);
95 c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
96 c_ptr->add_quality (ExportFormatBase::Q_LosslessCompression);
97 c_ptr->add_sample_format (ExportFormatBase::SF_16);
98 c_ptr->add_sample_format (ExportFormatBase::SF_24);
99 c_ptr->add_sample_format (ExportFormatBase::SF_32);
100 c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
101 add_compatibility (c_ptr);
105 ExportFormatManager::init_qualities ()
107 add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_Any, _("Any"))));
108 add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_LosslessLinear, _("Lossless (linear PCM)"))));
109 add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_LossyCompression, _("Lossy compression"))));
110 add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_LosslessCompression, _("Lossless compression"))));
114 ExportFormatManager::init_formats ()
117 ExportFormatLinear * fl_ptr;
119 f_ptr.reset (fl_ptr = new ExportFormatLinear ("AIFF", ExportFormatBase::F_AIFF));
120 fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
121 fl_ptr->add_sample_format (ExportFormatBase::SF_8);
122 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
123 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
124 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
125 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
126 fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
127 fl_ptr->set_default_sample_format (ExportFormatBase::SF_16);
128 fl_ptr->set_extension ("aiff");
131 f_ptr.reset (fl_ptr = new ExportFormatLinear ("AU", ExportFormatBase::F_AU));
132 fl_ptr->add_sample_format (ExportFormatBase::SF_8);
133 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
134 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
135 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
136 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
137 fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
138 fl_ptr->set_default_sample_format (ExportFormatBase::SF_16);
139 fl_ptr->set_extension ("au");
142 f_ptr.reset (new ExportFormatBWF ());
145 f_ptr.reset (fl_ptr = new ExportFormatLinear ("IRCAM", ExportFormatBase::F_IRCAM));
146 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
147 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
148 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
149 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
150 fl_ptr->set_default_sample_format (ExportFormatBase::SF_24);
151 fl_ptr->set_extension ("sf");
154 f_ptr.reset (fl_ptr = new ExportFormatLinear ("WAV", ExportFormatBase::F_WAV));
155 fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
156 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
157 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
158 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
159 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
160 fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
161 fl_ptr->set_default_sample_format (ExportFormatBase::SF_16);
162 fl_ptr->set_extension ("wav");
165 f_ptr.reset (fl_ptr = new ExportFormatLinear ("W64", ExportFormatBase::F_W64));
166 fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
167 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
168 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
169 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
170 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
171 fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
172 fl_ptr->set_default_sample_format (ExportFormatBase::SF_Double);
173 fl_ptr->set_extension ("w64");
176 f_ptr.reset (fl_ptr = new ExportFormatLinear ("RAW", ExportFormatBase::F_RAW));
177 fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
178 fl_ptr->add_sample_format (ExportFormatBase::SF_8);
179 fl_ptr->add_sample_format (ExportFormatBase::SF_16);
180 fl_ptr->add_sample_format (ExportFormatBase::SF_24);
181 fl_ptr->add_sample_format (ExportFormatBase::SF_32);
182 fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
183 fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
184 fl_ptr->set_default_sample_format (ExportFormatBase::SF_Float);
185 fl_ptr->set_extension ("raw");
188 f_ptr.reset (new ExportFormatOggVorbis ());
191 f_ptr.reset (new ExportFormatFLAC ());
196 ExportFormatManager::init_sample_rates ()
198 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_22_05, "22,05 kHz")));
199 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_44_1, "44,1 kHz")));
200 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_48, "48 kHz")));
201 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_88_2, "88,2 kHz")));
202 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_96, "96 kHz")));
203 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_192, "192 kHz")));
207 ExportFormatManager::add_compatibility (CompatPtr ptr)
209 compatibilities.push_back (ptr);
210 ptr->SelectChanged.connect (sigc::bind (sigc::mem_fun (*this, &ExportFormatManager::change_compatibility_selection), WeakCompatPtr (ptr)));
214 ExportFormatManager::add_quality (QualityPtr ptr)
216 ptr->SelectChanged.connect (sigc::bind (sigc::mem_fun (*this, &ExportFormatManager::change_quality_selection), WeakQualityPtr (ptr)));
217 qualities.push_back (ptr);
221 ExportFormatManager::add_format (FormatPtr ptr)
223 formats.push_back (ptr);
224 ptr->SelectChanged.connect (sigc::bind (sigc::mem_fun (*this, &ExportFormatManager::change_format_selection), WeakFormatPtr (ptr)));
225 universal_set = universal_set->get_union (*ptr);
227 /* Encoding options */
229 boost::shared_ptr<HasSampleFormat> hsf;
231 if (hsf = boost::dynamic_pointer_cast<HasSampleFormat> (ptr)) {
232 hsf->SampleFormatSelectChanged.connect (sigc::mem_fun (*this, &ExportFormatManager::change_sample_format_selection));
233 hsf->DitherTypeSelectChanged.connect (sigc::mem_fun (*this, &ExportFormatManager::change_dither_type_selection));
238 ExportFormatManager::add_sample_rate (SampleRatePtr ptr)
240 ptr->SelectChanged.connect (sigc::bind (sigc::mem_fun (*this, &ExportFormatManager::change_sample_rate_selection), WeakSampleRatePtr (ptr)));
241 sample_rates.push_back (ptr);
245 ExportFormatManager::set_name (Glib::ustring name)
247 current_selection->set_name (name);
251 ExportFormatManager::select_src_quality (ExportFormatBase::SRCQuality value)
253 current_selection->set_src_quality (value);
257 ExportFormatManager::select_trim_beginning (bool value)
259 current_selection->set_trim_beginning (value);
263 ExportFormatManager::select_silence_beginning (AnyTime const & time)
265 current_selection->set_silence_beginning (time);
269 ExportFormatManager::select_trim_end (bool value)
271 current_selection->set_trim_end (value);
275 ExportFormatManager::select_silence_end (AnyTime const & time)
277 current_selection->set_silence_end (time);
281 ExportFormatManager::select_normalize (bool value)
283 current_selection->set_normalize (value);
287 ExportFormatManager::select_normalize_target (float value)
289 current_selection->set_normalize_target (value);
293 ExportFormatManager::select_tagging (bool tag)
295 current_selection->set_tag (tag);
299 ExportFormatManager::change_compatibility_selection (bool select, WeakCompatPtr const & compat)
301 bool do_selection_changed = !pending_selection_change;
302 if (!pending_selection_change) {
303 pending_selection_change = true;
306 CompatPtr ptr = compat.lock();
309 select_compatibility (ptr);
312 if (do_selection_changed) {
313 selection_changed ();
318 ExportFormatManager::change_quality_selection (bool select, WeakQualityPtr const & quality)
320 QualityPtr ptr = quality.lock ();
327 select_quality (ptr);
328 } else if (ptr->quality == current_selection->quality()) {
330 select_quality (ptr);
335 ExportFormatManager::change_format_selection (bool select, WeakFormatPtr const & format)
337 FormatPtr ptr = format.lock();
345 } else if (ptr->get_format_id() == current_selection->format_id()) {
352 ExportFormatManager::change_sample_rate_selection (bool select, WeakSampleRatePtr const & rate)
354 SampleRatePtr ptr = rate.lock();
361 select_sample_rate (ptr);
362 } else if (ptr->rate == current_selection->sample_rate()) {
364 select_sample_rate (ptr);
369 ExportFormatManager::change_sample_format_selection (bool select, WeakSampleFormatPtr const & format)
371 SampleFormatPtr ptr = format.lock();
378 select_sample_format (ptr);
379 } else if (ptr->format == current_selection->sample_format()) {
381 select_sample_format (ptr);
386 ExportFormatManager::change_dither_type_selection (bool select, WeakDitherTypePtr const & type)
388 DitherTypePtr ptr = type.lock();
395 select_dither_type (ptr);
396 } else if (ptr->type == current_selection->dither_type()) {
398 select_dither_type (ptr);
403 ExportFormatManager::select_compatibility (WeakCompatPtr const & compat)
405 /* Calculate compatibility intersection for the selection */
407 FormatBasePtr compat_intersect = get_compatibility_intersection ();
409 /* Unselect incompatible items */
411 boost::shared_ptr<ExportFormatBase> select_intersect;
413 select_intersect = compat_intersect->get_intersection (*current_selection);
414 if (select_intersect->qualities_empty()) {
415 select_quality (QualityPtr());
418 select_intersect = compat_intersect->get_intersection (*current_selection);
419 if (select_intersect->formats_empty()) {
420 select_format (FormatPtr());
423 select_intersect = compat_intersect->get_intersection (*current_selection);
424 if (select_intersect->sample_rates_empty()) {
425 select_sample_rate (SampleRatePtr());
428 select_intersect = compat_intersect->get_intersection (*current_selection);
429 if (select_intersect->sample_formats_empty()) {
430 select_sample_format (SampleFormatPtr());
435 ExportFormatManager::select_quality (QualityPtr const & quality)
437 bool do_selection_changed = !pending_selection_change;
438 if (!pending_selection_change) {
439 pending_selection_change = true;
443 current_selection->set_quality (quality->quality);
445 /* Deselect format if it is incompatible */
447 FormatPtr format = get_selected_format();
448 if (format && !format->has_quality (quality->quality)) {
449 format->set_selected (false);
453 current_selection->set_quality (ExportFormatBase::Q_None);
455 QualityPtr current_quality = get_selected_quality();
456 if (current_quality) {
457 current_quality->set_selected (false);
461 * A quality is never explicitly deselected without also deselecting the format
462 * so we don't need to deselect the format here.
463 * doing so causes extra complications
467 if (do_selection_changed) {
468 selection_changed ();
473 ExportFormatManager::select_format (FormatPtr const & format)
475 bool do_selection_changed = !pending_selection_change;
476 if (!pending_selection_change) {
477 pending_selection_change = true;
480 current_selection->set_format (format);
484 /* Slect right quality for format */
486 ExportFormatBase::Quality quality = format->get_quality();
487 for (QualityList::iterator it = qualities.begin (); it != qualities.end (); ++it) {
488 if ((*it)->quality == quality) {
489 (*it)->set_selected (true);
491 (*it)->set_selected (false);
495 /* Handle sample formats */
497 ExportFormatBase::SampleFormat format_to_select;
498 if (format->sample_format_is_compatible (current_selection->sample_format())) {
499 format_to_select = current_selection->sample_format();
501 format_to_select = format->default_sample_format();
504 boost::shared_ptr<HasSampleFormat> hsf;
505 if ((hsf = boost::dynamic_pointer_cast<HasSampleFormat> (format))) {
506 SampleFormatList sample_formats = hsf->get_sample_formats();
507 for (SampleFormatList::iterator it = sample_formats.begin (); it != sample_formats.end (); ++it) {
508 if ((*it)->format == format_to_select) {
509 (*it)->set_selected (true);
511 (*it)->set_selected (false);
516 current_selection->set_sample_format (format_to_select);
519 FormatPtr current_format = get_selected_format ();
520 if (current_format) {
521 current_format->set_selected (false);
525 if (do_selection_changed) {
526 selection_changed ();
531 ExportFormatManager::select_sample_rate (SampleRatePtr const & rate)
534 bool do_selection_changed = !pending_selection_change;
535 if (!pending_selection_change) {
536 pending_selection_change = true;
540 current_selection->set_sample_rate (rate->rate);
542 current_selection->set_sample_rate (ExportFormatBase::SR_None);
544 SampleRatePtr current_rate = get_selected_sample_rate();
546 current_rate->set_selected (false);
550 if (do_selection_changed) {
551 selection_changed ();
556 ExportFormatManager::select_sample_format (SampleFormatPtr const & format)
559 bool do_selection_changed = !pending_selection_change;
560 if (!pending_selection_change) {
561 pending_selection_change = true;
565 current_selection->set_sample_format (format->format);
567 current_selection->set_sample_format (ExportFormatBase::SF_None);
569 SampleFormatPtr current_format = get_selected_sample_format();
570 if (current_format) {
571 current_format->set_selected (false);
575 if (do_selection_changed) {
576 selection_changed ();
581 ExportFormatManager::select_dither_type (DitherTypePtr const & type)
584 bool do_selection_changed = !pending_selection_change;
585 if (!pending_selection_change) {
586 pending_selection_change = true;
590 current_selection->set_dither_type (type->type);
592 current_selection->set_dither_type (ExportFormatBase::D_None);
595 if (do_selection_changed) {
596 selection_changed ();
601 ExportFormatManager::selection_changed ()
603 /* Get a list of incompatible compatibility selections */
605 CompatList incompatibles;
606 for (CompatList::iterator it = compatibilities.begin(); it != compatibilities.end(); ++it) {
607 if (!current_selection->is_compatible_with (**it)) {
608 incompatibles.push_back (*it);
614 for (CompatList::iterator it = incompatibles.begin(); it != incompatibles.end(); ++it) {
615 (*it)->set_selected (false);
618 /* Mark compatibility for everything necessary */
620 std::set<ExportFormatBase::Quality> compatible_qualities;
621 FormatBasePtr compat_intersect = get_compatibility_intersection ();
622 ExportFormatCompatibility global_compat (*compat_intersect);
624 for (FormatList::iterator it = formats.begin(); it != formats.end(); ++it) {
625 if ((*it)->set_compatibility_state (global_compat)) {
626 compatible_qualities.insert ((*it)->get_quality());
630 bool any_quality_compatible = true;
631 for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
632 if (compatible_qualities.find((*it)->quality) != compatible_qualities.end()) {
633 (*it)->set_compatible (true);
636 (*it)->set_compatible (false);
638 if ((*it)->quality != ExportFormatBase::Q_Any) {
639 any_quality_compatible = false;
644 if (any_quality_compatible) {
645 for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
646 if ((*it)->quality == ExportFormatBase::Q_Any) {
647 (*it)->set_compatible (true);
653 for (SampleRateList::iterator it = sample_rates.begin(); it != sample_rates.end(); ++it) {
654 if (compat_intersect->has_sample_rate ((*it)->rate)) {
655 (*it)->set_compatible (true);
657 (*it)->set_compatible (false);
661 boost::shared_ptr<HasSampleFormat> hsf;
662 if (hsf = boost::dynamic_pointer_cast<HasSampleFormat> (get_selected_format())) {
664 SampleFormatList sf_list = hsf->get_sample_formats();
665 for (SampleFormatList::iterator it = sf_list.begin(); it != sf_list.end(); ++it) {
666 if (compat_intersect->has_sample_format ((*it)->format)) {
667 (*it)->set_compatible (true);
669 (*it)->set_compatible (false);
675 /* Signal completeness */
677 CompleteChanged (current_selection->is_complete());
679 /* Reset pending state */
681 pending_selection_change = false;
684 ExportFormatManager::QualityPtr
685 ExportFormatManager::get_selected_quality ()
687 for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
688 if ((*it)->selected()) {
696 ExportFormatManager::FormatPtr
697 ExportFormatManager::get_selected_format ()
701 for (FormatList::iterator it = formats.begin(); it != formats.end(); ++it) {
702 if ((*it)->selected()) {
710 ExportFormatManager::SampleRatePtr
711 ExportFormatManager::get_selected_sample_rate ()
713 for (SampleRateList::iterator it = sample_rates.begin(); it != sample_rates.end(); ++it) {
714 if ((*it)->selected()) {
719 return SampleRatePtr();
722 ExportFormatManager::SampleFormatPtr
723 ExportFormatManager::get_selected_sample_format ()
725 boost::shared_ptr<HasSampleFormat> hsf;
727 if (hsf = boost::dynamic_pointer_cast<HasSampleFormat> (get_selected_format())) {
728 return hsf->get_selected_sample_format ();
730 return SampleFormatPtr ();
735 ExportFormatManager::FormatBasePtr
736 ExportFormatManager::get_compatibility_intersection ()
738 FormatBasePtr compat_intersect = universal_set;
740 for (CompatList::iterator it = compatibilities.begin(); it != compatibilities.end(); ++it) {
741 if ((*it)->selected ()) {
742 compat_intersect = compat_intersect->get_intersection (**it);
746 return compat_intersect;
749 }; // namespace ARDOUR