2 Copyright (C) 2004-2007 Paul Davis
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.
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.
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.
23 #include <pbd/error.h>
24 #include <rubberband/RubberBandStretcher.h>
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>
36 using namespace ARDOUR;
38 using namespace RubberBand;
40 Pitch::Pitch (Session& s, TimeFXRequest& req)
45 Stretch::Stretch (Session& s, TimeFXRequest& req)
50 RBEffect::RBEffect (Session& s, TimeFXRequest& req)
58 RBEffect::~RBEffect ()
63 RBEffect::run (boost::shared_ptr<AudioRegion> region)
68 const nframes_t bufsize = 256;
69 gain_t* gain_buffer = 0;
77 RubberBandStretcher stretcher (session.frame_rate(), region->n_channels(),
78 RubberBandStretcher::DefaultOptions,
79 tsr.time_fraction, tsr.pitch_fraction);
81 stretcher.setExpectedInputDuration(region->length());
82 stretcher.setDebugLevel(1);
87 uint32_t channels = region->n_channels();
88 nframes_t duration = region->length();
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
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));
99 snprintf (suffix, sizeof (suffix), "@%d-%d",
100 (int) floor (tsr.time_fraction * 100.0f),
101 (int) floor (tsr.pitch_fraction * 100.0f));
104 /* create new sources */
106 if (make_new_sources (region, nsrcs, suffix)) {
110 gain_buffer = new gain_t[bufsize];
111 buffers = new float *[channels];
113 for (uint32_t i = 0; i < channels; ++i) {
114 buffers[i] = new float[bufsize];
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. */
121 /* study first, process afterwards. */
128 while (pos < duration && !tsr.cancel) {
130 nframes_t this_read = 0;
132 for (uint32_t i = 0; i < channels; ++i) {
137 this_time = min(bufsize, duration - pos);
139 this_read = region->master_read_at
143 pos + region->position(),
147 if (this_read != this_time) {
148 error << string_compose
149 (_("tempoize: error reading data from %1"),
150 nsrcs[i]->name()) << endmsg;
158 tsr.progress = ((float) done / duration) * 0.75;
160 stretcher.study(buffers, this_read, pos == duration);
166 while (pos < duration && !tsr.cancel) {
168 nframes_t this_read = 0;
170 for (uint32_t i = 0; i < channels; ++i) {
175 this_time = min(bufsize, duration - pos);
177 this_read = region->master_read_at
181 pos + region->position(),
185 if (this_read != this_time) {
186 error << string_compose
187 (_("tempoize: error reading data from %1"),
188 nsrcs[i]->name()) << endmsg;
196 tsr.progress = 0.75 + ((float) done / duration) * 0.25;
198 stretcher.process(buffers, this_read, pos == duration);
202 while ((avail = stretcher.available()) > 0) {
204 this_read = min(bufsize, uint32_t(avail));
206 stretcher.retrieve(buffers, this_read);
208 for (uint32_t i = 0; i < nsrcs.size(); ++i) {
210 if (nsrcs[i]->write(buffers[i], this_read) !=
212 error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name()) << endmsg;
219 while ((avail = stretcher.available()) >= 0) {
221 uint32_t this_read = min(bufsize, uint32_t(avail));
223 stretcher.retrieve(buffers, this_read);
225 for (uint32_t i = 0; i < nsrcs.size(); ++i) {
227 if (nsrcs[i]->write(buffers[i], this_read) !=
229 error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name()) << endmsg;
235 } catch (runtime_error& err) {
236 error << _("timefx code failure. please notify ardour-developers.") << endmsg;
237 error << err.what() << endmsg;
241 new_name = region->name();
242 at = new_name.find ('@');
244 // remove any existing stretch indicator
246 if (at != string::npos && at > 2) {
247 new_name = new_name.substr (0, at - 1);
252 ret = finish (region, nsrcs, new_name);
254 /* now reset ancestral data for each new region */
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();
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.
266 float stretch = (*x)->stretch() * (tsr.time_fraction/100.0);
267 float shift = (*x)->shift() * tsr.pitch_fraction;
269 start = (nframes_t) floor (astart + ((astart - (*x)->start()) / stretch));
270 length = (nframes_t) floor (alength / stretch);
272 (*x)->set_ancestral_data (start, length, stretch, shift);
278 delete [] gain_buffer;
282 for (uint32_t i = 0; i < channels; ++i) {
288 if (ret || tsr.cancel) {
289 for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
290 (*si)->mark_for_remove ();