Merge master.
[dcpomatic.git] / src / lib / audio_mapping.cc
index d0aa33657fb383c9fbddcdde996186a9485e4b92..1db827046946a8008d54bff21597d4fd1d201bff 100644 (file)
@@ -1,5 +1,3 @@
-/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
-
 /*
     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
 
@@ -23,6 +21,7 @@
 #include <libxml++/libxml++.h>
 #include <libcxml/cxml.h>
 #include "audio_mapping.h"
+#include "util.h"
 
 using std::list;
 using std::cout;
@@ -34,6 +33,7 @@ using boost::lexical_cast;
 using boost::dynamic_pointer_cast;
 
 AudioMapping::AudioMapping ()
+       : _content_channels (0)
 {
 
 }
@@ -43,76 +43,85 @@ AudioMapping::AudioMapping ()
  */
 AudioMapping::AudioMapping (int c)
 {
-       if (c == 1) {
-               /* Mono -> Centre */
-               add (0, libdcp::CENTRE);
-       } else {
-               /* 1:1 mapping */
-               for (int i = 0; i < c; ++i) {
-                       add (i, static_cast<libdcp::Channel> (i));
-               }
-       }
+       setup (c);
 }
 
-AudioMapping::AudioMapping (shared_ptr<const cxml::Node> node)
+void
+AudioMapping::setup (int c)
 {
-       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")));
+       _content_channels = c;
+       
+       _gain.resize (_content_channels);
+       for (int i = 0; i < _content_channels; ++i) {
+               _gain[i].resize (MAX_AUDIO_CHANNELS);
        }
 }
 
 void
-AudioMapping::add (int c, libdcp::Channel d)
+AudioMapping::make_default ()
 {
-       _content_to_dcp.push_back (make_pair (c, d));
-}
-
-list<int>
-AudioMapping::dcp_to_content (libdcp::Channel d) 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);
+       for (int i = 0; i < _content_channels; ++i) {
+               for (int j = 0; j < MAX_AUDIO_CHANNELS; ++j) {
+                       _gain[i][j] = 0;
                }
        }
 
-       return c;
+       if (_content_channels == 1) {
+               /* Mono -> Centre */
+               set (0, dcp::CENTRE, 1);
+       } else {
+               /* 1:1 mapping */
+               for (int i = 0; i < _content_channels; ++i) {
+                       set (i, static_cast<dcp::Channel> (i), 1);
+               }
+       }
 }
 
-list<int>
-AudioMapping::content_channels () const
+AudioMapping::AudioMapping (shared_ptr<const cxml::Node> node, int state_version)
 {
-       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);
+       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")),
+                               lexical_cast<float> ((*i)->content ())
+                               );
                }
        }
-
-       return c;
 }
 
-list<libdcp::Channel>
-AudioMapping::content_to_dcp (int c) const
+void
+AudioMapping::set (int c, dcp::Channel d, float g)
 {
-       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);
-               }
-       }
+       _gain[c][d] = g;
+}
 
-       return d;
+float
+AudioMapping::get (int c, dcp::Channel d) const
+{
+       return _gain[c][d];
 }
 
 void
 AudioMapping::as_xml (xmlpp::Node* node) const
 {
-       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));
+       node->add_child ("ContentChannels")->add_child_text (lexical_cast<string> (_content_channels));
+
+       for (int c = 0; c < _content_channels; ++c) {
+               for (int d = 0; d < MAX_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))));
+               }
        }
 }