notch-bank: increase max Q, use logscale
[ardour.git] / scripts / _notch_bank.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "Notch Bank",
4         category    = "Example",
5         license     = "MIT",
6         author      = "Ardour Lua Task Force",
7         description = [[An Example Filter Plugin]]
8 }
9
10 function dsp_ioconfig ()
11         return
12         {
13                 -- allow any number of I/O as long as port-count matches
14                 { audio_in = -1, audio_out = -1},
15         }
16 end
17
18 function dsp_params ()
19         return
20         {
21                 { ["type"] = "input", name = "Base Freq", min = 10, max = 1000, default = 100, unit="Hz", logarithmic = true },
22                 { ["type"] = "input", name = "Quality", min = 1.0, max = 100.0, default = 8.0, logarithmic = true },
23                 { ["type"] = "input", name = "Stages", min = 1.0, max = 100, default = 8.0, integer = true },
24         }
25 end
26
27 local filters = {} -- the biquad filter instances
28 local sample_rate = 0
29 local chn = 0 -- channel count
30 local max_stages = 100
31 local freq = 0
32 local qual = 0
33
34 function dsp_init (rate)
35         sample_rate = rate
36 end
37
38 function dsp_configure (ins, outs)
39         assert (ins:n_audio () == outs:n_audio ())
40         chn = ins:n_audio ()
41         for c = 1, chn do
42                 filters[c] = {}
43                 for i = 1, max_stages do
44                         filters[c][i] = ARDOUR.DSP.Biquad (sample_rate)
45                 end
46         end
47 end
48
49 function dsp_run (ins, outs, n_samples)
50         assert (#ins == chn)
51         assert (n_samples < 8192)
52
53         -- this is quick/dirty: no declick, no de-zipper, no latency reporting,...
54         -- and no documentation :)
55
56         local ctrl = CtrlPorts:array() -- get control parameters
57         if freq ~= ctrl[1] or qual ~= ctrl[2] then
58                 freq = ctrl[1]
59                 qual = ctrl[2]
60                 for c = 1, chn do
61                         for i = 1, max_stages do
62                                 filters[c][i]:compute (ARDOUR.DSP.BiquadType.Notch, freq * i, qual, 0)
63                         end
64                 end
65         end
66
67         local limit = math.floor (sample_rate / ( 2 * freq ))
68         local stages = math.floor (ctrl['3'])
69         if stages < 1 then stages = 1 end
70         if stages > max_stages then stages = max_stages end
71         if stages > limit then stages = limit end
72
73         -- process all channels
74         for c = 1, chn do
75                 if not ins[c]:sameinstance (outs[c]) then
76                         ARDOUR.DSP.copy_vector (outs[c], outs[c], n_samples)
77                 end
78
79                 for i = 1, stages do
80                         filters[c][i]:run (outs[c], n_samples)
81                 end
82         end
83 end