Hand-apply a2f81da6d9afc5d3b5e647e1e05ca5d4507af42c from master;
[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 <libcxml/cxml.h>
24 #include <dcp/raw_convert.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 using dcp::raw_convert;
37
38 AudioMapping::AudioMapping ()
39         : _content_channels (0)
40 {
41
42 }
43
44 /** Create an empty AudioMapping for a given channel count.
45  *  @param channels Number of channels.
46  */
47 AudioMapping::AudioMapping (int channels)
48 {
49         setup (channels);
50 }
51
52 void
53 AudioMapping::setup (int c)
54 {
55         _content_channels = c;
56         
57         _gain.resize (_content_channels);
58         for (int i = 0; i < _content_channels; ++i) {
59                 _gain[i].resize (MAX_DCP_AUDIO_CHANNELS);
60         }
61 }
62
63 void
64 AudioMapping::make_default ()
65 {
66         for (int i = 0; i < _content_channels; ++i) {
67                 for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) {
68                         _gain[i][j] = 0;
69                 }
70         }
71
72         if (_content_channels == 1) {
73                 /* Mono -> Centre */
74                 set (0, dcp::CENTRE, 1);
75         } else {
76                 /* 1:1 mapping */
77                 for (int i = 0; i < min (_content_channels, MAX_DCP_AUDIO_CHANNELS); ++i) {
78                         set (i, static_cast<dcp::Channel> (i), 1);
79                 }
80         }
81 }
82
83 AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
84 {
85         setup (node->number_child<int> ("ContentChannels"));
86
87         if (state_version <= 5) {
88                 /* Old-style: on/off mapping */
89                 list<cxml::NodePtr> const c = node->node_children ("Map");
90                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
91                         set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
92                 }
93         } else {
94                 list<cxml::NodePtr> const c = node->node_children ("Gain");
95                 for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
96                         set (
97                                 (*i)->number_attribute<int> ("Content"),
98                                 static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
99                                 raw_convert<float> ((*i)->content ())
100                                 );
101                 }
102         }
103 }
104
105 void
106 AudioMapping::set (int c, dcp::Channel d, float g)
107 {
108         _gain[c][d] = g;
109 }
110
111 float
112 AudioMapping::get (int c, dcp::Channel d) const
113 {
114         return _gain[c][d];
115 }
116
117 void
118 AudioMapping::as_xml (xmlpp::Node* node) const
119 {
120         node->add_child ("ContentChannels")->add_child_text (raw_convert<string> (_content_channels));
121
122         for (int c = 0; c < _content_channels; ++c) {
123                 for (int d = 0; d < MAX_DCP_AUDIO_CHANNELS; ++d) {
124                         xmlpp::Element* t = node->add_child ("Gain");
125                         t->set_attribute ("Content", raw_convert<string> (c));
126                         t->set_attribute ("DCP", raw_convert<string> (d));
127                         t->add_child_text (raw_convert<string> (get (c, static_cast<dcp::Channel> (d))));
128                 }
129         }
130 }
131
132 /** @return a string which is unique for a given AudioMapping configuration, for
133  *  differentiation between different AudioMappings.
134  */
135 string
136 AudioMapping::digest () const
137 {
138         MD5Digester digester;
139         digester.add (_content_channels);
140         for (int i = 0; i < _content_channels; ++i) {
141                 for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) {
142                         digester.add (_gain[i][j]);
143                 }
144         }
145
146         return digester.get ();
147 }
148
149 list<dcp::Channel>
150 AudioMapping::mapped_dcp_channels () const
151 {
152         static float const minus_96_db = 0.000015849;
153
154         list<dcp::Channel> mapped;
155         
156         for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
157                 for (size_t j = 0; j < i->size(); ++j) {
158                         if (abs ((*i)[j]) > minus_96_db) {
159                                 mapped.push_back ((dcp::Channel) j);
160                         }
161                 }
162         }
163
164         mapped.sort ();
165         mapped.unique ();
166         
167         return mapped;
168 }
169
170 void
171 AudioMapping::unmap_all ()
172 {
173         for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
174                 for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {
175                         *j = 0;
176                 }
177         }
178 }
179