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