Merge.
[dcpomatic.git] / src / lib / audio_analysis.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <stdint.h>
21 #include <cmath>
22 #include <cassert>
23 #include <cstdio>
24 #include <boost/filesystem.hpp>
25 #include "audio_analysis.h"
26 #include "cross.h"
27
28 using std::ostream;
29 using std::istream;
30 using std::string;
31 using std::vector;
32 using std::cout;
33 using std::max;
34 using std::list;
35
36 AudioPoint::AudioPoint ()
37 {
38         for (int i = 0; i < COUNT; ++i) {
39                 _data[i] = 0;
40         }
41 }
42
43 AudioPoint::AudioPoint (FILE* f)
44 {
45         for (int i = 0; i < COUNT; ++i) {
46                 fscanf (f, "%f", &_data[i]);
47         }
48 }
49
50 AudioPoint::AudioPoint (AudioPoint const & other)
51 {
52         for (int i = 0; i < COUNT; ++i) {
53                 _data[i] = other._data[i];
54         }
55 }
56
57 AudioPoint &
58 AudioPoint::operator= (AudioPoint const & other)
59 {
60         if (this == &other) {
61                 return *this;
62         }
63         
64         for (int i = 0; i < COUNT; ++i) {
65                 _data[i] = other._data[i];
66         }
67
68         return *this;
69 }
70
71 void
72 AudioPoint::write (FILE* f) const
73 {
74         for (int i = 0; i < COUNT; ++i) {
75                 fprintf (f, "%f\n", _data[i]);
76         }
77 }
78         
79
80 AudioAnalysis::AudioAnalysis (int channels)
81 {
82         _data.resize (channels);
83 }
84
85 AudioAnalysis::AudioAnalysis (boost::filesystem::path filename)
86 {
87         FILE* f = fopen_boost (filename, "r");
88
89         int channels;
90         fscanf (f, "%d", &channels);
91         _data.resize (channels);
92
93         for (int i = 0; i < channels; ++i) {
94                 int points;
95                 fscanf (f, "%d", &points);
96                 if (feof (f)) {
97                         fclose (f);
98                         return;
99                 }
100                 
101                 for (int j = 0; j < points; ++j) {
102                         _data[i].push_back (AudioPoint (f));
103                         if (feof (f)) {
104                                 fclose (f);
105                                 return;
106                         }
107                 }
108         }
109
110         fclose (f);
111 }
112
113 void
114 AudioAnalysis::add_point (int c, AudioPoint const & p)
115 {
116         assert (c < channels ());
117         _data[c].push_back (p);
118 }
119
120 AudioPoint
121 AudioAnalysis::get_point (int c, int p) const
122 {
123         assert (p < points (c));
124         return _data[c][p];
125 }
126
127 int
128 AudioAnalysis::channels () const
129 {
130         return _data.size ();
131 }
132
133 int
134 AudioAnalysis::points (int c) const
135 {
136         assert (c < channels ());
137         return _data[c].size ();
138 }
139
140 void
141 AudioAnalysis::write (boost::filesystem::path filename)
142 {
143         boost::filesystem::path tmp = filename;
144         tmp.replace_extension (".tmp");
145
146         FILE* f = fopen_boost (tmp, "w");
147
148         fprintf (f, "%ld\n", _data.size ());
149         for (vector<vector<AudioPoint> >::iterator i = _data.begin(); i != _data.end(); ++i) {
150                 fprintf (f, "%ld\n", i->size ());
151                 for (vector<AudioPoint>::iterator j = i->begin(); j != i->end(); ++j) {
152                         j->write (f);
153                 }
154         }
155
156         fclose (f);
157         boost::filesystem::rename (tmp, filename);
158 }