906e79bd89b089a777d8c727fd42c886f066c69a
[dcpomatic.git] / src / lib / audio_mapping.cc
1 /*
2     Copyright (C) 2013-2015 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 "audio_mapping.h"
21 #include "util.h"
22 #include "md5_digester.h"
23 #include "raw_convert.h"
24 #include <libcxml/cxml.h>
25 #include <libxml++/libxml++.h>
26 #include <iostream>
27
28 using std::list;
29 using std::cout;
30 using std::make_pair;
31 using std::pair;
32 using std::string;
33 using std::min;
34 using std::vector;
35 using std::abs;
36 using boost::shared_ptr;
37 using boost::dynamic_pointer_cast;
38
39 AudioMapping::AudioMapping ()
40         : _input_channels (0)
41         , _output_channels (0)
42 {
43
44 }
45
46 /** Create an empty AudioMapping.
47  *  @param input_channels Number of input channels.
48  *  @param output_channels Number of output channels.
49  */
50 AudioMapping::AudioMapping (int input_channels, int output_channels)
51 {
52         setup (input_channels, output_channels);
53 }
54
55 void
56 AudioMapping::setup (int input_channels, int output_channels)
57 {
58         _input_channels = input_channels;
59         _output_channels = output_channels;
60
61         _gain.resize (_input_channels);
62         for (int i = 0; i < _input_channels; ++i) {
63                 _gain[i].resize (_output_channels);
64         }
65
66         make_zero ();
67 }
68
69 void
70 AudioMapping::make_zero ()
71 {
72         for (int i = 0; i < _input_channels; ++i) {
73                 for (int j = 0; j < _output_channels; ++j) {
74                         _gain[i][j] = 0;
75                 }
76         }
77 }
78
79 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
80 {
81         if (state_version < 32) {
82                 setup (node->number_child<int> ("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
83         } else {
84                 setup (node->number_child<int> ("InputChannels"), node->number_child<int> ("OutputChannels"));
85         }
86
87         if (state_version <= 5) {
88                 /* Old-style: on/off mapping */
89                 list<cxml::NodePtr> const c = node->node_children ("Map");
90                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
91                         set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
92                 }
93         } else {
94                 list<cxml::NodePtr> const c = node->node_children ("Gain");
95                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
96                         if (state_version < 32) {
97                                 set (
98                                         (*i)->number_attribute<int> ("Content"),
99                                         static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
100                                         raw_convert<float> ((*i)->content ())
101                                         );
102                         } else {
103                                 set (
104                                         (*i)->number_attribute<int> ("Input"),
105                                         (*i)->number_attribute<int> ("Output"),
106                                         raw_convert<float> ((*i)->content ())
107                                         );
108                         }
109                 }
110         }
111 }
112
113 void
114 AudioMapping::set (int input_channel, int output_channel, float g)
115 {
116         _gain[input_channel][output_channel] = g;
117 }
118
119 float
120 AudioMapping::get (int input_channel, int output_channel) const
121 {
122         DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
123         DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
124         return _gain[input_channel][output_channel];
125 }
126
127 void
128 AudioMapping::as_xml (xmlpp::Node* node) const
129 {
130         node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
131         node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
132
133         for (int c = 0; c < _input_channels; ++c) {
134                 for (int d = 0; d < _output_channels; ++d) {
135                         xmlpp::Element* t = node->add_child ("Gain");
136                         t->set_attribute ("Input", raw_convert<string> (c));
137                         t->set_attribute ("Output", raw_convert<string> (d));
138                         t->add_child_text (raw_convert<string> (get (c, d)));
139                 }
140         }
141 }
142
143 /** @return a string which is unique for a given AudioMapping configuration, for
144  *  differentiation between different AudioMappings.
145  */
146 string
147 AudioMapping::digest () const
148 {
149         MD5Digester digester;
150         digester.add (_input_channels);
151         digester.add (_output_channels);
152         for (int i = 0; i < _input_channels; ++i) {
153                 for (int j = 0; j < _output_channels; ++j) {
154                         digester.add (_gain[i][j]);
155                 }
156         }
157
158         return digester.get ();
159 }
160
161 list<int>
162 AudioMapping::mapped_output_channels () const
163 {
164         static float const minus_96_db = 0.000015849;
165
166         list<int> mapped;
167
168         for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
169                 for (size_t j = 0; j < i->size(); ++j) {
170                         if (abs ((*i)[j]) > minus_96_db) {
171                                 mapped.push_back (j);
172                         }
173                 }
174         }
175
176         mapped.sort ();
177         mapped.unique ();
178
179         return mapped;
180 }
181
182 void
183 AudioMapping::unmap_all ()
184 {
185         for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
186                 for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {
187                         *j = 0;
188                 }
189         }
190 }