0de5a5bf4a402730903f1c6fa947ff367e29d2b6
[ardour.git] / libs / ardour / rb_effect.cc
1 /*
2     Copyright (C) 2004-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 <algorithm>
21 #include <cmath>
22
23 #include <pbd/error.h>
24 #include <rubberband/RubberBandStretcher.h>
25
26 #include <ardour/types.h>
27 #include <ardour/stretch.h>
28 #include <ardour/pitch.h>
29 #include <ardour/audiofilesource.h>
30 #include <ardour/session.h>
31 #include <ardour/audioregion.h>
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38 using namespace RubberBand;
39
40 Pitch::Pitch (Session& s, TimeFXRequest& req)
41         : RBEffect (s, req)
42 {
43 }
44
45 Stretch::Stretch (Session& s, TimeFXRequest& req)
46         : RBEffect (s, req)
47 {
48 }
49
50 RBEffect::RBEffect (Session& s, TimeFXRequest& req)
51         : Filter (s)
52         , tsr (req)
53
54 {
55         tsr.progress = 0.0f;
56 }
57
58 RBEffect::~RBEffect ()
59 {
60 }
61
62 int
63 RBEffect::run (boost::shared_ptr<AudioRegion> region)
64 {
65         SourceList nsrcs;
66         nframes_t done;
67         int ret = -1;
68         const nframes_t bufsize = 256;
69         gain_t* gain_buffer = 0;
70         Sample** buffers = 0;
71         char suffix[32];
72         string new_name;
73         string::size_type at;
74         nframes_t pos = 0;
75         int avail = 0;
76
77         RubberBandStretcher stretcher (session.frame_rate(), region->n_channels(),
78                                        RubberBandStretcher::DefaultOptions,
79                                        tsr.time_fraction, tsr.pitch_fraction);
80         
81         stretcher.setExpectedInputDuration(region->length());
82         stretcher.setDebugLevel(1);
83
84         tsr.progress = 0.0f;
85         tsr.done = false;
86
87         uint32_t channels = region->n_channels();
88         nframes_t duration = region->length();
89
90         /* the name doesn't need to be super-precise, but allow for 2 fractional
91            digits just to disambiguate close but not identical FX
92         */
93
94         if (tsr.time_fraction == 1.0) {
95                 snprintf (suffix, sizeof (suffix), "@%d", (int) floor (tsr.pitch_fraction * 100.0f));
96         } else if (tsr.pitch_fraction == 1.0) {
97                 snprintf (suffix, sizeof (suffix), "@%d", (int) floor (tsr.time_fraction * 100.0f));
98         } else {
99                 snprintf (suffix, sizeof (suffix), "@%d-%d", 
100                           (int) floor (tsr.time_fraction * 100.0f),
101                           (int) floor (tsr.pitch_fraction * 100.0f));
102         }
103
104         /* create new sources */
105
106         if (make_new_sources (region, nsrcs, suffix)) {
107                 goto out;
108         }
109
110         gain_buffer = new gain_t[bufsize];
111         buffers = new float *[channels];
112
113         for (uint32_t i = 0; i < channels; ++i) {
114                 buffers[i] = new float[bufsize];
115         }
116
117         /* we read from the master (original) sources for the region,
118            not the ones currently in use, in case it's already been
119            subject to timefx.  */
120
121         /* study first, process afterwards. */
122
123         pos = 0;
124         avail = 0;
125         done = 0;
126
127         try { 
128                 while (pos < duration && !tsr.cancel) {
129                         
130                         nframes_t this_read = 0;
131                         
132                         for (uint32_t i = 0; i < channels; ++i) {
133                                 
134                                 this_read = 0;
135                                 nframes_t this_time;
136                                 
137                                 this_time = min(bufsize, duration - pos);
138                                 
139                                 this_read = region->master_read_at
140                                         (buffers[i],
141                                          buffers[i],
142                                          gain_buffer,
143                                          pos + region->position(),
144                                          this_time,
145                                          i);
146                                 
147                                 if (this_read != this_time) {
148                                         error << string_compose
149                                                 (_("tempoize: error reading data from %1"),
150                                                  nsrcs[i]->name()) << endmsg;
151                                         goto out;
152                                 }
153                         }
154                         
155                         pos += this_read;
156                         done += this_read;
157
158                         tsr.progress = ((float) done / duration) * 0.75;
159
160                         stretcher.study(buffers, this_read, pos == duration);
161                 }
162                 
163                 done = 0;
164                 pos = 0;
165
166                 while (pos < duration && !tsr.cancel) {
167                         
168                         nframes_t this_read = 0;
169                         
170                         for (uint32_t i = 0; i < channels; ++i) {
171                                 
172                                 this_read = 0;
173                                 nframes_t this_time;
174                                 
175                                 this_time = min(bufsize, duration - pos);
176                                 
177                                 this_read = region->master_read_at
178                                         (buffers[i],
179                                          buffers[i],
180                                          gain_buffer,
181                                          pos + region->position(),
182                                          this_time,
183                                          i);
184                                 
185                                 if (this_read != this_time) {
186                                         error << string_compose
187                                                 (_("tempoize: error reading data from %1"),
188                                                  nsrcs[i]->name()) << endmsg;
189                                         goto out;
190                                 }
191                         }
192
193                         pos += this_read;
194                         done += this_read;
195
196                         tsr.progress = 0.75 + ((float) done / duration) * 0.25;
197
198                         stretcher.process(buffers, this_read, pos == duration);
199
200                         int avail = 0;
201
202                         while ((avail = stretcher.available()) > 0) {
203
204                                 this_read = min(bufsize, uint32_t(avail));
205
206                                 stretcher.retrieve(buffers, this_read);
207                         
208                                 for (uint32_t i = 0; i < nsrcs.size(); ++i) {
209
210                                         if (nsrcs[i]->write(buffers[i], this_read) !=
211                                             this_read) {
212                                                 error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name()) << endmsg;
213                                                 goto out;
214                                         }
215                                 }
216                         }
217                 }
218
219                 while ((avail = stretcher.available()) >= 0) {
220
221                         uint32_t this_read = min(bufsize, uint32_t(avail));
222
223                         stretcher.retrieve(buffers, this_read);
224
225                         for (uint32_t i = 0; i < nsrcs.size(); ++i) {
226
227                                 if (nsrcs[i]->write(buffers[i], this_read) !=
228                                     this_read) {
229                                         error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name()) << endmsg;
230                                         goto out;
231                                 }
232                         }
233                 }
234
235         } catch (runtime_error& err) {
236                 error << _("timefx code failure. please notify ardour-developers.") << endmsg;
237                 error << err.what() << endmsg;
238                 goto out;
239         }
240
241         new_name = region->name();
242         at = new_name.find ('@');
243
244         // remove any existing stretch indicator
245
246         if (at != string::npos && at > 2) {
247                 new_name = new_name.substr (0, at - 1);
248         }
249
250         new_name += suffix;
251
252         ret = finish (region, nsrcs, new_name);
253
254         /* now reset ancestral data for each new region */
255
256         for (vector<boost::shared_ptr<AudioRegion> >::iterator x = results.begin(); x != results.end(); ++x) {
257                 nframes64_t astart = (*x)->ancestral_start();
258                 nframes64_t alength = (*x)->ancestral_length();
259                 nframes_t start;
260                 nframes_t length;
261
262                 // note: tsr.time_fraction is a percentage of original length. 100 = no change, 
263                 // 50 is half as long, 200 is twice as long, etc.
264                 
265                 
266                 float stretch = (*x)->stretch() * (tsr.time_fraction/100.0);
267                 float shift = (*x)->shift() * tsr.pitch_fraction;
268
269                 start = (nframes_t) floor (astart + ((astart - (*x)->start()) / stretch));
270                 length = (nframes_t) floor (alength / stretch);
271
272                 (*x)->set_ancestral_data (start, length, stretch, shift);
273         }
274
275   out:
276
277         if (gain_buffer) {
278                 delete [] gain_buffer;
279         }
280
281         if (buffers) {
282                 for (uint32_t i = 0; i < channels; ++i) {
283                         delete buffers[i];
284                 }
285                 delete [] buffers;
286         }
287
288         if (ret || tsr.cancel) {
289                 for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
290                         (*si)->mark_for_remove ();
291                 }
292         }
293         
294         tsr.done = true;
295
296         return ret;
297 }
298
299
300
301
302