1 ardour { ["type"] = "EditorAction",
2 name = "Normalize All Tracks",
4 author = "Ardour Team",
5 description = [[Normalize all regions using a common gain-factor per track.]]
8 function factory () return function ()
9 -- target values -- TODO: use a LuaDialog.Dialog and ask..
10 local target_peak = -1 --dBFS
11 local target_rms = -18 --dBFS/RMS
13 -- prepare undo operation
14 Session:begin_reversible_command ("Normalize Tracks")
15 local add_undo = false -- keep track if something has changed
17 -- loop over all tracks in the session
18 for track in Session:get_tracks():iter() do
19 local norm = 0 -- per track gain
20 -- loop over all regions on track
21 for r in track:to_track():playlist():region_list():iter() do
22 -- test if it's an audio region
23 local ar = r:to_audioregion ()
24 if ar:isnil () then goto next end
26 local peak = ar:maximum_amplitude (nil)
27 local rms = ar:rms (nil)
28 -- check if region is silent
30 local f_rms = rms / 10 ^ (.05 * target_rms)
31 local f_peak = peak / 10 ^ (.05 * target_peak)
32 local tg = (f_peak > f_rms) and f_peak or f_rms -- max (f_peak, f_rms)
33 norm = (tg > norm) and tg or norm -- max (tg, norm)
38 -- apply same gain to all regions on track
40 for r in track:to_track():playlist():region_list():iter() do
41 local ar = r:to_audioregion ()
42 if ar:isnil () then goto skip end
43 ar:to_stateful ():clear_changes ()
44 ar:set_scale_amplitude (1 / norm)
51 -- all done. now commit the combined undo operation
53 -- the 'nil' command here means to use all collected diffs
54 Session:commit_reversible_command (nil)
56 Session:abort_reversible_command ()