Restore short-cutting of analysis gain updates.
[dcpomatic.git] / src / lib / audio_analysis.cc
1 /*
2     Copyright (C) 2012-2015 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 "cross.h"
22 #include "util.h"
23 #include "raw_convert.h"
24 #include <libxml++/libxml++.h>
25 #include <boost/filesystem.hpp>
26 #include <boost/foreach.hpp>
27 #include <stdint.h>
28 #include <cmath>
29 #include <cstdio>
30 #include <iostream>
31 #include <inttypes.h>
32
33 using std::ostream;
34 using std::istream;
35 using std::string;
36 using std::vector;
37 using std::cout;
38 using std::max;
39 using std::list;
40 using boost::shared_ptr;
41
42 AudioPoint::AudioPoint ()
43 {
44         for (int i = 0; i < COUNT; ++i) {
45                 _data[i] = 0;
46         }
47 }
48
49 AudioPoint::AudioPoint (cxml::ConstNodePtr node)
50 {
51         _data[PEAK] = node->number_child<float> ("Peak");
52         _data[RMS] = node->number_child<float> ("RMS");
53 }
54
55 AudioPoint::AudioPoint (AudioPoint const & other)
56 {
57         for (int i = 0; i < COUNT; ++i) {
58                 _data[i] = other._data[i];
59         }
60 }
61
62 AudioPoint &
63 AudioPoint::operator= (AudioPoint const & other)
64 {
65         if (this == &other) {
66                 return *this;
67         }
68
69         for (int i = 0; i < COUNT; ++i) {
70                 _data[i] = other._data[i];
71         }
72
73         return *this;
74 }
75
76 void
77 AudioPoint::as_xml (xmlpp::Element* parent) const
78 {
79         parent->add_child ("Peak")->add_child_text (raw_convert<string> (_data[PEAK]));
80         parent->add_child ("RMS")->add_child_text (raw_convert<string> (_data[RMS]));
81 }
82
83 AudioAnalysis::AudioAnalysis (int channels)
84 {
85         _data.resize (channels);
86 }
87
88 AudioAnalysis::AudioAnalysis (boost::filesystem::path filename)
89 {
90         cxml::Document f ("AudioAnalysis");
91         f.read_file (filename);
92
93         BOOST_FOREACH (cxml::NodePtr i, f.node_children ("Channel")) {
94                 vector<AudioPoint> channel;
95
96                 BOOST_FOREACH (cxml::NodePtr j, i->node_children ("Point")) {
97                         channel.push_back (AudioPoint (j));
98                 }
99
100                 _data.push_back (channel);
101         }
102
103         _peak = f.number_child<float> ("Peak");
104         _peak_time = DCPTime (f.number_child<DCPTime::Type> ("PeakTime"));
105         _analysis_gain = f.optional_number_child<double> ("AnalysisGain");
106 }
107
108 void
109 AudioAnalysis::add_point (int c, AudioPoint const & p)
110 {
111         DCPOMATIC_ASSERT (c < channels ());
112         _data[c].push_back (p);
113 }
114
115 AudioPoint
116 AudioAnalysis::get_point (int c, int p) const
117 {
118         DCPOMATIC_ASSERT (p < points (c));
119         return _data[c][p];
120 }
121
122 int
123 AudioAnalysis::channels () const
124 {
125         return _data.size ();
126 }
127
128 int
129 AudioAnalysis::points (int c) const
130 {
131         DCPOMATIC_ASSERT (c < channels ());
132         return _data[c].size ();
133 }
134
135 void
136 AudioAnalysis::write (boost::filesystem::path filename)
137 {
138         shared_ptr<xmlpp::Document> doc (new xmlpp::Document);
139         xmlpp::Element* root = doc->create_root_node ("AudioAnalysis");
140
141         BOOST_FOREACH (vector<AudioPoint>& i, _data) {
142                 xmlpp::Element* channel = root->add_child ("Channel");
143                 BOOST_FOREACH (AudioPoint& j, i) {
144                         j.as_xml (channel->add_child ("Point"));
145                 }
146         }
147
148         if (_peak) {
149                 root->add_child("Peak")->add_child_text (raw_convert<string> (_peak.get ()));
150                 root->add_child("PeakTime")->add_child_text (raw_convert<string> (_peak_time.get().get ()));
151         }
152
153         if (_analysis_gain) {
154                 root->add_child("AnalysisGain")->add_child_text (raw_convert<string> (_analysis_gain.get ()));
155         }
156
157         doc->write_to_file_formatted (filename.string ());
158 }