/*
- Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2013-2014 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 <dcp/raw_convert.h>
#include "audio_mapping.h"
+#include "util.h"
+#include "md5_digester.h"
using std::list;
using std::cout;
using std::make_pair;
using std::pair;
using std::string;
+using std::min;
using boost::shared_ptr;
-using boost::lexical_cast;
using boost::dynamic_pointer_cast;
+using dcp::raw_convert;
-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 an empty AudioMapping for a given channel count.
+ * @param channels Number of channels.
+ */
+AudioMapping::AudioMapping (int channels)
{
- 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;
+ setup (channels);
}
-list<AudioMapping::Channel>
-AudioMapping::dcp_to_content (libdcp::Channel d) const
+void
+AudioMapping::setup (int c)
{
- 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);
- }
+ _content_channels = c;
+
+ _gain.resize (_content_channels);
+ for (int i = 0; i < _content_channels; ++i) {
+ _gain[i].resize (MAX_DCP_AUDIO_CHANNELS);
}
-
- return c;
}
-list<AudioMapping::Channel>
-AudioMapping::content_channels () 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 (find (c.begin(), c.end(), i->first) == c.end ()) {
- c.push_back (i->first);
+ for (int i = 0; i < _content_channels; ++i) {
+ for (int j = 0; j < MAX_DCP_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 < min (_content_channels, MAX_DCP_AUDIO_CHANNELS); ++i) {
+ set (i, static_cast<dcp::Channel> (i), 1);
+ }
+ }
}
-list<libdcp::Channel>
-AudioMapping::content_to_dcp (Channel c) const
+AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version)
{
- 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);
+ 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 ())
+ );
}
}
-
- return d;
}
void
-AudioMapping::as_xml (xmlpp::Node* node) const
+AudioMapping::set (int c, dcp::Channel d, float g)
{
- 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->file().string ());
- 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));
- }
+ _gain[c][d] = g;
}
-void
-AudioMapping::set_from_xml (ContentList const & content, shared_ptr<const cxml::Node> node)
+float
+AudioMapping::get (int c, dcp::Channel d) const
{
- 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)->file().string() != c) {
- ++j;
- }
+ return _gain[c][d];
+}
- if (j == content.end ()) {
- continue;
+void
+AudioMapping::as_xml (xmlpp::Node* node) const
+{
+ 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))));
}
-
- shared_ptr<const AudioContent> ac = dynamic_pointer_cast<AudioContent> (*j);
- assert (ac);
-
- 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)
+/** @return a string which is unique for a given AudioMapping configuration, for
+ * differentiation between different AudioMappings.
+ */
+string
+AudioMapping::digest () const
{
- shared_ptr<const AudioContent> sa = a.content.lock ();
- shared_ptr<const AudioContent> sb = b.content.lock ();
- return sa == sb && a.index == b.index;
+ 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 digester.get ();
}