X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fresampler.cc;h=553180f0832c7a26852b2354bec045b1263faa01;hb=2c35515c5db7b8e49c17fd4ddfa085393d544f9d;hp=4476d9f1f9b2220cab1160ae876246e301a49661;hpb=5a5324ed3a381a86dfe0a6e3932c1d58fdcd596f;p=dcpomatic.git diff --git a/src/lib/resampler.cc b/src/lib/resampler.cc index 4476d9f1f..553180f08 100644 --- a/src/lib/resampler.cc +++ b/src/lib/resampler.cc @@ -24,8 +24,8 @@ #include "compose.hpp" #include "dcpomatic_assert.h" #include -#include #include +#include #include "i18n.h" @@ -34,20 +34,18 @@ using std::pair; using std::make_pair; using std::runtime_error; using boost::shared_ptr; -using boost::make_shared; /** @param in Input sampling rate (Hz) * @param out Output sampling rate (Hz) * @param channels Number of channels. - * @param fast true to be fast rather than good. */ -Resampler::Resampler (int in, int out, int channels, bool fast) +Resampler::Resampler (int in, int out, int channels) : _in_rate (in) , _out_rate (out) , _channels (channels) { int error; - _src = src_new (fast ? SRC_LINEAR : SRC_SINC_BEST_QUALITY, _channels, &error); + _src = src_new (SRC_SINC_BEST_QUALITY, _channels, &error); if (!_src) { throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error)); } @@ -58,13 +56,24 @@ Resampler::~Resampler () src_delete (_src); } +void +Resampler::set_fast () +{ + src_delete (_src); + int error; + _src = src_new (SRC_LINEAR, _channels, &error); + if (!_src) { + throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error)); + } +} + shared_ptr Resampler::run (shared_ptr in) { int in_frames = in->frames (); int in_offset = 0; int out_offset = 0; - shared_ptr resampled = make_shared (_channels, 0); + shared_ptr resampled (new AudioBuffers (_channels, 0)); while (in_frames > 0) { @@ -72,11 +81,11 @@ Resampler::run (shared_ptr 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]; @@ -84,6 +93,7 @@ Resampler::run (shared_ptr in) } } + data.data_in = in_buffer; data.input_frames = in_frames; data.data_out = new float[max_resampled_frames * _channels]; @@ -108,6 +118,8 @@ Resampler::run (shared_ptr in) } if (data.output_frames_gen == 0) { + delete[] data.data_in; + delete[] data.data_out; break; } @@ -138,7 +150,7 @@ Resampler::run (shared_ptr in) shared_ptr Resampler::flush () { - shared_ptr out = make_shared (_channels, 0); + shared_ptr out (new AudioBuffers (_channels, 0)); int out_offset = 0; int64_t const output_size = 65536; @@ -175,3 +187,9 @@ Resampler::flush () delete[] buffer; return out; } + +void +Resampler::reset () +{ + src_reset (_src); +}