aboutsummaryrefslogtreecommitdiffstats
path: root/lib/time.moon
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-02-16 11:47:24 +0000
committers-ol <s-ol@users.noreply.github.com>2020-02-16 11:47:24 +0000
commit2953f1e56b408fc26eb54fa65935505dd128ce82 (patch)
tree8c7c513ccf03df06c4e9e2e70ba68031d2d0b7a5 /lib/time.moon
parentadd ==, mod (diff)
downloadalive-2953f1e56b408fc26eb54fa65935505dd128ce82.tar.gz
alive-2953f1e56b408fc26eb54fa65935505dd128ce82.zip
major refactoring: Const, Stream + ResultNode
Diffstat (limited to 'lib/time.moon')
-rw-r--r--lib/time.moon57
1 files changed, 38 insertions, 19 deletions
diff --git a/lib/time.moon b/lib/time.moon
index 89f947e..7ce8b84 100644
--- a/lib/time.moon
+++ b/lib/time.moon
@@ -1,4 +1,4 @@
-import Const, Op from require 'core'
+import Stream, Const, Op from require 'core'
class lfo extends Op
@doc: "(lfo freq [wave]) - low-frequency oscillator
@@ -12,19 +12,19 @@ wave selects the wave shape from the following (default sin):
tau = math.pi * 2
new: (...) =>
super ...
+ print "creating LFO"
+ @out = Stream 'num'
@phase = 0
default_wave = Const 'str', 'sin'
setup: (@freq, @wave=default_wave) =>
assert @freq, "lfo requires a frequency value"
L\trace "setup #{@}, freq=#{@freq}, wave=#{@wave}"
+ @out
update: (dt) =>
- @freq\update dt
- @wave\update dt
-
- @phase += dt * @freq\get!
- @value = switch @wave\get!
+ @phase += dt * @freq\unwrap 'num'
+ @out\set switch @wave\unwrap!
when 'sin' then .5 + .5 * math.cos @phase * tau
when 'saw' then @phase % 1
when 'tri' then math.abs (2*@phase % 2) - 1
@@ -37,45 +37,64 @@ ramps from 0 to max (default same as ramp) once every period seconds"
new: (...) =>
super ...
+ @out = Stream 'num'
@phase = 0
+ @out
setup: (@period, @max) =>
assert @period, "tick requires a period value"
update: (dt) =>
- @period\update dt
- max = if @max
- @max\update dt
- @max\get!
- else
- @period\get!
-
- @phase += dt / @period\get!
+ period = @period\unwrap 'num'
+ max = if @max then @max\unwrap! else period
+ @phase += dt / period
if @phase >= 1
@phase -= 1
- @value = @phase * max
+ @out\set @phase * max
class tick extends Op
@doc: "(tick period) - count ticks
-counts upwards every period seconds and returns the number of completed ticks."
+counts upwards by one every period seconds and returns the number of completed ticks."
new: (...) =>
super ...
+ @out = Stream 'num'
@phase = 0
setup: (@period) =>
assert @period, "tick requires a period value"
+ @out
update: (dt) =>
- @period\update dt
+ @phase += dt / @period\unwrap 'num'
+ @out\set math.floor @phase
+
+class every extends Op
+ @doc: "(every period) - sometimes true
- @phase += dt / @period\get!
- @value = math.floor @phase
+returns true once every period seconds."
+ new: (...) =>
+ super ...
+ @out = Stream 'bool'
+ @phase = 0
+
+ setup: (@period) =>
+ assert @period, "every requires a period value"
+ @out
+
+ update: (dt) =>
+ @phase += dt / @period\unwrap 'num'
+ if @phase > 1
+ @phase -= 1
+ @out\set true
+ else
+ @out\set false
{
:lfo
:ramp
:tick
+ :every
}