X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fresampled_source.cc;h=04eecf0dee9eebadd93a1991aff0e5fbe5d09694;hb=e845b9f9357c4ff471c02b1f63a61275bb4a7d28;hp=38aa3832b93d352fa22363200eb349a62ff7281a;hpb=f7f9d6fdc40248b190ec9c6e1a886261d55777ae;p=ardour.git diff --git a/libs/ardour/resampled_source.cc b/libs/ardour/resampled_source.cc index 38aa3832b9..04eecf0dee 100644 --- a/libs/ardour/resampled_source.cc +++ b/libs/ardour/resampled_source.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2007 Paul Davis + Copyright (C) 2007 Paul Davis This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,112 +17,134 @@ */ -#include -#include -#include +#include "pbd/error.h" +#include "ardour/resampled_source.h" +#include "pbd/failed_constructor.h" #include "i18n.h" using namespace ARDOUR; using namespace PBD; -const uint32_t ResampledImportableSource::blocksize = 4096U; +#ifdef PLATFORM_WINDOWS +const uint32_t ResampledImportableSource::blocksize = 524288U; +#else +const uint32_t ResampledImportableSource::blocksize = 16384U; +#endif -ResampledImportableSource::ResampledImportableSource (SNDFILE* sf, SF_INFO* info, nframes_t rate, SrcQuality srcq) - : ImportableSource (sf, info) +ResampledImportableSource::ResampledImportableSource (boost::shared_ptr src, framecnt_t rate, SrcQuality srcq) + : source (src) + , _src_state (0) { - int err; - - sf_seek (in, 0, SEEK_SET) ; - - /* Initialize the sample rate converter. */ - - int src_type; + _src_type = SRC_SINC_BEST_QUALITY; switch (srcq) { case SrcBest: - src_type = SRC_SINC_BEST_QUALITY; + _src_type = SRC_SINC_BEST_QUALITY; break; case SrcGood: - src_type = SRC_SINC_MEDIUM_QUALITY; + _src_type = SRC_SINC_MEDIUM_QUALITY; break; case SrcQuick: - src_type = SRC_SINC_FASTEST; + _src_type = SRC_SINC_FASTEST; break; case SrcFast: - src_type = SRC_ZERO_ORDER_HOLD; + _src_type = SRC_ZERO_ORDER_HOLD; break; case SrcFastest: - src_type = SRC_LINEAR; + _src_type = SRC_LINEAR; break; } - - if ((src_state = src_new (src_type, sf_info->channels, &err)) == 0) { - error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ; - throw failed_constructor (); - } - - src_data.end_of_input = 0 ; /* Set this later. */ - - /* Start with zero to force load in while loop. */ - - src_data.input_frames = 0 ; - src_data.data_in = input ; - - src_data.src_ratio = ((float) rate) / sf_info->samplerate ; - - input = new float[blocksize]; + + _input = new float[blocksize]; + + seek (0); + + _src_data.src_ratio = ((float) rate) / source->samplerate(); } ResampledImportableSource::~ResampledImportableSource () { - src_state = src_delete (src_state) ; - delete [] input; + _src_state = src_delete (_src_state) ; + delete [] _input; } -nframes_t -ResampledImportableSource::read (Sample* output, nframes_t nframes) +framecnt_t +ResampledImportableSource::read (Sample* output, framecnt_t nframes) { int err; + size_t bs = floor ((float)(blocksize / source->channels())) * source->channels(); /* If the input buffer is empty, refill it. */ - - if (src_data.input_frames == 0) { + if (_src_data.input_frames == 0) { - src_data.input_frames = ImportableSource::read (input, blocksize); + _src_data.input_frames = source->read (_input, bs); /* The last read will not be a full buffer, so set end_of_input. */ + if ((framecnt_t) _src_data.input_frames < bs) { + _end_of_input = true; + } - if ((nframes_t) src_data.input_frames < blocksize) { - src_data.end_of_input = SF_TRUE ; - } - - src_data.input_frames /= sf_info->channels; - src_data.data_in = input ; - } - - src_data.data_out = output; + _src_data.input_frames /= source->channels(); + _src_data.data_in = _input; + } - if (!src_data.end_of_input) { - src_data.output_frames = nframes / sf_info->channels ; - } else { - src_data.output_frames = src_data.input_frames; + _src_data.data_out = output; + _src_data.output_frames = nframes / source->channels(); + + if (_end_of_input && _src_data.input_frames * _src_data.src_ratio <= _src_data.output_frames) { + /* only set src_data.end_of_input for the last cycle. + * + * The flag only affects writing out remaining data in the + * internal buffer of src_state. + * SRC is not aware of data bufered here in _src_data.input + * which needs to be processed first. + */ + _src_data.end_of_input = true; } - if ((err = src_process (src_state, &src_data))) { + if ((err = src_process (_src_state, &_src_data))) { error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ; return 0 ; - } - + } + /* Terminate if at end */ - - if (src_data.end_of_input && src_data.output_frames_gen == 0) { + if (_src_data.end_of_input && _src_data.output_frames_gen == 0) { return 0; } - - src_data.data_in += src_data.input_frames_used * sf_info->channels ; - src_data.input_frames -= src_data.input_frames_used ; - return src_data.output_frames_gen * sf_info->channels; + _src_data.data_in += _src_data.input_frames_used * source->channels(); + _src_data.input_frames -= _src_data.input_frames_used ; + + return _src_data.output_frames_gen * source->channels(); } +void +ResampledImportableSource::seek (framepos_t pos) +{ + source->seek (pos); + + /* and reset things so that we start from scratch with the conversion */ + + if (_src_state) { + src_delete (_src_state); + } + + int err; + + if ((_src_state = src_new (_src_type, source->channels(), &err)) == 0) { + error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ; + throw failed_constructor (); + } + + _src_data.input_frames = 0; + _src_data.data_in = _input; + _src_data.end_of_input = 0; + _end_of_input = false; +} + +framepos_t +ResampledImportableSource::natural_position () const +{ + return source->natural_position() * ratio (); +}