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