3f6e2b83c89908e3d4e59c4b54ee7399d1353fd1
[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 {
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);
176 }
177
178 ExportFormatSpecification::ExportFormatSpecification (Session & s, XMLNode const & state)
179         : session (s)
180         , _silence_beginning (s)
181         , _silence_end (s)
182 {
183         _silence_beginning.type = Time::Timecode;
184         _silence_end.type = Time::Timecode;
185
186         set_state (state);
187 }
188
189 ExportFormatSpecification::ExportFormatSpecification (ExportFormatSpecification const & other)
190         : ExportFormatBase(other)
191         , session (other.session)
192         , _silence_beginning (other.session)
193         , _silence_end (other.session)
194 {
195         set_name (other.name() + " (copy)");
196
197         _format_name = other._format_name;
198         has_sample_format = other.has_sample_format;
199
200         supports_tagging = other.supports_tagging;
201         _has_broadcast_info = other._has_broadcast_info;
202         _channel_limit = other._channel_limit;
203
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());
210
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());
217
218         set_tag (other.tag());
219
220         set_silence_beginning (other.silence_beginning_time());
221         set_silence_end (other.silence_end_time());
222
223         set_extension(other.extension());
224 }
225
226 ExportFormatSpecification::~ExportFormatSpecification ()
227 {
228 }
229
230 XMLNode &
231 ExportFormatSpecification::get_state ()
232 {
233         XMLNode * node;
234         XMLNode * root = new XMLNode ("ExportFormatSpecification");
235
236         root->add_property ("name", _name);
237         root->add_property ("id", _id.to_s());
238
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));
246
247         node = root->add_child ("SampleRate");
248         node->add_property ("rate", to_string (sample_rate(), std::dec));
249
250         node = root->add_child ("SRCQuality");
251         node->add_property ("quality", enum_2_string (src_quality()));
252
253         XMLNode * enc_opts = root->add_child ("EncodingOptions");
254
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");
260
261         XMLNode * processing = root->add_child ("Processing");
262
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));
266
267         XMLNode * silence = processing->add_child ("Silence");
268         XMLNode * start = silence->add_child ("Start");
269         XMLNode * end = silence->add_child ("End");
270
271         node = start->add_child ("Trim");
272         node->add_property ("enabled", trim_beginning() ? "true" : "false");
273
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());
277
278         node = end->add_child ("Trim");
279         node->add_property ("enabled", trim_end() ? "true" : "false");
280
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());
284
285         return *root;
286 }
287
288 int
289 ExportFormatSpecification::set_state (const XMLNode & root)
290 {
291         XMLProperty const * prop;
292         XMLNode const * child;
293         string value;
294
295         if ((prop = root.property ("name"))) {
296                 _name = prop->value();
297         }
298
299         if ((prop = root.property ("id"))) {
300                 _id = prop->value();
301         }
302
303         /* Encoding and SRC */
304
305         if ((child = root.child ("Encoding"))) {
306                 if ((prop = child->property ("id"))) {
307                         set_format_id ((FormatId) string_2_enum (prop->value(), FormatId));
308                 }
309
310                 if ((prop = child->property ("type"))) {
311                         set_type ((Type) string_2_enum (prop->value(), Type));
312                 }
313
314                 if ((prop = child->property ("extension"))) {
315                         set_extension (prop->value());
316                 }
317
318                 if ((prop = child->property ("name"))) {
319                         _format_name = prop->value();
320                 }
321
322                 if ((prop = child->property ("has-sample-format"))) {
323                         has_sample_format = !prop->value().compare ("true");
324                 }
325
326                 if ((prop = child->property ("channel-limit"))) {
327                         _channel_limit = atoi (prop->value());
328                 }
329         }
330
331         if ((child = root.child ("SampleRate"))) {
332                 if ((prop = child->property ("rate"))) {
333                         set_sample_rate ( (SampleRate) string_2_enum (prop->value(), SampleRate));
334                 }
335         }
336
337         if ((child = root.child ("SRCQuality"))) {
338                 if ((prop = child->property ("quality"))) {
339                         _src_quality = (SRCQuality) string_2_enum (prop->value(), SRCQuality);
340                 }
341         }
342
343         /* Encoding options */
344
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")));
351         }
352
353         /* Processing */
354
355         XMLNode const * proc = root.child ("Processing");
356         if (!proc) { std::cerr << X_("Could not load processing for export format") << std::endl; return -1; }
357
358         if ((child = proc->child ("Normalize"))) {
359                 if ((prop = child->property ("enabled"))) {
360                         _normalize = (!prop->value().compare ("true"));
361                 }
362
363                 if ((prop = child->property ("target"))) {
364                         _normalize_target = atof (prop->value());
365                 }
366         }
367
368         XMLNode const * silence = proc->child ("Silence");
369         if (!silence) { std::cerr << X_("Could not load silence for export format") << std::endl; return -1; }
370
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; }
374
375         /* Silence start */
376
377         if ((child = start->child ("Trim"))) {
378                 if ((prop = child->property ("enabled"))) {
379                         _trim_beginning = (!prop->value().compare ("true"));
380                 }
381         }
382
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);
388                                 }
389                         } else {
390                                 _silence_beginning.type = Time::Timecode;
391                         }
392                 }
393         }
394
395         /* Silence end */
396
397         if ((child = end->child ("Trim"))) {
398                 if ((prop = child->property ("enabled"))) {
399                         _trim_end = (!prop->value().compare ("true"));
400                 }
401         }
402
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);
408                                 }
409                         } else {
410                                 _silence_end.type = Time::Timecode;
411                         }
412                 }
413         }
414
415         return 0;
416 }
417
418 bool
419 ExportFormatSpecification::is_compatible_with (ExportFormatCompatibility const & compatibility) const
420 {
421         boost::shared_ptr<ExportFormatBase> intersection = get_intersection (compatibility);
422
423         if (intersection->formats_empty() && format_id() != 0) {
424                 return false;
425         }
426
427         if (intersection->endiannesses_empty() && endianness() != E_FileDefault) {
428                 return false;
429         }
430
431         if (intersection->sample_rates_empty() && sample_rate() != SR_None) {
432                 return false;
433         }
434
435         if (intersection->sample_formats_empty() && sample_format() != SF_None) {
436                 return false;
437         }
438
439         if (intersection->qualities_empty() && quality() != Q_None) {
440                 return false;
441         }
442
443         return true;
444 }
445
446 bool
447 ExportFormatSpecification::is_complete () const
448 {
449         if (type() == T_None) {
450                 return false;
451         }
452
453         if (!format_id()) {
454                 return false;
455         }
456
457         if (!sample_rate()) {
458                 return false;
459         }
460
461         if (has_sample_format) {
462                 if (sample_format() == SF_None) {
463                         return false;
464                 }
465         }
466
467         return true;
468 }
469
470 void
471 ExportFormatSpecification::set_format (boost::shared_ptr<ExportFormat> format)
472 {
473         if (format) {
474                 set_format_id (format->get_format_id ());
475                 set_type (format->get_type());
476                 set_extension (format->extension());
477
478                 if (format->get_explicit_sample_format()) {
479                         set_sample_format (format->get_explicit_sample_format());
480                 }
481
482                 if (format->has_sample_format()) {
483                         has_sample_format = true;
484                 }
485
486                 if (format->has_broadcast_info()) {
487                         _has_broadcast_info = true;
488                 }
489
490                 supports_tagging = format->supports_tagging ();
491                 _channel_limit = format->get_channel_limit();
492
493                 _format_name = format->name();
494         } else {
495                 set_format_id (F_None);
496                 set_type (T_None);
497                 set_extension ("");
498                 _has_broadcast_info = false;
499                 has_sample_format = false;
500                 supports_tagging = false;
501                 _channel_limit = 0;
502                 _format_name = "";
503         }
504 }
505
506 string
507 ExportFormatSpecification::description ()
508 {
509         string desc;
510
511         desc = _name + ": ";
512
513         if (_normalize) {
514                 desc += _("normalize, ");
515         }
516
517         if (_trim_beginning && _trim_end) {
518                 desc += _("trim, ");
519         } else if (_trim_beginning) {
520                 desc += _("trim start, ");
521         } else if (_trim_end) {
522                 desc += "trim end, ";
523         }
524
525         desc += _format_name + ", ";
526
527         if (has_sample_format) {
528                 desc += HasSampleFormat::get_sample_format_name (sample_format())  + ", ";
529         }
530
531         switch (sample_rate()) {
532           case SR_22_05:
533                 desc += "22,5 kHz";
534                 break;
535           case SR_44_1:
536                 desc += "44,1 kHz";
537                 break;
538           case SR_48:
539                 desc += "48 kHz";
540                 break;
541           case SR_88_2:
542                 desc += "88,2 kHz";
543                 break;
544           case SR_96:
545                 desc += "96 kHz";
546                 break;
547           case SR_192:
548                 desc += "192 kHz";
549                 break;
550           case SR_Session:
551                 desc += _("Session rate");
552                 break;
553           case SR_None:
554                 break;
555         }
556
557         return desc;
558 }
559
560 void
561 ExportFormatSpecification::add_option (XMLNode * node, std::string const & name, std::string const & value)
562 {
563         node = node->add_child ("Option");
564         node->add_property ("name", name);
565         node->add_property ("value", value);
566 }
567
568 std::string
569 ExportFormatSpecification::get_option (XMLNode const * node, std::string const & name)
570 {
571         XMLNodeList list (node->children ("Option"));
572
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");
577                         if (prop) {
578                                 return prop->value();
579                         }
580                 }
581         }
582
583         std::cerr << "Could not load encoding option \"" << name << "\" for export format" << std::endl;
584
585         return "";
586 }
587
588 }; // namespace ARDOUR