6e8c4e30d3641f04cfa96c3961155d4f7168da2f
[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         _gain.resize(input_channels);
66         for (int i = 0; i < input_channels; ++i) {
67                 _gain[i].resize(output_channels);
68         }
69
70         make_zero ();
71 }
72
73
74 void
75 AudioMapping::make_zero ()
76 {
77         for (auto& input: _gain) {
78                 for (auto& output: input) {
79                         output = 0;
80                 }
81         }
82 }
83
84
85 struct ChannelRegex
86 {
87         ChannelRegex (string regex_, int channel_)
88                 : regex (regex_)
89                 , channel (channel_)
90         {}
91
92         string regex;
93         int channel;
94 };
95
96
97 void
98 AudioMapping::make_default (AudioProcessor const * processor, optional<boost::filesystem::path> filename)
99 {
100         static ChannelRegex const regex[] = {
101                 ChannelRegex(".*[\\._-]L[\\._-].*", 0),
102                 ChannelRegex(".*[\\._-]R[\\._-].*", 1),
103                 ChannelRegex(".*[\\._-]C[\\._-].*", 2),
104                 ChannelRegex(".*[\\._-]Lfe[\\._-].*", 3),
105                 ChannelRegex(".*[\\._-]LFE[\\._-].*", 3),
106                 ChannelRegex(".*[\\._-]Lss[\\._-].*", 4),
107                 ChannelRegex(".*[\\._-]Lsr[\\._-].*", 6),
108                 ChannelRegex(".*[\\._-]Lrs[\\._-].*", 6),
109                 ChannelRegex(".*[\\._-]Ls[\\._-].*", 4),
110                 ChannelRegex(".*[\\._-]Rss[\\._-].*", 5),
111                 ChannelRegex(".*[\\._-]Rsr[\\._-].*", 7),
112                 ChannelRegex(".*[\\._-]Rrs[\\._-].*", 7),
113                 ChannelRegex(".*[\\._-]Rs[\\._-].*", 5),
114         };
115
116         static int const regexes = sizeof(regex) / sizeof(*regex);
117
118         if (processor) {
119                 processor->make_audio_mapping_default (*this);
120         } else {
121                 make_zero ();
122                 if (input_channels() == 1) {
123                         bool guessed = false;
124
125                         /* See if we can guess where this stream should go */
126                         if (filename) {
127                                 for (int i = 0; i < regexes; ++i) {
128                                         boost::regex e (regex[i].regex, boost::regex::icase);
129                                         if (boost::regex_match(filename->filename().string(), e) && regex[i].channel < output_channels()) {
130                                                 set (0, regex[i].channel, 1);
131                                                 guessed = true;
132                                         }
133                                 }
134                         }
135
136                         if (!guessed) {
137                                 /* If we have no idea, just put it on centre */
138                                 set (0, static_cast<int>(dcp::Channel::CENTRE), 1);
139                         }
140                 } else {
141                         /* 1:1 mapping */
142                         for (int i = 0; i < min (input_channels(), output_channels()); ++i) {
143                                 set (i, i, 1);
144                         }
145                 }
146         }
147 }
148
149
150 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
151 {
152         if (state_version < 32) {
153                 setup (node->number_child<int>("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
154         } else {
155                 setup (node->number_child<int>("InputChannels"), node->number_child<int>("OutputChannels"));
156         }
157
158         if (state_version <= 5) {
159                 /* Old-style: on/off mapping */
160                 for (auto i: node->node_children ("Map")) {
161                         set (i->number_child<int>("ContentIndex"), i->number_child<int>("DCP"), 1);
162                 }
163         } else {
164                 for (auto i: node->node_children("Gain")) {
165                         if (state_version < 32) {
166                                 set (
167                                         i->number_attribute<int>("Content"),
168                                         i->number_attribute<int>("DCP"),
169                                         raw_convert<float>(i->content())
170                                         );
171                         } else {
172                                 set (
173                                         number_attribute<int>(i, "Input", "input"),
174                                         number_attribute<int>(i, "Output", "output"),
175                                         raw_convert<float>(i->content())
176                                         );
177                         }
178                 }
179         }
180 }
181
182
183 void
184 AudioMapping::set (dcp::Channel input_channel, int output_channel, float g)
185 {
186         set (static_cast<int>(input_channel), output_channel, g);
187 }
188
189
190 void
191 AudioMapping::set (int input_channel, dcp::Channel output_channel, float g)
192 {
193         set (input_channel, static_cast<int>(output_channel), g);
194 }
195
196
197 void
198 AudioMapping::set (int input_channel, int output_channel, float g)
199 {
200         DCPOMATIC_ASSERT (input_channel < int(_gain.size()));
201         DCPOMATIC_ASSERT (output_channel < int(_gain[0].size()));
202         _gain[input_channel][output_channel] = g;
203 }
204
205
206 float
207 AudioMapping::get (int input_channel, dcp::Channel output_channel) const
208 {
209         return get (input_channel, static_cast<int>(output_channel));
210 }
211
212
213 float
214 AudioMapping::get (int input_channel, int output_channel) const
215 {
216         DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
217         DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
218         return _gain[input_channel][output_channel];
219 }
220
221
222 void
223 AudioMapping::as_xml (xmlpp::Node* node) const
224 {
225         auto const input = input_channels();
226         auto const output = output_channels();
227
228         node->add_child("InputChannels")->add_child_text(raw_convert<string>(input));
229         node->add_child("OutputChannels")->add_child_text(raw_convert<string>(output));
230
231         for (int c = 0; c < input; ++c) {
232                 for (int d = 0; d < output; ++d) {
233                         auto t = node->add_child ("Gain");
234                         t->set_attribute("input", raw_convert<string>(c));
235                         t->set_attribute("output", raw_convert<string>(d));
236                         t->add_child_text (raw_convert<string> (get (c, d)));
237                 }
238         }
239 }
240
241
242 /** @return a string which is unique for a given AudioMapping configuration, for
243  *  differentiation between different AudioMappings.
244  */
245 string
246 AudioMapping::digest () const
247 {
248         Digester digester;
249         digester.add(input_channels());
250         digester.add(output_channels());
251         for (auto const& input: _gain) {
252                 for (auto output: input) {
253                         digester.add(output);
254                 }
255         }
256
257         return digester.get ();
258 }
259
260
261 list<int>
262 AudioMapping::mapped_output_channels () const
263 {
264         static float const minus_96_db = 0.000015849;
265
266         list<int> mapped;
267
268         for (auto const& i: _gain) {
269                 for (auto j: dcp::used_audio_channels()) {
270                         if (abs(i[static_cast<int>(j)]) > minus_96_db) {
271                                 mapped.push_back (static_cast<int>(j));
272                         }
273                 }
274         }
275
276         mapped.sort ();
277         mapped.unique ();
278
279         return mapped;
280 }
281
282
283 void
284 AudioMapping::unmap_all ()
285 {
286         for (auto& i: _gain) {
287                 for (auto& j: i) {
288                         j = 0;
289                 }
290         }
291 }
292
293
294 void
295 AudioMapping::take_from(AudioMapping const& other)
296 {
297         auto input = std::min(input_channels(), other.input_channels());
298         auto output = std::min(output_channels(), other.output_channels());
299
300         for (auto i = 0; i < input; ++i) {
301                 for (auto o = 0; o < output; ++o) {
302                         set(i, o, other.get(i, o));
303                 }
304         }
305 }
306