More attempts to fix audio / merging etc.
authorCarl Hetherington <cth@carlh.net>
Sat, 27 Jul 2013 20:44:29 +0000 (21:44 +0100)
committerCarl Hetherington <cth@carlh.net>
Sat, 27 Jul 2013 20:44:29 +0000 (21:44 +0100)
src/lib/audio_merger.h
src/lib/player.cc
src/lib/player.h
test/audio_merger_test.cc

index afb21871bd0c72f031acae149ff69eaa7aa11c23..226601e0ec61dffc92726bd8f98bbd976f21e9ad 100644 (file)
@@ -26,50 +26,51 @@ 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_out (0)
+               , _last_pull (0)
                , _t_to_f (t_to_f)
                , _f_to_t (f_to_t)
        {}
 
+       /** Pull audio up to a given time; after this call, no more data can be pushed
+        *  before the specified time.
+        */
        TimedAudioBuffers<T>
-       push (boost::shared_ptr<const AudioBuffers> audio, T time)
+       pull (T time)
        {
-               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_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 */
-                       out.audio->make_silent (0, to_return - to_return_from_buffers);
+               F const to_return = _t_to_f (time - _last_pull);
+               out.audio.reset (new AudioBuffers (_buffers->channels(), to_return));
+               /* And this is how many we will get from our buffer */
+               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 */
+               out.audio->make_silent (0, to_return - to_return_from_buffers);
+               
+               out.time = _last_pull;
+               _last_pull = time;
+               
+               /* 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_return_from_buffers);
 
-                       out.time = _next_out;
-                       _next_out += _f_to_t (to_return);
+               return out;
+       }
 
-                       /* 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_return_from_buffers);
-               }
+       void
+       push (boost::shared_ptr<const AudioBuffers> audio, T time)
+       {
+               assert (time >= _last_pull);
 
-               /* 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_out));
+               F after = max (_buffers->frames(), frame + audio->frames() - _t_to_f (_last_pull));
                _buffers->ensure_size (after);
-               _buffers->accumulate_frames (audio.get(), 0, frame - _t_to_f (_next_out), audio->frames ());
+               _buffers->accumulate_frames (audio.get(), 0, frame - _t_to_f (_last_pull), audio->frames ());
                _buffers->set_frames (after);
-
-               return out;
        }
 
        F min (F a, int b)
@@ -97,12 +98,12 @@ public:
                        return TimedAudioBuffers<T> ();
                }
                
-               return TimedAudioBuffers<T> (_buffers, _next_out);
+               return TimedAudioBuffers<T> (_buffers, _last_pull);
        }
        
 private:
        boost::shared_ptr<AudioBuffers> _buffers;
-       T _next_out;
+       T _last_pull;
        boost::function<F (T)> _t_to_f;
        boost::function<T (F)> _f_to_t;
 };
index e2f23e6e37e6319989c5b8155a015ccc41abdbc1..bf5bc6489eb1e5f6c61009ff9cd0769c5e43cd37 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
 {
@@ -98,6 +98,7 @@ Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
        , _video_position (0)
        , _audio_position (0)
        , _audio_merger (f->audio_channels(), bind (&Film::time_to_audio_frames, f.get(), _1), bind (&Film::audio_frames_to_time, f.get(), _1))
+       , _last_emit_was_black (false)
 {
        _playlist->Changed.connect (bind (&Player::playlist_changed, this));
        _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2, _3));
@@ -191,7 +192,7 @@ Player::pass ()
                } else {
 #ifdef DEBUG_PLAYER
                        cout << "Pass " << *earliest << "\n";
-#endif                 
+#endif
                        earliest->decoder->pass ();
 
                        if (earliest->decoder->done()) {
@@ -205,11 +206,25 @@ Player::pass ()
                                        }
                                }
                        }
-       
+
+                       
                }
+
+               Time done_up_to = TIME_MAX;
+               for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
+                       if (dynamic_pointer_cast<AudioContent> ((*i)->content)) {
+                               done_up_to = min (done_up_to, (*i)->audio_position);
+                       }
+               }
+
+               TimedAudioBuffers<Time> tb = _audio_merger.pull (done_up_to);
+               Audio (tb.audio, tb.time);
+               _audio_position += _film->audio_frames_to_time (tb.audio->frames ());
                break;
        }
 
+       
+
 #ifdef DEBUG_PLAYER
        cout << "\tpost pass " << _video_position << " " << _audio_position << "\n";
 #endif 
@@ -266,6 +281,8 @@ Player::process_video (weak_ptr<Piece> weak_piece, shared_ptr<const Image> image
                time += TIME_HZ / _film->video_frame_rate();
        }
 
+       _last_emit_was_black = false;
+
        _video_position = piece->video_position = time;
 }
 
@@ -318,15 +335,8 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
                time = 0;
        }
 
-       cout << "push " << audio->frames() << " @ " << time << " from " << content->path() << "\n";
-       TimedAudioBuffers<Time> tb = _audio_merger.push (audio, time);
+       _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
@@ -531,9 +541,9 @@ Player::emit_black ()
        _last_video.reset ();
 #endif
        
-       /* XXX: use same here */
-       Video (_black_frame, EYES_BOTH, false, _video_position);
+       Video (_black_frame, EYES_BOTH, _last_emit_was_black, _video_position);
        _video_position += _film->video_frames_to_time (1);
+       _last_emit_was_black = true;
 }
 
 void
index 79eecf13666b40aeb2148857d45301a9d9ae8182..cb5643753855bf7296e0575188e8c7fb2405ff61 100644 (file)
@@ -133,7 +133,9 @@ private:
 
 #ifdef DCPOMATIC_DEBUG
        boost::shared_ptr<Content> _last_video;
-#endif 
+#endif
+
+       bool _last_emit_was_black;
 };
 
 #endif
index f1d1dd634a6af8046f73608e4cd4a4fad9072ed6..82b7a29d8acae1cc694cab3e2699e9a2cb9f41aa 100644 (file)
@@ -45,16 +45,12 @@ BOOST_AUTO_TEST_CASE (audio_merger_test1)
        for (int i = 0; i < 64; ++i) {
                buffers->data()[0][i] = i;
        }
-       TimedAudioBuffers<int> tb = merger.push (buffers, 0);
-
-       /* That should not have caused an emission */
-       BOOST_CHECK_EQUAL (tb.audio, shared_ptr<const AudioBuffers> ());
-       BOOST_CHECK_EQUAL (tb.time, 0);
+       merger.push (buffers, 0);
 
        /* Push 64 samples, 0 -> 63 at time 22 */
-       tb = merger.push (buffers, 22);
+       merger.push (buffers, 22);
 
-       /* That should have caused an emission of 22 samples at 0 */
+       TimedAudioBuffers<int> tb = merger.pull (22);
        BOOST_CHECK (tb.audio != shared_ptr<const AudioBuffers> ());
        BOOST_CHECK_EQUAL (tb.audio->frames(), 22);
        BOOST_CHECK_EQUAL (tb.time, 0);
@@ -89,9 +85,9 @@ BOOST_AUTO_TEST_CASE (audio_merger_test2)
        for (int i = 0; i < 64; ++i) {
                buffers->data()[0][i] = i;
        }
-       TimedAudioBuffers<int> tb = merger.push (buffers, 9);
+       merger.push (buffers, 9);
 
-       /* That flush should give us 9 samples at 0 */
+       TimedAudioBuffers<int> tb = merger.pull (9);
        BOOST_CHECK_EQUAL (tb.audio->frames(), 9);
        BOOST_CHECK_EQUAL (tb.time, 0);