Handle multiple audio streams in a single piece of content
[dcpomatic.git] / src / lib / audio_mapping.cc
index d0aa33657fb383c9fbddcdde996186a9485e4b92..65eb5fc962c77baa42b7fb4ca5faec9b78a47190 100644 (file)
@@ -1,7 +1,5 @@
-/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
-
 /*
-    Copyright (C) 2013 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;
 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 ()
+       : _content_channels (0)
 {
 
 }
 
-/** Create a default AudioMapping for a given channel count.
- *  @param c Number of channels.
+/** Create an empty AudioMapping for a given channel count.
+ *  @param channels Number of channels.
  */
-AudioMapping::AudioMapping (int c)
+AudioMapping::AudioMapping (int channels)
+{
+       setup (channels);
+}
+
+void
+AudioMapping::setup (int c)
+{
+       _content_channels = c;
+       
+       _gain.resize (_content_channels);
+       for (int i = 0; i < _content_channels; ++i) {
+               _gain[i].resize (MAX_DCP_AUDIO_CHANNELS);
+       }
+
+       _name.resize (_content_channels);
+
+       make_zero ();
+}
+
+void
+AudioMapping::make_zero ()
+{
+       for (int i = 0; i < _content_channels; ++i) {
+               for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) {
+                       _gain[i][j] = 0;
+               }
+       }
+}
+
+void
+AudioMapping::make_default ()
 {
-       if (c == 1) {
+       make_zero ();
+
+       if (_content_channels == 1) {
                /* Mono -> Centre */
-               add (0, libdcp::CENTRE);
+               set (0, dcp::CENTRE, 1);
        } else {
                /* 1:1 mapping */
-               for (int i = 0; i < c; ++i) {
-                       add (i, static_cast<libdcp::Channel> (i));
+               for (int i = 0; i < min (_content_channels, MAX_DCP_AUDIO_CHANNELS); ++i) {
+                       set (i, static_cast<dcp::Channel> (i), 1);
                }
        }
 }
 
-AudioMapping::AudioMapping (shared_ptr<const cxml::Node> node)
+AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
 {
-       list<shared_ptr<cxml::Node> > const c = node->node_children ("Map");
-       for (list<shared_ptr<cxml::Node> >::const_iterator i = c.begin(); i != c.end(); ++i) {
-               add ((*i)->number_child<int> ("ContentIndex"), static_cast<libdcp::Channel> ((*i)->number_child<int> ("DCP")));
+       setup (node->number_child<int> ("ContentChannels"));
+
+       if (state_version <= 5) {
+               /* Old-style: on/off mapping */
+               list<cxml::NodePtr> const c = node->node_children ("Map");
+               for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
+                       set ((*i)->number_child<int> ("ContentIndex"), static_cast<dcp::Channel> ((*i)->number_child<int> ("DCP")), 1);
+               }
+       } else {
+               list<cxml::NodePtr> const c = node->node_children ("Gain");
+               for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
+                       set (
+                               (*i)->number_attribute<int> ("Content"),
+                               static_cast<dcp::Channel> ((*i)->number_attribute<int> ("DCP")),
+                               raw_convert<float> ((*i)->content ())
+                               );
+               }
        }
 }
 
 void
-AudioMapping::add (int c, libdcp::Channel d)
+AudioMapping::set (int c, dcp::Channel d, float g)
+{
+       _gain[c][d] = g;
+}
+
+float
+AudioMapping::get (int c, dcp::Channel d) const
 {
-       _content_to_dcp.push_back (make_pair (c, d));
+       return _gain[c][d];
 }
 
-list<int>
-AudioMapping::dcp_to_content (libdcp::Channel d) const
+void
+AudioMapping::as_xml (xmlpp::Node* node) const
 {
-       list<int> c;
-       for (list<pair<int, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
-               if (i->second == d) {
-                       c.push_back (i->first);
+       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", 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 c;
 }
 
-list<int>
-AudioMapping::content_channels () const
+/** @return a string which is unique for a given AudioMapping configuration, for
+ *  differentiation between different AudioMappings.
+ */
+string
+AudioMapping::digest () const
 {
-       list<int> c;
-       for (list<pair<int, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
-               if (find (c.begin(), c.end(), i->first) == c.end ()) {
-                       c.push_back (i->first);
+       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 c;
+       return digester.get ();
 }
 
-list<libdcp::Channel>
-AudioMapping::content_to_dcp (int c) const
+list<dcp::Channel>
+AudioMapping::mapped_dcp_channels () const
 {
-       list<libdcp::Channel> d;
-       for (list<pair<int, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
-               if (i->first == c) {
-                       d.push_back (i->second);
+       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);
+                       }
                }
        }
 
-       return d;
+       mapped.sort ();
+       mapped.unique ();
+       
+       return mapped;
 }
 
 void
-AudioMapping::as_xml (xmlpp::Node* node) const
+AudioMapping::unmap_all ()
 {
-       for (list<pair<int, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
-               xmlpp::Node* t = node->add_child ("Map");
-               t->add_child ("ContentIndex")->add_child_text (lexical_cast<string> (i->first));
-               t->add_child ("DCP")->add_child_text (lexical_cast<string> (i->second));
+       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;
+               }
        }
 }
+
+void
+AudioMapping::set_name (int channel, string name)
+{
+       _name[channel] = name;
+}