aboutsummaryrefslogtreecommitdiffstats
path: root/lib/time.moon
diff options
context:
space:
mode:
Diffstat (limited to 'lib/time.moon')
-rw-r--r--lib/time.moon40
1 files changed, 35 insertions, 5 deletions
diff --git a/lib/time.moon b/lib/time.moon
index dac5063..89f947e 100644
--- a/lib/time.moon
+++ b/lib/time.moon
@@ -16,6 +16,7 @@ wave selects the wave shape from the following (default sin):
default_wave = Const 'str', 'sin'
setup: (@freq, @wave=default_wave) =>
+ assert @freq, "lfo requires a frequency value"
L\trace "setup #{@}, freq=#{@freq}, wave=#{@wave}"
update: (dt) =>
@@ -29,23 +30,52 @@ wave selects the wave shape from the following (default sin):
when 'tri' then math.abs (2*@phase % 2) - 1
else error "unknown wave type"
+class ramp extends Op
+ @doc: "(ramp period [max]) - sawtooth lfo
+
+ramps from 0 to max (default same as ramp) once every period seconds"
+
+ new: (...) =>
+ super ...
+ @phase = 0
+
+ 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!
+
+ if @phase >= 1
+ @phase -= 1
+
+ @value = @phase * max
+
class tick extends Op
- @doc: "(tick freq) - count ticks
+ @doc: "(tick period) - count ticks
-counts upwards at freq and returns the number of completed ticks."
+counts upwards every period seconds and returns the number of completed ticks."
new: (...) =>
super ...
@phase = 0
- setup: (@freq) =>
+ setup: (@period) =>
+ assert @period, "tick requires a period value"
update: (dt) =>
- @freq\update dt
+ @period\update dt
- @phase += dt / @freq\get!
+ @phase += dt / @period\get!
@value = math.floor @phase
{
:lfo
+ :ramp
:tick
}