13
0
livetrax/share/scripts/notch_bank.lua

126 lines
3.9 KiB
Lua
Raw Permalink Normal View History

2016-08-27 11:48:11 -04:00
ardour {
["type"] = "dsp",
name = "ACE Notch Bank",
2019-04-18 08:11:52 -04:00
category = "Filter",
2016-08-27 11:48:11 -04:00
license = "MIT",
author = "Ardour Community",
description = [[Notch Filter Bank; useful to remove noise with a harmonic spectrum (e.g, mains hum, GSM signals, charge-pump noise, etc).
2019-04-18 08:11:52 -04:00
Note: this plugin is not suitable to be automated, it is intended for static noise only.]]
2016-08-27 11:48:11 -04:00
}
2019-04-18 08:11:52 -04:00
------------------------------------------------------------------
-- this is a quick/dirty example filter: no de-click, no de-zipper
2016-08-27 17:35:02 -04:00
-------------------------------------------------------------------
-- configuration
local max_stages = 100
-- plugin i/o ports
2016-08-27 11:48:11 -04:00
function dsp_ioconfig ()
return
{
-- allow any number of I/O as long as port-count matches
{ audio_in = -1, audio_out = -1},
2016-08-27 11:48:11 -04:00
}
end
2016-08-27 17:35:02 -04:00
-- plugin control ports
2016-08-27 11:48:11 -04:00
function dsp_params ()
return
{
2019-04-18 08:11:52 -04:00
{ ["type"] = "input", name = "Base Freq", min = 10, max = 2000, default = 100, unit="Hz", logarithmic = true },
{ ["type"] = "input", name = "Quality", min = 1.0, max = 100.0, default = 8.0, logarithmic = true },
2016-08-27 17:35:02 -04:00
{ ["type"] = "input", name = "Stages", min = 1.0, max = max_stages, default = 8.0, integer = true },
2016-08-27 11:48:11 -04:00
}
end
2016-08-27 17:35:02 -04:00
-- plugin instance state
2016-08-27 11:48:11 -04:00
local filters = {} -- the biquad filter instances
2016-08-27 17:35:02 -04:00
local chn = 0 -- configured channel count
local sample_rate = 0 -- configured sample-rate
2019-04-18 08:11:52 -04:00
local limit = 0 -- max number of stages (given freq & sample-rate)
2016-08-27 17:35:02 -04:00
-- cached control ports (keep track of changed)
2016-08-27 11:48:11 -04:00
local freq = 0
local qual = 0
2016-08-27 11:48:11 -04:00
2016-08-27 17:35:02 -04:00
-- dsp_init is called once when instantiating the plugin
2016-08-27 11:48:11 -04:00
function dsp_init (rate)
2016-08-27 17:35:02 -04:00
-- remember the sample-rate
sample_rate = rate
end
2016-08-27 17:35:02 -04:00
-- dsp_configure is called every time when the channel-count
-- changes, and at least once at the beginning.
function dsp_configure (ins, outs)
assert (ins:n_audio () == outs:n_audio ())
2016-08-27 17:35:02 -04:00
2019-04-18 08:11:52 -04:00
-- explicit cleanup
filters = {}
collectgarbage ()
2016-08-27 17:35:02 -04:00
-- remember audio-channels
chn = ins:n_audio ()
2016-08-27 17:35:02 -04:00
-- set up filter instances for all channels
for c = 1, chn do
filters[c] = {}
for i = 1, max_stages do
filters[c][i] = ARDOUR.DSP.Biquad (sample_rate)
end
2016-08-27 11:48:11 -04:00
end
end
2016-08-27 17:35:02 -04:00
-- the actual process function, called every cycle
-- ins, outs are audio-data arrays
-- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
-- n_samples are the number of samples to process
2016-08-27 11:48:11 -04:00
function dsp_run (ins, outs, n_samples)
2016-08-27 17:35:02 -04:00
-- make sure input and output count matches...
assert (#ins == #outs)
-- ...and matches the configured number of channels
assert (#ins == chn)
2016-08-27 11:48:11 -04:00
2019-04-18 08:11:52 -04:00
local ctrl = CtrlPorts:array () -- get control parameters as array
-- ctrl[] .. correspond to the parameters given in in dsp_params()
2016-08-27 11:48:11 -04:00
2016-08-27 17:35:02 -04:00
-- test if the plugin-parameters have changed
if freq ~= ctrl[1] or qual ~= ctrl[2] then
2016-08-27 17:35:02 -04:00
-- remember current settings
2016-08-27 11:48:11 -04:00
freq = ctrl[1]
qual = ctrl[2]
2019-04-18 08:11:52 -04:00
-- calc max number of states to configure/process
limit = math.floor (sample_rate / (2 * freq)) -- at most up to SR / 2
if limit > max_stages then limit = max_stages end
2016-08-27 17:35:02 -04:00
-- re-compute the filter coefficients for all filters
for c = 1, chn do -- for each channel
2019-04-18 08:11:52 -04:00
for i = 1, limit do -- and for each filter stage
2016-08-27 17:35:02 -04:00
-- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
2019-04-18 08:11:52 -04:00
-- and for a list of available types, see
2016-08-27 17:35:02 -04:00
-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.DSP.Biquad.Type
2019-04-18 08:11:52 -04:00
-- the parameters are type, frequency, quality(bandwidth), gain
filters[c][i]:compute (ARDOUR.DSP.BiquadType.Notch, freq * i, qual * i, 0)
end
2016-08-27 11:48:11 -04:00
end
end
2016-08-27 17:35:02 -04:00
-- limit the number of process stages
local stages = math.floor (ctrl['3']) -- current user-set parameter
if stages < 1 then stages = 1 end -- at least one stage...
if stages > limit then stages = limit end
-- process all channels
for c = 1, chn do
2016-08-27 17:35:02 -04:00
-- when not processing in-place, copy the data from input to output first
2017-03-16 15:55:41 -04:00
if ins[c] ~= outs[c] then
2016-08-27 18:02:16 -04:00
ARDOUR.DSP.copy_vector (outs[c], ins[c], n_samples)
end
2016-08-27 11:48:11 -04:00
2016-08-27 17:35:02 -04:00
-- run all stages, in-place on the output buffer
for i = 1, stages do
filters[c][i]:run (outs[c], n_samples)
end
2016-08-27 11:48:11 -04:00
end
end