Only show user-presets in favorite sidebar
[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 "ardour/export_format_compatibility.h"
24 #include "ardour/export_formats.h"
25 #include "ardour/session.h"
26 #include "ardour/types_convert.h"
27
28 #include "pbd/error.h"
29 #include "pbd/xml++.h"
30 #include "pbd/enumwriter.h"
31 #include "pbd/enum_convert.h"
32 #include "pbd/string_convert.h"
33 #include "pbd/types_convert.h"
34
35 #include "pbd/i18n.h"
36
37 namespace PBD {
38         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::FormatId)
39         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::SampleRate)
40         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::SampleFormat)
41         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::DitherType)
42         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::SRCQuality)
43         DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::Type)
44 }
45
46 namespace ARDOUR
47 {
48
49 using namespace PBD;
50 using std::string;
51 using std::list;
52
53 ExportFormatSpecification::Time &
54 ExportFormatSpecification::Time::operator= (AnyTime const & other)
55 {
56         static_cast<AnyTime &>(*this) = other;
57         return *this;
58 }
59
60 samplecnt_t
61 ExportFormatSpecification::Time::get_samples_at (samplepos_t position, samplecnt_t target_rate) const
62 {
63         samplecnt_t duration = session.any_duration_to_samples (position, *this);
64         return ((double) target_rate / session.sample_rate()) * duration + 0.5;
65 }
66
67 XMLNode &
68 ExportFormatSpecification::Time::get_state ()
69 {
70
71         XMLNode * node = new XMLNode ("Duration");
72
73         node->set_property ("format", type);
74
75         switch (type) {
76           case Timecode:
77                 node->set_property ("hours", timecode.hours);
78                 node->set_property ("minutes", timecode.minutes);
79                 node->set_property ("seconds", timecode.seconds);
80                 node->set_property ("frames", timecode.frames);
81                 break;
82           case BBT:
83                 node->set_property ("bars", bbt.bars);
84                 node->set_property ("beats", bbt.beats);
85                 node->set_property ("ticks", bbt.ticks);
86                 break;
87           case Samples:
88                 node->set_property ("samples", samples);
89                 break;
90           case Seconds:
91                 node->set_property ("seconds", seconds);
92                 break;
93         }
94
95         return *node;
96 }
97
98 int
99 ExportFormatSpecification::Time::set_state (const XMLNode & node)
100 {
101         if (!node.get_property ("format", type)) {
102                 return -1;
103         }
104
105         switch (type) {
106         case Timecode:
107                 node.get_property ("hours", timecode.hours);
108                 node.get_property ("minutes", timecode.minutes);
109                 node.get_property ("seconds", timecode.seconds);
110                 node.get_property ("frames", timecode.frames);
111                 break;
112
113         case BBT:
114                 node.get_property ("bars", bbt.bars);
115                 node.get_property ("beats", bbt.beats);
116                 node.get_property ("ticks", bbt.ticks);
117                 break;
118
119         case Samples:
120                 node.get_property ("samples", samples);
121                 break;
122
123         case Seconds:
124                 node.get_property ("seconds", seconds);
125                 break;
126
127         }
128
129         return 0;
130 }
131
132 ExportFormatSpecification::ExportFormatSpecification (Session & s)
133         : session (s)
134         , has_sample_format (false)
135         , supports_tagging (false)
136         , _has_codec_quality (false)
137         , _has_broadcast_info (false)
138         , _channel_limit (0)
139         , _dither_type (D_None)
140         , _src_quality (SRC_SincBest)
141         , _tag (true)
142
143         , _trim_beginning (false)
144         , _silence_beginning (s)
145         , _trim_end (false)
146         , _silence_end (s)
147
148         , _normalize (false)
149         , _normalize_loudness (false)
150         , _normalize_dbfs (GAIN_COEFF_UNITY)
151         , _normalize_lufs (-23)
152         , _normalize_dbtp (-1)
153         , _with_toc (false)
154         , _with_cue (false)
155         , _with_mp4chaps (false)
156         , _soundcloud_upload (false)
157         , _command ("")
158         , _analyse (true)
159         , _codec_quality (0)
160 {
161         format_ids.insert (F_None);
162         endiannesses.insert (E_FileDefault);
163         sample_formats.insert (SF_None);
164         sample_rates.insert (SR_None);
165         qualities.insert (Q_None);
166 }
167
168 ExportFormatSpecification::ExportFormatSpecification (Session & s, XMLNode const & state)
169         : session (s)
170         , has_sample_format (false)
171         , supports_tagging (false)
172         , _has_codec_quality (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_loudness (false)
186         , _normalize_dbfs (GAIN_COEFF_UNITY)
187         , _normalize_lufs (-23)
188         , _normalize_dbtp (-1)
189         , _with_toc (false)
190         , _with_cue (false)
191         , _with_mp4chaps (false)
192         , _soundcloud_upload (false)
193         , _command ("")
194         , _analyse (true)
195         , _codec_quality (0)
196 {
197         _silence_beginning.type = Time::Timecode;
198         _silence_end.type = Time::Timecode;
199
200         set_state (state);
201 }
202
203 ExportFormatSpecification::ExportFormatSpecification (ExportFormatSpecification const & other, bool modify_name)
204         : ExportFormatBase(other)
205         , session (other.session)
206         , _silence_beginning (other.session)
207         , _silence_end (other.session)
208         , _with_toc (other._with_toc)
209         , _with_cue (other._with_cue)
210         , _with_mp4chaps (other._with_mp4chaps)
211         , _soundcloud_upload (false)
212         , _command (other._command)
213         , _analyse (other._analyse)
214         , _codec_quality (other._codec_quality)
215 {
216         if (modify_name) {
217                 set_name (other.name() + " (copy)");
218         } else {
219                 set_name (other.name());
220         }
221
222         _format_name = other._format_name;
223         has_sample_format = other.has_sample_format;
224         supports_tagging = other.supports_tagging;
225         _has_codec_quality = other._has_codec_quality;
226         _has_broadcast_info = other._has_broadcast_info;
227         _channel_limit = other._channel_limit;
228
229         set_type (other.type());
230         set_format_id (other.format_id());
231         set_endianness (other.endianness());
232         set_sample_format (other.sample_format());
233         set_sample_rate (other.sample_rate());
234         set_quality (other.quality());
235
236         set_dither_type (other.dither_type());
237         set_src_quality (other.src_quality());
238         set_trim_beginning (other.trim_beginning());
239         set_trim_end (other.trim_end());
240         set_normalize (other.normalize());
241         set_normalize_loudness (other.normalize_loudness());
242         set_normalize_dbfs (other.normalize_dbfs());
243         set_normalize_lufs (other.normalize_lufs());
244         set_normalize_dbtp (other.normalize_dbtp());
245
246         set_tag (other.tag());
247
248         set_silence_beginning (other.silence_beginning_time());
249         set_silence_end (other.silence_end_time());
250
251         set_extension(other.extension());
252 }
253
254 ExportFormatSpecification::~ExportFormatSpecification ()
255 {
256 }
257
258 XMLNode &
259 ExportFormatSpecification::get_state ()
260 {
261         XMLNode * node;
262         XMLNode * root = new XMLNode ("ExportFormatSpecification");
263
264         root->set_property ("name", _name);
265         root->set_property ("id", _id.to_s());
266         root->set_property ("with-cue", _with_cue);
267         root->set_property ("with-toc", _with_toc);
268         root->set_property ("with-mp4chaps", _with_mp4chaps);
269         root->set_property ("command", _command);
270         root->set_property ("analyse", _analyse);
271         root->set_property ("soundcloud-upload", _soundcloud_upload);
272
273         node = root->add_child ("Encoding");
274         node->set_property ("id", format_id());
275         node->set_property ("type", type());
276         node->set_property ("extension", extension());
277         node->set_property ("name", _format_name);
278         node->set_property ("has-sample-format", has_sample_format);
279         node->set_property ("channel-limit", _channel_limit);
280
281         node = root->add_child ("SampleRate");
282         node->set_property ("rate", sample_rate());
283
284         node = root->add_child ("SRCQuality");
285         node->set_property ("quality", src_quality());
286
287         if (_has_codec_quality) {
288                 node = root->add_child ("CodecQuality");
289                 node->set_property ("quality", codec_quality());
290         }
291
292         XMLNode * enc_opts = root->add_child ("EncodingOptions");
293
294         add_option (enc_opts, "sample-format", to_string(sample_format()));
295         add_option (enc_opts, "dithering", to_string (dither_type()));
296         add_option (enc_opts, "tag-metadata", to_string (_tag));
297         add_option (enc_opts, "tag-support", to_string (supports_tagging));
298         add_option (enc_opts, "broadcast-info", to_string (_has_broadcast_info));
299
300         XMLNode * processing = root->add_child ("Processing");
301
302         node = processing->add_child ("Normalize");
303         node->set_property ("enabled", normalize());
304         node->set_property ("loudness", normalize_loudness());
305         node->set_property ("dbfs", normalize_dbfs());
306         node->set_property ("lufs", normalize_lufs());
307         node->set_property ("dbtp", normalize_dbtp());
308
309         XMLNode * silence = processing->add_child ("Silence");
310         XMLNode * start = silence->add_child ("Start");
311         XMLNode * end = silence->add_child ("End");
312
313         node = start->add_child ("Trim");
314         node->set_property ("enabled", trim_beginning());
315
316         node = start->add_child ("Add");
317         node->set_property ("enabled", _silence_beginning.not_zero());
318         node->add_child_nocopy (_silence_beginning.get_state());
319
320         node = end->add_child ("Trim");
321         node->set_property ("enabled", trim_end());
322
323         node = end->add_child ("Add");
324         node->set_property ("enabled", _silence_end.not_zero());
325         node->add_child_nocopy (_silence_end.get_state());
326
327         return *root;
328 }
329
330 int
331 ExportFormatSpecification::set_state (const XMLNode & root)
332 {
333         XMLNode const * child;
334         string str;
335
336         root.get_property ("name", _name);
337
338         if (root.get_property ("id", str)) {
339                 _id = str;
340         }
341
342         if (!root.get_property ("with-cue", _with_cue)) {
343                 _with_cue = false;
344         }
345
346         if (!root.get_property ("with-toc", _with_toc)) {
347                 _with_toc = false;
348         }
349
350         if (!root.get_property ("with-mp4chaps", _with_mp4chaps)) {
351                 _with_mp4chaps = false;
352         }
353
354         if (!root.get_property ("command", _command)) {
355                 _command = "";
356         }
357
358         if (!root.get_property ("analyse", _analyse)) {
359                 _analyse = false;
360         }
361
362         if (!root.get_property ("soundcloud-upload", _soundcloud_upload)) {
363                 _soundcloud_upload = false;
364         }
365
366         /* Encoding and SRC */
367
368         if ((child = root.child ("Encoding"))) {
369                 FormatId fid;
370                 if (child->get_property ("id", fid)) {
371                         set_format_id (fid);
372                 }
373
374                 ExportFormatBase::Type type;
375                 if (child->get_property ("type", type)) {
376                         set_type (type);
377                 }
378
379                 if (child->get_property ("extension", str)) {
380                         set_extension (str);
381                 }
382
383                 child->get_property ("name", _format_name);
384                 child->get_property ("has-sample-format", has_sample_format);
385                 child->get_property ("channel-limit", _channel_limit);
386         }
387
388         if ((child = root.child ("SampleRate"))) {
389                 SampleRate rate;
390                 if (child->get_property ("rate", rate)) {
391                         set_sample_rate (rate);
392                 }
393         }
394
395         if ((child = root.child ("SRCQuality"))) {
396                 child->get_property ("quality", _src_quality);
397         }
398
399         if ((child = root.child ("CodecQuality"))) {
400                 child->get_property ("quality", _codec_quality);
401                 _has_codec_quality = true;
402         } else {
403                 _has_codec_quality = false;
404         }
405
406         /* fixup codec quality for old states */
407         if (!_has_codec_quality) {
408                 /* We'd need an instance of ExportFormatManager to look up
409                  * defaults for a given type -- in the future there may even be
410                  * difference qualities depending on sub-type, so we just
411                  * hardcode them here for the time being.
412                  */
413                 if (format_id() == F_FFMPEG) {
414                         _codec_quality = -2; // ExportFormatOggVorbis::default_codec_quality();
415                 }
416                 else if (format_id() == F_Ogg) {
417                         _codec_quality = 40; // ExportFormatFFMPEG::default_codec_quality();
418                 }
419         }
420
421         /* Encoding options */
422
423         if ((child = root.child ("EncodingOptions"))) {
424                 set_sample_format ((SampleFormat) string_2_enum (get_option (child, "sample-format"), SampleFormat));
425                 set_dither_type ((DitherType) string_2_enum (get_option (child, "dithering"), DitherType));
426                 set_tag (string_to<bool>(get_option (child, "tag-metadata")));
427                 supports_tagging = string_to<bool>(get_option (child, "tag-support"));
428                 _has_broadcast_info = string_to<bool>(get_option (child, "broadcast-info"));
429         }
430
431         /* Processing */
432
433         XMLNode const * proc = root.child ("Processing");
434         if (!proc) { std::cerr << X_("Could not load processing for export format") << std::endl; return -1; }
435
436         if ((child = proc->child ("Normalize"))) {
437                 child->get_property ("enabled", _normalize);
438                 // old formats before ~ 4.7-930ish
439                 child->get_property ("target", _normalize_dbfs);
440                 child->get_property ("loudness", _normalize_loudness);
441                 child->get_property ("dbfs", _normalize_dbfs);
442                 child->get_property ("lufs", _normalize_lufs);
443                 child->get_property ("dbtp", _normalize_dbtp);
444         }
445
446         XMLNode const * silence = proc->child ("Silence");
447         if (!silence) { std::cerr << X_("Could not load silence for export format") << std::endl; return -1; }
448
449         XMLNode const * start = silence->child ("Start");
450         XMLNode const * end = silence->child ("End");
451         if (!start || !end) { std::cerr << X_("Could not load end or start silence for export format") << std::endl; return -1; }
452
453         /* Silence start */
454
455         if ((child = start->child ("Trim"))) {
456                 child->get_property ("enabled", _trim_beginning);
457         }
458
459         bool enabled;
460         if ((child = start->child ("Add"))) {
461                 if (child->get_property ("enabled", enabled) && enabled) {
462                         if ((child = child->child ("Duration"))) {
463                                 _silence_beginning.set_state (*child);
464                         }
465                 } else {
466                         _silence_beginning.type = Time::Timecode;
467                 }
468         }
469
470         /* Silence end */
471
472         if ((child = end->child ("Trim"))) {
473                 child->get_property ("enabled", _trim_end);
474         }
475
476         if ((child = end->child ("Add"))) {
477                 if (child->get_property ("enabled", enabled) && enabled) {
478                         if ((child = child->child ("Duration"))) {
479                                 _silence_end.set_state (*child);
480                         }
481                 } else {
482                                 _silence_end.type = Time::Timecode;
483                 }
484         }
485
486         return 0;
487 }
488
489 bool
490 ExportFormatSpecification::is_compatible_with (ExportFormatCompatibility const & compatibility) const
491 {
492         boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
493
494         if (intersection->formats_empty() && format_id() != 0) {
495                 return false;
496         }
497
498         if (intersection->endiannesses_empty() && endianness() != E_FileDefault) {
499                 return false;
500         }
501
502         if (intersection->sample_rates_empty() && sample_rate() != SR_None) {
503                 return false;
504         }
505
506         if (intersection->sample_formats_empty() && sample_format() != SF_None) {
507                 return false;
508         }
509
510         if (intersection->qualities_empty() && quality() != Q_None) {
511                 return false;
512         }
513
514         return true;
515 }
516
517 bool
518 ExportFormatSpecification::is_complete () const
519 {
520         if (type() == T_None) {
521                 return false;
522         }
523
524         if (!format_id()) {
525                 return false;
526         }
527
528         if (!sample_rate()) {
529                 return false;
530         }
531
532         if (has_sample_format) {
533                 if (sample_format() == SF_None) {
534                         return false;
535                 }
536         }
537
538         return true;
539 }
540
541 void
542 ExportFormatSpecification::set_format (boost::shared_ptr<ExportFormat> format)
543 {
544         if (format) {
545                 FormatId new_fmt = format->get_format_id ();
546                 bool fmt_changed = format_id() != new_fmt;
547                 set_format_id (new_fmt);
548
549                 set_type (format->get_type());
550                 set_extension (format->extension());
551
552                 if (format->get_explicit_sample_format()) {
553                         set_sample_format (format->get_explicit_sample_format());
554                 }
555
556                 if (format->has_sample_format()) {
557                         has_sample_format = true;
558                 }
559
560                 if (format->has_broadcast_info()) {
561                         _has_broadcast_info = true;
562                 }
563
564                 _has_codec_quality = format->has_codec_quality();
565                 if (!_has_codec_quality) {
566                         _codec_quality = 0;
567                 } else if (fmt_changed) {
568                         _codec_quality = boost::dynamic_pointer_cast<HasCodecQuality> (format)->default_codec_quality();
569                 }
570
571                 supports_tagging = format->supports_tagging ();
572                 _channel_limit = format->get_channel_limit();
573
574                 _format_name = format->name();
575         } else {
576                 set_format_id (F_None);
577                 set_type (T_None);
578                 set_extension ("");
579                 _has_broadcast_info = false;
580                 has_sample_format = false;
581                 supports_tagging = false;
582                 _channel_limit = 0;
583                 _codec_quality = 0;
584                 _format_name = "";
585         }
586 }
587
588 string
589 ExportFormatSpecification::description (bool include_name)
590 {
591         list<string> components;
592
593         if (_normalize) {
594                 if (_normalize_loudness) {
595                         components.push_back (_("normalize loudness"));
596                 } else {
597                         components.push_back (_("normalize peak"));
598                 }
599         }
600
601         if (_trim_beginning && _trim_end) {
602                 components.push_back ( _("trim"));
603         } else if (_trim_beginning) {
604                 components.push_back (_("trim start"));
605         } else if (_trim_end) {
606                 components.push_back (_("trim end"));
607         }
608
609         if (_format_name != "") {
610                 components.push_back (_format_name);
611         }
612
613         if (has_sample_format) {
614                 components.push_back (HasSampleFormat::get_sample_format_name (sample_format()));
615         }
616
617         switch (sample_rate()) {
618         case SR_8:
619                 components.push_back ("8 kHz");
620                 break;
621         case SR_22_05:
622                 components.push_back ("22,5 kHz");
623                 break;
624         case SR_44_1:
625                 components.push_back ("44,1 kHz");
626                 break;
627         case SR_48:
628                 components.push_back ("48 kHz");
629                 break;
630         case SR_88_2:
631                 components.push_back ("88,2 kHz");
632                 break;
633         case SR_96:
634                 components.push_back ("96 kHz");
635                 break;
636         case SR_176_4:
637                 components.push_back ("176.4 kHz");
638                 break;
639         case SR_192:
640                 components.push_back ("192 kHz");
641                 break;
642         case SR_Session:
643                 components.push_back (_("Session rate"));
644                 break;
645         case SR_None:
646                 break;
647         }
648
649         if (_with_toc) {
650                 components.push_back ("TOC");
651         }
652
653         if (_with_cue) {
654                 components.push_back ("CUE");
655         }
656
657         if (_with_mp4chaps) {
658                 components.push_back ("MP4ch");
659         }
660
661         if (!_command.empty()) {
662                 components.push_back ("+");
663         }
664
665         string desc;
666         if (include_name) {
667                 desc = _name + ": ";
668         }
669
670         for (list<string>::const_iterator it = components.begin(); it != components.end(); ++it) {
671                 if (it != components.begin()) { desc += ", "; }
672                 desc += *it;
673         }
674         return desc;
675 }
676
677 void
678 ExportFormatSpecification::add_option (XMLNode * node, std::string const & name, std::string const & value)
679 {
680         node = node->add_child ("Option");
681         node->set_property ("name", name);
682         node->set_property ("value", value);
683 }
684
685 std::string
686 ExportFormatSpecification::get_option (XMLNode const * node, std::string const & name)
687 {
688         XMLNodeList list (node->children ("Option"));
689
690         for (XMLNodeList::iterator it = list.begin(); it != list.end(); ++it) {
691                 std::string str;
692                 if ((*it)->get_property ("name", str) && name == str) {
693                         if ((*it)->get_property ("value", str)) {
694                                 return str;
695                         }
696                 }
697         }
698
699         std::cerr << "Could not load encoding option \"" << name << "\" for export format" << std::endl;
700
701         return "";
702 }
703
704 }; // namespace ARDOUR