2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "audio_mapping.h"
22 #include "md5_digester.h"
23 #include "raw_convert.h"
24 #include <libcxml/cxml.h>
25 #include <libxml++/libxml++.h>
35 using boost::shared_ptr;
36 using boost::dynamic_pointer_cast;
38 AudioMapping::AudioMapping ()
40 , _output_channels (0)
45 /** Create an empty AudioMapping.
46 * @param input_channels Number of input channels.
47 * @param output_channels Number of output channels.
49 AudioMapping::AudioMapping (int input_channels, int output_channels)
51 setup (input_channels, output_channels);
55 AudioMapping::setup (int input_channels, int output_channels)
57 _input_channels = input_channels;
58 _output_channels = output_channels;
60 _gain.resize (_input_channels);
61 for (int i = 0; i < _input_channels; ++i) {
62 _gain[i].resize (_output_channels);
69 AudioMapping::make_zero ()
71 for (int i = 0; i < _input_channels; ++i) {
72 for (int j = 0; j < _output_channels; ++j) {
78 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
80 if (state_version < 32) {
81 setup (node->number_child<int> ("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
83 setup (node->number_child<int> ("InputChannels"), node->number_child<int> ("OutputChannels"));
86 if (state_version <= 5) {
87 /* Old-style: on/off mapping */
88 list<cxml::NodePtr> const c = node->node_children ("Map");
89 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
90 set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
93 list<cxml::NodePtr> const c = node->node_children ("Gain");
94 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
95 if (state_version < 32) {
97 (*i)->number_attribute<int> ("Content"),
98 static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
99 raw_convert<float> ((*i)->content ())
103 (*i)->number_attribute<int> ("Input"),
104 (*i)->number_attribute<int> ("Output"),
105 raw_convert<float> ((*i)->content ())
113 AudioMapping::set (int input_channel, int output_channel, float g)
115 _gain[input_channel][output_channel] = g;
119 AudioMapping::get (int input_channel, int output_channel) const
121 return _gain[input_channel][output_channel];
125 AudioMapping::as_xml (xmlpp::Node* node) const
127 node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
128 node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
130 for (int c = 0; c < _input_channels; ++c) {
131 for (int d = 0; d < _output_channels; ++d) {
132 xmlpp::Element* t = node->add_child ("Gain");
133 t->set_attribute ("Input", raw_convert<string> (c));
134 t->set_attribute ("Output", raw_convert<string> (d));
135 t->add_child_text (raw_convert<string> (get (c, d)));
140 /** @return a string which is unique for a given AudioMapping configuration, for
141 * differentiation between different AudioMappings.
144 AudioMapping::digest () const
146 MD5Digester digester;
147 digester.add (_input_channels);
148 digester.add (_output_channels);
149 for (int i = 0; i < _input_channels; ++i) {
150 for (int j = 0; j < _output_channels; ++j) {
151 digester.add (_gain[i][j]);
155 return digester.get ();
159 AudioMapping::mapped_output_channels () const
161 static float const minus_96_db = 0.000015849;
165 for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
166 for (size_t j = 0; j < i->size(); ++j) {
167 if (abs ((*i)[j]) > minus_96_db) {
168 mapped.push_back (j);
180 AudioMapping::unmap_all ()
182 for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
183 for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {