Add some lua scripts
[ardour.git] / scripts / voice_activate.lua
1 ardour {
2         ["type"]    = "dsp",
3         name        = "Voice/Level Activate",
4         license     = "MIT",
5         author      = "Robin Gareus",
6         authoremail = "robin@gareus.org",
7         site        = "http://gareus.org",
8         description = [[
9         An Example Audio Plugin that rolls the transport
10         when the signal level on the plugin's input a given threshold.]]
11 }
12
13 function dsp_ioconfig ()
14         return
15         {
16                 { audio_in = -1, audio_out = -1},
17         }
18 end
19
20 function dsp_params ()
21         return
22         {
23                 { ["type"] = "input", name = "Threshold", min = -20, max = 0, default = -6, doc = "Threshold in dBFS for all channels" },
24                 { ["type"] = "output", name = "Level", min = -120, max = 0 },
25         }
26 end
27
28 function dsp_configure (ins, outs)
29         n_channels = ins:n_audio();
30 end
31
32 -- use ardour's vectorized functions
33 function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
34         local ctrl = CtrlPorts:array() -- get control port array (read/write)
35         if Session:transport_rolling() then ctrl[2] = -math.huge return end
36         local threshold = 10 ^ (.05 * ctrl[1]) -- dBFS to coefficient
37         local level = -math.huge
38         for c = 1,n_channels do
39                 local b = in_map:get(ARDOUR.DataType("audio"), c - 1); -- get id of buffer for given cannel
40                 if b ~= ARDOUR.ChanMapping.Invalid then
41                         local a = ARDOUR.DSP.compute_peak(bufs:get_audio(b):data(offset), n_samples, 0)
42                         if a > threshold then
43                                         Session:request_transport_speed(1.0, true)
44                         end
45                         if a > level then level = a end
46                 end
47         end
48         ctrl[2] = ARDOUR.DSP.accurate_coefficient_to_dB (level)
49 end