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 // note: this_time_fraction is a ratio of original length. 1.0 = no change,
78 // 0.5 is half as long, 2.0 is twice as long, etc.
80 double this_time_fraction = tsr.time_fraction * region->stretch ();
81 double this_pitch_fraction = tsr.pitch_fraction * region->shift ();
83 RubberBandStretcher stretcher (session.frame_rate(), region->n_channels(),
84 (RubberBandStretcher::Options) tsr.opts,
85 this_time_fraction, this_pitch_fraction);
90 uint32_t channels = region->n_channels();
91 nframes_t duration = region->ancestral_length();
93 stretcher.setExpectedInputDuration(duration);
94 stretcher.setDebugLevel(1);
96 /* the name doesn't need to be super-precise, but allow for 2 fractional
97 digits just to disambiguate close but not identical FX
100 if (this_time_fraction == 1.0) {
101 snprintf (suffix, sizeof (suffix), "@%d", (int) floor (this_pitch_fraction * 100.0f));
102 } else if (this_pitch_fraction == 1.0) {
103 snprintf (suffix, sizeof (suffix), "@%d", (int) floor (this_time_fraction * 100.0f));
105 snprintf (suffix, sizeof (suffix), "@%d-%d",
106 (int) floor (this_time_fraction * 100.0f),
107 (int) floor (this_pitch_fraction * 100.0f));
110 /* create new sources */
112 if (make_new_sources (region, nsrcs, suffix)) {
116 gain_buffer = new gain_t[bufsize];
117 buffers = new float *[channels];
119 for (uint32_t i = 0; i < channels; ++i) {
120 buffers[i] = new float[bufsize];
123 /* we read from the master (original) sources for the region,
124 not the ones currently in use, in case it's already been
125 subject to timefx. */
127 /* study first, process afterwards. */
134 while (pos < duration && !tsr.cancel) {
136 nframes_t this_read = 0;
138 for (uint32_t i = 0; i < channels; ++i) {
143 this_time = min(bufsize, duration - pos);
145 this_read = region->master_read_at
149 pos + region->position(),
153 if (this_read != this_time) {
154 error << string_compose
155 (_("tempoize: error reading data from %1 at %2 (wanted %3, got %4)"),
156 region->name(), pos + region->position(), this_time, this_read) << endmsg;
164 tsr.progress = ((float) done / duration) * 0.25;
166 stretcher.study(buffers, this_read, pos == duration);
172 while (pos < duration && !tsr.cancel) {
174 nframes_t this_read = 0;
176 for (uint32_t i = 0; i < channels; ++i) {
181 this_time = min(bufsize, duration - pos);
183 this_read = region->master_read_at
187 pos + region->position(),
191 if (this_read != this_time) {
192 error << string_compose
193 (_("tempoize: error reading data from %1 at %2 (wanted %3, got %4)"),
194 region->name(), pos + region->position(), this_time, this_read) << endmsg;
202 tsr.progress = 0.25 + ((float) done / duration) * 0.75;
204 stretcher.process(buffers, this_read, pos == duration);
208 while ((avail = stretcher.available()) > 0) {
210 this_read = min(bufsize, uint32_t(avail));
212 stretcher.retrieve(buffers, this_read);
214 for (uint32_t i = 0; i < nsrcs.size(); ++i) {
216 if (nsrcs[i]->write(buffers[i], this_read) !=
218 error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name()) << endmsg;
225 while ((avail = stretcher.available()) >= 0) {
227 uint32_t this_read = min(bufsize, uint32_t(avail));
229 stretcher.retrieve(buffers, this_read);
231 for (uint32_t i = 0; i < nsrcs.size(); ++i) {
233 if (nsrcs[i]->write(buffers[i], this_read) !=
235 error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name()) << endmsg;
241 } catch (runtime_error& err) {
242 error << _("timefx code failure. please notify ardour-developers.") << endmsg;
243 error << err.what() << endmsg;
247 new_name = region->name();
248 at = new_name.find ('@');
250 // remove any existing stretch indicator
252 if (at != string::npos && at > 2) {
253 new_name = new_name.substr (0, at - 1);
258 ret = finish (region, nsrcs, new_name);
260 /* now reset ancestral data for each new region */
262 for (vector<boost::shared_ptr<AudioRegion> >::iterator x = results.begin(); x != results.end(); ++x) {
264 (*x)->set_ancestral_data (region->ancestral_start(),
265 region->ancestral_length(),
267 this_pitch_fraction );
268 (*x)->set_master_sources (region->get_master_sources());
274 delete [] gain_buffer;
278 for (uint32_t i = 0; i < channels; ++i) {
284 if (ret || tsr.cancel) {
285 for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
286 (*si)->mark_for_remove ();