git.s-ol.nu alive / c6fa7aa
add vis field to Ops s-ol 1 year, 3 months ago
5 changed file(s) with 31 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
2121 fork: =>
2222 out = if @out then @out\fork!
2323 state = if @state then deep_copy @state
24 @@ out, state
24 vis = if @vis then deep_copy @vis
25 @@ out, state, vis
2526
2627 --- internal state of this Op.
2728 --
2930 -- no metatables, multiple references/loops, userdata etc.
3031 --
3132 -- @tfield table state
33
34 --- visualisation data of this Op.
35 --
36 -- This may be any simple Lua value, including Lua tables, as long as it has
37 -- no metatables, multiple references/loops, userdata etc.
38 --
39 -- This value is exposed to alv editors in order to render realtime
40 -- visualisations overlaid onto the program text.
41 --
42 -- @tfield table vis
3243
3344 --- `Result` instance representing this Op's computed output value.
3445 --
106117 -- @classmethod
107118 -- @tparam ?Result out `out`
108119 -- @tparam ?table state `state`
109 new: (@out, @state) =>
120 -- @tparam ?table vis `vis`
121 new: (@out, @state, @vis={}) =>
110122
111123 do_setup = (old, cur) ->
112124 for k, cur_val in pairs cur
330330 when false then 1
331331 else (math.floor i!) % #values
332332
333 @vis.step = ii
333334 @out\set if v = values[ii + 1] then v!
334335
335336 trace_ = Constant.meta
11 import encode, decode from require 'dkjson'
22 import fn_invoke, op_invoke from require 'alv.invoke'
33
4 encode_res = (copilot) =>
4 encode_res = =>
55 return unless @
66
77 {
4444 if builtin and builtin.__class.__name ~= 'DummyReg'
4545 res.head_meta = builtin.head.meta
4646 res.result = encode_res builtin.node.result
47 res.vis = if builtin.op then builtin.op.vis
4748 res.kind = switch builtin.__class
4849 when op_invoke then 'op'
4950 when fn_invoke then 'fn'
5051 else 'builtin'
51 else
52 res.error = 'not_registered'
53 when 'state'
54 res.tag = msg.tag
55 res.module = msg.module or '__root'
56 mod = @copilot.last_modules[res.module]
57 builtin = mod.registry\last msg.tag
58 if builtin and builtin.__class.__name ~= 'DummyReg'
59 res.value = encode_res builtin.node.result, @copilot
60 res.state = if builtin.op then builtin.op.state
6152 else
6253 res.error = 'not_registered'
6354 else
7979 range: Input.cold range or Constant.str 'uni'
8080
8181 @out or= T.num\mk_sig apply_range @inputs.range, 0
82 @vis.bar = 0
8283
8384 tick: =>
8485 { :port, :cc, :chan, :range } = @inputs
8788 (chan! == -1 or msg.chan == chan!) and
8889 msg.a == cc!
8990 @out\set apply_range range, msg.b
91 @vis.bar = msg.b / 127
9092
9193 {
9294 :input
109109 when 'saw' then @state % 1
110110 when 'tri' then math.abs (2*@state % 2) - 1
111111 else error Error 'argument', "unknown wave type '#{wave}'"
112 @vis.bar = @out!
112113
113114 ramp = Constant.meta
114115 meta:
139140 { :clock, :period, :max } = @unwrap_all!
140141 max or= period
141142
142 @phase += clock.dt / period
143 while @phase >= 1
144 @phase -= 1
145
146 @out\set @phase * max
143 @state += clock.dt / period
144 while @state >= 1
145 @state -= 1
146
147 @vis.bar = @state
148 @out\set @state * max
147149
148150 tick = Constant.meta
149151 meta:
161163 super ...
162164 @state or= { phase: 0, count: 0 }
163165 @out or= T.num\mk_sig @state.count
166 @vis.onchange = true
164167
165168 pattern = -evt.clock + sig.num
166169 setup: (inputs, scope) =>
227230 new: (...) =>
228231 super ...
229232 @state or= { i: 1, t: 0 }
233 @vis.step = @state.i
230234
231235 pair = (sig! + sig.num)\named('value', 'delay')
232236 pattern = -evt.clock + sig.num + pair*0
255259 if @state.t >= current.delay!
256260 @state.t -= current.delay!
257261 @state.i = 1 + (@state.i % #@inputs.steps)
262 @vis.step = @state.i
258263 change = true
259264 else
260265 break
279284 super ...
280285 @out = T.bang\mk_evt!
281286 @state or= { i: 1, t: 0 }
287 @vis.step = @state.i
282288
283289 pattern = -evt.clock + sig.num*0
284290 setup: (inputs, scope) =>
298304 if @state.t >= current
299305 @state.t -= current
300306 @state.i = 1 + (@state.i % #@inputs.steps)
307 @vis.step = @state.i
301308 bang = true
302309 else
303310 break