changeset 194:8148422e9102

Rename truncatedTo to withDuration and make it able to extend as well as truncate
author Chris Cannam
date Mon, 06 May 2013 15:03:22 +0100
parents bcd837523744
children 3f4f3af724b0
files yetilab/stream/filter.yeti yetilab/stream/test/test_filter.yeti yetilab/vamp/test/test_vamp.yeti
diffstat 3 files changed, 55 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/yetilab/stream/filter.yeti	Mon May 06 09:32:58 2013 +0100
+++ b/yetilab/stream/filter.yeti	Mon May 06 15:03:22 2013 +0100
@@ -24,25 +24,32 @@
         d2;
     esac;
 
-truncatedTo nsamples s = //!!! should nsamples be a time in seconds?
+withDuration nsamples s = //!!! should nsamples be a time in seconds? (no)
+   (var pos = 0;
     {
-        get position () = s.position,
+        get position () = pos,
         get channels () = s.channels, 
         get sampleRate () = s.sampleRate,
-        get available () = Known (nsamples - s.position),
-        get finished? () = not (nsamples > s.position),
+        get available () = Known (nsamples - pos),
+        get finished? () = not (nsamples > pos),
         read count =
-            if nsamples > s.position + count then
-                s.read count;
-            elif nsamples > s.position then
-                s.read (nsamples - s.position)
-            else
-                mat.zeroMatrix { columns = 0, rows = s.channels }
-            fi,
+           (n = min count (nsamples - pos);
+            pos := pos + n;
+            if not s.finished? then
+                fromStream = s.read n;
+                got = fromStream.size.columns;
+                mat.concat (Horizontal ())
+                   (fromStream ::
+                    if got == n then []
+                    else [mat.zeroMatrix { columns = n - got, rows = s.channels}]
+                    fi);
+            else 
+                mat.zeroMatrix { columns = n, rows = s.channels }
+            fi),
         close = s.close,
-    };
+    });
 
