Adjust AudioMerger to non-signalling API.
authorCarl Hetherington <cth@carlh.net>
Sat, 27 Jul 2013 20:10:45 +0000 (21:10 +0100)
committerCarl Hetherington <cth@carlh.net>
Sat, 27 Jul 2013 20:10:45 +0000 (21:10 +0100)
src/lib/audio_merger.h
src/lib/player.cc
src/lib/player.h
src/lib/types.h
test/audio_merger_test.cc

index 126325a9d61cf127dd10bdaa94ac5b5998e2797e..afb21871bd0c72f031acae149ff69eaa7aa11c23 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 #include "audio_buffers.h"
+#include "util.h"
 
 template <class T, class F>
 class AudioMerger
@@ -25,47 +26,50 @@ class AudioMerger
 public:
        AudioMerger (int channels, boost::function<F (T)> t_to_f, boost::function<T (F)> f_to_t)
                : _buffers (new AudioBuffers (channels, 0))
-               , _next_emission (0)
+               , _next_out (0)
                , _t_to_f (t_to_f)
                , _f_to_t (f_to_t)
        {}
 
-       void push (boost::shared_ptr<const AudioBuffers> audio, T time)
+       TimedAudioBuffers<T>
+       push (boost::shared_ptr<const AudioBuffers> audio, T time)
        {
-               if (time > _next_emission) {
-                       /* We can emit some audio from our buffer; this is how many frames
-                          we are going to emit.
-                       */
-                       F const to_emit = _t_to_f (time - _next_emission);
-                       boost::shared_ptr<AudioBuffers> emit (new AudioBuffers (_buffers->channels(), to_emit));
+               assert (time >= _next_out);
 
+               TimedAudioBuffers<T> out;
+               
+               if (time > _next_out) {
+                       /* We can return some audio from our buffer; this is how many frames
+                          we are going to return.
+                       */
+                       F const to_return = _t_to_f (time - _next_out);
+                       out.audio.reset (new AudioBuffers (_buffers->channels(), to_return));
                        /* And this is how many we will get from our buffer */
-                       F const to_emit_from_buffers = min (to_emit, _buffers->frames ());
-
-                       /* Copy the data that we have to the back end of `emit' */
-                       emit->copy_from (_buffers.get(), to_emit_from_buffers, 0, to_emit - to_emit_from_buffers);
+                       F const to_return_from_buffers = min (to_return, _buffers->frames ());
 
+                       /* Copy the data that we have to the back end of the return buffer */
+                       out.audio->copy_from (_buffers.get(), to_return_from_buffers, 0, to_return - to_return_from_buffers);
                        /* Silence any gap at the start */
-                       emit->make_silent (0, to_emit - to_emit_from_buffers);
+                       out.audio->make_silent (0, to_return - to_return_from_buffers);
 
-                       /* Emit that */
-                       Audio (emit, _next_emission);
+                       out.time = _next_out;
+                       _next_out += _f_to_t (to_return);
 
-                       _next_emission += _f_to_t (to_emit);
-
-                       /* And remove the data we've emitted from our buffers */
-                       if (_buffers->frames() > to_emit_from_buffers) {
-                               _buffers->move (to_emit_from_buffers, 0, _buffers->frames() - to_emit_from_buffers);
+                       /* And remove the data we're returning from our buffers */
+                       if (_buffers->frames() > to_return_from_buffers) {
+                               _buffers->move (to_return_from_buffers, 0, _buffers->frames() - to_return_from_buffers);
                        }
-                       _buffers->set_frames (_buffers->frames() - to_emit_from_buffers);
+                       _buffers->set_frames (_buffers->frames() - to_return_from_buffers);
                }
 
                /* Now accumulate the new audio into our buffers */
                F frame = _t_to_f (time);
-               F after = max (_buffers->frames(), frame + audio->frames() - _t_to_f (_next_emission));
+               F after = max (_buffers->frames(), frame + audio->frames() - _t_to_f (_next_out));
                _buffers->ensure_size (after);
-               _buffers->accumulate_frames (audio.get(), 0, frame - _t_to_f (_next_emission), audio->frames ());
+               _buffers->accumulate_frames (audio.get(), 0, frame - _t_to_f (_next_out), audio->frames ());
                _buffers->set_frames (after);
+
+               return out;
        }
 
        F min (F a, int b)
@@ -86,18 +90,19 @@ public:
                return b;
        }
                
-       void flush ()
+       TimedAudioBuffers<T>
+       flush ()
        {
-               if (_buffers->frames() > 0) {
-                       Audio (_buffers, _next_emission);
+               if (_buffers->frames() == 0) {
+                       return TimedAudioBuffers<T> ();
                }
+               
+               return TimedAudioBuffers<T> (_buffers, _next_out);
        }
        
-       boost::signals2::signal<void (boost::shared_ptr<const AudioBuffers>, T)> Audio;
-
 private:
        boost::shared_ptr<AudioBuffers> _buffers;
-       T _next_emission;
+       T _next_out;
        boost::function<F (T)> _t_to_f;
        boost::function<T (F)> _f_to_t;
 };
index dbc78b8d02593f5abb90e925b057499c1dd0ede2..e2f23e6e37e6319989c5b8155a015ccc41abdbc1 100644 (file)
@@ -48,7 +48,7 @@ using boost::shared_ptr;
 using boost::weak_ptr;
 using boost::dynamic_pointer_cast;
 
-//#define DEBUG_PLAYER 1
+#define DEBUG_PLAYER 1
 
 class Piece
 {
@@ -102,7 +102,6 @@ Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
        _playlist->Changed.connect (bind (&Player::playlist_changed, this));
        _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2, _3));
        _film->Changed.connect (bind (&Player::film_changed, this, _1));
