2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
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"
28 #include <boost/foreach.hpp>
36 using boost::shared_ptr;
37 using boost::dynamic_pointer_cast;
39 int const AnalyseAudioJob::_num_points = 1024;
41 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
43 , _playlist (playlist)
45 , _samples_per_point (1)
48 , _overall_peak_frame (0)
53 AnalyseAudioJob::~AnalyseAudioJob ()
59 AnalyseAudioJob::name () const
61 return _("Analyse audio");
65 AnalyseAudioJob::json_name () const
67 return N_("analyse_audio");
71 AnalyseAudioJob::run ()
73 shared_ptr<Player> player (new Player (_film, _playlist));
74 player->set_ignore_video ();
77 int64_t const len = _playlist->length().frames_round (_film->audio_frame_rate());
78 _samples_per_point = max (int64_t (1), len / _num_points);
81 _current = new AudioPoint[_film->audio_channels ()];
82 _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
84 bool has_any_audio = false;
85 BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
86 if (dynamic_pointer_cast<AudioContent> (c)) {
93 DCPTime const block = DCPTime::from_seconds (1.0 / 8);
94 for (DCPTime t; t < _film->length(); t += block) {
95 analyse (player->get_audio (t, block, false));
96 set_progress (t.seconds() / _film->length().seconds());
100 _analysis->set_peak (_overall_peak, DCPTime::from_frames (_overall_peak_frame, _film->audio_frame_rate ()));
102 if (_playlist->content().size() == 1) {
103 /* If there was only one piece of content in this analysis we may later need to know what its
104 gain was when we analysed it.
106 shared_ptr<const AudioContent> ac = dynamic_pointer_cast<const AudioContent> (_playlist->content().front ());
107 DCPOMATIC_ASSERT (ac);
108 _analysis->set_analysis_gain (ac->audio_gain ());
111 _analysis->write (_film->audio_analysis_path (_playlist));
114 set_state (FINISHED_OK);
118 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b)
120 int const frames = b->frames ();
121 int const channels = b->channels ();
123 for (int j = 0; j < channels; ++j) {
124 float* data = b->data(j);
125 for (int i = 0; i < frames; ++i) {
127 float as = fabsf (s);
129 /* SafeStringStream can't serialise and recover inf or -inf, so prevent such
130 values by replacing with this (140dB down) */
133 _current[j][AudioPoint::RMS] += pow (s, 2);
134 _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
136 if (as > _overall_peak) {
138 _overall_peak_frame = _done + i;
141 if (((_done + i) % _samples_per_point) == 0) {
142 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
143 _analysis->add_point (j, _current[j]);
144 _current[j] = AudioPoint ();