2 Copyright (C) 2013-2015 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 <dcp/raw_convert.h>
25 #include <libcxml/cxml.h>
26 #include <libxml++/libxml++.h>
37 using boost::shared_ptr;
38 using boost::dynamic_pointer_cast;
39 using dcp::raw_convert;
41 AudioMapping::AudioMapping ()
43 , _output_channels (0)
48 /** Create an empty AudioMapping.
49 * @param input_channels Number of input channels.
50 * @param output_channels Number of output channels.
52 AudioMapping::AudioMapping (int input_channels, int output_channels)
54 setup (input_channels, output_channels);
58 AudioMapping::setup (int input_channels, int output_channels)
60 _input_channels = input_channels;
61 _output_channels = output_channels;
63 _gain.resize (_input_channels);
64 for (int i = 0; i < _input_channels; ++i) {
65 _gain[i].resize (_output_channels);
72 AudioMapping::make_zero ()
74 for (int i = 0; i < _input_channels; ++i) {
75 for (int j = 0; j < _output_channels; ++j) {
81 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
83 if (state_version < 32) {
84 setup (node->number_child<int> ("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
86 setup (node->number_child<int> ("InputChannels"), node->number_child<int> ("OutputChannels"));
89 if (state_version <= 5) {
90 /* Old-style: on/off mapping */
91 list<cxml::NodePtr> const c = node->node_children ("Map");
92 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
93 set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
96 list<cxml::NodePtr> const c = node->node_children ("Gain");
97 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
98 if (state_version < 32) {
100 (*i)->number_attribute<int> ("Content"),
101 static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
102 raw_convert<float> ((*i)->content ())
106 (*i)->number_attribute<int> ("Input"),
107 (*i)->number_attribute<int> ("Output"),
108 raw_convert<float> ((*i)->content ())
116 AudioMapping::set (int input_channel, int output_channel, float g)
118 _gain[input_channel][output_channel] = g;
122 AudioMapping::get (int input_channel, int output_channel) const
124 DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
125 DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
126 return _gain[input_channel][output_channel];
130 AudioMapping::as_xml (xmlpp::Node* node) const
132 node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
133 node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
135 for (int c = 0; c < _input_channels; ++c) {
136 for (int d = 0; d < _output_channels; ++d) {
137 xmlpp::Element* t = node->add_child ("Gain");
138 t->set_attribute ("Input", raw_convert<string> (c));
139 t->set_attribute ("Output", raw_convert<string> (d));
140 t->add_child_text (raw_convert<string> (get (c, d)));
145 /** @return a string which is unique for a given AudioMapping configuration, for
146 * differentiation between different AudioMappings.
149 AudioMapping::digest () const
152 digester.add (_input_channels);
153 digester.add (_output_channels);
154 for (int i = 0; i < _input_channels; ++i) {
155 for (int j = 0; j < _output_channels; ++j) {
156 digester.add (_gain[i][j]);
160 return digester.get ();
164 AudioMapping::mapped_output_channels () const
166 static float const minus_96_db = 0.000015849;
170 for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
171 for (size_t j = 0; j < i->size(); ++j) {
172 if (abs ((*i)[j]) > minus_96_db) {
173 mapped.push_back (j);
185 AudioMapping::unmap_all ()
187 for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
188 for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {