61d9bef523e4199a35ef3b7e24a218ecb027058d
[ardour.git] / libs / ardour / export_format_specification.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_specification.h>
22
23 #include <sstream>
24
25 #include <ardour/export_format_compatibility.h>
26 #include <ardour/export_formats.h>
27 #include <ardour/session.h>
28
29 #include <pbd/error.h>
30 #include <pbd/xml++.h>
31 #include <pbd/enumwriter.h>
32 #include <pbd/convert.h>
33
34 #include "i18n.h"
35
36 namespace ARDOUR
37 {
38
39 using namespace PBD;
40 using std::string;
41
42 /* The id counter is initialized to 1000 so that user created profiles have a id > 1000 
43  * while ones shipped with ardour have one < 1000
44  */
45 uint32_t ExportFormatSpecification::_counter = 1000;
46
47 ExportFormatSpecification::Time &
48 ExportFormatSpecification::Time::operator= (AnyTime const & other)
49 {
50         type = other.type;
51         smpte = other.smpte;
52         bbt = other.bbt;
53         
54         if (type == Frames) {
55                 frames = other.frames;
56         } else {
57                 seconds = other.seconds;
58         }
59         
60         return *this;
61 }
62
63 nframes_t
64 ExportFormatSpecification::Time::get_frames (nframes_t target_rate) const
65 {       
66         //TODO position
67         nframes_t duration = session.convert_to_frames_at (0, *this);
68         
69         return ((double) target_rate / session.frame_rate()) * duration + 0.5;
70 }
71
72 XMLNode &
73 ExportFormatSpecification::Time::get_state ()
74 {
75         
76         XMLNode * node = new XMLNode ("Duration");
77
78         node->add_property ("format", enum_2_string (type));
79
80         switch (type) {
81           case SMPTE:
82                 node->add_property ("hours", to_string (smpte.hours, std::dec));
83                 node->add_property ("minutes", to_string (smpte.minutes, std::dec));
84                 node->add_property ("seconds", to_string (smpte.seconds, std::dec));
85                 node->add_property ("frames", to_string (smpte.frames, std::dec));
86                 break;
87           case BBT:
88                 node->add_property ("bars", to_string (bbt.bars, std::dec));
89                 node->add_property ("beats", to_string (bbt.beats, std::dec));
90                 node->add_property ("ticks", to_string (bbt.ticks, std::dec));
91                 break;
92           case Frames:
93                 node->add_property ("frames", to_string (frames, std::dec));
94                 break;
95           case Seconds:
96                 node->add_property ("seconds", to_string (seconds, std::dec));
97                 break;
98         }
99         
100         return *node;
101 }
102
103 int
104 ExportFormatSpecification::Time::set_state (const XMLNode & node)
105 {
106         XMLProperty const * prop;
107         
108         prop = node.property ("format");
109         
110         if (!prop) { return -1; }
111         
112         type = (Type) string_2_enum (prop->value(), Type);
113         
114         switch (type) {
115           case SMPTE:
116                 if ((prop = node.property ("hours"))) {
117                         smpte.hours = atoi (prop->value());
118                 }
119                 
120                 if ((prop = node.property ("minutes"))) {
121                         smpte.minutes = atoi (prop->value());
122                 }
123                 
124                 if ((prop = node.property ("seconds"))) {
125                         smpte.seconds = atoi (prop->value());
126                 }
127                 
128                 if ((prop = node.property ("frames"))) {
129                         smpte.frames = atoi (prop->value());
130                 }
131                 
132                 break;
133         
134           case BBT:
135                 if ((prop = node.property ("bars"))) {
136                         bbt.bars = atoi (prop->value());
137                 }
138                 
139                 if ((prop = node.property ("beats"))) {
140                         bbt.beats = atoi (prop->value());
141                 }
142                 
143                 if ((prop = node.property ("ticks"))) {
144                         bbt.ticks = atoi (prop->value());
145                 }
146         
147                 break;
148         
149           case Frames:
150                 if ((prop = node.property ("frames"))) {
151                         std::istringstream iss (prop->value());
152                         iss >> frames;
153                 }
154                 
155                 break;
156                 
157           case Seconds:
158                 if ((prop = node.property ("seconds"))) {
159                         seconds = atof (prop->value());
160                 }
161                 
162                 break;
163         }
164
165         return 0;
166 }
167
168 ExportFormatSpecification::ExportFormatSpecification (Session & s) :
169   session (s),
170
171    has_sample_format (false),
172    supports_tagging (false),
173   _has_broadcast_info (false),
174   _channel_limit (0),
175   _dither_type (D_None),
176   _src_quality (SRC_SincBest),
177   _tag (true),
178
179   _trim_beginning (false),
180   _silence_beginning (s),
181   _trim_end (false),
182   _silence_end (s),
183
184   _normalize (false),
185   _normalize_target (1.0)
186 {
187         format_ids.insert (F_None);
188         endiannesses.insert (E_FileDefault);
189         sample_formats.insert (SF_None);
190         sample_rates.insert (SR_None);
191         qualities.insert (Q_None);
192         
193         _id = ++_counter;
194 }
195
196 ExportFormatSpecification::ExportFormatSpecification (Session & s, XMLNode const & state) :
197    session (s),
198   _silence_beginning (s),
199   _silence_end (s)
200 {
201         _silence_beginning.type = Time::SMPTE;
202         _silence_end.type = Time::SMPTE;
203
204         set_state (state);
205 }
206
207 ExportFormatSpecification::ExportFormatSpecification (ExportFormatSpecification const & other) :
208    session (other.session),
209   _silence_beginning (other.session),
210   _silence_end (other.session)
211 {
212         set_name (other.name() + " (copy)");
213         _id = ++_counter;
214
215         _format_name = other._format_name;
216         has_sample_format = other.has_sample_format;
217         
218         supports_tagging = other.supports_tagging;
219         _has_broadcast_info = other._has_broadcast_info;
220         _channel_limit = other._channel_limit;
221
222         set_type (other.type());
223         set_format_id (other.format_id());
224         set_endianness (other.endianness());
225         set_sample_format (other.sample_format());
226         set_sample_rate (other.sample_rate());
227         set_quality (other.quality());
228         
229         set_dither_type (other.dither_type());
230         set_src_quality (other.src_quality());
231         set_trim_beginning (other.trim_beginning());
232         set_trim_end (other.trim_end());
233         set_normalize (other.normalize());
234         set_normalize_target (other.normalize_target());
235         
236         set_tag (other.tag());
237         
238         set_silence_beginning (other.silence_beginning_time());
239         set_silence_end (other.silence_end_time());
240 }
241
242 ExportFormatSpecification::~ExportFormatSpecification ()
243 {
244 }
245
246 XMLNode &
247 ExportFormatSpecification::get_state ()
248 {
249         XMLNode * node;
250         XMLNode * root = new XMLNode ("ExportFormatSpecification");
251         
252         root->add_property ("name", _name);
253         root->add_property ("id", to_string (_id, std::dec));
254         
255         node = root->add_child ("Encoding");
256         node->add_property ("id", enum_2_string (format_id()));
257         node->add_property ("type", enum_2_string (type()));
258         node->add_property ("extension", extension());
259         node->add_property ("name", _format_name);
260         node->add_property ("has-sample-format", has_sample_format ? "true" : "false");
261         node->add_property ("channel-limit", to_string (_channel_limit, std::dec));
262         
263         node = root->add_child ("SampleRate");
264         node->add_property ("rate", to_string (sample_rate(), std::dec));
265         
266         node = root->add_child ("SRCQuality");
267         node->add_property ("quality", enum_2_string (src_quality()));
268         
269         XMLNode * enc_opts = root->add_child ("EncodingOptions");
270         
271         add_option (enc_opts, "sample-format", enum_2_string (sample_format()));
272         add_option (enc_opts, "dithering", enum_2_string (dither_type()));
273         add_option (enc_opts, "tag-metadata", _tag ? "true" : "false");
274         add_option (enc_opts, "tag-support", supports_tagging ? "true" : "false");
275         add_option (enc_opts, "broadcast-info", _has_broadcast_info ? "true" : "false");
276         
277         XMLNode * processing = root->add_child ("Processing");
278         
279         node = processing->add_child ("Normalize");
280         node->add_property ("enabled", normalize() ? "true" : "false");
281         node->add_property ("target", to_string (normalize_target(), std::dec));
282         
283         XMLNode * silence = processing->add_child ("Silence");
284         XMLNode * start = silence->add_child ("Start");
285         XMLNode * end = silence->add_child ("End");
286         
287         node = start->add_child ("Trim");
288         node->add_property ("enabled", trim_beginning() ? "true" : "false");
289         
290         node = start->add_child ("Add");
291         node->add_property ("enabled", silence_beginning() > 0 ? "true" : "false");
292         node->add_child_nocopy (_silence_beginning.get_state());
293         
294         node = end->add_child ("Trim");
295         node->add_property ("enabled", trim_end() ? "true" : "false");
296         
297         node = end->add_child ("Add");
298         node->add_property ("enabled", silence_end() > 0 ? "true" : "false");
299         node->add_child_nocopy (_silence_end.get_state());
300         
301         return *root;
302 }
303
304 int
305 ExportFormatSpecification::set_state (const XMLNode & root)
306 {
307         XMLProperty const * prop;
308         XMLNode const * child;
309         string value;
310         
311         if ((prop = root.property ("name"))) {
312                 _name = prop->value();
313         }
314         
315         if ((prop = root.property ("id"))) {
316                 std::istringstream iss (prop->value());
317                 iss >> _id;
318         }
319         
320         /* Encoding and SRC */
321         
322         if ((child = root.child ("Encoding"))) {
323                 if ((prop = child->property ("id"))) {
324                         set_format_id ((FormatId) string_2_enum (prop->value(), FormatId));
325                 }
326                 
327                 if ((prop = child->property ("type"))) {
328                         set_type ((Type) string_2_enum (prop->value(), Type));
329                 }
330                 
331                 if ((prop = child->property ("extension"))) {
332                         set_extension (prop->value());
333                 }
334                 
335                 if ((prop = child->property ("name"))) {
336                         _format_name = prop->value();
337                 }
338                 
339                 if ((prop = child->property ("has-sample-format"))) {
340                         has_sample_format = !prop->value().compare ("true");
341                 }
342                 
343                 if ((prop = child->property ("channel-limit"))) {
344                         _channel_limit = atoi (prop->value());
345                 }
346         }
347         
348         if ((child = root.child ("SampleRate"))) {
349                 if ((prop = child->property ("rate"))) {
350                         set_sample_rate ( (SampleRate) string_2_enum (prop->value(), SampleRate));
351                 }
352         }
353         
354         if ((child = root.child ("SRCQuality"))) {
355                 if ((prop = child->property ("quality"))) {
356                         _src_quality = (SRCQuality) string_2_enum (prop->value(), SRCQuality);
357                 }
358         }
359         
360         /* Encoding options */
361         
362         if ((child = root.child ("EncodingOptions"))) {
363                 set_sample_format ((SampleFormat) string_2_enum (get_option (child, "sample-format"), SampleFormat));
364                 set_dither_type ((DitherType) string_2_enum (get_option (child, "dithering"), DitherType));
365                 set_tag (!(get_option (child, "tag-metadata").compare ("true")));
366                 supports_tagging = (!(get_option (child, "tag-support").compare ("true")));
367                 _has_broadcast_info = (!(get_option (child, "broadcast-info").compare ("true")));
368         }
369         
370         /* Processing */
371         
372         XMLNode const * proc = root.child ("Processing");
373         if (!proc) { std::cerr << X_("Could not load processing for export format") << std::endl; return -1; }
374         
375         if ((child = proc->child ("Normalize"))) {
376                 if ((prop = child->property ("enabled"))) {
377                         _normalize = (!prop->value().compare ("true"));
378                 }
379                 
380                 if ((prop = child->property ("target"))) {
381                         _normalize_target = atof (prop->value());
382                 }
383         }
384         
385         XMLNode const * silence = proc->child ("Silence");
386         if (!silence) { std::cerr << X_("Could not load silence for export format") << std::endl; return -1; }
387         
388         XMLNode const * start = silence->child ("Start");
389         XMLNode const * end = silence->child ("End");
390         if (!start || !end) { std::cerr << X_("Could not load end or start silence for export format") << std::endl; return -1; }
391         
392         /* Silence start */
393         
394         if ((child = start->child ("Trim"))) {
395                 if ((prop = child->property ("enabled"))) {
396                         _trim_beginning = (!prop->value().compare ("true"));
397                 }
398         }
399         
400         if ((child = start->child ("Add"))) {
401                 if ((prop = child->property ("enabled"))) {
402                         if (!prop->value().compare ("true")) {
403                                 if ((child = child->child ("Duration"))) {
404                                         _silence_beginning.set_state (*child);
405                                 }
406                         } else {
407                                 _silence_beginning.type = Time::SMPTE;
408                         }
409                 }
410         }
411         
412         /* Silence end */
413         
414         if ((child = end->child ("Trim"))) {
415                 if ((prop = child->property ("enabled"))) {
416                         _trim_end = (!prop->value().compare ("true"));
417                 }
418         }
419         
420         if ((child = end->child ("Add"))) {
421                 if ((prop = child->property ("enabled"))) {
422                         if (!prop->value().compare ("true")) {
423                                 if ((child = child->child ("Duration"))) {
424                                         _silence_end.set_state (*child);
425                                 }
426                         } else {
427                                 _silence_end.type = Time::SMPTE;
428                         }
429                 }
430         }
431         
432         return 0;
433 }
434
435 bool
436 ExportFormatSpecification::is_compatible_with (ExportFormatCompatibility const & compatibility) const
437 {
438         boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
439         
440         if (intersection->formats_empty() && format_id() != 0) {
441                 return false;
442         }
443         
444         if (intersection->endiannesses_empty() && endianness() != E_FileDefault) {
445                 return false;
446         }
447         
448         if (intersection->sample_rates_empty() && sample_rate() != SR_None) {
449                 return false;
450         }
451         
452         if (intersection->sample_formats_empty() && sample_format() != SF_None) {
453                 return false;
454         }
455         
456         if (intersection->qualities_empty() && quality() != Q_None) {
457                 return false;
458         }
459         
460         return true;
461 }
462
463 bool
464 ExportFormatSpecification::is_complete () const
465 {
466         if (type() == T_None) {
467                 return false;
468         }
469         
470         if (!format_id()) {
471                 return false;
472         }
473         
474         if (!sample_rate()) {
475                 return false;
476         }
477         
478         if (has_sample_format) {
479                 if (sample_format() == SF_None) {
480                         return false;
481                 }
482         }
483
484         return true;
485 }
486
487 void
488 ExportFormatSpecification::set_format (boost::shared_ptr<ExportFormat> format)
489 {
490         if (format) {
491                 set_format_id (format->get_format_id ());
492                 set_type (format->get_type());
493                 set_extension (format->extension());
494                 
495                 if (format->get_explicit_sample_format()) {
496                         set_sample_format (format->get_explicit_sample_format());
497                 }
498                 
499                 if (format->has_sample_format()) {
500                         has_sample_format = true;
501                 }
502                 
503                 if (format->has_broadcast_info()) {
504                         _has_broadcast_info = true;
505                 }
506                 
507                 supports_tagging = format->supports_tagging ();
508                 _channel_limit = format->get_channel_limit();
509                 
510                 _format_name = format->name();
511         } else {
512                 set_format_id (F_None);
513                 set_type (T_None);
514                 set_extension ("");
515                 _has_broadcast_info = false;
516                 has_sample_format = false;
517                 supports_tagging = false;
518                 _channel_limit = 0;
519                 _format_name = "";
520         }
521 }
522
523 Glib::ustring
524 ExportFormatSpecification::description ()
525 {
526         Glib::ustring desc;
527         
528         desc = _name + ": ";
529         
530         if (_normalize) {
531                 desc += _("normalize, ");
532         }
533         
534         if (_trim_beginning && _trim_end) {
535                 desc += _("trim, ");
536         } else if (_trim_beginning) {
537                 desc += _("trim start, ");
538         } else if (_trim_end) {
539                 desc += "trim end, ";
540         }
541         
542         desc += _format_name + ", ";
543         
544         if (has_sample_format) {
545                 desc += HasSampleFormat::get_sample_format_name (sample_format())  + ", ";
546         }
547         
548         switch (sample_rate()) {
549           case SR_22_05:
550                 desc += "22,5 kHz";
551                 break;
552           case SR_44_1:
553                 desc += "44,1 kHz";
554                 break;
555           case SR_48:
556                 desc += "48 kHz";
557                 break;
558           case SR_88_2:
559                 desc += "88,2 kHz";
560                 break;
561           case SR_96:
562                 desc += "96 kHz";
563                 break;
564           case SR_192:
565                 desc += "192 kHz";
566                 break;
567           case SR_None:
568                 break;
569         }
570         
571         return desc;
572 }
573
574 void
575 ExportFormatSpecification::add_option (XMLNode * node, std::string const & name, std::string const & value)
576 {
577         node = node->add_child ("Option");
578         node->add_property ("name", name);
579         node->add_property ("value", value);
580 }
581
582 std::string
583 ExportFormatSpecification::get_option (XMLNode const * node, std::string const & name)
584 {
585         XMLNodeList list (node->children ("Option"));
586         
587         for (XMLNodeList::iterator it = list.begin(); it != list.end(); ++it) {
588                 XMLProperty * prop = (*it)->property ("name");
589                 if (prop && !name.compare (prop->value())) {
590                         prop = (*it)->property ("value");
591                         if (prop) {
592                                 return prop->value();
593                         }
594                 }
595         }
596         
597         std::cerr << "Could not load encoding option \"" << name << "\" for export format" << std::endl;
598         
599         return "";
600 }
601
602 }; // namespace ARDOUR