summaryrefslogtreecommitdiff
path: root/scripts/hplp.lua
blob: 3b6a34c0fd0ebb8b16950cb2d8c7c5184131efd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
ardour {
	["type"]    = "dsp",
	name        = "High/Low Pass Filter",
	category    = "Filter",
	license     = "MIT",
	author      = "Robin Gareus",
	email       = "robin@gareus.org",
	site        = "http://gareus.org",
	description = [[Example Ardour Lua DSP Plugin]]
}

function dsp_ioconfig ()
	return
	{
		-- allow any number of I/O as long as port-count matches
		{ audio_in = -1, audio_out = -1},
	}
end


function dsp_params ()
	return
	{
		{ ["type"] = "input", name = "Type", min = 0, max = 1, default = 0, enum = true, scalepoints =
			{
				["High Pass"]  = 0,
				["Low Pass"]   = 1,
			}
		},
		{ ["type"] = "input", name = "Steepness", min = 1, max = 4, default = 1, enum = true, scalepoints =
			{
				["12dB/oct"] = 1,
				["24dB/oct"] = 2,
				["36dB/oct"] = 3,
				["48dB/oct"] = 4,
			}
		},
		{ ["type"] = "input", name = "Cut off frequency", min =   5, max = 20000, default = 1000, unit="Hz", logarithmic = true },
		{ ["type"] = "input", name = "Resonance",         min = 0.1, max = 8,     default = .707, logarithmic = true },
	}
end

-- translate type parameter to DSP enum
-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR.DSP.Biquad.Type
function map_type (t)
	if     t == 1 then
		return ARDOUR.DSP.BiquadType.LowPass
	else
		return ARDOUR.DSP.BiquadType.HighPass
	end
end

-- these globals are *not* shared between DSP and UI
local filters = {}  -- the biquad filter instances (DSP)
local filt -- the biquad filter instance (GUI, response)
local cur = {0, 0, 0, 0} -- current parameters
local lpf = 0.03 -- parameter low-pass filter time-constant

function dsp_init (rate)
	self:shmem ():allocate (1) -- shared mem to tell the GUI the samplerate
	local cfg = self:shmem ():to_int (0):array ()
	cfg[1] = rate
	-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:Biquad
	for k = 1,4 do
		filters[k] = ARDOUR.DSP.Biquad (rate) -- initialize filters
	end
	lpf = 13000 / rate -- interpolation time constant
end

-- helper functions for parameter interpolation
function param_changed (ctrl)
	if ctrl[1] == cur[1] and ctrl[2] == cur[2] and ctrl[3] == cur[3] and ctrl[4] == cur[4] then
		return false
	end
	return true
end

function low_pass_filter_param (old, new, limit)
	if math.abs (old - new) < limit  then
		return new
	else
		return old + lpf * (new - old)
	end
end

-- apply parameters, re-compute filter coefficients if needed
function apply_params (ctrl)
	if not param_changed (ctrl) then
		return
	end

	if cur[1] ~= ctrl[1] or cur[2] ~= ctrl[2] then
		-- reset filter state when type or order changes
		for k = 1,4 do
			filters[k]:reset ()
		end
		for k = 1,4 do cur[k] = ctrl[k] end
	else
		-- low-pass filter ctrl parameter values, smooth transition
		cur[3] = low_pass_filter_param (cur[3], ctrl[3], 1.0) -- freq/Hz
		cur[4] = low_pass_filter_param (cur[4], ctrl[4], 0.01) -- quality
	end

	if cur[2] < 1 then cur[2] = 1 end
	if cur[2] > 4 then cur[2] = 4 end

	for k = 1,4 do
		filters[k]:compute (map_type (cur[1]), cur[3], cur[4], 0)
	end
end


-- the actual DSP callback
function dsp_run (ins, outs, n_samples)
	local changed = false
	local siz = n_samples
	local off = 0

	-- if a parameter was changed, process at most 64 samples at a time
	-- and interpolate parameters until the current settings match
	-- the target values
	if param_changed (CtrlPorts:array ()) then
		changed = true
		siz = 64
	end

	local o = cur[2]

	while n_samples > 0 do
		if changed then apply_params (CtrlPorts:array ()) end
		if siz > n_samples then siz = n_samples end

		-- process all channels
		for c = 1,#ins do
			-- check if output and input buffers for this channel are identical
			-- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
			if ins[c]:sameinstance (outs[c]) then
				for k = 1,o do
					filters[k]:run (ins[c]:offset (off), siz) -- in-place processing
				end
			else
				-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP
				ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz)
				for k = 1,o do
					filters[o]:run (outs[c]:offset (off), siz)
				end
			end
		end

		n_samples = n_samples - siz
		off = off + siz
	end

	if changed then
		-- notify display
		self:queue_draw ()
	end
