Merge 1.0.
[dcpomatic.git] / src / lib / audio_mapping.cc
index 48cc23307504457f1e94a7724635e852ba0d052d..7a5da7d2a4571445fd8d253393d7043d2f89a790 100644 (file)
 
 */
 
+#include <boost/lexical_cast.hpp>
+#include <libxml++/libxml++.h>
+#include <libcxml/cxml.h>
 #include "audio_mapping.h"
 
 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)
+AudioMapping::AudioMapping ()
+       : _content_channels (0)
 {
-       _content_to_dcp.push_back (make_pair (c, d));
+
 }
 
-/* XXX: this is grotty */
-int
-AudioMapping::dcp_channels () const
+/** Create a default AudioMapping for a given channel count.
+ *  @param c Number of channels.
+ */
+AudioMapping::AudioMapping (int c)
+       : _content_channels (c)
 {
-       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;
-               }
-       }
 
-       return 2;
 }
 
-list<AudioMapping::Channel>
-AudioMapping::dcp_to_content (libdcp::Channel d) const
+void
+AudioMapping::make_default ()
 {
-       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 (_content_channels == 1) {
+               /* Mono -> Centre */
+               add (0, libdcp::CENTRE);
+       } else {
+               /* 1:1 mapping */
+               for (int i = 0; i < _content_channels; ++i) {
+                       add (i, static_cast<libdcp::Channel> (i));
                }
        }
+}
 
-       return c;
+AudioMapping::AudioMapping (shared_ptr<const cxml::Node> node)
+{
+       _content_channels = node->number_child<int> ("ContentChannels");
+       
+       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")));
+       }
 }
 
-list<AudioMapping::Channel>
-AudioMapping::content_channels () const
+void
+AudioMapping::add (int c, libdcp::Channel d)
 {
-       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 ()) {
+       _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);
                }
        }
@@ -71,10 +90,12 @@ AudioMapping::content_channels () const
 }
 
 list<libdcp::Channel>
-AudioMapping::content_to_dcp (Channel c) const
+AudioMapping::content_to_dcp (int c) const
 {
+       assert (c < _content_channels);
+       
        list<libdcp::Channel> d;
-       for (list<pair<Channel, libdcp::Channel> >::const_iterator i = _content_to_dcp.begin(); i != _content_to_dcp.end(); ++i) {
+       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);
                }
@@ -83,13 +104,14 @@ AudioMapping::content_to_dcp (Channel c) const
        return d;
 }
 
-bool
-operator== (AudioMapping::Channel const & a, AudioMapping::Channel const & b)
+void
+AudioMapping::as_xml (xmlpp::Node* node) const
 {
-       shared_ptr<const AudioContent> sa = a.content.lock ();
-       shared_ptr<const AudioContent> sb = b.content.lock ();
-       return sa == sb && a.index == b.index;
-}
-
-       
+       node->add_child ("ContentChannels")->add_child_text (lexical_cast<string> (_content_channels));
        
+       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));
+       }
+}