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