Merged with trunk R920.
[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     $Id$
19 */
20
21 #include <algorithm>
22
23 #include <pbd/basename.h>
24
25 #include <ardour/types.h>
26 #include <ardour/reverse.h>
27 #include <ardour/audiofilesource.h>
28 #include <ardour/session.h>
29 #include <ardour/audioregion.h>
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35
36 Reverse::Reverse (Session& s)
37         : AudioFilter (s)
38 {
39 }
40
41 Reverse::~Reverse ()
42 {
43 }
44
45 int
46 Reverse::run (boost::shared_ptr<AudioRegion> region)
47 {
48         SourceList nsrcs;
49         SourceList::iterator si;
50         const jack_nframes_t blocksize = 256 * 1048;
51         Sample buf[blocksize];
52         jack_nframes_t fpos;
53         jack_nframes_t fend;
54         jack_nframes_t fstart;
55         jack_nframes_t to_read;
56         int ret = -1;
57
58         /* create new sources */
59
60         if (make_new_sources (region, nsrcs)) {
61                 goto out;
62         }
63
64         fend = region->start() + region->length();
65         fstart = region->start();
66
67         if (blocksize < fend) {
68                 fpos =max(fstart, fend - blocksize);
69         } else {
70                 fpos = fstart;
71         }
72
73         to_read = min (region->length(), blocksize);
74
75         /* now read it backwards */
76
77         while (1) {
78
79                 uint32_t n;
80
81                 for (n = 0, si = nsrcs.begin(); n < region->n_channels(); ++n, ++si) {
82
83                         /* read it in */
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 (jack_nframes_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) {
105                         break;
106                 } else if (fpos > fstart + to_read) {
107                         fpos -= to_read;
108                         to_read = min (fstart - fpos, blocksize);
109                 } else {
110                         to_read = fpos-fstart;
111                         fpos = fstart;
112                 }
113         };
114
115         ret = finish (region, nsrcs);
116
117   out:
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 }