WIP
[dcpomatic.git] / src / lib / analyse_audio_job.cc
index e77f83bdedfd8c8b82dd21f8c28d287f2b8b8ab8..47beb0d4398383f007de7a4c59ba5ca69fa1b839 100644 (file)
@@ -23,6 +23,7 @@
 #include "analyse_audio_job.h"
 #include "audio_content.h"
 #include "compose.hpp"
+#include "dcpomatic_log.h"
 #include "film.h"
 #include "player.h"
 #include "playlist.h"
@@ -74,6 +75,8 @@ AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const
        , _ebur128 (new AudioFilterGraph (film->audio_frame_rate(), film->audio_channels()))
 #endif
 {
+       LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::AnalyseAudioJob");
+
 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
        _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
        _ebur128->setup (_filters);
@@ -89,7 +92,7 @@ AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const
        }
 
        /* XXX: is this right?  Especially for more than 5.1? */
-       vector<double> channel_corrections(film->audio_channels(), 1);
+       vector<double> channel_corrections(static_cast<size_t>(film->audio_channels()), 1);
        add_if_required (channel_corrections,  4,   -3); // Ls
        add_if_required (channel_corrections,  5,   -3); // Rs
        add_if_required (channel_corrections,  6, -144); // HI
@@ -110,7 +113,7 @@ AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const
                channel_corrections,
                850, // suggested by leqm_nrt CLI source
                64,  // suggested by leqm_nrt CLI source
-               boost::thread::hardware_concurrency()
+               static_cast<int>(boost::thread::hardware_concurrency())
                ));
 }
 
@@ -140,6 +143,8 @@ AnalyseAudioJob::json_name () const
 void
 AnalyseAudioJob::run ()
 {
+       LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::run");
+
        shared_ptr<Player> player (new Player(_film, _playlist));
        player->set_ignore_video ();
        player->set_ignore_text ();
@@ -164,11 +169,15 @@ AnalyseAudioJob::run ()
        }
 
        if (has_any_audio) {
+               LOG_DEBUG_AUDIO_ANALYSIS("Seeking to %1", to_string(_start));
                player->seek (_start, true);
                _done = 0;
+               LOG_DEBUG_AUDIO_ANALYSIS("Starting loop for playlist of length %1", to_string(length));
                while (!player->pass ()) {}
        }
 
+       LOG_DEBUG_AUDIO_ANALYSIS_NC("Loop complete");
+
        vector<AudioAnalysis::PeakTime> sample_peak;
        for (int i = 0; i < _film->audio_channels(); ++i) {
                sample_peak.push_back (
@@ -205,6 +214,7 @@ AnalyseAudioJob::run ()
        _analysis->set_leqm (_leqm->leq_m());
        _analysis->write (_path);
 
+       LOG_DEBUG_AUDIO_ANALYSIS_NC("Job finished");
        set_progress (1);
        set_state (FINISHED_OK);
 }
@@ -212,6 +222,7 @@ AnalyseAudioJob::run ()
 void
 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
 {
+       LOG_DEBUG_AUDIO_ANALYSIS("Received %1 frames at %2", b->frames(), to_string(time));
        DCPOMATIC_ASSERT (time >= _start);
 
 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
@@ -220,16 +231,17 @@ AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
        }
 #endif
 
-       int const frames = b->frames ();
-       int const channels = b->channels ();
-       vector<double> interleaved(frames * channels);
+       DCPOMATIC_ASSERT (b->frames());
+       DCPOMATIC_ASSERT (b->channels());
+
+       vector<double> interleaved(static_cast<size_t>(b->frames() * b->channels()));
 
-       for (int j = 0; j < channels; ++j) {
+       for (int j = 0; j < b->channels(); ++j) {
                float* data = b->data(j);
-               for (int i = 0; i < frames; ++i) {
-                       float s = data[i];
+               for (Frame i = 0; i < b->frames(); ++i) {
+                       float s = data[static_cast<size_t>(i)];
 
-                       interleaved[i * channels + j] = s;
+                       interleaved[static_cast<size_t>(i * b->channels() + j)] = s;
 
                        float as = fabsf (s);
                        if (as < 10e-7) {
@@ -255,8 +267,9 @@ AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
 
        _leqm->add(interleaved);
 
-       _done += frames;
+       _done += b->frames();
 
        DCPTime const length = _playlist->length (_film);
        set_progress ((time.seconds() - _start.seconds()) / (length.seconds() - _start.seconds()));
+       LOG_DEBUG_AUDIO_ANALYSIS_NC("Frames processed");
 }