Lua Scripting with REAPER 5 - Part. 3: Get/Set First Selected Items Properties
-
0:00 - 0:04Hello everyone, this is X-Raym,
for the third part of the tutorial -
0:04 - 0:09about Lua coding for
REAPER ReaScript. -
0:09 - 0:12Today, we will work on Items.
-
0:12 - 0:16We will get infos of first selected items,
-
0:16 - 0:20and then, we will learn
how to modify them. -
0:20 - 0:24We will start by the script
we wrote last time. -
0:25 - 0:32I will set Notepad++ this way
so that it stay in foreground. -
0:32 - 0:38Imagine that I want to get
the infos of that items. -
0:39 - 0:44First, I have to return this item
in a variable. -
0:45 - 0:50I check the API Doc,
and I write "Selected Item" -
0:50 - 0:55We see that there are some
functions about Item Selection -
0:56 - 0:59The function that we want
is this one. -
0:59 - 1:01"GetSelectedMedialtem"
-
1:02 - 1:09If we add a project number ID
and an integer -
1:09 - 1:10we get an Item.
-
1:12 - 1:15Project ID is 0 for current project.
-
1:15 - 1:20And the integer is the place
of the item in selection. -
1:20 - 1:25If it is the first item,
the integer will be 0. -
1:25 - 1:31Demo. I will create a function "Main"
-
1:31 - 1:35which will contain the core
of my script. -
1:35 - 1:38Hop, I put the function end
-
1:38 - 1:41at the end of the script,
I call "Main". -
1:41 - 1:48I move the call to that function below
so that it is more clean -
1:48 - 1:51just before the call of Main.
-
1:51 - 1:55Remember, ShowConsoleMsg("")
is used to clean the console. -
1:55 - 2:01I take this.
This function return an item. -
2:01 - 2:05So I will stock this item in a variable.
-
2:05 - 2:11Item = reaper. GetSelectedMedialtem()
-
2:11 - 2:16Here I delete value type reference,
we don't need it. -
2:16 - 2:20As first parameter,
0 for current project, -
2:20 - 2:27and 0 as ineteger,
for First Selected Item. -
2:29 - 2:31If I run the scrip...
-
2:34 - 2:39The console open because of
ShowConsoleMsg(""), but nothing happen. -
2:40 - 2:43If I display item in the console...
-
2:45 - 2:48Hop, save, run..
-
2:48 - 2:53We see that the item
is a special type of data -
2:53 - 2:55called userdata.
-
2:55 - 2:59Userdata in Lua represent an array value...
-
2:59 - 3:04but all you have to remember is that
-
3:04 - 3:10this userdata is specific to this item
the project with a unique ID. -
3:10 - 3:13We will try to get infos on this item.
-
3:15 - 3:21I get back in function list,
And I enter Item Infos. -
3:22 - 3:26I see that there is a function
GetMedialtemlnfo_Value -
3:26 - 3:29and there is a variant for Take properties.
-
3:29 - 3:33We will focus on item.
So, I click on GetMedialtemlnfo_Value -
3:33 - 3:35and I see this function.
-
3:35 - 3:40If I want to get the state of
Mute of the item. -
3:42 - 3:46I add a breakline,
it is more readable. -
3:46 - 3:50I add item_mute =
-
3:50 - 3:51and I copy this function.
-
3:55 - 4:03The item that we will set as parameter
is the item stored in item variable. -
4:03 - 4:06I activate the automatic break lines
of Notepad++ so it is more readable... -
4:08 - 4:13As second parameter, it asks for the
name of the type of value we want -
4:13 - 4:18It has to be a string
so I write "". -
4:18 - 4:22The parameter we want in our case
is called "B_Mute". -
4:22 - 4:25I copy "B_Mute"
and I paste it here. -
4:28 - 4:34I delete that...
I add a break line here... -
4:39 - 4:43and I ask to the console
to display the value of item_mute. -
4:45 - 4:46I get back here.
-
4:47 - 4:49I run the script.
-
4:49 - 4:52And we see that the value is 0.
-
4:52 - 4:57If I mute the item
and I run the script again... -
4:57 - 5:02we see that when the item is muted,
the value displayed is one. -
5:02 - 5:08In other words,
GetMedialtemlnfo_Value("B_Mute") -
5:08 - 5:12return 1, when the item is muted.
-
5:12 - 5:17Another parameter very interesting
is the position. -
5:18 - 5:23We will duplicate this line.
-
5:23 - 5:26We name this variable item_pos.
-
5:28 - 5:31We add the parameter "D_POS"
-
5:31 - 5:36and we will display the value
of this variable. -
5:36 - 5:40So that we can see better,
I will add... -
5:40 - 5:45"Item Mute =" ..
-
5:47 - 5:51And here "Item Position =" ..
-
5:59 - 6:01I save and run.
-
6:03 - 6:08It seems that I made a mistake somewhere...
-
6:08 - 6:14Oh I forgot to add points
to tell we merge two strings. -
6:14 - 6:16I run the script again.
-
6:16 - 6:20We see Item Mute = 0 and
Item Position = 0 -
6:20 - 6:26Item Position = 0 because
I made a mistake here (one trailing space). -
6:26 - 6:32We will have to be vigilent when coding one small
space at the wrong place can broke the script. -
6:32 - 6:34I run the script again.
-
6:36 - 6:41Here we see the item position
in seconds. -
6:41 - 6:48As you can see, it is very precise.
-
6:48 - 6:51If I want to display the duration,
I duplicate -
6:53 - 6:56I get the parameter value here.
-
7:00 - 7:02I replace.
-
7:03 - 7:06I rename the variable.
-
7:08 - 7:13And I duplicate that too,
and display the item length. -
7:15 - 7:19I save, I select the item
I run the script. -
7:20 - 7:25We see that the item length
as the same value as the item pos -
7:25 - 7:30because I forgot to copy paste
the variable here... -
7:30 - 7:32Voilà. I run again.
-
7:35 - 7:37We will see that the
item length is 9 seconds. -
7:37 - 7:45If I make it shorter, it display 5s,
if I extend, it displays 9 seconds. -
7:46 - 7:52Another item parameter often used
by scripters is the volume. -
7:52 - 7:56I will show you how it works,
because it is a bit special. -
7:56 - 7:58I duplicate this.
-
7:58 - 8:00"D_VOL"
-
8:00 - 8:07Note that the variable can be named
with every name that you want. -
8:10 - 8:13I duplicate this line.
-
8:16 - 8:18And I wrote item_vol here.
-
8:20 - 8:23If I execute the script, I can see that
item_vol = 1 -
8:23 - 8:27However, I see that item volume
is set to 0 dB. -
8:28 - 8:32It is because the scale of the
vol parameter displayed in the console -
8:32 - 8:36is completly different.
-
8:36 - 8:41The value returned bu "D_VOL"
is on a log scale. -
8:42 - 8:50If item_vol = 0, it means that
that item volume is -inf dB -
8:50 - 8:55If item_vol > 1, it means that
the item had a volume boost. -
8:55 - 9:00I'll show you, if I set -7 dB,
and that I run the script again... -
9:00 - 9:03We see that it is 0.4 (log scale)
-
9:03 - 9:08If I boost at 10 dB, it is equal to 3
(approx). -
9:08 - 9:122 on log scale is for 6 dB boost
(approx). -
9:12 - 9:16To convert the log scale value
into dB -
9:16 - 9:19We have to do some maths.
-
9:19 - 9:23It is a bit complicated but
we can use a code snippet -
9:23 - 9:31a small code chunk that you can use
without having to write it from scratch -
9:31 - 9:35You only have to understand
what variable you have to replace. -
9:35 - 9:39Here, we just have to replace
some variable names. -
9:39 - 9:47We will replace VolDB by item_voldB so
that it matches our other variable naming -
9:47 - 9:51Here, the value to put
is the value of item_vol -
9:52 - 9:57which is transform
by this formula. -
9:57 - 10:03If I display item_volDB here...
-
10:03 - 10:06we can see that here is 0dB.
-
10:06 - 10:10And here is too. If I set to -7db
it displays -7 in the console. -
10:10 - 10:14If I go to +6db it displays +6.
-
10:14 - 10:19There is other nice parameters,
-
10:19 - 10:21such as fades length,
-
10:22 - 10:25snap offset position,
-
10:25 - 10:28and color of the object.
-
10:28 - 10:32Now imagine that we want to
modify these values. -
10:32 - 10:38There is a sister function of
GetMedialtemlnfo_Value which is... -
10:38 - 10:40I'll show you...
-
10:43 - 10:46which is SetMedialtemlnfo_Value
-
10:46 - 10:52we can see that it look like the Get
version so that there a third parameter -
10:52 - 10:55which is the new value we want
to set. -
10:55 - 10:56I will copy that.
-
10:59 - 11:00Put it here...
-
11:01 - 11:05The item we want to modify
is still in the item variable. -
11:05 - 11:12We will start by setting
the mute parameter. -
11:15 - 11:18"B_MUTE"
-
11:18 - 11:22As new value, we set 0.
-
11:22 - 11:23If I run the script...
-
11:24 - 11:26Nothing happens.
-
11:26 - 11:30But if I zoom out,
or click, you see that -
11:30 - 11:32the item has been unmuted.
-
11:32 - 11:35We have to add at the end of the script
-
11:35 - 11:38a simple function UpdateArrange
-
11:38 - 11:42which allows to update the arrange view
if some items have been modified. -
11:42 - 11:45Now, if I change the script...
-
11:46 - 11:50You see that I don't need to click
-
11:51 - 11:53to refresh the arrange view
(and the item). -
11:54 - 11:56Now if I want to modify...
-
11:57 - 12:00the position...
-
12:00 - 12:04I write a new position in seconds.
-
12:05 - 12:10I set "D_POSITION" as second parameter.
-
12:10 - 12:14And now my item should go to 1.5 second.
-
12:14 - 12:17The item is nicely placed at 1.5 s
in the project. -
12:21 - 12:23If I set 3...
-
12:24 - 12:25it will go there...
-
12:25 - 12:28We can of course use calculations.
-
12:28 - 12:315 + 10
-
12:31 - 12:33and it will go at position 15 s.
-
12:33 - 12:35To modify the volume,
-
12:35 - 12:40I will advice you to modify the
volume converted in dB. -
12:40 - 12:45But you will have to set it back
to log to make the modification valid. -
12:45 - 12:49If we want to add
+1 dB to our item -
12:50 - 12:53We can do the following thing...
-
12:53 - 12:54And here I do...
-
12:56 - 12:59reaper. SeMedialtemlnfo_Value(item,
"D_VOL"... -
13:03 - 13:06reaper. SeMedialtemlnfo_Value(item,
"D_VOL", item_volDB) -
13:06 - 13:12It is still express into dB
we have to convert it in Log scale -
13:12 - 13:17Here is the code snippet.
-
13:20 - 13:21We put it here.
-
13:22 - 13:30And we replace calc
the variable we have to change, -
13:30 - 13:35by what we wanted (our dB value +1)
-
13:35 - 13:39I unmute the item,
and I run the script. -
13:43 - 13:48The item goes to position 0,
its volume had a +1 dB boost, -
13:48 - 13:51and it is not muted anymore.
If I run the script again... -
13:51 - 13:55we will see that the item volume
gets another extra +1 dB boost. -
13:55 - 14:01If I want to substract 1 dB,
I set - here, and when I run the script... -
14:02 - 14:04The item volume goes down.
-
14:04 - 14:09In the next videos, we will see how to get the
parameter values of several selected items -
14:09 - 14:11and how to modify them
one after the other.
- Title:
- Lua Scripting with REAPER 5 - Part. 3: Get/Set First Selected Items Properties
- Description:
-
In this tutorial, you will learn how to get and set properties of the first selected items.
Source Article :
http://extremraym.com/reascript-video-getset/Any feedback would be really appreciated ! :)
- Video Language:
- French
- Duration:
- 14:13