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