Hand-apply d849d411cff28ef5453085791d0b4d7cd73bd070 from master; replace all assert...
[dcpomatic.git] / src / lib / audio_analysis.cc
index 4a710f4c1849481ca028f42cd55b116adaacdae0..597c04a222cd278ebfd95ccab5b54a2bc246c7d7 100644 (file)
 
 */
 
+#include "audio_analysis.h"
+#include "dcpomatic_assert.h"
+#include "cross.h"
+#include <boost/filesystem.hpp>
 #include <stdint.h>
+#include <cmath>
 #include <cassert>
-#include <fstream>
-#include "audio_analysis.h"
+#include <cstdio>
+#include <iostream>
 
 using std::ostream;
+using std::istream;
 using std::string;
-using std::ofstream;
 using std::vector;
+using std::cout;
+using std::max;
+using std::list;
 
 AudioPoint::AudioPoint ()
 {
@@ -34,11 +42,42 @@ AudioPoint::AudioPoint ()
        }
 }
 
+AudioPoint::AudioPoint (FILE* f)
+{
+       for (int i = 0; i < COUNT; ++i) {
+               int n = fscanf (f, "%f", &_data[i]);
+               if (n != 1) {
+                       _data[i] = 0;
+               }
+       }
+}
+
+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
+AudioPoint::write (FILE* f) const
 {
        for (int i = 0; i < COUNT; ++i) {
-               s << _data[i] << "\n";
+               fprintf (f, "%f\n", _data[i]);
        }
 }
        
@@ -48,22 +87,77 @@ AudioAnalysis::AudioAnalysis (int channels)
        _data.resize (channels);
 }
 
+AudioAnalysis::AudioAnalysis (boost::filesystem::path filename)
+{
+       FILE* f = fopen_boost (filename, "r");
+
+       int channels = 0;
+       fscanf (f, "%d", &channels);
+       _data.resize (channels);
+
+       for (int i = 0; i < channels; ++i) {
+               int points;
+               fscanf (f, "%d", &points);
+               if (feof (f)) {
+                       fclose (f);
+                       return;
+               }
+               
+               for (int j = 0; j < points; ++j) {
+                       _data[i].push_back (AudioPoint (f));
+                       if (feof (f)) {
+                               fclose (f);
+                               return;
+                       }
+               }
+       }
+
+       fclose (f);
+}
+
 void
 AudioAnalysis::add_point (int c, AudioPoint const & p)
 {
-       assert (c < int (_data.size ()));
+       DCPOMATIC_ASSERT (c < channels ());
        _data[c].push_back (p);
 }
 
+AudioPoint
+AudioAnalysis::get_point (int c, int p) const
+{
+       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 ());
+       return _data[c].size ();
+}
+
 void
-AudioAnalysis::write (string filename)
+AudioAnalysis::write (boost::filesystem::path filename)
 {
-       ofstream f (filename.c_str ());
-       f << _data.size() << "\n";
+       boost::filesystem::path tmp = filename;
+       tmp.replace_extension (".tmp");
+
+       FILE* f = fopen_boost (tmp, "w");
+
+       fprintf (f, "%ld\n", _data.size ());
        for (vector<vector<AudioPoint> >::iterator i = _data.begin(); i != _data.end(); ++i) {
-               f << i->size () << "\n";
+               fprintf (f, "%ld\n", i->size ());
                for (vector<AudioPoint>::iterator j = i->begin(); j != i->end(); ++j) {
                        j->write (f);
                }
        }
+
+       fclose (f);
+       boost::filesystem::rename (tmp, filename);
 }