fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / export_format_base.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_base.h"
22
23 namespace ARDOUR
24 {
25
26 void
27 ExportFormatBase::SelectableCompatible::set_selected (bool value)
28 {
29         if (_selected != value) {
30                 _selected = value;
31                 SelectChanged (value);
32         }
33 }
34
35 void
36 ExportFormatBase::SelectableCompatible::set_compatible (bool value)
37 {
38         if (_compatible != value) {
39                 _compatible = value;
40                 CompatibleChanged (value);
41         }
42         if (!value) {
43                 set_selected (false);
44         }
45 }
46
47 ExportFormatBase::ExportFormatBase ()
48 {
49
50 }
51
52 ExportFormatBase::ExportFormatBase (ExportFormatBase const & other) :
53   sample_formats (other.sample_formats),
54   endiannesses (other.endiannesses),
55   sample_rates (other.sample_rates),
56   format_ids (other.format_ids),
57   qualities (other.qualities)
58 {
59
60 }
61
62 ExportFormatBase::~ExportFormatBase ()
63 {
64
65 }
66
67 boost::shared_ptr<ExportFormatBase>
68 ExportFormatBase::get_intersection (ExportFormatBase const & other) const
69 {
70         return do_set_operation (other, SetIntersection);
71 }
72
73 boost::shared_ptr<ExportFormatBase>
74 ExportFormatBase::get_union (ExportFormatBase const & other) const
75 {
76         return do_set_operation (other, SetUnion);
77 }
78
79 boost::shared_ptr<ExportFormatBase>
80 ExportFormatBase::do_set_operation (ExportFormatBase const & other, SetOperation operation) const
81 {
82         boost::shared_ptr<ExportFormatBase> result (new ExportFormatBase ());
83
84         /* Sets */
85
86         // Endiannesses
87         {
88                 EndianSet::const_iterator start1 = endiannesses.begin();
89                 EndianSet::const_iterator end1 = endiannesses.end();
90                 EndianSet::const_iterator start2 = other.endiannesses.begin();
91                 EndianSet::const_iterator end2 = other.endiannesses.end();
92                 std::insert_iterator<EndianSet> insert (result->endiannesses, result->endiannesses.begin());
93
94                 switch (operation) {
95                   case SetIntersection:
96                         std::set_intersection (start1, end1, start2, end2, insert);
97                         break;
98                   case SetUnion:
99                         std::set_union (start1, end1, start2, end2, insert);
100                         break;
101                 }
102         }
103
104         // Sample formats
105         {
106                 SampleFormatSet::const_iterator start1 = sample_formats.begin();
107                 SampleFormatSet::const_iterator end1 = sample_formats.end();
108                 SampleFormatSet::const_iterator start2 = other.sample_formats.begin();
109                 SampleFormatSet::const_iterator end2 = other.sample_formats.end();
110                 std::insert_iterator<SampleFormatSet> insert (result->sample_formats, result->sample_formats.begin());
111
112                 switch (operation) {
113                   case SetIntersection:
114                         std::set_intersection (start1, end1, start2, end2, insert);
115                         break;
116                   case SetUnion:
117                         std::set_union (start1, end1, start2, end2, insert);
118                         break;
119                 }
120         }
121
122
123         // Sample rates
124         {
125                 SampleRateSet::const_iterator start1 = sample_rates.begin();
126                 SampleRateSet::const_iterator end1 = sample_rates.end();
127                 SampleRateSet::const_iterator start2 = other.sample_rates.begin();
128                 SampleRateSet::const_iterator end2 = other.sample_rates.end();
129                 std::insert_iterator<SampleRateSet> insert (result->sample_rates, result->sample_rates.begin());
130
131                 switch (operation) {
132                   case SetIntersection:
133                         std::set_intersection (start1, end1, start2, end2, insert);
134                         break;
135                   case SetUnion:
136                         std::set_union (start1, end1, start2, end2, insert);
137                         break;
138                 }
139         }
140
141         // Format ids
142         {
143                 FormatSet::const_iterator start1 = format_ids.begin();
144                 FormatSet::const_iterator end1 = format_ids.end();
145                 FormatSet::const_iterator start2 = other.format_ids.begin();
146                 FormatSet::const_iterator end2 = other.format_ids.end();
147                 std::insert_iterator<FormatSet> insert (result->format_ids, result->format_ids.begin());
148
149                 switch (operation) {
150                   case SetIntersection:
151                         std::set_intersection (start1, end1, start2, end2, insert);
152                         break;
153                   case SetUnion:
154                         std::set_union (start1, end1, start2, end2, insert);
155                         break;
156                 }
157         }
158
159         // Qualities
160         {
161                 QualitySet::const_iterator start1 = qualities.begin();
162                 QualitySet::const_iterator end1 = qualities.end();
163                 QualitySet::const_iterator start2 = other.qualities.begin();
164                 QualitySet::const_iterator end2 = other.qualities.end();
165                 std::insert_iterator<QualitySet> insert (result->qualities, result->qualities.begin());
166
167                 switch (operation) {
168                   case SetIntersection:
169                         std::set_intersection (start1, end1, start2, end2, insert);
170                         break;
171                   case SetUnion:
172                         std::set_union (start1, end1, start2, end2, insert);
173                         break;
174                 }
175         }
176
177         return result;
178 }
179
180 ExportFormatBase::SampleRate
181 ExportFormatBase::nearest_sample_rate (framecnt_t sample_rate)
182 {
183         int diff = 0;
184         int smallest_diff = INT_MAX;
185         SampleRate best_match = SR_None;
186
187         #define DO_SR_COMPARISON(rate) \
188         diff = std::fabs((double)((rate) - sample_rate)); \
189         if(diff < smallest_diff) { \
190                 smallest_diff = diff; \
191                 best_match = (rate); \
192         }
193
194         DO_SR_COMPARISON(SR_8);
195         DO_SR_COMPARISON(SR_22_05);
196         DO_SR_COMPARISON(SR_44_1);
197         DO_SR_COMPARISON(SR_48);
198         DO_SR_COMPARISON(SR_88_2);
199         DO_SR_COMPARISON(SR_96);
200         DO_SR_COMPARISON(SR_192);
201
202         return best_match;
203         #undef DO_SR_COMPARISON
204 }
205
206 }; // namespace ARDOUR