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