git.s-ol.nu alive / 2733d44
lib: add time/delay! s-ol 6 months ago
1 changed file(s) with 52 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
365365 delta = value - current
366366 @out\set current + delta * rate
367367
368 delay = Constant.meta
369 meta:
370 name: 'delay!'
371 summary: "Delay a !-stream event"
372 examples: { '(delay! [clock] delay evt)' }
373 description: "
374 Delays incoming `evt`s by `delay`.
375
376 - `clock` should be a `time/clock!` stream. This argument can be omitted
377 and the stream be passed as a dynamic definition in `*clock*` instead.
378 - `delay` should be a num= or num~ stream.
379 - `evt` is a !-stream."
380
381 value: class extends Op
382 new: (...) =>
383 super ...
384 @state = {}
385
386 pattern = -evt.clock + (sig.num / evt.num) + (sig! / evt!)
387 setup: (inputs, scope) =>
388 { clock, delay, evt } = pattern\match inputs
389
390 super
391 clock: Input.hot clock or scope\get '*clock*'
392 delay: Input.cold delay
393 evt: Input.hot evt
394
395 if @out and @out.type != @inputs.evt\type!
396 -- clear queue if type is not compatible
397 @state = {}
398
399 @out = @inputs.evt\type!\mk_evt!
400
401 tick: =>
402 clock = @inputs.clock!
403 if clock and #@state > 0
404 delta = clock.dt
405 for item in *@state
406 item.delay -= delta
407
408 if @state[1].delay < 0
409 item = table.remove @state, 1
410 @out\set item.value
411
412 if @inputs.evt\dirty!
413 value = @inputs.evt!
414 delay = @inputs.delay!
415 table.insert @state, { :delay, :value }
416
417
368418 RTNode
369419 children: { default_clock }
370420
382432 :every
383433 'val-seq': val_seq
384434 'bang-seq': bang_seq
435
385436 :smooth
437 :delay
386438
387439 '*clock*': default_clock