-delayedBy nsamples s = //!!! should nsamples be a time in seconds?
+delayedBy nsamples s = //!!! should nsamples be a time in seconds? (no)
    (var prepos = 0;
     delay = nsamples;
     zeros n = mat.toRowMajor
@@ -148,12 +155,12 @@
     fi;
 
 {
-    truncatedTo, 
+    withDuration, 
     delayedBy,
     multiplexed,
     repeated,
 //!!!} as {
-//    truncatedTo is number -> stream -> stream
+//    withDuration is number -> stream -> stream
 }
 
 
--- a/yetilab/stream/test/test_filter.yeti	Mon May 06 09:32:58 2013 +0100
+++ b/yetilab/stream/test/test_filter.yeti	Mon May 06 15:03:22 2013 +0100
@@ -9,12 +9,12 @@
 { compare, compareUsing } = load yetilab.test.test;
 
 makeTests name withUnknown =
-   (truncatedTo n str =
+   (withDuration n str =
         // Truncate a stream, but if withUnknown is true, return it
         // with availability Unknown -- so as to test that other
         // filter functions behave correctly even if availability is
         // not known on their underlying streams
-       (ts = filt.truncatedTo n str;
+       (ts = filt.withDuration n str;
         if withUnknown then
             ts with
             { 
@@ -32,10 +32,10 @@
 [
 
 "truncatedTo-\(name)": \(
-    // not using truncatedTo wrapper above, because we're actually
-    // testing filt.truncatedTo here rather than just generating a
+    // not using withDuration wrapper above, because we're actually
+    // testing filt.withDuration here rather than just generating a
     // stream for use in another test
-    str = filt.truncatedTo 3 (syn.generated 2 id);
+    str = filt.withDuration 3 (syn.generated 2 id);
     compare str.position 0 and
         compare str.channels 1 and
         compare str.sampleRate 2 and
@@ -48,12 +48,27 @@
         ( str.close (); true )
 ),
 
+"truncatedTo-b-\(name)": \(
+    // as above
+    str = filt.withDuration 3 (syn.generated 2 id);
+    compare str.position 0 and
+        compare (bl.list ((str.read 2).getRow 0)) [ 0,1 ] and
+        compare str.position 2 and
+        compare str.available (Known 1) and
+        compare str.finished? false and
+        compare (bl.list ((str.read 2).getRow 0)) [ 2 ] and
+        compare str.position 3 and
+        compare str.available (Known 0) and
+        compare str.finished? true and
+        ( str.close (); true )
+),
+
 "extendedTo-\(name)": \(
-    // not using truncatedTo wrapper above for the outer call, because
-    // we're actually testing filt.truncatedTo here rather than just
+    // not using withDuration wrapper above for the outer call, because
+    // we're actually testing filt.withDuration here rather than just
     // generating a stream for use in another test. The inner call
     // does use the wrapper.
-    str = filt.truncatedTo 5 (truncatedTo 3 (syn.generated 2 id));
+    str = filt.withDuration 5 (withDuration 3 (syn.generated 2 id));
     compare str.position 0 and
         compare str.channels 1 and
         compare str.sampleRate 2 and
@@ -63,7 +78,7 @@
         compare str.position 4 and
         compare str.available (Known 1) and
         compare str.finished? false and
-        compare (bl.list ((str.read 2).getRow 0)) [ 0,0 ] and
+        compare (bl.list ((str.read 2).getRow 0)) [ 0 ] and
         compare str.position 5 and
         compare str.available (Known 0) and
         compare str.finished? true and
@@ -71,7 +86,7 @@
 ),
 
 "delayedBy-0-3-\(name)": \(
-    str = filt.delayedBy 0 (truncatedTo 3 (syn.generated 2 id));
+    str = filt.delayedBy 0 (withDuration 3 (syn.generated 2 id));
     compare str.position 0 and
         compare str.channels 1 and
         compare str.sampleRate 2 and
@@ -99,7 +114,7 @@
 ),
 
 "delayedBy-2-3-\(name)": \(
-    str = filt.delayedBy 2 (truncatedTo 3 (syn.generated 2 id));
+    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
@@ -117,7 +132,7 @@
 ),
 
 "delayedBy-2-3b-\(name)": \(
-    str = filt.delayedBy 2 (truncatedTo 3 (syn.generated 2 id));
+    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
@@ -135,7 +150,7 @@
 ),
 
 "delayedBy-2-3c-\(name)": \(
-    str = filt.delayedBy 2 (truncatedTo 3 (syn.generated 2 id));
+    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
@@ -182,7 +197,7 @@
 ),
 
 "multiplexed-inf-trunc-\(name)": \(
-    str = filt.multiplexed [syn.generated 2 id, truncatedTo 3 (syn.generated 2 (0-))];
+    str = filt.multiplexed [syn.generated 2 id, withDuration 3 (syn.generated 2 (0-))];
     compare str.position 0 and
         compare str.channels 2 and
         compare str.sampleRate 2 and
@@ -198,7 +213,7 @@
 "multiplexed-precalc-trunc-\(name)": \(
     str = filt.multiplexed
        [syn.precalculated 2 (bl.fromList [1,2]),
-        truncatedTo 3 (syn.generated 2 (0-))];
+        withDuration 3 (syn.generated 2 (0-))];
     compare str.position 0 and
         compare str.channels 2 and
         compare str.sampleRate 2 and
@@ -215,7 +230,7 @@
     str = filt.multiplexed
        [syn.precalculated 2 (bl.fromList [1,2]),
         filt.multiplexed [syn.precalculated 2 (bl.fromList [3,4]),
-                          truncatedTo 3 (syn.generated 2 (0-))]];
+                          withDuration 3 (syn.generated 2 (0-))]];
     compare str.position 0 and
         compare str.channels 3 and
         compare str.sampleRate 2 and
@@ -232,7 +247,7 @@
     str = filt.multiplexed
        [syn.precalculated 2 (bl.fromList [1,2]),
         syn.precalculated 2 (bl.fromList [3,4]),
-        truncatedTo 3 (syn.generated 2 (0-))];
+        withDuration 3 (syn.generated 2 (0-))];
     compare str.position 0 and
         compare str.channels 3 and
         compare str.sampleRate 2 and
--- a/yetilab/vamp/test/test_vamp.yeti	Mon May 06 09:32:58 2013 +0100
+++ b/yetilab/vamp/test/test_vamp.yeti	Mon May 06 15:03:22 2013 +0100
@@ -12,7 +12,7 @@
 
 rate = 44100;
 
-testStream () = filter.truncatedTo (rate * 20) (synthetic.whiteNoise rate);
+testStream () = filter.withDuration (rate * 20) (synthetic.whiteNoise rate);
 
 processTest output = 
     v.processStreamStructured testPluginKey output (testStream ());