2 Copyright (C) 2008 Paul Davis
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.
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.
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.
21 #include "ardour/export_format_specification.h"
25 #include "ardour/export_format_compatibility.h"
26 #include "ardour/export_formats.h"
27 #include "ardour/session.h"
29 #include "pbd/error.h"
30 #include "pbd/xml++.h"
31 #include "pbd/enumwriter.h"
32 #include "pbd/convert.h"
43 ExportFormatSpecification::Time &
44 ExportFormatSpecification::Time::operator= (AnyTime const & other)
46 static_cast<AnyTime &>(*this) = other;
51 ExportFormatSpecification::Time::get_frames_at (framepos_t position, framecnt_t target_rate) const
53 framecnt_t duration = session.any_duration_to_frames (position, *this);
54 return ((double) target_rate / session.frame_rate()) * duration + 0.5;
58 ExportFormatSpecification::Time::get_state ()
61 XMLNode * node = new XMLNode ("Duration");
63 node->add_property ("format", enum_2_string (type));
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));
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));
78 node->add_property ("frames", to_string (frames, std::dec));
81 node->add_property ("seconds", to_string (seconds, std::dec));
89 ExportFormatSpecification::Time::set_state (const XMLNode & node)
91 XMLProperty const * prop;
93 prop = node.property ("format");
95 if (!prop) { return -1; }
97 type = (Type) string_2_enum (prop->value(), Type);
101 if ((prop = node.property ("hours"))) {
102 timecode.hours = atoi (prop->value());
105 if ((prop = node.property ("minutes"))) {
106 timecode.minutes = atoi (prop->value());
109 if ((prop = node.property ("seconds"))) {
110 timecode.seconds = atoi (prop->value());
113 if ((prop = node.property ("frames"))) {
114 timecode.frames = atoi (prop->value());
120 if ((prop = node.property ("bars"))) {
121 bbt.bars = atoi (prop->value());
124 if ((prop = node.property ("beats"))) {
125 bbt.beats = atoi (prop->value());
128 if ((prop = node.property ("ticks"))) {
129 bbt.ticks = atoi (prop->value());
135 if ((prop = node.property ("frames"))) {
136 std::istringstream iss (prop->value());
143 if ((prop = node.property ("seconds"))) {
144 seconds = atof (prop->value());
153 ExportFormatSpecification::ExportFormatSpecification (Session & s)
156 , has_sample_format (false)
157 , supports_tagging (false)
158 , _has_broadcast_info (false)
160 , _dither_type (D_None)
161 , _src_quality (SRC_SincBest)
164 , _trim_beginning (false)
165 , _silence_beginning (s)
170 , _normalize_loudness (false)
171 , _normalize_dbfs (GAIN_COEFF_UNITY)
172 , _normalize_lufs (-23)
173 , _normalize_dbtp (-1)
176 , _with_mp4chaps (false)
177 , _soundcloud_upload (false)
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);
188 ExportFormatSpecification::ExportFormatSpecification (Session & s, XMLNode const & state)
191 , has_sample_format (false)
192 , supports_tagging (false)
193 , _has_broadcast_info (false)
195 , _dither_type (D_None)
196 , _src_quality (SRC_SincBest)
199 , _trim_beginning (false)
200 , _silence_beginning (s)
205 , _normalize_loudness (false)
206 , _normalize_dbfs (GAIN_COEFF_UNITY)
207 , _normalize_lufs (-23)
208 , _normalize_dbtp (-1)
211 , _with_mp4chaps (false)
212 , _soundcloud_upload (false)
216 _silence_beginning.type = Time::Timecode;
217 _silence_end.type = Time::Timecode;
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)
231 set_name (other.name() + " (copy)");
233 set_name (other.name());
236 _format_name = other._format_name;
237 has_sample_format = other.has_sample_format;
239 supports_tagging = other.supports_tagging;
240 _has_broadcast_info = other._has_broadcast_info;
241 _channel_limit = other._channel_limit;
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());
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());
260 set_tag (other.tag());
262 set_silence_beginning (other.silence_beginning_time());
263 set_silence_end (other.silence_end_time());
265 set_extension(other.extension());
268 ExportFormatSpecification::~ExportFormatSpecification ()
273 ExportFormatSpecification::get_state ()
277 XMLNode * root = new XMLNode ("ExportFormatSpecification");
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);
286 node = root->add_child ("Encoding");
287 node->add_property ("id", enum_2_string (format_id()));
288 node->add_property ("type", enum_2_string (type()));
289 node->add_property ("extension", extension());
290 node->add_property ("name", _format_name);
291 node->add_property ("has-sample-format", has_sample_format ? "true" : "false");
292 node->add_property ("channel-limit", to_string (_channel_limit, std::dec));
294 node = root->add_child ("SampleRate");
295 node->add_property ("rate", to_string (sample_rate(), std::dec));
297 node = root->add_child ("SRCQuality");
298 node->add_property ("quality", enum_2_string (src_quality()));
300 XMLNode * enc_opts = root->add_child ("EncodingOptions");
302 add_option (enc_opts, "sample-format", enum_2_string (sample_format()));
303 add_option (enc_opts, "dithering", enum_2_string (dither_type()));
304 add_option (enc_opts, "tag-metadata", _tag ? "true" : "false");
305 add_option (enc_opts, "tag-support", supports_tagging ? "true" : "false");
306 add_option (enc_opts, "broadcast-info", _has_broadcast_info ? "true" : "false");
308 XMLNode * processing = root->add_child ("Processing");
310 node = processing->add_child ("Normalize");
311 node->add_property ("enabled", normalize() ? "true" : "false");
312 node->add_property ("loudness", normalize_loudness() ? "yes" : "no");
313 node->add_property ("dbfs", to_string (normalize_dbfs(), std::dec));
314 node->add_property ("lufs", to_string (normalize_lufs(), std::dec));
315 node->add_property ("dbtp", to_string (normalize_dbtp(), std::dec));
317 XMLNode * silence = processing->add_child ("Silence");
318 XMLNode * start = silence->add_child ("Start");
319 XMLNode * end = silence->add_child ("End");
321 node = start->add_child ("Trim");
322 node->add_property ("enabled", trim_beginning() ? "true" : "false");
324 node = start->add_child ("Add");
325 node->add_property ("enabled", _silence_beginning.not_zero() ? "true" : "false");
326 node->add_child_nocopy (_silence_beginning.get_state());
328 node = end->add_child ("Trim");
329 node->add_property ("enabled", trim_end() ? "true" : "false");
331 node = end->add_child ("Add");
332 node->add_property ("enabled", _silence_end.not_zero() ? "true" : "false");
333 node->add_child_nocopy (_silence_end.get_state());
339 ExportFormatSpecification::set_state (const XMLNode & root)
341 XMLProperty const * prop;
342 XMLNode const * child;
346 if ((prop = root.property ("name"))) {
347 _name = prop->value();
350 if ((prop = root.property ("id"))) {
354 if ((prop = root.property ("with-cue"))) {
355 _with_cue = string_is_affirmative (prop->value());
360 if ((prop = root.property ("with-toc"))) {
361 _with_toc = string_is_affirmative (prop->value());
366 if ((prop = root.property ("with-mp4chaps"))) {
367 _with_mp4chaps = string_is_affirmative (prop->value());
369 _with_mp4chaps = false;
372 if ((prop = root.property ("command"))) {
373 _command = prop->value();
378 /* Encoding and SRC */
380 if ((child = root.child ("Encoding"))) {
381 if ((prop = child->property ("id"))) {
382 set_format_id ((FormatId) string_2_enum (prop->value(), FormatId));
385 if ((prop = child->property ("type"))) {
386 set_type ((Type) string_2_enum (prop->value(), Type));
389 if ((prop = child->property ("extension"))) {
390 set_extension (prop->value());
393 if ((prop = child->property ("name"))) {
394 _format_name = prop->value();
397 if ((prop = child->property ("has-sample-format"))) {
398 has_sample_format = string_is_affirmative (prop->value());
401 if ((prop = child->property ("has-sample-format"))) {
402 has_sample_format = string_is_affirmative (prop->value());
405 if ((prop = child->property ("channel-limit"))) {
406 _channel_limit = atoi (prop->value());
410 if ((child = root.child ("SampleRate"))) {
411 if ((prop = child->property ("rate"))) {
412 set_sample_rate ( (SampleRate) string_2_enum (prop->value(), SampleRate));
416 if ((child = root.child ("SRCQuality"))) {
417 if ((prop = child->property ("quality"))) {
418 _src_quality = (SRCQuality) string_2_enum (prop->value(), SRCQuality);
422 /* Encoding options */
424 if ((child = root.child ("EncodingOptions"))) {
425 set_sample_format ((SampleFormat) string_2_enum (get_option (child, "sample-format"), SampleFormat));
426 set_dither_type ((DitherType) string_2_enum (get_option (child, "dithering"), DitherType));
427 set_tag (!(get_option (child, "tag-metadata").compare ("true")));
428 supports_tagging = (!(get_option (child, "tag-support").compare ("true")));
429 _has_broadcast_info = (!(get_option (child, "broadcast-info").compare ("true")));
434 XMLNode const * proc = root.child ("Processing");
435 if (!proc) { std::cerr << X_("Could not load processing for export format") << std::endl; return -1; }
437 if ((child = proc->child ("Normalize"))) {
438 if ((prop = child->property ("enabled"))) {
439 _normalize = (!prop->value().compare ("true"));
442 // old formats before ~ 4.7-930ish
443 if ((prop = child->property ("target"))) {
444 _normalize_dbfs = atof (prop->value());
447 if ((prop = child->property ("loudness"))) {
448 _normalize_loudness = string_is_affirmative (prop->value());
451 if ((prop = child->property ("dbfs"))) {
452 _normalize_dbfs = atof (prop->value());
455 if ((prop = child->property ("lufs"))) {
456 _normalize_lufs = atof (prop->value());
459 if ((prop = child->property ("dbtp"))) {
460 _normalize_dbtp = atof (prop->value());
464 XMLNode const * silence = proc->child ("Silence");
465 if (!silence) { std::cerr << X_("Could not load silence for export format") << std::endl; return -1; }
467 XMLNode const * start = silence->child ("Start");
468 XMLNode const * end = silence->child ("End");
469 if (!start || !end) { std::cerr << X_("Could not load end or start silence for export format") << std::endl; return -1; }
473 if ((child = start->child ("Trim"))) {
474 if ((prop = child->property ("enabled"))) {
475 _trim_beginning = (!prop->value().compare ("true"));
479 if ((child = start->child ("Add"))) {
480 if ((prop = child->property ("enabled"))) {
481 if (!prop->value().compare ("true")) {
482 if ((child = child->child ("Duration"))) {
483 _silence_beginning.set_state (*child);
486 _silence_beginning.type = Time::Timecode;
493 if ((child = end->child ("Trim"))) {
494 if ((prop = child->property ("enabled"))) {
495 _trim_end = (!prop->value().compare ("true"));
499 if ((child = end->child ("Add"))) {
500 if ((prop = child->property ("enabled"))) {
501 if (!prop->value().compare ("true")) {
502 if ((child = child->child ("Duration"))) {
503 _silence_end.set_state (*child);
506 _silence_end.type = Time::Timecode;
515 ExportFormatSpecification::is_compatible_with (ExportFormatCompatibility const & compatibility) const
517 boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
519 if (intersection->formats_empty() && format_id() != 0) {
523 if (intersection->endiannesses_empty() && endianness() != E_FileDefault) {
527 if (intersection->sample_rates_empty() && sample_rate() != SR_None) {
531 if (intersection->sample_formats_empty() && sample_format() != SF_None) {
535 if (intersection->qualities_empty() && quality() != Q_None) {
543 ExportFormatSpecification::is_complete () const
545 if (type() == T_None) {
553 if (!sample_rate()) {
557 if (has_sample_format) {
558 if (sample_format() == SF_None) {
567 ExportFormatSpecification::set_format (boost::shared_ptr<ExportFormat> format)
570 set_format_id (format->get_format_id ());
571 set_type (format->get_type());
572 set_extension (format->extension());
574 if (format->get_explicit_sample_format()) {
575 set_sample_format (format->get_explicit_sample_format());
578 if (format->has_sample_format()) {
579 has_sample_format = true;
582 if (format->has_broadcast_info()) {
583 _has_broadcast_info = true;
586 supports_tagging = format->supports_tagging ();
587 _channel_limit = format->get_channel_limit();
589 _format_name = format->name();
591 set_format_id (F_None);
594 _has_broadcast_info = false;
595 has_sample_format = false;
596 supports_tagging = false;
603 ExportFormatSpecification::description (bool include_name)
605 list<string> components;
608 if (_normalize_loudness) {
609 components.push_back (_("normalize loudness"));
611 components.push_back (_("normalize peak"));
615 if (_trim_beginning && _trim_end) {
616 components.push_back ( _("trim"));
617 } else if (_trim_beginning) {
618 components.push_back (_("trim start"));
619 } else if (_trim_end) {
620 components.push_back (_("trim end"));
623 if (_format_name != "") {
624 components.push_back (_format_name);
627 if (has_sample_format) {
628 components.push_back (HasSampleFormat::get_sample_format_name (sample_format()));
631 switch (sample_rate()) {
633 components.push_back ("8 kHz");
636 components.push_back ("22,5 kHz");
639 components.push_back ("44,1 kHz");
642 components.push_back ("48 kHz");
645 components.push_back ("88,2 kHz");
648 components.push_back ("96 kHz");
651 components.push_back ("176.4 kHz");
654 components.push_back ("192 kHz");
657 components.push_back (_("Session rate"));
664 components.push_back ("TOC");
668 components.push_back ("CUE");
671 if (_with_mp4chaps) {
672 components.push_back ("MP4ch");
675 if (!_command.empty()) {
676 components.push_back ("+");
684 for (list<string>::const_iterator it = components.begin(); it != components.end(); ++it) {
685 if (it != components.begin()) { desc += ", "; }
692 ExportFormatSpecification::add_option (XMLNode * node, std::string const & name, std::string const & value)
694 node = node->add_child ("Option");
695 node->add_property ("name", name);
696 node->add_property ("value", value);
700 ExportFormatSpecification::get_option (XMLNode const * node, std::string const & name)
702 XMLNodeList list (node->children ("Option"));
704 for (XMLNodeList::iterator it = list.begin(); it != list.end(); ++it) {
705 XMLProperty const * prop = (*it)->property ("name");
706 if (prop && !name.compare (prop->value())) {
707 prop = (*it)->property ("value");
709 return prop->value();
714 std::cerr << "Could not load encoding option \"" << name << "\" for export format" << std::endl;
719 }; // namespace ARDOUR