X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Faudio_analysis.cc;h=1488f89fcfdfb6f2ac32aaed158e36e1a9eb589d;hb=f0e95aa5b7ada81a1c40f06facab2e94e45ab26c;hp=0cf08c5bdf1b42078f571b0b6105a99572d7e66c;hpb=1fadfdf60bb2c02086c2c9689ea44c73bed41571;p=dcpomatic.git diff --git a/src/lib/audio_analysis.cc b/src/lib/audio_analysis.cc index 0cf08c5bd..1488f89fc 100644 --- a/src/lib/audio_analysis.cc +++ b/src/lib/audio_analysis.cc @@ -20,15 +20,14 @@ #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; @@ -41,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]); } } @@ -62,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)); } @@ -107,45 +127,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); } - -float -AudioAnalysis::smooth (list const & data, AudioPoint::Type t) -{ - float val; - - switch (t) { - case AudioPoint::PEAK: - /* XXX: fall-off, or something...? */ - val = -200; - for (list::const_iterator i = data.begin(); i != data.end(); ++i) { - val = max (val, *i); - } - return val; - case AudioPoint::RMS: - val = 0; - for (list::const_iterator i = data.begin(); i != data.end(); ++i) { - val += pow (*i, 2); - } - return sqrt (val / data.size()); - default: - assert (false); - } - - return 0; -}