X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Faudio_analysis.cc;h=597c04a222cd278ebfd95ccab5b54a2bc246c7d7;hb=4dbc6ef917aeceb906b1ef1caf6911033e7e2c54;hp=e1251662089bd543fc3d713eadb8748ca37c8a9d;hpb=89115db77729a2c99f1a09ff6a461720e16f889e;p=dcpomatic.git diff --git a/src/lib/audio_analysis.cc b/src/lib/audio_analysis.cc index e12516620..597c04a22 100644 --- a/src/lib/audio_analysis.cc +++ b/src/lib/audio_analysis.cc @@ -17,18 +17,19 @@ */ +#include "audio_analysis.h" +#include "dcpomatic_assert.h" +#include "cross.h" +#include #include #include #include -#include -#include -#include "audio_analysis.h" +#include +#include using std::ostream; using std::istream; using std::string; -using std::ofstream; -using std::ifstream; using std::vector; using std::cout; using std::max; @@ -41,18 +42,42 @@ AudioPoint::AudioPoint () } } -AudioPoint::AudioPoint (istream& s) +AudioPoint::AudioPoint (FILE* f) { for (int i = 0; i < COUNT; ++i) { - s >> _data[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]); } } @@ -64,32 +89,43 @@ AudioAnalysis::AudioAnalysis (int channels) AudioAnalysis::AudioAnalysis (boost::filesystem::path filename) { - ifstream f (filename.string().c_str ()); + FILE* f = fopen_boost (filename, "r"); - int channels; - f >> channels; + int channels = 0; + fscanf (f, "%d", &channels); _data.resize (channels); for (int i = 0; i < channels; ++i) { int points; - f >> 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 < channels ()); + DCPOMATIC_ASSERT (c < channels ()); _data[c].push_back (p); } AudioPoint AudioAnalysis::get_point (int c, int p) const { - assert (p < points (c)); + DCPOMATIC_ASSERT (p < points (c)); return _data[c][p]; } @@ -102,24 +138,26 @@ AudioAnalysis::channels () const int AudioAnalysis::points (int c) const { - assert (c < channels ()); + DCPOMATIC_ASSERT (c < channels ()); return _data[c].size (); } void AudioAnalysis::write (boost::filesystem::path filename) { - string tmp = filename.string() + ".tmp"; + boost::filesystem::path tmp = filename; + tmp.replace_extension (".tmp"); + + FILE* f = fopen_boost (tmp, "w"); - ofstream f (tmp.c_str ()); - f << _data.size() << "\n"; + 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); } } - f.close (); + fclose (f); boost::filesystem::rename (tmp, filename); }