Fix auto capture alignment when bouncing metronome
[ardour.git] / scripts / a_slow_mute.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "a-Slow-Mute",
4         category    = "Amplifier",
5         license     = "MIT",
6         author      = "Ardour Team",
7         description = [[Mute button with slow fade in/out]]
8 }
9
10 function dsp_ioconfig ()
11         -- -1, -1 = any number of channels as long as input and output count matches
12         return { { audio_in = -1, audio_out = -1} }
13 end
14
15
16 function dsp_params ()
17         return { { ["type"] = "input", name = "Mute", min = 0, max = 1, default = 0, toggled = true } }
18 end
19
20 local cur_gain = 1
21 local lpf = 0.002 -- parameter low-pass filter time-constant
22
23 function dsp_init (rate)
24         lpf = 100 / rate -- interpolation time constant
25 end
26
27 function low_pass_filter_param (old, new, limit)
28         if math.abs (old - new) < limit  then
29                 return new
30         else
31                 return old + lpf * (new - old)
32         end
33 end
34
35 -- the DSP callback function
36 -- "ins" and "outs" are http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
37 function dsp_run (ins, outs, n_samples)
38         local ctrl = CtrlPorts:array() -- get control port array
39         local target_gain = ctrl[1] > 0 and 0.0 or 1.0;  -- when muted, target_gain = 0.0; otherwise use 1.0
40         local siz = n_samples -- samples remaining to process
41         local off = 0 -- already processed samples
42         local changed = false
43
44         -- if the target gain changes, process at most 32 samples at a time,
45         -- and interpolate gain until the current settings match the target values
46         if cur_gain ~= target_gain then
47                 changed = true
48                 siz = 32
49         end
50
51         while n_samples > 0 do
52                 if siz > n_samples then siz = n_samples end
53                 if changed then
54                         cur_gain = low_pass_filter_param (cur_gain, target_gain, 0.001)
55                 end
56
57                 for c = 1,#ins do -- process all channels
58                         if ins[c] ~= outs[c] then
59                                 ARDOUR.DSP.copy_vector (outs[c]:offset (off), ins[c]:offset (off), siz)
60                         end
61                         ARDOUR.DSP.apply_gain_to_buffer (outs[c]:offset (off), siz, cur_gain);
62                 end
63                 n_samples = n_samples - siz
64                 off = off + siz
65         end
66 end