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 filters = {} -- the biquad filter instances (DSP)
57 local filt -- the biquad filter instance (GUI, response)
58 local cur = {0, 0, 0, 0} -- current parameters
59 local lpf = 0.03 -- parameter low-pass filter time-constant
60 local chn = 0 -- channel/filter count
62 function dsp_init (rate)
63 self:shmem ():allocate (1) -- shared mem to tell the GUI the samplerate
64 local cfg = self:shmem ():to_int (0):array ()
66 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
67 filt = ARDOUR.DSP.Biquad (rate) -- initialize filter
68 lpf = 13000 / rate -- interpolation time constant
71 function dsp_configure (ins, outs)
72 assert (ins:n_audio () == outs:n_audio ())
73 local cfg = self:shmem ():to_int (0):array ()
77 filters[c] = ARDOUR.DSP.Biquad (rate) -- initialize filters
82 -- helper functions for parameter interpolation
83 function param_changed (ctrl)
84 if ctrl[1] == cur[1] and ctrl[2] == cur[2] and ctrl[3] == cur[3] and ctrl[4] == cur[4] then
90 function low_pass_filter_param (old, new, limit)
91 if math.abs (old - new) < limit then
94 return old + lpf * (new - old)
98 -- apply parameters, re-compute filter coefficients if needed
99 function apply_params (ctrl)
100 if not param_changed (ctrl) then
104 if cur[1] ~= ctrl[1] then
105 -- reset filter state when type changes
107 for k = 1,4 do cur[k] = ctrl[k] end
109 -- low-pass filter ctrl parameter values, smooth transition
110 cur[2] = low_pass_filter_param (cur[2], ctrl[2], 0.1) -- gain/dB
111 cur[3] = low_pass_filter_param (cur[3], ctrl[3], 1.0) -- freq/Hz
112 cur[4] = low_pass_filter_param (cur[4], ctrl[4], 0.01) -- quality
116 filters[c]:compute (map_type (cur[1]), cur[3], cur[4], cur[2])
121 -- the actual DSP callback
122 function dsp_run (ins, outs, n_samples)
123 local changed = false
124 local siz = n_samples
127 -- if a parameter was changed, process at most 64 samples at a time
128 -- and interpolate parameters until the current settings match
130 if param_changed (CtrlPorts:array ()) then
135 while n_samples > 0 do
136 if changed then apply_params (CtrlPorts:array ()) end
137 if siz > n_samples then siz = n_samples end
139 -- process all channels
141 -- check if output and input buffers for this channel are identical
142 -- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
143 if ins[c]:sameinstance (outs[c]) then
144 filters[c]:run (ins[c]:offset (off), siz) -- in-place processing
146 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
147 ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz)
148 filters[c]:run (outs[c]:offset (off), siz)
152 n_samples = n_samples - siz
163 -------------------------------------------------------------------------------
167 return math.floor (n + .5)
170 function freq_at_x (x, w)
171 -- x-axis pixel for given freq, power-scale
172 return 20 * 1000 ^ (x / w)
175 function x_at_freq (f, w)
176 -- frequency at given x-axis pixel
177 return w * math.log (f / 20.0) / math.log (1000.0)
180 function db_to_y (db, h)
181 -- y-axis gain mapping
182 if db < -20 then db = -20 end
183 if db > 20 then db = 20 end
184 return -.5 + 0.5 * h * (1 - db / 20)
187 function grid_db (ctx, w, h, db)
188 -- draw horizontal grid line
189 local y = -.5 + round (db_to_y (db, h))
195 function grid_freq (ctx, w, h, f)
196 -- draw vertical grid line
197 local x = -.5 + round (x_at_freq (f, w))
203 function render_inline (ctx, w, max_h)
205 -- the GUI is separate from the DSP, but the GUI needs to know
206 -- the sample-rate that the DSP is using.
207 local shmem = self:shmem () -- get shared memory region
208 local cfg = shmem:to_int (0):array () -- "cast" into lua-table
209 -- instantiate filter (to calculate the transfer function's response)
210 filt = ARDOUR.DSP.Biquad (cfg[1])
213 -- set filter coefficients if they have changed
214 if param_changed (CtrlPorts:array ()) then
215 local ctrl = CtrlPorts:array ()
216 for k = 1,4 do cur[k] = ctrl[k] end
217 filt:compute (map_type (cur[1]), cur[3], cur[4], cur[2])
220 -- calc height of inline display
221 local h = math.ceil (w * 10 / 16) -- use 16:10 aspect
222 h = 2 * round (h / 2) -- with an even number of vertical pixels
223 if (h > max_h) then h = max_h end -- but at most max-height
225 -- ctx is a http://cairographics.org/ context
226 -- http://manual.ardour.org/lua-scripting/class_reference/#Cairo:Context
229 ctx:rectangle (0, 0, w, h)
230 ctx:set_source_rgba (.2, .2, .2, 1.0)
233 -- set line width: 1px
234 -- Note: a cairo pixel at [1,1] spans [0.5->1.5 , 0.5->1.5]
235 -- hence the offset -0.5 in various move_to(), line_to() calls
236 ctx:set_line_width (1.0)
239 local dash3 = C.DoubleVector ()
241 ctx:set_dash (dash3, 2) -- dotted line
242 ctx:set_source_rgba (.5, .5, .5, .5)
243 grid_db (ctx, w, h, 0)
244 grid_db (ctx, w, h, 6)
245 grid_db (ctx, w, h, 12)
246 grid_db (ctx, w, h, 18)
247 grid_db (ctx, w, h, -6)
248 grid_db (ctx, w, h, -12)
249 grid_db (ctx, w, h, -18)
250 grid_freq (ctx, w, h, 100)
251 grid_freq (ctx, w, h, 1000)
252 grid_freq (ctx, w, h, 10000)
255 -- draw transfer function line
256 ctx:set_source_rgba (.8, .8, .8, 1.0)
257 ctx:move_to (-.5, db_to_y (filt:dB_at_freq (freq_at_x (0, w)), h))
259 local db = filt:dB_at_freq (freq_at_x (x, w))
260 ctx:line_to (-.5 + x, db_to_y (db, h))
262 ctx:stroke_preserve ()
264 -- fill area to zero under the curve
265 ctx:line_to (w, -.5 + h * .5)
266 ctx:line_to (0, -.5 + h * .5)
268 ctx:set_source_rgba (.5, .5, .5, .5)