98bed30dcab22e89a97a322684dc3c3666d04549
[dcpomatic.git] / src / lib / analyse_audio_job.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 "audio_buffers.h"
22 #include "analyse_audio_job.h"
23 #include "compose.hpp"
24 #include "film.h"
25 #include "player.h"
26 #include <boost/foreach.hpp>
27
28 #include "i18n.h"
29
30 using std::string;
31 using std::max;
32 using std::min;
33 using std::cout;
34 using boost::shared_ptr;
35 using boost::dynamic_pointer_cast;
36
37 int const AnalyseAudioJob::_num_points = 1024;
38
39 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
40         : Job (f)
41         , _playlist (p)
42         , _done (0)
43         , _samples_per_point (1)
44         , _overall_peak (0)
45         , _overall_peak_frame (0)
46 {
47
48 }
49
50 string
51 AnalyseAudioJob::name () const
52 {
53         return _("Analyse audio");
54 }
55
56 string
57 AnalyseAudioJob::json_name () const
58 {
59         return N_("analyse_audio");
60 }
61
62 void
63 AnalyseAudioJob::run ()
64 {
65         shared_ptr<Player> player (new Player (_film));
66         player->set_ignore_video ();
67         
68         int64_t const len = _film->length().frames (_film->audio_frame_rate());
69         _samples_per_point = max (int64_t (1), len / _num_points);
70
71         _current.resize (_film->audio_channels ());
72         _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
73
74         bool has_any_audio = false;
75         BOOST_FOREACH (shared_ptr<Content> c, _film->content ()) {
76                 if (dynamic_pointer_cast<AudioContent> (c)) {
77                         has_any_audio = true;
78                 }
79         }
80
81         if (has_any_audio) {
82                 _done = 0;
83                 DCPTime const block = DCPTime::from_seconds (1.0 / 8);
84                 for (DCPTime t; t < _film->length(); t += block) {
85                         analyse (player->get_audio (t, block, false));
86                         set_progress (t.seconds() / _film->length().seconds());
87                 }
88         }
89
90         _analysis->set_peak (_overall_peak, DCPTime::from_frames (_overall_peak_frame, _film->audio_frame_rate ()));
91         _analysis->write (_film->audio_analysis_path ());
92         
93         set_progress (1);
94         set_state (FINISHED_OK);
95 }
96
97 void
98 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b)
99 {
100         for (int i = 0; i < b->frames(); ++i) {
101                 for (int j = 0; j < b->channels(); ++j) {
102                         float s = b->data(j)[i];
103                         if (fabsf (s) < 10e-7) {
104                                 /* SafeStringStream can't serialise and recover inf or -inf, so prevent such
105                                    values by replacing with this (140dB down) */
106                                 s = 10e-7;
107                         }
108                         _current[j][AudioPoint::RMS] += pow (s, 2);
109                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], fabsf (s));
110
111                         float const as = fabs (s);
112
113                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
114
115                         if (as > _overall_peak) {
116                                 _overall_peak = as;
117                                 _overall_peak_frame = _done + i;
118                         }
119
120                         if ((_done % _samples_per_point) == 0) {
121                                 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
122                                 _analysis->add_point (j, _current[j]);
123
124                                 _current[j] = AudioPoint ();
125                         }
126                 }
127
128                 ++_done;
129         }
130 }
131