2016-11-19 15:58:03 -05:00
|
|
|
ardour { ["type"] = "Snippet", name = "Set Region Gain" }
|
|
|
|
|
|
|
|
function factory () return function ()
|
|
|
|
-- get Editor GUI Selection
|
|
|
|
-- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:Selection
|
|
|
|
local sel = Editor:get_selection ()
|
|
|
|
|
|
|
|
-- allocate a buffer (float* in C)
|
|
|
|
-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:DspShm
|
|
|
|
local cmem = ARDOUR.DSP.DspShm (8192)
|
|
|
|
|
|
|
|
-- prepare undo operation
|
|
|
|
Session:begin_reversible_command ("Lua Region Gain")
|
|
|
|
|
|
|
|
-- iterate over selected regions
|
|
|
|
-- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:RegionSelection
|
|
|
|
for r in sel.regions:regionlist ():iter () do
|
|
|
|
-- test if it's an audio region
|
2022-09-27 12:58:53 -04:00
|
|
|
local ar = r:to_audioregion ()
|
|
|
|
if ar:isnil () then
|
2016-11-19 15:58:03 -05:00
|
|
|
goto next
|
|
|
|
end
|
|
|
|
|
|
|
|
-- to read the Region data, we use the Readable interface of the Region
|
2020-09-20 18:37:32 -04:00
|
|
|
-- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:AudioReadable
|
2022-09-27 12:58:53 -04:00
|
|
|
local rd = ar:to_readable ()
|
2016-11-19 15:58:03 -05:00
|
|
|
|
|
|
|
local n_samples = rd:readable_length ()
|
|
|
|
local n_channels = rd:n_channels ()
|
|
|
|
|
|
|
|
local peak = 0 -- the audio peak to be calculated
|
|
|
|
|
|
|
|
-- iterate over all channels in Audio Region
|
2020-01-20 00:37:30 -05:00
|
|
|
for c = 0, n_channels -1 do
|
|
|
|
local pos = 0
|
2016-11-19 15:58:03 -05:00
|
|
|
repeat
|
2020-01-20 00:37:30 -05:00
|
|
|
-- read at most 8K samples of channel 'c' starting at 'pos'
|
2016-11-19 15:58:03 -05:00
|
|
|
local s = rd:read (cmem:to_float (0), pos, 8192, c)
|
|
|
|
pos = pos + s
|
|
|
|
-- access the raw audio data
|
|
|
|
-- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
|
|
|
|
local d = cmem:to_float (0):array()
|
|
|
|
-- iterate over the audio sample data
|
2020-03-02 18:27:25 -05:00
|
|
|
for i = 1, s do
|
2016-11-19 15:58:03 -05:00
|
|
|
if math.abs (d[i]) > peak then
|
|
|
|
peak = math.abs (d[i])
|
|
|
|
end
|
|
|
|
end
|
2020-01-20 00:37:30 -05:00
|
|
|
until s < 8192
|
|
|
|
assert (pos == n_samples)
|
2016-11-19 15:58:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if (peak > 0) then
|
|
|
|
print ("Region:", r:name (), "peak:", 20 * math.log (peak) / math.log(10), "dBFS")
|
|
|
|
else
|
|
|
|
print ("Region:", r:name (), " is silent")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- normalize region
|
|
|
|
if (peak > 0) then
|
|
|
|
-- prepare for undo
|
|
|
|
r:to_stateful ():clear_changes ()
|
|
|
|
-- apply gain
|
|
|
|
r:to_audioregion (): set_scale_amplitude (1 / peak)
|
|
|
|
-- save changes (if any) to undo command
|
2022-09-27 12:58:53 -04:00
|
|
|
Session:add_stateful_diff_command (r:to_statefuldestructible ())
|
2016-11-19 15:58:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
::next::
|
|
|
|
end
|
|
|
|
|
2022-09-27 12:58:53 -04:00
|
|
|
if not Session:abort_empty_reversible_command () then
|
2016-11-19 15:58:03 -05:00
|
|
|
Session:commit_reversible_command (nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
end end
|