allow to customize variable i/o plugin inputs
[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 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_target (GAIN_COEFF_UNITY)
171         , _with_toc (false)
172         , _with_cue (false)
173         , _with_mp4chaps (false)
174         , _soundcloud_upload (false)
175         , _command ("")
176         , _analyse (true)
177 {
178         format_ids.insert (F_None);
179         endiannesses.insert (E_FileDefault);
180         sample_formats.insert (SF_None);
181         sample_rates.insert (SR_None);
182         qualities.insert (Q_None);
183 }
184
185 ExportFormatSpecification::ExportFormatSpecification (Session & s, XMLNode const & state)
186         : session (s)
187         , _silence_beginning (s)
188         , _silence_end (s)
189         , _soundcloud_upload (false)
190         , _analyse (true)
191 {
192         _silence_beginning.type = Time::Timecode;
193         _silence_end.type = Time::Timecode;
194
195         set_state (state);
196 }
197
198 ExportFormatSpecification::ExportFormatSpecification (ExportFormatSpecification const & other, bool modify_name)
199         : ExportFormatBase(other)
200         , session (other.session)
201         , _silence_beginning (other.session)
202         , _silence_end (other.session)
203         , _soundcloud_upload (false)
204         , _analyse (other._analyse)
205 {
206         if (modify_name) {
207                 set_name (other.name() + " (copy)");
208         } else {
209                 set_name (other.name());
210         }
211
212         _format_name = other._format_name;
213         has_sample_format = other.has_sample_format;
214
215         supports_tagging = other.supports_tagging;
216         _has_broadcast_info = other._has_broadcast_info;
217         _channel_limit = other._channel_limit;
218
219         set_type (other.type());
220         set_format_id (other.format_id());
221         set_endianness (other.endianness());
222         set_sample_format (other.sample_format());
223         set_sample_rate (other.sample_rate());
224         set_quality (other.quality());
225
226         set_dither_type (other.dither_type());
227         set_src_quality (other.src_quality());
228         set_trim_beginning (other.trim_beginning());
229         set_trim_end (other.trim_end());
230         set_normalize (other.normalize());
231         set_normalize_target (other.normalize_target());
232
233         set_tag (other.tag());
234
235         set_silence_beginning (other.silence_beginning_time());
236         set_silence_end (other.silence_end_time());
237
238         set_extension(other.extension());
239 }
240
241 ExportFormatSpecification::~ExportFormatSpecification ()
242 {
243 }
244
245 XMLNode &
246 ExportFormatSpecification::get_state ()
247 {
248         XMLNode * node;
249         XMLNode * root = new XMLNode ("ExportFormatSpecification");
250
251         root->add_property ("name", _name);
252         root->add_property ("id", _id.to_s());
253         root->add_property ("with-cue", _with_cue ? "true" : "false");
254         root->add_property ("with-toc", _with_toc ? "true" : "false");
255         root->add_property ("with-mp4chaps", _with_mp4chaps ? "true" : "false");
256         root->add_property ("command", _command);
257
258         node = root->add_child ("Encoding");
259         node->add_property ("id", enum_2_string (format_id()));
260         node->add_property ("type", enum_2_string (type()));
261         node->add_property ("extension", extension());
262         node->add_property ("name", _format_name);
263         node->add_property ("has-sample-format", has_sample_format ? "true" : "false");
264         node->add_property ("channel-limit", to_string (_channel_limit, std::dec));
265
266         node = root->add_child ("SampleRate");
267         node->add_property ("rate", to_string (sample_rate(), std::dec));
268
269         node = root->add_child ("SRCQuality");
270         node->add_property ("quality", enum_2_string (src_quality()));
271
272         XMLNode * enc_opts = root->add_child ("EncodingOptions");
273
274         add_option (enc_opts, "sample-format", enum_2_string (sample_format()));
275         add_option (enc_opts, "dithering", enum_2_string (dither_type()));
276         add_option (enc_opts, "tag-metadata", _tag ? "true" : "false");
277         add_option (enc_opts, "tag-support", supports_tagging ? "true" : "false");
278         add_option (enc_opts, "broadcast-info", _has_broadcast_info ? "true" : "false");
279
280         XMLNode * processing = root->add_child ("Processing");
281
282         node = processing->add_child ("Normalize");
283         node->add_property ("enabled", normalize() ? "true" : "false");
284         node->add_property ("target", to_string (normalize_target(), std::dec));
285
286         XMLNode * silence = processing->add_child ("Silence");
287         XMLNode * start = silence->add_child ("Start");
288         XMLNode * end = silence->add_child ("End");
289
290         node = start->add_child ("Trim");
291         node->add_property ("enabled", trim_beginning() ? "true" : "false");
292
293         node = start->add_child ("Add");
294         node->add_property ("enabled", _silence_beginning.not_zero() ? "true" : "false");
295         node->add_child_nocopy (_silence_beginning.get_state());
296
297         node = end->add_child ("Trim");
298         node->add_property ("enabled", trim_end() ? "true" : "false");
299
300         node = end->add_child ("Add");
301         node->add_property ("enabled", _silence_end.not_zero() ? "true" : "false");
302         node->add_child_nocopy (_silence_end.get_state());
303
304         return *root;
305 }
306
307 int
308 ExportFormatSpecification::set_state (const XMLNode & root)
309 {
310         XMLProperty const * prop;
311         XMLNode const * child;
312         string value;
313
314         if ((prop = root.property ("name"))) {
315                 _name = prop->value();
316         }
317
318         if ((prop = root.property ("id"))) {
319                 _id = prop->value();
320         }
321
322         if ((prop = root.property ("with-cue"))) {
323                 _with_cue = string_is_affirmative (prop->value());
324         } else {
325                 _with_cue = false;
326         }
327
328         if ((prop = root.property ("with-toc"))) {
329                 _with_toc = string_is_affirmative (prop->value());
330         } else {
331                 _with_toc = false;
332         }
333
334         if ((prop = root.property ("with-mp4chaps"))) {
335                 _with_mp4chaps = string_is_affirmative (prop->value());
336         } else {
337                 _with_mp4chaps = false;
338         }
339
340         if ((prop = root.property ("command"))) {
341                 _command = prop->value();
342         } else {
343                 _command = "";
344         }
345
346         /* Encoding and SRC */
347
348         if ((child = root.child ("Encoding"))) {
349                 if ((prop = child->property ("id"))) {
350                         set_format_id ((FormatId) string_2_enum (prop->value(), FormatId));
351                 }
352
353                 if ((prop = child->property ("type"))) {
354                         set_type ((Type) string_2_enum (prop->value(), Type));
355                 }
356
357                 if ((prop = child->property ("extension"))) {
358                         set_extension (prop->value());
359                 }
360
361                 if ((prop = child->property ("name"))) {
362                         _format_name = prop->value();
363                 }
364
365                 if ((prop = child->property ("has-sample-format"))) {
366                         has_sample_format = string_is_affirmative (prop->value());
367                 }
368
369                 if ((prop = child->property ("has-sample-format"))) {
370                         has_sample_format = string_is_affirmative (prop->value());
371                 }
372
373                 if ((prop = child->property ("channel-limit"))) {
374                         _channel_limit = atoi (prop->value());
375                 }
376         }
377
378         if ((child = root.child ("SampleRate"))) {
379                 if ((prop = child->property ("rate"))) {
380                         set_sample_rate ( (SampleRate) string_2_enum (prop->value(), SampleRate));
381                 }
382         }
383
384         if ((child = root.child ("SRCQuality"))) {
385                 if ((prop = child->property ("quality"))) {
386                         _src_quality = (SRCQuality) string_2_enum (prop->value(), SRCQuality);
387                 }
388         }
389
390         /* Encoding options */
391
392         if ((child = root.child ("EncodingOptions"))) {
393                 set_sample_format ((SampleFormat) string_2_enum (get_option (child, "sample-format"), SampleFormat));
394                 set_dither_type ((DitherType) string_2_enum (get_option (child, "dithering"), DitherType));
395                 set_tag (!(get_option (child, "tag-metadata").compare ("true")));
396                 supports_tagging = (!(get_option (child, "tag-support").compare ("true")));
397                 _has_broadcast_info = (!(get_option (child, "broadcast-info").compare ("true")));
398         }
399
400         /* Processing */
401
402         XMLNode const * proc = root.child ("Processing");
403         if (!proc) { std::cerr << X_("Could not load processing for export format") << std::endl; return -1; }
404
405         if ((child = proc->child ("Normalize"))) {
406                 if ((prop = child->property ("enabled"))) {
407                         _normalize = (!prop->value().compare ("true"));
408                 }
409
410                 if ((prop = child->property ("target"))) {
411                         _normalize_target = atof (prop->value());
412                 }
413         }
414
415         XMLNode const * silence = proc->child ("Silence");
416         if (!silence) { std::cerr << X_("Could not load silence for export format") << std::endl; return -1; }
417
418         XMLNode const * start = silence->child ("Start");
419         XMLNode const * end = silence->child ("End");
420         if (!start || !end) { std::cerr << X_("Could not load end or start silence for export format") << std::endl; return -1; }
421
422         /* Silence start */
423
424         if ((child = start->child ("Trim"))) {
425                 if ((prop = child->property ("enabled"))) {
426                         _trim_beginning = (!prop->value().compare ("true"));
427                 }
428         }
429
430         if ((child = start->child ("Add"))) {
431                 if ((prop = child->property ("enabled"))) {
432                         if (!prop->value().compare ("true")) {
433                                 if ((child = child->child ("Duration"))) {
434                                         _silence_beginning.set_state (*child);
435                                 }
436                         } else {
437                                 _silence_beginning.type = Time::Timecode;
438                         }
439                 }
440         }
441
442         /* Silence end */
443
444         if ((child = end->child ("Trim"))) {
445                 if ((prop = child->property ("enabled"))) {
446                         _trim_end = (!prop->value().compare ("true"));
447                 }
448         }
449
450         if ((child = end->child ("Add"))) {
451                 if ((prop = child->property ("enabled"))) {
452                         if (!prop->value().compare ("true")) {
453                                 if ((child = child->child ("Duration"))) {
454                                         _silence_end.set_state (*child);
455                                 }
456                         } else {
457                                 _silence_end.type = Time::Timecode;
458                         }
459                 }
460         }
461
462         return 0;
463 }
464
465 bool
466 ExportFormatSpecification::is_compatible_with (ExportFormatCompatibility const & compatibility) const
467 {
468         boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
469
470         if (intersection->formats_empty() && format_id() != 0) {
471                 return false;
472         }
473
474         if (intersection->endiannesses_empty() && endianness() != E_FileDefault) {
475                 return false;
476         }
477
478         if (intersection->sample_rates_empty() && sample_rate() != SR_None) {
479                 return false;
480         }
481
482         if (intersection->sample_formats_empty() && sample_format() != SF_None) {
483                 return false;
484         }
485
486         if (intersection->qualities_empty() && quality() != Q_None) {
487                 return false;
488         }
489
490         return true;
491 }
492
493 bool
494 ExportFormatSpecification::is_complete () const
495 {
496         if (type() == T_None) {
497                 return false;
498         }
499
500         if (!format_id()) {
501                 return false;
502         }
503
504         if (!sample_rate()) {
505                 return false;
506         }
507
508         if (has_sample_format) {
509                 if (sample_format() == SF_None) {
510                         return false;
511                 }
512         }
513
514         return true;
515 }
516
517 void
518 ExportFormatSpecification::set_format (boost::shared_ptr<ExportFormat> format)
519 {
520         if (format) {
521                 set_format_id (format->get_format_id ());
522                 set_type (format->get_type());
523                 set_extension (format->extension());
524
525                 if (format->get_explicit_sample_format()) {
526                         set_sample_format (format->get_explicit_sample_format());
527                 }
528
529                 if (format->has_sample_format()) {
530                         has_sample_format = true;
531                 }
532
533                 if (format->has_broadcast_info()) {
534                         _has_broadcast_info = true;
535                 }
536
537                 supports_tagging = format->supports_tagging ();
538                 _channel_limit = format->get_channel_limit();
539
540                 _format_name = format->name();
541         } else {
542                 set_format_id (F_None);
543                 set_type (T_None);
544                 set_extension ("");
545                 _has_broadcast_info = false;
546                 has_sample_format = false;
547                 supports_tagging = false;
548                 _channel_limit = 0;
549                 _format_name = "";
550         }
551 }
552
553 string
554 ExportFormatSpecification::description (bool include_name)
555 {
556         list<string> components;
557
558         if (_normalize) {
559                 components.push_back (_("normalize"));
560         }
561
562         if (_trim_beginning && _trim_end) {
563                 components.push_back ( _("trim"));
564         } else if (_trim_beginning) {
565                 components.push_back (_("trim start"));
566         } else if (_trim_end) {
567                 components.push_back (_("trim end"));
568         }
569
570         if (_format_name != "") {
571                 components.push_back (_format_name);
572         }
573
574         if (has_sample_format) {
575                 components.push_back (HasSampleFormat::get_sample_format_name (sample_format()));
576         }
577
578         switch (sample_rate()) {
579         case SR_8:
580                 components.push_back ("8 kHz");
581                 break;
582         case SR_22_05:
583                 components.push_back ("22,5 kHz");
584                 break;
585         case SR_44_1:
586                 components.push_back ("44,1 kHz");
587                 break;
588         case SR_48:
589                 components.push_back ("48 kHz");
590                 break;
591         case SR_88_2:
592                 components.push_back ("88,2 kHz");
593                 break;
594         case SR_96:
595                 components.push_back ("96 kHz");
596                 break;
597         case SR_176_4:
598                 components.push_back ("176.4 kHz");
599                 break;
600         case SR_192:
601                 components.push_back ("192 kHz");
602                 break;
603         case SR_Session:
604                 components.push_back (_("Session rate"));
605                 break;
606         case SR_None:
607                 break;
608         }
609
610         if (_with_toc) {
611                 components.push_back ("TOC");
612         }
613
614         if (_with_cue) {
615                 components.push_back ("CUE");
616         }
617
618         if (_with_mp4chaps) {
619                 components.push_back ("MP4ch");
620         }
621
622         if (!_command.empty()) {
623                 components.push_back ("+");
624         }
625
626         string desc;
627         if (include_name) {
628                 desc = _name + ": ";
629         }
630
631         for (list<string>::const_iterator it = components.begin(); it != components.end(); ++it) {
632                 if (it != components.begin()) { desc += ", "; }
633                 desc += *it;
634         }
635         return desc;
636 }
637
638 void
639 ExportFormatSpecification::add_option (XMLNode * node, std::string const & name, std::string const & value)
640 {
641         node = node->add_child ("Option");
642         node->add_property ("name", name);
643         node->add_property ("value", value);
644 }
645
646 std::string
647 ExportFormatSpecification::get_option (XMLNode const * node, std::string const & name)
648 {
649         XMLNodeList list (node->children ("Option"));
650
651         for (XMLNodeList::iterator it = list.begin(); it != list.end(); ++it) {
652                 XMLProperty * prop = (*it)->property ("name");
653                 if (prop && !name.compare (prop->value())) {
654                         prop = (*it)->property ("value");
655                         if (prop) {
656                                 return prop->value();
657                         }
658                 }
659         }
660
661         std::cerr << "Could not load encoding option \"" << name << "\" for export format" << std::endl;
662
663         return "";
664 }
665
666 }; // namespace ARDOUR