7fd73c0cf1f723896826c77fec3720c5c404d4e8 from master; tidy audio analysis dialogue...
[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 "audio_analysis.h"
21 #include "dcpomatic_assert.h"
22 #include "cross.h"
23 #include <boost/filesystem.hpp>
24 #include <stdint.h>
25 #include <inttypes.h>
26 #include <cmath>
27 #include <cassert>
28 #include <cstdio>
29 #include <iostream>
30
31 using std::ostream;
32 using std::istream;
33 using std::string;
34 using std::vector;
35 using std::cout;
36 using std::max;
37 using std::list;
38
39 AudioPoint::AudioPoint ()
40 {
41         for (int i = 0; i < COUNT; ++i) {
42                 _data[i] = 0;
43         }
44 }
45
46 AudioPoint::AudioPoint (FILE* f)
47 {
48         for (int i = 0; i < COUNT; ++i) {
49                 int n = fscanf (f, "%f", &_data[i]);
50                 if (n != 1) {
51                         _data[i] = 0;
52                 }
53         }
54 }
55
56 AudioPoint::AudioPoint (AudioPoint const & other)
57 {
58         for (int i = 0; i < COUNT; ++i) {
59                 _data[i] = other._data[i];
60         }
61 }
62
63 AudioPoint &
64 AudioPoint::operator= (AudioPoint const & other)
65 {
66         if (this == &other) {
67                 return *this;
68         }
69         
70         for (int i = 0; i < COUNT; ++i) {
71                 _data[i] = other._data[i];
72         }
73
74         return *this;
75 }
76
77 void
78 AudioPoint::write (FILE* f) const
79 {
80         for (int i = 0; i < COUNT; ++i) {
81                 fprintf (f, "%f\n", _data[i]);
82         }
83 }
84         
85
86 AudioAnalysis::AudioAnalysis (int channels)
87 {
88         _data.resize (channels);
89 }
90
91 AudioAnalysis::AudioAnalysis (boost::filesystem::path filename)
92 {
93         FILE* f = fopen_boost (filename, "r");
94         if (!f) {
95                 throw OpenFileError (filename);
96         }
97
98         int channels = 0;
99         fscanf (f, "%d", &channels);
100         _data.resize (channels);
101
102         for (int i = 0; i < channels; ++i) {
103                 int points;
104                 fscanf (f, "%d", &points);
105                 if (feof (f)) {
106                         fclose (f);
107                         return;
108                 }
109                 
110                 for (int j = 0; j < points; ++j) {
111                         _data[i].push_back (AudioPoint (f));
112                         if (feof (f)) {
113                                 fclose (f);
114                                 return;
115                         }
116                 }
117         }
118
119         /* These may not exist in old analysis files, so be careful
120            about reading them.
121         */
122         
123         float peak;
124         DCPTime::Type peak_time;
125         if (fscanf (f, "%f%" SCNd64, &peak, &peak_time) == 2) {
126                 _peak = peak;
127                 _peak_time = DCPTime (peak_time);
128         }
129         
130         fclose (f);
131 }
132
133 void
134 AudioAnalysis::add_point (int c, AudioPoint const & p)
135 {
136         DCPOMATIC_ASSERT (c < channels ());
137         _data[c].push_back (p);
138 }
139
140 AudioPoint
141 AudioAnalysis::get_point (int c, int p) const
142 {
143         DCPOMATIC_ASSERT (p < points (c));
144         return _data[c][p];
145 }
146
147 int
148 AudioAnalysis::channels () const
149 {
150         return _data.size ();
151 }
152
153 int
154 AudioAnalysis::points (int c) const
155 {
156         DCPOMATIC_ASSERT (c < channels ());
157         return _data[c].size ();
158 }
159
160 void
161 AudioAnalysis::write (boost::filesystem::path filename)
162 {
163         boost::filesystem::path tmp = filename;
164         tmp.replace_extension (".tmp");
165
166         FILE* f = fopen_boost (tmp, "w");
167         if (!f) {
168                 throw OpenFileError (tmp);
169         }
170
171         fprintf (f, "%ld\n", _data.size ());
172         for (vector<vector<AudioPoint> >::iterator i = _data.begin(); i != _data.end(); ++i) {
173                 fprintf (f, "%ld\n", i->size ());
174                 for (vector<AudioPoint>::iterator j = i->begin(); j != i->end(); ++j) {
175                         j->write (f);
176                 }
177         }
178
179         if (_peak) {
180                 fprintf (f, "%f%" PRId64, _peak.get (), _peak_time.get().get ());
181         }
182
183         fclose (f);
184         boost::filesystem::rename (tmp, filename);
185 }