3 name = "a-High and Low Pass Filter",
6 author = "Ardour Team",
7 description = [[Example Ardour Lua DSP Plugin]]
10 function dsp_ioconfig ()
13 -- allow any number of I/O as long as port-count matches
14 { audio_in = -1, audio_out = -1},
19 function dsp_params ()
22 { ["type"] = "input", name = "High Pass Steepness", min = 0, max = 4, default = 1, enum = true, scalepoints =
31 { ["type"] = "input", name = "High Pass Cut off frequency", min = 5, max = 20000, default = 100, unit="Hz", logarithmic = true },
32 { ["type"] = "input", name = "High Pass Resonance", min = 0.1, max = 6, default = .707, logarithmic = true },
34 { ["type"] = "input", name = "Low Pass Steepness", min = 0, max = 4, default = 1, enum = true, scalepoints =
43 { ["type"] = "input", name = "Low Pass Cut off frequency", min = 20, max = 20000, default = 18000, unit="Hz", logarithmic = true },
44 { ["type"] = "input", name = "Low Pass Resonance", min = 0.1, max = 6, default = .707, logarithmic = true },
48 -- these globals are *not* shared between DSP and UI
49 local hp = {} -- the biquad high-pass filter instances (DSP)
50 local lp = {} -- the biquad high-pass filter instances (DSP)
51 local filt = nil -- the biquad filter instance (GUI, response)
52 local cur = {0, 0, 0, 0, 0, 0, 0} -- current parameters
53 local lpf = 0.03 -- parameter low-pass filter time-constant
54 local chn = 0 -- channel/filter count
56 local mem = nil -- memory x-fade buffer
58 function dsp_init (rate)
59 -- allocate some mix-buffer
60 mem = ARDOUR.DSP.DspShm (8192)
62 -- create a table of objects to share with the GUI
65 tbl['samplerate'] = rate
66 self:table ():set (tbl)
68 -- interpolation time constant
72 function dsp_configure (ins, outs)
73 assert (ins:n_audio () == outs:n_audio ())
74 local tbl = self:table ():get () -- get shared memory table
75 local rate = tbl['samplerate']
83 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
85 hp[c][k] = ARDOUR.DSP.Biquad (rate)
86 lp[c][k] = ARDOUR.DSP.Biquad (rate)
89 cur = {0, 0, 0, 0, 0, 0}
92 -- helper functions for parameter interpolation
93 function param_changed (ctrl)
95 if ctrl[p] ~= cur[p] then
102 function low_pass_filter_param (old, new, limit)
103 if math.abs (old - new) < limit then
106 return old + lpf * (new - old)
110 -- apply parameters, re-compute filter coefficients if needed
111 function apply_params (ctrl)
112 if not param_changed (ctrl) then
116 -- low-pass filter ctrl parameter values, smooth transition
117 cur[1] = low_pass_filter_param (cur[1], ctrl[1], 0.05) -- HP order x-fade
118 cur[2] = low_pass_filter_param (cur[2], ctrl[2], 1.0) -- HP freq/Hz
119 cur[3] = low_pass_filter_param (cur[3], ctrl[3], 0.01) -- HP quality
120 cur[4] = low_pass_filter_param (cur[4], ctrl[4], 0.05) -- LP order x-fade
121 cur[5] = low_pass_filter_param (cur[5], ctrl[5], 1.0) -- LP freq/Hz
122 cur[6] = low_pass_filter_param (cur[6], ctrl[6], 0.01) -- LP quality
126 hp[c][k]:compute (ARDOUR.DSP.BiquadType.HighPass, cur[2], cur[3], 0)
127 lp[c][k]:compute (ARDOUR.DSP.BiquadType.LowPass, cur[5], cur[6], 0)
133 -- the actual DSP callback
134 function dsp_run (ins, outs, n_samples)
135 assert (n_samples < 8192)
136 local changed = false
137 local siz = n_samples
140 -- if a parameter was changed, process at most 64 samples at a time
141 -- and interpolate parameters until the current settings match
143 if param_changed (CtrlPorts:array ()) then
148 while n_samples > 0 do
149 if changed then apply_params (CtrlPorts:array ()) end
150 if siz > n_samples then siz = n_samples end
152 local ho = math.floor(cur[1])
153 local lo = math.floor(cur[4])
157 -- process all channels
160 local xfade = hox - ho
161 assert (xfade >= 0 and xfade < 1)
163 ARDOUR.DSP.copy_vector (mem:to_float (off), ins[c]:offset (off), siz)
167 ARDOUR.DSP.copy_vector (outs[c]:offset (off), mem:to_float (off), siz)
169 ARDOUR.DSP.memset (outs[c]:offset (off), 0, siz)
173 if xfade > 0 and k > ho and k <= ho + 1 then
174 ARDOUR.DSP.mix_buffers_with_gain (outs[c]:offset (off), mem:to_float (off), siz, 1 - xfade)
177 hp[c][k]:run (mem:to_float (off), siz)
179 if k == ho and xfade == 0 then
180 ARDOUR.DSP.copy_vector (outs[c]:offset (off), mem:to_float (off), siz)
181 elseif k > ho and k <= ho + 1 then
182 ARDOUR.DSP.mix_buffers_with_gain (outs[c]:offset (off), mem:to_float (off), siz, xfade)
188 assert (xfade >= 0 and xfade < 1)
190 ARDOUR.DSP.copy_vector (mem:to_float (off), outs[c]:offset (off), siz)
193 ARDOUR.DSP.memset (outs[c]:offset (off), 0, siz)
197 if xfade > 0 and k > lo and k <= lo + 1 then
198 ARDOUR.DSP.mix_buffers_with_gain (outs[c]:offset (off), mem:to_float (off), siz, 1 - xfade)
201 lp[c][k]:run (mem:to_float (off), siz)
203 if k == lo and xfade == 0 then
204 ARDOUR.DSP.copy_vector (outs[c]:offset (off), mem:to_float (off), siz)
205 elseif k > lo and k <= lo + 1 then
206 ARDOUR.DSP.mix_buffers_with_gain (outs[c]:offset (off), mem:to_float (off), siz, xfade)
212 n_samples = n_samples - siz
223 -------------------------------------------------------------------------------
227 return math.floor (n + .5)
230 function freq_at_x (x, w)
231 -- x-axis pixel for given freq, power-scale
232 return 20 * 1000 ^ (x / w)
235 function x_at_freq (f, w)
236 -- frequency at given x-axis pixel
237 return w * math.log (f / 20.0) / math.log (1000.0)
240 function db_to_y (db, h)
241 -- y-axis gain mapping
242 if db < -60 then db = -60 end
243 if db > 12 then db = 12 end
244 return -.5 + round (0.2 * h) - h * db / 60
247 function grid_db (ctx, w, h, db)
248 -- draw horizontal grid line
249 local y = -.5 + round (db_to_y (db, h))
255 function grid_freq (ctx, w, h, f)
256 -- draw vertical grid line
257 local x = -.5 + round (x_at_freq (f, w))
263 function response (ho, lo, f)
264 local db = ho * filt['hp']:dB_at_freq (f)
265 return db + lo * filt['lp']:dB_at_freq (f)
268 function render_inline (ctx, w, max_h)
270 local tbl = self:table ():get () -- get shared memory table
271 -- instantiate filter (to calculate the transfer function's response)
273 filt['hp'] = ARDOUR.DSP.Biquad (tbl['samplerate'])
274 filt['lp'] = ARDOUR.DSP.Biquad (tbl['samplerate'])
277 -- set filter coefficients if they have changed
278 if param_changed (CtrlPorts:array ()) then
279 local ctrl = CtrlPorts:array ()
280 for k = 1,6 do cur[k] = ctrl[k] end
281 filt['hp']:compute (ARDOUR.DSP.BiquadType.HighPass, cur[2], cur[3], 0)
282 filt['lp']:compute (ARDOUR.DSP.BiquadType.LowPass, cur[5], cur[6], 0)
285 -- calc height of inline display
286 local h = 1 | math.ceil (w * 9 / 16) -- use 16:9 aspect, odd number of y pixels
287 if (h > max_h) then h = max_h end -- but at most max-height
289 -- ctx is a http://cairographics.org/ context
290 -- http://manual.ardour.org/lua-scripting/class_reference/#Cairo:Context
293 ctx:rectangle (0, 0, w, h)
294 ctx:set_source_rgba (.2, .2, .2, 1.0)
296 ctx:rectangle (0, 0, w, h)
299 -- set line width: 1px
300 -- Note: a cairo pixel at [1,1] spans [0.5->1.5 , 0.5->1.5]
301 -- hence the offset -0.5 in various move_to(), line_to() calls
302 ctx:set_line_width (1.0)
305 local dash3 = C.DoubleVector ()
306 local dash2 = C.DoubleVector ()
309 ctx:set_dash (dash2, 2) -- dotted line
310 ctx:set_source_rgba (.5, .5, .5, .8)
311 grid_db (ctx, w, h, 0)
312 ctx:set_dash (dash3, 2) -- dotted line
313 ctx:set_source_rgba (.5, .5, .5, .5)
314 grid_db (ctx, w, h, -12)
315 grid_db (ctx, w, h, -24)
316 grid_db (ctx, w, h, -36)
317 grid_freq (ctx, w, h, 100)
318 grid_freq (ctx, w, h, 1000)
319 grid_freq (ctx, w, h, 10000)
322 local ho = math.floor(cur[1])
323 local lo = math.floor(cur[4])
325 -- draw transfer function line
326 ctx:set_source_rgba (.8, .8, .8, 1.0)
327 ctx:move_to (-.5, db_to_y (response(ho, lo, freq_at_x (0, w)), h))
329 local db = response(ho, lo, freq_at_x (x, w))
330 ctx:line_to (-.5 + x, db_to_y (db, h))
332 ctx:stroke_preserve ()
334 -- fill area to zero under the curve
335 ctx:line_to (w, -.5 + round (db_to_y (0, h)))
336 ctx:line_to (0, -.5 + round (db_to_y (0, h)))
338 ctx:set_source_rgba (.5, .5, .5, .5)