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 double this_time_fraction = tsr.time_fraction * region->stretch ();
78 double this_pitch_fraction = tsr.pitch_fraction * region->shift ();
80 RubberBandStretcher stretcher (session.frame_rate(), region->n_channels(),
81 (RubberBandStretcher::Options) tsr.opts,
82 this_time_fraction, this_pitch_fraction);
84 stretcher.setExpectedInputDuration(region->length());
85 stretcher.setDebugLevel(1);
90 uint32_t channels = region->n_channels();
91 nframes_t duration = region->length();
93 /* the name doesn't need to be super-precise, but allow for 2 fractional
94 digits just to disambiguate close but not identical FX
97 if (this_time_fraction == 1.0) {
98 snprintf (suffix, sizeof (suffix), "@%d", (int) floor (this_pitch_fraction * 100.0f));
99 } else if (this_pitch_fraction == 1.0) {
100 snprintf (suffix, sizeof (suffix), "@%d", (int) floor (this_time_fraction * 100.0f));
102 snprintf (suffix, sizeof (suffix), "@%d-%d",
103 (int) floor (this_time_fraction * 100.0f),
104 (int) floor (this_pitch_fraction * 100.0f));
107 /* create new sources */
109 if (make_new_sources (region, nsrcs, suffix)) {
113 gain_buffer = new gain_t[bufsize];
114 buffers = new float *[channels];
116 for (uint32_t i = 0; i < channels; ++i) {
117 buffers[i] = new float[bufsize];
120 /* we read from the master (original) sources for the region,
121 not the ones currently in use, in case it's already been
122 subject to timefx. */
124 /* study first, process afterwards. */
131 while (pos < duration && !tsr.cancel) {
133 nframes_t this_read = 0;
135 for (uint32_t i = 0; i < channels; ++i) {
140 this_time = min(bufsize, duration - pos);
142 this_read = region->master_read_at
146 pos + region->position(),
150 if (this_read != this_time) {
151 error << string_compose
152 (_("tempoize: error reading data from %1"),
153 nsrcs[i]->name()) << endmsg;
161 tsr.progress = ((float) done / duration) * 0.25;
163 stretcher.study(buffers, this_read, pos == duration);
169 while (pos < duration && !tsr.cancel) {
171 nframes_t this_read = 0;
173 for (uint32_t i = 0; i < channels; ++i) {
178 this_time = min(bufsize, duration - pos);
180 this_read = region->master_read_at
184 pos + region->position(),
188 if (this_read != this_time) {
189 error << string_compose
190 (_("tempoize: error reading data from %1"),
191 nsrcs[i]->name()) << endmsg;
199 tsr.progress = 0.25 + ((float) done / duration) * 0.75;
201 stretcher.process(buffers, this_read, pos == duration);
205 while ((avail = stretcher.available()) > 0) {
207 this_read = min(bufsize, uint32_t(avail));
209 stretcher.retrieve(buffers, this_read);
211 for (uint32_t i = 0; i < nsrcs.size(); ++i) {
213 if (nsrcs[i]->write(buffers[i], this_read) !=
215 error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name()) << endmsg;
222 while ((avail = stretcher.available()) >= 0) {
224 uint32_t this_read = min(bufsize, uint32_t(avail));
226 stretcher.retrieve(buffers, this_read);
228 for (uint32_t i = 0; i < nsrcs.size(); ++i) {
230 if (nsrcs[i]->write(buffers[i], this_read) !=
232 error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name()) << endmsg;
238 } catch (runtime_error& err) {
239 error << _("timefx code failure. please notify ardour-developers.") << endmsg;
240 error << err.what() << endmsg;
244 new_name = region->name();
245 at = new_name.find ('@');
247 // remove any existing stretch indicator
249 if (at != string::npos && at > 2) {
250 new_name = new_name.substr (0, at - 1);
255 ret = finish (region, nsrcs, new_name);
257 /* now reset ancestral data for each new region */
259 for (vector<boost::shared_ptr<AudioRegion> >::iterator x = results.begin(); x != results.end(); ++x) {
260 nframes64_t astart = (*x)->ancestral_start();
261 nframes64_t alength = (*x)->ancestral_length();
265 // note: this_time_fraction is a ratio of original length. 1.0 = no change,
266 // 0.5 is half as long, 2.0 is twice as long, etc.
268 float stretch = (*x)->stretch() * (tsr.time_fraction/100.0);
269 float shift = (*x)->shift() * tsr.pitch_fraction;
271 start = (nframes_t) floor (astart + ((astart - (*x)->start()) / stretch));
272 length = (nframes_t) floor (alength / stretch);
274 (*x)->set_ancestral_data (start, length, stretch, shift);
280 delete [] gain_buffer;
284 for (uint32_t i = 0; i < channels; ++i) {
290 if (ret || tsr.cancel) {
291 for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
292 (*si)->mark_for_remove ();