Fix ID numbering.
[dcpomatic.git] / src / lib / audio_mapping.cc
index 969397b0bdf9a1a43f696cfc9d4b22ac04e125ef..4e5a8afa20e6b890046e4f489be31545f0171a5b 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
 */
 
-#include <boost/lexical_cast.hpp>
-#include <libxml++/libxml++.h>
-#include <libcxml/cxml.h>
 #include "audio_mapping.h"
 #include "util.h"
+#include "md5_digester.h"
+#include "raw_convert.h"
+#include <libcxml/cxml.h>
+#include <libxml++/libxml++.h>
 
 using std::list;
 using std::cout;
@@ -29,8 +30,8 @@ using std::make_pair;
 using std::pair;
 using std::string;
 using std::min;
+using std::vector;
 using boost::shared_ptr;
-using boost::lexical_cast;
 using boost::dynamic_pointer_cast;
 
 AudioMapping::AudioMapping ()
@@ -39,7 +40,7 @@ AudioMapping::AudioMapping ()
 
 }
 
-/** Create a default AudioMapping for a given channel count.
+/** Create an empty AudioMapping for a given channel count.
  *  @param channels Number of channels.
  */
 AudioMapping::AudioMapping (int channels)
@@ -78,7 +79,7 @@ AudioMapping::make_default ()
        }
 }
 
-AudioMapping::AudioMapping (shared_ptr<const cxml::Node> node, int state_version)
+AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
 {
        setup (node->number_child<int> ("ContentChannels"));
 
@@ -94,7 +95,7 @@ AudioMapping::AudioMapping (shared_ptr<const cxml::Node> node, int state_version
                        set (
                                (*i)->number_attribute<int> ("Content"),
                                static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
-                               lexical_cast<float> ((*i)->content ())
+                               raw_convert<float> ((*i)->content ())
                                );
                }
        }
@@ -115,14 +116,63 @@ AudioMapping::get (int c, dcp::Channel d) const
 void
 AudioMapping::as_xml (xmlpp::Node* node) const
 {
-       node->add_child ("ContentChannels")->add_child_text (lexical_cast<string> (_content_channels));
+       node->add_child ("ContentChannels")->add_child_text (raw_convert<string> (_content_channels));
 
        for (int c = 0; c < _content_channels; ++c) {
                for (int d = 0; d < MAX_DCP_AUDIO_CHANNELS; ++d) {
                        xmlpp::Element* t = node->add_child ("Gain");
-                       t->set_attribute ("Content", lexical_cast<string> (c));
-                       t->set_attribute ("DCP", lexical_cast<string> (d));
-                       t->add_child_text (lexical_cast<string> (get (c, static_cast<dcp::Channel> (d))));
+                       t->set_attribute ("Content", raw_convert<string> (c));
+                       t->set_attribute ("DCP", raw_convert<string> (d));
+                       t->add_child_text (raw_convert<string> (get (c, static_cast<dcp::Channel> (d))));
                }
        }
 }
+
+/** @return a string which is unique for a given AudioMapping configuration, for
+ *  differentiation between different AudioMappings.
+ */
+string
+AudioMapping::digest () const
+{
+       MD5Digester digester;
+       digester.add (_content_channels);
+       for (int i = 0; i < _content_channels; ++i) {
+               for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) {
+                       digester.add (_gain[i][j]);
+               }
+       }
+
+       return digester.get ();
+}
+
+list<dcp::Channel>
+AudioMapping::mapped_dcp_channels () const
+{
+       static float const minus_96_db = 0.000015849;
+
+       list<dcp::Channel> mapped;
+       
+       for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
+               for (size_t j = 0; j < i->size(); ++j) {
+                       if (abs ((*i)[j]) > minus_96_db) {
+                               mapped.push_back ((dcp::Channel) j);
+                       }
+               }
+       }
+
+       mapped.sort ();
+       mapped.unique ();
+       
+       return mapped;
+}
+
+void
+AudioMapping::unmap_all ()
+{
+       for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
+               for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {
+                       *j = 0;
+               }
+       }
+}
+