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 "raw_convert.h"
25 #include <libcxml/cxml.h>
26 #include <libxml++/libxml++.h>
37 using boost::shared_ptr;
38 using boost::dynamic_pointer_cast;
40 AudioMapping::AudioMapping ()
42 , _output_channels (0)
47 /** Create an empty AudioMapping.
48 * @param input_channels Number of input channels.
49 * @param output_channels Number of output channels.
51 AudioMapping::AudioMapping (int input_channels, int output_channels)
53 setup (input_channels, output_channels);
57 AudioMapping::setup (int input_channels, int output_channels)
59 _input_channels = input_channels;
60 _output_channels = output_channels;
62 _gain.resize (_input_channels);
63 for (int i = 0; i < _input_channels; ++i) {
64 _gain[i].resize (_output_channels);
71 AudioMapping::make_zero ()
73 for (int i = 0; i < _input_channels; ++i) {
74 for (int j = 0; j < _output_channels; ++j) {
80 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
82 if (state_version < 32) {
83 setup (node->number_child<int> ("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
85 setup (node->number_child<int> ("InputChannels"), node->number_child<int> ("OutputChannels"));
88 if (state_version <= 5) {
89 /* Old-style: on/off mapping */
90 list<cxml::NodePtr> const c = node->node_children ("Map");
91 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
92 set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
95 list<cxml::NodePtr> const c = node->node_children ("Gain");
96 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
97 if (state_version < 32) {
99 (*i)->number_attribute<int> ("Content"),
100 static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
101 raw_convert<float> ((*i)->content ())
105 (*i)->number_attribute<int> ("Input"),
106 (*i)->number_attribute<int> ("Output"),
107 raw_convert<float> ((*i)->content ())
115 AudioMapping::set (int input_channel, int output_channel, float g)
117 _gain[input_channel][output_channel] = g;
121 AudioMapping::get (int input_channel, int output_channel) const
123 DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
124 DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
125 return _gain[input_channel][output_channel];
129 AudioMapping::as_xml (xmlpp::Node* node) const
131 node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
132 node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
134 for (int c = 0; c < _input_channels; ++c) {
135 for (int d = 0; d < _output_channels; ++d) {
136 xmlpp::Element* t = node->add_child ("Gain");
137 t->set_attribute ("Input", raw_convert<string> (c));
138 t->set_attribute ("Output", raw_convert<string> (d));
139 t->add_child_text (raw_convert<string> (get (c, d)));
144 /** @return a string which is unique for a given AudioMapping configuration, for
145 * differentiation between different AudioMappings.
148 AudioMapping::digest () const
151 digester.add (_input_channels);
152 digester.add (_output_channels);
153 for (int i = 0; i < _input_channels; ++i) {
154 for (int j = 0; j < _output_channels; ++j) {
155 digester.add (_gain[i][j]);
159 return digester.get ();
163 AudioMapping::mapped_output_channels () const
165 static float const minus_96_db = 0.000015849;
169 for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
170 for (size_t j = 0; j < i->size(); ++j) {
171 if (abs ((*i)[j]) > minus_96_db) {
172 mapped.push_back (j);
184 AudioMapping::unmap_all ()
186 for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
187 for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {