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