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