Setup DCP name after changing 3D switch.
[dcpomatic.git] / src / lib / audio_analysis.cc
index 39c1ba2261a910924ed41e9024ccfc03d9b5b1e1..bc59bcccaa56539a6f8a4a1728c2d9e50e9e1a50 100644 (file)
@@ -21,6 +21,7 @@
 #include <cmath>
 #include <cassert>
 #include <fstream>
+#include <boost/filesystem.hpp>
 #include "audio_analysis.h"
 
 using std::ostream;
@@ -30,6 +31,8 @@ using std::ofstream;
 using std::ifstream;
 using std::vector;
 using std::cout;
+using std::max;
+using std::list;
 
 AudioPoint::AudioPoint ()
 {
@@ -45,6 +48,27 @@ AudioPoint::AudioPoint (istream& s)
        }
 }
 
+AudioPoint::AudioPoint (AudioPoint const & other)
+{
+       for (int i = 0; i < COUNT; ++i) {
+               _data[i] = other._data[i];
+       }
+}
+
+AudioPoint &
+AudioPoint::operator= (AudioPoint const & other)
+{
+       if (this == &other) {
+               return *this;
+       }
+       
+       for (int i = 0; i < COUNT; ++i) {
+               _data[i] = other._data[i];
+       }
+
+       return *this;
+}
+
 void
 AudioPoint::write (ostream& s) const
 {
@@ -59,9 +83,9 @@ AudioAnalysis::AudioAnalysis (int channels)
        _data.resize (channels);
 }
 
-AudioAnalysis::AudioAnalysis (string filename)
+AudioAnalysis::AudioAnalysis (boost::filesystem::path filename)
 {
-       ifstream f (filename.c_str ());
+       ifstream f (filename.string().c_str ());
 
        int channels;
        f >> channels;
@@ -79,29 +103,36 @@ AudioAnalysis::AudioAnalysis (string filename)
 void
 AudioAnalysis::add_point (int c, AudioPoint const & p)
 {
-       assert (c < int (_data.size ()));
+       assert (c < channels ());
        _data[c].push_back (p);
 }
 
 AudioPoint
 AudioAnalysis::get_point (int c, int p) const
 {
-       assert (c < int (_data.size ()));
-       assert (p < int (_data[c].size ()));
+       assert (p < points (c));
        return _data[c][p];
 }
 
+int
+AudioAnalysis::channels () const
+{
+       return _data.size ();
+}
+
 int
 AudioAnalysis::points (int c) const
 {
-       assert (c < int (_data.size ()));
+       assert (c < channels ());
        return _data[c].size ();
 }
 
 void
-AudioAnalysis::write (string filename)
+AudioAnalysis::write (boost::filesystem::path filename)
 {
-       ofstream f (filename.c_str ());
+       string tmp = filename.string() + ".tmp";
+
+       ofstream f (tmp.c_str ());
        f << _data.size() << "\n";
        for (vector<vector<AudioPoint> >::iterator i = _data.begin(); i != _data.end(); ++i) {
                f << i->size () << "\n";
@@ -109,4 +140,7 @@ AudioAnalysis::write (string filename)
                        j->write (f);
                }
        }
+
+       f.close ();
+       boost::filesystem::rename (tmp, filename);
 }