Fix odd behaviour when analysing single bits of audio which start at non-zero positio...
authorCarl Hetherington <cth@carlh.net>
Sat, 10 Feb 2018 02:05:14 +0000 (02:05 +0000)
committerCarl Hetherington <cth@carlh.net>
Sat, 10 Feb 2018 02:05:14 +0000 (02:05 +0000)
src/lib/analyse_audio_job.cc
src/lib/analyse_audio_job.h
src/lib/film.cc
src/lib/job_manager.cc
src/lib/job_manager.h
src/wx/audio_dialog.cc
test/audio_analysis_test.cc
test/audio_processor_test.cc

index fe56b3b..b96c704 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -50,9 +50,11 @@ using boost::dynamic_pointer_cast;
 
 int const AnalyseAudioJob::_num_points = 1024;
 
-AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
+/** @param from_zero true to analyse audio from time 0 in the playlist, otherwise begin at Playlist::start */
+AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, bool from_zero)
        : Job (film)
        , _playlist (playlist)
+       , _from_zero (from_zero)
        , _done (0)
        , _samples_per_point (1)
        , _current (0)
@@ -71,6 +73,10 @@ AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const
                _sample_peak[i] = 0;
                _sample_peak_frame[i] = 0;
        }
+
+       if (!_from_zero) {
+               _start = _playlist->start().get_value_or(DCPTime());
+       }
 }
 
 AnalyseAudioJob::~AnalyseAudioJob ()
@@ -105,10 +111,9 @@ AnalyseAudioJob::run ()
        player->set_play_referenced ();
        player->Audio.connect (bind (&AnalyseAudioJob::analyse, this, _1, _2));
 
-       DCPTime const start = _playlist->start().get_value_or (DCPTime ());
        DCPTime const length = _playlist->length ();
 
-       Frame const len = DCPTime (length - start).frames_round (_film->audio_frame_rate());
+       Frame const len = DCPTime (length - _start).frames_round (_film->audio_frame_rate());
        _samples_per_point = max (int64_t (1), len / _num_points);
 
        delete[] _current;
@@ -123,6 +128,7 @@ AnalyseAudioJob::run ()
        }
 
        if (has_any_audio) {
+               player->seek (_start, true);
                _done = 0;
                while (!player->pass ()) {}
        }
@@ -166,6 +172,8 @@ AnalyseAudioJob::run ()
 void
 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
 {
+       DCPOMATIC_ASSERT (time >= _start);
+
 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
        if (Config::instance()->analyse_ebur128 ()) {
                _ebur128->process (b);
@@ -203,7 +211,6 @@ AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
 
        _done += frames;
 
-       DCPTime const start = _playlist->start().get_value_or (DCPTime ());
        DCPTime const length = _playlist->length ();
-       set_progress ((time.seconds() - start.seconds()) / (length.seconds() - start.seconds()));
+       set_progress ((time.seconds() - _start.seconds()) / (length.seconds() - _start.seconds()));
 }
index 7e5ea47..d8dcda2 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -44,7 +44,7 @@ class Filter;
 class AnalyseAudioJob : public Job
 {
 public:
-       AnalyseAudioJob (boost::shared_ptr<const Film>, boost::shared_ptr<const Playlist>);
+       AnalyseAudioJob (boost::shared_ptr<const Film>, boost::shared_ptr<const Playlist>, bool from_zero);
        ~AnalyseAudioJob ();
 
        std::string name () const;
@@ -59,6 +59,8 @@ private:
        void analyse (boost::shared_ptr<const AudioBuffers>, DCPTime time);
 
        boost::shared_ptr<const Playlist> _playlist;
+       DCPTime _start;
+       bool _from_zero;
 
        int64_t _done;
        int64_t _samples_per_point;
index db1cb39..d5af889 100644 (file)
@@ -1067,7 +1067,7 @@ Film::maybe_add_content (weak_ptr<Job> j, weak_ptr<Content> c, bool disable_audi
                playlist->add (content);
                boost::signals2::connection c;
                JobManager::instance()->analyse_audio (
-                       shared_from_this (), playlist, c, bind (&Film::audio_analysis_finished, this)
+                       shared_from_this(), playlist, false, c, bind (&Film::audio_analysis_finished, this)
                        );
                _audio_analysis_connections.push_back (c);
        }
index 3992e68..d76f860 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -182,6 +182,7 @@ void
 JobManager::analyse_audio (
        shared_ptr<const Film> film,
        shared_ptr<const Playlist> playlist,
+       bool from_zero,
        boost::signals2::connection& connection,
        function<void()> ready
        )
@@ -203,7 +204,7 @@ JobManager::analyse_audio (
        {
                boost::mutex::scoped_lock lm (_mutex);
 
-               job.reset (new AnalyseAudioJob (film, playlist));
+               job.reset (new AnalyseAudioJob (film, playlist, from_zero));
                connection = job->Finished.connect (ready);
                _jobs.push_back (job);
        }
index c623b6e..9cba0fd 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -50,6 +50,7 @@ public:
        void analyse_audio (
                boost::shared_ptr<const Film> film,
                boost::shared_ptr<const Playlist> playlist,
+               bool from_zero,
                boost::signals2::connection& connection,
                boost::function<void()> ready
                );
index 73ddd4c..a08d29a 100644 (file)
@@ -169,11 +169,15 @@ AudioDialog::try_to_load_analysis ()
        shared_ptr<const Film> film = _film.lock ();
        DCPOMATIC_ASSERT (film);
 
+       shared_ptr<Content> check = _content.lock();
+
        boost::filesystem::path const path = film->audio_analysis_path (_playlist);
        if (!boost::filesystem::exists (path)) {
                _plot->set_analysis (shared_ptr<AudioAnalysis> ());
                _analysis.reset ();
-               JobManager::instance()->analyse_audio (film, _playlist, _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this));
+               JobManager::instance()->analyse_audio (
+                       film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
+                       );
                return;
        }
 
@@ -181,11 +185,15 @@ AudioDialog::try_to_load_analysis ()
                _analysis.reset (new AudioAnalysis (path));
        } catch (OldFormatError& e) {
                /* An old analysis file: recreate it */
-               JobManager::instance()->analyse_audio (film, _playlist, _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this));
+               JobManager::instance()->analyse_audio (
+                       film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
+                       );
                return;
        } catch (xmlpp::exception& e) {
                /* Probably a (very) old-style analysis file: recreate it */
-               JobManager::instance()->analyse_audio (film, _playlist, _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this));
+               JobManager::instance()->analyse_audio (
+                       film, _playlist, !static_cast<bool>(check), _analysis_finished_connection, bind (&AudioDialog::analysis_finished, this)
+                       );
                return;
         }
 
