1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
import Constant, Error, Op, Input, T, sig, evt, any from require 'alv.base'
import RTNode from require 'alv'
import monotime from require 'system'
clock = Constant.meta
meta:
name: 'clock'
summary: "Create a clock source."
examples: { '(clock)', '(clock fps)' }
description: "Creates a new `time/clock!` stream.
The clock event stream is an IO that triggers other operators at a fixed
frame rate.
- `fps` has to be a num= constant and defaults to `60` if omitted."
value: class extends Op
setup: (inputs) =>
fps = (-sig.num)\match inputs
super
fps: Input.cold fps or Constant.num 60
io: Input.hot T.bang\mk_evt!
@setup_out '!', T.clock
poll: =>
time = monotime!
@state or= time
dt = time - @state
ft = 1 / @inputs.fps!
if dt >= ft and not @inputs.io.result\dirty!
@inputs.io.result\set true
true
tick: =>
time = monotime!
dt = time - @state
@state = time
@out\set :time, :dt
default_clock = do
op = clock!!
op\setup {}
op.out.meta =
name: '*clock*'
summary: 'Default clock source (60fps).'
RTNode :op, result: op.out
scale_time = Constant.meta
meta:
name: 'scale-time'
summary: "Scale clock time."
examples: { '(scale-time [clock] scale)' }
description: "Creates a new clock event stream scaled by `scale`.
- `clock` should be a `time/clock!` stream. This argument can be omitted
and the stream be passed as a dynamic definition in `*clock*` instead.
- `scale` should be a num~ stream."
value: class extends Op
pattern = -evt.clock + sig.num + -sig.str
setup: (inputs, scope) =>
{ clock, scale } = pattern\match inputs
super
clock: Input.hot clock or scope\get '*clock*'
scale: Input.cold scale
@setup_out '!', T.clock
tick: =>
{ :clock, :scale } = @unwrap_all!
@out\set {k, v*scale for k,v in pairs clock}
lfo = Constant.meta
meta:
name: 'lfo'
summary: "Low-frequency oscillator."
examples: { '(lfo [clock] freq [wave])' }
description: "Oscillates betwen `0` and `1` at the frequency `freq`.
- `clock` should be a `time/clock!` stream. This argument can be omitted
and the stream be passed as a dynamic definition in `*clock*` instead.
- `freq` should be a num~ stream.
- `wave` selects the wave shape from one of the following:
- `'sin'` (the default)
- `'saw'`
- `'tri'`"
value: class extends Op
default_wave = Constant.str 'sin'
pattern = -evt.clock + sig.num + -sig.str
setup: (inputs, scope) =>
{ clock, freq, wave } = pattern\match inputs
super
clock: Input.hot clock or scope\get '*clock*'
freq: Input.cold freq
wave: Input.hot wave or default_wave
@state or= 0
@setup_out '~', T.num
tau = math.pi * 2
tick: =>
if tick = @inputs.clock!
@state += tick.dt * @inputs.freq!
@out\set switch @inputs.wave!
when 'sin' then .5 + .5 * math.cos @state * tau
when 'saw' then @state % 1
when 'tri' then math.abs (2*@state % 2) - 1
else error Error 'argument', "unknown wave type '#{wave}'"
vis: =>
{
type: 'bar'
bar: @.out!
}
ramp = Constant.meta
meta:
name: 'ramp'
summary: "Sawtooth LFO."
examples: { '(ramp [clock] period [max])' }
description: "Ramps from `0` to `max` once every `period` seconds.
- `clock` should be a `time/clock!` stream. This argument can be omitted
and the stream be passed as a dynamic definition in `*clock*` instead.
- `period` should be a num~ stream.
- `max` should be a num~ stream and defaults to `period` if omitted."
value: class extends Op
pattern = -evt.clock + sig.num + -sig.num
setup: (inputs, scope) =>
{ clock, period, max } = pattern\match inputs
super
clock: Input.hot clock or scope\get '*clock*'
period: Input.cold period
max: max and Input.cold max
@state or= 0
@setup_out '~', T.num, 0
tick: =>
{ :clock, :period, :max } = @unwrap_all!
max or= period
return if period == 0
@state += clock.dt / period
while @state >= 1
@state -= 1
@out\set @state * max
vis: =>
{
type: 'bar'
bar: @state
}
tick = Constant.meta
meta:
name: 'tick'
summary: "Count ticks."
examples: { '(tick [clock] period)' }
description: "Counts upwards by one every `period` seconds.
- `clock` should be a `time/clock!` stream. This argument can be omitted
and the stream be passed as a dynamic definition in `*clock*` instead.
- `period` should be a num~ stream.
- returns a `num~` stream that increases by 1 every `period`."
value: class extends Op
pattern = -evt.clock + sig.num
setup: (inputs, scope) =>
{ clock, period } = pattern\match inputs
super
clock: Input.hot clock or scope\get '*clock*'
period: Input.cold period
@state or= { phase: 0, count: 0 }
@setup_out '~', T.num, @state.count
tick: =>
{ :clock, :period } = @unwrap_all!
return if period == 0
@state.phase += clock.dt / period
if @state.phase >= 1
@state.phase -= 1
@state.count += 1
@out\set @state.count
vis: => type: 'event'
every = Constant.meta
meta:
name: 'every'
summary: "Emit events regularly."
examples: { '(every [clock] period [evt])' }
description: "Emits `evt` as an event once every `period` seconds.
- `clock` should be a `time/clock!` stream. This argument can be omitted
and the stream be passed as a dynamic definition in `*clock*` instead.
- `period` should be a num~ stream.
- `evt` can be a value of any type. It defaults to `bang`.
- the return type will be an event stream with the same type as `evt`."
value: class extends Op
pattern = -evt.clock + sig.num + -sig!
setup: (inputs, scope) =>
{ clock, period, evt } = pattern\match inputs
super
clock: Input.hot clock or scope\get '*clock*'
period: Input.cold period
evt: Input.cold evt or T.bang\mk_const true
@state or= 0
@setup_out '!', @inputs.evt\type!
tick: =>
{ :clock, :period, :evt } = @unwrap_all!
return if period == 0
@state += clock.dt / period
if @state >= 1
@state = @state % 1
@out\set evt
val_seq = Constant.meta
meta:
name: 'val-seq'
summary: "Emit a sequence of values as events over time."
examples: { '(val-seq [clock] delay0 evt1 delay1 evt2 delay2…)' }
description: "
Emits `evt1`, `evt2`, … as events with delays `delay0`, `delay1`, … in between.
- `clock` should be a `time/clock!` stream. This argument can be omitted
and the stream be passed as a dynamic definition in `*clock*` instead.
- `delay0`, `delay1`, … must be num~ streams.
- `evt1`, `evt2`, … must be signal streams of the same type.
- the return type will be an event stream with the same type as the `evt`s."
value: class extends Op
new: (...) =>
super ...
@state or= { i: 1, t: 0 }
pair = (sig! + sig.num)\named('value', 'delay')
pattern = -evt.clock + sig.num + pair*0
inputify = (step) ->
{
delay: Input.cold step.delay
value: if step.value then Input.hot step.value
}
setup: (inputs, scope) =>
{ clock, first, steps } = pattern\match inputs
table.insert steps, 1, { delay: first }
super
clock: Input.hot clock or scope\get '*clock*'
steps: [inputify step for step in *steps]
@setup_out '!', steps[1].value\type!
tick: =>
if tick = @inputs.clock!
@state.t += tick.dt
change, current = false, nil
while true
current = @inputs.steps[@state.i]
if @state.t >= current.delay!
@state.t -= current.delay!
@state.i = 1 + (@state.i % #@inputs.steps)
change = true
else
break
if current.value and (change or current.value\dirty!)
@out\set current.value!
vis: => step: @state.i
bang_seq = Constant.meta
meta:
name: 'bang-seq'
summary: "Generate rhythms based on a sequence of delays"
examples: { '(bang-seq [clock] delay0 delay1…)' }
description: "
Emits `bang!`s with delays `delay0`, `delay1`, … in between.
- `clock` should be a `time/clock!` stream. This argument can be omitted
and the stream be passed as a dynamic definition in `*clock*` instead.
- `delay0`, `delay1`, … must be num~ streams."
value: class extends Op
new: (...) =>
super ...
@out = T.bang\mk_evt!
@state or= { i: 1, t: 0 }
pattern = -evt.clock + sig.num*0
setup: (inputs, scope) =>
{ clock, steps } = pattern\match inputs
super
clock: Input.hot clock or scope\get '*clock*'
steps: [Input.cold step for step in *steps]
tick: =>
if tick = @inputs.clock!
@state.t += tick.dt
bang = false
while true
current = @inputs.steps[@state.i]!
if @state.t >= current
@state.t -= current
@state.i = 1 + (@state.i % #@inputs.steps)
bang = true
else
break
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
pattern = -evt.clock + sig.num + any.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
@setup_out '~', T.num, @inputs.value!
tick: =>
{ :clock, :rate, :value } = @unwrap_all!
if clock
current = @.out!
delta = value - current
return if 1e-15 > math.abs delta
@out\set current + delta * rate
spring = Constant.meta
meta:
name: 'spring'
summary: "Integrate impulses using easing"
examples: { '(spring [clock] evt0 [evt1…])' }
description: "
Every arriving `evt` triggers an impulse that is integrated as `clock` ticks.
Returns a num~ stream that starts at 0.
- `clock` should be a `time/clock!` stream. This argument can be omitted
and the stream be passed as a dynamic definition in `*clock*` instead.
- `evt!` are !-streams of structs.
The following struct keys can be specified:
- `amp`: amplitude of the impulse
- `dur`: duration of the impulse (seconds)
- `del`: start delay (seconds)
- `ease`: easing function to use
Instead of `dur`, `in` and `out` can be specified to create both an impulse
and counterimpulse, which has an amplitude of `-amp` and is delayed so as to
start immediately after the original impulse. Similarily, `easein` and `easeout`
can be specified instead of the single `ease` value."
value: class extends Op
pattern = -evt.clock + evt!*0
setup: (inputs, scope) =>
{ clock, events } = pattern\match inputs
super
clock: Input.hot clock or scope\get '*clock*'
events: [Input.hot e for e in *events]
@state or= { offset: 0, events: {} }
@setup_out '~', T.num, @state.offset
tick: =>
for inp in *@inputs.events
if evt = inp!
amp = evt.amp or 1
del = evt.del or 0
dur = evt.dur or 1
on = {
i: -del
amp: amp
dur: evt.in or evt.dur
ease: evt.easein or evt.ease or "quad.out"
}
on.i = on.i / on.dur
@state.events[on] = true
if evt.out
off = {
i: -(del + on.dur),
amp: -amp,
dur: evt.out
ease: evt.easeout or evt.ease or "quad.out"
}
off.i = off.i / off.dur
@state.events[off] = true
if clk = @inputs.clock!
dt = clk.dt
accum = 0
for evt in pairs @state.events
evt.i += dt / evt.dur
if evt.i >= 1
@state.offset += evt.amp
@state.events[evt] = nil
elseif evt.i >= 0
t = 1 - evt.i
t = 1 - t * t
accum += t * evt.amp
@out\set @state.offset + accum
delay = Constant.meta
meta:
name: 'delay!'
summary: "Delay a !-stream event"
examples: { '(delay! [clock] delay evt)' }
description: "
Delays incoming `evt`s by `delay`.
- `clock` should be a `time/clock!` stream. This argument can be omitted
and the stream be passed as a dynamic definition in `*clock*` instead.
- `delay` should be a num= or num~ stream.
- `evt` is a !-stream."
value: class extends Op
pattern = -evt.clock + any.num + any!
setup: (inputs, scope) =>
{ clock, delay, evt } = pattern\match inputs
super
clock: Input.hot clock or scope\get '*clock*'
delay: Input.cold delay
evt: Input.hot evt
@state or= {}
if @setup_out '!', @inputs.evt\type!
@state = {}
tick: =>
clock = @inputs.clock!
if clock and #@state > 0
delta = clock.dt
for item in *@state
item.delay -= delta
if @state[1].delay < 0
item = table.remove @state, 1
@out\set item.value
if @inputs.evt\dirty!
value = @inputs.evt!
delay = @inputs.delay!
table.insert @state, { :delay, :value }
RTNode
children: { default_clock }
result: Constant.meta
meta:
name: 'time'
summary: "Time-variant operators."
value:
:clock
'scale-time': scale_time
:lfo
:ramp
:tick
:every
'val-seq': val_seq
'bang-seq': bang_seq
:smooth
:spring
'delay!': delay
'*clock*': default_clock
|