local newValue, _, _, _, _, _, val = reaper.get_action_context()

if val == 0 or not newValue then
	return
end

VOL_MIN = 0
DB_CHANGE = 0.5

local volMod = 0

if val > 0 then
	volMod = 10 ^ (DB_CHANGE / 20)
elseif val < 0 then
	volMod = 10 ^ (-DB_CHANGE / 20)
else
	return
end

local selectedItemCount = reaper.CountSelectedMediaItems(0)

if selectedItemCount == 0 then
	return
end

for i = 0, selectedItemCount - 1 do
	local item = reaper.GetSelectedMediaItem(0, i)

	if item then
		local previousVolume = reaper.GetMediaItemInfo_Value(item, "D_VOL")
		local newVolume = math.max(previousVolume * volMod, VOL_MIN)
		reaper.SetMediaItemInfo_Value(item, "D_VOL", newVolume)
	end
end

reaper.UpdateArrange()
