diff options
| author | s-ol <s+removethis@s-ol.nu> | 2021-11-11 15:14:56 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-02 14:24:49 +0000 |
| commit | 50221c49db12070d1cc428244ae152526f9cbe4c (patch) | |
| tree | 1e8aa675865fd38be11bcc888e831c72c8314b18 | |
| parent | allow top-level input in Op.inputs (diff) | |
| download | alive-50221c49db12070d1cc428244ae152526f9cbe4c.tar.gz alive-50221c49db12070d1cc428244ae152526f9cbe4c.zip | |
lib: add time/smooth
| -rw-r--r-- | alv-lib/time.moon | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/alv-lib/time.moon b/alv-lib/time.moon index f0dcefa..49b193a 100644 --- a/alv-lib/time.moon +++ b/alv-lib/time.moon @@ -328,11 +328,44 @@ Emits `bang!`s with delays `delay0`, `delay1`, … in between. else break - if bang - @out - vis: => step: @state.i\set true +smooth = Constant.meta + meta: + name: 'smooth' + summary: "Smooth out value transitions" + examples: { '(smooth [clock] value rate)' } + description: " +Creates smooth transitions when `value` changes. + +- `clock` should be a `time/clock!` stream. This argument can be omitted + and the stream be passed as a dynamic definition in `*clock*` instead. +- `value` should be a num~ or num! stream. +- `rate` is a num~ or num= value." + + value: class extends Op + new: (...) => + super ... + @out = T.num\mk_sig! + + pattern = -evt.clock + sig.num + (sig.num / evt.num) + setup: (inputs, scope) => + { clock, rate, value } = pattern\match inputs + + super + clock: Input.hot clock or scope\get '*clock*' + rate: Input.cold rate + value: Input.cold value + + @out\set @inputs.value! unless @.out! + + tick: => + { :clock, :rate, :value } = @unwrap_all! + if clock + current = @.out! + delta = value - current + @out\set current + delta * rate + RTNode children: { default_clock } @@ -350,5 +383,6 @@ RTNode :every 'val-seq': val_seq 'bang-seq': bang_seq + :smooth '*clock*': default_clock |
