fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / resampled_source.cc
1 /*
2     Copyright (C) 2007 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include "pbd/error.h"
21 #include "ardour/resampled_source.h"
22 #include "pbd/failed_constructor.h"
23
24 #include "pbd/i18n.h"
25
26 using namespace ARDOUR;
27 using namespace PBD;
28
29 #ifdef PLATFORM_WINDOWS
30 const uint32_t ResampledImportableSource::blocksize = 524288U;
31 #else
32 const uint32_t ResampledImportableSource::blocksize = 16384U;
33 #endif
34
35 ResampledImportableSource::ResampledImportableSource (boost::shared_ptr<ImportableSource> src, framecnt_t rate, SrcQuality srcq)
36         : source (src)
37         , _src_state (0)
38 {
39         _src_type = SRC_SINC_BEST_QUALITY;
40
41         switch (srcq) {
42         case SrcBest:
43                 _src_type = SRC_SINC_BEST_QUALITY;
44                 break;
45         case SrcGood:
46                 _src_type = SRC_SINC_MEDIUM_QUALITY;
47                 break;
48         case SrcQuick:
49                 _src_type = SRC_SINC_FASTEST;
50                 break;
51         case SrcFast:
52                 _src_type = SRC_ZERO_ORDER_HOLD;
53                 break;
54         case SrcFastest:
55                 _src_type = SRC_LINEAR;
56                 break;
57         }
58
59         _input = new float[blocksize];
60
61         seek (0);
62
63         _src_data.src_ratio = ((float) rate) / source->samplerate();
64 }
65
66 ResampledImportableSource::~ResampledImportableSource ()
67 {
68         _src_state = src_delete (_src_state) ;
69         delete [] _input;
70 }
71
72 framecnt_t
73 ResampledImportableSource::read (Sample* output, framecnt_t nframes)
74 {
75         int err;
76         size_t bs = floor ((float)(blocksize / source->channels())) *  source->channels();
77
78         /* If the input buffer is empty, refill it. */
79         if (_src_data.input_frames == 0) {
80
81                 _src_data.input_frames = source->read (_input, bs);
82
83                 /* The last read will not be a full buffer, so set end_of_input. */
84                 if ((size_t) _src_data.input_frames < bs) {
85                         _end_of_input = true;
86                 }
87
88                 _src_data.input_frames /= source->channels();
89                 _src_data.data_in = _input;
90         }
91
92         _src_data.data_out = output;
93         _src_data.output_frames = nframes / source->channels();
94
95         if (_end_of_input && _src_data.input_frames * _src_data.src_ratio <= _src_data.output_frames) {
96                 /* only set src_data.end_of_input for the last cycle.
97                  *
98                  * The flag only affects writing out remaining data in the
99                  * internal buffer of src_state.
100                  * SRC is not aware of data bufered here in _src_data.input
101                  * which needs to be processed first.
102                  */
103                 _src_data.end_of_input = true;
104         }
105
106         if ((err = src_process (_src_state, &_src_data))) {
107                 error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
108                 return 0 ;
109         }
110
111         /* Terminate if at end */
112         if (_src_data.end_of_input && _src_data.output_frames_gen == 0) {
113                 return 0;
114         }
115
116         _src_data.data_in += _src_data.input_frames_used * source->channels();
117         _src_data.input_frames -= _src_data.input_frames_used ;
118
119         return _src_data.output_frames_gen * source->channels();
120 }
121
122 void
123 ResampledImportableSource::seek (framepos_t pos)
124 {
125         source->seek (pos);
126
127         /* and reset things so that we start from scratch with the conversion */
128
129         if (_src_state) {
130                 src_delete (_src_state);
131         }
132
133         int err;
134
135         if ((_src_state = src_new (_src_type, source->channels(), &err)) == 0) {
136                 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
137                 throw failed_constructor ();
138         }
139
140         _src_data.input_frames = 0;
141         _src_data.data_in = _input;
142         _src_data.end_of_input = 0;
143         _end_of_input = false;
144 }
145
146 framepos_t
147 ResampledImportableSource::natural_position () const
148 {
149         return source->natural_position() * ratio ();
150 }