Posted on

How to turn one script into three

If you’ve read through the previous post in this series then you’ve got a completed Reascript that will arm your selected tracks and disarm any armed tracks first.

At this point, I could go encourage you to enjoy your creative freedom and plow through some script ideas of your own.  You can totally do that, or you can read this post first.

Why do I encourage the latter?  Because, my friend, you are on the verge of having 3 fully functional scripts and you don’t even know it.  If you read the rest, you’ll have them made fast and learn some cool shit in the process!

Reusing existing code

If you’ve never programmed before, I hope at this point you’re stoked.  You should have at least one completed script, if not two (the “Hello, World!” script counts).  But as soon as you dig into writing more scripts – you’re going to find yourself getting frustrated as soon as you have to re-write that “loop through every track in my project” code again.

Most programming languages use functions to solve this problem.  You write a function in separate code, and “call it” (looking much like the reaper.CountTracks function) in the code you’re currently writing.  This is super efficient and cuts down on the busyness of writing the same code over and over.

Now that I’m introducing you to that idea, I’m going to teach you the totally wrong way to do it.  Why?  In this case, it’s a tad faster, and it’ll be easier for you to wrap your mind around as you see what’s going on visually.  I tell you about functions, though, because you should throw it in your Google machine and learn!  They’re super useful.

What you’re going to create

By reading this you’re going to turn your record arm script into two other scripts.  I’m only going to give you the code for one, and the other I’m going to encourage you to go through on your own (it’s the exact same code with parameter changes, don’t worry).

Your two new scripts will be:

  • Mute all selected tracks/Un-mute muted tracks
  • Solo all selected tracks/Un-solo solo’ed tracks

As you can see, the two scripts will follow the workflow of the one we just created but do different things.

Mute all selected tracks

Let’s look at our code from last time:

-- Get the total number of tracks in the active project
total_tracks = reaper.CountTracks(0)

-- Get the number of selected tracks in the project
number_of_tracks = reaper.CountSelectedTracks(0)

-- Set a boolean (true/false) variable to false
any_track_armed = false

--[[ Loop through all of the tracks in the project
 if we find one with the record arm set,
 set our any_track_armed variable to true to note that
 and un-arm the record on the track]]
for i = 0, total_tracks-1 do
 track = reaper.GetTrack(0,i)
 if reaper.GetMediaTrackInfo_Value(track, "I_RECARM") == 1 then
 any_track_armed = true
 reaper.SetMediaTrackInfo_Value(track, "I_RECARM", 0)
 end
end

--[[ If no tracks are armed to record, loop through the
 selected tracks and arm them all to record ]]
if any_track_armed == false then
 for i = 0, number_of_tracks-1 do
 track = reaper.GetSelectedTrack(0,i)
 reaper.SetMediaTrackInfo_Value(track, "I_RECARM", 1)
 end
end

What’s really cool here is the functions reaper.GetMediaTrackInfo_Value and reaper.SetMediaTrackInfo_Value both want parameters that can get or set various different things with a track.  That means if we edit the parameters, we have a brand new script.

“Save as” what you have, and rename it MuteSelectedTracks.lua

Now, I encourage you that if you have an idea of what you’re doing – give it a shot and try and make your script into a mute script without my help.  Otherwise, here’s the entire code again with what you need to edit bolded.  I’ll put an explanation below that:

-- Get the total number of tracks in the active project
total_tracks = reaper.CountTracks(0)

-- Get the number of selected tracks in the project
number_of_tracks = reaper.CountSelectedTracks(0)

-- Set a boolean (true/false) variable to false
any_track_armed = false

--[[ Loop through all of the tracks in the project
 if we find one with the mute set,
 set our any_track_armed variable to true to note that
 and mute the track]]
for i = 0, total_tracks-1 do
 track = reaper.GetTrack(0,i)
 if reaper.GetMediaTrackInfo_Value(track, "B_MUTE") == 1 then
 any_track_armed = true
 reaper.SetMediaTrackInfo_Value(track, "B_MUTE", 0)
 end
end

--[[ If no tracks are muted, loop through the
 selected tracks and mute them all ]]
if any_track_armed == false then
 for i = 0, number_of_tracks-1 do
 track = reaper.GetSelectedTrack(0,i)
 reaper.SetMediaTrackInfo_Value(track, "B_MUTE", 1)
 end
end

As you see – all you have to change is the string parameter and make it refer to muting instead of the record arm!  Otherwise, the number convention (1 means “turn the mute/record arm on”, 0 means “off) stays the exact same.

Pretty sweet right?

Going forward

So you should be able to do the same thing with soloing tracks.  If you look in the API, you’ll find that the parameter for solo is “I_SOLO”.  But dig deeper!  You should be able to turn this into even more scripts that manipulate phase, track color, track height, etc!

I hope you’ve learned a ton by reading this, and you’re super stoked to jump on more.  Remember, if you’ve got any questions – ping me on twitter @adamtcroft


Copyright 2016-2021, NIR LLC, all rights reserved.