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 <libxml++/libxml++.h>
22 #include <libcxml/cxml.h>
23 #include "audio_mapping.h"
24
25 using std::list;
26 using std::cout;
27 using std::make_pair;
28 using std::pair;
29 using std::string;
30 using boost::shared_ptr;
31 using boost::lexical_cast;
32 using boost::dynamic_pointer_cast;
33
34 AudioMapping::AudioMapping ()
35 {
36
37 }
38
39 /** Create a default AudioMapping for a given channel count.
40  *  @param c Number of channels.
41  */
42 AudioMapping::AudioMapping (int c)
43 {
44         if (c == 1) {
45                 /* Mono -> Centre */
46                 add (0, libdcp::CENTRE);
47         } else {
48                 /* 1:1 mapping */
49                 for (int i = 0; i < c; ++i) {
50                         add (i, static_cast<libdcp::Channel> (i));
51                 }
52         }
53 }
54
55 AudioMapping::AudioMapping (shared_ptr<const cxml::Node> node)
56 {
57         list<shared_ptr<cxml::Node> > const c = node->node_children ("Map");
58         for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
59                 add ((*i)->number_child<int> ("ContentIndex"), static_cast<libdcp::Channel> ((*i)->number_child<int> ("DCP")));
60         }
61 }
62
63 void
64 AudioMapping::add (int c, libdcp::Channel d)
65 {
66         _content_to_dcp.push_back (make_pair (c, d));
67 }
68
69 list<int>
70 AudioMapping::dcp_to_content (libdcp::Channel d) const
71 {
72         list<int> c;
73         for (list<pair<int, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
74                 if (i->second == d) {
75                         c.push_back (i->first);
76                 }
77         }
78
79         return c;
80 }
81
82 list<int>
83 AudioMapping::content_channels () const
84 {
85         list<int> c;
86         for (list<pair<int, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
87                 if (find (c.begin(), c.end(), i->first) == c.end ()) {
88                         c.push_back (i->first);
89                 }
90         }
91
92         return c;
93 }
94
95 list<libdcp::Channel>
96 AudioMapping::content_to_dcp (int c) const
97 {
98         list<libdcp::Channel> d;
99         for (list<pair<int, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
100                 if (i->first == c) {
101                         d.push_back (i->second);
102                 }
103         }
104
105         return d;
106 }
107
108 void
109 AudioMapping::as_xml (xmlpp::Node* node) const
110 {
111         for (list<pair<int, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
112                 xmlpp::Node* t = node->add_child ("Map");
113                 t->add_child ("ContentIndex")->add_child_text (lexical_cast<string> (i->first));
114                 t->add_child ("DCP")->add_child_text (lexical_cast<string> (i->second));
115         }
116 }