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_target (1.0)
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);
183 ExportFormatSpecification::ExportFormatSpecification (Session & s, XMLNode const & state)
185 , _silence_beginning (s)
188 _silence_beginning.type = Time::Timecode;
189 _silence_end.type = Time::Timecode;
194 ExportFormatSpecification::ExportFormatSpecification (ExportFormatSpecification const & other, bool modify_name)
195 : ExportFormatBase(other)
196 , session (other.session)
197 , _silence_beginning (other.session)
198 , _silence_end (other.session)
201 set_name (other.name() + " (copy)");
203 set_name (other.name());
206 _format_name = other._format_name;
207 has_sample_format = other.has_sample_format;
209 supports_tagging = other.supports_tagging;
210 _has_broadcast_info = other._has_broadcast_info;
211 _channel_limit = other._channel_limit;
213 set_type (other.type());
214 set_format_id (other.format_id());
215 set_endianness (other.endianness());
216 set_sample_format (other.sample_format());
217 set_sample_rate (other.sample_rate());
218 set_quality (other.quality());
220 set_dither_type (other.dither_type());
221 set_src_quality (other.src_quality());
222 set_trim_beginning (other.trim_beginning());
223 set_trim_end (other.trim_end());
224 set_normalize (other.normalize());
225 set_normalize_target (other.normalize_target());
227 set_tag (other.tag());
229 set_silence_beginning (other.silence_beginning_time());
230 set_silence_end (other.silence_end_time());
232 set_extension(other.extension());
235 ExportFormatSpecification::~ExportFormatSpecification ()
240 ExportFormatSpecification::get_state ()
243 XMLNode * root = new XMLNode ("ExportFormatSpecification");
245 root->add_property ("name", _name);
246 root->add_property ("id", _id.to_s());
247 root->add_property ("with-cue", _with_cue ? "true" : "false");
248 root->add_property ("with-toc", _with_toc ? "true" : "false");
249 root->add_property ("upload", _upload ? "true" : "false");
250 root->add_property ("command", _command);
252 node = root->add_child ("Encoding");
253 node->add_property ("id", enum_2_string (format_id()));
254 node->add_property ("type", enum_2_string (type()));
255 node->add_property ("extension", extension());
256 node->add_property ("name", _format_name);
257 node->add_property ("has-sample-format", has_sample_format ? "true" : "false");
258 node->add_property ("channel-limit", to_string (_channel_limit, std::dec));
260 node = root->add_child ("SampleRate");
261 node->add_property ("rate", to_string (sample_rate(), std::dec));
263 node = root->add_child ("SRCQuality");
264 node->add_property ("quality", enum_2_string (src_quality()));
266 XMLNode * enc_opts = root->add_child ("EncodingOptions");
268 add_option (enc_opts, "sample-format", enum_2_string (sample_format()));
269 add_option (enc_opts, "dithering", enum_2_string (dither_type()));
270 add_option (enc_opts, "tag-metadata", _tag ? "true" : "false");
271 add_option (enc_opts, "tag-support", supports_tagging ? "true" : "false");
272 add_option (enc_opts, "broadcast-info", _has_broadcast_info ? "true" : "false");
274 XMLNode * processing = root->add_child ("Processing");
276 node = processing->add_child ("Normalize");
277 node->add_property ("enabled", normalize() ? "true" : "false");
278 node->add_property ("target", to_string (normalize_target(), std::dec));
280 XMLNode * silence = processing->add_child ("Silence");
281 XMLNode * start = silence->add_child ("Start");
282 XMLNode * end = silence->add_child ("End");
284 node = start->add_child ("Trim");
285 node->add_property ("enabled", trim_beginning() ? "true" : "false");
287 node = start->add_child ("Add");
288 node->add_property ("enabled", _silence_beginning.not_zero() ? "true" : "false");
289 node->add_child_nocopy (_silence_beginning.get_state());
291 node = end->add_child ("Trim");
292 node->add_property ("enabled", trim_end() ? "true" : "false");
294 node = end->add_child ("Add");
295 node->add_property ("enabled", _silence_end.not_zero() ? "true" : "false");
296 node->add_child_nocopy (_silence_end.get_state());
302 ExportFormatSpecification::set_state (const XMLNode & root)
304 XMLProperty const * prop;
305 XMLNode const * child;
308 if ((prop = root.property ("name"))) {
309 _name = prop->value();
312 if ((prop = root.property ("id"))) {
316 if ((prop = root.property ("with-cue"))) {
317 _with_cue = string_is_affirmative (prop->value());
322 if ((prop = root.property ("with-toc"))) {
323 _with_toc = string_is_affirmative (prop->value());
328 if ((prop = root.property ("upload"))) {
329 _upload = string_is_affirmative (prop->value());
334 if ((prop = root.property ("command"))) {
335 _command = prop->value();
340 /* Encoding and SRC */
342 if ((child = root.child ("Encoding"))) {
343 if ((prop = child->property ("id"))) {
344 set_format_id ((FormatId) string_2_enum (prop->value(), FormatId));
347 if ((prop = child->property ("type"))) {
348 set_type ((Type) string_2_enum (prop->value(), Type));
351 if ((prop = child->property ("extension"))) {
352 set_extension (prop->value());
355 if ((prop = child->property ("name"))) {
356 _format_name = prop->value();
359 if ((prop = child->property ("has-sample-format"))) {
360 has_sample_format = string_is_affirmative (prop->value());
363 if ((prop = child->property ("has-sample-format"))) {
364 has_sample_format = string_is_affirmative (prop->value());
367 if ((prop = child->property ("channel-limit"))) {
368 _channel_limit = atoi (prop->value());
372 if ((child = root.child ("SampleRate"))) {
373 if ((prop = child->property ("rate"))) {
374 set_sample_rate ( (SampleRate) string_2_enum (prop->value(), SampleRate));
378 if ((child = root.child ("SRCQuality"))) {
379 if ((prop = child->property ("quality"))) {
380 _src_quality = (SRCQuality) string_2_enum (prop->value(), SRCQuality);
384 /* Encoding options */
386 if ((child = root.child ("EncodingOptions"))) {
387 set_sample_format ((SampleFormat) string_2_enum (get_option (child, "sample-format"), SampleFormat));
388 set_dither_type ((DitherType) string_2_enum (get_option (child, "dithering"), DitherType));
389 set_tag (!(get_option (child, "tag-metadata").compare ("true")));
390 supports_tagging = (!(get_option (child, "tag-support").compare ("true")));
391 _has_broadcast_info = (!(get_option (child, "broadcast-info").compare ("true")));
396 XMLNode const * proc = root.child ("Processing");
397 if (!proc) { std::cerr << X_("Could not load processing for export format") << std::endl; return -1; }
399 if ((child = proc->child ("Normalize"))) {
400 if ((prop = child->property ("enabled"))) {
401 _normalize = (!prop->value().compare ("true"));
404 if ((prop = child->property ("target"))) {
405 _normalize_target = atof (prop->value());
409 XMLNode const * silence = proc->child ("Silence");
410 if (!silence) { std::cerr << X_("Could not load silence for export format") << std::endl; return -1; }
412 XMLNode const * start = silence->child ("Start");
413 XMLNode const * end = silence->child ("End");
414 if (!start || !end) { std::cerr << X_("Could not load end or start silence for export format") << std::endl; return -1; }
418 if ((child = start->child ("Trim"))) {
419 if ((prop = child->property ("enabled"))) {
420 _trim_beginning = (!prop->value().compare ("true"));
424 if ((child = start->child ("Add"))) {
425 if ((prop = child->property ("enabled"))) {
426 if (!prop->value().compare ("true")) {
427 if ((child = child->child ("Duration"))) {
428 _silence_beginning.set_state (*child);
431 _silence_beginning.type = Time::Timecode;
438 if ((child = end->child ("Trim"))) {
439 if ((prop = child->property ("enabled"))) {
440 _trim_end = (!prop->value().compare ("true"));
444 if ((child = end->child ("Add"))) {
445 if ((prop = child->property ("enabled"))) {
446 if (!prop->value().compare ("true")) {
447 if ((child = child->child ("Duration"))) {
448 _silence_end.set_state (*child);
451 _silence_end.type = Time::Timecode;
460 ExportFormatSpecification::is_compatible_with (ExportFormatCompatibility const & compatibility) const
462 boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
464 if (intersection->formats_empty() && format_id() != 0) {
468 if (intersection->endiannesses_empty() && endianness() != E_FileDefault) {
472 if (intersection->sample_rates_empty() && sample_rate() != SR_None) {
476 if (intersection->sample_formats_empty() && sample_format() != SF_None) {
480 if (intersection->qualities_empty() && quality() != Q_None) {
488 ExportFormatSpecification::is_complete () const
490 if (type() == T_None) {
498 if (!sample_rate()) {
502 if (has_sample_format) {
503 if (sample_format() == SF_None) {
512 ExportFormatSpecification::set_format (boost::shared_ptr<ExportFormat> format)
515 set_format_id (format->get_format_id ());
516 set_type (format->get_type());
517 set_extension (format->extension());
519 if (format->get_explicit_sample_format()) {
520 set_sample_format (format->get_explicit_sample_format());
523 if (format->has_sample_format()) {
524 has_sample_format = true;
527 if (format->has_broadcast_info()) {
528 _has_broadcast_info = true;
531 supports_tagging = format->supports_tagging ();
532 _channel_limit = format->get_channel_limit();
534 _format_name = format->name();
536 set_format_id (F_None);
539 _has_broadcast_info = false;
540 has_sample_format = false;
541 supports_tagging = false;
548 ExportFormatSpecification::description (bool include_name)
550 list<string> components;
553 components.push_back (_("normalize"));
556 if (_trim_beginning && _trim_end) {
557 components.push_back ( _("trim"));
558 } else if (_trim_beginning) {
559 components.push_back (_("trim start"));
560 } else if (_trim_end) {
561 components.push_back (_("trim end"));
564 if (_format_name != "") {
565 components.push_back (_format_name);
568 if (has_sample_format) {
569 components.push_back (HasSampleFormat::get_sample_format_name (sample_format()));
572 switch (sample_rate()) {
574 components.push_back ("8 kHz");
577 components.push_back ("22,5 kHz");
580 components.push_back ("44,1 kHz");
583 components.push_back ("48 kHz");
586 components.push_back ("88,2 kHz");
589 components.push_back ("96 kHz");
592 components.push_back ("192 kHz");
595 components.push_back (_("Session rate"));
602 components.push_back ("TOC");
606 components.push_back ("CUE");
610 components.push_back ("Upload");
613 if (!_command.empty()) {
614 components.push_back ("+");
622 for (list<string>::const_iterator it = components.begin(); it != components.end(); ++it) {
623 if (it != components.begin()) { desc += ", "; }
630 ExportFormatSpecification::add_option (XMLNode * node, std::string const & name, std::string const & value)
632 node = node->add_child ("Option");
633 node->add_property ("name", name);
634 node->add_property ("value", value);
638 ExportFormatSpecification::get_option (XMLNode const * node, std::string const & name)
640 XMLNodeList list (node->children ("Option"));
642 for (XMLNodeList::iterator it = list.begin(); it != list.end(); ++it) {
643 XMLProperty * prop = (*it)->property ("name");
644 if (prop && !name.compare (prop->value())) {
645 prop = (*it)->property ("value");
647 return prop->value();
652 std::cerr << "Could not load encoding option \"" << name << "\" for export format" << std::endl;
657 }; // namespace ARDOUR