*** NEW CODING POLICY ***
[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 {
34         int err;
35         
36         source->seek (0);
37         
38         /* Initialize the sample rate converter. */
39         
40         int src_type = SRC_SINC_BEST_QUALITY;
41
42         switch (srcq) {
43         case SrcBest:
44                 src_type = SRC_SINC_BEST_QUALITY;
45                 break;
46         case SrcGood:
47                 src_type = SRC_SINC_MEDIUM_QUALITY;
48                 break;
49         case SrcQuick:
50                 src_type = SRC_SINC_FASTEST;
51                 break;
52         case SrcFast:
53                 src_type = SRC_ZERO_ORDER_HOLD;
54                 break;
55         case SrcFastest:
56                 src_type = SRC_LINEAR;
57                 break;
58         }
59         
60         if ((src_state = src_new (src_type, source->channels(), &err)) == 0) {  
61                 error << string_compose(_("Import: src_new() failed : %1"), src_strerror (err)) << endmsg ;
62                 throw failed_constructor ();
63         }
64         
65         src_data.end_of_input = 0 ; /* Set this later. */
66         
67         /* Start with zero to force load in while loop. */
68         
69         src_data.input_frames = 0 ;
70         src_data.data_in = input ;
71         
72         src_data.src_ratio = ((float) rate) / source->samplerate();
73         
74         input = new float[blocksize];
75 }
76
77 ResampledImportableSource::~ResampledImportableSource ()
78 {
79         src_state = src_delete (src_state) ;
80         delete [] input;
81 }
82
83 nframes_t 
84 ResampledImportableSource::read (Sample* output, nframes_t nframes)
85 {
86         int err;
87
88         /* If the input buffer is empty, refill it. */
89         
90         if (src_data.input_frames == 0) {       
91
92                 src_data.input_frames = source->read (input, blocksize);
93
94                 /* The last read will not be a full buffer, so set end_of_input. */
95
96                 if ((nframes_t) src_data.input_frames < blocksize) {
97                         src_data.end_of_input = true;
98                 }               
99
100                 src_data.input_frames /= source->channels();
101                 src_data.data_in = input;
102         } 
103         
104         src_data.data_out = output;
105
106         if (!src_data.end_of_input) {
107                 src_data.output_frames = nframes / source->channels();
108         } else {
109                 src_data.output_frames = src_data.input_frames;
110         }
111
112         if ((err = src_process (src_state, &src_data))) {
113                 error << string_compose(_("Import: %1"), src_strerror (err)) << endmsg ;
114                 return 0 ;
115         } 
116         
117         /* Terminate if at end */
118         
119         if (src_data.end_of_input && src_data.output_frames_gen == 0) {
120                 return 0;
121         }
122         
123         src_data.data_in += src_data.input_frames_used * source->channels();
124         src_data.input_frames -= src_data.input_frames_used ;
125
126         return src_data.output_frames_gen * source->channels();
127 }
128