Setup fast state of decoder after creation.
[dcpomatic.git] / src / lib / resampler.cc
index ff93d1609ae5e68590c3dcb76f8e2a337be216bf..b27d7c9668dda9f4bbb0419fcf1a2ed381f42c75 100644 (file)
@@ -25,6 +25,7 @@
 #include "dcpomatic_assert.h"
 #include <samplerate.h>
 #include <iostream>
+#include <cmath>
 
 #include "i18n.h"
 
@@ -42,6 +43,7 @@ Resampler::Resampler (int in, int out, int channels)
        : _in_rate (in)
        , _out_rate (out)
        , _channels (channels)
+       , _fast (false)
 {
        int error;
        _src = src_new (SRC_SINC_BEST_QUALITY, _channels, &error);
@@ -52,15 +54,25 @@ Resampler::Resampler (int in, int out, int channels)
 
 Resampler::~Resampler ()
 {
-       src_delete (_src);
+       if (_src) {
+               src_delete (_src);
+       }
 }
 
 void
-Resampler::set_fast ()
+Resampler::set_fast (bool fast)
 {
+       if (fast == _fast) {
+               return;
+       }
+
+       _fast = fast;
+
        src_delete (_src);
+       _src = 0;
+
        int error;
-       _src = src_new (SRC_LINEAR, _channels, &error);
+       _src = src_new (_fast ? SRC_LINEAR : SRC_SINC_BEST_QUALITY, _channels, &error);
        if (!_src) {
                throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
        }
@@ -80,11 +92,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 +104,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];
@@ -116,6 +129,8 @@ Resampler::run (shared_ptr<const AudioBuffers> in)
                }
 
                if (data.output_frames_gen == 0) {
+                       delete[] data.data_in;
+                       delete[] data.data_out;
                        break;
                }
 
@@ -183,3 +198,9 @@ Resampler::flush ()
        delete[] buffer;
        return out;
 }
+
+void
+Resampler::reset ()
+{
+       src_reset (_src);
+}