2 Copyright (C) 2007 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "pbd/error.h"
21 #include "ardour/resampled_source.h"
22 #include "pbd/failed_constructor.h"
26 using namespace ARDOUR;
29 #ifdef PLATFORM_WINDOWS
30 const uint32_t ResampledImportableSource::blocksize = 524288U;
32 const uint32_t ResampledImportableSource::blocksize = 16384U;
35 ResampledImportableSource::ResampledImportableSource (boost::shared_ptr<ImportableSource> src, framecnt_t rate, SrcQuality srcq)
39 _src_type = SRC_SINC_BEST_QUALITY;
43 _src_type = SRC_SINC_BEST_QUALITY;
46 _src_type = SRC_SINC_MEDIUM_QUALITY;
49 _src_type = SRC_SINC_FASTEST;
52 _src_type = SRC_ZERO_ORDER_HOLD;
55 _src_type = SRC_LINEAR;
59 _input = new float[blocksize];
63 _src_data.src_ratio = ((float) rate) / source->samplerate();
66 ResampledImportableSource::~ResampledImportableSource ()
68 _src_state = src_delete (_src_state) ;
73 ResampledImportableSource::read (Sample* output, framecnt_t nframes)
77 /* If the input buffer is empty, refill it. */
78 if (_src_data.input_frames == 0) {
80 _src_data.input_frames = source->read (_input, blocksize);
82 /* The last read will not be a full buffer, so set end_of_input. */
83 if ((framecnt_t) _src_data.input_frames < blocksize) {
87 _src_data.input_frames /= source->channels();
88 _src_data.data_in = _input;
91 _src_data.data_out = output;
92 _src_data.output_frames = nframes / source->channels();
94 if (_end_of_input && _src_data.input_frames * _src_data.src_ratio <= _src_data.output_frames) {
95 /* only set src_data.end_of_input for the last cycle.
97 * The flag only affects writing out remaining data in the
98 * internal buffer of src_state.
99 * SRC is not aware of data bufered here in _src_data.input
100 * which needs to be processed first.
102 _src_data.end_of_input = true;
105 if ((err = src_process (_src_state, &_src_data))) {
106 error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
110 /* Terminate if at end */
111 if (_src_data.end_of_input && _src_data.output_frames_gen == 0) {
115 _src_data.data_in += _src_data.input_frames_used * source->channels();
116 _src_data.input_frames -= _src_data.input_frames_used ;
118 return _src_data.output_frames_gen * source->channels();
122 ResampledImportableSource::seek (framepos_t pos)
126 /* and reset things so that we start from scratch with the conversion */
129 src_delete (_src_state);
134 if ((_src_state = src_new (_src_type, source->channels(), &err)) == 0) {
135 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
136 throw failed_constructor ();
139 _src_data.input_frames = 0;
140 _src_data.data_in = _input;
141 _src_data.end_of_input = 0;
142 _end_of_input = false;
146 ResampledImportableSource::natural_position () const
148 return source->natural_position() * ratio ();