#include "dcpomatic_assert.h"
#include <samplerate.h>
#include <iostream>
+#include <cmath>
#include "i18n.h"
}
}
-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;
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];
}
}
+ data.data_in = in_buffer;
data.input_frames = in_frames;
data.data_out = new float[max_resampled_frames * _channels];
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));
out->set_frames (out_offset);
delete[] buffer;
- return out;
+ return make_pair (out, _next_out.get ());
}