git.s-ol.nu alive / 5c79491
Op: vis method instead of field s-ol 1 year, 3 months ago
5 changed file(s) with 54 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
22 --
33 -- @classmod Op
44 import deep_copy, deep_iter, deep_map from require 'alv.util'
5 import T from require 'alv.type'
56
67 class Op
78 --- members
2122 fork: =>
2223 out = if @out then @out\fork!
2324 state = if @state then deep_copy @state
24 vis = if @vis then deep_copy @vis
25 @@ out, state, vis
25 @@ out, state
2626
2727 --- internal state of this Op.
2828 --
3030 -- no metatables, multiple references/loops, userdata etc.
3131 --
3232 -- @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
4333
4434 --- `Result` instance representing this Op's computed output value.
4535 --
10494 --- called when the Op is destroyed (optional).
10595 destroy: =>
10696
97 --- collect visualisation data (optional).
98 --
99 -- This may return any simple Lua value, including Lua tables, as long as it
100 -- has no metatables, multiple references/loops, userdata etc.
101 --
102 -- This value is exposed to alv editors in order to render realtime
103 -- visualisations overlaid onto the program text.
104 --
105 -- @treturn table vis
106 vis: =>
107 if @out and @out.metatype == '!'
108 { type: 'event' }
109 elseif @out and @out.type == T.bool
110 { type: 'bool' }
111 else
112 {}
113
107114 --- implementation utilities.
108115 --
109116 -- super-methods and utilities for use by implementations.
117124 -- @classmethod
118125 -- @tparam ?Result out `out`
119126 -- @tparam ?table state `state`
120 -- @tparam ?table vis `vis`
121 new: (@out, @state, @vis={}) =>
127 new: (@out, @state) =>
122128
123129 do_setup = (old, cur) ->
124130 for k, cur_val in pairs cur
308308
309309 if i\type! == T.bang
310310 @state or= 1
311 else
312 @state = nil
313311
314312 super
315313 i: Input.hot i
318316 tick: =>
319317 { :i, :values } = @inputs
320318
321 ii = if i\type! == T.bang
319 if i\type! == T.bang
322320 if i\dirty!
323321 @state += 1
324322 while @state >= #values
325323 @state -= #values
326324 @state
327325 else
328 switch i!
326 @state = switch i!
329327 when true then 0
330328 when false then 1
331329 else (math.floor i!) % #values
332
333 @vis.step = ii
334 @out\set if v = values[ii + 1] then v!
330 @state
331
332 @out\set if v = values[@state + 1] then v!
333
334 vis: => step: @state
335335
336336 trace_ = Constant.meta
337337 meta:
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
47 res.vis = if builtin.op then builtin.op\vis!
4848 res.kind = switch builtin.__class
4949 when op_invoke then 'op'
5050 when fn_invoke then 'fn'
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
8382
8483 tick: =>
8584 { :port, :cc, :chan, :range } = @inputs
8786 if msg.status == 'control-change' and
8887 (chan! == -1 or msg.chan == chan!) and
8988 msg.a == cc!
89 @state = msg.b / 128
9090 @out\set apply_range range, msg.b
91 @vis.bar = msg.b / 127
91
92 vis: =>
93 {
94 type: 'bar'
95 bar: @state
96 }
9297
9398 {
9499 :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!
112
113 vis: =>
114 {
115 type: 'bar'
116 bar: @.out!
117 }
113118
114119 ramp = Constant.meta
115120 meta:
144149 while @state >= 1
145150 @state -= 1
146151
147 @vis.bar = @state
148152 @out\set @state * max
153
154 vis: =>
155 {
156 type: 'bar'
157 bar: @state
158 }
149159
150160 tick = Constant.meta
151161 meta:
163173 super ...
164174 @state or= { phase: 0, count: 0 }
165175 @out or= T.num\mk_sig @state.count
166 @vis.onchange = true
167176
168177 pattern = -evt.clock + sig.num
169178 setup: (inputs, scope) =>
179188 @state.phase -= 1
180189 @state.count += 1
181190 @out\set @state.count
191
192 vis: => type: 'event'
182193
183194 every = Constant.meta
184195 meta:
230241 new: (...) =>
231242 super ...
232243 @state or= { i: 1, t: 0 }
233 @vis.step = @state.i
234244
235245 pair = (sig! + sig.num)\named('value', 'delay')
236246 pattern = -evt.clock + sig.num + pair*0
259269 if @state.t >= current.delay!
260270 @state.t -= current.delay!
261271 @state.i = 1 + (@state.i % #@inputs.steps)
262 @vis.step = @state.i
263272 change = true
264273 else
265274 break
266275
267276 if current.value and (change or current.value\dirty!)
268277 @out\set current.value!
278
279 vis: => step: @state.i
269280
270281 bang_seq = Constant.meta
271282 meta:
284295 super ...
285296 @out = T.bang\mk_evt!
286297 @state or= { i: 1, t: 0 }
287 @vis.step = @state.i
288298
289299 pattern = -evt.clock + sig.num*0
290300 setup: (inputs, scope) =>
304314 if @state.t >= current
305315 @state.t -= current
306316 @state.i = 1 + (@state.i % #@inputs.steps)
307 @vis.step = @state.i
308317 bang = true
309318 else
310319 break
311320
312321 if bang
313 @out\set true
322 @out
323
324 vis: => step: @state.i\set true
314325
315326 {
316327 :clock