better comments
[ardour.git] / libs / ardour / reverse.cc
1 /*
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>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <algorithm>
22
23 #include "ardour/audioregion.h"
24 #include "ardour/audiosource.h"
25 #include "ardour/reverse.h"
26 #include "ardour/types.h"
27
28 using namespace std;
29 using namespace ARDOUR;
30
31 namespace ARDOUR { class Progress; class Session; }
32
33 Reverse::Reverse (Session& s)
34         : Filter (s)
35 {
36 }
37
38 Reverse::~Reverse ()
39 {
40 }
41
42 int
43 Reverse::run (boost::shared_ptr<Region> r, Progress*)
44 {
45         SourceList nsrcs;
46         SourceList::iterator si;
47         samplecnt_t blocksize = 256 * 1024;
48         Sample* buf = 0;
49         samplepos_t fpos;
50         samplepos_t fstart;
51         samplecnt_t to_read;
52         int ret = -1;
53
54         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(r);
55         if (!region)
56                 return ret;
57
58         /* create new sources */
59
60         if (make_new_sources (region, nsrcs, "", false)) {
61                 goto out;
62         }
63
64         fstart = region->start();
65
66         if (blocksize > region->length()) {
67                 blocksize = region->length();
68         }
69
70         fpos = max (fstart, (fstart + region->length() - blocksize));
71
72         buf = new Sample[blocksize];
73         to_read = blocksize;
74
75         /* now read it backwards */
76
77         while (to_read) {
78
79                 uint32_t n;
80
81                 for (n = 0, si = nsrcs.begin(); n < region->n_channels(); ++n, ++si) {
82
83                         /* read it in directly from the source */
84
85                         if (region->audio_source (n)->read (buf, fpos, to_read) != to_read) {
86                                 goto out;
87                         }
88
89                         /* swap memory order */
90
91                         for (samplecnt_t i = 0; i < to_read/2; ++i) {
92                                 swap (buf[i],buf[to_read-1-i]);
93                         }
94
95                         /* write it out */
96
97                         boost::shared_ptr<AudioSource> asrc(boost::dynamic_pointer_cast<AudioSource>(*si));
98
99                         if (asrc && asrc->write (buf, to_read) != to_read) {
100                                 goto out;
101                         }
102                 }
103
104                 if (fpos > fstart + blocksize) {
105                         fpos -= to_read;
106                         to_read = blocksize;
107                 } else {
108                         to_read = fpos - fstart;
109                         fpos = fstart;
110                 }
111         };
112
113         ret = finish (region, nsrcs);
114
115   out:
116
117         delete [] buf;
118
119         if (ret) {
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 ();
123                 }
124         }
125
126         return ret;
127 }