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