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