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