2016-05-20 17:43:47 -04:00
|
|
|
ardour {
|
|
|
|
["type"] = "dsp",
|
|
|
|
name = "Biquad Filter",
|
|
|
|
category = "Filter",
|
|
|
|
license = "MIT",
|
2020-09-30 16:06:35 -04:00
|
|
|
author = "Ardour Team",
|
2016-07-12 09:21:23 -04:00
|
|
|
description = [[A Versatile Filter Plugin]]
|
2016-05-20 17:43:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function dsp_ioconfig ()
|
|
|
|
return
|
|
|
|
{
|
2016-05-21 10:24:15 -04:00
|
|
|
-- allow any number of I/O as long as port-count matches
|
2016-05-20 17:43:47 -04:00
|
|
|
{ audio_in = -1, audio_out = -1},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function dsp_params ()
|
|
|
|
return
|
|
|
|
{
|
2016-07-07 10:45:10 -04:00
|
|
|
{ ["type"] = "input", name = "Enable", min = 0, max = 1, default = 1, bypass = true, toggled = true },
|
2016-05-20 17:43:47 -04:00
|
|
|
{ ["type"] = "input", name = "Type", min = 0, max = 4, default = 0, enum = true, scalepoints =
|
|
|
|
{
|
|
|
|
["Peaking"] = 0,
|
|
|
|
["Low Shelf"] = 1,
|
|
|
|
["High Shelf"] = 2,
|
|
|
|
["Low Pass"] = 3,
|
|
|
|
["High Pass"] = 4,
|
2022-10-31 17:45:45 -04:00
|
|
|
["Analog PK"] = 5,
|
|
|
|
["Analog LP"] = 6,
|
|
|
|
["Analog HP"] = 7,
|
|
|
|
["Analog BP"] = 8,
|
2016-05-20 17:43:47 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{ ["type"] = "input", name = "Gain", min = -20, max = 20, default = 0, unit="dB" },
|
|
|
|
{ ["type"] = "input", name = "Freq", min = 20, max = 20000, default = 1000, unit="Hz", logarithmic = true },
|
|
|
|
{ ["type"] = "input", name = "Q", min = 0.1, max = 8, default = .707, logarithmic = true },
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-05-21 10:24:15 -04:00
|
|
|
-- translate type parameter to DSP enum
|
|
|
|
-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.DSP.Biquad.Type
|
|
|
|
function map_type (t)
|
|
|
|
if t == 1 then
|
|
|
|
return ARDOUR.DSP.BiquadType.LowShelf
|
|
|
|
elseif t == 2 then
|
|
|
|
return ARDOUR.DSP.BiquadType.HighShelf
|
|
|
|
elseif t == 3 then
|
|
|
|
return ARDOUR.DSP.BiquadType.LowPass
|
|
|
|
elseif t == 4 then
|
|
|
|
return ARDOUR.DSP.BiquadType.HighPass
|
2022-10-31 17:45:45 -04:00
|
|
|
elseif t == 5 then
|
|
|
|
return ARDOUR.DSP.BiquadType.MatchedPeaking
|
|
|
|
elseif t == 6 then
|
|
|
|
return ARDOUR.DSP.BiquadType.MatchedLowPass
|
|
|
|
elseif t == 7 then
|
|
|
|
return ARDOUR.DSP.BiquadType.MatchedHighPass
|
|
|
|
elseif t == 8 then
|
|
|
|
return ARDOUR.DSP.BiquadType.MatchedBandPass0dB
|
2016-05-21 10:24:15 -04:00
|
|
|
else
|
|
|
|
return ARDOUR.DSP.BiquadType.Peaking
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-07 10:45:10 -04:00
|
|
|
function ctrl_data ()
|
|
|
|
local ctrl = CtrlPorts:array ()
|
|
|
|
if ctrl[1] <= 0 then -- when disabled
|
|
|
|
ctrl[3] = 0; -- force gain to 0dB
|
|
|
|
end
|
|
|
|
return ctrl
|
|
|
|
end
|
|
|
|
|
2016-05-20 17:43:47 -04:00
|
|
|
-- these globals are *not* shared between DSP and UI
|
2016-07-01 10:01:19 -04:00
|
|
|
local filters = {} -- the biquad filter instances (DSP)
|
|
|
|
local filt -- the biquad filter instance (GUI, response)
|
2016-07-07 10:45:10 -04:00
|
|
|
local cur = {0, 0, 0, 0, 0} -- current parameters
|
2016-05-21 10:24:15 -04:00
|
|
|
local lpf = 0.03 -- parameter low-pass filter time-constant
|
2016-07-01 10:01:19 -04:00
|
|
|
local chn = 0 -- channel/filter count
|
2016-05-20 17:43:47 -04:00
|
|
|
|
|
|
|
function dsp_init (rate)
|
2016-05-21 10:24:15 -04:00
|
|
|
self:shmem ():allocate (1) -- shared mem to tell the GUI the samplerate
|
2016-05-20 17:43:47 -04:00
|
|
|
local cfg = self:shmem ():to_int (0):array ()
|
|
|
|
cfg[1] = rate
|
2016-05-21 10:24:15 -04:00
|
|
|
-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
|
2016-05-20 17:43:47 -04:00
|
|
|
filt = ARDOUR.DSP.Biquad (rate) -- initialize filter
|
2016-05-21 10:24:15 -04:00
|
|
|
lpf = 13000 / rate -- interpolation time constant
|
|
|
|
end
|
|
|
|
|
2016-07-01 10:01:19 -04:00
|
|
|
function dsp_configure (ins, outs)
|
|
|
|
assert (ins:n_audio () == outs:n_audio ())
|
|
|
|
local cfg = self:shmem ():to_int (0):array ()
|
|
|
|
local rate = cfg[1]
|
|
|
|
chn = ins:n_audio ()
|
|
|
|
for c = 1, chn do
|
|
|
|
filters[c] = ARDOUR.DSP.Biquad (rate) -- initialize filters
|
|
|
|
end
|
2016-07-07 10:45:10 -04:00
|
|
|
cur = {0, 0, 0, 0, 0}
|
2016-07-01 10:01:19 -04:00
|
|
|
end
|
|
|
|
|
2016-05-21 10:24:15 -04:00
|
|
|
-- helper functions for parameter interpolation
|
|
|
|
function param_changed (ctrl)
|
2016-07-07 10:45:10 -04:00
|
|
|
if ctrl[2] == cur[2] and ctrl[3] == cur[3] and ctrl[4] == cur[4] and ctrl[5] == cur[5] then
|
2016-05-21 10:24:15 -04:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
2016-05-20 17:43:47 -04:00
|
|
|
end
|
|
|
|
|
2016-05-21 10:24:15 -04:00
|
|
|
function low_pass_filter_param (old, new, limit)
|
|
|
|
if math.abs (old - new) < limit then
|
|
|
|
return new
|
|
|
|
else
|
|
|
|
return old + lpf * (new - old)
|
|
|
|
end
|
|
|
|
end
|
2016-05-20 17:43:47 -04:00
|
|
|
|
|
|
|
-- apply parameters, re-compute filter coefficients if needed
|
|
|
|
function apply_params (ctrl)
|
2016-05-21 10:24:15 -04:00
|
|
|
if not param_changed (ctrl) then
|
|
|
|
return
|
2016-05-20 17:43:47 -04:00
|
|
|
end
|
|
|
|
|
2016-07-07 10:45:10 -04:00
|
|
|
if cur[2] ~= ctrl[2] then
|
2016-05-21 10:24:15 -04:00
|
|
|
-- reset filter state when type changes
|
|
|
|
filt:reset ()
|
2016-07-07 10:45:10 -04:00
|
|
|
for k = 2,5 do cur[k] = ctrl[k] end
|
2016-05-20 17:43:47 -04:00
|
|
|
else
|
2016-05-21 10:24:15 -04:00
|
|
|
-- low-pass filter ctrl parameter values, smooth transition
|
2016-07-07 10:45:10 -04:00
|
|
|
cur[3] = low_pass_filter_param (cur[3], ctrl[3], 0.1) -- gain/dB
|
|
|
|
cur[4] = low_pass_filter_param (cur[4], ctrl[4], 1.0) -- freq/Hz
|
|
|
|
cur[5] = low_pass_filter_param (cur[5], ctrl[5], 0.01) -- quality
|
2016-05-20 17:43:47 -04:00
|
|
|
end
|
|
|
|
|
2016-07-01 10:01:19 -04:00
|
|
|
for c = 1, chn do
|
2016-07-07 10:45:10 -04:00
|
|
|
filters[c]:compute (map_type (cur[2]), cur[4], cur[5], cur[3])
|
2016-07-01 10:01:19 -04:00
|
|
|
end
|
2016-05-20 17:43:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- the actual DSP callback
|
|
|
|
function dsp_run (ins, outs, n_samples)
|
2016-05-21 10:24:15 -04:00
|
|
|
local changed = false
|
|
|
|
local siz = n_samples
|
|
|
|
local off = 0
|
2016-07-07 10:45:10 -04:00
|
|
|
local ctrl = ctrl_data ()
|
2016-05-21 10:24:15 -04:00
|
|
|
|
|
|
|
-- if a parameter was changed, process at most 64 samples at a time
|
|
|
|
-- and interpolate parameters until the current settings match
|
|
|
|
-- the target values
|
2016-07-07 10:45:10 -04:00
|
|
|
if param_changed (ctrl) then
|
2016-05-21 10:24:15 -04:00
|
|
|
changed = true
|
|
|
|
siz = 64
|
2016-05-20 17:43:47 -04:00
|
|
|
end
|
2016-05-21 10:24:15 -04:00
|
|
|
|
|
|
|
while n_samples > 0 do
|
2016-07-07 10:45:10 -04:00
|
|
|
if changed then apply_params (ctrl) end
|
2016-05-21 10:24:15 -04:00
|
|
|
if siz > n_samples then siz = n_samples end
|
|
|
|
|
|
|
|
-- 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
|
2017-03-16 15:55:41 -04:00
|
|
|
if ins[c] == outs[c] then
|
2016-07-01 10:01:19 -04:00
|
|
|
filters[c]:run (ins[c]:offset (off), siz) -- in-place processing
|
2016-05-21 10:24:15 -04:00
|
|
|
else
|
|
|
|
-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
|
|
|
|
ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz)
|
2016-07-01 10:01:19 -04:00
|
|
|
filters[c]:run (outs[c]:offset (off), siz)
|
2016-05-21 10:24:15 -04:00
|
|
|
end
|
2016-05-20 17:43:47 -04:00
|
|
|
end
|
2016-05-21 10:24:15 -04:00
|
|
|
|
|
|
|
n_samples = n_samples - siz
|
|
|
|
off = off + siz
|
|
|
|
end
|
|
|
|
|
|
|
|
if changed then
|
|
|
|
-- notify display
|
|
|
|
self:queue_draw ()
|
2016-05-20 17:43:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
--- inline display
|
|
|
|
|
|
|
|
function round (n)
|
|
|
|
return math.floor (n + .5)
|
|
|
|
end
|
|
|
|
|
|
|
|
function freq_at_x (x, w)
|
2016-05-21 10:24:15 -04:00
|
|
|
-- x-axis pixel for given freq, power-scale
|
2016-05-20 17:43:47 -04:00
|
|
|
return 20 * 1000 ^ (x / w)
|
|
|
|
end
|
|
|
|
|
|
|
|
function x_at_freq (f, w)
|
2016-05-21 10:24:15 -04:00
|
|
|
-- frequency at given x-axis pixel
|
2016-05-20 17:43:47 -04:00
|
|
|
return w * math.log (f / 20.0) / math.log (1000.0)
|
|
|
|
end
|
|
|
|
|
|
|
|
function db_to_y (db, h)
|
2016-05-21 10:24:15 -04:00
|
|
|
-- y-axis gain mapping
|
2016-05-20 17:43:47 -04:00
|
|
|
if db < -20 then db = -20 end
|
|
|
|
if db > 20 then db = 20 end
|
|
|
|
return -.5 + 0.5 * h * (1 - db / 20)
|
|
|
|
end
|
|
|
|
|
|
|
|
function grid_db (ctx, w, h, db)
|
2016-05-21 10:24:15 -04:00
|
|
|
-- draw horizontal grid line
|
2016-05-20 17:43:47 -04:00
|
|
|
local y = -.5 + round (db_to_y (db, h))
|
|
|
|
ctx:move_to (0, y)
|
|
|
|
ctx:line_to (w, y)
|
|
|
|
ctx:stroke ()
|
|
|
|
end
|
|
|
|
|
|
|
|
function grid_freq (ctx, w, h, f)
|
2016-05-21 10:24:15 -04:00
|
|
|
-- draw vertical grid line
|
2016-05-20 17:43:47 -04:00
|
|
|
local x = -.5 + round (x_at_freq (f, w))
|
|
|
|
ctx:move_to (x, 0)
|
|
|
|
ctx:line_to (x, h)
|
|
|
|
ctx:stroke ()
|
|
|
|
end
|
|
|
|
|
|
|
|
function render_inline (ctx, w, max_h)
|
|
|
|
if not filt then
|
2016-05-21 10:24:15 -04:00
|
|
|
-- the GUI is separate from the DSP, but the GUI needs to know
|
|
|
|
-- the sample-rate that the DSP is using.
|
2016-05-20 17:43:47 -04:00
|
|
|
local shmem = self:shmem () -- get shared memory region
|
|
|
|
local cfg = shmem:to_int (0):array () -- "cast" into lua-table
|
2016-05-21 10:24:15 -04:00
|
|
|
-- instantiate filter (to calculate the transfer function's response)
|
2016-05-20 17:43:47 -04:00
|
|
|
filt = ARDOUR.DSP.Biquad (cfg[1])
|
|
|
|
end
|
|
|
|
|
2016-05-21 10:24:15 -04:00
|
|
|
-- set filter coefficients if they have changed
|
|
|
|
if param_changed (CtrlPorts:array ()) then
|
2016-07-07 10:45:10 -04:00
|
|
|
local ctrl = ctrl_data ()
|
|
|
|
for k = 2,5 do cur[k] = ctrl[k] end
|
|
|
|
filt:compute (map_type (cur[2]), cur[4], cur[5], cur[3])
|
2016-05-21 10:24:15 -04:00
|
|
|
end
|
2016-05-20 17:43:47 -04:00
|
|
|
|
|
|
|
-- calc height of inline display
|
2016-05-21 10:24:15 -04:00
|
|
|
local h = math.ceil (w * 10 / 16) -- use 16:10 aspect
|
|
|
|
h = 2 * round (h / 2) -- with an even number of vertical pixels
|
|
|
|
if (h > max_h) then h = max_h end -- but at most max-height
|
|
|
|
|
|
|
|
-- ctx is a http://cairographics.org/ context
|
|
|
|
-- http://manual.ardour.org/lua-scripting/class_reference/#Cairo:Context
|
2016-05-20 17:43:47 -04:00
|
|
|
|
|
|
|
-- clear background
|
|
|
|
ctx:rectangle (0, 0, w, h)
|
|
|
|
ctx:set_source_rgba (.2, .2, .2, 1.0)
|
|
|
|
ctx:fill ()
|
|
|
|
|
2016-05-21 10:24:15 -04:00
|
|
|
-- 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
|
2016-05-20 17:43:47 -04:00
|
|
|
ctx:set_line_width (1.0)
|
|
|
|
|
|
|
|
-- draw grid
|
|
|
|
local dash3 = C.DoubleVector ()
|
|
|
|
dash3:add ({1, 3})
|
2016-05-21 10:24:15 -04:00
|
|
|
ctx:set_dash (dash3, 2) -- dotted line
|
2016-05-20 17:43:47 -04:00
|
|
|
ctx:set_source_rgba (.5, .5, .5, .5)
|
|
|
|
grid_db (ctx, w, h, 0)
|
|
|
|
grid_db (ctx, w, h, 6)
|
|
|
|
grid_db (ctx, w, h, 12)
|
|
|
|
grid_db (ctx, w, h, 18)
|
|
|
|
grid_db (ctx, w, h, -6)
|
|
|
|
grid_db (ctx, w, h, -12)
|
|
|
|
grid_db (ctx, w, h, -18)
|
|
|
|
grid_freq (ctx, w, h, 100)
|
|
|
|
grid_freq (ctx, w, h, 1000)
|
|
|
|
grid_freq (ctx, w, h, 10000)
|
|
|
|
ctx:unset_dash ()
|
|
|
|
|
2016-05-21 10:24:15 -04:00
|
|
|
-- draw transfer function line
|
2016-05-20 17:43:47 -04:00
|
|
|
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))
|
|
|
|
for x = 1,w do
|
|
|
|
local db = filt:dB_at_freq (freq_at_x (x, w))
|
|
|
|
ctx:line_to (-.5 + x, db_to_y (db, h))
|
|
|
|
end
|
|
|
|
ctx:stroke_preserve ()
|
|
|
|
|
|
|
|
-- fill area to zero under the curve
|
|
|
|
ctx:line_to (w, -.5 + h * .5)
|
|
|
|
ctx:line_to (0, -.5 + h * .5)
|
|
|
|
ctx:close_path ()
|
|
|
|
ctx:set_source_rgba (.5, .5, .5, .5)
|
|
|
|
ctx:fill ()
|
|
|
|
|
|
|
|
return {w, h}
|
|
|
|
end
|