More consistent dialog window titles (Recall Mixer Settings).
[ardour.git] / scripts / s_region_gain.lua
1 ardour { ["type"] = "Snippet", name = "Set Region Gain" }
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         -- allocate a buffer (float* in C)
9         -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:DSP:DspShm
10         local cmem = ARDOUR.DSP.DspShm (8192)
11
12         -- prepare undo operation
13         Session:begin_reversible_command ("Lua Region Gain")
14         local add_undo = false -- keep track if something has changed
15
16         -- iterate over selected regions
17         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:RegionSelection
18         for r in sel.regions:regionlist ():iter () do
19                 -- test if it's an audio region
20                 if r:to_audioregion ():isnil () then
21                         goto next
22                 end
23
24                 -- to read the Region data, we use the Readable interface of the Region
25                 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:Readable
26                 local rd = r:to_readable ()
27
28                 local n_samples = rd:readable_length ()
29                 local n_channels = rd:n_channels ()
30
31                 local peak = 0 -- the audio peak to be calculated
32
33                 -- iterate over all channels in Audio Region
34                 for c = 0, n_channels do
35                         repeat
36                                 local pos = 0
37                                 -- read at most 8K samples of channel 'c'
38                                 local s = rd:read (cmem:to_float (0), pos, 8192, c)
39                                 pos = pos + s
40                                 -- access the raw audio data
41                                 -- http://manual.ardour.org/lua-scripting/class_reference/#C:FloatArray
42                                 local d = cmem:to_float (0):array()
43                                 -- iterate over the audio sample data
44                                 for i = 0, s do
45                                         if math.abs (d[i]) > peak then
46                                                 peak = math.abs (d[i])
47                                         end
48                                 end
49                         until pos < n_samples
50                 end
51
52                 if (peak > 0) then
53                         print ("Region:", r:name (), "peak:", 20 * math.log (peak) / math.log(10), "dBFS")
54                 else
55                         print ("Region:", r:name (), " is silent")
56                 end
57
58                 -- normalize region
59                 if (peak > 0) then
60                         -- prepare for undo
61                         r:to_stateful ():clear_changes ()
62                         -- apply gain
63                         r:to_audioregion (): set_scale_amplitude (1 / peak)
64                         -- save changes (if any) to undo command
65                         if not Session:add_stateful_diff_command (r:to_statefuldestructible ()):empty () then
66                                 add_undo = true
67                         end
68                 end
69
70                 ::next::
71         end
72
73         -- all done. now commit the combined undo operation
74         if add_undo then
75                 -- the 'nil' command here means to use all collected diffs
76                 Session:commit_reversible_command (nil)
77         else
78                 Session:abort_reversible_command ()
79         end
80
81 end end