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