2 * Copyright (C) 2008-2013 Sakari Bergen <sakari.bergen@beatwaves.net>
3 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2009-2011 David Robillard <d@drobilla.net>
5 * Copyright (C) 2013-2014 Colin Fletcher <colin.m.fletcher@googlemail.com>
6 * Copyright (C) 2016-2018 Robin Gareus <robin@gareus.org>
7 * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "ardour/export_format_specification.h"
26 #include "ardour/export_format_compatibility.h"
27 #include "ardour/export_formats.h"
28 #include "ardour/session.h"
29 #include "ardour/types_convert.h"
31 #include "pbd/error.h"
32 #include "pbd/xml++.h"
33 #include "pbd/enumwriter.h"
34 #include "pbd/enum_convert.h"
35 #include "pbd/string_convert.h"
36 #include "pbd/types_convert.h"
41 DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::FormatId)
42 DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::SampleRate)
43 DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::SampleFormat)
44 DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::DitherType)
45 DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::SRCQuality)
46 DEFINE_ENUM_CONVERT (ARDOUR::ExportFormatBase::Type)
56 ExportFormatSpecification::Time &
57 ExportFormatSpecification::Time::operator= (AnyTime const & other)
59 static_cast<AnyTime &>(*this) = other;
64 ExportFormatSpecification::Time::get_samples_at (samplepos_t position, samplecnt_t target_rate) const
66 samplecnt_t duration = session.any_duration_to_samples (position, *this);
67 return ((double) target_rate / session.sample_rate()) * duration + 0.5;
71 ExportFormatSpecification::Time::get_state ()
74 XMLNode * node = new XMLNode ("Duration");
76 node->set_property ("format", type);
80 node->set_property ("hours", timecode.hours);
81 node->set_property ("minutes", timecode.minutes);
82 node->set_property ("seconds", timecode.seconds);
83 node->set_property ("frames", timecode.frames);
86 node->set_property ("bars", bbt.bars);
87 node->set_property ("beats", bbt.beats);
88 node->set_property ("ticks", bbt.ticks);
91 node->set_property ("samples", samples);
94 node->set_property ("seconds", seconds);
102 ExportFormatSpecification::Time::set_state (const XMLNode & node)
104 if (!node.get_property ("format", type)) {
110 node.get_property ("hours", timecode.hours);
111 node.get_property ("minutes", timecode.minutes);
112 node.get_property ("seconds", timecode.seconds);
113 node.get_property ("frames", timecode.frames);
117 node.get_property ("bars", bbt.bars);
118 node.get_property ("beats", bbt.beats);
119 node.get_property ("ticks", bbt.ticks);
123 node.get_property ("samples", samples);
127 node.get_property ("seconds", seconds);
135 ExportFormatSpecification::ExportFormatSpecification (Session & s)
137 , has_sample_format (false)
138 , supports_tagging (false)
139 , _has_codec_quality (false)
140 , _has_broadcast_info (false)
142 , _dither_type (D_None)
143 , _src_quality (SRC_SincBest)
146 , _trim_beginning (false)
147 , _silence_beginning (s)
152 , _normalize_loudness (false)
153 , _normalize_dbfs (GAIN_COEFF_UNITY)
154 , _normalize_lufs (-23)
155 , _normalize_dbtp (-1)
158 , _with_mp4chaps (false)
159 , _soundcloud_upload (false)
164 format_ids.insert (F_None);
165 endiannesses.insert (E_FileDefault);
166 sample_formats.insert (SF_None);
167 sample_rates.insert (SR_None);
168 qualities.insert (Q_None);
171 ExportFormatSpecification::ExportFormatSpecification (Session & s, XMLNode const & state)
173 , has_sample_format (false)
174 , supports_tagging (false)
175 , _has_codec_quality (false)
176 , _has_broadcast_info (false)
178 , _dither_type (D_None)
179 , _src_quality (SRC_SincBest)
182 , _trim_beginning (false)
183 , _silence_beginning (s)
188 , _normalize_loudness (false)
189 , _normalize_dbfs (GAIN_COEFF_UNITY)
190 , _normalize_lufs (-23)
191 , _normalize_dbtp (-1)
194 , _with_mp4chaps (false)
195 , _soundcloud_upload (false)
200 _silence_beginning.type = Time::Timecode;
201 _silence_end.type = Time::Timecode;
206 ExportFormatSpecification::ExportFormatSpecification (ExportFormatSpecification const & other, bool modify_name)
207 : ExportFormatBase(other)
208 , session (other.session)
209 , _silence_beginning (other.session)
210 , _silence_end (other.session)
211 , _with_toc (other._with_toc)
212 , _with_cue (other._with_cue)
213 , _with_mp4chaps (other._with_mp4chaps)
214 , _soundcloud_upload (false)
215 , _command (other._command)
216 , _analyse (other._analyse)
217 , _codec_quality (other._codec_quality)
220 set_name (other.name() + " (copy)");
222 set_name (other.name());
225 _format_name = other._format_name;
226 has_sample_format = other.has_sample_format;
227 supports_tagging = other.supports_tagging;
228 _has_codec_quality = other._has_codec_quality;
229 _has_broadcast_info = other._has_broadcast_info;
230 _channel_limit = other._channel_limit;
232 set_type (other.type());
233 set_format_id (other.format_id());
234 set_endianness (other.endianness());
235 set_sample_format (other.sample_format());
236 set_sample_rate (other.sample_rate());
237 set_quality (other.quality());
239 set_dither_type (other.dither_type());
240 set_src_quality (other.src_quality());
241 set_trim_beginning (other.trim_beginning());
242 set_trim_end (other.trim_end());
243 set_normalize (other.normalize());
244 set_normalize_loudness (other.normalize_loudness());
245 set_normalize_dbfs (other.normalize_dbfs());
246 set_normalize_lufs (other.normalize_lufs());
247 set_normalize_dbtp (other.normalize_dbtp());
249 set_tag (other.tag());
251 set_silence_beginning (other.silence_beginning_time());
252 set_silence_end (other.silence_end_time());
254 set_extension(other.extension());
257 ExportFormatSpecification::~ExportFormatSpecification ()
262 ExportFormatSpecification::get_state ()
265 XMLNode * root = new XMLNode ("ExportFormatSpecification");
267 root->set_property ("name", _name);
268 root->set_property ("id", _id.to_s());
269 root->set_property ("with-cue", _with_cue);
270 root->set_property ("with-toc", _with_toc);
271 root->set_property ("with-mp4chaps", _with_mp4chaps);
272 root->set_property ("command", _command);
273 root->set_property ("analyse", _analyse);
274 root->set_property ("soundcloud-upload", _soundcloud_upload);
276 node = root->add_child ("Encoding");
277 node->set_property ("id", format_id());
278 node->set_property ("type", type());
279 node->set_property ("extension", extension());
280 node->set_property ("name", _format_name);
281 node->set_property ("has-sample-format", has_sample_format);
282 node->set_property ("channel-limit", _channel_limit);
284 node = root->add_child ("SampleRate");
285 node->set_property ("rate", sample_rate());
287 node = root->add_child ("SRCQuality");
288 node->set_property ("quality", src_quality());
290 if (_has_codec_quality) {
291 node = root->add_child ("CodecQuality");
292 node->set_property ("quality", codec_quality());
295 XMLNode * enc_opts = root->add_child ("EncodingOptions");
297 add_option (enc_opts, "sample-format", to_string(sample_format()));
298 add_option (enc_opts, "dithering", to_string (dither_type()));
299 add_option (enc_opts, "tag-metadata", to_string (_tag));
300 add_option (enc_opts, "tag-support", to_string (supports_tagging));
301 add_option (enc_opts, "broadcast-info", to_string (_has_broadcast_info));
303 XMLNode * processing = root->add_child ("Processing");
305 node = processing->add_child ("Normalize");
306 node->set_property ("enabled", normalize());
307 node->set_property ("loudness", normalize_loudness());
308 node->set_property ("dbfs", normalize_dbfs());
309 node->set_property ("lufs", normalize_lufs());
310 node->set_property ("dbtp", normalize_dbtp());
312 XMLNode * silence = processing->add_child ("Silence");
313 XMLNode * start = silence->add_child ("Start");
314 XMLNode * end = silence->add_child ("End");
316 node = start->add_child ("Trim");
317 node->set_property ("enabled", trim_beginning());
319 node = start->add_child ("Add");
320 node->set_property ("enabled", _silence_beginning.not_zero());
321 node->add_child_nocopy (_silence_beginning.get_state());
323 node = end->add_child ("Trim");
324 node->set_property ("enabled", trim_end());
326 node = end->add_child ("Add");
327 node->set_property ("enabled", _silence_end.not_zero());
328 node->add_child_nocopy (_silence_end.get_state());
334 ExportFormatSpecification::set_state (const XMLNode & root)
336 XMLNode const * child;
339 root.get_property ("name", _name);
341 if (root.get_property ("id", str)) {
345 if (!root.get_property ("with-cue", _with_cue)) {
349 if (!root.get_property ("with-toc", _with_toc)) {
353 if (!root.get_property ("with-mp4chaps", _with_mp4chaps)) {
354 _with_mp4chaps = false;
357 if (!root.get_property ("command", _command)) {
361 if (!root.get_property ("analyse", _analyse)) {
365 if (!root.get_property ("soundcloud-upload", _soundcloud_upload)) {
366 _soundcloud_upload = false;
369 /* Encoding and SRC */
371 if ((child = root.child ("Encoding"))) {
373 if (child->get_property ("id", fid)) {
377 ExportFormatBase::Type type;
378 if (child->get_property ("type", type)) {
382 if (child->get_property ("extension", str)) {
386 child->get_property ("name", _format_name);
387 child->get_property ("has-sample-format", has_sample_format);
388 child->get_property ("channel-limit", _channel_limit);
391 if ((child = root.child ("SampleRate"))) {
393 if (child->get_property ("rate", rate)) {
394 set_sample_rate (rate);
398 if ((child = root.child ("SRCQuality"))) {
399 child->get_property ("quality", _src_quality);
402 if ((child = root.child ("CodecQuality"))) {
403 child->get_property ("quality", _codec_quality);
404 _has_codec_quality = true;
406 _has_codec_quality = false;
409 /* fixup codec quality for old states */
410 if (!_has_codec_quality) {
411 /* We'd need an instance of ExportFormatManager to look up
412 * defaults for a given type -- in the future there may even be
413 * difference qualities depending on sub-type, so we just
414 * hardcode them here for the time being.
416 if (format_id() == F_FFMPEG) {
417 _codec_quality = -2; // ExportFormatOggVorbis::default_codec_quality();
419 else if (format_id() == F_Ogg) {
420 _codec_quality = 40; // ExportFormatFFMPEG::default_codec_quality();
424 /* Encoding options */
426 if ((child = root.child ("EncodingOptions"))) {
427 set_sample_format ((SampleFormat) string_2_enum (get_option (child, "sample-format"), SampleFormat));
428 set_dither_type ((DitherType) string_2_enum (get_option (child, "dithering"), DitherType));
429 set_tag (string_to<bool>(get_option (child, "tag-metadata")));
430 supports_tagging = string_to<bool>(get_option (child, "tag-support"));
431 _has_broadcast_info = string_to<bool>(get_option (child, "broadcast-info"));
436 XMLNode const * proc = root.child ("Processing");
437 if (!proc) { std::cerr << X_("Could not load processing for export format") << std::endl; return -1; }
439 if ((child = proc->child ("Normalize"))) {
440 child->get_property ("enabled", _normalize);
441 // old formats before ~ 4.7-930ish
442 child->get_property ("target", _normalize_dbfs);
443 child->get_property ("loudness", _normalize_loudness);
444 child->get_property ("dbfs", _normalize_dbfs);
445 child->get_property ("lufs", _normalize_lufs);
446 child->get_property ("dbtp", _normalize_dbtp);
449 XMLNode const * silence = proc->child ("Silence");
450 if (!silence) { std::cerr << X_("Could not load silence for export format") << std::endl; return -1; }
452 XMLNode const * start = silence->child ("Start");
453 XMLNode const * end = silence->child ("End");
454 if (!start || !end) { std::cerr << X_("Could not load end or start silence for export format") << std::endl; return -1; }
458 if ((child = start->child ("Trim"))) {
459 child->get_property ("enabled", _trim_beginning);
463 if ((child = start->child ("Add"))) {
464 if (child->get_property ("enabled", enabled) && enabled) {
465 if ((child = child->child ("Duration"))) {
466 _silence_beginning.set_state (*child);
469 _silence_beginning.type = Time::Timecode;
475 if ((child = end->child ("Trim"))) {
476 child->get_property ("enabled", _trim_end);
479 if ((child = end->child ("Add"))) {
480 if (child->get_property ("enabled", enabled) && enabled) {
481 if ((child = child->child ("Duration"))) {
482 _silence_end.set_state (*child);
485 _silence_end.type = Time::Timecode;
493 ExportFormatSpecification::is_compatible_with (ExportFormatCompatibility const & compatibility) const
495 boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
497 if (intersection->formats_empty() && format_id() != 0) {
501 if (intersection->endiannesses_empty() && endianness() != E_FileDefault) {
505 if (intersection->sample_rates_empty() && sample_rate() != SR_None) {
509 if (intersection->sample_formats_empty() && sample_format() != SF_None) {
513 if (intersection->qualities_empty() && quality() != Q_None) {
521 ExportFormatSpecification::is_complete () const
523 if (type() == T_None) {
531 if (!sample_rate()) {
535 if (has_sample_format) {
536 if (sample_format() == SF_None) {
545 ExportFormatSpecification::set_format (boost::shared_ptr<ExportFormat> format)
548 FormatId new_fmt = format->get_format_id ();
549 bool fmt_changed = format_id() != new_fmt;
550 set_format_id (new_fmt);
552 set_type (format->get_type());
553 set_extension (format->extension());
555 if (format->get_explicit_sample_format()) {
556 set_sample_format (format->get_explicit_sample_format());
559 if (format->has_sample_format()) {
560 has_sample_format = true;
563 if (format->has_broadcast_info()) {
564 _has_broadcast_info = true;
567 _has_codec_quality = format->has_codec_quality();
568 if (!_has_codec_quality) {
570 } else if (fmt_changed) {
571 _codec_quality = boost::dynamic_pointer_cast<HasCodecQuality> (format)->default_codec_quality();
574 supports_tagging = format->supports_tagging ();
575 _channel_limit = format->get_channel_limit();
577 _format_name = format->name();
579 set_format_id (F_None);
582 _has_broadcast_info = false;
583 has_sample_format = false;
584 supports_tagging = false;
592 ExportFormatSpecification::description (bool include_name)
594 list<string> components;
597 if (_normalize_loudness) {
598 components.push_back (_("normalize loudness"));
600 components.push_back (_("normalize peak"));
604 if (_trim_beginning && _trim_end) {
605 components.push_back ( _("trim"));
606 } else if (_trim_beginning) {
607 components.push_back (_("trim start"));
608 } else if (_trim_end) {
609 components.push_back (_("trim end"));
612 if (_format_name != "") {
613 components.push_back (_format_name);
616 if (has_sample_format) {
617 components.push_back (HasSampleFormat::get_sample_format_name (sample_format()));
620 switch (sample_rate()) {
622 components.push_back ("8 kHz");
625 components.push_back ("22,5 kHz");
628 components.push_back ("44,1 kHz");
631 components.push_back ("48 kHz");
634 components.push_back ("88,2 kHz");
637 components.push_back ("96 kHz");
640 components.push_back ("176.4 kHz");
643 components.push_back ("192 kHz");
646 components.push_back (_("Session rate"));
653 components.push_back ("TOC");
657 components.push_back ("CUE");
660 if (_with_mp4chaps) {
661 components.push_back ("MP4ch");
664 if (!_command.empty()) {
665 components.push_back ("+");
673 for (list<string>::const_iterator it = components.begin(); it != components.end(); ++it) {
674 if (it != components.begin()) { desc += ", "; }
681 ExportFormatSpecification::add_option (XMLNode * node, std::string const & name, std::string const & value)
683 node = node->add_child ("Option");
684 node->set_property ("name", name);
685 node->set_property ("value", value);
689 ExportFormatSpecification::get_option (XMLNode const * node, std::string const & name)
691 XMLNodeList list (node->children ("Option"));
693 for (XMLNodeList::iterator it = list.begin(); it != list.end(); ++it) {
695 if ((*it)->get_property ("name", str) && name == str) {
696 if ((*it)->get_property ("value", str)) {
702 std::cerr << "Could not load encoding option \"" << name << "\" for export format" << std::endl;
707 }; // namespace ARDOUR