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 "analyse_audio_job.h"
22 #include "compose.hpp"
33 using boost::shared_ptr;
35 int const AnalyseAudioJob::_num_points = 1024;
37 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<Film> f)
40 , _samples_per_point (1)
46 AnalyseAudioJob::name () const
48 return String::compose (_("Analyse audio of %1"), _film->name());
52 AnalyseAudioJob::run ()
54 shared_ptr<Playlist> playlist = _film->playlist ();
55 playlist->disable_video ();
57 playlist->Audio.connect (bind (&AnalyseAudioJob::audio, this, _1));
59 _samples_per_point = max (int64_t (1), playlist->audio_length() / _num_points);
61 _current.resize (playlist->audio_channels ());
62 _analysis.reset (new AudioAnalysis (playlist->audio_channels()));
64 while (!playlist->pass()) {
65 set_progress (float (_done) / playlist->audio_length ());
68 _analysis->write (_film->audio_analysis_path ());
71 set_state (FINISHED_OK);
75 AnalyseAudioJob::audio (shared_ptr<AudioBuffers> b)
77 for (int i = 0; i < b->frames(); ++i) {
78 for (int j = 0; j < b->channels(); ++j) {
79 float s = b->data(j)[i];
80 if (fabsf (s) < 10e-7) {
81 /* stringstream can't serialise and recover inf or -inf, so prevent such
82 values by replacing with this (140dB down) */
85 _current[j][AudioPoint::RMS] += pow (s, 2);
86 _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], fabsf (s));
88 if ((_done % _samples_per_point) == 0) {
89 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
90 _analysis->add_point (j, _current[j]);
92 _current[j] = AudioPoint ();