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"
42 ExportFormatSpecification::Time &
43 ExportFormatSpecification::Time::operator= (AnyTime const & other)
45 static_cast<AnyTime &>(*this) = other;
50 ExportFormatSpecification::Time::get_frames_at (framepos_t position, framecnt_t target_rate) const
52 framecnt_t duration = session.any_duration_to_frames (position, *this);
53 return ((double) target_rate / session.frame_rate()) * duration + 0.5;
57 ExportFormatSpecification::Time::get_state ()
60 XMLNode * node = new XMLNode ("Duration");
62 node->add_property ("format", enum_2_string (type));
66 node->add_property ("hours", to_string (timecode.hours, std::dec));
67 node->add_property ("minutes", to_string (timecode.minutes, std::dec));
68 node->add_property ("seconds", to_string (timecode.seconds, std::dec));
69 node->add_property ("frames", to_string (timecode.frames, std::dec));
72 node->add_property ("bars", to_string (bbt.bars, std::dec));
73 node->add_property ("beats", to_string (bbt.beats, std::dec));
74 node->add_property ("ticks", to_string (bbt.ticks, std::dec));
77 node->add_property ("frames", to_string (frames, std::dec));
80 node->add_property ("seconds", to_string (seconds, std::dec));
88 ExportFormatSpecification::Time::set_state (const XMLNode & node)
90 XMLProperty const * prop;
92 prop = node.property ("format");
94 if (!prop) { return -1; }
96 type = (Type) string_2_enum (prop->value(), Type);
100 if ((prop = node.property ("hours"))) {
101 timecode.hours = atoi (prop->value());
104 if ((prop = node.property ("minutes"))) {
105 timecode.minutes = atoi (prop->value());
108 if ((prop = node.property ("seconds"))) {
109 timecode.seconds = atoi (prop->value());
112 if ((prop = node.property ("frames"))) {
113 timecode.frames = atoi (prop->value());
119 if ((prop = node.property ("bars"))) {
120 bbt.bars = atoi (prop->value());
123 if ((prop = node.property ("beats"))) {
124 bbt.beats = atoi (prop->value());
127 if ((prop = node.property ("ticks"))) {
128 bbt.ticks = atoi (prop->value());
134 if ((prop = node.property ("frames"))) {
135 std::istringstream iss (prop->value());
142 if ((prop = node.property ("seconds"))) {
143 seconds = atof (prop->value());
152 ExportFormatSpecification::ExportFormatSpecification (Session & s)
155 , has_sample_format (false)
156 , supports_tagging (false)
157 , _has_broadcast_info (false)
159 , _dither_type (D_None)
160 , _src_quality (SRC_SincBest)
163 , _trim_beginning (false)
164 , _silence_beginning (s)
169 , _normalize_target (1.0)
171 format_ids.insert (F_None);
172 endiannesses.insert (E_FileDefault);
173 sample_formats.insert (SF_None);
174 sample_rates.insert (SR_None);
175 qualities.insert (Q_None);
178 ExportFormatSpecification::ExportFormatSpecification (Session & s, XMLNode const & state)
180 , _silence_beginning (s)
183 _silence_beginning.type = Time::Timecode;
184 _silence_end.type = Time::Timecode;
189 ExportFormatSpecification::ExportFormatSpecification (ExportFormatSpecification const & other)
190 : ExportFormatBase(other)
191 , session (other.session)
192 , _silence_beginning (other.session)
193 , _silence_end (other.session)
195 set_name (other.name() + " (copy)");
197 _format_name = other._format_name;
198 has_sample_format = other.has_sample_format;
200 supports_tagging = other.supports_tagging;
201 _has_broadcast_info = other._has_broadcast_info;
202 _channel_limit = other._channel_limit;
204 set_type (other.type());
205 set_format_id (other.format_id());
206 set_endianness (other.endianness());
207 set_sample_format (other.sample_format());
208 set_sample_rate (other.sample_rate());
209 set_quality (other.quality());
211 set_dither_type (other.dither_type());
212 set_src_quality (other.src_quality());
213 set_trim_beginning (other.trim_beginning());
214 set_trim_end (other.trim_end());
215 set_normalize (other.normalize());
216 set_normalize_target (other.normalize_target());
218 set_tag (other.tag());
220 set_silence_beginning (other.silence_beginning_time());
221 set_silence_end (other.silence_end_time());
223 set_extension(other.extension());
226 ExportFormatSpecification::~ExportFormatSpecification ()
231 ExportFormatSpecification::get_state ()
234 XMLNode * root = new XMLNode ("ExportFormatSpecification");
236 root->add_property ("name", _name);
237 root->add_property ("id", _id.to_s());
239 node = root->add_child ("Encoding");
240 node->add_property ("id", enum_2_string (format_id()));
241 node->add_property ("type", enum_2_string (type()));
242 node->add_property ("extension", extension());
243 node->add_property ("name", _format_name);
244 node->add_property ("has-sample-format", has_sample_format ? "true" : "false");
245 node->add_property ("channel-limit", to_string (_channel_limit, std::dec));
247 node = root->add_child ("SampleRate");
248 node->add_property ("rate", to_string (sample_rate(), std::dec));
250 node = root->add_child ("SRCQuality");
251 node->add_property ("quality", enum_2_string (src_quality()));
253 XMLNode * enc_opts = root->add_child ("EncodingOptions");
255 add_option (enc_opts, "sample-format", enum_2_string (sample_format()));
256 add_option (enc_opts, "dithering", enum_2_string (dither_type()));
257 add_option (enc_opts, "tag-metadata", _tag ? "true" : "false");
258 add_option (enc_opts, "tag-support", supports_tagging ? "true" : "false");
259 add_option (enc_opts, "broadcast-info", _has_broadcast_info ? "true" : "false");
261 XMLNode * processing = root->add_child ("Processing");
263 node = processing->add_child ("Normalize");
264 node->add_property ("enabled", normalize() ? "true" : "false");
265 node->add_property ("target", to_string (normalize_target(), std::dec));
267 XMLNode * silence = processing->add_child ("Silence");
268 XMLNode * start = silence->add_child ("Start");
269 XMLNode * end = silence->add_child ("End");
271 node = start->add_child ("Trim");
272 node->add_property ("enabled", trim_beginning() ? "true" : "false");
274 node = start->add_child ("Add");
275 node->add_property ("enabled", _silence_beginning.not_zero() ? "true" : "false");
276 node->add_child_nocopy (_silence_beginning.get_state());
278 node = end->add_child ("Trim");
279 node->add_property ("enabled", trim_end() ? "true" : "false");
281 node = end->add_child ("Add");
282 node->add_property ("enabled", _silence_end.not_zero() ? "true" : "false");
283 node->add_child_nocopy (_silence_end.get_state());
289 ExportFormatSpecification::set_state (const XMLNode & root)
291 XMLProperty const * prop;
292 XMLNode const * child;
295 if ((prop = root.property ("name"))) {
296 _name = prop->value();
299 if ((prop = root.property ("id"))) {
303 /* Encoding and SRC */
305 if ((child = root.child ("Encoding"))) {
306 if ((prop = child->property ("id"))) {
307 set_format_id ((FormatId) string_2_enum (prop->value(), FormatId));
310 if ((prop = child->property ("type"))) {
311 set_type ((Type) string_2_enum (prop->value(), Type));
314 if ((prop = child->property ("extension"))) {
315 set_extension (prop->value());
318 if ((prop = child->property ("name"))) {
319 _format_name = prop->value();
322 if ((prop = child->property ("has-sample-format"))) {
323 has_sample_format = !prop->value().compare ("true");
326 if ((prop = child->property ("channel-limit"))) {
327 _channel_limit = atoi (prop->value());
331 if ((child = root.child ("SampleRate"))) {
332 if ((prop = child->property ("rate"))) {
333 set_sample_rate ( (SampleRate) string_2_enum (prop->value(), SampleRate));
337 if ((child = root.child ("SRCQuality"))) {
338 if ((prop = child->property ("quality"))) {
339 _src_quality = (SRCQuality) string_2_enum (prop->value(), SRCQuality);
343 /* Encoding options */
345 if ((child = root.child ("EncodingOptions"))) {
346 set_sample_format ((SampleFormat) string_2_enum (get_option (child, "sample-format"), SampleFormat));
347 set_dither_type ((DitherType) string_2_enum (get_option (child, "dithering"), DitherType));
348 set_tag (!(get_option (child, "tag-metadata").compare ("true")));
349 supports_tagging = (!(get_option (child, "tag-support").compare ("true")));
350 _has_broadcast_info = (!(get_option (child, "broadcast-info").compare ("true")));
355 XMLNode const * proc = root.child ("Processing");
356 if (!proc) { std::cerr << X_("Could not load processing for export format") << std::endl; return -1; }
358 if ((child = proc->child ("Normalize"))) {
359 if ((prop = child->property ("enabled"))) {
360 _normalize = (!prop->value().compare ("true"));
363 if ((prop = child->property ("target"))) {
364 _normalize_target = atof (prop->value());
368 XMLNode const * silence = proc->child ("Silence");
369 if (!silence) { std::cerr << X_("Could not load silence for export format") << std::endl; return -1; }
371 XMLNode const * start = silence->child ("Start");
372 XMLNode const * end = silence->child ("End");
373 if (!start || !end) { std::cerr << X_("Could not load end or start silence for export format") << std::endl; return -1; }
377 if ((child = start->child ("Trim"))) {
378 if ((prop = child->property ("enabled"))) {
379 _trim_beginning = (!prop->value().compare ("true"));
383 if ((child = start->child ("Add"))) {
384 if ((prop = child->property ("enabled"))) {
385 if (!prop->value().compare ("true")) {
386 if ((child = child->child ("Duration"))) {
387 _silence_beginning.set_state (*child);
390 _silence_beginning.type = Time::Timecode;
397 if ((child = end->child ("Trim"))) {
398 if ((prop = child->property ("enabled"))) {
399 _trim_end = (!prop->value().compare ("true"));
403 if ((child = end->child ("Add"))) {
404 if ((prop = child->property ("enabled"))) {
405 if (!prop->value().compare ("true")) {
406 if ((child = child->child ("Duration"))) {
407 _silence_end.set_state (*child);
410 _silence_end.type = Time::Timecode;
419 ExportFormatSpecification::is_compatible_with (ExportFormatCompatibility const & compatibility) const
421 boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
423 if (intersection->formats_empty() && format_id() != 0) {
427 if (intersection->endiannesses_empty() && endianness() != E_FileDefault) {
431 if (intersection->sample_rates_empty() && sample_rate() != SR_None) {
435 if (intersection->sample_formats_empty() && sample_format() != SF_None) {
439 if (intersection->qualities_empty() && quality() != Q_None) {
447 ExportFormatSpecification::is_complete () const
449 if (type() == T_None) {
457 if (!sample_rate()) {
461 if (has_sample_format) {
462 if (sample_format() == SF_None) {
471 ExportFormatSpecification::set_format (boost::shared_ptr<ExportFormat> format)
474 set_format_id (format->get_format_id ());
475 set_type (format->get_type());
476 set_extension (format->extension());
478 if (format->get_explicit_sample_format()) {
479 set_sample_format (format->get_explicit_sample_format());
482 if (format->has_sample_format()) {
483 has_sample_format = true;
486 if (format->has_broadcast_info()) {
487 _has_broadcast_info = true;
490 supports_tagging = format->supports_tagging ();
491 _channel_limit = format->get_channel_limit();
493 _format_name = format->name();
495 set_format_id (F_None);
498 _has_broadcast_info = false;
499 has_sample_format = false;
500 supports_tagging = false;
507 ExportFormatSpecification::description ()
514 desc += _("normalize, ");
517 if (_trim_beginning && _trim_end) {
519 } else if (_trim_beginning) {
520 desc += _("trim start, ");
521 } else if (_trim_end) {
522 desc += "trim end, ";
525 desc += _format_name + ", ";
527 if (has_sample_format) {
528 desc += HasSampleFormat::get_sample_format_name (sample_format()) + ", ";
531 switch (sample_rate()) {
551 desc += _("Session rate");
561 ExportFormatSpecification::add_option (XMLNode * node, std::string const & name, std::string const & value)
563 node = node->add_child ("Option");
564 node->add_property ("name", name);
565 node->add_property ("value", value);
569 ExportFormatSpecification::get_option (XMLNode const * node, std::string const & name)
571 XMLNodeList list (node->children ("Option"));
573 for (XMLNodeList::iterator it = list.begin(); it != list.end(); ++it) {
574 XMLProperty * prop = (*it)->property ("name");
575 if (prop && !name.compare (prop->value())) {
576 prop = (*it)->property ("value");
578 return prop->value();
583 std::cerr << "Could not load encoding option \"" << name << "\" for export format" << std::endl;
588 }; // namespace ARDOUR