make resampled sources (during import) report their "natural position" (i.e. BWF...
[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 "i18n.h"
25
26 using namespace ARDOUR;
27 using namespace PBD;
28
29 const uint32_t ResampledImportableSource::blocksize = 16384U;
30
31 ResampledImportableSource::ResampledImportableSource (boost::shared_ptr<ImportableSource> src, nframes_t rate, SrcQuality srcq)
32         : source (src)
33         , src_state (0)
34 {
35         _src_type = SRC_SINC_BEST_QUALITY;
36
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;
53         }
54
55         input = new float[blocksize];
56
57         seek (0);
58         
59         src_data.src_ratio = ((float) rate) / source->samplerate();
60 }
61
62 ResampledImportableSource::~ResampledImportableSource ()
63 {
64         src_state = src_delete (src_state) ;
65         delete [] input;
66 }
67
68 nframes_t
69 ResampledImportableSource::read (Sample* output, nframes_t nframes)
70 {
71         int err;
72
73         /* If the input buffer is empty, refill it. */
74
75         if (src_data.input_frames == 0) {
76
77                 src_data.input_frames = source->read (input, blocksize);
78
79                 /* The last read will not be a full buffer, so set end_of_input. */
80
81                 if ((nframes_t) src_data.input_frames < blocksize) {
82                         src_data.end_of_input = true;
83                 }
84
85                 src_data.input_frames /= source->channels();
86                 src_data.data_in = input;
87         }
88
89         src_data.data_out = output;
90
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());
95         }
96
97         if ((err = src_process (src_state, &src_data))) {
98                 error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
99                 return 0 ;
100         }
101
102         /* Terminate if at end */
103
104         if (src_data.end_of_input && src_data.output_frames_gen == 0) {
105                 return 0;
106         }
107
108         src_data.data_in += src_data.input_frames_used * source->channels();
109         src_data.input_frames -= src_data.input_frames_used ;
110
111         return src_data.output_frames_gen * source->channels();
112 }
113
114 void
115 ResampledImportableSource::seek (nframes_t pos)
116 {
117         source->seek (pos);
118
119         /* and reset things so that we start from scratch with the conversion */
120
121         if (src_state) {
122                 src_delete (src_state);
123         }
124
125         int err;
126
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 ();
130         }
131         
132         src_data.input_frames = 0;
133         src_data.data_in = input;
134         src_data.end_of_input = 0;
135 }
136         
137 framepos_t
138 ResampledImportableSource::natural_position () const
139 {
140         return source->natural_position() * ratio ();
141 }