-- @description RTL Gain Stage to -18 LUFS
-- @author Rockettree Labs
-- @version 1.0
-- @about Normalizes selected items to -18 LUFS-I for optimal analog plugin gain staging

local num_items = reaper.CountSelectedMediaItems(0)
if num_items == 0 then
    reaper.ShowMessageBox("Please select at least one media item to gain stage.", "RTL Gain Stage", 0)
    return
end

reaper.Undo_BeginBlock()

-- Command 42468 = Item properties: Normalize items to -23 LUFS-I
reaper.Main_OnCommand(42468, 0)

-- We are at -23 LUFS-I. We want to be at -18 LUFS-I.
-- That requires a +5dB boost.
-- Linear gain factor for +5dB = 10^(5/20) = 1.778279

for i = 0, num_items - 1 do
    local item = reaper.GetSelectedMediaItem(0, i)
    local vol = reaper.GetMediaItemInfo_Value(item, "D_VOL")
    reaper.SetMediaItemInfo_Value(item, "D_VOL", vol * 1.778279)
end

reaper.UpdateArrange()
reaper.Undo_EndBlock("RTL: Gain Stage to -18 LUFS", -1)
