3 name = "High/Low Pass 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 = 1, default = 0, enum = true, scalepoints =
30 { ["type"] = "input", name = "Steepness", min = 1, max = 4, default = 1, enum = true, scalepoints =
38 { ["type"] = "input", name = "Cut off frequency", min = 5, max = 20000, default = 1000, unit="Hz", logarithmic = true },
39 { ["type"] = "input", name = "Resonance", min = 0.1, max = 8, default = .707, logarithmic = true },
43 -- translate type parameter to DSP enum
44 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.DSP.Biquad.Type
47 return ARDOUR.DSP.BiquadType.LowPass
49 return ARDOUR.DSP.BiquadType.HighPass
53 -- these globals are *not* shared between DSP and UI
54 local filters = {} -- the biquad filter instances (DSP)
55 local filt -- the biquad filter instance (GUI, response)
56 local cur = {0, 0, 0, 0} -- current parameters
57 local lpf = 0.03 -- parameter low-pass filter time-constant
58 local chn = 0 -- channel/filter count
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 lpf = 13000 / rate -- interpolation time constant
67 function dsp_configure (ins, outs)
68 assert (ins:n_audio () == outs:n_audio ())
69 local cfg = self:shmem ():to_int (0):array ()
74 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
76 filters[c][k] = ARDOUR.DSP.Biquad (rate) -- initialize filters
81 -- helper functions for parameter interpolation
82 function param_changed (ctrl)
83 if ctrl[1] == cur[1] and ctrl[2] == cur[2] and ctrl[3] == cur[3] and ctrl[4] == cur[4] then
89 function low_pass_filter_param (old, new, limit)
90 if math.abs (old - new) < limit then
93 return old + lpf * (new - old)
97 -- apply parameters, re-compute filter coefficients if needed
98 function apply_params (ctrl)
99 if not param_changed (ctrl) then
103 if cur[1] ~= ctrl[1] or cur[2] ~= ctrl[2] then
104 -- reset filter state when type or order changes
107 filters[c][k]:reset ()
110 for k = 1,4 do cur[k] = ctrl[k] end
112 -- low-pass filter ctrl parameter values, smooth transition
113 cur[3] = low_pass_filter_param (cur[3], ctrl[3], 1.0) -- freq/Hz
114 cur[4] = low_pass_filter_param (cur[4], ctrl[4], 0.01) -- quality
117 if cur[2] < 1 then cur[2] = 1 end
118 if cur[2] > 4 then cur[2] = 4 end
122 filters[c][k]:compute (map_type (cur[1]), cur[3], cur[4], 0)
128 -- the actual DSP callback
129 function dsp_run (ins, outs, n_samples)
130 local changed = false
131 local siz = n_samples
134 -- if a parameter was changed, process at most 64 samples at a time
135 -- and interpolate parameters until the current settings match
137 if param_changed (CtrlPorts:array ()) then
144 while n_samples > 0 do
145 if changed then apply_params (CtrlPorts:array ()) end
146 if siz > n_samples then siz = n_samples end
148 -- process all channels
150 -- check if output and input buffers for this channel are identical
151 -- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
152 if false then --- ins[c]:sameinstance (outs[c]) then
154 filters[c][k]:run (ins[c]:offset (off), siz) -- in-place processing
157 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
158 ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz)
160 filters[c][k]:run (outs[c]:offset (off), siz)
165 n_samples = n_samples - siz
176 -------------------------------------------------------------------------------
180 return math.floor (n + .5)
183 function freq_at_x (x, w)
184 -- x-axis pixel for given freq, power-scale
185 return 20 * 1000 ^ (x / w)
188 function x_at_freq (f, w)
189 -- frequency at given x-axis pixel
190 return w * math.log (f / 20.0) / math.log (1000.0)
193 function db_to_y (db, h)
194 -- y-axis gain mapping
195 if db < -48 then db = -48 end
196 if db > 12 then db = 12 end
197 return -.5 + round (0.2 * h) - h * db / 60
200 function grid_db (ctx, w, h, db)
201 -- draw horizontal grid line
202 local y = -.5 + round (db_to_y (db, h))
208 function grid_freq (ctx, w, h, f)
209 -- draw vertical grid line
210 local x = -.5 + round (x_at_freq (f, w))
216 function render_inline (ctx, w, max_h)
218 -- the GUI is separate from the DSP, but the GUI needs to know
219 -- the sample-rate that the DSP is using.
220 local shmem = self:shmem () -- get shared memory region
221 local cfg = shmem:to_int (0):array () -- "cast" into lua-table
222 -- instantiate filter (to calculate the transfer function's response)
223 filt = ARDOUR.DSP.Biquad (cfg[1])
226 -- set filter coefficients if they have changed
227 if param_changed (CtrlPorts:array ()) then
228 local ctrl = CtrlPorts:array ()
229 for k = 1,4 do cur[k] = ctrl[k] end
230 if cur[2] < 1 then cur[2] = 1 end
231 if cur[2] > 4 then cur[2] = 4 end
232 filt:compute (map_type (cur[1]), cur[3], cur[4], 0)
235 -- calc height of inline display
236 local h = math.ceil (w * 10 / 16) -- use 16:10 aspect
237 if (h > max_h) then h = max_h end -- but at most max-height
239 -- ctx is a http://cairographics.org/ context
240 -- http://manual.ardour.org/lua-scripting/class_reference/#Cairo:Context
243 ctx:rectangle (0, 0, w, h)
244 ctx:set_source_rgba (.2, .2, .2, 1.0)
247 -- set line width: 1px
248 -- Note: a cairo pixel at [1,1] spans [0.5->1.5 , 0.5->1.5]
249 -- hence the offset -0.5 in various move_to(), line_to() calls
250 ctx:set_line_width (1.0)
253 local dash3 = C.DoubleVector ()
254 local dash2 = C.DoubleVector ()
257 ctx:set_dash (dash2, 2) -- dotted line
258 ctx:set_source_rgba (.5, .5, .5, .8)
259 grid_db (ctx, w, h, 0)
260 ctx:set_dash (dash3, 2) -- dotted line
261 ctx:set_source_rgba (.5, .5, .5, .5)
262 grid_db (ctx, w, h, -12)
263 grid_db (ctx, w, h, -24)
264 grid_db (ctx, w, h, -36)
265 grid_freq (ctx, w, h, 100)
266 grid_freq (ctx, w, h, 1000)
267 grid_freq (ctx, w, h, 10000)
271 -- draw transfer function line
272 ctx:set_source_rgba (.8, .8, .8, 1.0)
273 ctx:move_to (-.5, db_to_y (o * filt:dB_at_freq (freq_at_x (0, w)), h))
275 local db = o * filt:dB_at_freq (freq_at_x (x, w))
276 ctx:line_to (-.5 + x, db_to_y (db, h))
278 ctx:stroke_preserve ()
280 -- fill area to zero under the curve
281 ctx:line_to (w, -.5 + round (db_to_y (0, h)))
282 ctx:line_to (0, -.5 + round (db_to_y (0, h)))
284 ctx:set_source_rgba (.5, .5, .5, .5)