summaryrefslogtreecommitdiff
path: root/libs/ardour/rb_effect.cc
blob: f1c0223c29e6b86a76d99e929a514a8d9a60b2ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
 * Copyright (C) 2008-2014 David Robillard <d@drobilla.net>
 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <algorithm>
#include <cmath>

#include <glibmm.h>

#include <rubberband/RubberBandStretcher.h>

#include "pbd/error.h"

#include "ardour/audioregion.h"
#include "ardour/audiosource.h"
#include "ardour/pitch.h"
#include "ardour/progress.h"
#include "ardour/session.h"
#include "ardour/stretch.h"
#include "ardour/types.h"

#include "pbd/i18n.h"

using namespace std;
using namespace ARDOUR;
using namespace PBD;
using namespace RubberBand;

Pitch::Pitch (Session& s, TimeFXRequest& req)
	: RBEffect (s, req)
{
}

RBStretch::RBStretch (Session& s, TimeFXRequest& req)
	: RBEffect (s, req)
{
}

RBEffect::RBEffect (Session& s, TimeFXRequest& req)
	: Filter (s)
	, tsr (req)

{
}

RBEffect::~RBEffect ()
{
}

int
RBEffect::run (boost::shared_ptr<Region> r, Progress* progress)
{
	boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);

	if (!region) {
		error << "RBEffect::run() passed a non-audio region! WTF?" << endmsg;
		return -1;
	}

	SourceList        nsrcs;
	int               ret         = -1;
	const samplecnt_t bufsize     = 8192;
	gain_t*           gain_buffer = 0;
	Sample**          buffers     = 0;
	char              suffix[32];
	string            new_name;
	string::size_type at;

#ifndef NDEBUG
	cerr << "RBEffect: source region: position = " << region->position ()
	     << ", start = " << region->start ()
	     << ", length = " << region->length ()
	     << ", ancestral_start = " << region->ancestral_start ()
	     << ", ancestral_length = " << region->ancestral_length ()
	     << ", stretch " << region->stretch ()
	     << ", shift " << region->shift () << endl;
#endif

	/*
	 * We have two cases to consider:
	 *
	 * 1. The region has not been stretched before.
	 *
	 * In this case, we just want to read region->length() samples
	 * from region->start().
	 *
	 * We will create a new region of region->length() *
	 * tsr.time_fraction samples.  The new region will have its
	 * start set to 0 (because it has a new audio file that begins
	 * at the start of the stretched area) and its ancestral_start
	 * set to region->start() (so that we know where to begin
	 * reading if we want to stretch it again).
	 *
	 * 2. The region has been stretched before.
	 *
	 * The region starts at region->start() samples into its
	 * (possibly previously stretched) source file.  But we don't
	 * want to read from its source file; we want to read from the
	 * file it was originally stretched from.
	 *
	 * The region's source begins at region->ancestral_start()
	 * samples into its master source file.  Thus, we need to start
	 * reading at region->ancestral_start() + (region->start() /
	 * region->stretch()) samples into the master source.  This
	 * value will also become the ancestral_start for the new
	 * region.
	 *
	 * We cannot use region->ancestral_length() to establish how
	 * many samples to read, because it won't be up to date if the
	 * region has been trimmed since it was last stretched.  We
	 * must read region->length() / region->stretch() samples and
	 * stretch them by tsr.time_fraction * region->stretch(), for
	 * a new region of region->length() * tsr.time_fraction
	 * samples.
	 *
	 * Case 1 is of course a special case of 2, where
	 * region->ancestral_start() == 0 and region->stretch() == 1.
	 *
	 * When we ask to read from a region, we supply a position on
	 * the global timeline.  The read function calculates the
	 * offset into the source as (position - region->position()) +
	 * region->start().  This calculation is used regardless of
	 * whether we are reading from a master or
	 * previously-stretched region.  In order to read from a point
	 * n samples into the master source, we need to provide n -
	 * region->start() + region->position() as our position
	 * argument to master_read_at().
	 *
	 * Note that region->ancestral_length() is not used.
	 *
	 * I hope this is clear.
	 */

	double stretch = region->stretch () * tsr.time_fraction;
	double shift   = region->shift () * tsr.pitch_fraction;

	samplecnt_t read_start = region->ancestral_start () +
	                         samplecnt_t (region->start () / (double)region->stretch ());

	samplecnt_t read_duration =
	    samplecnt_t (region->length () / (double)region->stretch ());

	uint32_t channels = region->n_channels ();

#ifndef NDEBUG
	cerr << "RBStretcher: input-len = " << read_duration
	     << ", rate = " << session.sample_rate ()
	     << ", channels = " << channels
	     << ", opts = " << tsr.opts
	     << ", stretch = " << stretch
	     << ", shift = " << shift << endl;
