Release notch-bank filter
[ardour.git] / scripts / notch_bank.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "a-Notch Bank",
4         category    = "Filter",
5         license     = "MIT",
6         author      = "Ardour Lua Task Force",
7         description = [[Notch Filter Bank; useful to remove noise with a harmonic spectum (e.g, mains hum, GSM signals, charge-pump noise, etc).
8 Note: this plugin is not suitable to be automated, it is intended for static noise only.]]
9 }
10
11 ------------------------------------------------------------------
12 -- this is a quick/dirty example filter: no de-click, no de-zipper
13 -------------------------------------------------------------------
14
15 -- configuration
16 local max_stages = 100
17
18 -- plugin i/o ports
19 function dsp_ioconfig ()
20         return
21         {
22                 -- allow any number of I/O as long as port-count matches
23                 { audio_in = -1, audio_out = -1},
24         }
25 end
26
27 -- plugin control ports
28 function dsp_params ()
29         return
30         {
31                 { ["type"] = "input", name = "Base Freq", min = 10, max = 2000, default = 100, unit="Hz", logarithmic = true },
32                 { ["type"] = "input", name = "Quality", min = 1.0, max = 100.0, default = 8.0, logarithmic = true },
33                 { ["type"] = "input", name = "Stages", min = 1.0, max = max_stages, default = 8.0, integer = true },
34         }
35 end
36
37 -- plugin instance state
38 local filters = {} -- the biquad filter instances
39 local chn = 0 -- configured channel count
40 local sample_rate = 0 -- configured sample-rate
41 local limit = 0 -- max number of stages (given freq & sample-rate)
42
43 -- cached control ports (keep track of changed)
44 local freq = 0
45 local qual = 0
46
47 -- dsp_init is called once when instantiating the plugin
48 function dsp_init (rate)
49         -- remember the sample-rate
50         sample_rate = rate
51 end
52
53 -- dsp_configure is called every time when the channel-count
54 -- changes, and at least once at the beginning.
55 function dsp_configure (ins, outs)
56         assert (ins:n_audio () == outs:n_audio ())
57
58         -- explicit cleanup
59         filters = {}
60         collectgarbage ()
61
62         -- remember audio-channels
63         chn = ins:n_audio ()
64
65         -- set up filter instances for all channels
66         for c = 1, chn do
67                 filters[c] = {}
68                 for i = 1, max_stages do
69                         filters[c][i] = ARDOUR.DSP.Biquad (sample_rate)
70                 end
71         end
72 end
73
74 -- the actual process function, called every cycle
75 -- ins, outs are audio-data arrays
76 --   http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
77 -- n_samples are the number of samples to process
78 function dsp_run (ins, outs, n_samples)
79         -- make sure input and output count matches...
80         assert (#ins == #outs)
81         -- ...and matches the configured number of channels
82         assert (#ins == chn)
83
84         local ctrl = CtrlPorts:array () -- get control parameters as array
85         -- ctrl[] .. correspond to the parameters given in in dsp_params()
86
87         -- test if the plugin-parameters have changed
88         if freq ~= ctrl[1] or qual ~= ctrl[2] then
89                 -- remember current settings
90                 freq = ctrl[1]
91                 qual = ctrl[2]
92                 -- calc max number of states to configure/process
93                 limit = math.floor (sample_rate / (2 * freq)) -- at most up to SR / 2
94                 if limit > max_stages then limit = max_stages end
95
96                 -- re-compute the filter coefficients for all filters
97                 for c = 1, chn do -- for each channel
98                         for i = 1, limit do -- and for each filter stage
99                                 -- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
100                                 -- and for a list of available types, see
101                                 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.DSP.Biquad.Type
102                                 -- the parameters are  type, frequency, quality(bandwidth), gain
103                                 filters[c][i]:compute (ARDOUR.DSP.BiquadType.Notch, freq * i, qual * i, 0)
104                         end
105                 end
106         end
107
108         -- limit the number of process stages
109         local stages = math.floor (ctrl['3']) -- current user-set parameter
110         if stages < 1 then stages = 1 end -- at least one stage...
111         if stages > limit then stages = limit end
112
113         -- process all channels
114         for c = 1, chn do
115                 -- when not processing in-place, copy the data from input to output first
116                 if ins[c] ~= outs[c] then
117                         ARDOUR.DSP.copy_vector (outs[c], ins[c], n_samples)
118                 end
119
120                 -- run all stages, in-place on the output buffer
121                 for i = 1, stages do
122                         filters[c][i]:run (outs[c], n_samples)
123                 end
124         end
125 end