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