Handle multiple audio streams in a single piece of content
[dcpomatic.git] / src / lib / audio_mapping.cc
index e86e2e2ac6820499cd551ddaed17ffe8e7f30019..65eb5fc962c77baa42b7fb4ca5faec9b78a47190 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2015 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 <libxml++/libxml++.h>
-#include <libcxml/cxml.h>
-#include <dcp/raw_convert.h>
 #include "audio_mapping.h"
 #include "util.h"
 #include "md5_digester.h"
+#include "raw_convert.h"
+#include <libcxml/cxml.h>
+#include <libxml++/libxml++.h>
 
 using std::list;
 using std::cout;
@@ -30,9 +30,9 @@ using std::make_pair;
 using std::pair;
 using std::string;
 using std::min;
+using std::vector;
 using boost::shared_ptr;
 using boost::dynamic_pointer_cast;
-using dcp::raw_convert;
 
 AudioMapping::AudioMapping ()
        : _content_channels (0)
@@ -57,16 +57,26 @@ AudioMapping::setup (int c)
        for (int i = 0; i < _content_channels; ++i) {
                _gain[i].resize (MAX_DCP_AUDIO_CHANNELS);
        }
+
+       _name.resize (_content_channels);
+
+       make_zero ();
 }
 
 void
-AudioMapping::make_default ()
+AudioMapping::make_zero ()
 {
        for (int i = 0; i < _content_channels; ++i) {
                for (int j = 0; j < MAX_DCP_AUDIO_CHANNELS; ++j) {
                        _gain[i][j] = 0;
                }
        }
+}
+
+void
+AudioMapping::make_default ()
+{
+       make_zero ();
 
        if (_content_channels == 1) {
                /* Mono -> Centre */
@@ -144,3 +154,40 @@ AudioMapping::digest () const
 
        return digester.get ();
 }
+
+list<dcp::Channel>
+AudioMapping::mapped_dcp_channels () const
+{
+       static float const minus_96_db = 0.000015849;
+
+       list<dcp::Channel> mapped;
+       
+       for (vector<vector<float> >::const_iterator i = _gain.begin(); i != _gain.end(); ++i) {
+               for (size_t j = 0; j < i->size(); ++j) {
+                       if (abs ((*i)[j]) > minus_96_db) {
+                               mapped.push_back ((dcp::Channel) j);
+                       }
+               }
+       }
+
+       mapped.sort ();
+       mapped.unique ();
+       
+       return mapped;
+}
+
+void
+AudioMapping::unmap_all ()
+{
+       for (vector<vector<float> >::iterator i = _gain.begin(); i != _gain.end(); ++i) {
+               for (vector<float>::iterator j = i->begin(); j != i->end(); ++j) {
+                       *j = 0;
+               }
+       }
+}
+
+void
+AudioMapping::set_name (int channel, string name)
+{
+       _name[channel] = name;
+}