1 ardour { ["type"] = "Snippet", name = "Normalize Regions" }
3 function factory () return function ()
4 -- get Editor GUI Selection
5 -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:Selection
6 local sel = Editor:get_selection ()
8 -- prepare undo operation
9 Session:begin_reversible_command ("Lua Normalize")
10 local add_undo = false -- keep track if something has changed
12 -- iterate over selected regions
13 -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:RegionSelection
14 for r in sel.regions:regionlist ():iter () do
15 -- test if it's an audio region
16 local ar = r:to_audioregion ();
21 local peak = ar:maximum_amplitude (nil);
22 local rms = ar:rms (nil);
25 print ("Region:", r:name (), "peak:", 20 * math.log (peak) / math.log(10), "dBFS")
26 print ("Region:", r:name (), "rms :", 20 * math.log (rms) / math.log(10), "dBFS")
28 print ("Region:", r:name (), " is silent")
34 r:to_stateful ():clear_changes ()
36 local f_rms = rms / 10 ^ (.05 * -18) -- -18dBFS/RMS
37 local f_peak = peak / 10 ^ (.05 * -1) -- -1dbFS/peak
39 if (f_rms > f_peak) then
40 print ("Region:", r:name (), "RMS normalized by:", -20 * math.log (f_rms) / math.log(10), "dB")
41 ar:set_scale_amplitude (1 / f_rms)
43 print ("Region:", r:name (), "peak normalized by:", -20 * math.log (f_peak) / math.log(10), "dB")
44 ar:set_scale_amplitude (1 / f_peak)
46 -- save changes (if any) to undo command
47 if not Session:add_stateful_diff_command (r:to_statefuldestructible ()):empty () then
55 -- all done. now commit the combined undo operation
57 -- the 'nil' command here means to use all collected diffs
58 Session:commit_reversible_command (nil)
60 Session:abort_reversible_command ()