Merge master; fix crash on new film.
[dcpomatic.git] / src / lib / analyse_audio_job.cc
1 /*
2     Copyright (C) 2012 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 "analyse_audio_job.h"
22 #include "compose.hpp"
23 #include "film.h"
24 #include "player.h"
25
26 #include "i18n.h"
27
28 using std::string;
29 using std::max;
30 using std::min;
31 using std::cout;
32 using boost::shared_ptr;
33
34 int const AnalyseAudioJob::_num_points = 1024;
35
36 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<Film> f)
37         : Job (f)
38         , _done (0)
39         , _samples_per_point (1)
40 {
41
42 }
43
44 string
45 AnalyseAudioJob::name () const
46 {
47         return String::compose (_("Analyse audio of %1"), _film->name());
48 }
49
50 void
51 AnalyseAudioJob::run ()
52 {
53         shared_ptr<Player> player = _film->player ();
54         player->disable_video ();
55         
56         player->Audio.connect (bind (&AnalyseAudioJob::audio, this, _1));
57
58         _samples_per_point = max (int64_t (1), _film->audio_length() / _num_points);
59
60         _current.resize (MAX_AUDIO_CHANNELS);
61         _analysis.reset (new AudioAnalysis (MAX_AUDIO_CHANNELS));
62                          
63         while (!player->pass()) {
64                 set_progress (float (_done) / _film->audio_length ());
65         }
66
67         _analysis->write (_film->audio_analysis_path ());
68         
69         set_progress (1);
70         set_state (FINISHED_OK);
71 }
72
73 void
74 AnalyseAudioJob::audio (shared_ptr<const AudioBuffers> b)
75 {
76         for (int i = 0; i < b->frames(); ++i) {
77                 for (int j = 0; j < b->channels(); ++j) {
78                         float s = b->data(j)[i];
79                         if (fabsf (s) < 10e-7) {
80                                 /* stringstream can't serialise and recover inf or -inf, so prevent such
81                                    values by replacing with this (140dB down) */
82                                 s = 10e-7;
83                         }
84                         _current[j][AudioPoint::RMS] += pow (s, 2);
85                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], fabsf (s));
86
87                         if ((_done % _samples_per_point) == 0) {
88                                 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
89                                 _analysis->add_point (j, _current[j]);
90                                 
91                                 _current[j] = AudioPoint ();
92                         }
93                 }
94
95                 ++_done;
96         }
97 }
98