Various Doxygen fixes.
[dcpomatic.git] / src / lib / resampler.cc
index ff93d1609ae5e68590c3dcb76f8e2a337be216bf..1a0c6073dcb6cc8278b74007d60b74f226069ba6 100644 (file)
@@ -25,6 +25,7 @@
 #include "dcpomatic_assert.h"
 #include <samplerate.h>
 #include <iostream>
+#include <cmath>
 
 #include "i18n.h"
 
@@ -66,9 +67,19 @@ Resampler::set_fast ()
        }
 }
 
-shared_ptr<const AudioBuffers>
-Resampler::run (shared_ptr<const AudioBuffers> in)
+pair<shared_ptr<const AudioBuffers>, Frame>
+Resampler::run (shared_ptr<const AudioBuffers> in, Frame frame)
 {
+       if (!_next_in || !_next_out || _next_in.get() != frame) {
+               /* Either there was a discontinuity in the input or this is the first input;
+                  reset _next_out.
+               */
+               _next_out = lrintf (frame * _out_rate / _in_rate);
+       }
+
+       /* Expected next input frame */
+       _next_in = frame + in->frames ();
+
        int in_frames = in->frames ();
        int in_offset = 0;
        int out_offset = 0;
@@ -80,11 +91,11 @@ Resampler::run (shared_ptr<const AudioBuffers> in)
                int const max_resampled_frames = ceil ((double) in_frames * _out_rate / _in_rate) + 32;
 
                SRC_DATA data;
-               data.data_in = new float[in_frames * _channels];
+               float* in_buffer = new float[in_frames * _channels];
 
                {
                        float** p = in->data ();
-                       float* q = data.data_in;
+                       float* q = in_buffer;
                        for (int i = 0; i < in_frames; ++i) {
                                for (int j = 0; j < _channels; ++j) {
                                        *q++ = p[j][in_offset + i];
@@ -92,6 +103,7 @@ Resampler::run (shared_ptr<const AudioBuffers> in)
                        }
                }
 
+               data.data_in = in_buffer;
                data.input_frames = in_frames;
 
                data.data_out = new float[max_resampled_frames * _channels];
@@ -140,10 +152,15 @@ Resampler::run (shared_ptr<const AudioBuffers> in)
                delete[] data.data_out;
        }
 
-       return resampled;
+       Frame out_frame = _next_out.get ();
+
+       /* Expected next output frame */
+       _next_out = _next_out.get() + resampled->frames();
+
+       return make_pair (resampled, out_frame);
 }
 
-shared_ptr<const AudioBuffers>
+pair<shared_ptr<const AudioBuffers>, Frame>
 Resampler::flush ()
 {
        shared_ptr<AudioBuffers> out (new AudioBuffers (_channels, 0));
@@ -181,5 +198,5 @@ Resampler::flush ()
        out->set_frames (out_offset);
 
        delete[] buffer;
-       return out;
+       return make_pair (out, _next_out.get ());
 }