#endif

	RubberBandStretcher stretcher (session.sample_rate (), channels,
	                               (RubberBandStretcher::Options)tsr.opts,
	                               stretch, shift);

	progress->set_progress (0);
	tsr.done = false;

	stretcher.setDebugLevel (1);
	stretcher.setExpectedInputDuration (read_duration);

	/* the name doesn't need to be super-precise, but allow for 2 fractional
	 * digits just to disambiguate close but not identical FX
	 */

	if (stretch == 1.0) {
		snprintf (suffix, sizeof (suffix), "@%d", (int)floor (shift * 100.0f));
	} else if (shift == 1.0) {
		snprintf (suffix, sizeof (suffix), "@%d", (int)floor (stretch * 100.0f));
	} else {
		snprintf (suffix, sizeof (suffix), "@%d-%d",
		          (int)floor (stretch * 100.0f),
		          (int)floor (shift * 100.0f));
	}

	/* create new sources */

	samplepos_t pos = 0;

	if (make_new_sources (region, nsrcs, suffix)) {
		goto out;
	}

	gain_buffer = new gain_t[bufsize];
	buffers     = new float*[channels];

	for (uint32_t i = 0; i < channels; ++i) {
		buffers[i] = new float[bufsize];
	}

	/* we read from the master (original) sources for the region,
	 * not the ones currently in use, in case it's already been
	 * subject to timefx. */

	/* study first, process afterwards. */
	try {
		while (pos < read_duration && !tsr.cancel) {
			samplecnt_t this_read = 0;

			for (uint32_t i = 0; i < channels; ++i) {
				samplepos_t this_time = min (bufsize, read_duration - pos);

				samplepos_t this_position;
				this_position = read_start + pos -
				                region->start () + region->position ();

				this_read = region->master_read_at (buffers[i],
				                                    buffers[i],
				                                    gain_buffer,
				                                    this_position,
				                                    this_time,
				                                    i);

				if (this_read != this_time) {
					error << string_compose (_("tempoize: error reading data from %1 at %2 (wanted %3, got %4)"),
					                         region->name (), this_position, this_time, this_read)
					      << endmsg;
					goto out;
				}
			}

			pos += this_read;

			progress->set_progress (((float)pos / read_duration) * 0.25);

			stretcher.study (buffers, this_read, pos == read_duration);
		}

		/* done studing, start process */
		pos = 0;

		while (pos < read_duration && !tsr.cancel) {
			samplecnt_t this_read = 0;

			for (uint32_t i = 0; i < channels; ++i) {
				samplepos_t this_time;
				this_time = min (bufsize, read_duration - pos);
				this_time = min (this_time, (samplepos_t)stretcher.getSamplesRequired ());

				samplepos_t this_position;
				this_position = read_start + pos -
				                region->start () + region->position ();

				this_read = region->master_read_at (buffers[i],
				                                    buffers[i],
				                                    gain_buffer,
				                                    this_position,
				                                    this_time,
				                                    i);

				if (this_read != this_time) {
					error << string_compose (_("tempoize: error reading data from %1 at %2 (wanted %3, got %4)"),
					                         region->name (), pos + region->position (), this_time, this_read)
					      << endmsg;
					goto out;
				}
			}

			pos += this_read;

			progress->set_progress (0.25 + ((float)pos / read_duration) * 0.75);

			stretcher.process (buffers, this_read, pos == read_duration);

			samplecnt_t avail = 0;
			while ((avail = stretcher.available ()) > 0) {
				this_read = min (bufsize, avail);

				this_read = stretcher.retrieve (buffers, this_read);

				for (uint32_t i = 0; i < nsrcs.size (); ++i) {
					boost::shared_ptr<AudioSource> asrc = boost::dynamic_pointer_cast<AudioSource> (nsrcs[i]);
					if (!asrc) {
						continue;
					}

					if (asrc->write (buffers[i], this_read) != this_read) {
						error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name ()) << endmsg;
						goto out;
					}
				}
			}
		}

		/* completing */

		samplecnt_t avail = 0;
		while ((avail = stretcher.available ()) >= 0 && !tsr.cancel) {
			if (avail == 0) {
				/* wait for stretcher threads */
				Glib::usleep (10000);
				continue;
			}

			samplecnt_t this_read = min (bufsize, avail);

			this_read = stretcher.retrieve (buffers, this_read);

			for (uint32_t i = 0; i < nsrcs.size (); ++i) {
				boost::shared_ptr<AudioSource> asrc = boost::dynamic_pointer_cast<AudioSource> (nsrcs[i]);
				if (!asrc) {
					continue;
				}

				if (asrc->write (buffers[i], this_read) !=
				    this_read) {
					error << string_compose (_("error writing tempo-adjusted data to %1"), nsrcs[i]->name ()) << endmsg;
					goto out;
				}
			}
		}

	} catch (runtime_error& err) {
		error << string_compose (_("programming error: %1"), X_("timefx code failure")) << endmsg;
		error << err.what () << endmsg;
		goto out;
	}

	new_name = region->name ();
	at       = new_name.find ('@');

	/* remove any existing stretch indicator */

	if (at != string::npos && at > 2) {
		new_name = new_name.substr (0, at - 1);
	}

	new_name += suffix;

	if (!tsr.cancel) {
		ret = finish (region, nsrcs, new_name);
	}

	/* now reset ancestral data for each new region */

	for (vector<boost::shared_ptr<Region> >::iterator x = results.begin (); x != results.end (); ++x) {
		(*x)->set_ancestral_data (read_start,
		                          read_duration,
		                          stretch,
		                          shift);
		(*x)->set_master_sources (region->master_sources ());
		/* multiply the old (possibly previously stretched) region length by the extra
		 * stretch this time around to get its new length. this is a non-music based edit atm.
		 */
		(*x)->set_length ((*x)->length () * tsr.time_fraction, 0);
	}

	/* stretch region gain envelope */
	/* XXX: assuming we've only processed one input region into one result here */

	if (ret == 0 && tsr.time_fraction != 1) {
		boost::shared_ptr<AudioRegion> result = boost::dynamic_pointer_cast<AudioRegion> (results.front ());
		assert (result);
		result->envelope ()->x_scale (tsr.time_fraction);
	}

out:

	delete[] gain_buffer;

	if (buffers) {
		for (uint32_t i = 0; i < channels; ++i) {
			delete[] buffers[i];
		}
		delete[] buffers;
	}

	if (ret || tsr.cancel) {
		for (SourceList::iterator si = nsrcs.begin (); si != nsrcs.end (); ++si) {
			(*si)->mark_for_remove ();
		}
	}

	return ret;
}