'libs/ardour' - Platform specific changes and includes
[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 #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
77         /* If the input buffer is empty, refill it. */
78
79         if (src_data.input_frames == 0) {
80
81                 src_data.input_frames = source->read (input, blocksize);
82
83                 /* The last read will not be a full buffer, so set end_of_input. */
84
85                 if ((framecnt_t) src_data.input_frames < blocksize) {
86                         src_data.end_of_input = true;
87                 }
88
89                 src_data.input_frames /= source->channels();
90                 src_data.data_in = input;
91         }
92
93         src_data.data_out = output;
94
95         if (!src_data.end_of_input) {
96                 src_data.output_frames = nframes / source->channels();
97         } else {
98                 src_data.output_frames = std::min ((framecnt_t) src_data.input_frames, nframes / source->channels());
99         }
100
101         if ((err = src_process (src_state, &src_data))) {
102                 error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
103                 return 0 ;
104         }
105
106         /* Terminate if at end */
107
108         if (src_data.end_of_input && src_data.output_frames_gen == 0) {
109                 return 0;
110         }
111
112         src_data.data_in += src_data.input_frames_used * source->channels();
113         src_data.input_frames -= src_data.input_frames_used ;
114
115         return src_data.output_frames_gen * source->channels();
116 }
117
118 void
119 ResampledImportableSource::seek (framepos_t pos)
120 {
121         source->seek (pos);
122
123         /* and reset things so that we start from scratch with the conversion */
124
125         if (src_state) {
126                 src_delete (src_state);
127         }
128
129         int err;
130
131         if ((src_state = src_new (_src_type, source->channels(), &err)) == 0) {
132                 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
133                 throw failed_constructor ();
134         }
135
136         src_data.input_frames = 0;
137         src_data.data_in = input;
138         src_data.end_of_input = 0;
139 }
140
141 framepos_t
142 ResampledImportableSource::natural_position () const
143 {
144         return source->natural_position() * ratio ();
145 }