X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Faudio_mapping.cc;h=e86e2e2ac6820499cd551ddaed17ffe8e7f30019;hb=04eebe07e2f64bd15f6887a10ab51fb92c52901a;hp=3fc423e101a891074eada7261bff77d622311cba;hpb=8750efb9e072cf3b42e6c3c29521c7031c0b5dfd;p=dcpomatic.git diff --git a/src/lib/audio_mapping.cc b/src/lib/audio_mapping.cc index 3fc423e10..e86e2e2ac 100644 --- a/src/lib/audio_mapping.cc +++ b/src/lib/audio_mapping.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 Carl Hetherington + Copyright (C) 2013-2014 Carl Hetherington 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 @@ -17,87 +17,130 @@ */ +#include +#include +#include #include "audio_mapping.h" - -using std::map; -using boost::optional; - -AutomaticAudioMapping::AutomaticAudioMapping (int c) - : _source_channels (c) +#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::dynamic_pointer_cast; +using dcp::raw_convert; + +AudioMapping::AudioMapping () + : _content_channels (0) { } -optional -AutomaticAudioMapping::source_to_dcp (int c) const +/** Create an empty AudioMapping for a given channel count. + * @param channels Number of channels. + */ +AudioMapping::AudioMapping (int channels) { - if (c >= _source_channels) { - return optional (); - } + setup (channels); +} - if (_source_channels == 1) { - /* mono sources to centre */ - return libdcp::CENTRE; - } +void +AudioMapping::setup (int c) +{ + _content_channels = c; - return static_cast (c); + _gain.resize (_content_channels); + for (int i = 0; i < _content_channels; ++i) { + _gain[i].resize (MAX_DCP_AUDIO_CHANNELS); + } } -optional -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 (); + for (int i = 0; i < _content_channels; ++i) { + for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) { + _gain[i][j] = 0; } } - if (static_cast (c) >= _source_channels) { - return optional (); + 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 (i), 1); + } } - - return static_cast (c); } -int -AutomaticAudioMapping::dcp_channels () const +AudioMapping::AudioMapping (cxml::ConstNodePtr node, int state_version) { - 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; + setup (node->number_child ("ContentChannels")); + + if (state_version <= 5) { + /* Old-style: on/off mapping */ + list const c = node->node_children ("Map"); + for (list::const_iterator i = c.begin(); i != c.end(); ++i) { + set ((*i)->number_child ("ContentIndex"), static_cast ((*i)->number_child ("DCP")), 1); + } + } else { + list const c = node->node_children ("Gain"); + for (list::const_iterator i = c.begin(); i != c.end(); ++i) { + set ( + (*i)->number_attribute ("Content"), + static_cast ((*i)->number_attribute ("DCP")), + raw_convert ((*i)->content ()) + ); + } } +} - return _source_channels; +void +AudioMapping::set (int c, dcp::Channel d, float g) +{ + _gain[c][d] = g; } -optional -ConfiguredAudioMapping::dcp_to_source (libdcp::Channel c) const +float +AudioMapping::get (int c, dcp::Channel d) const { - map::const_iterator i = _source_to_dcp.begin (); - while (i != _source_to_dcp.end() && i->second != c) { - ++i; - } + return _gain[c][d]; +} - if (i == _source_to_dcp.end ()) { - return boost::none; +void +AudioMapping::as_xml (xmlpp::Node* node) const +{ + node->add_child ("ContentChannels")->add_child_text (raw_convert (_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 (c)); + t->set_attribute ("DCP", raw_convert (d)); + t->add_child_text (raw_convert (get (c, static_cast (d)))); + } } - - return i->first; } -optional -ConfiguredAudioMapping::source_to_dcp (int c) const +/** @return a string which is unique for a given AudioMapping configuration, for + * differentiation between different AudioMappings. + */ +string +AudioMapping::digest () const { - map::const_iterator i = _source_to_dcp.find (c); - if (i == _source_to_dcp.end ()) { - return boost::none; + 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 i->second; + return digester.get (); } - -