Remove uses of boost::system::error_code::message(), which seem to cause the pure...
[dcpomatic.git] / src / lib / audio_mapping.cc
index 3fc423e101a891074eada7261bff77d622311cba..3620001251ec688857c172c80c2883f1b8add42a 100644 (file)
 
 */
 
+#include <boost/lexical_cast.hpp>
+#include <libxml++/libxml++.h>
+#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;
+
+AudioMapping::AudioMapping ()
+       : _content_channels (0)
 {
 
 }
 
-optional<libdcp::Channel>
-AutomaticAudioMapping::source_to_dcp (int c) const
+/** Create a default AudioMapping for a given channel count.
+ *  @param c Number of channels.
+ */
+AudioMapping::AudioMapping (int c)
+       : _content_channels (c)
 {
-       if (c >= _source_channels) {
-               return optional<libdcp::Channel> ();
-       }
 
-       if (_source_channels == 1) {
-               /* mono sources to centre */
-               return libdcp::CENTRE;
-       }
-       
-       return static_cast<libdcp::Channel> (c);
 }
 
-optional<int>
-AutomaticAudioMapping::dcp_to_source (libdcp::Channel c) const
+void
+AudioMapping::make_default ()
 {
-       if (_source_channels == 1) {
-               if (c == libdcp::CENTRE) {
-                       return 0;
-               } else {
-                       return optional<int> ();
+       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));
                }
        }
-
-       if (static_cast<int> (c) >= _source_channels) {
-               return optional<int> ();
-       }
-       
-       return static_cast<int> (c);
 }
 
-int
-AutomaticAudioMapping::dcp_channels () const
+AudioMapping::AudioMapping (shared_ptr<const cxml::Node> node)
 {
-       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;
+       _content_channels = node->number_child<int> ("ContentChannels");
+       
+       list<cxml::NodePtr> const c = node->node_children ("Map");
+       for (list<cxml::NodePtr>::const_iterator i = c.begin(); i != c.end(); ++i) {
+               add ((*i)->number_child<int> ("ContentIndex"), static_cast<libdcp::Channel> ((*i)->number_child<int> ("DCP")));
        }
-
-       return _source_channels;
 }
 
-optional<int>
-ConfiguredAudioMapping::dcp_to_source (libdcp::Channel c) const
+void
+AudioMapping::add (int c, libdcp::Channel d)
 {
-       map<int, libdcp::Channel>::const_iterator i = _source_to_dcp.begin ();
-       while (i != _source_to_dcp.end() && i->second != c) {
-               ++i;
-       }
+       _content_to_dcp.push_back (make_pair (c, d));
+}
 
-       if (i == _source_to_dcp.end ()) {
-               return boost::none;
+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);
+               }
        }
 
-       return i->first;
+       return c;
 }
 
-optional<libdcp::Channel>
-ConfiguredAudioMapping::source_to_dcp (int c) const
+list<libdcp::Channel>
+AudioMapping::content_to_dcp (int c) const
 {
-       map<int, libdcp::Channel>::const_iterator i = _source_to_dcp.find (c);
-       if (i == _source_to_dcp.end ()) {
-               return boost::none;
+       assert (c < _content_channels);
+       
+       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);
+               }
        }
 
-       return i->second;
+       return d;
 }
 
+void
+AudioMapping::as_xml (xmlpp::Node* node) const
+{
+       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));
+       }
+}