Skip to content

Instantly share code, notes, and snippets.

@SpotlightKid
Created January 21, 2015 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpotlightKid/6ad8cf5b0891e72f233e to your computer and use it in GitHub Desktop.
Save SpotlightKid/6ad8cf5b0891e72f233e to your computer and use it in GitHub Desktop.
Simple delay with feedback for ProtoplugFX
--[[
name: Simple Delay
description: Simple delay with feedback
author: carndt
--]]
require "include/protoplug"
-- Delay line state
local buf, buflen, writepos, sr
-- Plugin params
local length, feedback, drywetmix
-- 'prepareToPlay' will be triggered when the host sample rate is known,
plugin.addHandler('prepareToPlay', function()
sr = plugin.getSampleRate()
buflen = sr * 5
buf = ffi.new("double[?]", buflen)
writepos = 0
setLength(250)
end)
function plugin.processBlock(samples, smax)
for i = 0, smax do
-- Read from delay line with fixed delay length in samples
local readpos = writepos - length
-- wrap around sample read position index
if readpos < 0 then
readpos = buflen + readpos
end
local out = buf[readpos]
-- Read left and write sample from input buffer,
local left, right = samples[0][i], samples[1][i]
-- mix them with delay according to dry/wet mix param
-- and set the left and right output samples
local wetsignal = out * drywetmix
samples[0][i] = left * (1 - drywetmix) + wetsignal
samples[1][i] = right * (1 - drywetmix) + wetsignal
-- Save mono mix of input mixed with delay, attenuated by feedback,
-- to delay line at current write position
buf[writepos] = (left + right) / 2 + out * feedback
-- Increase delay position pointer
writepos = writepos + 1
if writepos >= buflen then writepos = 0 end
end
end
function setLength(val)
if plugin.isSampleRateKnown() then
length = val / 1000 * plugin.getSampleRate()
end
end
params = plugin.manageParams {
{
name = "Length (ms)",
type = "int",
min = 1,
max = 5000,
default = 250,
changed = setLength
},
{
name = "Feedback",
type = "int",
max = 100,
default = 1,
changed = function(val) feedback = val / 100 end
},
{
name = "Dry/Wet Mix",
type = "int",
min = -100,
max = 100,
default = 0,
changed = function(val) drywetmix = (val + 100) / 201 end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment