2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "audio_analysis.h"
21 #include "dcpomatic_assert.h"
23 #include <boost/filesystem.hpp>
38 AudioPoint::AudioPoint ()
40 for (int i = 0; i < COUNT; ++i) {
45 AudioPoint::AudioPoint (FILE* f)
47 for (int i = 0; i < COUNT; ++i) {
48 int n = fscanf (f, "%f", &_data[i]);
55 AudioPoint::AudioPoint (AudioPoint const & other)
57 for (int i = 0; i < COUNT; ++i) {
58 _data[i] = other._data[i];
63 AudioPoint::operator= (AudioPoint const & other)
69 for (int i = 0; i < COUNT; ++i) {
70 _data[i] = other._data[i];
77 AudioPoint::write (FILE* f) const
79 for (int i = 0; i < COUNT; ++i) {
80 fprintf (f, "%f\n", _data[i]);
85 AudioAnalysis::AudioAnalysis (int channels)
87 _data.resize (channels);
90 AudioAnalysis::AudioAnalysis (boost::filesystem::path filename)
92 FILE* f = fopen_boost (filename, "r");
95 fscanf (f, "%d", &channels);
96 _data.resize (channels);
98 for (int i = 0; i < channels; ++i) {
100 fscanf (f, "%d", &points);
106 for (int j = 0; j < points; ++j) {
107 _data[i].push_back (AudioPoint (f));
119 AudioAnalysis::add_point (int c, AudioPoint const & p)
121 DCPOMATIC_ASSERT (c < channels ());
122 _data[c].push_back (p);
126 AudioAnalysis::get_point (int c, int p) const
128 DCPOMATIC_ASSERT (p < points (c));
133 AudioAnalysis::channels () const
135 return _data.size ();
139 AudioAnalysis::points (int c) const
141 DCPOMATIC_ASSERT (c < channels ());
142 return _data[c].size ();
146 AudioAnalysis::write (boost::filesystem::path filename)
148 boost::filesystem::path tmp = filename;
149 tmp.replace_extension (".tmp");
151 FILE* f = fopen_boost (tmp, "w");
153 fprintf (f, "%ld\n", _data.size ());
154 for (vector<vector<AudioPoint> >::iterator i = _data.begin(); i != _data.end(); ++i) {
155 fprintf (f, "%ld\n", i->size ());
156 for (vector<AudioPoint>::iterator j = i->begin(); j != i->end(); ++j) {
162 boost::filesystem::rename (tmp, filename);