From 49deab5be257f3a11f5b053224f4a3218fad8da3 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Thu, 11 Jul 2013 12:57:53 +0100 Subject: [PATCH] Untested flushing of resamplers. --- src/lib/audio_decoder.cc | 30 ------------------------------ src/lib/player.cc | 12 +++++++++++- src/lib/player.h | 1 + src/lib/resampler.cc | 28 ++++++++++++++++++++++++++++ src/lib/resampler.h | 1 + 5 files changed, 41 insertions(+), 31 deletions(-) diff --git a/src/lib/audio_decoder.cc b/src/lib/audio_decoder.cc index dc49a1846..ade11cc32 100644 --- a/src/lib/audio_decoder.cc +++ b/src/lib/audio_decoder.cc @@ -37,36 +37,6 @@ AudioDecoder::AudioDecoder (shared_ptr f) { } -#if 0 -void -AudioDecoder::process_end () -{ - if (_swr_context) { - - shared_ptr film = _film.lock (); - assert (film); - - shared_ptr out (new AudioBuffers (film->audio_mapping().dcp_channels(), 256)); - - while (1) { - int const frames = swr_convert (_swr_context, (uint8_t **) out->data(), 256, 0, 0); - - if (frames < 0) { - throw EncodeError (_("could not run sample-rate converter")); - } - - if (frames == 0) { - break; - } - - out->set_frames (frames); - _writer->write (out); - } - - } -} -#endif - void AudioDecoder::audio (shared_ptr data, AudioContent::Frame frame) { diff --git a/src/lib/player.cc b/src/lib/player.cc index 467f92374..18c42296f 100644 --- a/src/lib/player.cc +++ b/src/lib/player.cc @@ -261,7 +261,11 @@ Player::process_audio (weak_ptr weak_piece, shared_ptrcontent_audio_frame_rate() != content->output_audio_frame_rate()) { - audio = resampler(content)->run (audio); + shared_ptr r = resampler (content); + audio = r->run (audio); + _last_resampler = r; + } else { + _last_resampler.reset (); } /* Remap channels */ @@ -313,6 +317,12 @@ Player::flush () _audio_buffers.set_frames (0); } + if (_last_resampler) { + shared_ptr resamp = _last_resampler->flush (); + Audio (resamp, _audio_position); + _audio_position += _film->audio_frames_to_time (resamp->frames ()); + } + while (_video_position < _audio_position) { emit_black (); } diff --git a/src/lib/player.h b/src/lib/player.h index 5a4ee97be..b3eadd7c0 100644 --- a/src/lib/player.h +++ b/src/lib/player.h @@ -110,6 +110,7 @@ private: libdcp::Size _video_container_size; boost::shared_ptr _black_frame; std::map, boost::shared_ptr > _resamplers; + boost::shared_ptr _last_resampler; struct { boost::weak_ptr piece; diff --git a/src/lib/resampler.cc b/src/lib/resampler.cc index 1235b9038..ebb507cb1 100644 --- a/src/lib/resampler.cc +++ b/src/lib/resampler.cc @@ -59,3 +59,31 @@ Resampler::run (shared_ptr in) resampled->set_frames (resampled_frames); return resampled; } + +shared_ptr +Resampler::flush () +{ + shared_ptr out (new AudioBuffers (_channels, 0)); + int out_offset = 0; + int64_t const pass_size = 256; + shared_ptr pass (new AudioBuffers (_channels, 256)); + + while (1) { + int const frames = swr_convert (_swr_context, (uint8_t **) pass->data(), pass_size, 0, 0); + + if (frames < 0) { + throw EncodeError (_("could not run sample-rate converter")); + } + + if (frames == 0) { + break; + } + + out->ensure_size (out_offset + frames); + out->copy_from (pass.get(), frames, 0, out_offset); + out_offset += frames; + out->set_frames (out_offset); + } + + return out; +} diff --git a/src/lib/resampler.h b/src/lib/resampler.h index cda718934..21979846e 100644 --- a/src/lib/resampler.h +++ b/src/lib/resampler.h @@ -12,6 +12,7 @@ public: ~Resampler (); boost::shared_ptr run (boost::shared_ptr); + boost::shared_ptr flush (); private: SwrContext* _swr_context; -- 2.30.2