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"
22 #include <boost/filesystem.hpp>
37 AudioPoint::AudioPoint ()
39 for (int i = 0; i < COUNT; ++i) {
44 AudioPoint::AudioPoint (FILE* f)
46 for (int i = 0; i < COUNT; ++i) {
47 int n = fscanf (f, "%f", &_data[i]);
54 AudioPoint::AudioPoint (AudioPoint const & other)
56 for (int i = 0; i < COUNT; ++i) {
57 _data[i] = other._data[i];
62 AudioPoint::operator= (AudioPoint const & other)
68 for (int i = 0; i < COUNT; ++i) {
69 _data[i] = other._data[i];
76 AudioPoint::write (FILE* f) const
78 for (int i = 0; i < COUNT; ++i) {
79 fprintf (f, "%f\n", _data[i]);
84 AudioAnalysis::AudioAnalysis (int channels)
86 _data.resize (channels);
89 AudioAnalysis::AudioAnalysis (boost::filesystem::path filename)
91 FILE* f = fopen_boost (filename, "r");
94 fscanf (f, "%d", &channels);
95 _data.resize (channels);
97 for (int i = 0; i < channels; ++i) {
99 fscanf (f, "%d", &points);
105 for (int j = 0; j < points; ++j) {
106 _data[i].push_back (AudioPoint (f));
118 AudioAnalysis::add_point (int c, AudioPoint const & p)
120 assert (c < channels ());
121 _data[c].push_back (p);
125 AudioAnalysis::get_point (int c, int p) const
127 assert (p < points (c));
132 AudioAnalysis::channels () const
134 return _data.size ();
138 AudioAnalysis::points (int c) const
140 assert (c < channels ());
141 return _data[c].size ();
145 AudioAnalysis::write (boost::filesystem::path filename)
147 boost::filesystem::path tmp = filename;
148 tmp.replace_extension (".tmp");
150 FILE* f = fopen_boost (tmp, "w");
152 fprintf (f, "%ld\n", _data.size ());
153 for (vector<vector<AudioPoint> >::iterator i = _data.begin(); i != _data.end(); ++i) {
154 fprintf (f, "%ld\n", i->size ());
155 for (vector<AudioPoint>::iterator j = i->begin(); j != i->end(); ++j) {
161 boost::filesystem::rename (tmp, filename);