1
I found this one at this reddit
There were some comments (copied below) on how it could be improved.
One addition I make (mentioned below) is to default the send to -∞ dB,
rather than the 0dB that is the default for sends (and I don't want to
change that default since it's good for other sends).
local tr_cnt = reaper.CountSelectedTracks( 0 )
local fnd = 0
local sel_tr = {}
local store = {}
-- add channel selector
reaper.Undo_BeginBlock2( 0 )
reaper.PreventUIRefresh( 1 )
for i = 0, tr_cnt-1 do
sel_tr[#sel_tr+1] = reaper.GetSelectedTrack( 0, i )
end
-- Finding track named Reverb
for trackIndex = 0, reaper.CountTracks(0) - 1 do
local tr = reaper.GetTrack(0, trackIndex)
local _, trackName = reaper.GetSetMediaTrackInfo_String(tr, 'P_NAME', '', false)
if string.match(trackName, "Reverb") ~= nil then -- Change Reverb for any other string you like
store[#store+1] = tr
fnd = fnd+1
end
end
if fnd == 0 then
reaper.ShowMessageBox("No tracks with the name Reverb found!", "Error!", 0)
end
if fnd == 1 then
for i = 1, #sel_tr do
reaper.CreateTrackSend( sel_tr[i], store[fnd] )
end
end
if fnd > 1 then
local _, nmb = reaper.GetUserInputs("Multiple Reverb Tracks Found, input number:", 1, "number:", 0)
nmb = tonumber(nmb)
if _ == true and nmb ~= 0 and nmb <= fnd then
for i = 1, #sel_tr do
reaper.CreateTrackSend( sel_tr[i], store[nmb] )
end
end
end
reaper.PreventUIRefresh( -1 )
reaper.Undo_EndBlock2( 0, "Route Selected Tracks to Reverb", 1)
To default send level to -∞ dB
send_idx = reaper.CreateTrackSend( sel_tr[i], store[fnd] )
reaper.SetTrackSendInfo_Value( sel_tr[i], 0, send_idx, "D_VOL", 0.0)
One could also put this in a function with the Reverb string a parameter. So we could use
the same code to do sends to tracks named Delay.
Comments and Improvement
These comments were made by a [deleted] user on the Reddit linked above.
-
You're counting how many time you find a track named
"Reverb"via the variablefndas you insert those tracks into an array (store). There's no need to maintain a separate count. That number is part of the array, accessed via#store. You're already using this fact to insert elements into the array; that's what the#storeinstore[#store+1] = strmeans. So thefndvariable is superfluous. -
string.match(trackName, "Reverb")can be writtentrackName:match("Reverb"). -
if blah ~= nil thencan be writtenif blah then. The~= nil is implicit; that's whattruemeans in Lua. -
Regarding
_ == true: if_is equal to true then_and_ == trueproduce the same value, so the== trueportion is unnecessary. -
_is sometimes used if a value is being discarded, but here the value is actually being used and so should be named. -
You only use
tr_cntonce, so it doesn't really need to even be stored (matter of taste). -
#store > 1is a superset of#store == 1and contains duplicate code. You can eliminate the duplication by folding these cases together. -
You have a comment indicating where someone can edit the code if they use a different reverb track name. It's easier for a user to find and edit that kind of configuration stuff if you put it to the top of the script.
-
Naming a variable
storeis a lot like naming itvariable. Yes, we know it stores something, but what does it store? The name should reflect its content. Given that your other variable for storing tracks is calledsel_tr(presumably short for selected tracks), you could call this onerev_tr(to maintain the same level of terseness, though I'd probably just call them something likeselectedandreverb). -
There's no feedback for when no tracks are selected.
-
When you prompt the user to choose which reverb track to send to, you give them a useless default and give no feedback if they type in an invalid option.
-
If you're iterating through an array using a
for i=1,Nand you only useito index the array, you can use theipairsiterator instead and have a tidier loop body. -
If you wrap the body of your script in a function, you can
- use early returns to abort and
- nicely group the UI/undo handling stuff together.
Improved Version
local reverbTrackName = "Reverb" -- Change for any name you like
function main()
local sel_tr = {}
local reverb_tr = {}
for i = 0, reaper.CountSelectedTracks(0)-1 do
sel_tr[#sel_tr+1] = reaper.GetSelectedTrack(0,i)
end
if #sel_tr == 0 then
reaper.ShowMessageBox("No tracks selected!", "Error!", 0)
return
end
-- find all reverb tracks
for trackIndex = 0, reaper.CountTracks(0) - 1 do
local track = reaper.GetTrack(0, trackIndex)
local _, trackName = reaper.GetSetMediaTrackInfo_String(track, 'P_NAME', '', false)
if trackName:match(reverbTrackName) then
reverb_tr[#reverb_tr+1] = track
end
end
if #reverb_tr == 0 then
reaper.ShowMessageBox("No tracks with the name " + reverbTrackName + " found!", "Error!", 0)
return
elseif #reverb_tr > 1 then
local ok, nmb = reaper.GetUserInputs("Which reverb track should we send to?", 1, "number:", 1)
if not ok then
return
end
nmb = tonumber(nmb)
if nmb < 1 or nmb > #reverb_tr then
reaper.ShowMessageBox("That's not a valid reverb track.", "Error!", 0)
return
end
reverb_tr[1] = reverb_tr[nmb]
end
for i,track in ipairs(sel_tr) do
reaper.CreateTrackSend( track, reverb_tr[1] )
end
end
reaper.Undo_BeginBlock2( 0 )
reaper.PreventUIRefresh( 1 )
main()
reaper.PreventUIRefresh( -1 )
reaper.Undo_EndBlock2( 0, "Route Selected Tracks to Reverb", 1)