index 64081b8..b461680 100644 (file)
@@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE (audio_analysis_test)
        film->examine_and_add_content (c);
        wait_for_jobs ();
 
-       shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
+       shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist(), false));
        job->Finished.connect (boost::bind (&finished));
        JobManager::instance()->add (job);
        wait_for_jobs ();
@@ -128,7 +128,7 @@ BOOST_AUTO_TEST_CASE (audio_analysis_negative_delay_test)
 
        c->audio->set_delay (-250);
 
-       shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
+       shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist(), false));
        job->Finished.connect (boost::bind (&finished));
        JobManager::instance()->add (job);
        wait_for_jobs ();
@@ -143,7 +143,7 @@ BOOST_AUTO_TEST_CASE (audio_analysis_test2)
        film->examine_and_add_content (c);
        wait_for_jobs ();
 
-       shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
+       shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist(), false));
        job->Finished.connect (boost::bind (&finished));
        JobManager::instance()->add (job);
        wait_for_jobs ();
@@ -174,7 +174,7 @@ BOOST_AUTO_TEST_CASE (audio_analysis_test3)
 
        film->set_audio_channels (12);
        boost::signals2::connection connection;
-       JobManager::instance()->analyse_audio (film, film->playlist(), connection, boost::bind (&analysis_finished));
+       JobManager::instance()->analyse_audio (film, film->playlist(), false, connection, boost::bind (&analysis_finished));
        wait_for_jobs ();
        BOOST_CHECK (done);
 }
@@ -193,6 +193,6 @@ BOOST_AUTO_TEST_CASE (analyse_audio_test4)
        shared_ptr<Playlist> playlist (new Playlist);
        playlist->add (content);
        boost::signals2::connection c;
-       JobManager::instance()->analyse_audio (film, playlist, c, boost::bind (&finished));
+       JobManager::instance()->analyse_audio (film, playlist, false, c, boost::bind (&finished));
        BOOST_CHECK (!wait_for_jobs ());
 }
index e3917af..de212f6 100644 (file)
@@ -48,7 +48,7 @@ BOOST_AUTO_TEST_CASE (audio_processor_test)
        film->set_audio_processor (AudioProcessor::from_id ("mid-side-decoder"));
 
        /* Analyse the audio and check it doesn't crash */
-       shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist ()));
+       shared_ptr<AnalyseAudioJob> job (new AnalyseAudioJob (film, film->playlist(), false));
        JobManager::instance()->add (job);
        wait_for_jobs ();