Various work on audio mapping.
[dcpomatic.git] / src / lib / audio_mapping.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "audio_mapping.h"
21 #include "util.h"
22 #include "md5_digester.h"
23 #include "raw_convert.h"
24 #include <libcxml/cxml.h>
25 #include <libxml++/libxml++.h>
26
27 using std::list;
28 using std::cout;
29 using std::make_pair;
30 using std::pair;
31 using std::string;
32 using std::min;
33 using std::vector;
34 using boost::shared_ptr;
35 using boost::dynamic_pointer_cast;
36
37 AudioMapping::AudioMapping ()
38         : _input_channels (0)
39         , _output_channels (0)
40 {
41
42 }
43
44 /** Create an empty AudioMapping.
45  *  @param input_channels Number of input channels.
46  *  @param output_channels Number of output channels.
47  */
48 AudioMapping::AudioMapping (int input_channels, int output_channels)
49 {
50         setup (input_channels, output_channels);
51 }
52
53 void
54 AudioMapping::setup (int input_channels, int output_channels)
55 {
56         _input_channels = input_channels;
57         _output_channels = output_channels;
58         
59         _gain.resize (_input_channels);
60         for (int i = 0; i < _input_channels; ++i) {
61                 _gain[i].resize (_output_channels);
62         }
63
64         make_zero ();
65 }
66
67 void
68 AudioMapping::make_zero ()
69 {
70         for (int i = 0; i < _input_channels; ++i) {
71                 for (int j = 0; j < _output_channels; ++j) {
72                         _gain[i][j] = 0;
73                 }
74         }
75 }
76
77 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
78 {
79         if (state_version < 32) {
80                 setup (node->number_child<int> ("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
81         } else {
82                 setup (node->number_child<int> ("InputChannels"), node->number_child<int> ("OutputChannels"));
83         }
84
85         if (state_version <= 5) {
86                 /* Old-style: on/off mapping */
87                 list<cxml::NodePtr> const c = node->node_children ("Map");
88                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
89                         set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
90                 }
91         } else {
92                 list<cxml::NodePtr> const c = node->node_children ("Gain");
93                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
94                         if (state_version < 32) {
95                                 set (
96                                         (*i)->number_attribute<int> ("Content"),
97                                         static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
98                                         raw_convert<float> ((*i)->content ())
99                                         );
100                         } else {
101                                 set (
102                                         (*i)->number_attribute<int> ("Input"),
103                                         (*i)->number_attribute<int> ("Output"),
104                                         raw_convert<float> ((*i)->content ())
105                                         );
106                         }
107                 }
108         }
109 }
110
111 void
112 AudioMapping::set (int input_channel, int output_channel, float g)
113 {
114         _gain[input_channel][output_channel] = g;
115 }
116
117 float
118 AudioMapping::get (int input_channel, int output_channel) const
119 {
120         return _gain[input_channel][output_channel];
121 }
122
123 void
124 AudioMapping::as_xml (xmlpp::Node* node) const
125 {
126         node->add_child ("InputChannels")->add_child_text (raw_convert<string> (_input_channels));
127         node->add_child ("OutputChannels")->add_child_text (raw_convert<string> (_output_channels));
128
129         for (int c = 0; c < _input_channels; ++c) {
130                 for (int d = 0; d < _output_channels; ++d) {
131                         xmlpp::Element* t = node->add_child ("Gain");
132                         t->set_attribute ("Input", raw_convert<string> (c));
133                         t->set_attribute ("Output", raw_convert<string> (d));
134                         t->add_child_text (raw_convert<string> (get (c, d)));
135                 }
136         }
137 }
138
139 /** @return a string which is unique for a given AudioMapping configuration, for
140  *  differentiation between different AudioMappings.
141  */
142 string
143 AudioMapping::digest () const
144 {
145         MD5Digester digester;
146         digester.add (_input_channels);
147         digester.add (_output_channels);
148         for (int i = 0; i < _input_channels; ++i) {
149                 for (int j = 0; j < _output_channels; ++j) {
150                         digester.add (_gain[i][j]);
151                 }
152         }
153
154         return digester.get ();
155 }
156
157 list<int>
158 AudioMapping::mapped_output_channels () const
159 {
160         static float const minus_96_db = 0.000015849;
161
162         list<int> mapped;
163         
164         for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
165                 for (size_t j = 0; j < i->size(); ++j) {
166                         if (abs ((*i)[j]) > minus_96_db) {
167                                 mapped.push_back (j);
168                         }
169                 }
170         }
171
172         mapped.sort ();
173         mapped.unique ();
174         
175         return mapped;
176 }
177
178 void
179 AudioMapping::unmap_all ()
180 {
181         for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
182                 for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {
183                         *j = 0;
184                 }
185         }
186 }