2 Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "audio_mapping.h"
23 #include "audio_processor.h"
24 #include "constants.h"
25 #include "dcpomatic_assert.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>
45 using std::shared_ptr;
46 using std::dynamic_pointer_cast;
47 using boost::optional;
48 using dcp::raw_convert;
51 /** Create an empty AudioMapping.
52 * @param input_channels Number of input channels.
53 * @param output_channels Number of output channels.
55 AudioMapping::AudioMapping (int input_channels, int output_channels)
57 setup (input_channels, output_channels);
62 AudioMapping::setup (int input_channels, int output_channels)
64 _gain.resize(input_channels);
65 for (int i = 0; i < input_channels; ++i) {
66 _gain[i].resize(output_channels);
74 AudioMapping::make_zero ()
76 for (auto& input: _gain) {
77 for (auto& output: input) {
86 ChannelRegex (string regex_, int channel_)
97 AudioMapping::make_default (AudioProcessor const * processor, optional<boost::filesystem::path> filename)
99 static ChannelRegex const regex[] = {
100 ChannelRegex(".*[\\._-]L[\\._-].*", 0),
101 ChannelRegex(".*[\\._-]R[\\._-].*", 1),
102 ChannelRegex(".*[\\._-]C[\\._-].*", 2),
103 ChannelRegex(".*[\\._-]Lfe[\\._-].*", 3),
104 ChannelRegex(".*[\\._-]LFE[\\._-].*", 3),
105 ChannelRegex(".*[\\._-]Lss[\\._-].*", 4),
106 ChannelRegex(".*[\\._-]Lsr[\\._-].*", 6),
107 ChannelRegex(".*[\\._-]Lrs[\\._-].*", 6),
108 ChannelRegex(".*[\\._-]Ls[\\._-].*", 4),
109 ChannelRegex(".*[\\._-]Rss[\\._-].*", 5),
110 ChannelRegex(".*[\\._-]Rsr[\\._-].*", 7),
111 ChannelRegex(".*[\\._-]Rrs[\\._-].*", 7),
112 ChannelRegex(".*[\\._-]Rs[\\._-].*", 5),
115 static int const regexes = sizeof(regex) / sizeof(*regex);
118 processor->make_audio_mapping_default (*this);
121 if (input_channels() == 1) {
122 bool guessed = false;
124 /* See if we can guess where this stream should go */
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);
136 /* If we have no idea, just put it on centre */
137 set (0, static_cast<int>(dcp::Channel::CENTRE), 1);
141 for (int i = 0; i < min (input_channels(), output_channels()); ++i) {
149 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
151 if (state_version < 32) {
152 setup (node->number_child<int>("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
154 setup (node->number_child<int>("InputChannels"), node->number_child<int>("OutputChannels"));
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);
163 for (auto i: node->node_children("Gain")) {
164 if (state_version < 32) {
166 i->number_attribute<int>("Content"),
167 i->number_attribute<int>("DCP"),
168 raw_convert<float>(i->content())
172 i->number_attribute<int>("Input"),
173 i->number_attribute<int>("Output"),
174 raw_convert<float>(i->content())
183 AudioMapping::set (dcp::Channel input_channel, int output_channel, float g)
185 set (static_cast<int>(input_channel), output_channel, g);
190 AudioMapping::set (int input_channel, dcp::Channel output_channel, float g)
192 set (input_channel, static_cast<int>(output_channel), g);
197 AudioMapping::set (int input_channel, int output_channel, float g)
199 DCPOMATIC_ASSERT (input_channel < int(_gain.size()));
200 DCPOMATIC_ASSERT (output_channel < int(_gain[0].size()));
201 _gain[input_channel][output_channel] = g;
206 AudioMapping::get (int input_channel, dcp::Channel output_channel) const
208 return get (input_channel, static_cast<int>(output_channel));
213 AudioMapping::get (int input_channel, int output_channel) const
215 DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
216 DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
217 return _gain[input_channel][output_channel];
222 AudioMapping::as_xml (xmlpp::Node* node) const
224 auto const input = input_channels();
225 auto const output = output_channels();
227 node->add_child("InputChannels")->add_child_text(raw_convert<string>(input));
228 node->add_child("OutputChannels")->add_child_text(raw_convert<string>(output));
230 for (int c = 0; c < input; ++c) {
231 for (int d = 0; d < output; ++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)));
241 /** @return a string which is unique for a given AudioMapping configuration, for
242 * differentiation between different AudioMappings.
245 AudioMapping::digest () const
248 digester.add(input_channels());
249 digester.add(output_channels());
250 for (auto const& input: _gain) {
251 for (auto output: input) {
252 digester.add(output);
256 return digester.get ();
261 AudioMapping::mapped_output_channels () const
263 static float const minus_96_db = 0.000015849;
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));
283 AudioMapping::unmap_all ()
285 for (auto& i: _gain) {
294 AudioMapping::take_from(AudioMapping const& other)
296 auto input = std::min(input_channels(), other.input_channels());
297 auto output = std::min(output_channels(), other.output_channels());
299 for (auto i = 0; i < input; ++i) {
300 for (auto o = 0; o < output; ++o) {
301 set(i, o, other.get(i, o));