Merge branch 'master' into 1.0
[dcpomatic.git] / src / lib / audio_mapping.cc
index 3fc423e101a891074eada7261bff77d622311cba..83c748f1aada87d374894d3cb9ff5d1d31c4dca0 100644 (file)
 
 */
 
+#include <boost/lexical_cast.hpp>
+#include <libcxml/cxml.h>
 #include "audio_mapping.h"
 
-using std::map;
-using boost::optional;
-
-AutomaticAudioMapping::AutomaticAudioMapping (int c)
-       : _source_channels (c)
+using std::list;
+using std::cout;
+using std::make_pair;
+using std::pair;
+using std::string;
+using boost::shared_ptr;
+using boost::lexical_cast;
+using boost::dynamic_pointer_cast;
+
+void
+AudioMapping::add (Channel c, libdcp::Channel d)
 {
-
+       _content_to_dcp.push_back (make_pair (c, d));
 }
 
-optional<libdcp::Channel>
-AutomaticAudioMapping::source_to_dcp (int c) const
+/* XXX: this is grotty */
+int
+AudioMapping::dcp_channels () const
 {
-       if (c >= _source_channels) {
-               return optional<libdcp::Channel> ();
+       for (list<pair<Channel, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
+               if (((int) i->second) >= 2) {
+                       return 6;
+               }
        }
 
-       if (_source_channels == 1) {
-               /* mono sources to centre */
-               return libdcp::CENTRE;
-       }
-       
-       return static_cast<libdcp::Channel> (c);
+       return 2;
 }
 
-optional<int>
-AutomaticAudioMapping::dcp_to_source (libdcp::Channel c) const
+list<AudioMapping::Channel>
+AudioMapping::dcp_to_content (libdcp::Channel d) const
 {
-       if (_source_channels == 1) {
-               if (c == libdcp::CENTRE) {
-                       return 0;
-               } else {
-                       return optional<int> ();
+       list<AudioMapping::Channel> c;
+       for (list<pair<Channel, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
+               if (i->second == d) {
+                       c.push_back (i->first);
                }
        }
 
-       if (static_cast<int> (c) >= _source_channels) {
-               return optional<int> ();
-       }
-       
-       return static_cast<int> (c);
+       return c;
 }
 
-int
-AutomaticAudioMapping::dcp_channels () const
+list<AudioMapping::Channel>
+AudioMapping::content_channels () const
 {
-       if (_source_channels == 1) {
-               /* The source is mono, so to put the mono channel into
-                  the centre we need to generate a 5.1 soundtrack.
-               */
-               return 6;
+       list<AudioMapping::Channel> c;
+       for (list<pair<Channel, 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);
+               }
        }
 
-       return _source_channels;
+       return c;
 }
 
-optional<int>
-ConfiguredAudioMapping::dcp_to_source (libdcp::Channel c) const
+list<libdcp::Channel>
+AudioMapping::content_to_dcp (Channel c) const
 {
-       map<int, libdcp::Channel>::const_iterator i = _source_to_dcp.begin ();
-       while (i != _source_to_dcp.end() && i->second != c) {
-               ++i;
-       }
-
-       if (i == _source_to_dcp.end ()) {
-               return boost::none;
+       list<libdcp::Channel> d;
+       for (list<pair<Channel, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
+               if (i->first == c) {
+                       d.push_back (i->second);
+               }
        }
 
-       return i->first;
+       return d;
 }
 
-optional<libdcp::Channel>
-ConfiguredAudioMapping::source_to_dcp (int c) const
+void
+AudioMapping::as_xml (xmlpp::Node* node) const
 {
-       map<int, libdcp::Channel>::const_iterator i = _source_to_dcp.find (c);
-       if (i == _source_to_dcp.end ()) {
-               return boost::none;
+       for (list<pair<Channel, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
+               xmlpp::Node* t = node->add_child ("Map");
+               shared_ptr<const AudioContent> c = i->first.content.lock ();
+               t->add_child ("Content")->add_child_text (c->digest ());
+               t->add_child ("ContentIndex")->add_child_text (lexical_cast<string> (i->first.index));
+               t->add_child ("DCP")->add_child_text (lexical_cast<string> (i->second));
        }
+}
+
+void
+AudioMapping::set_from_xml (ContentList const & content, shared_ptr<const cxml::Node> node)
+{
+       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) {
+               string const c = (*i)->string_child ("Content");
+               ContentList::const_iterator j = content.begin ();
+               while (j != content.end() && (*j)->digest() != c) {
+                       ++j;
+               }
+
+               if (j == content.end ()) {
+                       continue;
+               }
+
+               shared_ptr<const AudioContent> ac = dynamic_pointer_cast<AudioContent> (*j);
+               assert (ac);
 
-       return i->second;
+               add (AudioMapping::Channel (ac, (*i)->number_child<int> ("ContentIndex")), static_cast<libdcp::Channel> ((*i)->number_child<int> ("DCP")));
+       }
 }
 
-       
+bool
+operator== (AudioMapping::Channel const & a, AudioMapping::Channel const & b)
+{
+       shared_ptr<const AudioContent> sa = a.content.lock ();
+       shared_ptr<const AudioContent> sb = b.content.lock ();
+       return sa == sb && a.index == b.index;
+}