More player debugging for butler video-full states.
[dcpomatic.git] / src / lib / audio_mapping.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "audio_mapping.h"
22 #include "util.h"
23 #include "digester.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>
29 #include <iostream>
30
31 using std::list;
32 using std::cout;
33 using std::make_pair;
34 using std::pair;
35 using std::string;
36 using std::min;
37 using std::vector;
38 using std::abs;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using boost::optional;
42 using dcp::raw_convert;
43
44 AudioMapping::AudioMapping ()
45         : _input_channels (0)
46         , _output_channels (0)
47 {
48
49 }
50
51 /** Create an empty AudioMapping.
52  *  @param input_channels Number of input channels.
53  *  @param output_channels Number of output channels.
54  */
55 AudioMapping::AudioMapping (int input_channels, int output_channels)
56 {
57         setup (input_channels, output_channels);
58 }
59
60 void
61 AudioMapping::setup (int input_channels, int output_channels)
62 {
63         _input_channels = input_channels;
64         _output_channels = output_channels;
65
66         _gain.resize (_input_channels);
67         for (int i = 0; i < _input_channels; ++i) {
68                 _gain[i].resize (_output_channels);
69         }
70
71         make_zero ();
72 }
73
74 void
75 AudioMapping::make_zero ()
76 {
77         for (int i = 0; i < _input_channels; ++i) {
78                 for (int j = 0; j < _output_channels; ++j) {
79                         _gain[i][j] = 0;
80                 }
81         }
82 }
83
84 void
85 AudioMapping::make_default (AudioProcessor const * processor, optional<boost::filesystem::path> filename)
86 {
87         static string const regex[] = {
88                 ".*[\\._-]L[\\._-].*",
89                 ".*[\\._-]R[\\._-].*",
90                 ".*[\\._-]C[\\._-].*",
91                 ".*[\\._-]Lfe[\\._-].*",
92                 ".*[\\._-]Ls[\\._-].*",
93                 ".*[\\._-]Rs[\\._-].*"
94         };
95
96         static int const regexes = sizeof(regex) / sizeof(*regex);
97
98         if (processor) {
99                 processor->make_audio_mapping_default (*this);
100         } else {
101                 make_zero ();
102                 if (input_channels() == 1) {
103                         bool guessed = false;
104
105                         /* See if we can guess where this stream should go */
106                         if (filename) {
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()) {
110                                                 set (0, i, 1);
111                                                 guessed = true;
112                                         }
113                                 }
114                         }
115
116                         if (!guessed) {
117                                 /* If we have no idea, just put it on centre */
118                                 set (0, static_cast<int>(dcp::CENTRE), 1);
119                         }
120                 } else {
121                         /* 1:1 mapping */
122                         for (int i = 0; i < min (input_channels(), output_channels()); ++i) {
123                                 set (i, i, 1);
124                         }
125                 }
126         }
127 }
128
129 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
130 {
131         if (state_version < 32) {
132                 setup (node->number_child<int> ("ContentChannels"), MAX_DCP_AUDIO_CHANNELS);
133         } else {
134                 setup (node->number_child<int> ("InputChannels"), node->number_child<int> ("OutputChannels"));
135         }
136
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);
142                 }
143         } else {
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) {
147                                 set (
148                                         (*i)->number_attribute<int> ("Content"),
149                                         static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
150                                         raw_convert<float> ((*i)->content ())
151                                         );
152                         } else {
153                                 set (
154                                         (*i)->number_attribute<int> ("Input"),
155                                         (*i)->number_attribute<int> ("Output"),
156                                         raw_convert<float> ((*i)->content ())
157                                         );
158                         }
159                 }
160         }
161 }
162
163 void
164 AudioMapping::set (int input_channel, int output_channel, float g)
165 {
166         _gain[input_channel][output_channel] = g;
167 }
168
169 float
170 AudioMapping::get (int input_channel, int output_channel) const
171 {
172         DCPOMATIC_ASSERT (input_channel < int (_gain.size()));
173         DCPOMATIC_ASSERT (output_channel < int (_gain[0].size()));
174         return _gain[input_channel][output_channel];
175 }
176
177 void
178 AudioMapping::as_xml (xmlpp::Node* node) const
179 {
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));
182
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)));
189                 }
190         }
191 }
192
193 /** @return a string which is unique for a given AudioMapping configuration, for
194  *  differentiation between different AudioMappings.
195  */
196 string
197 AudioMapping::digest () const
198 {
199         Digester digester;
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]);
205                 }
206         }
207
208         return digester.get ();
209 }
210
211 list<int>
212 AudioMapping::mapped_output_channels () const
213 {
214         static float const minus_96_db = 0.000015849;
215
216         list<int> mapped;
217
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);
222                         }
223                 }
224         }
225
226         mapped.sort ();
227         mapped.unique ();
228
229         return mapped;
230 }
231
232 void
233 AudioMapping::unmap_all ()
234 {
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) {
237                         *j = 0;
238                 }
239         }
240 }