changeset 224:1cfa054e3077

Make delayedBy support negative delay
author Chris Cannam
date Sat, 11 May 2013 16:29:30 +0100
parents 51af10e6cd0d
children 8e04d298741b
files yetilab/stream/filter.yeti yetilab/stream/test/test_filter.yeti
diffstat 2 files changed, 49 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/yetilab/stream/filter.yeti	Sat May 11 16:00:58 2013 +0100
+++ b/yetilab/stream/filter.yeti	Sat May 11 16:29:30 2013 +0100
@@ -41,16 +41,23 @@
             fi),
     });
 
-delayedBy nsamples s = //!!! should nsamples be a time in seconds? (no)
-//!!! should allow nsamples < 0!
+delayedBy nsamples s = //!!! doc: nsamples may be -ve
    (var prepos = 0;
-    delay = nsamples;
     zeros n = mat.toRowMajor
        (prepos := prepos + n;
         mat.zeroMatrix { rows = s.channels, columns = n });
+    delay = 
+        if nsamples < 0 then
+            \0 (s.read (-nsamples));
+        else
+            nsamples
+        fi;
     {
         get position () = 
-            if prepos < delay then prepos else s.position + delay fi,
+            if prepos < delay then prepos
+            elif s.position + nsamples < 0 then 0
+            else s.position + nsamples
+            fi,
         get channels () = s.channels,
         get sampleRate () = s.sampleRate,
         get available () = 
--- a/yetilab/stream/test/test_filter.yeti	Sat May 11 16:00:58 2013 +0100
+++ b/yetilab/stream/test/test_filter.yeti	Sat May 11 16:29:30 2013 +0100
@@ -131,6 +131,31 @@
         ( str.close (); true )
 ),
 
+"delayedBy-m2-3-\(name)": \(
+    str = filt.delayedBy (-2) (withDuration 3 (syn.generated 2 id));
+    compare str.position 0 and
+        compare str.channels 1 and
+        compare str.sampleRate 2 and
+        compare str.available (maybeKnown 1) and
+        compare str.finished? false and
+        compare (vec.list (mat.getRow 0 (str.read 4))) [ 2 ] and
+        compare str.position 1 and
+        compare str.available (Known 0) and
+        compare str.finished? true and
+        ( str.close (); true )
+),
+
+"delayedBy-m4-3-\(name)": \(
+    str = filt.delayedBy (-4) (withDuration 3 (syn.generated 2 id));
+    compare str.position 0 and
+        compare str.channels 1 and
+        compare str.sampleRate 2 and
+        compare str.available (Known 0) and
+        compare str.finished? true and 
+        //!!! with this and others, need to check that we read an empty matrix after finished (perhaps have a helper function that checks finished properties such as available count as well)
+        ( str.close (); true )
+),
+
 "delayedBy-2-3b-\(name)": \(
     str = filt.delayedBy 2 (withDuration 3 (syn.generated 2 id));
     compare str.position 0 and
@@ -182,6 +207,19 @@
         ( str.close (); true )
 ),
 
+"delayedBy-m2-inf-\(name)": \(
+    str = filt.delayedBy (-2) (syn.generated 2 id);
+    compare str.position 0 and
+        compare str.channels 1 and
+        compare str.sampleRate 2 and
+        compare str.available (Infinite ()) and
+        compare str.finished? false and
+        compare (vec.list (mat.getRow 0 (str.read 2))) [ 2,3 ] and
+        compare str.position 2 and
+        compare str.finished? false and
+        ( str.close (); true )
+),
+
 "mixedTo-1-2-\(name)": \(
     str = filt.mixedTo 2 (withDuration 3 (syn.generated 2 id));
     compare str.position 0 and