Skip silent sources on session-archive -- fixes #7699
[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 -------------------------------------------------------------------
11 -- this is a quick/dirty example filter: no de-click, no de-zipper,
12 -- no latency reporting,...
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 = 1000, 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
38 -- plugin instance state
39 local filters = {} -- the biquad filter instances
40 local chn = 0 -- configured channel count
41 local sample_rate = 0 -- configured sample-rate
42
43 -- cached control ports (keep track of changed)
44 local freq = 0
45 local qual = 0
46
47
48 -- dsp_init is called once when instantiating the plugin
49 function dsp_init (rate)
50         -- remember the sample-rate
51         sample_rate = rate
52 end
53
54 -- dsp_configure is called every time when the channel-count
55 -- changes, and at least once at the beginning.
56 function dsp_configure (ins, outs)
57         assert (ins:n_audio () == outs:n_audio ())
58
59         -- remember audio-channels
60         chn = ins:n_audio ()
61
62         -- set up filter instances for all channels
63         for c = 1, chn do
64                 filters[c] = {}
65                 for i = 1, max_stages do
66                         filters[c][i] = ARDOUR.DSP.Biquad (sample_rate)
67                 end
68         end
69 end
70
71
72 -- the actual process function, called every cycle
73 -- ins, outs are audio-data arrays
74 --   http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
75 -- n_samples are the number of samples to process
76 function dsp_run (ins, outs, n_samples)
77         -- make sure input and output count matches...
78         assert (#ins == #outs)
79         -- ...and matches the configured number of channels
80         assert (#ins == chn)
81
82         local ctrl = CtrlPorts:array() -- get control parameters as array
83         -- ctrl[1] ..  corresponds to the parameters given in in dsp_params()
84
85         -- test if the plugin-parameters have changed
86         if freq ~= ctrl[1] or qual ~= ctrl[2] then
87                 -- remember current settings
88                 freq = ctrl[1]
89                 qual = ctrl[2]
90                 -- re-compute the filter coefficients for all filters
91                 for c = 1, chn do -- for each channel
92                         for i = 1, max_stages do -- and for each filter stage
93                                 -- the parameters are    type,  frequency,  quality(bandwidth),  gain
94                                 -- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
95                                 -- for a list of available types, see
96                                 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.DSP.Biquad.Type
97                                 filters[c][i]:compute (ARDOUR.DSP.BiquadType.Notch, freq * i, qual * i, 0)
98                         end
99                 end
100         end
101
102         -- limit the number of process stages
103         local limit = math.floor (sample_rate / ( 2 * freq )) -- at most up to SR / 2
104         local stages = math.floor (ctrl['3']) -- current user-set parameter
105         if stages < 1 then stages = 1 end -- at least one stage...
106         if stages > max_stages then stages = max_stages end
107         if stages > limit then stages = limit end
108
109         -- process all channels
110         for c = 1, chn do
111                 -- when not processing in-place, copy the data from input to output first
112                 if ins[c] ~= outs[c] then
113                         ARDOUR.DSP.copy_vector (outs[c], ins[c], n_samples)
114                 end
115
116                 -- run all stages, in-place on the output buffer
117                 for i = 1, stages do
118                         filters[c][i]:run (outs[c], n_samples)
119                 end
120         end
121 end