Merged with trunk revision 600
[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 (AudioRegion& region)
47 {
48         AudioRegion::SourceList nsrcs;
49         AudioRegion::SourceList::iterator si;
50         const jack_nframes_t blocksize = 256 * 1048;
51         Sample buf[blocksize];
52         char * workbuf = 0;;
53         jack_nframes_t fpos;
54         jack_nframes_t fend;
55         jack_nframes_t fstart;
56         jack_nframes_t to_read;
57         int ret = -1;
58
59         /* create new sources */
60
61         if (make_new_sources (region, nsrcs)) {
62                 goto out;
63         }
64
65         workbuf = new char[blocksize * 4];
66         
67         fend = region.start() + region.length();
68         fstart = region.start();
69
70         if (blocksize < fend) {
71                 fpos =max(fstart, fend - blocksize);
72         } else {
73                 fpos = fstart;
74         }
75
76         to_read = min (region.length(), blocksize);
77
78         /* now read it backwards */
79
80         while (1) {
81
82                 uint32_t n;
83
84                 for (n = 0, si = nsrcs.begin(); n < region.n_channels(); ++n, ++si) {
85
86                         /* read it in */
87                         
88                         if (region.source (n).read (buf, fpos, to_read, workbuf) != to_read) {
89                                 goto out;
90                         }
91                         
92                         /* swap memory order */
93                         
94                         for (jack_nframes_t i = 0; i < to_read/2; ++i) {
95                                 swap (buf[i],buf[to_read-1-i]);
96                         }
97                         
98                         /* write it out */
99
100                         if ((*si)->write (buf, to_read, workbuf) != to_read) {
101                                 goto out;
102                         }
103                 }
104
105                 if (fpos == fstart) {
106                         break;
107                 } else if (fpos > fstart + to_read) {
108                         fpos -= to_read;
109                         to_read = min (fstart - fpos, blocksize);
110                 } else {
111                         to_read = fpos-fstart;
112                         fpos = fstart;
113                 }
114         };
115
116         ret = finish (region, nsrcs);
117
118   out:
119
120         if (ret) {
121                 for (si = nsrcs.begin(); si != nsrcs.end(); ++si) {
122                         (*si)->mark_for_remove ();
123                         delete *si;
124                 }
125         }
126         if (workbuf) {
127                 delete [] workbuf;
128         }
129         
130         return ret;
131 }