If you’ve been following along with my recent Reascript posts, we’ve officially gotten through the hard stuff and the boring stuff. You’ve officially written your first Reascript too, but it hasn’t exactly been useful to you.
Today we start changing all of that. Over the next few days, I’m going to walk you through a script that Record Arms all of your currently selected tracks, and if any tracks are already armed to record it will clear them all for you first.
Sweet right? First, we have to figure out how to record arm one track. We’ll build from there. That’s what we’ll work on today.
Note: before you get started I recommend you download and work in notepad++ if you’re in windows. it sucks less than regular notepad. when you save, type “.lua” at the end of your filename
Your first useful Reascript
We’re going to go through all sorts of crazy programming stuff to build out our sweet new Reascript (You’ll get the .lua file with the code all complete at the end too!), but today is only going to be two lines of code. Always start simple.
If you look at the Reaper API for a way to set the record arm of a track, you’ll find the function SetMediaTrackInfo_Value. Look at the API reference I’ve provided you if it looks confusing. Don’t worry, we’re going to use it in context here and you’ll figure it out.
This function looks great – but it leaves us with an issue we haven’t tackled yet. How do you get a track and give it to a function?
Programming with objects
This is actually a little more straightforward and easier than it seems for someone who has never programmed. Let’s assume you know what a variable is. Every variable in a programming language is classified by a “type” (think all guitar riffs are riffs, but some are metal riffs, some are rock riffs, some are blues riffs. All riffs are variables, but the metal riff is a type of riff).
Some types are pretty standard, such as:
- integer = number without a decimal
- float = number with a decimal
- boolean = true/false value
- string = a text value, “usually with quotes around it”
The neat thing about some kinds of programming is that the user can define types. In this case, a track is a type of variable. That makes things REALLY easy to work with.
The first line of our code
So we have the function which will act on our track and turn on the record arm, but we need to feed it a variable that represents the track that we have selected. How do we do that?
Take a look at the API again and find GetSelectedTrack. This function will give us a track if we give it two integers. The first one will be 0 for our active project, the 2nd integer refers to which track we want.When you use this function, Reaper knows how many tracks are selected and refers to them by a number, starting with zero.
When you use the GetSelectedTrack function, Reaper knows how many tracks are selected and refers to them by a number, starting with zero. So if you have 3 tracks selected, the topmost track in your timeline will be 0, the next selected track down will be 1, and the last selected track will be 2.
We’ll make this code get all of the tracks we want later. For now, we’ll write a line of code that tells Reaper to get the topmost selected track. That code is:
track = reaper.GetSelectedTrack(0,0)
We’re asking Reaper for the topmost track, and storing it in a variable named track. Done! Easy, right?
Setting the record arm
So now that we have a track stored, all we have to do is call the SetMediaTrackInfo_Value function on our track. That looks like this:
reaper.SetMediaTrackInfo_Value(track, "I_RECARM", 1)
Let me explain the parameters of this function in further detail —
track is the track variable we just stored in our previous line of code. We want Reaper to do something to this track.
“I_RECARM” is a string that the Reaper API gives us. It means that the thing we want to do to the aforementioned track is change the record arm.
1 is an integer value that the Reaper API gives us. 1 in this case means “turn on the record arm”, 0 would mean “turn off the record arm”.
And with that, we’re done!
The final code
Your code should now look like this:
track = reaper.GetSelectedTrack(0,0) reaper.SetMediaTrackInfo_Value(track, "I_RECARM", 1)
Save it as a .lua file in your text editor, and load it into Reaper via the actions list.
Once you’ve loaded it, select a track and try out your new script! It should arm the track you’ve selected. If you select multiple tracks, it will arm the topmost track of all the tracks you’ve selected.
Next time we’ll build on this code, and keep making it awesome!
If you have any questions – feel free to ping me.
Copyright 2016-2017, Adam T. Croft, all rights reserved.