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