Merge branch 'selection_fixes' of https://github.com/nmains/ardour into cairocanvas
[ardour.git] / libs / ardour / export_format_manager.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "ardour/export_format_manager.h"
22
23 #include "ardour/export_format_specification.h"
24 #include "ardour/export_format_compatibility.h"
25
26 #include "i18n.h"
27
28 using std::string;
29
30 namespace ARDOUR
31 {
32
33 ExportFormatManager::ExportFormatManager (ExportFormatSpecPtr specification) :
34   pending_selection_change (false),
35   universal_set (new ExportFormatBase ())
36 {
37         current_selection = specification;
38
39         init_compatibilities ();
40         init_qualities ();
41         init_formats ();
42         init_sample_rates ();
43
44         prev_description = current_selection->description();
45 }
46
47 ExportFormatManager::~ExportFormatManager ()
48 {
49
50 }
51
52 void
53 ExportFormatManager::init_compatibilities ()
54 {
55         ExportFormatCompatibilityPtr c_ptr;
56
57         c_ptr.reset (new ExportFormatCompatibility (_("CD")));
58         c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
59         c_ptr->add_format_id (ExportFormatBase::F_WAV);
60         c_ptr->add_format_id (ExportFormatBase::F_AIFF);
61         c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
62         c_ptr->add_sample_format (ExportFormatBase::SF_16);
63         c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
64         add_compatibility (c_ptr);
65
66         c_ptr.reset (new ExportFormatCompatibility (_("DVD-A")));
67         c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
68         c_ptr->add_sample_rate (ExportFormatBase::SR_48);
69         c_ptr->add_sample_rate (ExportFormatBase::SR_88_2);
70         c_ptr->add_sample_rate (ExportFormatBase::SR_96);
71         c_ptr->add_sample_rate (ExportFormatBase::SR_192);
72         c_ptr->add_format_id (ExportFormatBase::F_WAV);
73         c_ptr->add_format_id (ExportFormatBase::F_AIFF);
74         c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
75         c_ptr->add_sample_format (ExportFormatBase::SF_16);
76         c_ptr->add_sample_format (ExportFormatBase::SF_24);
77         c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
78         add_compatibility (c_ptr);
79
80         c_ptr.reset (new ExportFormatCompatibility (_("iPod")));
81         c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
82         c_ptr->add_sample_rate (ExportFormatBase::SR_48);
83         c_ptr->add_format_id (ExportFormatBase::F_WAV);
84         c_ptr->add_format_id (ExportFormatBase::F_AIFF);
85         c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
86         c_ptr->add_sample_format (ExportFormatBase::SF_16);
87         c_ptr->add_sample_format (ExportFormatBase::SF_24);
88         c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
89         add_compatibility (c_ptr);
90
91         c_ptr.reset (new ExportFormatCompatibility (_("Something else")));
92         c_ptr->add_sample_rate (ExportFormatBase::SR_44_1);
93         c_ptr->add_sample_rate (ExportFormatBase::SR_48);
94         c_ptr->add_format_id (ExportFormatBase::F_WAV);
95         c_ptr->add_format_id (ExportFormatBase::F_AIFF);
96         c_ptr->add_format_id (ExportFormatBase::F_AU);
97         c_ptr->add_format_id (ExportFormatBase::F_FLAC);
98         c_ptr->add_quality (ExportFormatBase::Q_LosslessLinear);
99         c_ptr->add_quality (ExportFormatBase::Q_LosslessCompression);
100         c_ptr->add_sample_format (ExportFormatBase::SF_16);
101         c_ptr->add_sample_format (ExportFormatBase::SF_24);
102         c_ptr->add_sample_format (ExportFormatBase::SF_32);
103         c_ptr->add_endianness (ExportFormatBase::E_FileDefault);
104         add_compatibility (c_ptr);
105 }
106
107 void
108 ExportFormatManager::init_qualities ()
109 {
110         add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_Any, _("Any"))));
111         add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_LosslessLinear, _("Lossless (linear PCM)"))));
112         add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_LossyCompression, _("Lossy compression"))));
113         add_quality (QualityPtr (new QualityState (ExportFormatBase::Q_LosslessCompression, _("Lossless compression"))));
114 }
115
116 void
117 ExportFormatManager::init_formats ()
118 {
119         ExportFormatPtr f_ptr;
120         ExportFormatLinear * fl_ptr;
121
122         f_ptr.reset (fl_ptr = new ExportFormatLinear ("AIFF", ExportFormatBase::F_AIFF));
123         fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
124         fl_ptr->add_sample_format (ExportFormatBase::SF_8);
125         fl_ptr->add_sample_format (ExportFormatBase::SF_16);
126         fl_ptr->add_sample_format (ExportFormatBase::SF_24);
127         fl_ptr->add_sample_format (ExportFormatBase::SF_32);
128         fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
129         fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
130         fl_ptr->add_endianness (ExportFormatBase::E_Big);
131         fl_ptr->set_default_sample_format (ExportFormatBase::SF_16);
132         fl_ptr->set_extension ("aiff");
133         add_format (f_ptr);
134
135         f_ptr.reset (fl_ptr = new ExportFormatLinear ("AU", ExportFormatBase::F_AU));
136         fl_ptr->add_sample_format (ExportFormatBase::SF_8);
137         fl_ptr->add_sample_format (ExportFormatBase::SF_16);
138         fl_ptr->add_sample_format (ExportFormatBase::SF_24);
139         fl_ptr->add_sample_format (ExportFormatBase::SF_32);
140         fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
141         fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
142         fl_ptr->set_default_sample_format (ExportFormatBase::SF_16);
143         fl_ptr->set_extension ("au");
144         add_format (f_ptr);
145
146         f_ptr.reset (new ExportFormatBWF ());
147         add_format (f_ptr);
148
149         f_ptr.reset (fl_ptr = new ExportFormatLinear ("IRCAM", ExportFormatBase::F_IRCAM));
150         fl_ptr->add_sample_format (ExportFormatBase::SF_16);
151         fl_ptr->add_sample_format (ExportFormatBase::SF_24);
152         fl_ptr->add_sample_format (ExportFormatBase::SF_32);
153         fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
154         fl_ptr->set_default_sample_format (ExportFormatBase::SF_24);
155         fl_ptr->set_extension ("sf");
156         add_format (f_ptr);
157
158         f_ptr.reset (fl_ptr = new ExportFormatLinear ("WAV", ExportFormatBase::F_WAV));
159         fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
160         fl_ptr->add_sample_format (ExportFormatBase::SF_16);
161         fl_ptr->add_sample_format (ExportFormatBase::SF_24);
162         fl_ptr->add_sample_format (ExportFormatBase::SF_32);
163         fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
164         fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
165         fl_ptr->add_endianness (ExportFormatBase::E_Little);
166         fl_ptr->set_default_sample_format (ExportFormatBase::SF_16);
167         fl_ptr->set_extension ("wav");
168         add_format (f_ptr);
169
170         f_ptr.reset (fl_ptr = new ExportFormatLinear ("W64", ExportFormatBase::F_W64));
171         fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
172         fl_ptr->add_sample_format (ExportFormatBase::SF_16);
173         fl_ptr->add_sample_format (ExportFormatBase::SF_24);
174         fl_ptr->add_sample_format (ExportFormatBase::SF_32);
175         fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
176         fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
177         fl_ptr->set_default_sample_format (ExportFormatBase::SF_Double);
178         fl_ptr->set_extension ("w64");
179         add_format (f_ptr);
180
181         f_ptr.reset (fl_ptr = new ExportFormatLinear ("CAF", ExportFormatBase::F_CAF));
182         fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
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 ("caf");
190         add_format (f_ptr);
191
192         f_ptr.reset (fl_ptr = new ExportFormatLinear ("RAW", ExportFormatBase::F_RAW));
193         fl_ptr->add_sample_format (ExportFormatBase::SF_U8);
194         fl_ptr->add_sample_format (ExportFormatBase::SF_8);
195         fl_ptr->add_sample_format (ExportFormatBase::SF_16);
196         fl_ptr->add_sample_format (ExportFormatBase::SF_24);
197         fl_ptr->add_sample_format (ExportFormatBase::SF_32);
198         fl_ptr->add_sample_format (ExportFormatBase::SF_Float);
199         fl_ptr->add_sample_format (ExportFormatBase::SF_Double);
200         fl_ptr->set_default_sample_format (ExportFormatBase::SF_Float);
201         fl_ptr->set_extension ("raw");
202         add_format (f_ptr);
203
204         try {
205                 f_ptr.reset (new ExportFormatOggVorbis ());
206                 add_format (f_ptr);
207         } catch (ExportFormatIncompatible & e) {}
208
209         try {
210                 f_ptr.reset (new ExportFormatFLAC ());
211                 add_format (f_ptr);
212         } catch (ExportFormatIncompatible & e) {}
213 }
214
215 void
216 ExportFormatManager::init_sample_rates ()
217 {
218         add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_Session, _("Session rate"))));
219         add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_8, "8 kHz")));
220         add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_22_05, "22,05 kHz")));
221         add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_44_1, "44,1 kHz")));
222         add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_48, "48 kHz")));
223         add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_88_2, "88,2 kHz")));
224         add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_96, "96 kHz")));
225         add_sample_rate (SampleRatePtr (new SampleRateState (ExportFormatBase::SR_192, "192 kHz")));
226 }
227
228 void
229 ExportFormatManager::add_compatibility (ExportFormatCompatibilityPtr ptr)
230 {
231         compatibilities.push_back (ptr);
232         ptr->SelectChanged.connect_same_thread (*this,
233                                                 boost::bind (&ExportFormatManager::change_compatibility_selection,
234                                                              this, _1, WeakExportFormatCompatibilityPtr (ptr)));
235 }
236
237 void
238 ExportFormatManager::add_quality (QualityPtr ptr)
239 {
240         ptr->SelectChanged.connect_same_thread (*this, boost::bind (&ExportFormatManager::change_quality_selection, this, _1, WeakQualityPtr (ptr)));
241         qualities.push_back (ptr);
242 }
243
244 void
245 ExportFormatManager::add_format (ExportFormatPtr ptr)
246 {
247         formats.push_back (ptr);
248         ptr->SelectChanged.connect_same_thread (*this, boost::bind (&ExportFormatManager::change_format_selection, this, _1, WeakExportFormatPtr (ptr)));
249         universal_set = universal_set->get_union (*ptr);
250
251         /* Encoding options */
252
253         boost::shared_ptr<HasSampleFormat> hsf;
254
255         if ((hsf = boost::dynamic_pointer_cast<HasSampleFormat> (ptr))) {
256                 hsf->SampleFormatSelectChanged.connect_same_thread (*this, boost::bind (&ExportFormatManager::change_sample_format_selection, this, _1, _2));
257                 hsf->DitherTypeSelectChanged.connect_same_thread (*this, boost::bind (&ExportFormatManager::change_dither_type_selection, this, _1, _2));
258         }
259 }
260
261 void
262 ExportFormatManager::add_sample_rate (SampleRatePtr ptr)
263 {
264         ptr->SelectChanged.connect_same_thread (*this, boost::bind (&ExportFormatManager::change_sample_rate_selection, this, _1, WeakSampleRatePtr (ptr)));
265         sample_rates.push_back (ptr);
266 }
267
268 void
269 ExportFormatManager::set_name (string name)
270 {
271         current_selection->set_name (name);
272         check_for_description_change ();
273 }
274
275 void
276 ExportFormatManager::select_src_quality (ExportFormatBase::SRCQuality value)
277 {
278         current_selection->set_src_quality (value);
279         check_for_description_change ();
280 }
281
282 void
283 ExportFormatManager::select_with_cue (bool value)
284 {
285         current_selection->set_with_cue (value);
286         check_for_description_change ();
287 }
288
289 void
290 ExportFormatManager::select_with_toc (bool value)
291 {
292         current_selection->set_with_toc (value);
293         check_for_description_change ();
294 }
295
296
297 void
298 ExportFormatManager::set_command (std::string command)
299 {
300         current_selection->set_command (command);
301         check_for_description_change ();
302 }
303
304 void
305 ExportFormatManager::select_trim_beginning (bool value)
306 {
307         current_selection->set_trim_beginning (value);
308         check_for_description_change ();
309 }
310
311 void
312 ExportFormatManager::select_silence_beginning (AnyTime const & time)
313 {
314         current_selection->set_silence_beginning (time);
315         check_for_description_change ();
316 }
317
318 void
319 ExportFormatManager::select_trim_end (bool value)
320 {
321         current_selection->set_trim_end (value);
322         check_for_description_change ();
323 }
324
325 void
326 ExportFormatManager::select_silence_end (AnyTime const & time)
327 {
328         current_selection->set_silence_end (time);
329         check_for_description_change ();
330 }
331
332 void
333 ExportFormatManager::select_normalize (bool value)
334 {
335         current_selection->set_normalize (value);
336         check_for_description_change ();
337 }
338
339 void
340 ExportFormatManager::select_normalize_target (float value)
341 {
342         current_selection->set_normalize_target (value);
343         check_for_description_change ();
344 }
345
346 void
347 ExportFormatManager::select_tagging (bool tag)
348 {
349         current_selection->set_tag (tag);
350         check_for_description_change ();
351 }
352
353 void
354 ExportFormatManager::change_compatibility_selection (bool select, WeakExportFormatCompatibilityPtr const & compat)
355 {
356         bool do_selection_changed = !pending_selection_change;
357         if (!pending_selection_change) {
358                 pending_selection_change = true;
359         }
360
361         ExportFormatCompatibilityPtr ptr = compat.lock();
362
363         if (ptr && select) {
364                 select_compatibility (ptr);
365         }
366
367         if (do_selection_changed) {
368                 selection_changed ();
369         }
370 }
371
372 void
373 ExportFormatManager::change_quality_selection (bool select, WeakQualityPtr const & quality)
374 {
375         QualityPtr ptr = quality.lock ();
376
377         if (!ptr) {
378                 return;
379         }
380
381         if (select) {
382                 select_quality (ptr);
383         } else if (ptr->quality == current_selection->quality()) {
384                 ptr.reset();
385                 select_quality (ptr);
386         }
387 }
388
389 void
390 ExportFormatManager::change_format_selection (bool select, WeakExportFormatPtr const & format)
391 {
392         ExportFormatPtr ptr = format.lock();
393
394         if (!ptr) {
395                 return;
396         }
397
398         if (select) {
399                 select_format (ptr);
400         } else if (ptr->get_format_id() == current_selection->format_id()) {
401                 ptr.reset();
402                 select_format (ptr);
403         }
404 }
405
406 void
407 ExportFormatManager::change_sample_rate_selection (bool select, WeakSampleRatePtr const & rate)
408 {
409         SampleRatePtr ptr = rate.lock();
410
411         if (!ptr) {
412                 return;
413         }
414
415         if (select) {
416                 select_sample_rate (ptr);
417         } else if (ptr->rate == current_selection->sample_rate()) {
418                 ptr.reset();
419                 select_sample_rate (ptr);
420         }
421 }
422
423 void
424 ExportFormatManager::change_sample_format_selection (bool select, WeakSampleFormatPtr const & format)
425 {
426         SampleFormatPtr ptr = format.lock();
427
428         if (!ptr) {
429                 return;
430         }
431
432         if (select) {
433                 select_sample_format (ptr);
434         } else if (ptr->format == current_selection->sample_format()) {
435                 ptr.reset();
436                 select_sample_format (ptr);
437         }
438 }
439
440 void
441 ExportFormatManager::change_dither_type_selection (bool select, WeakDitherTypePtr const & type)
442 {
443         DitherTypePtr ptr = type.lock();
444
445         if (!ptr) {
446                 return;
447         }
448
449         if (select) {
450                 select_dither_type (ptr);
451         } else if (ptr->type == current_selection->dither_type()) {
452                 ptr.reset();
453                 select_dither_type (ptr);
454         }
455 }
456
457 void
458 ExportFormatManager::select_compatibility (WeakExportFormatCompatibilityPtr const & /*compat*/)
459 {
460         /* Calculate compatibility intersection for the selection */
461
462         ExportFormatBasePtr compat_intersect = get_compatibility_intersection ();
463
464         /* Unselect incompatible items */
465
466         boost::shared_ptr<ExportFormatBase> select_intersect;
467
468         select_intersect = compat_intersect->get_intersection (*current_selection);
469         if (select_intersect->qualities_empty()) {
470                 select_quality (QualityPtr());
471         }
472
473         select_intersect = compat_intersect->get_intersection (*current_selection);
474         if (select_intersect->formats_empty()) {
475                 select_format (ExportFormatPtr());
476         }
477
478         select_intersect = compat_intersect->get_intersection (*current_selection);
479         if (select_intersect->sample_rates_empty()) {
480                 select_sample_rate (SampleRatePtr());
481         }
482
483         select_intersect = compat_intersect->get_intersection (*current_selection);
484         if (select_intersect->sample_formats_empty()) {
485                 select_sample_format (SampleFormatPtr());
486         }
487 }
488
489 void
490 ExportFormatManager::select_quality (QualityPtr const & quality)
491 {
492         bool do_selection_changed = !pending_selection_change;
493         if (!pending_selection_change) {
494                 pending_selection_change = true;
495         }
496
497         if (quality) {
498                 current_selection->set_quality (quality->quality);
499
500                 /* Deselect format if it is incompatible */
501
502                 ExportFormatPtr format = get_selected_format();
503                 if (format && !format->has_quality (quality->quality)) {
504                         format->set_selected (false);
505                 }
506
507         } else {
508                 current_selection->set_quality (ExportFormatBase::Q_None);
509
510                 QualityPtr current_quality = get_selected_quality();
511                 if (current_quality) {
512                         current_quality->set_selected (false);
513                 }
514
515                 /* Note:
516                  * A quality is never explicitly deselected without also deselecting the format
517                  * so we don't need to deselect the format here.
518                  * doing so causes extra complications
519                  */
520         }
521
522         if (do_selection_changed) {
523                 selection_changed ();
524         }
525 }
526
527 void
528 ExportFormatManager::select_format (ExportFormatPtr const & format)
529 {
530         bool do_selection_changed = !pending_selection_change;
531         if (!pending_selection_change) {
532                 pending_selection_change = true;
533         }
534
535         current_selection->set_format (format);
536
537         if (format) {
538
539                 /* Slect right quality for format */
540
541                 ExportFormatBase::Quality quality = format->get_quality();
542                 for (QualityList::iterator it = qualities.begin (); it != qualities.end (); ++it) {
543                         if ((*it)->quality == quality) {
544                                 (*it)->set_selected (true);
545                         } else {
546                                 (*it)->set_selected (false);
547                         }
548                 }
549
550                 /* Handle sample formats */
551
552                 ExportFormatBase::SampleFormat format_to_select;
553                 if (format->sample_format_is_compatible (current_selection->sample_format())) {
554                         format_to_select = current_selection->sample_format();
555                 } else {
556                         format_to_select = format->default_sample_format();
557                 }
558
559                 boost::shared_ptr<HasSampleFormat> hsf;
560                 if ((hsf = boost::dynamic_pointer_cast<HasSampleFormat> (format))) {
561                         SampleFormatList sample_formats = hsf->get_sample_formats();
562                         for (SampleFormatList::iterator it = sample_formats.begin (); it != sample_formats.end (); ++it) {
563                                 if ((*it)->format == format_to_select) {
564                                         (*it)->set_selected (true);
565                                 } else {
566                                         (*it)->set_selected (false);
567                                 }
568                         }
569                 }
570
571                 current_selection->set_sample_format (format_to_select);
572
573         } else {
574                 ExportFormatPtr current_format = get_selected_format ();
575                 if (current_format) {
576                         current_format->set_selected (false);
577                 }
578         }
579
580         if (do_selection_changed) {
581                 selection_changed ();
582         }
583 }
584
585 void
586 ExportFormatManager::select_sample_rate (SampleRatePtr const & rate)
587 {
588
589         bool do_selection_changed = !pending_selection_change;
590         if (!pending_selection_change) {
591                 pending_selection_change = true;
592         }
593
594         if (rate) {
595                 current_selection->set_sample_rate (rate->rate);
596         } else {
597                 current_selection->set_sample_rate (ExportFormatBase::SR_None);
598
599                 SampleRatePtr current_rate = get_selected_sample_rate();
600                 if (current_rate) {
601                         current_rate->set_selected (false);
602                 }
603         }
604
605         if (do_selection_changed) {
606                 selection_changed ();
607         }
608 }
609
610 void
611 ExportFormatManager::select_sample_format (SampleFormatPtr const & format)
612 {
613
614         bool do_selection_changed = !pending_selection_change;
615         if (!pending_selection_change) {
616                 pending_selection_change = true;
617         }
618
619         if (format) {
620                 current_selection->set_sample_format (format->format);
621         } else {
622                 current_selection->set_sample_format (ExportFormatBase::SF_None);
623
624                 SampleFormatPtr current_format = get_selected_sample_format();
625                 if (current_format) {
626                         current_format->set_selected (false);
627                 }
628         }
629
630         if (do_selection_changed) {
631                 selection_changed ();
632         }
633 }
634
635 void
636 ExportFormatManager::select_dither_type (DitherTypePtr const & type)
637 {
638
639         bool do_selection_changed = !pending_selection_change;
640         if (!pending_selection_change) {
641                 pending_selection_change = true;
642         }
643
644         if (type) {
645                 current_selection->set_dither_type (type->type);
646         } else {
647                 current_selection->set_dither_type (ExportFormatBase::D_None);
648         }
649
650         if (do_selection_changed) {
651                 selection_changed ();
652         }
653 }
654
655 void
656 ExportFormatManager::selection_changed ()
657 {
658         /* Get a list of incompatible compatibility selections */
659
660         CompatList incompatibles;
661         for (CompatList::iterator it = compatibilities.begin(); it != compatibilities.end(); ++it) {
662                 if (!current_selection->is_compatible_with (**it)) {
663                         incompatibles.push_back (*it);
664                 }
665         }
666
667         /* Deselect them */
668
669         for (CompatList::iterator it = incompatibles.begin(); it != incompatibles.end(); ++it) {
670                 (*it)->set_selected (false);
671         }
672
673         /* Mark compatibility for everything necessary */
674
675         std::set<ExportFormatBase::Quality> compatible_qualities;
676         ExportFormatBasePtr compat_intersect = get_compatibility_intersection ();
677         ExportFormatCompatibility global_compat (*compat_intersect);
678
679         for (FormatList::iterator it = formats.begin(); it != formats.end(); ++it) {
680                 if ((*it)->set_compatibility_state (global_compat)) {
681                         compatible_qualities.insert ((*it)->get_quality());
682                 }
683         }
684
685         bool any_quality_compatible = true;
686         for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
687                 if (compatible_qualities.find((*it)->quality) != compatible_qualities.end()) {
688                         (*it)->set_compatible (true);
689
690                 } else {
691                         (*it)->set_compatible (false);
692
693                         if ((*it)->quality != ExportFormatBase::Q_Any) {
694                                 any_quality_compatible = false;
695                         }
696                 }
697         }
698
699         if (any_quality_compatible) {
700                 for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
701                         if ((*it)->quality == ExportFormatBase::Q_Any) {
702                                 (*it)->set_compatible (true);
703                                 break;
704                         }
705                 }
706         }
707
708         for (SampleRateList::iterator it = sample_rates.begin(); it != sample_rates.end(); ++it) {
709                 if (compat_intersect->has_sample_rate ((*it)->rate)) {
710                         (*it)->set_compatible (true);
711                 } else {
712                         (*it)->set_compatible (false);
713                 }
714         }
715
716         boost::shared_ptr<HasSampleFormat> hsf;
717         if ((hsf = boost::dynamic_pointer_cast<HasSampleFormat> (get_selected_format()))) {
718
719                 SampleFormatList sf_list = hsf->get_sample_formats();
720                 for (SampleFormatList::iterator it = sf_list.begin(); it != sf_list.end(); ++it) {
721                         if (compat_intersect->has_sample_format ((*it)->format)) {
722                                 (*it)->set_compatible (true);
723                         } else {
724                                 (*it)->set_compatible (false);
725                         }
726                 }
727
728         }
729
730         /* Signal completeness and possible description change */
731
732         CompleteChanged (current_selection->is_complete());
733         check_for_description_change ();
734
735         /* Reset pending state */
736
737         pending_selection_change = false;
738 }
739
740 void
741 ExportFormatManager::check_for_description_change ()
742 {
743         std::string new_description = current_selection->description();
744         if (new_description == prev_description) { return; }
745
746         prev_description = new_description;
747         DescriptionChanged();
748 }
749
750 ExportFormatManager::QualityPtr
751 ExportFormatManager::get_selected_quality ()
752 {
753         for (QualityList::iterator it = qualities.begin(); it != qualities.end(); ++it) {
754                 if ((*it)->selected()) {
755                         return *it;
756                 }
757         }
758
759         return QualityPtr();
760 }
761
762 ExportFormatPtr
763 ExportFormatManager::get_selected_format ()
764 {
765         ExportFormatPtr format;
766
767         for (FormatList::iterator it = formats.begin(); it != formats.end(); ++it) {
768                 if ((*it)->selected()) {
769                         return *it;
770                 }
771         }
772
773         return format;
774 }
775
776 ExportFormatManager::SampleRatePtr
777 ExportFormatManager::get_selected_sample_rate ()
778 {
779         for (SampleRateList::iterator it = sample_rates.begin(); it != sample_rates.end(); ++it) {
780                 if ((*it)->selected()) {
781                         return *it;
782                 }
783         }
784
785         return SampleRatePtr();
786 }
787
788 ExportFormatManager::SampleFormatPtr
789 ExportFormatManager::get_selected_sample_format ()
790 {
791         boost::shared_ptr<HasSampleFormat> hsf;
792
793         if ((hsf = boost::dynamic_pointer_cast<HasSampleFormat> (get_selected_format()))) {
794                 return hsf->get_selected_sample_format ();
795         } else {
796                 return SampleFormatPtr ();
797         }
798 }
799
800
801 ExportFormatBasePtr
802 ExportFormatManager::get_compatibility_intersection ()
803 {
804         ExportFormatBasePtr compat_intersect = universal_set;
805
806         for (CompatList::iterator it = compatibilities.begin(); it != compatibilities.end(); ++it) {
807                 if ((*it)->selected ()) {
808                         compat_intersect = compat_intersect->get_intersection (**it);
809                 }
810         }
811
812         return compat_intersect;
813 }
814
815 }; // namespace ARDOUR