Merge.
[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 #include "util.h"
25
26 using std::list;
27 using std::cout;
28 using std::make_pair;
29 using std::pair;
30 using std::string;
31 using boost::shared_ptr;
32 using boost::lexical_cast;
33 using boost::dynamic_pointer_cast;
34
35 AudioMapping::AudioMapping ()
36         : _content_channels (0)
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         setup (c);
47 }
48
49 void
50 AudioMapping::setup (int c)
51 {
52         _content_channels = c;
53         
54         _gain.resize (_content_channels);
55         for (int i = 0; i < _content_channels; ++i) {
56                 _gain[i].resize (MAX_AUDIO_CHANNELS);
57         }
58 }
59
60 void
61 AudioMapping::make_default ()
62 {
63         for (int i = 0; i < _content_channels; ++i) {
64                 for (int j = 0; j < MAX_AUDIO_CHANNELS; ++j) {
65                         _gain[i][j] = 0;
66                 }
67         }
68
69         if (_content_channels == 1) {
70                 /* Mono -> Centre */
71                 set (0, dcp::CENTRE, 1);
72         } else {
73                 /* 1:1 mapping */
74                 for (int i = 0; i < _content_channels; ++i) {
75                         set (i, static_cast<dcp::Channel> (i), 1);
76                 }
77         }
78 }
79
80 AudioMapping::AudioMapping (shared_ptr<const cxml::Node> node, int state_version)
81 {
82         setup (node->number_child<int> ("ContentChannels"));
83
84         if (state_version <= 5) {
85                 /* Old-style: on/off mapping */
86                 list<cxml::NodePtr> const c = node->node_children ("Map");
87                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
88                         set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
89                 }
90         } else {
91                 list<cxml::NodePtr> const c = node->node_children ("Gain");
92                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
93                         set (
94                                 (*i)->number_attribute<int> ("Content"),
95                                 static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
96                                 lexical_cast<float> ((*i)->content ())
97                                 );
98                 }
99         }
100 }
101
102 void
103 AudioMapping::set (int c, dcp::Channel d, float g)
104 {
105         _gain[c][d] = g;
106 }
107
108 float
109 AudioMapping::get (int c, dcp::Channel d) const
110 {
111         return _gain[c][d];
112 }
113
114 void
115 AudioMapping::as_xml (xmlpp::Node* node) const
116 {
117         node->add_child ("ContentChannels")->add_child_text (lexical_cast<string> (_content_channels));
118
119         for (int c = 0; c < _content_channels; ++c) {
120                 for (int d = 0; d < MAX_AUDIO_CHANNELS; ++d) {
121                         xmlpp::Element* t = node->add_child ("Gain");
122                         t->set_attribute ("Content", lexical_cast<string> (c));
123                         t->set_attribute ("DCP", lexical_cast<string> (d));
124                         t->add_child_text (lexical_cast<string> (get (c, static_cast<dcp::Channel> (d))));
125                 }
126         }
127 }