Untested flushing of resamplers.
authorCarl Hetherington <cth@carlh.net>
Thu, 11 Jul 2013 11:57:53 +0000 (12:57 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 11 Jul 2013 11:57:53 +0000 (12:57 +0100)
src/lib/audio_decoder.cc
src/lib/player.cc
src/lib/player.h
src/lib/resampler.cc
src/lib/resampler.h

index dc49a1846e74ada429e20603d213666d858ecd52..ade11cc3290ab0dcbb3d50628cad8689fe1aa034 100644 (file)
@@ -37,36 +37,6 @@ AudioDecoder::AudioDecoder (shared_ptr<const Film> f)
 {
 }
 
-#if 0
-void
-AudioDecoder::process_end ()
-{
-       if (_swr_context) {
-
-               shared_ptr<const Film> film = _film.lock ();
-               assert (film);
-               
-               shared_ptr<AudioBuffers> 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<const AudioBuffers> data, AudioContent::Frame frame)
 {
index 467f92374e66fcdd332090b49550c2aafc87b524..18c42296f5f2b92964e15ce020b82813b979da60 100644 (file)
@@ -261,7 +261,11 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
        assert (content);
 
        if (content->content_audio_frame_rate() != content->output_audio_frame_rate()) {
-               audio = resampler(content)->run (audio);
+               shared_ptr<Resampler> 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<const AudioBuffers> resamp = _last_resampler->flush ();
+               Audio (resamp, _audio_position);
+               _audio_position += _film->audio_frames_to_time (resamp->frames ());
+       }
+       
        while (_video_position < _audio_position) {
                emit_black ();
        }
index 5a4ee97becdc561b72cad6e59a36f97e77f02a80..b3eadd7c07e25521506bf6fcc29bc251262e811f 100644 (file)
@@ -110,6 +110,7 @@ private:
        libdcp::Size _video_container_size;
        boost::shared_ptr<Image> _black_frame;
        std::map<boost::shared_ptr<AudioContent>, boost::shared_ptr<Resampler> > _resamplers;
+       boost::shared_ptr<Resampler> _last_resampler;
 
        struct {
                boost::weak_ptr<Piece> piece;
index 1235b9038408b668dd1778f47ba62ffb83b64fee..ebb507cb124fd8ca68e93b813480aec581c209d2 100644 (file)
@@ -59,3 +59,31 @@ Resampler::run (shared_ptr<const AudioBuffers> in)
        resampled->set_frames (resampled_frames);
        return resampled;
 }      
+
+shared_ptr<const AudioBuffers>
+Resampler::flush ()
+{
+       shared_ptr<AudioBuffers> out (new AudioBuffers (_channels, 0));
+       int out_offset = 0;
+       int64_t const pass_size = 256;
+       shared_ptr<AudioBuffers> 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;
+}
index cda7189348f2987858b346bf5aad79edc539883c..21979846eaffa931afc548e74d963adfc219f26b 100644 (file)
@@ -12,6 +12,7 @@ public:
        ~Resampler ();
 
        boost::shared_ptr<const AudioBuffers> run (boost::shared_ptr<const AudioBuffers>);
+       boost::shared_ptr<const AudioBuffers> flush ();
 
 private:       
        SwrContext* _swr_context;