import Constant, Op, Builtin, Input, T, Struct, const, any from require 'alv.base' import new_message, add_item from require 'alv-lib._osc' import dns, udp from require 'socket' glsl_type = (type) -> switch type when T.num then 'float' when T.str then 'sampler3D' when T.bool then 'bool' else error "unknown primitive type" resubst = (str, fn) -> str = str\gsub '##', '#' str\gsub '#{(%d+)}', fn shader = Constant.meta meta: name: 'gate' summary: "gate from note-on and note-off messages." examples: { '(midi/gate [port] note [chan])' } value: class extends Op pattern = const.str + any!^0 setup: (inputs, scope) => { code, params } = pattern\match inputs super [Input.hot param for param in *params] uniforms = {} inputs = {} for i, input in ipairs @inputs table.insert uniforms, "uniform #{glsl_type input\type!} alv_param_#{i};" code = code.result! code = resubst code, (i) -> "alv_param_#{i}" table.insert uniforms, code code = table.concat uniforms, '\n' print code @state or= with sock = udp! \setpeername '127.0.0.1', 9000 msg = new_message "/-/shader" add_item msg, T.str, code @state\send msg.pack msg.content tick: (setup) => for i, input in ipairs @inputs if setup or input\dirty! msg = new_message "/alv_param_#{i}" print "/alv_param_#{i}", input! add_item msg, input\type!, input! @state\send msg.pack msg.content Constant.meta meta: name: 'glsl-view' summary: "glsl-view integration via OSC." value: :shader