Merge master.
[dcpomatic.git] / src / lib / audio_mapping.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/lexical_cast.hpp>
21 #include <libcxml/cxml.h>
22 #include "audio_mapping.h"
23
24 using std::list;
25 using std::cout;
26 using std::make_pair;
27 using std::pair;
28 using std::string;
29 using boost::shared_ptr;
30 using boost::lexical_cast;
31 using boost::dynamic_pointer_cast;
32
33 void
34 AudioMapping::add (Channel c, libdcp::Channel d)
35 {
36         _content_to_dcp.push_back (make_pair (c, d));
37 }
38
39 /* XXX: this is grotty */
40 int
41 AudioMapping::dcp_channels () const
42 {
43         for (list<pair<Channel, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
44                 if (((int) i->second) >= 2) {
45                         return 6;
46                 }
47         }
48
49         return 2;
50 }
51
52 list<AudioMapping::Channel>
53 AudioMapping::dcp_to_content (libdcp::Channel d) const
54 {
55         list<AudioMapping::Channel> c;
56         for (list<pair<Channel, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
57                 if (i->second == d) {
58                         c.push_back (i->first);
59                 }
60         }
61
62         return c;
63 }
64
65 list<AudioMapping::Channel>
66 AudioMapping::content_channels () const
67 {
68         list<AudioMapping::Channel> c;
69         for (list<pair<Channel, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
70                 if (find (c.begin(), c.end(), i->first) == c.end ()) {
71                         c.push_back (i->first);
72                 }
73         }
74
75         return c;
76 }
77
78 list<libdcp::Channel>
79 AudioMapping::content_to_dcp (Channel c) const
80 {
81         list<libdcp::Channel> d;
82         for (list<pair<Channel, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
83                 if (i->first == c) {
84                         d.push_back (i->second);
85                 }
86         }
87
88         return d;
89 }
90
91 void
92 AudioMapping::as_xml (xmlpp::Node* node) const
93 {
94         for (list<pair<Channel, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
95                 xmlpp::Node* t = node->add_child ("Map");
96                 shared_ptr<const AudioContent> c = i->first.content.lock ();
97                 t->add_child ("Content")->add_child_text (c->file().string ());
98                 t->add_child ("ContentIndex")->add_child_text (lexical_cast<string> (i->first.index));
99                 t->add_child ("DCP")->add_child_text (lexical_cast<string> (i->second));
100         }
101 }
102
103 void
104 AudioMapping::set_from_xml (ContentList const & content, shared_ptr<const cxml::Node> node)
105 {
106         list<shared_ptr<cxml::Node> > const c = node->node_children ("Map");
107         for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
108                 string const c = (*i)->string_child ("Content");
109                 ContentList::const_iterator j = content.begin ();
110                 while (j != content.end() && (*j)->file().string() != c) {
111                         ++j;
112                 }
113
114                 if (j == content.end ()) {
115                         continue;
116                 }
117
118                 shared_ptr<const AudioContent> ac = dynamic_pointer_cast<AudioContent> (*j);
119                 assert (ac);
120
121                 add (AudioMapping::Channel (ac, (*i)->number_child<int> ("ContentIndex")), static_cast<libdcp::Channel> ((*i)->number_child<int> ("DCP")));
122         }
123 }
124
125 bool
126 operator== (AudioMapping::Channel const & a, AudioMapping::Channel const & b)
127 {
128         shared_ptr<const AudioContent> sa = a.content.lock ();
129         shared_ptr<const AudioContent> sb = b.content.lock ();
130         return sa == sb && a.index == b.index;
131 }