2 Copyright (C) 2012 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 "compose.hpp"
33 using boost::shared_ptr;
35 int const AnalyseAudioJob::_num_points = 1024;
37 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> f, shared_ptr<AudioContent> c)
41 , _samples_per_point (1)
47 AnalyseAudioJob::name () const
49 return _("Analyse audio");
53 AnalyseAudioJob::json_name () const
55 return N_("analyse_audio");
59 AnalyseAudioJob::run ()
61 shared_ptr<AudioContent> content = _content.lock ();
66 shared_ptr<Playlist> playlist (new Playlist);
67 playlist->add (content);
68 shared_ptr<Player> player (new Player (_film, playlist));
70 int64_t const len = _film->length().frames (_film->audio_frame_rate());
71 _samples_per_point = max (int64_t (1), len / _num_points);
73 _current.resize (_film->audio_channels ());
74 _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
77 DCPTime const block = DCPTime::from_seconds (1.0 / 8);
78 for (DCPTime t; t < _film->length(); t += block) {
79 analyse (player->get_audio (t, block, false));
80 set_progress (t.seconds() / _film->length().seconds());
83 _analysis->write (content->audio_analysis_path ());
86 set_state (FINISHED_OK);
90 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b)
92 for (int i = 0; i < b->frames(); ++i) {
93 for (int j = 0; j < b->channels(); ++j) {
94 float s = b->data(j)[i];
95 if (fabsf (s) < 10e-7) {
96 /* stringstream can't serialise and recover inf or -inf, so prevent such
97 values by replacing with this (140dB down) */
100 _current[j][AudioPoint::RMS] += pow (s, 2);
101 _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], fabsf (s));
103 if ((_done % _samples_per_point) == 0) {
104 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
105 _analysis->add_point (j, _current[j]);
107 _current[j] = AudioPoint ();