-
Notifications
You must be signed in to change notification settings - Fork 147
/
Nantho_item Auto Gain Staging - Peak vs. LUFS.lua
168 lines (108 loc) · 4.16 KB
/
Nantho_item Auto Gain Staging - Peak vs. LUFS.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
--[[
* Provides: [nomain] spk77_Get max peak val and pos from take_function.lua > Nantho_PeakVsLUFS/spk77_Get max peak val and pos from take_function.lua
* ReaScript Name: Item Auto-Gain Staging - Peak vs. LUFS
* About:
* - Normalize selected items to -x dB LUFS/LU (x and unit specified by user imput)
* - If those normalized items have peaks over -y dB (y specified by user), trim them down to peak at -y dB max
* Link: http://forum.cockos.com/showthread.php?t=184368
* Instructions: Select items. Run.
* Screenshot:
* Author: Nantho
* Licence: GPL v3
* Forum Thread: REQ: http://forum.cockos.com/showthread.php?t=182701
* Forum Thread URI:
* REAPER: 5.0
* Extensions: spk77 Get max peak val and pos from take (function and example).lua
* Extensions: SWS extension
* Version: 1.0
--]]
--[[
* Changelog:
* v1.0 (2016-11-29)
+ Initial Release
--]]
-- Special thanks to X-Raym
-- USER CONFIG AREA -----------------------------------------------------------
threshold = -10 -- number: Default threshold to select items
direction = "+" -- "+"/"-": Select if over or below the threshold
all_items = false
popup = true -- true/false: display a pop up box
console = true -- true/false: display debug messages in the console
------------------------------------------------------- END OF USER CONFIG AREA
-- INCLUDES -----------------------------------------------------------
local info = debug.getinfo(1,'S');
script_path = info.source:match[[^@?(.*[\/])[^\/]-$]]
dofile(script_path .. "/Nantho_PeakVsLUFS//spk77_Get max peak val and pos from take_function.lua")
-------------------------------------------------------------- INCLUDES
-- UTILITIES -------------------------------------------------------------
-- Display a message in the console for debugging
function Msg(value)
if console then
reaper.ShowConsoleMsg(tostring(value) .. "\n")
end
end
-- Save item selection
function SaveSelectedItems (table)
for i = 0, reaper.CountSelectedMediaItems(0)-1 do
table[i+1] = reaper.GetSelectedMediaItem(0, i)
end
end
--------------------------------------------------------- END OF UTILITIES
-- Main function
function main()
for i, item in ipairs(init_sel_items) do
local take = reaper.GetActiveTake(item)
if take then
local ret, max_peak_val, peak_sample_pos = get_sample_max_val_and_pos(take, true, true, true)
if ret then
if direction_string == "+" then
if max_peak_val < threshold then
reaper.SetMediaItemSelected(item, false)
else
peak = max_peak_val
item_vol = reaper.GetMediaItemTakeInfo_Value(take,"D_VOL")
item_vol_DB = 20 * ( math.log( item_vol, 10 ) )
item_diff_DB = peak - threshold
new_item_vol_DB = item_vol_DB - item_diff_DB
new_item_vol = math.exp( new_item_vol_DB * 0.115129254 )
reaper.SetMediaItemTakeInfo_Value(take ,"D_VOL" , new_item_vol)
end
else
if max_peak_val > threshold then
reaper.SetMediaItemSelected(item, false)
end
end
end
end
end
end
-- INIT
if all_items then
reaper.Main_OnCommand(40182, 0) -- Select all items
end
-- See if there is items selected
count_sel_items = reaper.CountSelectedMediaItems(0)
if count_sel_items > 0 then
if popup then
threshold_string = tostring(threshold)
direction_string = tostring(direction)
retval, retvals_csv = reaper.GetUserInputs("Peak vs. LUFS", 2, "Peak Threshold (dB),Under/Over (-/+)?", threshold_string .. "," .. direction_string)
if retval then
threshold_string, direction_string = retvals_csv:match("([^,]+),([^,]+)")
if threshold_string then
threshold = tonumber(threshold_string)
end
end
end
if (retval or not popup) and threshold then
reaper.PreventUIRefresh(1)
reaper.Undo_BeginBlock() -- Begining of the undo block. Leave it at the top of your main function.
reaper.Main_OnCommand(reaper.NamedCommandLookup("_BR_NORMALIZE_LOUDNESS_ITEMS_LU"), 0)
init_sel_items = {}
SaveSelectedItems(init_sel_items)
main()
reaper.Undo_EndBlock("Item Auto-Gain Staging - Peak vs. LUFS", -1) -- End of the undo block. Leave it at the bottom of your main function.
reaper.UpdateArrange()
reaper.PreventUIRefresh(-1)
end
end