# HG changeset patch # User Chris Cannam # Date 1367518148 -3600 # Node ID 344f0fefa00cc1eb42597fd83e0b3c9561b6f356 # Parent ec43e528464be717f129d4f9fe48b8fe0ab5694e Implement and test delayedBy diff -r ec43e528464b -r 344f0fefa00c yetilab/stream/filter.yeti --- a/yetilab/stream/filter.yeti Thu May 02 19:09:01 2013 +0100 +++ b/yetilab/stream/filter.yeti Thu May 02 19:09:08 2013 +0100 @@ -2,6 +2,7 @@ module yetilab.stream.filter; mat = load yetilab.matrix.matrix; +bl = load yetilab.block.block; load yetilab.stream.streamtype; @@ -41,6 +42,38 @@ close = s.close, }; +delayedBy nsamples s = //!!! should nsamples be a time in seconds? + (var prepos = 0; + delay = nsamples; + zeros n = + (prepos := prepos + n; + mat.zeroMatrix { rows = s.channels, columns = n }); + glueMatrices m1 m2 = + mat.newMatrix (RowMajor ()) + (map2 do row1 row2: bl.concat [ row1, row2 ] done + (mat.asRows m1) (mat.asRows m2)); + { + get position () = + if prepos < delay then prepos else s.position + delay fi, + get channels () = s.channels, + get sampleRate () = s.sampleRate, + get available () = + case s.available of + Known a: Known (a + delay - prepos); + other: other + esac, + get finished? () = (prepos >= delay) and s.finished?, + read count = + if prepos >= delay then s.read count + elif prepos + count < delay then zeros count + else + nleft = delay - prepos; + left = zeros nleft; + right = s.read (count - nleft); + glueMatrices left right; + fi, + close = s.close + }); //!!! not really mixed -- what's the word for it? one per channel mixed s1 s2 = //!!! could generalise to list of streams @@ -64,6 +97,7 @@ { truncatedTo, + delayedBy, mixed, //!!!} as { // truncatedTo is number -> stream -> stream diff -r ec43e528464b -r 344f0fefa00c yetilab/stream/test/test_filter.yeti --- a/yetilab/stream/test/test_filter.yeti Thu May 02 19:09:01 2013 +0100 +++ b/yetilab/stream/test/test_filter.yeti Thu May 02 19:09:08 2013 +0100 @@ -20,6 +20,91 @@ compare str.finished? false and compare (bl.list ((str.read 4).getRow 0)) [ 0, 0.5, 1 ] and compare str.position 3 and + compare str.available (Known 0) and + compare str.finished? true and + ( str.close (); true ) +), + +"delayedBy-0-3": \( + str = filt.delayedBy 0 (filt.truncatedTo 3 (syn.generated 2 id)); + compare str.position 0 and + compare str.channels 1 and + compare str.sampleRate 2 and + compare str.available (Known 3) and + compare str.finished? false and + compare (bl.list ((str.read 4).getRow 0)) [ 0, 0.5, 1 ] and + compare str.position 3 and + compare str.available (Known 0) and + compare str.finished? true and + ( str.close (); true ) +), + +"delayedBy-0-inf": \( + str = filt.delayedBy 0 (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 (bl.list ((str.read 4).getRow 0)) [ 0, 0.5, 1, 1.5 ] and + compare str.position 4 and + compare str.available (Infinite ()) and + compare str.finished? false and + ( str.close (); true ) +), + +"delayedBy-2-3": \( + str = filt.delayedBy 2 (filt.truncatedTo 3 (syn.generated 2 id)); + compare str.position 0 and + compare str.channels 1 and + compare str.sampleRate 2 and + compare str.available (Known 5) and + compare str.finished? false and + compare (bl.list ((str.read 4).getRow 0)) [ 0, 0, 0, 0.5 ] and + compare str.position 4 and + compare str.available (Known 1) and + compare str.finished? false and + compare (bl.list ((str.read 4).getRow 0)) [ 1 ] and + compare str.position 5 and + compare str.available (Known 0) and + compare str.finished? true and + ( str.close (); true ) +), + +"delayedBy-2-3b": \( + str = filt.delayedBy 2 (filt.truncatedTo 3 (syn.generated 2 id)); + compare str.position 0 and + compare str.channels 1 and + compare str.sampleRate 2 and + compare str.available (Known 5) and + compare str.finished? false and + compare (bl.list ((str.read 0).getRow 0)) [ ] and + compare str.position 0 and + compare str.available (Known 5) and + compare str.finished? false and + compare (bl.list ((str.read 5).getRow 0)) [ 0, 0, 0, 0.5, 1 ] and + compare str.position 5 and + compare str.available (Known 0) and + compare str.finished? true and + ( str.close (); true ) +), + +"delayedBy-2-inf": \( + 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 (bl.list ((str.read 2).getRow 0)) [ 0, 0 ] and + compare str.position 2 and + compare str.finished? false and + compare (bl.list ((str.read 2).getRow 0)) [ 0, 0.5 ] and + compare str.position 4 and + compare str.finished? false and + compare (bl.list ((str.read 2).getRow 0)) [ 1, 1.5 ] and + compare str.position 6 and + compare str.finished? false and ( str.close (); true ) ), diff -r ec43e528464b -r 344f0fefa00c yetilab/stream/test/test_framer.yeti --- a/yetilab/stream/test/test_framer.yeti Thu May 02 19:09:01 2013 +0100 +++ b/yetilab/stream/test/test_framer.yeti Thu May 02 19:09:08 2013 +0100 @@ -4,8 +4,11 @@ fr = load yetilab.stream.framer; block = load yetilab.block.block; mat = load yetilab.matrix.matrix; +syn = load yetilab.stream.syntheticstream; -{ compare, compareUsing, testStream } = load yetilab.test.test; +{ compare, compareUsing } = load yetilab.test.test; + +testStream n is number -> 'a = syn.precalculated 1000 (block.fromList [1..n]); compareFrames frames1 frames2 = all id (map2 do f1 f2: compareUsing mat.equal f1 f2 done frames1 diff -r ec43e528464b -r 344f0fefa00c yetilab/test/test.yeti --- a/yetilab/test/test.yeti Thu May 02 19:09:01 2013 +0100 +++ b/yetilab/test/test.yeti Thu May 02 19:09:08 2013 +0100 @@ -1,14 +1,14 @@ module yetilab.test.test; block = load yetilab.block.block; -ss = load yetilab.stream.syntheticstream; import yeti.lang: FailureException; -testStream n is number -> 'a = ss.precalculated 1000 (block.fromList [1..n]); +var goodCompares = 0; compareUsing comparator obtained expected = if comparator obtained expected then + goodCompares := goodCompares + 1; true; else println "** expected: \(expected)\n obtained: \(obtained)"; @@ -44,6 +44,7 @@ bad); { - testStream, compare, compareUsing, failedTests, runTests + compare, compareUsing, + runTests, }