More player debugging for butler video-full states.
[dcpomatic.git] / src / lib / audio_analysis.h
1 /*
2     Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #ifndef DCPOMATIC_AUDIO_ANALYSIS_H
22 #define DCPOMATIC_AUDIO_ANALYSIS_H
23
24 #include "dcpomatic_time.h"
25 #include "audio_point.h"
26 #include <libcxml/cxml.h>
27 #include <boost/optional.hpp>
28 #include <boost/filesystem.hpp>
29 #include <vector>
30
31 namespace xmlpp {
32         class Element;
33 }
34
35 class Playlist;
36
37 class AudioAnalysis : public boost::noncopyable
38 {
39 public:
40         explicit AudioAnalysis (int c);
41         explicit AudioAnalysis (boost::filesystem::path);
42
43         void add_point (int c, AudioPoint const & p);
44
45         struct PeakTime {
46                 PeakTime (float p, DCPTime t)
47                         : peak (p)
48                         , time (t)
49                 {}
50
51                 float peak;
52                 DCPTime time;
53         };
54
55         void set_sample_peak (std::vector<PeakTime> peak) {
56                 _sample_peak = peak;
57         }
58
59         void set_true_peak (std::vector<float> peak) {
60                 _true_peak = peak;
61         }
62
63         void set_integrated_loudness (float l) {
64                 _integrated_loudness = l;
65         }
66
67         void set_loudness_range (float r) {
68                 _loudness_range = r;
69         }
70
71         AudioPoint get_point (int c, int p) const;
72         int points (int c) const;
73         int channels () const;
74
75         std::vector<PeakTime> sample_peak () const {
76                 return _sample_peak;
77         }
78
79         std::pair<PeakTime, int> overall_sample_peak () const;
80
81         std::vector<float> true_peak () const {
82                 return _true_peak;
83         }
84
85         boost::optional<float> overall_true_peak () const;
86
87         boost::optional<float> integrated_loudness () const {
88                 return _integrated_loudness;
89         }
90
91         boost::optional<float> loudness_range () const {
92                 return _loudness_range;
93         }
94
95         boost::optional<double> analysis_gain () const {
96                 return _analysis_gain;
97         }
98
99         void set_analysis_gain (double gain) {
100                 _analysis_gain = gain;
101         }
102
103         int64_t samples_per_point () const {
104                 return _samples_per_point;
105         }
106
107         void set_samples_per_point (int64_t spp) {
108                 _samples_per_point = spp;
109         }
110
111         int sample_rate () const {
112                 return _sample_rate;
113         }
114
115         void set_sample_rate (int sr) {
116                 _sample_rate = sr;
117         }
118
119         void write (boost::filesystem::path);
120
121         float gain_correction (boost::shared_ptr<const Playlist> playlist);
122
123 private:
124         std::vector<std::vector<AudioPoint> > _data;
125         std::vector<PeakTime> _sample_peak;
126         std::vector<float> _true_peak;
127         boost::optional<float> _integrated_loudness;
128         boost::optional<float> _loudness_range;
129         /** If this analysis was run on a single piece of
130          *  content we store its gain in dB when the analysis
131          *  happened.
132          */
133         boost::optional<double> _analysis_gain;
134         int64_t _samples_per_point;
135         int _sample_rate;
136
137         static int const _current_state_version;
138 };
139
140 #endif