Merge branch 'master' of ssh://main.carlh.net/home/carl/git/dcpomatic
[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                 fscanf (f, "%f", &_data[i]);
48         }
49 }
50
51 AudioPoint::AudioPoint (AudioPoint const & other)
52 {
53         for (int i = 0; i < COUNT; ++i) {
54                 _data[i] = other._data[i];
55         }
56 }
57
58 AudioPoint &
59 AudioPoint::operator= (AudioPoint const & other)
60 {
61         if (this == &other) {
62                 return *this;
63         }
64         
65         for (int i = 0; i < COUNT; ++i) {
66                 _data[i] = other._data[i];
67         }
68
69         return *this;
70 }
71
72 void
73 AudioPoint::write (FILE* f) const
74 {
75         for (int i = 0; i < COUNT; ++i) {
76                 fprintf (f, "%f\n", _data[i]);
77         }
78 }
79         
80
81 AudioAnalysis::AudioAnalysis (int channels)
82 {
83         _data.resize (channels);
84 }
85
86 AudioAnalysis::AudioAnalysis (boost::filesystem::path filename)
87 {
88         FILE* f = fopen_boost (filename, "r");
89
90         int channels;
91         fscanf (f, "%d", &channels);
92         _data.resize (channels);
93
94         for (int i = 0; i < channels; ++i) {
95                 int points;
96                 fscanf (f, "%d", &points);
97                 if (feof (f)) {
98                         fclose (f);
99                         return;
100                 }
101                 
102                 for (int j = 0; j < points; ++j) {
103                         _data[i].push_back (AudioPoint (f));
104                         if (feof (f)) {
105                                 fclose (f);
106                                 return;
107                         }
108                 }
109         }
110
111         fclose (f);
112 }
113
114 void
115 AudioAnalysis::add_point (int c, AudioPoint const & p)
116 {
117         assert (c < channels ());
118         _data[c].push_back (p);
119 }
120
121 AudioPoint
122 AudioAnalysis::get_point (int c, int p) const
123 {
124         assert (p < points (c));
125         return _data[c][p];
126 }
127
128 int
129 AudioAnalysis::channels () const
130 {
131         return _data.size ();
132 }
133
134 int
135 AudioAnalysis::points (int c) const
136 {
137         assert (c < channels ());
138         return _data[c].size ();
139 }
140
141 void
142 AudioAnalysis::write (boost::filesystem::path filename)
143 {
144         boost::filesystem::path tmp = filename;
145         tmp.replace_extension (".tmp");
146
147         FILE* f = fopen_boost (tmp, "w");
148
149         fprintf (f, "%ld\n", _data.size ());
150         for (vector<vector<AudioPoint> >::iterator i = _data.begin(); i != _data.end(); ++i) {
151                 fprintf (f, "%ld\n", i->size ());
152                 for (vector<AudioPoint>::iterator j = i->begin(); j != i->end(); ++j) {
153                         j->write (f);
154                 }
155         }
156
157         fclose (f);
158         boost::filesystem::rename (tmp, filename);
159 }