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