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"
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>
46 using std::shared_ptr;
47 using std::dynamic_pointer_cast;
48 using boost::optional;
49 using dcp::raw_convert;
52 /** Create an empty AudioMapping.
53 * @param input_channels Number of input channels.
54 * @param output_channels Number of output channels.
56 AudioMapping::AudioMapping (int input_channels, int output_channels)
58 setup (input_channels, output_channels);
63 AudioMapping::setup (int input_channels, int output_channels)
65 _input_channels = input_channels;
66 _output_channels = output_channels;
68 _gain.resize (_input_channels);
69 for (int i = 0; i < _input_channels; ++i) {
70 _gain[i].resize (_output_channels);
78 AudioMapping::make_zero ()
80 for (int i = 0; i < _input_channels; ++i) {
81 for (int j = 0; j < _output_channels; ++j) {
90 ChannelRegex (string regex_, int channel_)
101 AudioMapping::make_default (AudioProcessor const * processor, optional<boost::filesystem::path> filename)
103 static ChannelRegex const regex[] = {
104 ChannelRegex(".*[\\._-]L[\\._-].*", 0),
105 ChannelRegex(".*[\\._-]R[\\._-].*", 1),
106 ChannelRegex(".*[\\._-]C[\\._-].*", 2),
107 ChannelRegex(".*[\\._-]Lfe[\\._-].*", 3),
108 ChannelRegex(".*[\\._-]LFE[\\._-].*", 3),
109 ChannelRegex(".*[\\._-]Lss[\\._-].*", 4),
110 ChannelRegex(".*[\\._-]Lsr[\\._-].*", 6),
111 ChannelRegex(".*[\\._-]Ls[\\._-].*", 4),
112 ChannelRegex(".*[\\._-]Rss[\\._-].*", 5),
113 ChannelRegex(".*[\\._-]Rsr[\\._-].*", 7),
114 ChannelRegex(".*[\\._-]Rs[\\._-].*", 5),
117 static int const regexes = sizeof(regex) / sizeof(*regex);
120 processor->make_audio_mapping_default (*this);
123 if (input_channels() == 1) {
124 bool guessed = false;
126 /* See if we can guess where this stream should go */
128 for (int i = 0; i < regexes; ++i) {
129 boost::regex e (regex[i].regex, boost::regex::icase);
130 if (boost::regex_match(filename->filename().string(), e) && regex[i].channel < output_channels()) {
131 set (0, regex[i].channel, 1);
138 /* If we have no idea, just put it on centre */
139 set (0, static_cast<int>(dcp::Channel::CENTRE), 1);
143 for (int i = 0; i < min (input_channels(), output_channels()); ++i) {
151 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
153 if (state_version < 32) {
154 setup (node->number_child<int>("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
156 setup (node->number_child<int>("InputChannels"), node->number_child<int>("OutputChannels"));
159 if (state_version <= 5) {
160 /* Old-style: on/off mapping */
161 for (auto i: node->node_children ("Map")) {
162 set (i->number_child<int>("ContentIndex"), i->number_child<int>("DCP"), 1);
165 for (auto i: node->node_children("Gain")) {
166 if (state_version < 32) {
168 i->number_attribute<int>("Content"),
169 i->number_attribute<int>("DCP"),
170 raw_convert<float>(i->content())
174 number_attribute<int>(i, "Input", "input"),
175 number_attribute<int>(i, "Output", "output"),
176 raw_convert<float>(i->content())
185 AudioMapping::set (dcp::Channel input_channel, int output_channel, float g)
187 set (static_cast<int>(input_channel), output_channel, g);
192 AudioMapping::set (int input_channel, dcp::Channel output_channel, float g)
194 set (input_channel, static_cast<int>(output_channel), g);
199 AudioMapping::set (int input_channel, int output_channel, float g)
201 DCPOMATIC_ASSERT (input_channel < int(_gain.size()));
202 DCPOMATIC_ASSERT (output_channel < int(_gain[0].size()));
203 _gain[input_channel][output_channel] = g;
208 AudioMapping::get (int input_channel, dcp::Channel output_channel) const
210 return get (input_channel, static_cast<int>(output_channel));
215 AudioMapping::get (int input_channel, int output_channel) const
217 DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
218 DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
219 return _gain[input_channel][output_channel];
224 AudioMapping::as_xml (xmlpp::Node* node) const
226 node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
227 node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
229 for (int c = 0; c < _input_channels; ++c) {
230 for (int d = 0; d < _output_channels; ++d) {
231 auto t = node->add_child ("Gain");
232 t->set_attribute("input", raw_convert<string>(c));
233 t->set_attribute("output", raw_convert<string>(d));
234 t->add_child_text (raw_convert<string> (get (c, d)));
240 /** @return a string which is unique for a given AudioMapping configuration, for
241 * differentiation between different AudioMappings.
244 AudioMapping::digest () const
247 digester.add (_input_channels);
248 digester.add (_output_channels);
249 for (int i = 0; i < _input_channels; ++i) {
250 for (int j = 0; j < _output_channels; ++j) {
251 digester.add (_gain[i][j]);
255 return digester.get ();
260 AudioMapping::mapped_output_channels () const
262 static float const minus_96_db = 0.000015849;
266 for (auto const& i: _gain) {
267 for (auto j: dcp::used_audio_channels()) {
268 if (abs(i[static_cast<int>(j)]) > minus_96_db) {
269 mapped.push_back (static_cast<int>(j));
282 AudioMapping::unmap_all ()
284 for (auto& i: _gain) {