-       _audio_merger.Audio.connect (bind (&Player::merger_process_audio, this, _1, _2));
        set_video_container_size (_film->container()->size (_film->full_frame ()));
 }
 
@@ -319,20 +318,25 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
                time = 0;
        }
 
-       _audio_merger.push (audio, time);
-}
-
-void
-Player::merger_process_audio (shared_ptr<const AudioBuffers> audio, Time time)
-{
-       Audio (audio, time);
-       _audio_position += _film->audio_frames_to_time (audio->frames ());
+       cout << "push " << audio->frames() << " @ " << time << " from " << content->path() << "\n";
+       TimedAudioBuffers<Time> tb = _audio_merger.push (audio, time);
+       piece->audio_position += _film->audio_frames_to_time (audio->frames ());
+       
+       if (tb.audio) {
+               Audio (tb.audio, tb.time);
+               _audio_position += _film->audio_frames_to_time (tb.audio->frames ());
+               cout << "output " << tb.audio->frames() << " @ " << tb.time << "\n";
+       }
 }
 
 void
 Player::flush ()
 {
-       _audio_merger.flush ();
+       TimedAudioBuffers<Time> tb = _audio_merger.flush ();
+       if (tb.audio) {
+               Audio (tb.audio, tb.time);
+               _audio_position += _film->audio_frames_to_time (tb.audio->frames ());
+       }
 
        while (_video_position < _audio_position) {
                emit_black ();
index 206254713cac368f8e26db375e44e20e48f9427d..79eecf13666b40aeb2148857d45301a9d9ae8182 100644 (file)
@@ -94,7 +94,6 @@ private:
        boost::shared_ptr<Resampler> resampler (boost::shared_ptr<AudioContent>, bool);
        void film_changed (Film::Property);
        void update_subtitle ();
-       void merger_process_audio (boost::shared_ptr<const AudioBuffers>, Time);
 
        boost::shared_ptr<const Film> _film;
        boost::shared_ptr<const Playlist> _playlist;
index d6136fc3e2778d08dbbba65392827e704a2c3bc3..e1487ed4d708c07925f4978bafedc85a0035e0c4 100644 (file)
@@ -26,6 +26,7 @@
 #include <libdcp/util.h>
 
 class Content;
+class AudioBuffers;
 
 typedef int64_t Time;
 #define TIME_MAX INT64_MAX
@@ -34,6 +35,22 @@ typedef int64_t OutputAudioFrame;
 typedef int    OutputVideoFrame;
 typedef std::vector<boost::shared_ptr<Content> > ContentList;
 
+template<class T>
+struct TimedAudioBuffers
+{
+       TimedAudioBuffers ()
+               : time (0)
+       {}
+       
+       TimedAudioBuffers (boost::shared_ptr<AudioBuffers> a, T t)
+               : audio (a)
+               , time (t)
+       {}
+       
+       boost::shared_ptr<AudioBuffers> audio;
+       T time;
+};
+
 enum VideoFrameType
 {
        VIDEO_FRAME_TYPE_2D,
index 9c3fd3a30d9e249c7016240034dfcc44e6a4523f..f1d1dd634a6af8046f73608e4cd4a4fad9072ed6 100644 (file)
@@ -36,57 +36,39 @@ pass_through (int x)
        return x;
 }
 
-static void
-process_audio (shared_ptr<const AudioBuffers> audio, int time)
-{
-       last_audio = audio;
-       last_time = time;
-}
-
-static void
-reset ()
-{
-       last_audio.reset ();
-       last_time = 0;
-}
-
 BOOST_AUTO_TEST_CASE (audio_merger_test1)
 {
        AudioMerger<int, int> merger (1, bind (&pass_through, _1), boost::bind (&pass_through, _1));
-       merger.Audio.connect (bind (&process_audio, _1, _2));
 
-       reset ();
-       
        /* Push 64 samples, 0 -> 63 at time 0 */
        shared_ptr<AudioBuffers> buffers (new AudioBuffers (1, 64));
        for (int i = 0; i < 64; ++i) {
                buffers->data()[0][i] = i;
        }
-       merger.push (buffers, 0);
+       TimedAudioBuffers<int> tb = merger.push (buffers, 0);
 
        /* That should not have caused an emission */
-       BOOST_CHECK_EQUAL (last_audio, shared_ptr<const AudioBuffers> ());
-       BOOST_CHECK_EQUAL (last_time, 0);
+       BOOST_CHECK_EQUAL (tb.audio, shared_ptr<const AudioBuffers> ());
+       BOOST_CHECK_EQUAL (tb.time, 0);
 
        /* Push 64 samples, 0 -> 63 at time 22 */
-       merger.push (buffers, 22);
+       tb = merger.push (buffers, 22);
 
        /* That should have caused an emission of 22 samples at 0 */
-       BOOST_CHECK (last_audio != shared_ptr<const AudioBuffers> ());
-       BOOST_CHECK_EQUAL (last_audio->frames(), 22);
-       BOOST_CHECK_EQUAL (last_time, 0);
+       BOOST_CHECK (tb.audio != shared_ptr<const AudioBuffers> ());
+       BOOST_CHECK_EQUAL (tb.audio->frames(), 22);
+       BOOST_CHECK_EQUAL (tb.time, 0);
 
        /* And they should be a staircase */
        for (int i = 0; i < 22; ++i) {
-               BOOST_CHECK_EQUAL (last_audio->data()[0][i], i);
+               BOOST_CHECK_EQUAL (tb.audio->data()[0][i], i);
        }
 
-       reset ();
-       merger.flush ();
+       tb = merger.flush ();
 
        /* That flush should give us 64 samples at 22 */
-       BOOST_CHECK_EQUAL (last_audio->frames(), 64);
-       BOOST_CHECK_EQUAL (last_time, 22);
+       BOOST_CHECK_EQUAL (tb.audio->frames(), 64);
+       BOOST_CHECK_EQUAL (tb.time, 22);
 
        /* Check the sample values */
        for (int i = 0; i < 64; ++i) {
@@ -94,41 +76,37 @@ BOOST_AUTO_TEST_CASE (audio_merger_test1)
                if (i < (64 - 22)) {
                        correct += i + 22;
                }
-               BOOST_CHECK_EQUAL (last_audio->data()[0][i], correct);
+               BOOST_CHECK_EQUAL (tb.audio->data()[0][i], correct);
        }
 }
 
 BOOST_AUTO_TEST_CASE (audio_merger_test2)
 {
        AudioMerger<int, int> merger (1, bind (&pass_through, _1), boost::bind (&pass_through, _1));
-       merger.Audio.connect (bind (&process_audio, _1, _2));
 
-       reset ();
-       
        /* Push 64 samples, 0 -> 63 at time 9 */
        shared_ptr<AudioBuffers> buffers (new AudioBuffers (1, 64));
        for (int i = 0; i < 64; ++i) {
                buffers->data()[0][i] = i;
        }
-       merger.push (buffers, 9);
+       TimedAudioBuffers<int> tb = merger.push (buffers, 9);
 
        /* That flush should give us 9 samples at 0 */
-       BOOST_CHECK_EQUAL (last_audio->frames(), 9);
-       BOOST_CHECK_EQUAL (last_time, 0);
+       BOOST_CHECK_EQUAL (tb.audio->frames(), 9);
+       BOOST_CHECK_EQUAL (tb.time, 0);
        
        for (int i = 0; i < 9; ++i) {
-               BOOST_CHECK_EQUAL (last_audio->data()[0][i], 0);
+               BOOST_CHECK_EQUAL (tb.audio->data()[0][i], 0);
        }
        
-       reset ();
-       merger.flush ();
+       tb = merger.flush ();
 
        /* That flush should give us 64 samples at 9 */
-       BOOST_CHECK_EQUAL (last_audio->frames(), 64);
-       BOOST_CHECK_EQUAL (last_time, 9);
+       BOOST_CHECK_EQUAL (tb.audio->frames(), 64);
+       BOOST_CHECK_EQUAL (tb.time, 9);
        
        /* Check the sample values */
        for (int i = 0; i < 64; ++i) {
-               BOOST_CHECK_EQUAL (last_audio->data()[0][i], i);
+               BOOST_CHECK_EQUAL (tb.audio->data()[0][i], i);
        }
 }