diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2023-09-26 18:42:11 +0300 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2023-09-26 18:42:11 +0300 |
commit | 8f0147f9d37509306e8b045cf85e95db3fa6390d (patch) | |
tree | 47770106a7a8096e9109480d374937fd278e532b | |
parent | 72c38b3e2c140aab10af4ba21283e18819e1dddf (diff) | |
download | applause2-8f0147f9d37509306e8b045cf85e95db3fa6390d.tar.gz |
Stream:evrel(): scale output values only once per event
* The old implementation would require scaling operations at sample rate, even though
the value changes only very seldom.
* We are working with fractions, so we don't actually need to store the unscaled value in the closure.
-rw-r--r-- | evdev.lua | 15 |
1 files changed, 8 insertions, 7 deletions
@@ -178,22 +178,23 @@ end -- This is a C value, integer or string ('REL_X', 'REL_Y'...), specifying the axis to extract. -- The possible values correspond to the C header `input-event-codes.h`. -- @number[opt=1000] resolution --- The device resolution, ie. the number of steps between [-1,1]. +-- The device resolution, ie. the number of steps between [-1,+1]. -- The larger this value, the longer it takes to move from the minimum to the maximum position. --- @treturn Stream Stream of numbers between [-1,1]. +-- @treturn Stream Stream of numbers between [-1,+1]. -- @see EvdevStream:new -- @see Stream:scale -- @usage EvdevStream("TrackPoint"):evrel():scale(440,880):SinOsc():play() function Stream:evrel(code, resolution) code = ffi.cast("enum applause_evdev_rel", code) - resolution = resolution or 1000 + resolution = (resolution or 1000)/2 + local min, max = math.min, math.max return self:scan(function(last, sample) - last = last or 0 + last = last or -1 return sample.type == C.EV_REL and sample.code == code and - min(max(last+sample.value, 0), resolution) or last - end) / (resolution/2) - 1 + min(max(last+tonumber(sample.value)/resolution, -1), 1) or last + end) end --- Filter Evdev event stream to get the last value of a device with absolute positioning (EV_ABS). @@ -222,7 +223,7 @@ function Stream:evabs(code, min, max) return self:scan(function(last, sample) last = last or 0 return sample.type == C.EV_ABS and sample.code == code and - (sample.value - min)/((max-min)/2) - 1 or last + tonumber((sample.value - min)*2)/(max-min) - 1 or last end) end |