aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--applause.lua33
1 files changed, 33 insertions, 0 deletions
diff --git a/applause.lua b/applause.lua
index a568a4a..d598a86 100644
--- a/applause.lua
+++ b/applause.lua
@@ -135,6 +135,10 @@ function Stream:sync()
return SyncedStream:new(self)
end
+function Stream:rep(repeats)
+ return RepeatStream:new(self, repeats)
+end
+
function Stream:map(fnc)
return MapStream:new(self, fnc)
end
@@ -641,6 +645,35 @@ function ConcatStream:len()
return len
end
+RepeatStream = DeriveClass(Stream, function(self, stream, repeats)
+ self.streams = {tostream(stream)}
+ self.repeats = repeats or math.huge
+end)
+
+function RepeatStream:tick()
+ local i = 1
+ local stream_tick = self.streams[1]:tick()
+ local repeats = self.repeats
+
+ return function()
+ while i <= repeats do
+ local sample = stream_tick()
+ if sample then return sample end
+
+ -- next iteration
+ i = i + 1
+ -- FIXME: The tick() method itself may be too
+ -- inefficient for realtime purposes.
+ -- Also, we may slowly leak memory.
+ stream_tick = self.streams[1]:tick()
+ end
+ end
+end
+
+function RepeatStream:len()
+ return self.streams[1]:len() * self.repeats
+end
+
-- Ravel operation inspired by APL.
-- This removes one level of nesting from nested streams
-- (e.g. streams of streams), and is semantically similar