summaryrefslogtreecommitdiff
path: root/scripts/a_slow_mute.lua
blob: a9ef12b7544fe09b140a1a527e5cda4444ff4cb0 (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
ardour {
	["type"]    = "dsp",
	name        = "a-Slow-Mute",
	category    = "Amplifier",
	license     = "MIT",
	author      = "Ardour Team",
	description = [[Mute button with slow fade in/out]]
}

function dsp_ioconfig ()
	-- -1, -1 = any number of channels as long as input and output count matches
	return { { audio_in = -1, audio_out = -1} }
end


function dsp_params ()
	return { { ["type"] = "input", name = "Mute", min = 0, max = 1, default = 0, toggled = true } }
end

local cur_gain = 1
local lpf = 0.002 -- parameter low-pass filter time-constant

function dsp_init (rate)
	lpf = 100 / rate -- interpolation time constant
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

-- the DSP callback function
-- "ins" and "outs" are http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
function dsp_run (ins, outs, n_samples)
	local ctrl = CtrlPorts:array() -- get control port array
	local target_gain = ctrl[1] > 0 and 0.0 or 1.0;  -- when muted, target_gain = 0.0; otherwise use 1.0
	local siz = n_samples -- samples remaining to process
	local off = 0 -- already processed samples
	local changed = false

	-- if the target gain changes, process at most 32 samples at a time,
	-- and interpolate gain until the current settings match the target values
	if cur_gain ~= target_gain then
		changed = true
		siz = 32
	end

	while n_samples > 0 do
		if siz > n_samples then siz = n_samples end
		if changed then
			cur_gain = low_pass_filter_param (cur_gain, target_gain, 0.001)
		end

		for c = 1,#ins do -- process all channels
			if ins[c] ~= outs[c] then
				ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz)
			end
			ARDOUR.DSP.apply_gain_to_buffer (outs[c]:offset (off), siz, cur_gain);
		end
		n_samples = n_samples - siz
		off = off + siz
	end
end