Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / resampler.cc
index 80175e5f7e505fc72a267ef5951824a2e4a50634..322c00c136779f009ad1163119bce5496a191dad 100644 (file)
@@ -53,13 +53,17 @@ Resampler::Resampler (int in, int out, int channels)
 
 Resampler::~Resampler ()
 {
-       src_delete (_src);
+       if (_src) {
+               src_delete (_src);
+       }
 }
 
 void
 Resampler::set_fast ()
 {
        src_delete (_src);
+       _src = 0;
+
        int error;
        _src = src_new (SRC_LINEAR, _channels, &error);
        if (!_src) {
@@ -81,11 +85,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];
@@ -93,6 +97,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];
@@ -117,6 +122,8 @@ Resampler::run (shared_ptr<const AudioBuffers> in)
                }
 
                if (data.output_frames_gen == 0) {
+                       delete[] data.data_in;
+                       delete[] data.data_out;
                        break;
                }
 
@@ -184,3 +191,9 @@ Resampler::flush ()
        delete[] buffer;
        return out;
 }
+
+void
+Resampler::reset ()
+{
+       src_reset (_src);
+}