3 name = "Biquad Filter",
6 author = "Robin Gareus",
7 email = "robin@gareus.org",
8 site = "http://gareus.org",
9 description = [[Example Ardour Lua DSP Plugin]]
12 function dsp_ioconfig ()
15 -- allow any number of I/O as long as port-count matches
16 { audio_in = -1, audio_out = -1},
21 function dsp_params ()
24 { ["type"] = "input", name = "Type", min = 0, max = 4, default = 0, enum = true, scalepoints =
33 { ["type"] = "input", name = "Gain", min = -20, max = 20, default = 0, unit="dB" },
34 { ["type"] = "input", name = "Freq", min = 20, max = 20000, default = 1000, unit="Hz", logarithmic = true },
35 { ["type"] = "input", name = "Q", min = 0.1, max = 8, default = .707, logarithmic = true },
39 -- translate type parameter to DSP enum
40 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.DSP.Biquad.Type
43 return ARDOUR.DSP.BiquadType.LowShelf
45 return ARDOUR.DSP.BiquadType.HighShelf
47 return ARDOUR.DSP.BiquadType.LowPass
49 return ARDOUR.DSP.BiquadType.HighPass
51 return ARDOUR.DSP.BiquadType.Peaking
55 -- these globals are *not* shared between DSP and UI
56 local filt -- the biquad filter instance
57 local cur = {0, 0, 0, 0} -- current parameters
58 local lpf = 0.03 -- parameter low-pass filter time-constant
60 function dsp_init (rate)
61 self:shmem ():allocate (1) -- shared mem to tell the GUI the samplerate
62 local cfg = self:shmem ():to_int (0):array ()
64 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
65 filt = ARDOUR.DSP.Biquad (rate) -- initialize filter
66 lpf = 13000 / rate -- interpolation time constant
69 -- helper functions for parameter interpolation
70 function param_changed (ctrl)
71 if ctrl[1] == cur[1] and ctrl[2] == cur[2] and ctrl[3] == cur[3] and ctrl[4] == cur[4] then
77 function low_pass_filter_param (old, new, limit)
78 if math.abs (old - new) < limit then
81 return old + lpf * (new - old)
85 -- apply parameters, re-compute filter coefficients if needed
86 function apply_params (ctrl)
87 if not param_changed (ctrl) then
91 if cur[1] ~= ctrl[1] then
92 -- reset filter state when type changes
94 for k = 1,4 do cur[k] = ctrl[k] end
96 -- low-pass filter ctrl parameter values, smooth transition
97 cur[2] = low_pass_filter_param (cur[2], ctrl[2], 0.1) -- gain/dB
98 cur[3] = low_pass_filter_param (cur[3], ctrl[3], 1.0) -- freq/Hz
99 cur[4] = low_pass_filter_param (cur[4], ctrl[4], 0.01) -- quality
102 filt:compute (map_type (cur[1]), cur[3], cur[4], cur[2])
106 -- the actual DSP callback
107 function dsp_run (ins, outs, n_samples)
108 local changed = false
109 local siz = n_samples
112 -- if a parameter was changed, process at most 64 samples at a time
113 -- and interpolate parameters until the current settings match
115 if param_changed (CtrlPorts:array ()) then
120 while n_samples > 0 do
121 if changed then apply_params (CtrlPorts:array ()) end
122 if siz > n_samples then siz = n_samples end
124 -- process all channels
126 -- check if output and input buffers for this channel are identical
127 -- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
128 if ins[c]:sameinstance (outs[c]) then
129 filt:run (ins[c]:offset (off), siz) -- in-place processing
131 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
132 ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz)
133 filt:run (outs[c]:offset (off), siz)
137 n_samples = n_samples - siz
148 -------------------------------------------------------------------------------
152 return math.floor (n + .5)
155 function freq_at_x (x, w)
156 -- x-axis pixel for given freq, power-scale
157 return 20 * 1000 ^ (x / w)
160 function x_at_freq (f, w)
161 -- frequency at given x-axis pixel
162 return w * math.log (f / 20.0) / math.log (1000.0)
165 function db_to_y (db, h)
166 -- y-axis gain mapping
167 if db < -20 then db = -20 end
168 if db > 20 then db = 20 end
169 return -.5 + 0.5 * h * (1 - db / 20)
172 function grid_db (ctx, w, h, db)
173 -- draw horizontal grid line
174 local y = -.5 + round (db_to_y (db, h))
180 function grid_freq (ctx, w, h, f)
181 -- draw vertical grid line
182 local x = -.5 + round (x_at_freq (f, w))
188 function render_inline (ctx, w, max_h)
190 -- the GUI is separate from the DSP, but the GUI needs to know
191 -- the sample-rate that the DSP is using.
192 local shmem = self:shmem () -- get shared memory region
193 local cfg = shmem:to_int (0):array () -- "cast" into lua-table
194 -- instantiate filter (to calculate the transfer function's response)
195 filt = ARDOUR.DSP.Biquad (cfg[1])
198 -- set filter coefficients if they have changed
199 if param_changed (CtrlPorts:array ()) then
200 local ctrl = CtrlPorts:array ()
201 for k = 1,4 do cur[k] = ctrl[k] end
202 filt:compute (map_type (cur[1]), cur[3], cur[4], cur[2])
205 -- calc height of inline display
206 local h = math.ceil (w * 10 / 16) -- use 16:10 aspect
207 h = 2 * round (h / 2) -- with an even number of vertical pixels
208 if (h > max_h) then h = max_h end -- but at most max-height
210 -- ctx is a http://cairographics.org/ context
211 -- http://manual.ardour.org/lua-scripting/class_reference/#Cairo:Context
214 ctx:rectangle (0, 0, w, h)
215 ctx:set_source_rgba (.2, .2, .2, 1.0)
218 -- set line width: 1px
219 -- Note: a cairo pixel at [1,1] spans [0.5->1.5 , 0.5->1.5]
220 -- hence the offset -0.5 in various move_to(), line_to() calls
221 ctx:set_line_width (1.0)
224 local dash3 = C.DoubleVector ()
226 ctx:set_dash (dash3, 2) -- dotted line
227 ctx:set_source_rgba (.5, .5, .5, .5)
228 grid_db (ctx, w, h, 0)
229 grid_db (ctx, w, h, 6)
230 grid_db (ctx, w, h, 12)
231 grid_db (ctx, w, h, 18)
232 grid_db (ctx, w, h, -6)
233 grid_db (ctx, w, h, -12)
234 grid_db (ctx, w, h, -18)
235 grid_freq (ctx, w, h, 100)
236 grid_freq (ctx, w, h, 1000)
237 grid_freq (ctx, w, h, 10000)
240 -- draw transfer function line
241 ctx:set_source_rgba (.8, .8, .8, 1.0)
242 ctx:move_to (-.5, db_to_y (filt:dB_at_freq (freq_at_x (0, w)), h))
244 local db = filt:dB_at_freq (freq_at_x (x, w))
245 ctx:line_to (-.5 + x, db_to_y (db, h))
247 ctx:stroke_preserve ()
249 -- fill area to zero under the curve
250 ctx:line_to (w, -.5 + h * .5)
251 ctx:line_to (0, -.5 + h * .5)
253 ctx:set_source_rgba (.5, .5, .5, .5)