Merge master.
[dcpomatic.git] / src / lib / analyse_audio_job.cc
index 13cab1a469902f9c31a1d3700258ebe420fb96c4..60b10e7b6e05ba69dc418894fa10b4ca8ef7781e 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 #include "audio_analysis.h"
+#include "audio_buffers.h"
 #include "analyse_audio_job.h"
 #include "compose.hpp"
 #include "film.h"
@@ -33,8 +34,9 @@ using boost::shared_ptr;
 
 int const AnalyseAudioJob::_num_points = 1024;
 
-AnalyseAudioJob::AnalyseAudioJob (shared_ptr<Film> f)
+AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> f, shared_ptr<AudioContent> c)
        : Job (f)
+       , _content (c)
        , _done (0)
        , _samples_per_point (1)
 {
@@ -44,41 +46,48 @@ AnalyseAudioJob::AnalyseAudioJob (shared_ptr<Film> f)
 string
 AnalyseAudioJob::name () const
 {
-       return String::compose (_("Analyse audio of %1"), _film->name());
+       return _("Analyse audio");
 }
 
 void
 AnalyseAudioJob::run ()
 {
-       shared_ptr<Player> player = _film->player ();
-       player->disable_video ();
-       
-       player->Audio.connect (bind (&AnalyseAudioJob::audio, this, _1, _2));
+       shared_ptr<AudioContent> content = _content.lock ();
+       if (!content) {
+               return;
+       }
 
-       _samples_per_point = max (int64_t (1), _film->time_to_audio_frames (_film->length()) / _num_points);
+       shared_ptr<Playlist> playlist (new Playlist);
+       playlist->add (content);
+       shared_ptr<Player> player (new Player (_film, playlist));
+       
+       int64_t const len = _film->length().frames (_film->audio_frame_rate());
+       _samples_per_point = max (int64_t (1), len / _num_points);
 
-       _current.resize (MAX_AUDIO_CHANNELS);
-       _analysis.reset (new AudioAnalysis (MAX_AUDIO_CHANNELS));
+       _current.resize (_film->audio_channels ());
+       _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
 
        _done = 0;
-       while (player->pass ()) {
-               set_progress (double (_done) / _film->length ());
+       DCPTime const block = DCPTime::from_seconds (1.0 / 8);
+       for (DCPTime t; t < _film->length(); t += block) {
+               analyse (player->get_audio (t, block, false));
+               set_progress (t.seconds() / _film->length().seconds());
        }
 
-       _analysis->write (_film->audio_analysis_path ());
+       _analysis->write (content->audio_analysis_path ());
        
        set_progress (1);
        set_state (FINISHED_OK);
 }
 
 void
-AnalyseAudioJob::audio (shared_ptr<const AudioBuffers> b, Time t)
+AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b)
 {
        for (int i = 0; i < b->frames(); ++i) {
                for (int j = 0; j < b->channels(); ++j) {
                        float s = b->data(j)[i];
                        if (fabsf (s) < 10e-7) {
-                               /* stringstream can't serialise and recover inf or -inf, so prevent such
+                               /* SafeStringStream can't serialise and recover inf or -inf, so prevent such
                                   values by replacing with this (140dB down) */
                                s = 10e-7;
                        }
@@ -88,12 +97,12 @@ AnalyseAudioJob::audio (shared_ptr<const AudioBuffers> b, Time t)
                        if ((_done % _samples_per_point) == 0) {
                                _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
                                _analysis->add_point (j, _current[j]);
-                               
+
                                _current[j] = AudioPoint ();
                        }
                }
-       }
 
-       _done = t;
+               ++_done;
+       }
 }