Use cxml::ConstNodePtr.
[dcpomatic.git] / src / lib / audio_mapping.cc
1 /*
2     Copyright (C) 2013-2014 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 <libxml++/libxml++.h>
21 #include <libcxml/cxml.h>
22 #include <dcp/raw_convert.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 std::min;
32 using boost::shared_ptr;
33 using boost::dynamic_pointer_cast;
34 using dcp::raw_convert;
35
36 AudioMapping::AudioMapping ()
37         : _content_channels (0)
38 {
39
40 }
41
42 /** Create a default AudioMapping for a given channel count.
43  *  @param channels Number of channels.
44  */
45 AudioMapping::AudioMapping (int channels)
46 {
47         setup (channels);
48 }
49
50 void
51 AudioMapping::setup (int c)
52 {
53         _content_channels = c;
54         
55         _gain.resize (_content_channels);
56         for (int i = 0; i < _content_channels; ++i) {
57                 _gain[i].resize (MAX_DCP_AUDIO_CHANNELS);
58         }
59 }
60
61 void
62 AudioMapping::make_default ()
63 {
64         for (int i = 0; i < _content_channels; ++i) {
65                 for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) {
66                         _gain[i][j] = 0;
67                 }
68         }
69
70         if (_content_channels == 1) {
71                 /* Mono -> Centre */
72                 set (0, dcp::CENTRE, 1);
73         } else {
74                 /* 1:1 mapping */
75                 for (int i = 0; i < min (_content_channels, MAX_DCP_AUDIO_CHANNELS); ++i) {
76                         set (i, static_cast<dcp::Channel> (i), 1);
77                 }
78         }
79 }
80
81 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
82 {
83         setup (node->number_child<int> ("ContentChannels"));
84
85         if (state_version <= 5) {
86                 /* Old-style: on/off mapping */
87                 list<cxml::NodePtr> const c = node->node_children ("Map");
88                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
89                         set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
90                 }
91         } else {
92                 list<cxml::NodePtr> const c = node->node_children ("Gain");
93                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
94                         set (
95                                 (*i)->number_attribute<int> ("Content"),
96                                 static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
97                                 raw_convert<float> ((*i)->content ())
98                                 );
99                 }
100         }
101 }
102
103 void
104 AudioMapping::set (int c, dcp::Channel d, float g)
105 {
106         _gain[c][d] = g;
107 }
108
109 float
110 AudioMapping::get (int c, dcp::Channel d) const
111 {
112         return _gain[c][d];
113 }
114
115 void
116 AudioMapping::as_xml (xmlpp::Node* node) const
117 {
118         node->add_child ("ContentChannels")->add_child_text (raw_convert<string> (_content_channels));
119
120         for (int c = 0; c < _content_channels; ++c) {
121                 for (int d = 0; d < MAX_DCP_AUDIO_CHANNELS; ++d) {
122                         xmlpp::Element* t = node->add_child ("Gain");
123                         t->set_attribute ("Content", raw_convert<string> (c));
124                         t->set_attribute ("DCP", raw_convert<string> (d));
125                         t->add_child_text (raw_convert<string> (get (c, static_cast<dcp::Channel> (d))));
126                 }
127         }
128 }