C++11 tidying.
[dcpomatic.git] / src / lib / audio_analysis.cc
index 61cdd5fcce2b61a2d9d121038d4f86b6060b1281..b16fa708ad85d74bad44d134b2302745b4d8602c 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
+
 #include "audio_analysis.h"
 #include "cross.h"
 #include "util.h"
 #include "playlist.h"
 #include "audio_content.h"
+#include "warnings.h"
 #include <dcp/raw_convert.h>
+DCPOMATIC_DISABLE_WARNINGS
 #include <libxml++/libxml++.h>
+DCPOMATIC_ENABLE_WARNINGS
 #include <boost/filesystem.hpp>
-#include <boost/foreach.hpp>
 #include <stdint.h>
 #include <cmath>
 #include <cstdio>
 #include <iostream>
 #include <inttypes.h>
 
-using std::ostream;
-using std::istream;
-using std::string;
-using std::vector;
+
 using std::cout;
+using std::dynamic_pointer_cast;
+using std::istream;
+using std::list;
+using std::make_pair;
+using std::make_shared;
 using std::max;
+using std::ostream;
 using std::pair;
-using std::make_pair;
-using std::list;
-using boost::shared_ptr;
+using std::shared_ptr;
+using std::string;
+using std::vector;
 using boost::optional;
-using boost::dynamic_pointer_cast;
 using dcp::raw_convert;
+using namespace dcpomatic;
+
+
+int const AudioAnalysis::_current_state_version = 3;
 
-int const AudioAnalysis::_current_state_version = 2;
 
 AudioAnalysis::AudioAnalysis (int channels)
 {
        _data.resize (channels);
 }
 
+
 AudioAnalysis::AudioAnalysis (boost::filesystem::path filename)
 {
        cxml::Document f ("AudioAnalysis");
@@ -64,34 +73,39 @@ AudioAnalysis::AudioAnalysis (boost::filesystem::path filename)
                throw OldFormatError ("Audio analysis file is too old");
        }
 
