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) {
85 AudioMapping::make_default (AudioProcessor const * processor, optional<boost::filesystem::path> filename)
87 static string const regex[] = {
88 ".*[\\._-]L[\\._-].*",
89 ".*[\\._-]R[\\._-].*",
90 ".*[\\._-]C[\\._-].*",
91 ".*[\\._-]Lfe[\\._-].*",
92 ".*[\\._-]Ls[\\._-].*",
93 ".*[\\._-]Rs[\\._-].*"
96 static int const regexes = sizeof(regex) / sizeof(*regex);
99 processor->make_audio_mapping_default (*this);
102 if (input_channels() == 1) {
103 bool guessed = false;
105 /* See if we can guess where this stream should go */
107 for (int i = 0; i < regexes; ++i) {
108 boost::regex e (regex[i], boost::regex::icase);
109 if (boost::regex_match(filename->string(), e) && i < output_channels()) {
117 /* If we have no idea, just put it on centre */
118 set (0, static_cast<int>(dcp::CENTRE), 1);
122 for (int i = 0; i < min (input_channels(), output_channels()); ++i) {
129 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
131 if (state_version < 32) {
132 setup (node->number_child<int> ("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
134 setup (node->number_child<int> ("InputChannels"), node->number_child<int> ("OutputChannels"));
137 if (state_version <= 5) {
138 /* Old-style: on/off mapping */
139 list<cxml::NodePtr> const c = node->node_children ("Map");
140 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
141 set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
144 list<cxml::NodePtr> const c = node->node_children ("Gain");
145 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
146 if (state_version < 32) {
148 (*i)->number_attribute<int> ("Content"),
149 static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
150 raw_convert<float> ((*i)->content ())
154 (*i)->number_attribute<int> ("Input"),
155 (*i)->number_attribute<int> ("Output"),
156 raw_convert<float> ((*i)->content ())
164 AudioMapping::set (int input_channel, int output_channel, float g)
166 _gain[input_channel][output_channel] = g;
170 AudioMapping::get (int input_channel, int output_channel) const
172 DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
173 DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
174 return _gain[input_channel][output_channel];
178 AudioMapping::as_xml (xmlpp::Node* node) const
180 node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
181 node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
183 for (int c = 0; c < _input_channels; ++c) {
184 for (int d = 0; d < _output_channels; ++d) {
185 xmlpp::Element* t = node->add_child ("Gain");
186 t->set_attribute ("Input", raw_convert<string> (c));
187 t->set_attribute ("Output", raw_convert<string> (d));
188 t->add_child_text (raw_convert<string> (get (c, d)));
193 /** @return a string which is unique for a given AudioMapping configuration, for
194 * differentiation between different AudioMappings.
197 AudioMapping::digest () const
200 digester.add (_input_channels);
201 digester.add (_output_channels);
202 for (int i = 0; i < _input_channels; ++i) {
203 for (int j = 0; j < _output_channels; ++j) {
204 digester.add (_gain[i][j]);
208 return digester.get ();
212 AudioMapping::mapped_output_channels () const
214 static float const minus_96_db = 0.000015849;
218 for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
219 for (size_t j = 0; j < i->size(); ++j) {
220 if (abs ((*i)[j]) > minus_96_db) {
221 mapped.push_back (j);
233 AudioMapping::unmap_all ()
235 for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
236 for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {