/*
- Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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
*/
#include "audio_analysis.h"
-#include "dcpomatic_assert.h"
#include "cross.h"
+#include "util.h"
+#include "raw_convert.h"
+#include <libxml++/libxml++.h>
#include <boost/filesystem.hpp>
+#include <boost/foreach.hpp>
#include <stdint.h>
-#include <inttypes.h>
#include <cmath>
-#include <cassert>
#include <cstdio>
#include <iostream>
+#include <inttypes.h>
using std::ostream;
using std::istream;
using std::cout;
using std::max;
using std::list;
+using boost::shared_ptr;
AudioPoint::AudioPoint ()
{
}
}
-AudioPoint::AudioPoint (FILE* f)
+AudioPoint::AudioPoint (cxml::ConstNodePtr node)
{
- for (int i = 0; i < COUNT; ++i) {
- int n = fscanf (f, "%f", &_data[i]);
- if (n != 1) {
- _data[i] = 0;
- }
- }
+ _data[PEAK] = node->number_child<float> ("Peak");
+ _data[RMS] = node->number_child<float> ("RMS");
}
AudioPoint::AudioPoint (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<string> (_data[PEAK]));
+ parent->add_child ("RMS")->add_child_text (raw_convert<string> (_data[RMS]));
}
-
AudioAnalysis::AudioAnalysis (int channels)
{
_data.resize (channels);
AudioAnalysis::AudioAnalysis (boost::filesystem::path filename)
{
- FILE* f = fopen_boost (filename, "r");
- if (!f) {
- throw OpenFileError (filename);
- }
+ cxml::Document f ("AudioAnalysis");
+ f.read_file (filename);
- int channels = 0;
- fscanf (f, "%d", &channels);
- _data.resize (channels);
+ BOOST_FOREACH (cxml::NodePtr i, f.node_children ("Channel")) {
+ vector<AudioPoint> 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));
}
- }
- /* These may not exist in old analysis files, so be careful
- about reading them.
- */
-
- float peak;
- DCPTime::Type peak_time;
- if (fscanf (f, "%f%" SCNd64, &peak, &peak_time) == 2) {
- _peak = peak;
- _peak_time = DCPTime (peak_time);
+ _data.push_back (channel);
}
-
- fclose (f);
+
+ _peak = f.number_child<float> ("Peak");
+ _peak_time = DCPTime (f.number_child<DCPTime::Type> ("PeakTime"));
}
void
void
AudioAnalysis::write (boost::filesystem::path filename)
{
- boost::filesystem::path tmp = filename;
- tmp.replace_extension (".tmp");
-
- FILE* f = fopen_boost (tmp, "w");
- if (!f) {
- throw OpenFileError (tmp);
- }
+ shared_ptr<xmlpp::Document> doc (new xmlpp::Document);
+ xmlpp::Element* root = doc->create_root_node ("AudioAnalysis");
- fprintf (f, "%ld\n", _data.size ());
- for (vector<vector<AudioPoint> >::iterator i = _data.begin(); i != _data.end(); ++i) {
- fprintf (f, "%ld\n", i->size ());
- for (vector<AudioPoint>::iterator j = i->begin(); j != i->end(); ++j) {
- j->write (f);
+ BOOST_FOREACH (vector<AudioPoint>& i, _data) {
+ xmlpp::Element* channel = root->add_child ("Channel");
+ BOOST_FOREACH (AudioPoint& j, i) {
+ j.as_xml (channel->add_child ("Point"));
}
}
if (_peak) {
- fprintf (f, "%f%" PRId64, _peak.get (), _peak_time.get().get ());
+ root->add_child("Peak")->add_child_text (raw_convert<string> (_peak.get ()));
+ root->add_child("PeakTime")->add_child_text (raw_convert<string> (_peak_time.get().get ()));
}
- fclose (f);
- boost::filesystem::rename (tmp, filename);
+ doc->write_to_file_formatted (filename.string ());
}
*/
-/** @file src/lib/audio_analysis.h
- * @brief AudioAnalysis and AudioPoint classes.
- */
-
#ifndef DCPOMATIC_AUDIO_ANALYSIS_H
#define DCPOMATIC_AUDIO_ANALYSIS_H
-#include "types.h"
+#include <vector>
+#include <list>
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
-#include <vector>
+#include <libcxml/cxml.h>
+#include "types.h"
-/** @class AudioPoint
- * @brief A single point of an audio analysis for one portion of one channel.
- */
class AudioPoint
{
public:
};
AudioPoint ();
- AudioPoint (FILE *);
+ AudioPoint (cxml::ConstNodePtr node);
AudioPoint (AudioPoint const &);
AudioPoint& operator= (AudioPoint const &);
- void write (FILE *) const;
+ void as_xml (xmlpp::Element *) const;
float& operator[] (int t) {
return _data[t];
float _data[COUNT];
};
-/** @class AudioAnalysis
- * @brief An analysis of the audio data in a piece of AudioContent.
- *
- * This is a set of AudioPoints for each channel. The AudioPoints
- * each represent some measurement of the audio over a portion of the
- * content. For example each AudioPoint may give the RMS level of
- * a 1-minute portion of the audio.
- */
class AudioAnalysis : public boost::noncopyable
{
public: