prefix blessed scripted DSP plugins with a-*
[ardour.git] / scripts / voice_activate.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "Voice/Level Activate",
4         category    = "Utility",
5         author      = "Ardour Lua Task Force",
6         license     = "MIT",
7         description = [[Roll the transport when the signal level on the plugin's input exceeds a given threshold.]]
8 }
9
10 function dsp_ioconfig ()
11         return
12         {
13                 -- support all in/out as long as input port count equals output port count
14                 { audio_in = -1, audio_out = -1},
15         }
16 end
17
18 function dsp_params ()
19         return
20         {
21                 { ["type"] = "input", name = "Threshold", min = -20, max = 0, default = -6, doc = "Threshold in dBFS for all channels" },
22                 { ["type"] = "output", name = "Level", min = -120, max = 0 },
23         }
24 end
25
26 function dsp_configure (ins, outs)
27         n_channels = ins:n_audio()
28 end
29
30 function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
31         local ctrl = CtrlPorts:array() -- get control port array (read/write)
32
33         if Session:transport_rolling() then
34                 -- don't do anything if the transport is already rolling
35                 ctrl[2] = -math.huge -- set control output port value
36                 return
37         end
38
39         local threshold = 10 ^ (.05 * ctrl[1]) -- dBFS to coefficient
40         local level = -math.huge
41
42         for c = 1,n_channels do
43                 local b = in_map:get(ARDOUR.DataType("audio"), c - 1) -- get id of audio-buffer for the given channel
44                 if b ~= ARDOUR.ChanMapping.Invalid then -- check if channel is mapped
45                         local a = ARDOUR.DSP.compute_peak(bufs:get_audio(b):data(offset), n_samples, 0) -- compute digital peak
46                         if a > threshold then
47                                         Session:request_transport_speed(1.0, true)
48                         end
49                         if a > level then level = a end -- max level of all channels
50                 end
51         end
52         ctrl[2] = ARDOUR.DSP.accurate_coefficient_to_dB (level) -- set control output port value
53 end