Fix analysis of audio when the subject is later in the playlist than
[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 "audio_content.h"
24 #include "compose.hpp"
25 #include "film.h"
26 #include "player.h"
27 #include "playlist.h"
28 #include <boost/foreach.hpp>
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::max;
34 using std::min;
35 using std::cout;
36 using boost::shared_ptr;
37 using boost::dynamic_pointer_cast;
38
39 int const AnalyseAudioJob::_num_points = 1024;
40
41 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
42         : Job (film)
43         , _playlist (playlist)
44         , _done (0)
45         , _samples_per_point (1)
46         , _current (0)
47         , _overall_peak (0)
48         , _overall_peak_frame (0)
49 {
50
51 }
52
53 AnalyseAudioJob::~AnalyseAudioJob ()
54 {
55         delete[] _current;
56 }
57
58 string
59 AnalyseAudioJob::name () const
60 {
61         return _("Analyse audio");
62 }
63
64 string
65 AnalyseAudioJob::json_name () const
66 {
67         return N_("analyse_audio");
68 }
69
70 void
71 AnalyseAudioJob::run ()
72 {
73         shared_ptr<Player> player (new Player (_film, _playlist));
74         player->set_ignore_video ();
75         player->set_fast ();
76
77         DCPTime const start = _playlist->start().get_value_or (DCPTime ());
78         DCPTime const length = _playlist->length ();
79
80         Frame const len = DCPTime (length - start).frames_round (_film->audio_frame_rate());
81         _samples_per_point = max (int64_t (1), len / _num_points);
82
83         delete[] _current;
84         _current = new AudioPoint[_film->audio_channels ()];
85         _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
86
87         bool has_any_audio = false;
88         BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
89                 if (dynamic_pointer_cast<AudioContent> (c)) {
90                         has_any_audio = true;
91                 }
92         }
93
94         if (has_any_audio) {
95                 _done = 0;
96                 DCPTime const block = DCPTime::from_seconds (1.0 / 8);
97                 for (DCPTime t = start; t < length; t += block) {
98                         analyse (player->get_audio (t, block, false));
99                         set_progress ((t.seconds() - start.seconds()) / (length.seconds() - start.seconds()));
100                 }
101         }
102
103         _analysis->set_peak (_overall_peak, DCPTime::from_frames (_overall_peak_frame, _film->audio_frame_rate ()));
104
105         if (_playlist->content().size() == 1) {
106                 /* If there was only one piece of content in this analysis we may later need to know what its
107                    gain was when we analysed it.
108                 */
109                 shared_ptr<const AudioContent> ac = dynamic_pointer_cast<const AudioContent> (_playlist->content().front ());
110                 DCPOMATIC_ASSERT (ac);
111                 _analysis->set_analysis_gain (ac->audio_gain ());
112         }
113
114         _analysis->write (_film->audio_analysis_path (_playlist));
115
116         set_progress (1);
117         set_state (FINISHED_OK);
118 }
119
120 void
121 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b)
122 {
123         int const frames = b->frames ();
124         int const channels = b->channels ();
125
126         for (int j = 0; j < channels; ++j) {
127                 float* data = b->data(j);
128                 for (int i = 0; i < frames; ++i) {
129                         float s = data[i];
130                         float as = fabsf (s);
131                         if (as < 10e-7) {
132                                 /* SafeStringStream can't serialise and recover inf or -inf, so prevent such
133                                    values by replacing with this (140dB down) */
134                                 s = as = 10e-7;
135                         }
136                         _current[j][AudioPoint::RMS] += pow (s, 2);
137                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
138
139                         if (as > _overall_peak) {
140                                 _overall_peak = as;
141                                 _overall_peak_frame = _done + i;
142                         }
143
144                         if (((_done + i) % _samples_per_point) == 0) {
145                                 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
146                                 _analysis->add_point (j, _current[j]);
147                                 _current[j] = AudioPoint ();
148                         }
149                 }
150         }
151
152         _done += frames;
153 }