Normalise XML attribute names to be camelCase (#2241).
[dcpomatic.git] / src / lib / audio_mapping.cc
1 /*
2     Copyright (C) 2013-2021 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
22 #include "audio_mapping.h"
23 #include "audio_processor.h"
24 #include "constants.h"
25 #include "dcpomatic_assert.h"
26 #include "digester.h"
27 #include "util.h"
28 #include <dcp/raw_convert.h>
29 #include <dcp/warnings.h>
30 #include <libcxml/cxml.h>
31 LIBDCP_DISABLE_WARNINGS
32 #include <libxml++/libxml++.h>
33 LIBDCP_ENABLE_WARNINGS
34 #include <boost/regex.hpp>
35 #include <iostream>
36
37
38 using std::list;
39 using std::cout;
40 using std::make_pair;
41 using std::pair;
42 using std::string;
43 using std::min;
44 using std::vector;
45 using std::abs;
46 using std::shared_ptr;
47 using std::dynamic_pointer_cast;
48 using boost::optional;
49 using dcp::raw_convert;
50
51
52 /** Create an empty AudioMapping.
53  *  @param input_channels Number of input channels.
54  *  @param output_channels Number of output channels.
55  */
56 AudioMapping::AudioMapping (int input_channels, int output_channels)
57 {
58         setup (input_channels, output_channels);
59 }
60
61
62 void
63 AudioMapping::setup (int input_channels, int output_channels)
64 {
65         _input_channels = input_channels;
66         _output_channels = output_channels;
67
68         _gain.resize (_input_channels);
69         for (int i = 0; i < _input_channels; ++i) {
70                 _gain[i].resize (_output_channels);
71         }
72
73         make_zero ();
74 }
75
76
77 void
78 AudioMapping::make_zero ()
79 {
80         for (int i = 0; i < _input_channels; ++i) {
81                 for (int j = 0; j < _output_channels; ++j) {
82                         _gain[i][j] = 0;
83                 }
84         }
85 }
86
87
88 struct ChannelRegex
89 {
90         ChannelRegex (string regex_, int channel_)
91                 : regex (regex_)
92                 , channel (channel_)
93         {}
94
95         string regex;
96         int channel;
97 };
98
99
100 void
101 AudioMapping::make_default (AudioProcessor const * processor, optional<boost::filesystem::path> filename)
102 {
103         static ChannelRegex const regex[] = {
104                 ChannelRegex(".*[\\._-]L[\\._-].*", 0),
105                 ChannelRegex(".*[\\._-]R[\\._-].*", 1),
106                 ChannelRegex(".*[\\._-]C[\\._-].*", 2),
107                 ChannelRegex(".*[\\._-]Lfe[\\._-].*", 3),
108                 ChannelRegex(".*[\\._-]LFE[\\._-].*", 3),
109                 ChannelRegex(".*[\\._-]Lss[\\._-].*", 4),
110                 ChannelRegex(".*[\\._-]Lsr[\\._-].*", 6),
111                 ChannelRegex(".*[\\._-]Ls[\\._-].*", 4),
112                 ChannelRegex(".*[\\._-]Rss[\\._-].*", 5),
113                 ChannelRegex(".*[\\._-]Rsr[\\._-].*", 7),
114                 ChannelRegex(".*[\\._-]Rs[\\._-].*", 5),
115         };
116
117         static int const regexes = sizeof(regex) / sizeof(*regex);
118
119         if (processor) {
120                 processor->make_audio_mapping_default (*this);
121         } else {
122                 make_zero ();
123                 if (input_channels() == 1) {
124                         bool guessed = false;
125
126                         /* See if we can guess where this stream should go */
127                         if (filename) {
128                                 for (int i = 0; i < regexes; ++i) {
129                                         boost::regex e (regex[i].regex, boost::regex::icase);
130                                         if (boost::regex_match(filename->filename().string(), e) && regex[i].channel < output_channels()) {
131                                                 set (0, regex[i].channel, 1);
132                                                 guessed = true;
133                                         }
134                                 }
135                         }
136
137                         if (!guessed) {
138                                 /* If we have no idea, just put it on centre */
139                                 set (0, static_cast<int>(dcp::Channel::CENTRE), 1);
140                         }
141                 } else {
142                         /* 1:1 mapping */
143                         for (int i = 0; i < min (input_channels(), output_channels()); ++i) {
144                                 set (i, i, 1);
145                         }
146                 }
147         }
148 }
149
150
151 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
152 {
153         if (state_version < 32) {
154                 setup (node->number_child<int>("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
155         } else {
156                 setup (node->number_child<int>("InputChannels"), node->number_child<int>("OutputChannels"));
157         }
158
159         if (state_version <= 5) {
160                 /* Old-style: on/off mapping */
161                 for (auto i: node->node_children ("Map")) {
162                         set (i->number_child<int>("ContentIndex"), i->number_child<int>("DCP"), 1);
163                 }
164         } else {
165                 for (auto i: node->node_children("Gain")) {
166                         if (state_version < 32) {
167                                 set (
168                                         i->number_attribute<int>("Content"),
169                                         i->number_attribute<int>("DCP"),
170                                         raw_convert<float>(i->content())
171                                         );
172                         } else {
173                                 set (
174                                         number_attribute<int>(i, "Input", "input"),
175                                         number_attribute<int>(i, "Output", "output"),
176                                         raw_convert<float>(i->content())
177                                         );
178                         }
179                 }
180         }
181 }
182
183
184 void
185 AudioMapping::set (dcp::Channel input_channel, int output_channel, float g)
186 {
187         set (static_cast<int>(input_channel), output_channel, g);
188 }
189
190
191 void
192 AudioMapping::set (int input_channel, dcp::Channel output_channel, float g)
193 {
194         set (input_channel, static_cast<int>(output_channel), g);
195 }
196
197
198 void
199 AudioMapping::set (int input_channel, int output_channel, float g)
200 {
201         DCPOMATIC_ASSERT (input_channel < int(_gain.size()));
202         DCPOMATIC_ASSERT (output_channel < int(_gain[0].size()));
203         _gain[input_channel][output_channel] = g;
204 }
205
206
207 float
208 AudioMapping::get (int input_channel, dcp::Channel output_channel) const
209 {
210         return get (input_channel, static_cast<int>(output_channel));
211 }
212
213
214 float
215 AudioMapping::get (int input_channel, int output_channel) const
216 {
217         DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
218         DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
219         return _gain[input_channel][output_channel];
220 }
221
222
223 void
224 AudioMapping::as_xml (xmlpp::Node* node) const
225 {
226         node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
227         node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
228
229         for (int c = 0; c < _input_channels; ++c) {
230                 for (int d = 0; d < _output_channels; ++d) {
231                         auto t = node->add_child ("Gain");
232                         t->set_attribute("input", raw_convert<string>(c));
233                         t->set_attribute("output", raw_convert<string>(d));
234                         t->add_child_text (raw_convert<string> (get (c, d)));
235                 }
236         }
237 }
238
239
240 /** @return a string which is unique for a given AudioMapping configuration, for
241  *  differentiation between different AudioMappings.
242  */
243 string
244 AudioMapping::digest () const
245 {
246         Digester digester;
247         digester.add (_input_channels);
248         digester.add (_output_channels);
249         for (int i = 0; i < _input_channels; ++i) {
250                 for (int j = 0; j < _output_channels; ++j) {
251                         digester.add (_gain[i][j]);
252                 }
253         }
254
255         return digester.get ();
256 }
257
258
259 list<int>
260 AudioMapping::mapped_output_channels () const
261 {
262         static float const minus_96_db = 0.000015849;
263
264         list<int> mapped;
265
266         for (auto const& i: _gain) {
267                 for (auto j: dcp::used_audio_channels()) {
268                         if (abs(i[static_cast<int>(j)]) > minus_96_db) {
269                                 mapped.push_back (static_cast<int>(j));
270                         }
271                 }
272         }
273
274         mapped.sort ();
275         mapped.unique ();
276
277         return mapped;
278 }
279
280
281 void
282 AudioMapping::unmap_all ()
283 {
284         for (auto& i: _gain) {
285                 for (auto& j: i) {
286                         j = 0;
287                 }
288         }
289 }