de2632b0a7cd2556aa46086d8860548f7a0c2e07
[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 "options.h"
25 #include "decoder_factory.h"
26 #include "audio_decoder.h"
27
28 #include "i18n.h"
29
30 using std::string;
31 using std::max;
32 using std::cout;
33 using boost::shared_ptr;
34
35 int const AnalyseAudioJob::_num_points = 1024;
36
37 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<Film> f)
38         : Job (f)
39         , _done (0)
40         , _samples_per_point (1)
41 {
42
43 }
44
45 string
46 AnalyseAudioJob::name () const
47 {
48         return String::compose (_("Analyse audio of %1"), _film->name());
49 }
50
51 void
52 AnalyseAudioJob::run ()
53 {
54         if (!_film->audio_stream () || !_film->length()) {
55                 set_progress (1);
56                 set_state (FINISHED_ERROR);
57                 return;
58         }
59                 
60         DecodeOptions options;
61         options.decode_video = false;
62
63         Decoders decoders = decoder_factory (_film, options);
64         assert (decoders.audio);
65         
66         decoders.audio->set_audio_stream (_film->audio_stream ());
67         decoders.audio->Audio.connect (bind (&AnalyseAudioJob::audio, this, _1));
68
69         int64_t total_audio_frames = video_frames_to_audio_frames (_film->length().get(), _film->audio_stream()->sample_rate(), _film->source_frame_rate());
70         _samples_per_point = min (1, total_audio_frames / _num_points);
71
72         _current.resize (_film->audio_stream()->channels ());
73         _analysis.reset (new AudioAnalysis (_film->audio_stream()->channels()));
74                          
75         while (!decoders.audio->pass()) {
76                 set_progress (float (_done) / total_audio_frames);
77         }
78
79         _analysis->write (_film->audio_analysis_path ());
80         
81         set_progress (1);
82         set_state (FINISHED_OK);
83 }
84
85 void
86 AnalyseAudioJob::audio (shared_ptr<AudioBuffers> b)
87 {
88         for (int i = 0; i < b->frames(); ++i) {
89                 for (int j = 0; j < b->channels(); ++j) {
90                         float s = b->data(j)[i];
91                         if (fabsf (s) < 10e-7) {
92                                 /* stringstream can't serialise and recover inf or -inf, so prevent such
93                                    values by replacing with this (140dB down) */
94                                 s = 10e-7;
95                         }
96                         _current[j][AudioPoint::RMS] += pow (s, 2);
97                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], fabsf (s));
98
99                         if ((_done % _samples_per_point) == 0) {
100                                 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
101                                 _analysis->add_point (j, _current[j]);
102                                 
103                                 _current[j] = AudioPoint ();
104                         }
105                 }
106
107                 ++_done;
108         }
109 }
110