legato.lua
--------------------------------------------------------------------------------
--! @example legato.lua
--------------------------------------------------------------------------------
local notes = {}
Fade = Knob("fade", 40, 10, 100)
Retrigger = OnOffButton("retrigger", false)
local sampleOffset = 50 -- ms
function onNote(e)
if #notes > 0 then
local fadetime = Fade.value
fadeout(notes[#notes].id, fadetime, true)
local id = postEvent(e)
table.insert(notes, e)
setSampleOffset(id, sampleOffset)
fadein(id, fadetime, true)
else
local id = postEvent(e)
table.insert(notes, e)
end
end
function onRelease(e)
for i,noteon in ipairs(notes) do
if noteon.note == e.note then
table.remove(notes, i)
releaseVoice(noteon.id)
local shouldRetrigger = Retrigger.value and #notes > 0 and i > #notes
if shouldRetrigger then
local noteon = notes[#notes]
local id = playNote(noteon.note, noteon.velocity)
noteon.id = id
local fadetime = Fade.value
setSampleOffset(id, sampleOffset)
fadein(id, fadetime, true)
end
break
end
end
end