2 Copyright (C) 2013-2018 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/>.
21 #include "audio_mapping.h"
24 #include "audio_processor.h"
25 #include <dcp/raw_convert.h>
26 #include <libcxml/cxml.h>
27 #include <libxml++/libxml++.h>
28 #include <boost/regex.hpp>
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
42 using dcp::raw_convert;
44 AudioMapping::AudioMapping ()
46 , _output_channels (0)
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);
61 AudioMapping::setup (int input_channels, int output_channels)
63 _input_channels = input_channels;
64 _output_channels = output_channels;
66 _gain.resize (_input_channels);
67 for (int i = 0; i < _input_channels; ++i) {
68 _gain[i].resize (_output_channels);
75 AudioMapping::make_zero ()
77 for (int i = 0; i < _input_channels; ++i) {
78 for (int j = 0; j < _output_channels; ++j) {
86 ChannelRegex (string regex_, int channel_)
96 AudioMapping::make_default (AudioProcessor const * processor, optional<boost::filesystem::path> filename)
98 static ChannelRegex const regex[] = {
99 ChannelRegex(".*[\\._-]L[\\._-].*", 0),
100 ChannelRegex(".*[\\._-]R[\\._-].*", 1),
101 ChannelRegex(".*[\\._-]C[\\._-].*", 2),
102 ChannelRegex(".*[\\._-]Lfe[\\._-].*", 3),
103 ChannelRegex(".*[\\._-]LFE[\\._-].*", 3),
104 ChannelRegex(".*[\\._-]Lss[\\._-].*", 4),
105 ChannelRegex(".*[\\._-]Lsr[\\._-].*", 6),
106 ChannelRegex(".*[\\._-]Ls[\\._-].*", 4),
107 ChannelRegex(".*[\\._-]Rss[\\._-].*", 5),
108 ChannelRegex(".*[\\._-]Rsr[\\._-].*", 7),
109 ChannelRegex(".*[\\._-]Rs[\\._-].*", 5),
112 static int const regexes = sizeof(regex) / sizeof(*regex);
115 processor->make_audio_mapping_default (*this);
118 if (input_channels() == 1) {
119 bool guessed = false;
121 /* See if we can guess where this stream should go */
123 for (int i = 0; i < regexes; ++i) {
124 boost::regex e (regex[i].regex, boost::regex::icase);
125 if (boost::regex_match(filename->string(), e) && regex[i].channel < output_channels()) {
126 set (0, regex[i].channel, 1);
133 /* If we have no idea, just put it on centre */
134 set (0, static_cast<int>(dcp::CENTRE), 1);
138 for (int i = 0; i < min (input_channels(), output_channels()); ++i) {
145 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
147 if (state_version < 32) {
148 setup (node->number_child<int> ("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
150 setup (node->number_child<int> ("InputChannels"), node->number_child<int> ("OutputChannels"));
153 if (state_version <= 5) {
154 /* Old-style: on/off mapping */
155 list<cxml::NodePtr> const c = node->node_children ("Map");
156 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
157 set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
160 list<cxml::NodePtr> const c = node->node_children ("Gain");
161 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
162 if (state_version < 32) {
164 (*i)->number_attribute<int> ("Content"),
165 static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
166 raw_convert<float> ((*i)->content ())
170 (*i)->number_attribute<int> ("Input"),
171 (*i)->number_attribute<int> ("Output"),
172 raw_convert<float> ((*i)->content ())
180 AudioMapping::set (int input_channel, int output_channel, float g)
182 DCPOMATIC_ASSERT (input_channel < int(_gain.size()));
183 DCPOMATIC_ASSERT (output_channel < int(_gain[0].size()));
184 _gain[input_channel][output_channel] = g;
188 AudioMapping::get (int input_channel, int output_channel) const
190 DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
191 DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
192 return _gain[input_channel][output_channel];
196 AudioMapping::as_xml (xmlpp::Node* node) const
198 node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
199 node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
201 for (int c = 0; c < _input_channels; ++c) {
202 for (int d = 0; d < _output_channels; ++d) {
203 xmlpp::Element* t = node->add_child ("Gain");
204 t->set_attribute ("Input", raw_convert<string> (c));
205 t->set_attribute ("Output", raw_convert<string> (d));
206 t->add_child_text (raw_convert<string> (get (c, d)));
211 /** @return a string which is unique for a given AudioMapping configuration, for
212 * differentiation between different AudioMappings.
215 AudioMapping::digest () const
218 digester.add (_input_channels);
219 digester.add (_output_channels);
220 for (int i = 0; i < _input_channels; ++i) {
221 for (int j = 0; j < _output_channels; ++j) {
222 digester.add (_gain[i][j]);
226 return digester.get ();
230 AudioMapping::mapped_output_channels () const
232 static float const minus_96_db = 0.000015849;
236 for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
237 for (size_t j = 0; j < i->size(); ++j) {
238 if (abs ((*i)[j]) > minus_96_db) {
239 mapped.push_back (j);
251 AudioMapping::unmap_all ()
253 for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
254 for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {