X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Faudio_analysis.cc;h=1488f89fcfdfb6f2ac32aaed158e36e1a9eb589d;hb=102961229ac413e072d91a2929489a5e1450aa73;hp=39c1ba2261a910924ed41e9024ccfc03d9b5b1e1;hpb=dcd968d6d64d645816af0efbcd2f928128c95b9f;p=dcpomatic.git diff --git a/src/lib/audio_analysis.cc b/src/lib/audio_analysis.cc index 39c1ba226..1488f89fc 100644 --- a/src/lib/audio_analysis.cc +++ b/src/lib/audio_analysis.cc @@ -20,16 +20,18 @@ #include #include #include -#include +#include +#include #include "audio_analysis.h" +#include "cross.h" using std::ostream; using std::istream; using std::string; -using std::ofstream; -using std::ifstream; using std::vector; using std::cout; +using std::max; +using std::list; AudioPoint::AudioPoint () { @@ -38,18 +40,39 @@ AudioPoint::AudioPoint () } } -AudioPoint::AudioPoint (istream& s) +AudioPoint::AudioPoint (FILE* f) { for (int i = 0; i < COUNT; ++i) { - s >> _data[i]; + fscanf (f, "%f", &_data[i]); } } +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]); } } @@ -59,17 +82,17 @@ AudioAnalysis::AudioAnalysis (int channels) _data.resize (channels); } -AudioAnalysis::AudioAnalysis (string filename) +AudioAnalysis::AudioAnalysis (boost::filesystem::path filename) { - ifstream f (filename.c_str ()); + FILE* f = fopen_boost (filename, "r"); int channels; - f >> channels; + fscanf (f, "%d", &channels); _data.resize (channels); for (int i = 0; i < channels; ++i) { int points; - f >> points; + fscanf (f, "%d", &points); for (int j = 0; j < points; ++j) { _data[i].push_back (AudioPoint (f)); } @@ -79,34 +102,46 @@ 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 ()); - 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 >::iterator i = _data.begin(); i != _data.end(); ++i) { - f << i->size () << "\n"; + fprintf (f, "%ld\n", i->size ()); for (vector::iterator j = i->begin(); j != i->end(); ++j) { j->write (f); } } + + fclose (f); + boost::filesystem::rename (tmp, filename); }