Missed update to private test repo version.
[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 "digester.h"
25 #include "util.h"
26 #include <dcp/raw_convert.h>
27 #include <dcp/warnings.h>
28 #include <libcxml/cxml.h>
29 LIBDCP_DISABLE_WARNINGS
30 #include <libxml++/libxml++.h>
31 LIBDCP_ENABLE_WARNINGS
32 #include <boost/regex.hpp>
33 #include <iostream>
34
35
36 using std::list;
37 using std::cout;
38 using std::make_pair;
39 using std::pair;
40 using std::string;
41 using std::min;
42 using std::vector;
43 using std::abs;
44 using std::shared_ptr;
45 using std::dynamic_pointer_cast;
46 using boost::optional;
47 using dcp::raw_convert;
48
49
50 /** Create an empty AudioMapping.
51  *  @param input_channels Number of input channels.
52  *  @param output_channels Number of output channels.
53  */
54 AudioMapping::AudioMapping (int input_channels, int output_channels)
55 {
56         setup (input_channels, output_channels);
57 }
58
59
60 void
61 AudioMapping::setup (int input_channels, int output_channels)
62 {
63         _input_channels = input_channels;
64         _output_channels = output_channels;
65
66         _gain.resize (_input_channels);
67         for (int i = 0; i < _input_channels; ++i) {
68                 _gain[i].resize (_output_channels);
69         }
70
71         make_zero ();
72 }
73
74
75 void
76 AudioMapping::make_zero ()
77 {
78         for (int i = 0; i < _input_channels; ++i) {
79                 for (int j = 0; j < _output_channels; ++j) {
80                         _gain[i][j] = 0;
81                 }
82         }
83 }
84
85
86 struct ChannelRegex
87 {
88         ChannelRegex (string regex_, int channel_)
89                 : regex (regex_)
90                 , channel (channel_)
91         {}
92
93         string regex;
94         int channel;
95 };
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->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
149 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
150 {
151         if (state_version < 32) {
152                 setup (node->number_child<int>("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
153         } else {
154                 setup (node->number_child<int>("InputChannels"), node->number_child<int>("OutputChannels"));
155         }
156
157         if (state_version <= 5) {
158                 /* Old-style: on/off mapping */
159                 for (auto i: node->node_children ("Map")) {
160                         set (i->number_child<int>("ContentIndex"), i->number_child<int>("DCP"), 1);
161                 }
162         } else {
163                 for (auto i: node->node_children("Gain")) {
164                         if (state_version < 32) {
165                                 set (
166                                         i->number_attribute<int>("Content"),
167                                         i->number_attribute<int>("DCP"),
168                                         raw_convert<float>(i->content())
169                                         );
170                         } else {
171                                 set (
172                                         i->number_attribute<int>("Input"),
173                                         i->number_attribute<int>("Output"),
174                                         raw_convert<float>(i->content())
175                                         );
176                         }
177                 }
178         }
179 }
180
181
182 void
183 AudioMapping::set (dcp::Channel input_channel, int output_channel, float g)
184 {
185         set (static_cast<int>(input_channel), output_channel, g);
186 }
187
188
189 void
190 AudioMapping::set (int input_channel, dcp::Channel output_channel, float g)
191 {
192         set (input_channel, static_cast<int>(output_channel), g);
193 }
194
195
196 void
197 AudioMapping::set (int input_channel, int output_channel, float g)
198 {
199         DCPOMATIC_ASSERT (input_channel < int(_gain.size()));
200         DCPOMATIC_ASSERT (output_channel < int(_gain[0].size()));
201         _gain[input_channel][output_channel] = g;
202 }
203
204
205 float
206 AudioMapping::get (int input_channel, dcp::Channel output_channel) const
207 {
208         return get (input_channel, static_cast<int>(output_channel));
209 }
210
211
212 float
213 AudioMapping::get (int input_channel, int output_channel) const
214 {
215         DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
216         DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
217         return _gain[input_channel][output_channel];
218 }
219
220
221 void
222 AudioMapping::as_xml (xmlpp::Node* node) const
223 {
224         node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
225         node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
226
227         for (int c = 0; c < _input_channels; ++c) {
228                 for (int d = 0; d < _output_channels; ++d) {
229                         auto t = node->add_child ("Gain");
230                         t->set_attribute ("Input", raw_convert<string> (c));
231                         t->set_attribute ("Output", raw_convert<string> (d));
232                         t->add_child_text (raw_convert<string> (get (c, d)));
233                 }
234         }
235 }
236
237
238 /** @return a string which is unique for a given AudioMapping configuration, for
239  *  differentiation between different AudioMappings.
240  */
241 string
242 AudioMapping::digest () const
243 {
244         Digester digester;
245         digester.add (_input_channels);
246         digester.add (_output_channels);
247         for (int i = 0; i < _input_channels; ++i) {
248                 for (int j = 0; j < _output_channels; ++j) {
249                         digester.add (_gain[i][j]);
250                 }
251         }
252
253         return digester.get ();
254 }
255
256
257 list<int>
258 AudioMapping::mapped_output_channels () const
259 {
260         static float const minus_96_db = 0.000015849;
261
262         list<int> mapped;
263
264         for (auto const& i: _gain) {
265                 for (auto j: dcp::used_audio_channels()) {
266                         if (abs(i[static_cast<int>(j)]) > minus_96_db) {
267                                 mapped.push_back (static_cast<int>(j));
268                         }
269                 }
270         }
271
272         mapped.sort ();
273         mapped.unique ();
274
275         return mapped;
276 }
277
278
279 void
280 AudioMapping::unmap_all ()
281 {
282         for (auto& i: _gain) {
283                 for (auto& j: i) {
284                         j = 0;
285                 }
286         }
287 }