second (and hopefully) final part of changes to respond to header format changes...
[ArdourMidi.git] / libs / ardour / resampled_source.cc
blob675a0e426dc083473f11c03e5af2cea4c9d7ebc8
1 /*
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"
24 #include "i18n.h"
26 using namespace ARDOUR;
27 using namespace PBD;
29 const uint32_t ResampledImportableSource::blocksize = 16384U;
31 ResampledImportableSource::ResampledImportableSource (boost::shared_ptr<ImportableSource> src, nframes_t rate, SrcQuality srcq)
32 : source (src)
33 , src_state (0)
35 _src_type = SRC_SINC_BEST_QUALITY;
37 switch (srcq) {
38 case SrcBest:
39 _src_type = SRC_SINC_BEST_QUALITY;
40 break;
41 case SrcGood:
42 _src_type = SRC_SINC_MEDIUM_QUALITY;
43 break;
44 case SrcQuick:
45 _src_type = SRC_SINC_FASTEST;
46 break;
47 case SrcFast:
48 _src_type = SRC_ZERO_ORDER_HOLD;
49 break;
50 case SrcFastest:
51 _src_type = SRC_LINEAR;
52 break;
55 input = new float[blocksize];
57 seek (0);
59 src_data.src_ratio = ((float) rate) / source->samplerate();
62 ResampledImportableSource::~ResampledImportableSource ()
64 src_state = src_delete (src_state) ;
65 delete [] input;
68 nframes_t
69 ResampledImportableSource::read (Sample* output, nframes_t nframes)
71 int err;
73 /* If the input buffer is empty, refill it. */
75 if (src_data.input_frames == 0) {
77 src_data.input_frames = source->read (input, blocksize);
79 /* The last read will not be a full buffer, so set end_of_input. */
81 if ((nframes_t) src_data.input_frames < blocksize) {
82 src_data.end_of_input = true;
85 src_data.input_frames /= source->channels();
86 src_data.data_in = input;
89 src_data.data_out = output;
91 if (!src_data.end_of_input) {
92 src_data.output_frames = nframes / source->channels();
93 } else {
94 src_data.output_frames = std::min ((nframes_t) src_data.input_frames, nframes / source->channels());
97 if ((err = src_process (src_state, &src_data))) {
98 error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
99 return 0 ;
102 /* Terminate if at end */
104 if (src_data.end_of_input && src_data.output_frames_gen == 0) {
105 return 0;
108 src_data.data_in += src_data.input_frames_used * source->channels();
109 src_data.input_frames -= src_data.input_frames_used ;
111 return src_data.output_frames_gen * source->channels();
114 void
115 ResampledImportableSource::seek (nframes_t pos)
117 source->seek (pos);
119 /* and reset things so that we start from scratch with the conversion */
121 if (src_state) {
122 src_delete (src_state);
125 int err;
127 if ((src_state = src_new (_src_type, source->channels(), &err)) == 0) {
128 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
129 throw failed_constructor ();
132 src_data.input_frames = 0;
133 src_data.data_in = input;
134 src_data.end_of_input = 0;