aboutsummaryrefslogtreecommitdiffhomepage
path: root/applause.lua
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2015-04-20 23:16:07 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2015-04-20 23:16:07 +0200
commitbe7bbe30c4dc5f21cb43ec7475fb22ff403c9906 (patch)
treed1bf2eb75f8ff86e82cc863e4b39c4f6d9ec53cd /applause.lua
parenta001a9322f71ec5dc589884c8d6ad761c58dd03b (diff)
downloadapplause2-be7bbe30c4dc5f21cb43ec7475fb22ff403c9906.tar.gz
resample() and toplot() methods
Diffstat (limited to 'applause.lua')
-rw-r--r--applause.lua49
1 files changed, 49 insertions, 0 deletions
diff --git a/applause.lua b/applause.lua
index 1da9f10..4b204e2 100644
--- a/applause.lua
+++ b/applause.lua
@@ -125,6 +125,14 @@ function Stream:sub(i, j)
return SubStream:new(self, i, j)
end
+-- This is a linear resampler thanks to the
+-- semantics of __index
+function Stream:resample(factor)
+ -- FIXME: Mul should not make the stream infinite
+ local points = math.floor(self:len() * factor)
+ return self[iota(points) * Stream:new(1/factor):sub(1, points)]
+end
+
--
-- Wave forms with names derived from ChucK:
-- Can be written freq:SawOsc() or Stream.SawOsc(freq)
@@ -207,6 +215,47 @@ function Stream:play()
error("C function not registered!")
end
+function Stream:toplot(rows, cols)
+ rows = rows or 25
+ cols = cols or 80
+
+ -- FIXME: sub() should not be necessary since operations
+ -- should not make stream infinite
+ local scaled = ((self:resample(cols / self:len()) + 1)*(rows/2)):floor():sub(1, cols)()
+ local plot = {}
+
+ for i = 1, #scaled do
+ plot[i] = {}
+ for j = 1, rows do plot[i][j] = " " end
+
+ -- middle line (represents 0)
+ plot[i][math.ceil(rows/2)] = "-"
+
+ plot[i][scaled[i]] = "+" -- data point
+
+ -- connect with last data point
+ if i > 1 then
+ if scaled[i-1] < scaled[i] then
+ for j = scaled[i-1]+1, scaled[i]-1 do
+ plot[i][j] = "|"
+ end
+ elseif scaled[i-1] > scaled[i] then
+ for j = scaled[i-1]-1, scaled[i]+1, -1 do
+ plot[i][j] = "|"
+ end
+ end
+ end
+ end
+
+ local str = ""
+ for j = rows, 1, -1 do
+ for i = 1, cols do str = str..plot[i][j] end
+ str = str.."\n"
+ end
+
+ return str
+end
+
-- Stream metamethods
function Stream:__len() return self:len() end