end


-------------------------------------------------------------------------------
--- inline display

function round (n)
	return math.floor (n + .5)
end

function freq_at_x (x, w)
	-- x-axis pixel for given freq, power-scale
	return 20 * 1000 ^ (x / w)
end

function x_at_freq (f, w)
	-- frequency at given x-axis pixel
	return w * math.log (f / 20.0) / math.log (1000.0)
end

function db_to_y (db, h)
	-- y-axis gain mapping
	if db < -48 then db = -48 end
	if db >  12 then db =  12 end
	return -.5 + round (0.2 * h) - h * db / 60
end

function grid_db (ctx, w, h, db)
	-- draw horizontal grid line
	local y = -.5 + round (db_to_y (db, h))
	ctx:move_to (0, y)
	ctx:line_to (w, y)
	ctx:stroke ()
end

function grid_freq (ctx, w, h, f)
	-- draw vertical grid line
	local x = -.5 + round (x_at_freq (f, w))
	ctx:move_to (x, 0)
	ctx:line_to (x, h)
	ctx:stroke ()
end

function render_inline (ctx, w, max_h)
	if not filt then
		-- the GUI is separate from the DSP, but the GUI needs to know
		-- the sample-rate that the DSP is using.
		local shmem = self:shmem () -- get shared memory region
		local cfg = shmem:to_int (0):array () -- "cast" into lua-table
		-- instantiate filter (to calculate the transfer function's response)
		filt = ARDOUR.DSP.Biquad (cfg[1])
	end

	-- set filter coefficients if they have changed
	if param_changed (CtrlPorts:array ()) then
		local ctrl = CtrlPorts:array ()
		for k = 1,4 do cur[k] = ctrl[k] end
		if cur[2] < 1 then cur[2] = 1 end
		if cur[2] > 4 then cur[2] = 4 end
		filt:compute (map_type (cur[1]), cur[3], cur[4], 0)
	end

	-- calc height of inline display
	local h = math.ceil (w * 10 / 16) -- use 16:10 aspect
	if (h > max_h) then h = max_h end -- but at most max-height

	-- ctx is a http://cairographics.org/ context
	-- http://manual.ardour.org/lua-scripting/class_reference/#Cairo:Context

	-- clear background
	ctx:rectangle (0, 0, w, h)
	ctx:set_source_rgba (.2, .2, .2, 1.0)
	ctx:fill ()

	-- set line width: 1px
	-- Note: a cairo pixel at [1,1]  spans [0.5->1.5 , 0.5->1.5]
	-- hence the offset -0.5 in various move_to(), line_to() calls
	ctx:set_line_width (1.0)

	-- draw grid
	local dash3 = C.DoubleVector ()
	local dash2 = C.DoubleVector ()
	dash2:add ({1, 2})
	dash3:add ({1, 3})
	ctx:set_dash (dash2, 2) -- dotted line
	ctx:set_source_rgba (.5, .5, .5, .8)
	grid_db (ctx, w, h, 0)
	ctx:set_dash (dash3, 2) -- dotted line
	ctx:set_source_rgba (.5, .5, .5, .5)
	grid_db (ctx, w, h, -12)
	grid_db (ctx, w, h, -24)
	grid_db (ctx, w, h, -36)
	grid_freq (ctx, w, h, 100)
	grid_freq (ctx, w, h, 1000)
	grid_freq (ctx, w, h, 10000)
	ctx:unset_dash ()

	local o = cur[2]
	-- draw transfer function line
	ctx:set_source_rgba (.8, .8, .8, 1.0)
	ctx:move_to (-.5, db_to_y (o * filt:dB_at_freq (freq_at_x (0, w)), h))
	for x = 1,w do
		local db = o * filt:dB_at_freq (freq_at_x (x, w))
		ctx:line_to (-.5 + x, db_to_y (db, h))
	end
	ctx:stroke_preserve ()

	-- fill area to zero under the curve
	ctx:line_to (w, -.5 + round (db_to_y (0, h)))
	ctx:line_to (0, -.5 + round (db_to_y (0, h)))
	ctx:close_path ()
	ctx:set_source_rgba (.5, .5, .5, .5)
	ctx:fill ()

	return {w, h}
end