lib: add time/smooth
s-ol
6 months ago
327 | 327 | else |
328 | 328 | break |
329 | 329 | |
330 | if bang | |
331 | @out | |
332 | ||
333 | 330 | vis: => step: @state.i\set true |
331 | ||
332 | smooth = Constant.meta | |
333 | meta: | |
334 | name: 'smooth' | |
335 | summary: "Smooth out value transitions" | |
336 | examples: { '(smooth [clock] value rate)' } | |
337 | description: " | |
338 | Creates smooth transitions when `value` changes. | |
339 | ||
340 | - `clock` should be a `time/clock!` stream. This argument can be omitted | |
341 | and the stream be passed as a dynamic definition in `*clock*` instead. | |
342 | - `value` should be a num~ or num! stream. | |
343 | - `rate` is a num~ or num= value." | |
344 | ||
345 | value: class extends Op | |
346 | new: (...) => | |
347 | super ... | |
348 | @out = T.num\mk_sig! | |
349 | ||
350 | pattern = -evt.clock + sig.num + (sig.num / evt.num) | |
351 | setup: (inputs, scope) => | |
352 | { clock, rate, value } = pattern\match inputs | |
353 | ||
354 | super | |
355 | clock: Input.hot clock or scope\get '*clock*' | |
356 | rate: Input.cold rate | |
357 | value: Input.cold value | |
358 | ||
359 | @out\set @inputs.value! unless @.out! | |
360 | ||
361 | tick: => | |
362 | { :clock, :rate, :value } = @unwrap_all! | |
363 | if clock | |
364 | current = @.out! | |
365 | delta = value - current | |
366 | @out\set current + delta * rate | |
334 | 367 | |
335 | 368 | RTNode |
336 | 369 | children: { default_clock } |
349 | 382 | :every |
350 | 383 | 'val-seq': val_seq |
351 | 384 | 'bang-seq': bang_seq |
385 | :smooth | |
352 | 386 | |
353 | 387 | '*clock*': default_clock |