Merge branch 'master' into plot-audio
[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 <fstream>
24 #include "audio_analysis.h"
25
26 using std::ostream;
27 using std::istream;
28 using std::string;
29 using std::ofstream;
30 using std::ifstream;
31 using std::vector;
32 using std::cout;
33
34 AudioPoint::AudioPoint ()
35 {
36         for (int i = 0; i < COUNT; ++i) {
37                 _data[i] = 0;
38         }
39 }
40
41 AudioPoint::AudioPoint (istream& s)
42 {
43         for (int i = 0; i < COUNT; ++i) {
44                 s >> _data[i];
45         }
46 }
47
48 void
49 AudioPoint::write (ostream& s) const
50 {
51         for (int i = 0; i < COUNT; ++i) {
52                 s << _data[i] << "\n";
53         }
54 }
55         
56
57 AudioAnalysis::AudioAnalysis (int channels)
58 {
59         _data.resize (channels);
60 }
61
62 AudioAnalysis::AudioAnalysis (string filename)
63 {
64         ifstream f (filename.c_str ());
65
66         int channels;
67         f >> channels;
68         _data.resize (channels);
69
70         for (int i = 0; i < channels; ++i) {
71                 int points;
72                 f >> points;
73                 for (int j = 0; j < points; ++j) {
74                         _data[i].push_back (AudioPoint (f));
75                 }
76         }
77 }
78
79 void
80 AudioAnalysis::add_point (int c, AudioPoint const & p)
81 {
82         assert (c < int (_data.size ()));
83         _data[c].push_back (p);
84 }
85
86 AudioPoint
87 AudioAnalysis::get_point (int c, int p) const
88 {
89         assert (c < int (_data.size ()));
90         assert (p < int (_data[c].size ()));
91         return _data[c][p];
92 }
93
94 int
95 AudioAnalysis::points (int c) const
96 {
97         assert (c < int (_data.size ()));
98         return _data[c].size ();
99 }
100
101 void
102 AudioAnalysis::write (string filename)
103 {
104         ofstream f (filename.c_str ());
105         f << _data.size() << "\n";
106         for (vector<vector<AudioPoint> >::iterator i = _data.begin(); i != _data.end(); ++i) {
107                 f << i->size () << "\n";
108                 for (vector<AudioPoint>::iterator j = i->begin(); j != i->end(); ++j) {
109                         j->write (f);
110                 }
111         }
112 }