X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Faudio_analysis.cc;h=9f92bdb503be802bcdee6aa400d0a1738b0defc3;hb=12efbd5938f08eb445b43f539fa4f27aa5caccfb;hp=b29ed1707d0e01d8d8aaa08c7ab0a27019b0c446;hpb=6e0f2a39c9deeb51f05c0c8c9bd46632c2c6483a;p=dcpomatic.git diff --git a/src/lib/audio_analysis.cc b/src/lib/audio_analysis.cc index b29ed1707..9f92bdb50 100644 --- a/src/lib/audio_analysis.cc +++ b/src/lib/audio_analysis.cc @@ -20,17 +20,19 @@ #include #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 () { @@ -39,18 +41,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]); } } @@ -60,21 +86,32 @@ 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; + 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 @@ -105,19 +142,21 @@ AudioAnalysis::points (int c) const } void -AudioAnalysis::write (string filename) +AudioAnalysis::write (boost::filesystem::path filename) { - string tmp = filename + ".tmp"; - - ofstream f (tmp.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); } } - f.close (); + fclose (f); boost::filesystem::rename (tmp, filename); }