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. */
79 if (src_data.input_frames == 0) {
81 src_data.input_frames = source->read (input, blocksize);
83 /* The last read will not be a full buffer, so set end_of_input. */
85 if ((framecnt_t) src_data.input_frames < blocksize) {
86 src_data.end_of_input = true;
89 src_data.input_frames /= source->channels();
90 src_data.data_in = input;
93 src_data.data_out = output;
95 if (!src_data.end_of_input) {
96 src_data.output_frames = nframes / source->channels();
98 src_data.output_frames = std::min ((framecnt_t) src_data.input_frames, nframes / source->channels());
101 if ((err = src_process (src_state, &src_data))) {
102 error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
106 /* Terminate if at end */
108 if (src_data.end_of_input && src_data.output_frames_gen == 0) {
112 src_data.data_in += src_data.input_frames_used * source->channels();
113 src_data.input_frames -= src_data.input_frames_used ;
115 return src_data.output_frames_gen * source->channels();
119 ResampledImportableSource::seek (framepos_t pos)
123 /* and reset things so that we start from scratch with the conversion */
126 src_delete (src_state);
131 if ((src_state = src_new (_src_type, source->channels(), &err)) == 0) {
132 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
133 throw failed_constructor ();
136 src_data.input_frames = 0;
137 src_data.data_in = input;
138 src_data.end_of_input = 0;
142 ResampledImportableSource::natural_position () const
144 return source->natural_position() * ratio ();