Hopefully much cleaner handling of PTS changes under resample.
[dcpomatic.git] / src / lib / player.cc
index 8cc1849e865f0909001a55374954bca5fbe48247..620efbd0dc30cb17bcde648f75dadfbf71fae5c1 100644 (file)
@@ -191,7 +191,7 @@ Player::pass ()
                        cout << "Pass " << *earliest << "\n";
 #endif                 
                        earliest->decoder->pass ();
-                       
+
                        if (earliest->decoder->done()) {
                                shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> (earliest->content);
                                assert (ac);
@@ -203,6 +203,7 @@ Player::pass ()
                                        }
                                }
                        }
+       
                }
                break;
        }
@@ -280,9 +281,11 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
        /* Resample */
        if (content->content_audio_frame_rate() != content->output_audio_frame_rate()) {
                shared_ptr<Resampler> r = resampler (content, true);
-               audio = r->run (audio);
+               pair<shared_ptr<const AudioBuffers>, AudioContent::Frame> ro = r->run (audio, frame);
+               audio = ro.first;
+               frame = ro.second;
        }
-
+       
        /* Remap channels */
        shared_ptr<AudioBuffers> dcp_mapped (new AudioBuffers (_film->audio_channels(), audio->frames()));
        dcp_mapped->make_silent ();
@@ -295,10 +298,9 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
 
        audio = dcp_mapped;
 
-       /* Convert frame to time.  After resampling, the frame time (in the DCP rate) will be T_D where
-          T_D = frame * DCP_rate / original_rate.  Hence the time in seconds is T_D / DCP_rate.
-       */
-       Time time = content->start() + (frame * TIME_HZ / _film->audio_frame_rate()) + (content->audio_delay() * TIME_HZ / 1000);
+       Time time = content->start()
+               + _film->audio_frames_to_time (frame)
+               + (content->audio_delay() * TIME_HZ / 1000);
 
        /* We must cut off anything that comes before the start of all time */
        if (time < 0) {
@@ -319,7 +321,7 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
        */
 
        if (time > _audio_position) {
-               /* We can emit some audio from our buffers */
+               /* We can emit some audio from our buffers; this is how many frames */
                OutputAudioFrame const N = _film->time_to_audio_frames (time - _audio_position);
                if (N > _audio_buffers.frames()) {
                        /* We need some extra silence before whatever is in the buffers */
@@ -329,10 +331,13 @@ Player::process_audio (weak_ptr<Piece> weak_piece, shared_ptr<const AudioBuffers
                        _audio_buffers.set_frames (N);
                }
                assert (N <= _audio_buffers.frames());
+
+               /* XXX: not convinced that a copy is necessary here */
                shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
                emit->copy_from (&_audio_buffers, N, 0, 0);
                Audio (emit, _audio_position);
-               _audio_position = piece->audio_position = time + _film->audio_frames_to_time (N);
+
+               _audio_position = piece->audio_position = _audio_position + _film->audio_frames_to_time (N);
 
                /* And remove it from our buffers */
                if (_audio_buffers.frames() > N) {