2 Copyright (C) 2004 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.
22 #include "ardour/audioregion.h"
23 #include "ardour/audiosource.h"
24 #include "ardour/reverse.h"
25 #include "ardour/types.h"
28 using namespace ARDOUR;
30 namespace ARDOUR { class Progress; class Session; }
32 Reverse::Reverse (Session& s)
42 Reverse::run (boost::shared_ptr<Region> r, Progress*)
45 SourceList::iterator si;
46 samplecnt_t blocksize = 256 * 1024;
53 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(r);
57 /* create new sources */
59 if (make_new_sources (region, nsrcs, "", false)) {
63 fstart = region->start();
65 if (blocksize > region->length()) {
66 blocksize = region->length();
69 fpos = max (fstart, (fstart + region->length() - blocksize));
71 buf = new Sample[blocksize];
74 /* now read it backwards */
80 for (n = 0, si = nsrcs.begin(); n < region->n_channels(); ++n, ++si) {
82 /* read it in directly from the source */
84 if (region->audio_source (n)->read (buf, fpos, to_read) != to_read) {
88 /* swap memory order */
90 for (samplecnt_t i = 0; i < to_read/2; ++i) {
91 swap (buf[i],buf[to_read-1-i]);
96 boost::shared_ptr<AudioSource> asrc(boost::dynamic_pointer_cast<AudioSource>(*si));
98 if (asrc && asrc->write (buf, to_read) != to_read) {
103 if (fpos > fstart + blocksize) {
107 to_read = fpos - fstart;
112 ret = finish (region, nsrcs);
119 for (si = nsrcs.begin(); si != nsrcs.end(); ++si) {
120 boost::shared_ptr<AudioSource> asrc(boost::dynamic_pointer_cast<AudioSource>(*si));
121 asrc->mark_for_remove ();