changeset 180:a5417e489e2b

Generalise multiplexed to any number of streams
author Chris Cannam
date Thu, 02 May 2013 22:38:16 +0100
parents a1069ed26740
children ce21d31e5a64
files yetilab/stream/filter.yeti yetilab/stream/test/test_filter.yeti
diffstat 2 files changed, 24 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/yetilab/stream/filter.yeti	Thu May 02 22:17:49 2013 +0100
+++ b/yetilab/stream/filter.yeti	Thu May 02 22:38:16 2013 +0100
@@ -71,30 +71,29 @@
         close = s.close
     });
 
-//!!! not really mixed -- what's the word for it? one per channel
-mixed s1 s2 = //!!! could generalise to list of streams
+multiplexed streams = 
     {
-        get position () = min s1.position s2.position, // can differ after EOS
-        get channels () = s1.channels + s2.channels,
-        get sampleRate () = s1.sampleRate,
-        get available () = minDurationOf s1.available s2.available,
-        get finished? () = s1.finished? or s2.finished?,
+        get position () = head (sort (map (.position) streams)), // can differ after EOS
+        get channels () = sum (map (.channels) streams),
+        get sampleRate () = (head streams).sampleRate,
+        get available () = 
+            fold do dur s: minDurationOf dur s.available done (Infinite ()) streams,
+        get finished? () = any id (map (.finished?) streams),
         read count =
-           (outs = map do s: s.read count done [ s1, s2 ];
+           (outs = map do s: s.read count done streams;
             minlen = head (sort (map do m: m.size.columns done outs));
             outs = map do m:
                 mat.resizedTo { rows = m.size.rows, columns = minlen } m
                 done outs;
-            mat.newMatrix (RowMajor ())
-                (concat (map mat.asRows outs))
+            mat.concat (Vertical ()) outs
             ),
-        close () = (s1.close (); s2.close ())
+        close () = for streams do s: s.close() done,
     };
 
 {
     truncatedTo, 
     delayedBy,
-    mixed,
+    multiplexed,
 //!!!} as {
 //    truncatedTo is number -> stream -> stream
 }
--- a/yetilab/stream/test/test_filter.yeti	Thu May 02 22:17:49 2013 +0100
+++ b/yetilab/stream/test/test_filter.yeti	Thu May 02 22:38:16 2013 +0100
@@ -108,8 +108,8 @@
         ( str.close (); true )
 ),
 
-"mixed-inf-inf": \(
-    str = filt.mixed (syn.generated 2 id) (syn.generated 2 (0-));
+"multiplexed-inf-inf": \(
+    str = filt.multiplexed [syn.generated 2 id, syn.generated 2 (0-)];
     compare str.position 0 and
         compare str.channels 2 and
         compare str.sampleRate 2 and
@@ -122,8 +122,8 @@
         ( str.close (); true )
 ),
 
-"mixed-inf-trunc": \(
-    str = filt.mixed (syn.generated 2 id) (filt.truncatedTo 3 (syn.generated 2 (0-)));
+"multiplexed-inf-trunc": \(
+    str = filt.multiplexed [syn.generated 2 id, filt.truncatedTo 3 (syn.generated 2 (0-))];
     compare str.position 0 and
         compare str.channels 2 and
         compare str.sampleRate 2 and
@@ -137,9 +137,10 @@
         ( str.close (); true )
 ),
 
-"mixed-precalc-trunc": \(
-    str = filt.mixed (syn.precalculated 2 (bl.fromList [1,2]))
-       (filt.truncatedTo 3 (syn.generated 2 (0-)));
+"multiplexed-precalc-trunc": \(
+    str = filt.multiplexed
+       [syn.precalculated 2 (bl.fromList [1,2]),
+        filt.truncatedTo 3 (syn.generated 2 (0-))];
     compare str.position 0 and
         compare str.channels 2 and
         compare str.sampleRate 2 and
@@ -152,10 +153,11 @@
         ( str.close (); true )
 ),
 
-"mixed-2-1": \(
-    str = filt.mixed (syn.precalculated 2 (bl.fromList [1,2]))
-       (filt.mixed (syn.precalculated 2 (bl.fromList [3,4]))
-           (filt.truncatedTo 3 (syn.generated 2 (0-))));
+"multiplexed-2-1": \(
+    str = filt.multiplexed
+       [syn.precalculated 2 (bl.fromList [1,2]),
+        filt.multiplexed [syn.precalculated 2 (bl.fromList [3,4]),
+                          filt.truncatedTo 3 (syn.generated 2 (0-))]];
     compare str.position 0 and
         compare str.channels 3 and
         compare str.sampleRate 2 and