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