0:00:00.139,0:00:04.203
Hello everyone, this is X-Raym,[br]for the third part of the tutorial
0:00:04.204,0:00:08.822
about Lua coding for[br]REAPER ReaScript.
0:00:08.823,0:00:12.418
Today, we will work on Items.
0:00:12.419,0:00:16.020
We will get infos of first selected items,
0:00:16.021,0:00:19.795
and then, we will learn[br]how to modify them.
0:00:19.796,0:00:23.978
We will start by the script[br]we wrote last time.
0:00:25.003,0:00:32.441
I will set Notepad++ this way[br]so that it stay in foreground.
0:00:32.442,0:00:37.501
Imagine that I want to get[br]the infos of that items.
0:00:38.786,0:00:44.028
First, I have to return this item[br]in a variable.
0:00:45.037,0:00:50.243
I check the API Doc,[br]and I write "Selected Item"
0:00:50.244,0:00:54.750
We see that there are some[br]functions about Item Selection
0:00:55.770,0:00:59.105
The function that we want[br]is this one.
0:00:59.106,0:01:00.967
"GetSelectedMedialtem"
0:01:02.298,0:01:08.830
If we add a project number ID[br]and an integer
0:01:08.831,0:01:10.442
we get an Item.
0:01:11.703,0:01:15.029
Project ID is 0 for current project.
0:01:15.030,0:01:19.899
And the integer is the place[br]of the item in selection.
0:01:19.900,0:01:25.096
If it is the first item,[br]the integer will be 0.
0:01:25.097,0:01:30.928
Demo. I will create a function "Main"
0:01:30.929,0:01:35.362
which will contain the core[br]of my script.
0:01:35.363,0:01:38.223
Hop, I put the function end
0:01:38.224,0:01:41.206
at the end of the script,[br]I call "Main".
0:01:41.207,0:01:47.971
I move the call to that function below[br]so that it is more clean
0:01:47.972,0:01:51.104
just before the call of Main.
0:01:51.105,0:01:54.704
Remember, ShowConsoleMsg("")[br]is used to clean the console.
0:01:54.705,0:02:01.444
I take this.[br]This function return an item.
0:02:01.445,0:02:05.131
So I will stock this item in a variable.
0:02:05.132,0:02:10.672
Item = reaper. GetSelectedMedialtem()
0:02:10.673,0:02:15.648
Here I delete value type reference,[br]we don't need it.
0:02:15.649,0:02:20.067
As first parameter,[br]0 for current project,
0:02:20.068,0:02:27.248
and 0 as ineteger,[br]for First Selected Item.
0:02:29.177,0:02:30.994
If I run the scrip...
0:02:34.048,0:02:38.547
The console open because of[br]ShowConsoleMsg(""), but nothing happen.
0:02:39.882,0:02:42.818
If I display item in the console...
0:02:45.264,0:02:48.430
Hop, save, run..
0:02:48.431,0:02:52.940
We see that the item[br]is a special type of data
0:02:52.941,0:02:55.217
called userdata.
0:02:55.218,0:02:59.392
Userdata in Lua represent an array value...
0:02:59.393,0:03:03.870
but all you have to remember is that
0:03:03.871,0:03:09.820
this userdata is specific to this item[br]the project with a unique ID.
0:03:09.821,0:03:13.080
We will try to get infos on this item.
0:03:15.076,0:03:20.704
I get back in function list,[br]And I enter Item Infos.
0:03:21.918,0:03:26.069
I see that there is a function[br]GetMedialtemlnfo_Value
0:03:26.070,0:03:28.774
and there is a variant for Take properties.
0:03:28.775,0:03:32.750
We will focus on item.[br]So, I click on GetMedialtemlnfo_Value
0:03:32.751,0:03:35.257
and I see this function.
0:03:35.258,0:03:40.386
If I want to get the state of[br]Mute of the item.
0:03:42.493,0:03:45.519
I add a breakline,[br]it is more readable.
0:03:45.520,0:03:49.783
I add item_mute =
0:03:49.784,0:03:51.486
and I copy this function.
0:03:55.032,0:04:02.676
The item that we will set as parameter[br]is the item stored in item variable.
0:04:02.677,0:04:06.445
I activate the automatic break lines[br]of Notepad++ so it is more readable...
0:04:07.792,0:04:13.235
As second parameter, it asks for the[br]name of the type of value we want
0:04:13.236,0:04:18.452
It has to be a string[br]so I write "".
0:04:18.452,0:04:22.281
The parameter we want in our case[br]is called "B_Mute".
0:04:22.282,0:04:25.088
I copy "B_Mute"[br]and I paste it here.
0:04:27.569,0:04:34.038
I delete that...[br]I add a break line here...
0:04:39.180,0:04:43.047
and I ask to the console[br]to display the value of item_mute.
0:04:44.986,0:04:46.209
I get back here.
0:04:47.486,0:04:49.089
I run the script.
0:04:49.090,0:04:52.120
And we see that the value is 0.
0:04:52.121,0:04:57.057
If I mute the item[br]and I run the script again...
0:04:57.058,0:05:01.921
we see that when the item is muted,[br]the value displayed is one.
0:05:01.922,0:05:07.674
In other words,[br]GetMedialtemlnfo_Value("B_Mute")
0:05:07.675,0:05:12.406
return 1, when the item is muted.
0:05:12.407,0:05:17.411
Another parameter very interesting[br]is the position.
0:05:18.417,0:05:22.550
We will duplicate this line.
0:05:22.551,0:05:25.643
We name this variable item_pos.
0:05:27.602,0:05:30.653
We add the parameter "D_POS"
0:05:30.654,0:05:35.970
and we will display the value[br]of this variable.
0:05:35.971,0:05:39.917
So that we can see better,[br]I will add...
0:05:39.918,0:05:45.471
"Item Mute =" ..
0:05:47.190,0:05:50.957
And here "Item Position =" ..
0:05:59.080,0:06:01.367
I save and run.
0:06:03.248,0:06:08.040
It seems that I made a mistake somewhere...
0:06:08.041,0:06:13.821
Oh I forgot to add points[br]to tell we merge two strings.
0:06:13.822,0:06:15.635
I run the script again.
0:06:15.636,0:06:19.879
We see Item Mute = 0 and[br]Item Position = 0
0:06:19.880,0:06:25.642
Item Position = 0 because[br]I made a mistake here (one trailing space).
0:06:25.643,0:06:32.298
We will have to be vigilent when coding one small[br]space at the wrong place can broke the script.
0:06:32.299,0:06:33.789
I run the script again.
0:06:36.288,0:06:41.391
Here we see the item position[br]in seconds.
0:06:41.392,0:06:48.106
As you can see, it is very precise.
0:06:48.107,0:06:51.473
If I want to display the duration,[br]I duplicate
0:06:52.723,0:06:56.274
I get the parameter value here.
0:07:00.107,0:07:01.759
I replace.
0:07:02.817,0:07:06.255
I rename the variable.
0:07:07.672,0:07:13.077
And I duplicate that too,[br]and display the item length.
0:07:15.475,0:07:19.251
I save, I select the item[br]I run the script.
0:07:20.476,0:07:24.506
We see that the item length[br]as the same value as the item pos
0:07:24.507,0:07:30.218
because I forgot to copy paste[br]the variable here...
0:07:30.219,0:07:32.408
VoilĂ . I run again.
0:07:35.009,0:07:37.319
We will see that the[br]item length is 9 seconds.
0:07:37.320,0:07:45.320
If I make it shorter, it display 5s,[br]if I extend, it displays 9 seconds.
0:07:46.162,0:07:51.677
Another item parameter often used[br]by scripters is the volume.
0:07:51.678,0:07:55.740
I will show you how it works,[br]because it is a bit special.
0:07:55.741,0:07:58.465
I duplicate this.
0:07:58.466,0:08:00.322
"D_VOL"
0:08:00.323,0:08:07.028
Note that the variable can be named[br]with every name that you want.
0:08:09.560,0:08:13.045
I duplicate this line.
0:08:16.218,0:08:18.081
And I wrote item_vol here.
0:08:19.572,0:08:23.081
If I execute the script, I can see that[br]item_vol = 1
0:08:23.082,0:08:27.326
However, I see that item volume[br]is set to 0 dB.
0:08:28.336,0:08:31.889
It is because the scale of the[br]vol parameter displayed in the console
0:08:31.890,0:08:36.346
is completly different.
0:08:36.347,0:08:41.341
The value returned bu "D_VOL"[br]is on a log scale.
0:08:42.479,0:08:49.701
If item_vol = 0, it means that[br]that item volume is -inf dB
0:08:49.702,0:08:55.105
If item_vol > 1, it means that[br]the item had a volume boost.
0:08:55.106,0:08:59.694
I'll show you, if I set -7 dB,[br]and that I run the script again...
0:08:59.695,0:09:02.807
We see that it is 0.4 (log scale)
0:09:02.808,0:09:07.580
If I boost at 10 dB, it is equal to 3[br](approx).
0:09:07.581,0:09:12.429
2 on log scale is for 6 dB boost[br](approx).
0:09:12.430,0:09:16.254
To convert the log scale value[br]into dB
0:09:16.255,0:09:18.557
We have to do some maths.
0:09:18.558,0:09:23.018
It is a bit complicated but[br]we can use a code snippet
0:09:23.019,0:09:30.528
a small code chunk that you can use[br]without having to write it from scratch
0:09:30.529,0:09:34.557
You only have to understand[br]what variable you have to replace.
0:09:34.558,0:09:38.531
Here, we just have to replace[br]some variable names.
0:09:38.532,0:09:46.532
We will replace VolDB by item_voldB so[br]that it matches our other variable naming
0:09:46.712,0:09:50.931
Here, the value to put[br]is the value of item_vol
0:09:52.214,0:09:56.935
which is transform[br]by this formula.
0:09:56.936,0:10:02.952
If I display item_volDB here...
0:10:02.953,0:10:05.942
we can see that here is 0dB.
0:10:05.943,0:10:10.249
And here is too. If I set to -7db[br]it displays -7 in the console.
0:10:10.250,0:10:13.908
If I go to +6db it displays +6.
0:10:13.909,0:10:19.449
There is other nice parameters,
0:10:19.450,0:10:20.940
such as fades length,
0:10:22.410,0:10:25.378
snap offset position,
0:10:25.379,0:10:27.787
and color of the object.
0:10:27.788,0:10:32.145
Now imagine that we want to[br]modify these values.
0:10:32.146,0:10:38.197
There is a sister function of[br]GetMedialtemlnfo_Value which is...
0:10:38.198,0:10:40.082
I'll show you...
0:10:43.251,0:10:46.313
which is SetMedialtemlnfo_Value
0:10:46.314,0:10:52.321
we can see that it look like the Get[br]version so that there a third parameter
0:10:52.322,0:10:54.912
which is the new value we want[br]to set.
0:10:54.913,0:10:56.472
I will copy that.
0:10:58.569,0:10:59.569
Put it here...
0:11:00.980,0:11:05.387
The item we want to modify[br]is still in the item variable.
0:11:05.388,0:11:12.178
We will start by setting[br]the mute parameter.
0:11:15.096,0:11:17.830
"B_MUTE"
0:11:17.831,0:11:21.885
As new value, we set 0.
0:11:21.886,0:11:23.241
If I run the script...
0:11:24.415,0:11:25.946
Nothing happens.
0:11:25.947,0:11:29.894
But if I zoom out,[br]or click, you see that
0:11:29.895,0:11:32.185
the item has been unmuted.
0:11:32.186,0:11:35.080
We have to add at the end of the script
0:11:35.081,0:11:38.332
a simple function UpdateArrange
0:11:38.333,0:11:42.490
which allows to update the arrange view[br]if some items have been modified.
0:11:42.491,0:11:45.430
Now, if I change the script...
0:11:46.488,0:11:49.707
You see that I don't need to click
0:11:50.907,0:11:52.949
to refresh the arrange view[br](and the item).
0:11:53.974,0:11:55.615
Now if I want to modify...
0:11:57.467,0:11:59.984
the position...
0:11:59.985,0:12:03.548
I write a new position in seconds.
0:12:05.326,0:12:09.618
I set "D_POSITION" as second parameter.
0:12:09.619,0:12:14.360
And now my item should go to 1.5 second.
0:12:14.361,0:12:17.100
The item is nicely placed at 1.5 s[br]in the project.
0:12:20.874,0:12:22.862
If I set 3...
0:12:24.178,0:12:25.474
it will go there...
0:12:25.475,0:12:28.318
We can of course use calculations.
0:12:28.319,0:12:30.730
5 + 10
0:12:30.731,0:12:33.123
and it will go at position 15 s.
0:12:33.124,0:12:35.436
To modify the volume,
0:12:35.437,0:12:39.834
I will advice you to modify the[br]volume converted in dB.
0:12:39.835,0:12:44.502
But you will have to set it back[br]to log to make the modification valid.
0:12:44.503,0:12:49.069
If we want to add[br]+1 dB to our item
0:12:50.098,0:12:53.000
We can do the following thing...
0:12:53.001,0:12:54.387
And here I do...
0:12:55.989,0:12:59.209
reaper. SeMedialtemlnfo_Value(item,[br]"D_VOL"...
0:13:03.370,0:13:06.411
reaper. SeMedialtemlnfo_Value(item,[br]"D_VOL", item_volDB)
0:13:06.412,0:13:12.276
It is still express into dB[br]we have to convert it in Log scale
0:13:12.277,0:13:16.507
Here is the code snippet.
0:13:19.554,0:13:21.231
We put it here.
0:13:22.495,0:13:29.694
And we replace calc[br]the variable we have to change,
0:13:29.695,0:13:35.448
by what we wanted (our dB value +1)
0:13:35.449,0:13:39.064
I unmute the item,[br]and I run the script.
0:13:42.689,0:13:48.138
The item goes to position 0,[br]its volume had a +1 dB boost,
0:13:48.139,0:13:51.430
and it is not muted anymore.[br]If I run the script again...
0:13:51.431,0:13:54.966
we will see that the item volume[br]gets another extra +1 dB boost.
0:13:54.967,0:14:00.724
If I want to substract 1 dB,[br]I set - here, and when I run the script...
0:14:02.232,0:14:03.621
The item volume goes down.
0:14:03.622,0:14:08.760
In the next videos, we will see how to get the[br]parameter values of several selected items
0:14:08.761,0:14:11.485
and how to modify them[br]one after the other.