fix issue with initialization of a BBT_Time variable.
[ardour.git] / scripts / s_region_gain2.lua
1 ardour { ["type"] = "Snippet", name = "Normalize Regions" }
2
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 ()
7
8         -- prepare undo operation
9         Session:begin_reversible_command ("Lua Normalize")
10         local add_undo = false -- keep track if something has changed
11
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 ();
17                 if ar:isnil () then
18                         goto next
19                 end
20
21                 local peak = ar:maximum_amplitude (nil);
22                 local rms  = ar:rms (nil);
23
24                 if (peak > 0) then
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")
27                 else
28                         print ("Region:", r:name (), " is silent")
29                 end
30
31                 -- normalize region
32                 if (peak > 0) then
33                         -- prepare for undo
34                         r:to_stateful ():clear_changes ()
35                         -- calculate gain.  
36                         local f_rms = rms / 10 ^ (.05 * -18) -- -18dBFS/RMS
37                         local f_peak = peak / 10 ^ (.05 * -1) -- -1dbFS/peak
38                         -- apply gain
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)
42                         else 
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)
45                         end
46                         -- save changes (if any) to undo command
47                         if not Session:add_stateful_diff_command (r:to_statefuldestructible ()):empty () then
48                                 add_undo = true
49                         end
50                 end
51
52                 ::next::
53         end
54
55         -- all done. now commit the combined undo operation
56         if add_undo then
57                 -- the 'nil' command here means to use all collected diffs
58                 Session:commit_reversible_command (nil)
59         else
60                 Session:abort_reversible_command ()
61         end
62
63 end end