Some more tidying up.
[dcpomatic.git] / src / lib / analyse_audio_job.cc
index de2632b0a7cd2556aa46086d8860548f7a0c2e07..433b6c98fa800a36d812aa41588b83d849fa06fd 100644 (file)
 #include "analyse_audio_job.h"
 #include "compose.hpp"
 #include "film.h"
-#include "options.h"
-#include "decoder_factory.h"
-#include "audio_decoder.h"
+#include "player.h"
 
 #include "i18n.h"
 
 using std::string;
 using std::max;
+using std::min;
 using std::cout;
 using boost::shared_ptr;
 
@@ -36,7 +35,7 @@ int const AnalyseAudioJob::_num_points = 1024;
 
 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<Film> f)
        : Job (f)
-       , _done (0)
+       , _next (0)
        , _samples_per_point (1)
 {
 
@@ -51,29 +50,19 @@ AnalyseAudioJob::name () const
 void
 AnalyseAudioJob::run ()
 {
-       if (!_film->audio_stream () || !_film->length()) {
-               set_progress (1);
-               set_state (FINISHED_ERROR);
-               return;
-       }
-               
-       DecodeOptions options;
-       options.decode_video = false;
-
-       Decoders decoders = decoder_factory (_film, options);
-       assert (decoders.audio);
+       shared_ptr<Player> player = _film->player ();
+       player->disable_video ();
        
-       decoders.audio->set_audio_stream (_film->audio_stream ());
-       decoders.audio->Audio.connect (bind (&AnalyseAudioJob::audio, this, _1));
+       player->Audio.connect (bind (&AnalyseAudioJob::audio, this, _1, _2));
+
+       _samples_per_point = max (int64_t (1), _film->time_to_audio_frames (_film->length()) / _num_points);
 
-       int64_t total_audio_frames = video_frames_to_audio_frames (_film->length().get(), _film->audio_stream()->sample_rate(), _film->source_frame_rate());
-       _samples_per_point = min (1, total_audio_frames / _num_points);
+       _current.resize (MAX_AUDIO_CHANNELS);
+       _analysis.reset (new AudioAnalysis (MAX_AUDIO_CHANNELS));
 
-       _current.resize (_film->audio_stream()->channels ());
-       _analysis.reset (new AudioAnalysis (_film->audio_stream()->channels()));
-                        
-       while (!decoders.audio->pass()) {
-               set_progress (float (_done) / total_audio_frames);
+       _next = 0;
+       while (_next < _film->length()) {
+               set_progress (double (_next) / _film->length ());
        }
 
        _analysis->write (_film->audio_analysis_path ());
@@ -83,7 +72,7 @@ AnalyseAudioJob::run ()
 }
 
 void
-AnalyseAudioJob::audio (shared_ptr<AudioBuffers> b)
+AnalyseAudioJob::audio (shared_ptr<const AudioBuffers> b, Time t)
 {
        for (int i = 0; i < b->frames(); ++i) {
                for (int j = 0; j < b->channels(); ++j) {
@@ -103,8 +92,8 @@ AnalyseAudioJob::audio (shared_ptr<AudioBuffers> b)
                                _current[j] = AudioPoint ();
                        }
                }
-
-               ++_done;
        }
+
+       _next = (t + _film->audio_frames_to_time (b->frames()));
 }