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
)
35 _src_type
= SRC_SINC_BEST_QUALITY
;
39 _src_type
= SRC_SINC_BEST_QUALITY
;
42 _src_type
= SRC_SINC_MEDIUM_QUALITY
;
45 _src_type
= SRC_SINC_FASTEST
;
48 _src_type
= SRC_ZERO_ORDER_HOLD
;
51 _src_type
= SRC_LINEAR
;
55 input
= new float[blocksize
];
59 src_data
.src_ratio
= ((float) rate
) / source
->samplerate();
62 ResampledImportableSource::~ResampledImportableSource ()
64 src_state
= src_delete (src_state
) ;
69 ResampledImportableSource::read (Sample
* output
, nframes_t nframes
)
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();
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
;
102 /* Terminate if at end */
104 if (src_data
.end_of_input
&& src_data
.output_frames_gen
== 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();
115 ResampledImportableSource::seek (nframes_t pos
)
119 /* and reset things so that we start from scratch with the conversion */
122 src_delete (src_state
);
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;