X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Faudio_analysis.cc;h=7e1dc6e7884f619765c4a927bb31af85ddde9871;hb=e60bb3e51bd1508b149e6b8f6608f09b5196ae26;hp=46477950c90b7617abecee43565d90716dccbf1d;hpb=9cb16d975e3369e4163b4e78eaa478b9ce5c57a6;p=dcpomatic.git diff --git a/src/lib/audio_analysis.cc b/src/lib/audio_analysis.cc index 46477950c..7e1dc6e78 100644 --- a/src/lib/audio_analysis.cc +++ b/src/lib/audio_analysis.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Carl Hetherington + Copyright (C) 2012-2015 Carl Hetherington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,14 +17,18 @@ */ +#include "audio_analysis.h" +#include "cross.h" +#include "util.h" +#include "raw_convert.h" +#include +#include +#include #include #include -#include #include #include -#include -#include "audio_analysis.h" -#include "cross.h" +#include using std::ostream; using std::istream; @@ -33,6 +37,7 @@ using std::vector; using std::cout; using std::max; using std::list; +using boost::shared_ptr; AudioPoint::AudioPoint () { @@ -41,11 +46,10 @@ AudioPoint::AudioPoint () } } -AudioPoint::AudioPoint (FILE* f) +AudioPoint::AudioPoint (cxml::ConstNodePtr node) { - for (int i = 0; i < COUNT; ++i) { - fscanf (f, "%f", &_data[i]); - } + _data[PEAK] = node->number_child ("Peak"); + _data[RMS] = node->number_child ("RMS"); } AudioPoint::AudioPoint (AudioPoint const & other) @@ -61,7 +65,7 @@ AudioPoint::operator= (AudioPoint const & other) if (this == &other) { return *this; } - + for (int i = 0; i < COUNT; ++i) { _data[i] = other._data[i]; } @@ -70,13 +74,11 @@ AudioPoint::operator= (AudioPoint const & other) } void -AudioPoint::write (FILE* f) const +AudioPoint::as_xml (xmlpp::Element* parent) const { - for (int i = 0; i < COUNT; ++i) { - fprintf (f, "%f\n", _data[i]); - } + parent->add_child ("Peak")->add_child_text (raw_convert (_data[PEAK])); + parent->add_child ("RMS")->add_child_text (raw_convert (_data[RMS])); } - AudioAnalysis::AudioAnalysis (int channels) { @@ -85,43 +87,34 @@ AudioAnalysis::AudioAnalysis (int channels) AudioAnalysis::AudioAnalysis (boost::filesystem::path filename) { - FILE* f = fopen_boost (filename, "r"); + cxml::Document f ("AudioAnalysis"); + f.read_file (filename); - int channels; - fscanf (f, "%d", &channels); - _data.resize (channels); + BOOST_FOREACH (cxml::NodePtr i, f.node_children ("Channel")) { + vector channel; - 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; - } + BOOST_FOREACH (cxml::NodePtr j, i->node_children ("Point")) { + channel.push_back (AudioPoint (j)); } + + _data.push_back (channel); } - fclose (f); + _peak = f.number_child ("Peak"); + _peak_time = DCPTime (f.number_child ("PeakTime")); } 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]; } @@ -134,26 +127,27 @@ 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) { - boost::filesystem::path tmp = filename; - tmp.replace_extension (".tmp"); - - FILE* f = fopen_boost (tmp, "w"); + shared_ptr doc (new xmlpp::Document); + xmlpp::Element* root = doc->create_root_node ("AudioAnalysis"); - fprintf (f, "%ld\n", _data.size ()); - for (vector >::iterator i = _data.begin(); i != _data.end(); ++i) { - fprintf (f, "%ld\n", i->size ()); - for (vector::iterator j = i->begin(); j != i->end(); ++j) { - j->write (f); + BOOST_FOREACH (vector& i, _data) { + xmlpp::Element* channel = root->add_child ("Channel"); + BOOST_FOREACH (AudioPoint& j, i) { + j.as_xml (channel->add_child ("Point")); } } - fclose (f); - boost::filesystem::rename (tmp, filename); + if (_peak) { + root->add_child("Peak")->add_child_text (raw_convert (_peak.get ())); + root->add_child("PeakTime")->add_child_text (raw_convert (_peak_time.get().get ())); + } + + doc->write_to_file_formatted (filename.string ()); }