monome / crow

Crow speaks and listens and remembers bits of text. A scriptable USB-CV-II machine
GNU General Public License v3.0
162 stars 34 forks source link

`fnl` frame-based animator function in stdlib #450

Open trentgill opened 2 years ago

trentgill commented 2 years ago

from one of the Maps episodes, i made a function fnl that would 'funnel' a continuous change by turning it into discrete steps & calling a function at each point.

it is primarily focused at ii devices where you may want to step a parameter in a smooth way, but only have a discretely stepped function call available. thus the function could be ii.funnel or ii.fnl. special care should be paid to interaction with timeline.

it is somewhere between ASL and timeline, and i wonder if there is a way to unify it into the timeline module?

from here:

-- call a function repetitively in time
-- the arg to the function is interpolated fps times per second
-- dest_ms is a table of `to()` calls for ASL-like sequences
function fnl(fn, origin, dest_ms, fps)
    return clock.run(function()
        fps = fps or 15 -- default
        local spf = 1 / fps -- seconds per frame
        fn(origin)
        for _,v in ipairs(dest_ms) do
            local count = math.floor(v[3] * fps) -- number of iterations
            local stepsize = (v[2]-origin) / count -- how much to increment by each iteration
            while count > 0 do
                clock.sleep(spf)
                origin = origin + stepsize -- move toward destination
                count = count - 1 -- count iteration
                fn(origin)
            end
        end
    end)
end