-       BOOST_FOREACH (cxml::NodePtr i, f.node_children ("Channel")) {
+       for (auto i: f.node_children("Channel")) {
                vector<AudioPoint> channel;
 
-               BOOST_FOREACH (cxml::NodePtr j, i->node_children ("Point")) {
-                       channel.push_back (AudioPoint (j));
+               for (auto j: i->node_children("Point")) {
+                       channel.push_back (AudioPoint(j));
                }
 
                _data.push_back (channel);
        }
 
-       BOOST_FOREACH (cxml::ConstNodePtr i, f.node_children ("SamplePeak")) {
+       for (auto i: f.node_children ("SamplePeak")) {
                _sample_peak.push_back (
-                       PeakTime (
+                       PeakTime(
                                dcp::raw_convert<float>(i->content()), DCPTime(i->number_attribute<Frame>("Time"))
                                )
                        );
        }
 
-       BOOST_FOREACH (cxml::ConstNodePtr i, f.node_children ("TruePeak")) {
-               _true_peak.push_back (dcp::raw_convert<float> (i->content ()));
+       for (auto i: f.node_children("TruePeak")) {
+               _true_peak.push_back (dcp::raw_convert<float>(i->content()));
        }
 
-       _integrated_loudness = f.optional_number_child<float> ("IntegratedLoudness");
-       _loudness_range = f.optional_number_child<float> ("LoudnessRange");
+       _integrated_loudness = f.optional_number_child<float>("IntegratedLoudness");
+       _loudness_range = f.optional_number_child<float>("LoudnessRange");
+
+       _analysis_gain = f.optional_number_child<double>("AnalysisGain");
+       _samples_per_point = f.number_child<int64_t>("SamplesPerPoint");
+       _sample_rate = f.number_child<int64_t>("SampleRate");
 
-       _analysis_gain = f.optional_number_child<double> ("AnalysisGain");
+       _leqm = f.optional_number_child<double>("Leqm");
 }
 
+
 void
 AudioAnalysis::add_point (int c, AudioPoint const & p)
 {
@@ -99,48 +113,52 @@ AudioAnalysis::add_point (int c, AudioPoint const & p)
        _data[c].push_back (p);
 }
 
+
 AudioPoint
 AudioAnalysis::get_point (int c, int p) const
 {
-       DCPOMATIC_ASSERT (p < points (c));
+       DCPOMATIC_ASSERT (p < points(c));
        return _data[c][p];
 }
 
+
 int
 AudioAnalysis::channels () const
 {
        return _data.size ();
 }
 
+
 int
 AudioAnalysis::points (int c) const
 {
-       DCPOMATIC_ASSERT (c < channels ());
+       DCPOMATIC_ASSERT (c < channels());
        return _data[c].size ();
 }
 
+
 void
 AudioAnalysis::write (boost::filesystem::path filename)
 {
-       shared_ptr<xmlpp::Document> doc (new xmlpp::Document);
+       auto doc = make_shared<xmlpp::Document>();
        xmlpp::Element* root = doc->create_root_node ("AudioAnalysis");
 
-       root->add_child("Version")->add_child_text (raw_convert<string> (_current_state_version));
+       root->add_child("Version")->add_child_text(raw_convert<string>(_current_state_version));
 
-       BOOST_FOREACH (vector<AudioPoint>& i, _data) {
-               xmlpp::Element* channel = root->add_child ("Channel");
-               BOOST_FOREACH (AudioPoint& j, i) {
+       for (auto& i: _data) {
+               auto channel = root->add_child ("Channel");
+               for (auto& j: i) {
                        j.as_xml (channel->add_child ("Point"));
                }
        }
 
        for (size_t i = 0; i < _sample_peak.size(); ++i) {
-               xmlpp::Element* n = root->add_child("SamplePeak");
+               auto n = root->add_child("SamplePeak");
                n->add_child_text (raw_convert<string> (_sample_peak[i].peak));
                n->set_attribute ("Time", raw_convert<string> (_sample_peak[i].time.get()));
        }
 
-       BOOST_FOREACH (float i, _true_peak) {
+       for (auto i: _true_peak) {
                root->add_child("TruePeak")->add_child_text (raw_convert<string> (i));
        }
 
@@ -156,9 +174,17 @@ AudioAnalysis::write (boost::filesystem::path filename)
                root->add_child("AnalysisGain")->add_child_text (raw_convert<string> (_analysis_gain.get ()));
        }
 
+       root->add_child("SamplesPerPoint")->add_child_text (raw_convert<string> (_samples_per_point));
+       root->add_child("SampleRate")->add_child_text (raw_convert<string> (_sample_rate));
+
+       if (_leqm) {
+               root->add_child("Leqm")->add_child_text(raw_convert<string>(*_leqm));
+       }
+
        doc->write_to_file_formatted (filename.string ());
 }
 
+
 float
 AudioAnalysis::gain_correction (shared_ptr<const Playlist> playlist)
 {
@@ -168,17 +194,18 @@ AudioAnalysis::gain_correction (shared_ptr<const Playlist> playlist)
                   what correction is now needed to make it look `right'.
                */
                DCPOMATIC_ASSERT (playlist->content().front()->audio);
-               return playlist->content().front()->audio->gain() - analysis_gain().get ();
+               return playlist->content().front()->audio->gain() - analysis_gain().get();
        }
 
        return 0.0f;
 }
 
+
 /** @return Peak across all channels, and the channel number it is on */
 pair<AudioAnalysis::PeakTime, int>
 AudioAnalysis::overall_sample_peak () const
 {
-       DCPOMATIC_ASSERT (!_sample_peak.empty ());
+       DCPOMATIC_ASSERT (!_sample_peak.empty());
 
        optional<PeakTime> pt;
        int c = 0;
@@ -193,12 +220,13 @@ AudioAnalysis::overall_sample_peak () const
        return make_pair (pt.get(), c);
 }
 
+
 optional<float>
 AudioAnalysis::overall_true_peak () const
 {
        optional<float> p;
 
-       BOOST_FOREACH (float i, _true_peak) {
+       for (auto i: _true_peak) {
                if (!p || i > *p) {
                        p = i;
                }
@@ -206,3 +234,4 @@ AudioAnalysis::overall_true_peak () const
 
        return p;
 }
+