complete lua DSP filter with parameter interpolation & comments

This commit is contained in:
Robin Gareus 2016-05-21 16:24:15 +02:00
parent 298abc5c8c
commit d6aa79bca5

View file

@ -6,14 +6,13 @@ ardour {
author = "Robin Gareus", author = "Robin Gareus",
email = "robin@gareus.org", email = "robin@gareus.org",
site = "http://gareus.org", site = "http://gareus.org",
description = [[ description = [[Example Ardour Lua DSP Plugin]]
An Example DSP Plugin for processing audio, to
be used with Ardour's Lua scripting facility.]]
} }
function dsp_ioconfig () function dsp_ioconfig ()
return return
{ {
-- allow any number of I/O as long as port-count matches
{ audio_in = -1, audio_out = -1}, { audio_in = -1, audio_out = -1},
} }
end end
@ -37,59 +36,111 @@ function dsp_params ()
} }
end end
-- these globals are *not* shared between DSP and UI -- translate type parameter to DSP enum
local filt -- the filter instance -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.DSP.Biquad.Type
local cur = {0, 0, 0, 0} -- current settings function map_type (t)
if t == 1 then
function dsp_init (rate) return ARDOUR.DSP.BiquadType.LowShelf
self:shmem ():allocate (1) -- shared mem to tell UI about samplerate elseif t == 2 then
local cfg = self:shmem ():to_int (0):array () return ARDOUR.DSP.BiquadType.HighShelf
cfg[1] = rate elseif t == 3 then
filt = ARDOUR.DSP.Biquad (rate) -- initialize filter return ARDOUR.DSP.BiquadType.LowPass
elseif t == 4 then
return ARDOUR.DSP.BiquadType.HighPass
else
return ARDOUR.DSP.BiquadType.Peaking
end
end end
-- these globals are *not* shared between DSP and UI
local filt -- the biquad filter instance
local cur = {0, 0, 0, 0} -- current parameters
local lpf = 0.03 -- parameter low-pass filter time-constant
-- apply parameters, re-compute filter coefficients if needed function dsp_init (rate)
function apply_params (ctrl) self:shmem ():allocate (1) -- shared mem to tell the GUI the samplerate
local cfg = self:shmem ():to_int (0):array ()
cfg[1] = rate
-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
filt = ARDOUR.DSP.Biquad (rate) -- initialize filter
lpf = 13000 / rate -- interpolation time constant
end
-- helper functions for parameter interpolation
function param_changed (ctrl)
if ctrl[1] == cur[1] and ctrl[2] == cur[2] and ctrl[3] == cur[3] and ctrl[4] == cur[4] then if ctrl[1] == cur[1] and ctrl[2] == cur[2] and ctrl[3] == cur[3] and ctrl[4] == cur[4] then
return false return false
end end
return true
end
local ft function low_pass_filter_param (old, new, limit)
if ctrl[1] == 1 then if math.abs (old - new) < limit then
ft = ARDOUR.DSP.BiQuadType.LowShelf return new
elseif ctrl[1] == 2 then
ft = ARDOUR.DSP.BiQuadType.HighShelf
elseif ctrl[1] == 3 then
ft = ARDOUR.DSP.BiQuadType.LowPass
elseif ctrl[1] == 4 then
ft = ARDOUR.DSP.BiQuadType.HighPass
else else
ft = ARDOUR.DSP.BiQuadType.Peaking return old + lpf * (new - old)
end
end
-- apply parameters, re-compute filter coefficients if needed
function apply_params (ctrl)
if not param_changed (ctrl) then
return
end end
-- TODO low-pass filter ctrl values, smooth transition if cur[1] ~= ctrl[1] then
filt:compute (ft, ctrl[3], ctrl[4], ctrl[2]) -- reset filter state when type changes
cur[1] = ctrl[1] filt:reset ()
cur[2] = ctrl[2] for k = 1,4 do cur[k] = ctrl[k] end
cur[3] = ctrl[3] else
cur[4] = ctrl[4] -- low-pass filter ctrl parameter values, smooth transition
return true cur[2] = low_pass_filter_param (cur[2], ctrl[2], 0.1) -- gain/dB
cur[3] = low_pass_filter_param (cur[3], ctrl[3], 1.0) -- freq/Hz
cur[4] = low_pass_filter_param (cur[4], ctrl[4], 0.01) -- quality
end
filt:compute (map_type (cur[1]), cur[3], cur[4], cur[2])
end end
-- the actual DSP callback -- the actual DSP callback
function dsp_run (ins, outs, n_samples) function dsp_run (ins, outs, n_samples)
if apply_params (CtrlPorts:array ()) then local changed = false
self:queue_draw () local siz = n_samples
local off = 0
-- if a parameter was changed, process at most 64 samples at a time
-- and interpolate parameters until the current settings match
-- the target values
if param_changed (CtrlPorts:array ()) then
changed = true
siz = 64
end end
for c = 1,#ins do
if ins[c]:sameinstance (outs[c]) then while n_samples > 0 do
filt:run (ins[c], n_samples) -- in-place if changed then apply_params (CtrlPorts:array ()) end
else if siz > n_samples then siz = n_samples end
ARDOUR.DSP.copy_vector (outs[c], ins[c], n_samples)
filt:run (outs[c], n_samples) -- process all channels
for c = 1,#ins do
-- check if output and input buffers for this channel are identical
-- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
if ins[c]:sameinstance (outs[c]) then
filt:run (ins[c]:offset (off), siz) -- in-place processing
else
-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz)
filt:run (outs[c]:offset (off), siz)
end
end end
n_samples = n_samples - siz
off = off + siz
end
if changed then
-- notify display
self:queue_draw ()
end end
end end
@ -102,20 +153,24 @@ function round (n)
end end
function freq_at_x (x, w) function freq_at_x (x, w)
-- x-axis pixel for given freq, power-scale
return 20 * 1000 ^ (x / w) return 20 * 1000 ^ (x / w)
end end
function x_at_freq (f, w) function x_at_freq (f, w)
-- frequency at given x-axis pixel
return w * math.log (f / 20.0) / math.log (1000.0) return w * math.log (f / 20.0) / math.log (1000.0)
end end
function db_to_y (db, h) function db_to_y (db, h)
-- y-axis gain mapping
if db < -20 then db = -20 end if db < -20 then db = -20 end
if db > 20 then db = 20 end if db > 20 then db = 20 end
return -.5 + 0.5 * h * (1 - db / 20) return -.5 + 0.5 * h * (1 - db / 20)
end end
function grid_db (ctx, w, h, db) function grid_db (ctx, w, h, db)
-- draw horizontal grid line
local y = -.5 + round (db_to_y (db, h)) local y = -.5 + round (db_to_y (db, h))
ctx:move_to (0, y) ctx:move_to (0, y)
ctx:line_to (w, y) ctx:line_to (w, y)
@ -123,6 +178,7 @@ function grid_db (ctx, w, h, db)
end end
function grid_freq (ctx, w, h, f) function grid_freq (ctx, w, h, f)
-- draw vertical grid line
local x = -.5 + round (x_at_freq (f, w)) local x = -.5 + round (x_at_freq (f, w))
ctx:move_to (x, 0) ctx:move_to (x, 0)
ctx:line_to (x, h) ctx:line_to (x, h)
@ -131,32 +187,43 @@ end
function render_inline (ctx, w, max_h) function render_inline (ctx, w, max_h)
if not filt then if not filt then
-- instantiate filter (to calculate the transfer function) -- the GUI is separate from the DSP, but the GUI needs to know
-- the sample-rate that the DSP is using.
local shmem = self:shmem () -- get shared memory region local shmem = self:shmem () -- get shared memory region
local cfg = shmem:to_int (0):array () -- "cast" into lua-table local cfg = shmem:to_int (0):array () -- "cast" into lua-table
-- instantiate filter (to calculate the transfer function's response)
filt = ARDOUR.DSP.Biquad (cfg[1]) filt = ARDOUR.DSP.Biquad (cfg[1])
end end
apply_params (CtrlPorts:array ()) -- set filter coefficients if they have changed
if param_changed (CtrlPorts:array ()) then
local ctrl = CtrlPorts:array ()
for k = 1,4 do cur[k] = ctrl[k] end
filt:compute (map_type (cur[1]), cur[3], cur[4], cur[2])
end
-- calc height of inline display -- calc height of inline display
local h = math.ceil (w * 10 / 16) -- 16:10 aspect local h = math.ceil (w * 10 / 16) -- use 16:10 aspect
h = 2 * round (h / 2) -- even number of vertical px h = 2 * round (h / 2) -- with an even number of vertical pixels
if (h > max_h) then if (h > max_h) then h = max_h end -- but at most max-height
h = max_h
end -- ctx is a http://cairographics.org/ context
-- http://manual.ardour.org/lua-scripting/class_reference/#Cairo:Context
-- clear background -- clear background
ctx:rectangle (0, 0, w, h) ctx:rectangle (0, 0, w, h)
ctx:set_source_rgba (.2, .2, .2, 1.0) ctx:set_source_rgba (.2, .2, .2, 1.0)
ctx:fill () ctx:fill ()
-- set line width: 1px
-- Note: a cairo pixel at [1,1] spans [0.5->1.5 , 0.5->1.5]
-- hence the offset -0.5 in various move_to(), line_to() calls
ctx:set_line_width (1.0) ctx:set_line_width (1.0)
-- draw grid -- draw grid
local dash3 = C.DoubleVector () local dash3 = C.DoubleVector ()
dash3:add ({1, 3}) dash3:add ({1, 3})
ctx:set_dash (dash3, 2) ctx:set_dash (dash3, 2) -- dotted line
ctx:set_source_rgba (.5, .5, .5, .5) ctx:set_source_rgba (.5, .5, .5, .5)
grid_db (ctx, w, h, 0) grid_db (ctx, w, h, 0)
grid_db (ctx, w, h, 6) grid_db (ctx, w, h, 6)
@ -170,7 +237,7 @@ function render_inline (ctx, w, max_h)
grid_freq (ctx, w, h, 10000) grid_freq (ctx, w, h, 10000)
ctx:unset_dash () ctx:unset_dash ()
-- draw transfer function -- draw transfer function line
ctx:set_source_rgba (.8, .8, .8, 1.0) ctx:set_source_rgba (.8, .8, .8, 1.0)
ctx:move_to (-.5, db_to_y (filt:dB_at_freq (freq_at_x (0, w)), h)) ctx:move_to (-.5, db_to_y (filt:dB_at_freq (freq_at_x (0, w)), h))
for x = 1,w do for x = 1,w do