Merged with trunk R1705.
[ardour.git] / libs / ardour / reverse.cc
index 15f90fec457810c93375545fc11c5e3447bff09c..c7ebecea317029172a8d927a7cb005421861b3ab 100644 (file)
@@ -15,7 +15,6 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <algorithm>
@@ -47,12 +46,11 @@ Reverse::run (boost::shared_ptr<AudioRegion> region)
 {
        SourceList nsrcs;
        SourceList::iterator si;
-       const jack_nframes_t blocksize = 256 * 1048;
-       Sample buf[blocksize];
-       jack_nframes_t fpos;
-       jack_nframes_t fend;
-       jack_nframes_t fstart;
-       jack_nframes_t to_read;
+       nframes_t blocksize = 256 * 1024;
+       Sample* buf = 0;
+       nframes_t fpos;
+       nframes_t fstart;
+       nframes_t to_read;
        int ret = -1;
 
        /* create new sources */
@@ -61,20 +59,19 @@ Reverse::run (boost::shared_ptr<AudioRegion> region)
                goto out;
        }
 
-       fend = region->start() + region->length();
        fstart = region->start();
 
-       if (blocksize < fend) {
-               fpos =max(fstart, fend - blocksize);
-       } else {
-               fpos = fstart;
+       if (blocksize > region->length()) {
+               blocksize = region->length();
        }
 
-       to_read = min (region->length(), blocksize);
+       fpos = max (fstart, (fstart + region->length() - blocksize));
+       buf = new Sample[blocksize];
+       to_read = blocksize;
 
        /* now read it backwards */
 
-       while (1) {
+       while (to_read) {
 
                uint32_t n;
 
@@ -85,10 +82,10 @@ Reverse::run (boost::shared_ptr<AudioRegion> region)
                        if (region->audio_source (n)->read (buf, fpos, to_read) != to_read) {
                                goto out;
                        }
-                       
+
                        /* swap memory order */
                        
-                       for (jack_nframes_t i = 0; i < to_read/2; ++i) {
+                       for (nframes_t i = 0; i < to_read/2; ++i) {
                                swap (buf[i],buf[to_read-1-i]);
                        }
                        
@@ -101,13 +98,11 @@ Reverse::run (boost::shared_ptr<AudioRegion> region)
                        }
                }
 
-               if (fpos == fstart) {
-                       break;
-               } else if (fpos > fstart + to_read) {
+               if (fpos > fstart + blocksize) {
                        fpos -= to_read;
-                       to_read = min (fstart - fpos, blocksize);
+                       to_read = blocksize;
                } else {
-                       to_read = fpos-fstart;
+                       to_read = fpos - fstart;
                        fpos = fstart;
                }
        };
@@ -116,6 +111,10 @@ Reverse::run (boost::shared_ptr<AudioRegion> region)
 
   out:
 
+       if (buf) {
+               delete [] buf;
+       }
+
        if (ret) {
                for (si = nsrcs.begin(); si != nsrcs.end(); ++si) {
                        boost::shared_ptr<AudioSource> asrc(boost::dynamic_pointer_cast<AudioSource>(*si));