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"
23 #include "ardour/export_format_compatibility.h"
24 #include "ardour/export_formats.h"
25 #include "ardour/session.h"
26 #include "ardour/types_convert.h"
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"
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)
53 ExportFormatSpecification::Time &
54 ExportFormatSpecification::Time::operator= (AnyTime const & other)
56 static_cast<AnyTime &>(*this) = other;
61 ExportFormatSpecification::Time::get_samples_at (samplepos_t position, samplecnt_t target_rate) const
63 samplecnt_t duration = session.any_duration_to_samples (position, *this);
64 return ((double) target_rate / session.sample_rate()) * duration + 0.5;
68 ExportFormatSpecification::Time::get_state ()
71 XMLNode * node = new XMLNode ("Duration");
73 node->set_property ("format", type);
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);
83 node->set_property ("bars", bbt.bars);
84 node->set_property ("beats", bbt.beats);
85 node->set_property ("ticks", bbt.ticks);
88 node->set_property ("samples", samples);
91 node->set_property ("seconds", seconds);
99 ExportFormatSpecification::Time::set_state (const XMLNode & node)
101 if (!node.get_property ("format", type)) {
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);
114 node.get_property ("bars", bbt.bars);
115 node.get_property ("beats", bbt.beats);
116 node.get_property ("ticks", bbt.ticks);
120 node.get_property ("samples", samples);
124 node.get_property ("seconds", seconds);
132 ExportFormatSpecification::ExportFormatSpecification (Session & s)
134 , has_sample_format (false)
135 , supports_tagging (false)
136 , _has_codec_quality (false)
137 , _has_broadcast_info (false)
139 , _dither_type (D_None)
140 , _src_quality (SRC_SincBest)
143 , _trim_beginning (false)
144 , _silence_beginning (s)
149 , _normalize_loudness (false)
150 , _normalize_dbfs (GAIN_COEFF_UNITY)
151 , _normalize_lufs (-23)
152 , _normalize_dbtp (-1)
155 , _with_mp4chaps (false)
156 , _soundcloud_upload (false)
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);
168 ExportFormatSpecification::ExportFormatSpecification (Session & s, XMLNode const & state)
170 , has_sample_format (false)
171 , supports_tagging (false)
172 , _has_codec_quality (false)
173 , _has_broadcast_info (false)
175 , _dither_type (D_None)
176 , _src_quality (SRC_SincBest)
179 , _trim_beginning (false)
180 , _silence_beginning (s)
185 , _normalize_loudness (false)
186 , _normalize_dbfs (GAIN_COEFF_UNITY)
187 , _normalize_lufs (-23)
188 , _normalize_dbtp (-1)
191 , _with_mp4chaps (false)
192 , _soundcloud_upload (false)
197 _silence_beginning.type = Time::Timecode;
198 _silence_end.type = Time::Timecode;
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 , _soundcloud_upload (false)
209 , _analyse (other._analyse)
210 , _codec_quality (other._codec_quality)
213 set_name (other.name() + " (copy)");
215 set_name (other.name());
218 _format_name = other._format_name;
219 has_sample_format = other.has_sample_format;
220 supports_tagging = other.supports_tagging;
221 _has_codec_quality = other._has_codec_quality;
222 _has_broadcast_info = other._has_broadcast_info;
223 _channel_limit = other._channel_limit;
225 set_type (other.type());
226 set_format_id (other.format_id());
227 set_endianness (other.endianness());
228 set_sample_format (other.sample_format());
229 set_sample_rate (other.sample_rate());
230 set_quality (other.quality());
232 set_dither_type (other.dither_type());
233 set_src_quality (other.src_quality());
234 set_trim_beginning (other.trim_beginning());
235 set_trim_end (other.trim_end());
236 set_normalize (other.normalize());
237 set_normalize_loudness (other.normalize_loudness());
238 set_normalize_dbfs (other.normalize_dbfs());
239 set_normalize_lufs (other.normalize_lufs());
240 set_normalize_dbtp (other.normalize_dbtp());
242 set_tag (other.tag());
244 set_silence_beginning (other.silence_beginning_time());
245 set_silence_end (other.silence_end_time());
247 set_extension(other.extension());
250 ExportFormatSpecification::~ExportFormatSpecification ()
255 ExportFormatSpecification::get_state ()
258 XMLNode * root = new XMLNode ("ExportFormatSpecification");
260 root->set_property ("name", _name);
261 root->set_property ("id", _id.to_s());
262 root->set_property ("with-cue", _with_cue);
263 root->set_property ("with-toc", _with_toc);
264 root->set_property ("with-mp4chaps", _with_mp4chaps);
265 root->set_property ("command", _command);
266 root->set_property ("analyse", _analyse);
267 root->set_property ("soundcloud-upload", _soundcloud_upload);
269 node = root->add_child ("Encoding");
270 node->set_property ("id", format_id());
271 node->set_property ("type", type());
272 node->set_property ("extension", extension());
273 node->set_property ("name", _format_name);
274 node->set_property ("has-sample-format", has_sample_format);
275 node->set_property ("channel-limit", _channel_limit);
277 node = root->add_child ("SampleRate");
278 node->set_property ("rate", sample_rate());
280 node = root->add_child ("SRCQuality");
281 node->set_property ("quality", src_quality());
283 if (_has_codec_quality) {
284 node = root->add_child ("CodecQuality");
285 node->set_property ("quality", codec_quality());
288 XMLNode * enc_opts = root->add_child ("EncodingOptions");
290 add_option (enc_opts, "sample-format", to_string(sample_format()));
291 add_option (enc_opts, "dithering", to_string (dither_type()));
292 add_option (enc_opts, "tag-metadata", to_string (_tag));
293 add_option (enc_opts, "tag-support", to_string (supports_tagging));
294 add_option (enc_opts, "broadcast-info", to_string (_has_broadcast_info));
296 XMLNode * processing = root->add_child ("Processing");
298 node = processing->add_child ("Normalize");
299 node->set_property ("enabled", normalize());
300 node->set_property ("loudness", normalize_loudness());
301 node->set_property ("dbfs", normalize_dbfs());
302 node->set_property ("lufs", normalize_lufs());
303 node->set_property ("dbtp", normalize_dbtp());
305 XMLNode * silence = processing->add_child ("Silence");
306 XMLNode * start = silence->add_child ("Start");
307 XMLNode * end = silence->add_child ("End");
309 node = start->add_child ("Trim");
310 node->set_property ("enabled", trim_beginning());
312 node = start->add_child ("Add");
313 node->set_property ("enabled", _silence_beginning.not_zero());
314 node->add_child_nocopy (_silence_beginning.get_state());
316 node = end->add_child ("Trim");
317 node->set_property ("enabled", trim_end());
319 node = end->add_child ("Add");
320 node->set_property ("enabled", _silence_end.not_zero());
321 node->add_child_nocopy (_silence_end.get_state());
327 ExportFormatSpecification::set_state (const XMLNode & root)
329 XMLNode const * child;
332 root.get_property ("name", _name);
334 if (root.get_property ("id", str)) {
338 if (!root.get_property ("with-cue", _with_cue)) {
342 if (!root.get_property ("with-toc", _with_toc)) {
346 if (!root.get_property ("with-mp4chaps", _with_mp4chaps)) {
347 _with_mp4chaps = false;
350 if (!root.get_property ("command", _command)) {
354 if (!root.get_property ("analyse", _analyse)) {
358 if (!root.get_property ("soundcloud-upload", _soundcloud_upload)) {
359 _soundcloud_upload = false;
362 /* Encoding and SRC */
364 if ((child = root.child ("Encoding"))) {
366 if (child->get_property ("id", fid)) {
370 ExportFormatBase::Type type;
371 if (child->get_property ("type", type)) {
375 if (child->get_property ("extension", str)) {
379 child->get_property ("name", _format_name);
380 child->get_property ("has-sample-format", has_sample_format);
381 child->get_property ("channel-limit", _channel_limit);
384 if ((child = root.child ("SampleRate"))) {
386 if (child->get_property ("rate", rate)) {
387 set_sample_rate (rate);
391 if ((child = root.child ("SRCQuality"))) {
392 child->get_property ("quality", _src_quality);
395 if ((child = root.child ("CodecQuality"))) {
396 child->get_property ("quality", _codec_quality);
397 _has_codec_quality = true;
399 _has_codec_quality = false;
402 /* fixup codec quality for old states */
403 if (!_has_codec_quality) {
404 /* We'd need an instance of ExportFormatManager to look up
405 * defaults for a given type -- in the future there may even be
406 * difference qualities depending on sub-type, so we just
407 * hardcode them here for the time being.
409 if (format_id() == F_FFMPEG) {
410 _codec_quality = -2; // ExportFormatOggVorbis::default_codec_quality();
412 else if (format_id() == F_Ogg) {
413 _codec_quality = 40; // ExportFormatFFMPEG::default_codec_quality();
417 /* Encoding options */
419 if ((child = root.child ("EncodingOptions"))) {
420 set_sample_format ((SampleFormat) string_2_enum (get_option (child, "sample-format"), SampleFormat));
421 set_dither_type ((DitherType) string_2_enum (get_option (child, "dithering"), DitherType));
422 set_tag (string_to<bool>(get_option (child, "tag-metadata")));
423 supports_tagging = string_to<bool>(get_option (child, "tag-support"));
424 _has_broadcast_info = string_to<bool>(get_option (child, "broadcast-info"));
429 XMLNode const * proc = root.child ("Processing");
430 if (!proc) { std::cerr << X_("Could not load processing for export format") << std::endl; return -1; }
432 if ((child = proc->child ("Normalize"))) {
433 child->get_property ("enabled", _normalize);
434 // old formats before ~ 4.7-930ish
435 child->get_property ("target", _normalize_dbfs);
436 child->get_property ("loudness", _normalize_loudness);
437 child->get_property ("dbfs", _normalize_dbfs);
438 child->get_property ("lufs", _normalize_lufs);
439 child->get_property ("dbtp", _normalize_dbtp);
442 XMLNode const * silence = proc->child ("Silence");
443 if (!silence) { std::cerr << X_("Could not load silence for export format") << std::endl; return -1; }
445 XMLNode const * start = silence->child ("Start");
446 XMLNode const * end = silence->child ("End");
447 if (!start || !end) { std::cerr << X_("Could not load end or start silence for export format") << std::endl; return -1; }
451 if ((child = start->child ("Trim"))) {
452 child->get_property ("enabled", _trim_beginning);
456 if ((child = start->child ("Add"))) {
457 if (child->get_property ("enabled", enabled) && enabled) {
458 if ((child = child->child ("Duration"))) {
459 _silence_beginning.set_state (*child);
462 _silence_beginning.type = Time::Timecode;
468 if ((child = end->child ("Trim"))) {
469 child->get_property ("enabled", _trim_end);
472 if ((child = end->child ("Add"))) {
473 if (child->get_property ("enabled", enabled) && enabled) {
474 if ((child = child->child ("Duration"))) {
475 _silence_end.set_state (*child);
478 _silence_end.type = Time::Timecode;
486 ExportFormatSpecification::is_compatible_with (ExportFormatCompatibility const & compatibility) const
488 boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
490 if (intersection->formats_empty() && format_id() != 0) {
494 if (intersection->endiannesses_empty() && endianness() != E_FileDefault) {
498 if (intersection->sample_rates_empty() && sample_rate() != SR_None) {
502 if (intersection->sample_formats_empty() && sample_format() != SF_None) {
506 if (intersection->qualities_empty() && quality() != Q_None) {
514 ExportFormatSpecification::is_complete () const
516 if (type() == T_None) {
524 if (!sample_rate()) {
528 if (has_sample_format) {
529 if (sample_format() == SF_None) {
538 ExportFormatSpecification::set_format (boost::shared_ptr<ExportFormat> format)
541 FormatId new_fmt = format->get_format_id ();
542 bool fmt_changed = format_id() != new_fmt;
543 set_format_id (new_fmt);
545 set_type (format->get_type());
546 set_extension (format->extension());
548 if (format->get_explicit_sample_format()) {
549 set_sample_format (format->get_explicit_sample_format());
552 if (format->has_sample_format()) {
553 has_sample_format = true;
556 if (format->has_broadcast_info()) {
557 _has_broadcast_info = true;
560 _has_codec_quality = format->has_codec_quality();
561 if (!_has_codec_quality) {
563 } else if (fmt_changed) {
564 _codec_quality = boost::dynamic_pointer_cast<HasCodecQuality> (format)->default_codec_quality();
567 supports_tagging = format->supports_tagging ();
568 _channel_limit = format->get_channel_limit();
570 _format_name = format->name();
572 set_format_id (F_None);
575 _has_broadcast_info = false;
576 has_sample_format = false;
577 supports_tagging = false;
585 ExportFormatSpecification::description (bool include_name)
587 list<string> components;
590 if (_normalize_loudness) {
591 components.push_back (_("normalize loudness"));
593 components.push_back (_("normalize peak"));
597 if (_trim_beginning && _trim_end) {
598 components.push_back ( _("trim"));
599 } else if (_trim_beginning) {
600 components.push_back (_("trim start"));
601 } else if (_trim_end) {
602 components.push_back (_("trim end"));
605 if (_format_name != "") {
606 components.push_back (_format_name);
609 if (has_sample_format) {
610 components.push_back (HasSampleFormat::get_sample_format_name (sample_format()));
613 switch (sample_rate()) {
615 components.push_back ("8 kHz");
618 components.push_back ("22,5 kHz");
621 components.push_back ("44,1 kHz");
624 components.push_back ("48 kHz");
627 components.push_back ("88,2 kHz");
630 components.push_back ("96 kHz");
633 components.push_back ("176.4 kHz");
636 components.push_back ("192 kHz");
639 components.push_back (_("Session rate"));
646 components.push_back ("TOC");
650 components.push_back ("CUE");
653 if (_with_mp4chaps) {
654 components.push_back ("MP4ch");
657 if (!_command.empty()) {
658 components.push_back ("+");
666 for (list<string>::const_iterator it = components.begin(); it != components.end(); ++it) {
667 if (it != components.begin()) { desc += ", "; }
674 ExportFormatSpecification::add_option (XMLNode * node, std::string const & name, std::string const & value)
676 node = node->add_child ("Option");
677 node->set_property ("name", name);
678 node->set_property ("value", value);
682 ExportFormatSpecification::get_option (XMLNode const * node, std::string const & name)
684 XMLNodeList list (node->children ("Option"));
686 for (XMLNodeList::iterator it = list.begin(); it != list.end(); ++it) {
688 if ((*it)->get_property ("name", str) && name == str) {
689 if ((*it)->get_property ("value", str)) {
695 std::cerr << "Could not load encoding option \"" << name << "\" for export format" << std::endl;
700 }; // namespace ARDOUR