1
00:00:00,139 --> 00:00:04,203
Hello everyone, this is X-Raym,
for the third part of the tutorial
2
00:00:04,204 --> 00:00:08,822
about Lua coding for
REAPER ReaScript.
3
00:00:08,823 --> 00:00:12,418
Today, we will work on Items.
4
00:00:12,419 --> 00:00:16,020
We will get infos of first selected items,
5
00:00:16,021 --> 00:00:19,795
and then, we will learn
how to modify them.
6
00:00:19,796 --> 00:00:23,978
We will start by the script
we wrote last time.
7
00:00:25,003 --> 00:00:32,441
I will set Notepad++ this way
so that it stay in foreground.
8
00:00:32,442 --> 00:00:37,501
Imagine that I want to get
the infos of that items.
9
00:00:38,786 --> 00:00:44,028
First, I have to return this item
in a variable.
10
00:00:45,037 --> 00:00:50,243
I check the API Doc,
and I write "Selected Item"
11
00:00:50,244 --> 00:00:54,750
We see that there are some
functions about Item Selection
12
00:00:55,770 --> 00:00:59,105
The function that we want
is this one.
13
00:00:59,106 --> 00:01:00,967
"GetSelectedMedialtem"
14
00:01:02,298 --> 00:01:08,830
If we add a project number ID
and an integer
15
00:01:08,831 --> 00:01:10,442
we get an Item.
16
00:01:11,703 --> 00:01:15,029
Project ID is 0 for current project.
17
00:01:15,030 --> 00:01:19,899
And the integer is the place
of the item in selection.
18
00:01:19,900 --> 00:01:25,096
If it is the first item,
the integer will be 0.
19
00:01:25,097 --> 00:01:30,928
Demo. I will create a function "Main"
20
00:01:30,929 --> 00:01:35,362
which will contain the core
of my script.
21
00:01:35,363 --> 00:01:38,223
Hop, I put the function end
22
00:01:38,224 --> 00:01:41,206
at the end of the script,
I call "Main".
23
00:01:41,207 --> 00:01:47,971
I move the call to that function below
so that it is more clean
24
00:01:47,972 --> 00:01:51,104
just before the call of Main.
25
00:01:51,105 --> 00:01:54,704
Remember, ShowConsoleMsg("")
is used to clean the console.
26
00:01:54,705 --> 00:02:01,444
I take this.
This function return an item.
27
00:02:01,445 --> 00:02:05,131
So I will stock this item in a variable.
28
00:02:05,132 --> 00:02:10,672
Item = reaper. GetSelectedMedialtem()
29
00:02:10,673 --> 00:02:15,648
Here I delete value type reference,
we don't need it.
30
00:02:15,649 --> 00:02:20,067
As first parameter,
0 for current project,
31
00:02:20,068 --> 00:02:27,248
and 0 as ineteger,
for First Selected Item.
32
00:02:29,177 --> 00:02:30,994
If I run the scrip...
33
00:02:34,048 --> 00:02:38,547
The console open because of
ShowConsoleMsg(""), but nothing happen.
34
00:02:39,882 --> 00:02:42,818
If I display item in the console...
35
00:02:45,264 --> 00:02:48,430
Hop, save, run..
36
00:02:48,431 --> 00:02:52,940
We see that the item
is a special type of data
37
00:02:52,941 --> 00:02:55,217
called userdata.
38
00:02:55,218 --> 00:02:59,392
Userdata in Lua represent an array value...
39
00:02:59,393 --> 00:03:03,870
but all you have to remember is that
40
00:03:03,871 --> 00:03:09,820
this userdata is specific to this item
the project with a unique ID.
41
00:03:09,821 --> 00:03:13,080
We will try to get infos on this item.
42
00:03:15,076 --> 00:03:20,704
I get back in function list,
And I enter Item Infos.
43
00:03:21,918 --> 00:03:26,069
I see that there is a function
GetMedialtemlnfo_Value
44
00:03:26,070 --> 00:03:28,774
and there is a variant for Take properties.
45
00:03:28,775 --> 00:03:32,750
We will focus on item.
So, I click on GetMedialtemlnfo_Value
46
00:03:32,751 --> 00:03:35,257
and I see this function.
47
00:03:35,258 --> 00:03:40,386
If I want to get the state of
Mute of the item.
48
00:03:42,493 --> 00:03:45,519
I add a breakline,
it is more readable.
49
00:03:45,520 --> 00:03:49,783
I add item_mute =
50
00:03:49,784 --> 00:03:51,486
and I copy this function.
51
00:03:55,032 --> 00:04:02,676
The item that we will set as parameter
is the item stored in item variable.
52
00:04:02,677 --> 00:04:06,445
I activate the automatic break lines
of Notepad++ so it is more readable...
53
00:04:07,792 --> 00:04:13,235
As second parameter, it asks for the
name of the type of value we want
54
00:04:13,236 --> 00:04:18,452
It has to be a string
so I write "".
55
00:04:18,452 --> 00:04:22,281
The parameter we want in our case
is called "B_Mute".
56
00:04:22,282 --> 00:04:25,088
I copy "B_Mute"
and I paste it here.
57
00:04:27,569 --> 00:04:34,038
I delete that...
I add a break line here...
58
00:04:39,180 --> 00:04:43,047
and I ask to the console
to display the value of item_mute.
59
00:04:44,986 --> 00:04:46,209
I get back here.
60
00:04:47,486 --> 00:04:49,089
I run the script.
61
00:04:49,090 --> 00:04:52,120
And we see that the value is 0.
62
00:04:52,121 --> 00:04:57,057
If I mute the item
and I run the script again...
63
00:04:57,058 --> 00:05:01,921
we see that when the item is muted,
the value displayed is one.
64
00:05:01,922 --> 00:05:07,674
In other words,
GetMedialtemlnfo_Value("B_Mute")
65
00:05:07,675 --> 00:05:12,406
return 1, when the item is muted.
66
00:05:12,407 --> 00:05:17,411
Another parameter very interesting
is the position.
67
00:05:18,417 --> 00:05:22,550
We will duplicate this line.
68
00:05:22,551 --> 00:05:25,643
We name this variable item_pos.
69
00:05:27,602 --> 00:05:30,653
We add the parameter "D_POS"
70
00:05:30,654 --> 00:05:35,970
and we will display the value
of this variable.
71
00:05:35,971 --> 00:05:39,917
So that we can see better,
I will add...
72
00:05:39,918 --> 00:05:45,471
"Item Mute =" ..
73
00:05:47,190 --> 00:05:50,957
And here "Item Position =" ..
74
00:05:59,080 --> 00:06:01,367
I save and run.
75
00:06:03,248 --> 00:06:08,040
It seems that I made a mistake somewhere...
76
00:06:08,041 --> 00:06:13,821
Oh I forgot to add points
to tell we merge two strings.
77
00:06:13,822 --> 00:06:15,635
I run the script again.
78
00:06:15,636 --> 00:06:19,879
We see Item Mute = 0 and
Item Position = 0
79
00:06:19,880 --> 00:06:25,642
Item Position = 0 because
I made a mistake here (one trailing space).
80
00:06:25,643 --> 00:06:32,298
We will have to be vigilent when coding one small
space at the wrong place can broke the script.
81
00:06:32,299 --> 00:06:33,789
I run the script again.
82
00:06:36,288 --> 00:06:41,391
Here we see the item position
in seconds.
83
00:06:41,392 --> 00:06:48,106
As you can see, it is very precise.
84
00:06:48,107 --> 00:06:51,473
If I want to display the duration,
I duplicate
85
00:06:52,723 --> 00:06:56,274
I get the parameter value here.
86
00:07:00,107 --> 00:07:01,759
I replace.
87
00:07:02,817 --> 00:07:06,255
I rename the variable.
88
00:07:07,672 --> 00:07:13,077
And I duplicate that too,
and display the item length.
89
00:07:15,475 --> 00:07:19,251
I save, I select the item
I run the script.
90
00:07:20,476 --> 00:07:24,506
We see that the item length
as the same value as the item pos
91
00:07:24,507 --> 00:07:30,218
because I forgot to copy paste
the variable here...
92
00:07:30,219 --> 00:07:32,408
VoilĂ . I run again.
93
00:07:35,009 --> 00:07:37,319
We will see that the
item length is 9 seconds.
94
00:07:37,320 --> 00:07:45,320
If I make it shorter, it display 5s,
if I extend, it displays 9 seconds.
95
00:07:46,162 --> 00:07:51,677
Another item parameter often used
by scripters is the volume.
96
00:07:51,678 --> 00:07:55,740
I will show you how it works,
because it is a bit special.
97
00:07:55,741 --> 00:07:58,465
I duplicate this.
98
00:07:58,466 --> 00:08:00,322
"D_VOL"
99
00:08:00,323 --> 00:08:07,028
Note that the variable can be named
with every name that you want.
100
00:08:09,560 --> 00:08:13,045
I duplicate this line.
101
00:08:16,218 --> 00:08:18,081
And I wrote item_vol here.
102
00:08:19,572 --> 00:08:23,081
If I execute the script, I can see that
item_vol = 1
103
00:08:23,082 --> 00:08:27,326
However, I see that item volume
is set to 0 dB.
104
00:08:28,336 --> 00:08:31,889
It is because the scale of the
vol parameter displayed in the console
105
00:08:31,890 --> 00:08:36,346
is completly different.
106
00:08:36,347 --> 00:08:41,341
The value returned bu "D_VOL"
is on a log scale.
107
00:08:42,479 --> 00:08:49,701
If item_vol = 0, it means that
that item volume is -inf dB
108
00:08:49,702 --> 00:08:55,105
If item_vol > 1, it means that
the item had a volume boost.
109
00:08:55,106 --> 00:08:59,694
I'll show you, if I set -7 dB,
and that I run the script again...
110
00:08:59,695 --> 00:09:02,807
We see that it is 0.4 (log scale)
111
00:09:02,808 --> 00:09:07,580
If I boost at 10 dB, it is equal to 3
(approx).
112
00:09:07,581 --> 00:09:12,429
2 on log scale is for 6 dB boost
(approx).
113
00:09:12,430 --> 00:09:16,254
To convert the log scale value
into dB
114
00:09:16,255 --> 00:09:18,557
We have to do some maths.
115
00:09:18,558 --> 00:09:23,018
It is a bit complicated but
we can use a code snippet
116
00:09:23,019 --> 00:09:30,528
a small code chunk that you can use
without having to write it from scratch
117
00:09:30,529 --> 00:09:34,557
You only have to understand
what variable you have to replace.
118
00:09:34,558 --> 00:09:38,531
Here, we just have to replace
some variable names.
119
00:09:38,532 --> 00:09:46,532
We will replace VolDB by item_voldB so
that it matches our other variable naming
120
00:09:46,712 --> 00:09:50,931
Here, the value to put
is the value of item_vol
121
00:09:52,214 --> 00:09:56,935
which is transform
by this formula.
122
00:09:56,936 --> 00:10:02,952
If I display item_volDB here...
123
00:10:02,953 --> 00:10:05,942
we can see that here is 0dB.
124
00:10:05,943 --> 00:10:10,249
And here is too. If I set to -7db
it displays -7 in the console.
125
00:10:10,250 --> 00:10:13,908
If I go to +6db it displays +6.
126
00:10:13,909 --> 00:10:19,449
There is other nice parameters,
127
00:10:19,450 --> 00:10:20,940
such as fades length,
128
00:10:22,410 --> 00:10:25,378
snap offset position,
129
00:10:25,379 --> 00:10:27,787
and color of the object.
130
00:10:27,788 --> 00:10:32,145
Now imagine that we want to
modify these values.
131
00:10:32,146 --> 00:10:38,197
There is a sister function of
GetMedialtemlnfo_Value which is...
132
00:10:38,198 --> 00:10:40,082
I'll show you...
133
00:10:43,251 --> 00:10:46,313
which is SetMedialtemlnfo_Value
134
00:10:46,314 --> 00:10:52,321
we can see that it look like the Get
version so that there a third parameter
135
00:10:52,322 --> 00:10:54,912
which is the new value we want
to set.
136
00:10:54,913 --> 00:10:56,472
I will copy that.
137
00:10:58,569 --> 00:10:59,569
Put it here...
138
00:11:00,980 --> 00:11:05,387
The item we want to modify
is still in the item variable.
139
00:11:05,388 --> 00:11:12,178
We will start by setting
the mute parameter.
140
00:11:15,096 --> 00:11:17,830
"B_MUTE"
141
00:11:17,831 --> 00:11:21,885
As new value, we set 0.
142
00:11:21,886 --> 00:11:23,241
If I run the script...
143
00:11:24,415 --> 00:11:25,946
Nothing happens.
144
00:11:25,947 --> 00:11:29,894
But if I zoom out,
or click, you see that
145
00:11:29,895 --> 00:11:32,185
the item has been unmuted.
146
00:11:32,186 --> 00:11:35,080
We have to add at the end of the script
147
00:11:35,081 --> 00:11:38,332
a simple function UpdateArrange
148
00:11:38,333 --> 00:11:42,490
which allows to update the arrange view
if some items have been modified.
149
00:11:42,491 --> 00:11:45,430
Now, if I change the script...
150
00:11:46,488 --> 00:11:49,707
You see that I don't need to click
151
00:11:50,907 --> 00:11:52,949
to refresh the arrange view
(and the item).
152
00:11:53,974 --> 00:11:55,615
Now if I want to modify...
153
00:11:57,467 --> 00:11:59,984
the position...
154
00:11:59,985 --> 00:12:03,548
I write a new position in seconds.
155
00:12:05,326 --> 00:12:09,618
I set "D_POSITION" as second parameter.
156
00:12:09,619 --> 00:12:14,360
And now my item should go to 1.5 second.
157
00:12:14,361 --> 00:12:17,100
The item is nicely placed at 1.5 s
in the project.
158
00:12:20,874 --> 00:12:22,862
If I set 3...
159
00:12:24,178 --> 00:12:25,474
it will go there...
160
00:12:25,475 --> 00:12:28,318
We can of course use calculations.
161
00:12:28,319 --> 00:12:30,730
5 + 10
162
00:12:30,731 --> 00:12:33,123
and it will go at position 15 s.
163
00:12:33,124 --> 00:12:35,436
To modify the volume,
164
00:12:35,437 --> 00:12:39,834
I will advice you to modify the
volume converted in dB.
165
00:12:39,835 --> 00:12:44,502
But you will have to set it back
to log to make the modification valid.
166
00:12:44,503 --> 00:12:49,069
If we want to add
+1 dB to our item
167
00:12:50,098 --> 00:12:53,000
We can do the following thing...
168
00:12:53,001 --> 00:12:54,387
And here I do...
169
00:12:55,989 --> 00:12:59,209
reaper. SeMedialtemlnfo_Value(item,
"D_VOL"...
170
00:13:03,370 --> 00:13:06,411
reaper. SeMedialtemlnfo_Value(item,
"D_VOL", item_volDB)
171
00:13:06,412 --> 00:13:12,276
It is still express into dB
we have to convert it in Log scale
172
00:13:12,277 --> 00:13:16,507
Here is the code snippet.
173
00:13:19,554 --> 00:13:21,231
We put it here.
174
00:13:22,495 --> 00:13:29,694
And we replace calc
the variable we have to change,
175
00:13:29,695 --> 00:13:35,448
by what we wanted (our dB value +1)
176
00:13:35,449 --> 00:13:39,064
I unmute the item,
and I run the script.
177
00:13:42,689 --> 00:13:48,138
The item goes to position 0,
its volume had a +1 dB boost,
178
00:13:48,139 --> 00:13:51,430
and it is not muted anymore.
If I run the script again...
179
00:13:51,431 --> 00:13:54,966
we will see that the item volume
gets another extra +1 dB boost.
180
00:13:54,967 --> 00:14:00,724
If I want to substract 1 dB,
I set - here, and when I run the script...
181
00:14:02,232 --> 00:14:03,621
The item volume goes down.
182
00:14:03,622 --> 00:14:08,760
In the next videos, we will see how to get the
parameter values of several selected items
183
00:14:08,761 --> 00:14:11,485
and how to modify them
one after the other.