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 const uint32_t ResampledImportableSource::blocksize = 16384U;
31 ResampledImportableSource::ResampledImportableSource (boost::shared_ptr<ImportableSource> src, nframes_t rate, SrcQuality srcq)
38 /* Initialize the sample rate converter. */
40 int src_type = SRC_SINC_BEST_QUALITY;
44 src_type = SRC_SINC_BEST_QUALITY;
47 src_type = SRC_SINC_MEDIUM_QUALITY;
50 src_type = SRC_SINC_FASTEST;
53 src_type = SRC_ZERO_ORDER_HOLD;
56 src_type = SRC_LINEAR;
60 if ((src_state = src_new (src_type, source->channels(), &err)) == 0) {
61 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
62 throw failed_constructor ();
65 src_data.end_of_input = 0 ; /* Set this later. */
67 /* Start with zero to force load in while loop. */
69 src_data.input_frames = 0 ;
70 src_data.data_in = input ;
72 src_data.src_ratio = ((float) rate) / source->samplerate();
74 input = new float[blocksize];
77 ResampledImportableSource::~ResampledImportableSource ()
79 src_state = src_delete (src_state) ;
84 ResampledImportableSource::read (Sample* output, nframes_t nframes)
88 /* If the input buffer is empty, refill it. */
90 if (src_data.input_frames == 0) {
92 src_data.input_frames = source->read (input, blocksize);
94 /* The last read will not be a full buffer, so set end_of_input. */
96 if ((nframes_t) src_data.input_frames < blocksize) {
97 src_data.end_of_input = true;
100 src_data.input_frames /= source->channels();
101 src_data.data_in = input;
104 src_data.data_out = output;
106 if (!src_data.end_of_input) {
107 src_data.output_frames = nframes / source->channels();
109 src_data.output_frames = src_data.input_frames;
112 if ((err = src_process (src_state, &src_data))) {
113 error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
117 /* Terminate if at end */
119 if (src_data.end_of_input && src_data.output_frames_gen == 0) {
123 src_data.data_in += src_data.input_frames_used * source->channels();
124 src_data.input_frames -= src_data.input_frames_used ;
126 return src_data.output_frames_gen * source->channels();