Add toc and cue information to export format descriptions
[ardour.git] / libs / ardour / export_format_specification.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
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.
9
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.
14
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.
18
19 */
20
21 #include "ardour/export_format_specification.h"
22
23 #include <sstream>
24
25 #include "ardour/export_format_compatibility.h"
26 #include "ardour/export_formats.h"
27 #include "ardour/session.h"
28
29 #include "pbd/error.h"
30 #include "pbd/xml++.h"
31 #include "pbd/enumwriter.h"
32 #include "pbd/convert.h"
33
34 #include "i18n.h"
35
36 namespace ARDOUR
37 {
38
39 using namespace PBD;
40 using std::string;
41
42 ExportFormatSpecification::Time &
43 ExportFormatSpecification::Time::operator= (AnyTime const & other)
44 {
45         static_cast<AnyTime &>(*this) = other;
46         return *this;
47 }
48
49 framecnt_t
50 ExportFormatSpecification::Time::get_frames_at (framepos_t position, framecnt_t target_rate) const
51 {
52         framecnt_t duration = session.any_duration_to_frames (position, *this);
53         return ((double) target_rate / session.frame_rate()) * duration + 0.5;
54 }
55
56 XMLNode &
57 ExportFormatSpecification::Time::get_state ()
58 {
59
60         XMLNode * node = new XMLNode ("Duration");
61
62         node->add_property ("format", enum_2_string (type));
63
64         switch (type) {
65           case Timecode:
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));
70                 break;
71           case BBT:
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));
75                 break;
76           case Frames:
77                 node->add_property ("frames", to_string (frames, std::dec));
78                 break;
79           case Seconds:
80                 node->add_property ("seconds", to_string (seconds, std::dec));
81                 break;
82         }
83
84         return *node;
85 }
86
87 int
88 ExportFormatSpecification::Time::set_state (const XMLNode & node)
89 {
90         XMLProperty const * prop;
91
92         prop = node.property ("format");
93
94         if (!prop) { return -1; }
95
96         type = (Type) string_2_enum (prop->value(), Type);
97
98         switch (type) {
99           case Timecode:
100                 if ((prop = node.property ("hours"))) {
101                         timecode.hours = atoi (prop->value());
102                 }
103
104                 if ((prop = node.property ("minutes"))) {
105                         timecode.minutes = atoi (prop->value());
106                 }
107
108                 if ((prop = node.property ("seconds"))) {
109                         timecode.seconds = atoi (prop->value());
110                 }
111
112                 if ((prop = node.property ("frames"))) {
113                         timecode.frames = atoi (prop->value());
114                 }
115
116                 break;
117
118           case BBT:
119                 if ((prop = node.property ("bars"))) {
120                         bbt.bars = atoi (prop->value());
121                 }
122
123                 if ((prop = node.property ("beats"))) {
124                         bbt.beats = atoi (prop->value());
125                 }
126
127                 if ((prop = node.property ("ticks"))) {
128                         bbt.ticks = atoi (prop->value());
129                 }
130
131                 break;
132
133           case Frames:
134                 if ((prop = node.property ("frames"))) {
135                         std::istringstream iss (prop->value());
136                         iss >> frames;
137                 }
138
139                 break;
140
141           case Seconds:
142                 if ((prop = node.property ("seconds"))) {
143                         seconds = atof (prop->value());
144                 }
145
146                 break;
147         }
148
149         return 0;
150 }
151
152 ExportFormatSpecification::ExportFormatSpecification (Session & s)
153         : session (s)
154
155         , has_sample_format (false)
156         , supports_tagging (false)
157         , _has_broadcast_info (false)
158         , _channel_limit (0)
159         , _dither_type (D_None)
160         , _src_quality (SRC_SincBest)
161         , _tag (true)
162
163         , _trim_beginning (false)
164         , _silence_beginning (s)
165         , _trim_end (false)
166         , _silence_end (s)
167
168         , _normalize (false)
169         , _normalize_target (1.0)
170         , _with_toc (false)
171         , _with_cue (false)
172 {
173         format_ids.insert (F_None);
174         endiannesses.insert (E_FileDefault);
175         sample_formats.insert (SF_None);
176         sample_rates.insert (SR_None);
177         qualities.insert (Q_None);
178 }
179
180 ExportFormatSpecification::ExportFormatSpecification (Session & s, XMLNode const & state)
181         : session (s)
182         , _silence_beginning (s)
183         , _silence_end (s)
184 {
185         _silence_beginning.type = Time::Timecode;
186         _silence_end.type = Time::Timecode;
187
188         set_state (state);
189 }
190
191 ExportFormatSpecification::ExportFormatSpecification (ExportFormatSpecification const & other)
192         : ExportFormatBase(other)
193         , session (other.session)
194         , _silence_beginning (other.session)
195         , _silence_end (other.session)
196 {
197         set_name (other.name() + " (copy)");
198
199         _format_name = other._format_name;
200         has_sample_format = other.has_sample_format;
201
202         supports_tagging = other.supports_tagging;
203         _has_broadcast_info = other._has_broadcast_info;
204         _channel_limit = other._channel_limit;
205
206         set_type (other.type());
207         set_format_id (other.format_id());
208         set_endianness (other.endianness());
209         set_sample_format (other.sample_format());
210         set_sample_rate (other.sample_rate());
211         set_quality (other.quality());
212
213         set_dither_type (other.dither_type());
214         set_src_quality (other.src_quality());
215         set_trim_beginning (other.trim_beginning());
216         set_trim_end (other.trim_end());
217         set_normalize (other.normalize());
218         set_normalize_target (other.normalize_target());
219
220         set_tag (other.tag());
221
222         set_silence_beginning (other.silence_beginning_time());
223         set_silence_end (other.silence_end_time());
224
225         set_extension(other.extension());
226 }
227
228 ExportFormatSpecification::~ExportFormatSpecification ()
229 {
230 }
231
232 XMLNode &
233 ExportFormatSpecification::get_state ()
234 {
235         XMLNode * node;
236         XMLNode * root = new XMLNode ("ExportFormatSpecification");
237
238         root->add_property ("name", _name);
239         root->add_property ("id", _id.to_s());
240         root->add_property ("with-cue", _with_cue ? "true" : "false");
241         root->add_property ("with-toc", _with_toc ? "true" : "false");
242
243         node = root->add_child ("Encoding");
244         node->add_property ("id", enum_2_string (format_id()));
245         node->add_property ("type", enum_2_string (type()));
246         node->add_property ("extension", extension());
247         node->add_property ("name", _format_name);
248         node->add_property ("has-sample-format", has_sample_format ? "true" : "false");
249         node->add_property ("channel-limit", to_string (_channel_limit, std::dec));
250
251         node = root->add_child ("SampleRate");
252         node->add_property ("rate", to_string (sample_rate(), std::dec));
253
254         node = root->add_child ("SRCQuality");
255         node->add_property ("quality", enum_2_string (src_quality()));
256
257         XMLNode * enc_opts = root->add_child ("EncodingOptions");
258
259         add_option (enc_opts, "sample-format", enum_2_string (sample_format()));
260         add_option (enc_opts, "dithering", enum_2_string (dither_type()));
261         add_option (enc_opts, "tag-metadata", _tag ? "true" : "false");
262         add_option (enc_opts, "tag-support", supports_tagging ? "true" : "false");
263         add_option (enc_opts, "broadcast-info", _has_broadcast_info ? "true" : "false");
264
265         XMLNode * processing = root->add_child ("Processing");
266
267         node = processing->add_child ("Normalize");
268         node->add_property ("enabled", normalize() ? "true" : "false");
269         node->add_property ("target", to_string (normalize_target(), std::dec));
270
271         XMLNode * silence = processing->add_child ("Silence");
272         XMLNode * start = silence->add_child ("Start");
273         XMLNode * end = silence->add_child ("End");
274
275         node = start->add_child ("Trim");
276         node->add_property ("enabled", trim_beginning() ? "true" : "false");
277
278         node = start->add_child ("Add");
279         node->add_property ("enabled", _silence_beginning.not_zero() ? "true" : "false");
280         node->add_child_nocopy (_silence_beginning.get_state());
281
282         node = end->add_child ("Trim");
283         node->add_property ("enabled", trim_end() ? "true" : "false");
284
285         node = end->add_child ("Add");
286         node->add_property ("enabled", _silence_end.not_zero() ? "true" : "false");
287         node->add_child_nocopy (_silence_end.get_state());
288
289         return *root;
290 }
291
292 int
293 ExportFormatSpecification::set_state (const XMLNode & root)
294 {
295         XMLProperty const * prop;
296         XMLNode const * child;
297         string value;
298
299         if ((prop = root.property ("name"))) {
300                 _name = prop->value();
301         }
302
303         if ((prop = root.property ("id"))) {
304                 _id = prop->value();
305         }
306
307         if ((prop = root.property ("with-cue"))) {
308                 _with_cue = string_is_affirmative (prop->value());
309         } else {
310                 _with_cue = false;
311         }
312         
313         if ((prop = root.property ("with-toc"))) {
314                 _with_toc = string_is_affirmative (prop->value());
315         } else {
316                 _with_toc = false;
317         }
318         
319         /* Encoding and SRC */
320
321         if ((child = root.child ("Encoding"))) {
322                 if ((prop = child->property ("id"))) {
323                         set_format_id ((FormatId) string_2_enum (prop->value(), FormatId));
324                 }
325
326                 if ((prop = child->property ("type"))) {
327                         set_type ((Type) string_2_enum (prop->value(), Type));
328                 }
329
330                 if ((prop = child->property ("extension"))) {
331                         set_extension (prop->value());
332                 }
333
334                 if ((prop = child->property ("name"))) {
335                         _format_name = prop->value();
336                 }
337
338                 if ((prop = child->property ("has-sample-format"))) {
339                         has_sample_format = string_is_affirmative (prop->value());
340                 }
341
342                 if ((prop = child->property ("has-sample-format"))) {
343                         has_sample_format = string_is_affirmative (prop->value());
344                 }
345
346                 if ((prop = child->property ("channel-limit"))) {
347                         _channel_limit = atoi (prop->value());
348                 }
349         }
350
351         if ((child = root.child ("SampleRate"))) {
352                 if ((prop = child->property ("rate"))) {
353                         set_sample_rate ( (SampleRate) string_2_enum (prop->value(), SampleRate));
354                 }
355         }
356
357         if ((child = root.child ("SRCQuality"))) {
358                 if ((prop = child->property ("quality"))) {
359                         _src_quality = (SRCQuality) string_2_enum (prop->value(), SRCQuality);
360                 }
361         }
362
363         /* Encoding options */
364
365         if ((child = root.child ("EncodingOptions"))) {
366                 set_sample_format ((SampleFormat) string_2_enum (get_option (child, "sample-format"), SampleFormat));
367                 set_dither_type ((DitherType) string_2_enum (get_option (child, "dithering"), DitherType));
368                 set_tag (!(get_option (child, "tag-metadata").compare ("true")));
369                 supports_tagging = (!(get_option (child, "tag-support").compare ("true")));
370                 _has_broadcast_info = (!(get_option (child, "broadcast-info").compare ("true")));
371         }
372
373         /* Processing */
374
375         XMLNode const * proc = root.child ("Processing");
376         if (!proc) { std::cerr << X_("Could not load processing for export format") << std::endl; return -1; }
377
378         if ((child = proc->child ("Normalize"))) {
379                 if ((prop = child->property ("enabled"))) {
380                         _normalize = (!prop->value().compare ("true"));
381                 }
382
383                 if ((prop = child->property ("target"))) {
384                         _normalize_target = atof (prop->value());
385                 }
386         }
387
388         XMLNode const * silence = proc->child ("Silence");
389         if (!silence) { std::cerr << X_("Could not load silence for export format") << std::endl; return -1; }
390
391         XMLNode const * start = silence->child ("Start");
392         XMLNode const * end = silence->child ("End");
393         if (!start || !end) { std::cerr << X_("Could not load end or start silence for export format") << std::endl; return -1; }
394
395         /* Silence start */
396
397         if ((child = start->child ("Trim"))) {
398                 if ((prop = child->property ("enabled"))) {
399                         _trim_beginning = (!prop->value().compare ("true"));
400                 }
401         }
402
403         if ((child = start->child ("Add"))) {
404                 if ((prop = child->property ("enabled"))) {
405                         if (!prop->value().compare ("true")) {
406                                 if ((child = child->child ("Duration"))) {
407                                         _silence_beginning.set_state (*child);
408                                 }
409                         } else {
410                                 _silence_beginning.type = Time::Timecode;
411                         }
412                 }
413         }
414
415         /* Silence end */
416
417         if ((child = end->child ("Trim"))) {
418                 if ((prop = child->property ("enabled"))) {
419                         _trim_end = (!prop->value().compare ("true"));
420                 }
421         }
422
423         if ((child = end->child ("Add"))) {
424                 if ((prop = child->property ("enabled"))) {
425                         if (!prop->value().compare ("true")) {
426                                 if ((child = child->child ("Duration"))) {
427                                         _silence_end.set_state (*child);
428                                 }
429                         } else {
430                                 _silence_end.type = Time::Timecode;
431                         }
432                 }
433         }
434
435         return 0;
436 }
437
438 bool
439 ExportFormatSpecification::is_compatible_with (ExportFormatCompatibility const & compatibility) const
440 {
441         boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
442
443         if (intersection->formats_empty() && format_id() != 0) {
444                 return false;
445         }
446
447         if (intersection->endiannesses_empty() && endianness() != E_FileDefault) {
448                 return false;
449         }
450
451         if (intersection->sample_rates_empty() && sample_rate() != SR_None) {
452                 return false;
453         }
454
455         if (intersection->sample_formats_empty() && sample_format() != SF_None) {
456                 return false;
457         }
458
459         if (intersection->qualities_empty() && quality() != Q_None) {
460                 return false;
461         }
462
463         return true;
464 }
465
466 bool
467 ExportFormatSpecification::is_complete () const
468 {
469         if (type() == T_None) {
470                 return false;
471         }
472
473         if (!format_id()) {
474                 return false;
475         }
476
477         if (!sample_rate()) {
478                 return false;
479         }
480
481         if (has_sample_format) {
482                 if (sample_format() == SF_None) {
483                         return false;
484                 }
485         }
486
487         return true;
488 }
489
490 void
491 ExportFormatSpecification::set_format (boost::shared_ptr<ExportFormat> format)
492 {
493         if (format) {
494                 set_format_id (format->get_format_id ());
495                 set_type (format->get_type());
496                 set_extension (format->extension());
497
498                 if (format->get_explicit_sample_format()) {
499                         set_sample_format (format->get_explicit_sample_format());
500                 }
501
502                 if (format->has_sample_format()) {
503                         has_sample_format = true;
504                 }
505
506                 if (format->has_broadcast_info()) {
507                         _has_broadcast_info = true;
508                 }
509
510                 supports_tagging = format->supports_tagging ();
511                 _channel_limit = format->get_channel_limit();
512
513                 _format_name = format->name();
514         } else {
515                 set_format_id (F_None);
516                 set_type (T_None);
517                 set_extension ("");
518                 _has_broadcast_info = false;
519                 has_sample_format = false;
520                 supports_tagging = false;
521                 _channel_limit = 0;
522                 _format_name = "";
523         }
524 }
525
526 string
527 ExportFormatSpecification::description ()
528 {
529         string desc;
530
531         desc = _name + ": ";
532
533         if (_normalize) {
534                 desc += _("normalize, ");
535         }
536
537         if (_trim_beginning && _trim_end) {
538                 desc += _("trim, ");
539         } else if (_trim_beginning) {
540                 desc += _("trim start, ");
541         } else if (_trim_end) {
542                 desc += _("trim end, ");
543         }
544
545         desc += _format_name + ", ";
546
547         if (has_sample_format) {
548                 desc += HasSampleFormat::get_sample_format_name (sample_format())  + ", ";
549         }
550
551         switch (sample_rate()) {
552           case SR_22_05:
553                 desc += "22,5 kHz";
554                 break;
555           case SR_44_1:
556                 desc += "44,1 kHz";
557                 break;
558           case SR_48:
559                 desc += "48 kHz";
560                 break;
561           case SR_88_2:
562                 desc += "88,2 kHz";
563                 break;
564           case SR_96:
565                 desc += "96 kHz";
566                 break;
567           case SR_192:
568                 desc += "192 kHz";
569                 break;
570           case SR_Session:
571                 desc += _("Session rate");
572                 break;
573           case SR_None:
574                 break;
575         }
576
577         if (_with_toc) {
578                 desc += ", TOC";
579         }
580
581         if (_with_cue) {
582                 desc += ", CUE";
583         }
584
585         return desc;
586 }
587
588 void
589 ExportFormatSpecification::add_option (XMLNode * node, std::string const & name, std::string const & value)
590 {
591         node = node->add_child ("Option");
592         node->add_property ("name", name);
593         node->add_property ("value", value);
594 }
595
596 std::string
597 ExportFormatSpecification::get_option (XMLNode const * node, std::string const & name)
598 {
599         XMLNodeList list (node->children ("Option"));
600
601         for (XMLNodeList::iterator it = list.begin(); it != list.end(); ++it) {
602                 XMLProperty * prop = (*it)->property ("name");
603                 if (prop && !name.compare (prop->value())) {
604                         prop = (*it)->property ("value");
605                         if (prop) {
606                                 return prop->value();
607                         }
608                 }
609         }
610
611         std::cerr << "Could not load encoding option \"" << name << "\" for export format" << std::endl;
612
613         return "";
614 }
615
616 }; // namespace ARDOUR