Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[ardour.git] / libs / ardour / export_formats.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_formats.h>
22
23 #include "i18n.h"
24
25 namespace ARDOUR
26 {
27
28 bool
29 ExportFormat::has_sample_format ()
30 {
31         return dynamic_cast<HasSampleFormat *> (this);
32 }
33
34 bool
35 ExportFormat::sample_format_is_compatible (SampleFormat format) const
36 {
37         return (sample_formats.find (format) != sample_formats.end());
38 }
39
40 /*** HasSampleFormat ***/
41
42 HasSampleFormat::HasSampleFormat (ExportFormatBase::SampleFormatSet & sample_formats) :
43   _sample_formats (sample_formats)
44 {
45         /* Dither Types */
46         
47         add_dither_type (ExportFormatBase::D_Shaped, _("Shaped Noise"));
48         add_dither_type (ExportFormatBase::D_Tri, _("Triangular"));
49         add_dither_type (ExportFormatBase::D_Rect, _("Rectangular"));
50         add_dither_type (ExportFormatBase::D_None,  _("None"));
51 }
52
53 void
54 HasSampleFormat::add_sample_format (ExportFormatBase::SampleFormat format)
55 {
56         _sample_formats.insert (format);
57         
58         SampleFormatPtr ptr (new SampleFormatState (format, get_sample_format_name (format)));
59         sample_format_states.push_back (ptr);
60         ptr->SelectChanged.connect (sigc::bind (SampleFormatSelectChanged.make_slot(), WeakSampleFormatPtr (ptr)));
61         ptr->SelectChanged.connect (sigc::mem_fun (*this, &HasSampleFormat::update_sample_format_selection));
62         ptr->CompatibleChanged.connect (sigc::bind (SampleFormatCompatibleChanged.make_slot(), WeakSampleFormatPtr (ptr)));
63 }
64
65 void
66 HasSampleFormat::add_dither_type (ExportFormatBase::DitherType type, Glib::ustring name)
67 {
68         DitherTypePtr ptr (new DitherTypeState (type, name));
69         dither_type_states.push_back (ptr);
70         ptr->SelectChanged.connect (sigc::bind (DitherTypeSelectChanged.make_slot(), WeakDitherTypePtr (ptr)));
71         ptr->SelectChanged.connect (sigc::mem_fun (*this, &HasSampleFormat::update_dither_type_selection));
72         ptr->CompatibleChanged.connect (sigc::bind (DitherTypeCompatibleChanged.make_slot(), WeakDitherTypePtr (ptr)));
73 }
74
75 HasSampleFormat::SampleFormatPtr
76 HasSampleFormat::get_selected_sample_format ()
77 {
78         for (SampleFormatList::iterator it = sample_format_states.begin(); it != sample_format_states.end(); ++it) {
79                 if ((*it)->selected()) {
80                         return *it;
81                 }
82         }
83         
84         return SampleFormatPtr();
85 }
86
87 HasSampleFormat::DitherTypePtr
88 HasSampleFormat::get_selected_dither_type ()
89 {
90         for (DitherTypeList::iterator it = dither_type_states.begin(); it != dither_type_states.end(); ++it) {
91                 if ((*it)->selected()) {
92                         return *it;
93                 }
94         }
95         
96         return DitherTypePtr();
97 }
98
99 void
100 HasSampleFormat::update_sample_format_selection (bool)
101 {
102         SampleFormatPtr format = get_selected_sample_format();
103         if (!format) {
104                 return;
105         }
106         
107         if (format->format == ExportFormatBase::SF_24 ||
108             format->format == ExportFormatBase::SF_32 ||
109             format->format == ExportFormatBase::SF_Float ||
110             format->format == ExportFormatBase::SF_Double) {
111                 for (DitherTypeList::iterator it = dither_type_states.begin(); it != dither_type_states.end(); ++it) {
112                         if ((*it)->type == ExportFormatBase::D_None) {
113                                 (*it)->set_selected (true);
114                         } else {
115                                 (*it)->set_compatible (false);
116                         }
117                 }
118
119         } else {
120                 for (DitherTypeList::iterator it = dither_type_states.begin(); it != dither_type_states.end(); ++it) {
121                         (*it)->set_compatible (true);
122                 }
123         }
124 }
125
126 void
127 HasSampleFormat::update_dither_type_selection (bool)
128 {
129         DitherTypePtr type = get_selected_dither_type();
130         if (!type) {
131                 return;
132         }
133         
134         if (!type->compatible()) {
135                 SampleFormatPtr format = get_selected_sample_format();
136                 if (format) {
137                         format->set_selected (false);
138                 }
139                 
140                 for (DitherTypeList::iterator it = dither_type_states.begin(); it != dither_type_states.end(); ++it) {
141                         (*it)->set_compatible (true);
142                 }
143         }
144 }
145
146 string
147 HasSampleFormat::get_sample_format_name (ExportFormatBase::SampleFormat format)
148 {
149         switch (format) {
150           case ExportFormatBase::SF_8:
151                 return _("8bit");
152           case ExportFormatBase::SF_16:
153                 return _("16bit");
154           case ExportFormatBase::SF_24:
155                 return _("24bit");
156           case ExportFormatBase::SF_32:
157                 return _("32bit");
158           case ExportFormatBase::SF_Float:
159                 return _("float");
160           case ExportFormatBase::SF_Double:
161                 return _("double");
162           case ExportFormatBase::SF_U8:
163                 return _("8bit unsigned");
164           case ExportFormatBase::SF_Vorbis:
165                 return _("Vorbis sample format");
166           case ExportFormatBase::SF_None:
167                 return _("No sample format");
168         }
169         return "";
170 }
171
172 /*** Linear ***/
173
174 ExportFormatLinear::ExportFormatLinear (Glib::ustring name, FormatId format_id) :
175   HasSampleFormat (sample_formats),
176   _default_sample_format (SF_None)
177 {
178         set_name (name);
179         set_format_id (format_id);
180         
181         add_sample_rate (SR_22_05);
182         add_sample_rate (SR_44_1);
183         add_sample_rate (SR_48);
184         add_sample_rate (SR_88_2);
185         add_sample_rate (SR_96);
186         add_sample_rate (SR_192);
187         
188         add_endianness (E_FileDefault);
189         
190         set_quality (Q_LosslessLinear);
191 }
192
193 bool
194 ExportFormatLinear::set_compatibility_state (ExportFormatCompatibility const & compatibility)
195 {
196         /* Global state */
197
198         bool compatible = true;
199
200         if (!compatibility.has_quality (Q_LosslessLinear)) {
201                 compatible = false;
202         }
203         
204         if (!compatibility.has_format (get_format_id())) {
205                 compatible = false;
206         }
207
208         boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
209         
210         if (intersection->endiannesses_empty()) {
211                 compatible = false;
212         }
213         
214         if (intersection->sample_rates_empty()) {
215                 compatible = false;
216         }
217         
218         if (intersection->sample_formats_empty()) {
219                 compatible = false;
220         }
221         
222         set_compatible (compatible);
223
224         /* Sample Formats */
225         
226         for (SampleFormatList::iterator it = sample_format_states.begin(); it != sample_format_states.end(); ++it) {
227                 (*it)->set_compatible (compatibility.has_sample_format ((*it)->format));
228         }
229
230         return compatible;
231 }
232
233 /*** Ogg Vorbis ***/
234
235 ExportFormatOggVorbis::ExportFormatOggVorbis ()
236 {
237         /* Check system compatibility */
238         
239         SF_INFO sf_info;
240         sf_info.channels = 2;
241         sf_info.samplerate = SR_44_1;
242         sf_info.format = F_Ogg | SF_Vorbis;
243         if (sf_format_check (&sf_info) != SF_TRUE) {
244                 throw ExportFormatIncompatible();
245         }
246         
247         set_name ("Ogg Vorbis");
248         set_format_id (F_Ogg);
249         sample_formats.insert (SF_Vorbis);
250         
251         add_sample_rate (SR_22_05);
252         add_sample_rate (SR_44_1);
253         add_sample_rate (SR_48);
254         add_sample_rate (SR_88_2);
255         add_sample_rate (SR_96);
256         add_sample_rate (SR_192);
257         
258         add_endianness (E_FileDefault);
259         
260         set_extension ("ogg");
261         set_quality (Q_LossyCompression);
262 }
263
264 bool
265 ExportFormatOggVorbis::set_compatibility_state (ExportFormatCompatibility const & compatibility)
266 {
267         bool compatible = compatibility.has_format (F_Ogg);
268         set_compatible (compatible);
269         return compatible;
270 }
271
272 /*** FLAC ***/
273
274 ExportFormatFLAC::ExportFormatFLAC () :
275   HasSampleFormat (sample_formats)
276 {
277         /* Check system compatibility */
278         
279         SF_INFO sf_info;
280         sf_info.channels = 2;
281         sf_info.samplerate = SR_44_1;
282         sf_info.format = F_FLAC | SF_16;
283         if (sf_format_check (&sf_info) != SF_TRUE) {
284                 throw ExportFormatIncompatible();
285         }
286         
287         set_name ("FLAC");
288         set_format_id (F_FLAC);
289         
290         add_sample_rate (SR_22_05);
291         add_sample_rate (SR_44_1);
292         add_sample_rate (SR_48);
293         add_sample_rate (SR_88_2);
294         add_sample_rate (SR_96);
295         add_sample_rate (SR_192);
296         
297         add_sample_format (SF_8);
298         add_sample_format (SF_16);
299         add_sample_format (SF_24);
300         
301         add_endianness (E_FileDefault);
302         
303         set_extension ("flac");
304         set_quality (Q_LosslessCompression);
305 }
306
307 bool
308 ExportFormatFLAC::set_compatibility_state (ExportFormatCompatibility const & compatibility)
309 {
310         bool compatible = compatibility.has_format (F_FLAC);
311         set_compatible (compatible);
312         return compatible;
313 }
314
315 /*** BWF ***/
316
317 ExportFormatBWF::ExportFormatBWF () :
318   HasSampleFormat (sample_formats)
319 {
320         set_name ("BWF");
321         set_format_id (F_WAV);
322         
323         add_sample_rate (SR_22_05);
324         add_sample_rate (SR_44_1);
325         add_sample_rate (SR_48);
326         add_sample_rate (SR_88_2);
327         add_sample_rate (SR_96);
328         add_sample_rate (SR_192);
329
330         add_sample_format (SF_U8);
331         add_sample_format (SF_16);
332         add_sample_format (SF_24);
333         add_sample_format (SF_32);
334         add_sample_format (SF_Float);
335         add_sample_format (SF_Double);
336         
337         add_endianness (E_FileDefault);
338         
339         set_extension ("wav");
340         set_quality (Q_LosslessLinear);
341 }
342
343 bool
344 ExportFormatBWF::set_compatibility_state (ExportFormatCompatibility const & compatibility)
345 {
346         bool compatible = compatibility.has_format (F_WAV);
347         set_compatible (compatible);
348         return compatible;
349 }
350
351 }; // namespace ARDOUR