lua-scope handle unconnected in-place buffers
[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 exceeds a given threshold.]]
11 }
12
13 function dsp_ioconfig ()
14         return
15         {
16                 -- support all in/out as long as input port count equals output port count
17                 { audio_in = -1, audio_out = -1},
18         }
19 end
20
21 function dsp_params ()
22         return
23         {
24                 { ["type"] = "input", name = "Threshold", min = -20, max = 0, default = -6, doc = "Threshold in dBFS for all channels" },
25                 { ["type"] = "output", name = "Level", min = -120, max = 0 },
26         }
27 end
28
29 function dsp_configure (ins, outs)
30         n_channels = ins:n_audio()
31 end
32
33 function dsp_runmap (bufs, in_map, out_map, n_samples, offset)
34         local ctrl = CtrlPorts:array() -- get control port array (read/write)
35
36         if Session:transport_rolling() then
37                 -- don't do anything if the transport is already rolling
38                 ctrl[2] = -math.huge -- set control output port value
39                 return
40         end
41
42         local threshold = 10 ^ (.05 * ctrl[1]) -- dBFS to coefficient
43         local level = -math.huge
44
45         for c = 1,n_channels do
46                 local b = in_map:get(ARDOUR.DataType("audio"), c - 1) -- get id of audio-buffer for the given channel
47                 if b ~= ARDOUR.ChanMapping.Invalid then -- check if channel is mapped
48                         local a = ARDOUR.DSP.compute_peak(bufs:get_audio(b):data(offset), n_samples, 0) -- compute digital peak
49                         if a > threshold then
50                                         Session:request_transport_speed(1.0, true)
51                         end
52                         if a > level then level = a end -- max level of all channels
53                 end
54         end
55         ctrl[2] = ARDOUR.DSP.accurate_coefficient_to_dB (level) -- set control output port value
56 end