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");
189 f_ptr.reset (new ExportFormatOggVorbis ());
191 } catch (ExportFormatIncompatible & e) {}
194 f_ptr.reset (new ExportFormatFLAC ());
196 } catch (ExportFormatIncompatible & e) {}
200 ExportFormatManager::init_sample_rates ()
202 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_22_05, "22,05 kHz")));
203 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_44_1, "44,1 kHz")));
204 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_48, "48 kHz")));
205 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_88_2, "88,2 kHz")));
206 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_96, "96 kHz")));
207 add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_192, "192 kHz")));
211 ExportFormatManager::add_compatibility (CompatPtr ptr)
213 compatibilities.push_back (ptr);
214 ptr->SelectChanged.connect (sigc::bind (sigc::mem_fun (*this, &ExportFormatManager::change_compatibility_selection), WeakCompatPtr (ptr)));
218 ExportFormatManager::add_quality (QualityPtr ptr)
220 ptr->SelectChanged.connect (sigc::bind (sigc::mem_fun (*this, &ExportFormatManager::change_quality_selection), WeakQualityPtr (ptr)));
221 qualities.push_back (ptr);
225 ExportFormatManager::add_format (FormatPtr ptr)
227 formats.push_back (ptr);
228 ptr->SelectChanged.connect (sigc::bind (sigc::mem_fun (*this, &ExportFormatManager::change_format_selection), WeakFormatPtr (ptr)));
229 universal_set = universal_set->get_union (*ptr);
231 /* Encoding options */
233 boost::shared_ptr<HasSampleFormat> hsf;
235 if (hsf = boost::dynamic_pointer_cast<HasSampleFormat> (ptr)) {
236 hsf->SampleFormatSelectChanged.connect (sigc::mem_fun (*this, &ExportFormatManager::change_sample_format_selection));
237 hsf->DitherTypeSelectChanged.connect (sigc::mem_fun (*this, &ExportFormatManager::change_dither_type_selection));
242 ExportFormatManager::add_sample_rate (SampleRatePtr ptr)
244 ptr->SelectChanged.connect (sigc::bind (sigc::mem_fun (*this, &ExportFormatManager::change_sample_rate_selection), WeakSampleRatePtr (ptr)));
245 sample_rates.push_back (ptr);
249 ExportFormatManager::set_name (Glib::ustring name)
251 current_selection->set_name (name);
255 ExportFormatManager::select_src_quality (ExportFormatBase::SRCQuality value)
257 current_selection->set_src_quality (value);
261 ExportFormatManager::select_trim_beginning (bool value)
263 current_selection->set_trim_beginning (value);
267 ExportFormatManager::select_silence_beginning (AnyTime const & time)
269 current_selection->set_silence_beginning (time);
273 ExportFormatManager::select_trim_end (bool value)
275 current_selection->set_trim_end (value);
279 ExportFormatManager::select_silence_end (AnyTime const & time)
281 current_selection->set_silence_end (time);
285 ExportFormatManager::select_normalize (bool value)
287 current_selection->set_normalize (value);
291 ExportFormatManager::select_normalize_target (float value)
293 current_selection->set_normalize_target (value);
297 ExportFormatManager::select_tagging (bool tag)
299 current_selection->set_tag (tag);
303 ExportFormatManager::change_compatibility_selection (bool select, WeakCompatPtr const & compat)
305 bool do_selection_changed = !pending_selection_change;
306 if (!pending_selection_change) {
307 pending_selection_change = true;
310 CompatPtr ptr = compat.lock();
313 select_compatibility (ptr);
316 if (do_selection_changed) {
317 selection_changed ();
322 ExportFormatManager::change_quality_selection (bool select, WeakQualityPtr const & quality)
324 QualityPtr ptr = quality.lock ();
331 select_quality (ptr);
332 } else if (ptr->quality == current_selection->quality()) {
334 select_quality (ptr);
339 ExportFormatManager::change_format_selection (bool select, WeakFormatPtr const & format)
341 FormatPtr ptr = format.lock();
349 } else if (ptr->get_format_id() == current_selection->format_id()) {
356 ExportFormatManager::change_sample_rate_selection (bool select, WeakSampleRatePtr const & rate)
358 SampleRatePtr ptr = rate.lock();
365 select_sample_rate (ptr);
366 } else if (ptr->rate == current_selection->sample_rate()) {
368 select_sample_rate (ptr);
373 ExportFormatManager::change_sample_format_selection (bool select, WeakSampleFormatPtr const & format)
375 SampleFormatPtr ptr = format.lock();
382 select_sample_format (ptr);
383 } else if (ptr->format == current_selection->sample_format()) {
385 select_sample_format (ptr);
390 ExportFormatManager::change_dither_type_selection (bool select, WeakDitherTypePtr const & type)
392 DitherTypePtr ptr = type.lock();
399 select_dither_type (ptr);
400 } else if (ptr->type == current_selection->dither_type()) {
402 select_dither_type (ptr);
407 ExportFormatManager::select_compatibility (WeakCompatPtr const & compat)
409 /* Calculate compatibility intersection for the selection */
411 FormatBasePtr compat_intersect = get_compatibility_intersection ();
413 /* Unselect incompatible items */
415 boost::shared_ptr<ExportFormatBase> select_intersect;
417 select_intersect = compat_intersect->get_intersection (*current_selection);
418 if (select_intersect->qualities_empty()) {
419 select_quality (QualityPtr());
422 select_intersect = compat_intersect->get_intersection (*current_selection);
423 if (select_intersect->formats_empty()) {
424 select_format (FormatPtr());
427 select_intersect = compat_intersect->get_intersection (*current_selection);
428 if (select_intersect->sample_rates_empty()) {
429 select_sample_rate (SampleRatePtr());
432 select_intersect = compat_intersect->get_intersection (*current_selection);
433 if (select_intersect->sample_formats_empty()) {
434 select_sample_format (SampleFormatPtr());
439 ExportFormatManager::select_quality (QualityPtr const & quality)
441 bool do_selection_changed = !pending_selection_change;
442 if (!pending_selection_change) {
443 pending_selection_change = true;
447 current_selection->set_quality (quality->quality);
449 /* Deselect format if it is incompatible */
451 FormatPtr format = get_selected_format();
452 if (format && !format->has_quality (quality->quality)) {
453 format->set_selected (false);
457 current_selection->set_quality (ExportFormatBase::Q_None);
459 QualityPtr current_quality = get_selected_quality();
460 if (current_quality) {
461 current_quality->set_selected (false);
465 * A quality is never explicitly deselected without also deselecting the format
466 * so we don't need to deselect the format here.
467 * doing so causes extra complications
471 if (do_selection_changed) {
472 selection_changed ();
477 ExportFormatManager::select_format (FormatPtr const & format)
479 bool do_selection_changed = !pending_selection_change;
480 if (!pending_selection_change) {
481 pending_selection_change = true;
484 current_selection->set_format (format);
488 /* Slect right quality for format */
490 ExportFormatBase::Quality quality = format->get_quality();
491 for (QualityList::iterator it = qualities.begin (); it != qualities.end (); ++it) {
492 if ((*it)->quality == quality) {
493 (*it)->set_selected (true);
495 (*it)->set_selected (false);
499 /* Handle sample formats */
501 ExportFormatBase::SampleFormat format_to_select;
502 if (format->sample_format_is_compatible (current_selection->sample_format())) {
503 format_to_select = current_selection->sample_format();
505 format_to_select = format->default_sample_format();
508 boost::shared_ptr<HasSampleFormat> hsf;
509 if ((hsf = boost::dynamic_pointer_cast<HasSampleFormat> (format))) {
510 SampleFormatList sample_formats = hsf->get_sample_formats();
511 for (SampleFormatList::iterator it = sample_formats.begin (); it != sample_formats.end (); ++it) {
512 if ((*it)->format == format_to_select) {
513 (*it)->set_selected (true);
515 (*it)->set_selected (false);
520 current_selection->set_sample_format (format_to_select);
523 FormatPtr current_format = get_selected_format ();
524 if (current_format) {
525 current_format->set_selected (false);
529 if (do_selection_changed) {
530 selection_changed ();
535 ExportFormatManager::select_sample_rate (SampleRatePtr const & rate)
538 bool do_selection_changed = !pending_selection_change;
539 if (!pending_selection_change) {
540 pending_selection_change = true;
544 current_selection->set_sample_rate (rate->rate);
546 current_selection->set_sample_rate (ExportFormatBase::SR_None);
548 SampleRatePtr current_rate = get_selected_sample_rate();
550 current_rate->set_selected (false);
554 if (do_selection_changed) {
555 selection_changed ();
560 ExportFormatManager::select_sample_format (SampleFormatPtr const & format)
563 bool do_selection_changed = !pending_selection_change;
564 if (!pending_selection_change) {
565 pending_selection_change = true;
569 current_selection->set_sample_format (format->format);
571 current_selection->set_sample_format (ExportFormatBase::SF_None);
573 SampleFormatPtr current_format = get_selected_sample_format();
574 if (current_format) {
575 current_format->set_selected (false);
579 if (do_selection_changed) {
580 selection_changed ();
585 ExportFormatManager::select_dither_type (DitherTypePtr const & type)
588 bool do_selection_changed = !pending_selection_change;
589 if (!pending_selection_change) {
590 pending_selection_change = true;
594 current_selection->set_dither_type (type->type);
596 current_selection->set_dither_type (ExportFormatBase::D_None);
599 if (do_selection_changed) {
600 selection_changed ();
605 ExportFormatManager::selection_changed ()
607 /* Get a list of incompatible compatibility selections */
609 CompatList incompatibles;
610 for (CompatList::iterator it = compatibilities.begin(); it != compatibilities.end(); ++it) {
611 if (!current_selection->is_compatible_with (**it)) {
612 incompatibles.push_back (*it);
618 for (CompatList::iterator it = incompatibles.begin(); it != incompatibles.end(); ++it) {
619 (*it)->set_selected (false);
622 /* Mark compatibility for everything necessary */
624 std::set<ExportFormatBase::Quality> compatible_qualities;
625 FormatBasePtr compat_intersect = get_compatibility_intersection ();
626 ExportFormatCompatibility global_compat (*compat_intersect);
628 for (FormatList::iterator it = formats.begin(); it != formats.end(); ++it) {
629 if ((*it)->set_compatibility_state (global_compat)) {
630 compatible_qualities.insert ((*it)->get_quality());
634 bool any_quality_compatible = true;
635 for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
636 if (compatible_qualities.find((*it)->quality) != compatible_qualities.end()) {
637 (*it)->set_compatible (true);
640 (*it)->set_compatible (false);
642 if ((*it)->quality != ExportFormatBase::Q_Any) {
643 any_quality_compatible = false;
648 if (any_quality_compatible) {
649 for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
650 if ((*it)->quality == ExportFormatBase::Q_Any) {
651 (*it)->set_compatible (true);
657 for (SampleRateList::iterator it = sample_rates.begin(); it != sample_rates.end(); ++it) {
658 if (compat_intersect->has_sample_rate ((*it)->rate)) {
659 (*it)->set_compatible (true);
661 (*it)->set_compatible (false);
665 boost::shared_ptr<HasSampleFormat> hsf;
666 if (hsf = boost::dynamic_pointer_cast<HasSampleFormat> (get_selected_format())) {
668 SampleFormatList sf_list = hsf->get_sample_formats();
669 for (SampleFormatList::iterator it = sf_list.begin(); it != sf_list.end(); ++it) {
670 if (compat_intersect->has_sample_format ((*it)->format)) {
671 (*it)->set_compatible (true);
673 (*it)->set_compatible (false);
679 /* Signal completeness */
681 CompleteChanged (current_selection->is_complete());
683 /* Reset pending state */
685 pending_selection_change = false;
688 ExportFormatManager::QualityPtr
689 ExportFormatManager::get_selected_quality ()
691 for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
692 if ((*it)->selected()) {
700 ExportFormatManager::FormatPtr
701 ExportFormatManager::get_selected_format ()
705 for (FormatList::iterator it = formats.begin(); it != formats.end(); ++it) {
706 if ((*it)->selected()) {
714 ExportFormatManager::SampleRatePtr
715 ExportFormatManager::get_selected_sample_rate ()
717 for (SampleRateList::iterator it = sample_rates.begin(); it != sample_rates.end(); ++it) {
718 if ((*it)->selected()) {
723 return SampleRatePtr();
726 ExportFormatManager::SampleFormatPtr
727 ExportFormatManager::get_selected_sample_format ()
729 boost::shared_ptr<HasSampleFormat> hsf;
731 if (hsf = boost::dynamic_pointer_cast<HasSampleFormat> (get_selected_format())) {
732 return hsf->get_selected_sample_format ();
734 return SampleFormatPtr ();
739 ExportFormatManager::FormatBasePtr
740 ExportFormatManager::get_compatibility_intersection ()
742 FormatBasePtr compat_intersect = universal_set;
744 for (CompatList::iterator it = compatibilities.begin(); it != compatibilities.end(); ++it) {
745 if ((*it)->selected ()) {
746 compat_intersect = compat_intersect->get_intersection (**it);
750 return compat_intersect;
753 }; // namespace ARDOUR