2 * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
3 * Copyright (C) 2008-2010 Carl Hetherington <carl@carlh.net>
4 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "ardour/audioregion.h"
24 #include "ardour/audiosource.h"
25 #include "ardour/reverse.h"
26 #include "ardour/types.h"
29 using namespace ARDOUR;
31 namespace ARDOUR { class Progress; class Session; }
33 Reverse::Reverse (Session& s)
43 Reverse::run (boost::shared_ptr<Region> r, Progress*)
46 SourceList::iterator si;
47 samplecnt_t blocksize = 256 * 1024;
54 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(r);
58 /* create new sources */
60 if (make_new_sources (region, nsrcs, "", false)) {
64 fstart = region->start();
66 if (blocksize > region->length()) {
67 blocksize = region->length();
70 fpos = max (fstart, (fstart + region->length() - blocksize));
72 buf = new Sample[blocksize];
75 /* now read it backwards */
81 for (n = 0, si = nsrcs.begin(); n < region->n_channels(); ++n, ++si) {
83 /* read it in directly from the source */
85 if (region->audio_source (n)->read (buf, fpos, to_read) != to_read) {
89 /* swap memory order */
91 for (samplecnt_t i = 0; i < to_read/2; ++i) {
92 swap (buf[i],buf[to_read-1-i]);
97 boost::shared_ptr<AudioSource> asrc(boost::dynamic_pointer_cast<AudioSource>(*si));
99 if (asrc && asrc->write (buf, to_read) != to_read) {
104 if (fpos > fstart + blocksize) {
108 to_read = fpos - fstart;
113 ret = finish (region, nsrcs);
120 for (si = nsrcs.begin(); si != nsrcs.end(); ++si) {
121 boost::shared_ptr<AudioSource> asrc(boost::dynamic_pointer_cast<AudioSource>(*si));
122 asrc->mark_for_remove ();