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